Skip to content

Parse CUDA targets once and thread CudaArch through codegen #1183

Closed

Description

@almightychang

Summary

PR #1105 gives CUDA target grammar and PTX-floor policy one dependency-free authority, cuda-target-spec, but target values still flow through codegen mostly as &str/String. I propose parsing each external target once into CudaArch and passing that value through internal APIs.

This is “parse, don't validate”: CudaArch carries proof that the grammar was accepted, while a boolean or string does not. In categorical terms, it is the normalized representation of the valid subobject of raw target spellings (sm_80 and compute_80 map to the same architecture). Parsing is the mediating arrow into that representation.

Current flow

      Str ──parse (partial)──▶ CudaArch ──floor (partial)──▶ u16
       ▲                          │
       └──────── render ─────────┘        law: parse ∘ render = id

  Today, consumers holding an already-proven value still take the loop:

  CudaArch ──render──▶ Str ──parse──▶ CudaArch      (retraction recomputed)


  boundaries                    internal consumers — each spoke re-parses
  ─────────────                 ──────────────────────────────────────────
  CLI / env ─────┐
  device hint ───┼───▶ ((Str)) ──parse──▶ resolver ──returns String──┐
  catalog ───────┘        ▲                                          │
                          └──────────────────────────────────────────┘
                          │
                          ├──parse──▶ arch_satisfies ────▶ bool   ← witness discarded
                          ├──parse──▶ LLVM support gate
                          ├──parse──▶ final-IR check
                          └──parse──▶ PTX-floor / feature selection

  the same target string is parsed 3+ times along one production path

Concrete examples:

  • An explicit override is parsed at target.rs:1810-1818. validate_target_features then renders it with sm(), and arch_satisfies parses it again (target.rs:1770-1775, 1653-1658, 1929-1934). Generated-target validation gets another rendered string (target.rs:1825-1831).
  • Resolution returns String (target.rs:1802-1808). The production path subsequently checks it during requirement merge (ptx.rs:396-398), parses it for the LLVM gate (ptx.rs:409-410; target.rs:1878-1884), parses it after final IR analysis (ptx.rs:491-500), and parses it again for PTX feature selection (ptx.rs:534-536; target.rs:1850-1860).
  • is_known_cuda_target formats capability/suffix, parses the result, then returns only bool (target.rs:1759-1767). This is boolean blindness: the successful parse witness is discarded, so later code must reconstruct it.

These are separate factorizations through the string object. For render: CudaArch -> Str, parsing a rendered value should recover the same architecture; repeatedly doing so recomputes that retraction instead of retaining the value already proved valid.

Proposal

  CLI / env ──parse──┐                     ┌──▶ target selection
                     │                     ├──▶ requirement checks
  device hint ─parse─┼──▶    CudaArch    ──┼──▶ LLVM support gate
                     │  (proof-carrying)   ├──▶ feature checks
  catalog ────parse──┘         │           └──▶ recorded floor ⇀ u16
   (partial: once,             │                 (partial: fails only on
    at the boundary)           │                  a missing policy entry)
                               │
                               └──render──▶ output boundaries only
                                            (llc args, diagnostics, evidence)

All internal consumers should factor through the boundary parse arrow, so the diagram commutes by construction. In plain engineering terms: one parsing decision produces one value shared by every later check. Partiality stays at input boundaries and at recorded-floor lookup, which remains fail-closed because valid grammar does not imply a recorded policy entry.

Illustrative signatures:

fn arch_satisfies(arch: &CudaArch, features: DetectedFeatures) -> bool;
fn required_ptx_feature(arch: &CudaArch, requirement: PtxIsaRequirement)
    -> Result<Option<&'static str>, String>;
fn validate_target_for_llvm_major(arch: &CudaArch, llc_major: Option<u32>)
    -> Result<(), String>;

Target resolution would return CudaArch plus provenance. Rendering would be limited to command arguments, diagnostics, and serialized evidence.

Why now?

CudaArch already exists, has no dependencies, and both consumers link the shared crate. PR #1105 also exposed two format-then-reparse symptoms: 787e256c does this in is_known_cuda_target, and 17d02112 converts CatalogHardwareAlternative through describe_stage_hardware before parsing it for a recorded floor.

Scope

Internal refactor only: no intended behavior, public CLI/API, canonical spelling, or probe-evidence format change. Target grammar, PTX-floor policy, target preference, and LLVM support policy are out of scope.

Using centralized &str validators keeps losing the proof at each call. A separate ValidatedTarget newtype is useful only if it proves more than successful parsing; otherwise CudaArch should be the proof-carrying type.

Open questions

  • Should option/env/device parsing live in option construction or target resolution?
  • Should codegen keep using libnvvm-sys's CudaArch re-export or import cuda-target-spec directly?
  • One cross-module migration, or temporary string adapters per module?

This issue seeks agreement on those boundaries before implementation.

Activity

  1. nihalpasham commented on Aug 30, 2026

    @nihalpasham
    Collaborator

    Implemented by #1184 (merged as bad4d05): one typed target vocabulary in cuda-target-spec, parsed once at the boundary, with a 612/612 byte-identical artifact proof across the full example corpus that the refactor changed no behavior. The compute_XX device-hint question it deliberately left open lives in #1189.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions