Skip to content

codegen: attribute target selection to whoever chose the target - #416

Merged
nihalpasham merged 2 commits into
NVIDIA:mainfrom
alejandro-soto-franco:pr/target-arch-provenance
Jul 24, 2026
Merged

nihalpasham merged 2 commits into
NVIDIA:mainfrom
alejandro-soto-franco:pr/target-arch-provenance

Conversation

@alejandro-soto-franco

@alejandro-soto-franco alejandro-soto-franco commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

One of three parts of #357, split so each can be reviewed on its own. #357 bundled eight unrelated fixes across 12 files, which put the small ones behind a dependency discussion. The three parts are independent: each applies cleanly to main alone, and they merge cleanly in any order. #357 will be closed once all three are open.

Sibling parts: #415 (clone, liveness and diagnostic paths), #417 (scratch directory on tempfile).

Summary

A rejected target produced an error naming CUDA_OXIDE_TARGET, and classified the failure as a codegen stage failure. Both are wrong for every caller that reaches target selection by some other route.

Changes

  • BackendOptions carries target_arch_source alongside target_arch, and resolve_ptx_target reports the label it was given.
  • PipelineConfig carries the same label, defaulting to PipelineConfig::target_arch and set to CUDA_OXIDE_TARGET by rustc-codegen-cuda, which reads that variable itself.
  • The environment and config merge moves into backend_options_for, so the precedence is testable without running a compilation.
  • Target and feature-floor rejections travel as PipelineError::TargetSelection and land in CompilationStage::Input.
  • CompileError gains a matching TargetSelection variant.
  • New tests/target_provenance.rs, plus three mir-importer tests over the label precedence.

Rationale

resolve_ptx_target hardcoded "CUDA_OXIDE_TARGET" as the provenance of any explicit override, because the env-driven rustc pipeline was once the only caller. The standalone experimental API passes a Target the caller built in Rust and never touches the environment, so a caller who requested sm_75 for a module needing sm_80 was told to go and look at an environment variable they had never set.

The same mis-attribution existed one layer up: PipelineConfig::target_arch replaced target_arch and left from_env's label behind, so a caller setting the config field directly was still blamed on the environment. That is fixed here too, which is the only in-tree site that needed the new mechanism.

Target and feature-floor rejections are decided before llc is reached and describe the caller's request, so Codegen was the wrong stage for them.

CompileError::TargetSelection is a new variant rather than a reuse of InvalidTarget. InvalidTarget renders as invalid CUDA target `{target}`: {reason}, which suits a parse failure from Target::parse and misdescribes a feature-floor rejection: sm_75 is a valid architecture, and the reason text already names it, so routing feature-floor failures through InvalidTarget called a valid target invalid and printed the architecture twice. TargetSelection renders its reason alone, which is already phrased for a user. InvalidTarget keeps its parse-failure meaning and its rustdoc now points at the new variant.

target_arch_source stays &'static str. Every label in the tree is a fixed string. Widening it to Cow<'static, str>, which would let a caller build a label carrying the target text itself, can wait until a caller needs one.

Validation

  • cargo check --workspace --all-targets
  • cargo test -p cuda-oxide-codegen (123 passed)
  • cargo test -p mir-importer (804 passed)
  • cargo clippy --workspace -- -D warnings
  • cargo clippy -p mir-importer --all-targets -- -D warnings
  • cargo fmt --all -- --check
  • (cd crates/rustc-codegen-cuda && cargo fmt --all -- --check)
  • (cd crates/rustc-codegen-cuda && cargo check), since this adds a field to a PipelineConfig literal in device_codegen.rs and that crate is excluded from the workspace
  • RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --workspace

A rejected target produced an error naming CUDA_OXIDE_TARGET, and classified
the failure as a codegen stage failure. Both were wrong for every caller
that reaches target selection by some other route.

resolve_ptx_target hardcoded "CUDA_OXIDE_TARGET" as the provenance of any
explicit override, because the env-driven rustc pipeline was once the only
caller. The standalone experimental API passes a Target the caller built in
Rust and never touches the environment, so a caller who requested sm_75 for
a module needing sm_80 was told to go and look at an environment variable
they had never set. BackendOptions now carries target_arch_source alongside
target_arch, and resolve_ptx_target reports the label it was given.
mir-importer's PipelineConfig carries the same label, defaulting to
PipelineConfig::target_arch and set to CUDA_OXIDE_TARGET by
rustc-codegen-cuda, which reads that variable itself. Previously the
config-level override replaced target_arch and left from_env's label behind,
which reproduced the same mis-attribution one layer up.

The merge of environment and config options moves into backend_options_for
so this precedence is testable without running a compilation.

Target and feature-floor rejections were reported as
PipelineError::PtxGeneration, so the standalone API mapped them to
CompilationStage::Codegen. They are decided before llc is reached and
describe the caller's request, so they now travel as
PipelineError::TargetSelection and land in CompilationStage::Input.

CompileError gains a matching TargetSelection variant. InvalidTarget renders
as "invalid CUDA target `{target}`: {reason}", which suits a parse failure
from Target::parse and misdescribes a feature-floor rejection: sm_75 is a
valid architecture, and the reason text already names it, so routing
feature-floor failures through InvalidTarget called a valid target invalid
and printed the architecture twice. TargetSelection renders its reason alone,
which is already phrased for a user. InvalidTarget keeps its parse-failure
meaning and its rustdoc now points at the new variant.

target_arch_source stays &'static str. Every label in the tree is a fixed
string. Widening it to Cow<'static, str>, which would let a caller build a
label carrying the target text itself, can wait until a caller needs one.

Signed-off-by: Alejandro Soto Franco <[email protected]>
The provenance label was threaded through but only printed on the
verbose success path; rejected targets still gave env/--arch users no
pointer to the knob that chose the target. Interpolate it into the
parse-failure, feature-floor, and generated-floor reasons, and stop
double-wrapping the parser's own 'invalid CUDA target' prefix.
@nihalpasham nihalpasham added bug Something isn't working QoL-improvement Quality-of-life improvement; not a correctness fix codegen Device code-generation pipeline (Rust MIR to IR to PTX) interop Interop with external compilers / frontends labels Jul 24, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator

LGTM, the Input-stage classification and the InvalidTarget/TargetSelection split match how nvrtc separates INVALID_OPTION from COMPILATION.

I pushed one follow-up: the provenance label was threaded through but only printed on the verbose success path, so env and --arch users lost the pointer to the knob that chose the target. All three rejection reasons now carry it, e.g. invalid CUDA target sm_9x: compute capability must contain at least two digits (target from CUDA_OXIDE_TARGET), and the parse error is no longer double-wrapped in a second "invalid CUDA target" prefix. Unit test pins both.

One follow-up to track separately: the NVVM-IR path's resolver still reports target rejections as Export-stage. Merging.

@nihalpasham nihalpasham left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Validated; see merge comment.

@nihalpasham
nihalpasham merged commit 2e131b5 into NVIDIA:main Jul 24, 2026
33 checks passed
nihalpasham pushed a commit that referenced this pull request Jul 27, 2026
The NVVM IR resolver returned PipelineError::Export for every failure,
so a target the caller requested and the compiler refused was reported
as an export failure. The PTX resolver already routes the same
rejections through TargetSelection, which reaches the caller as
CompilationStage::Input.

Classify the rejections the NVVM IR route attributes to a target the
same way: an unparsable explicit target or device hint, a target below
a detected feature floor, a target below a generated intrinsic floor,
and the case where nothing supplied a target at all.

Failures that belong to the export keep reporting as Export.
select_target_with_generated fails when no target satisfies the module,
which the PTX resolver also classifies away from Input, and the PTX ISA
catalogue floor is a compiler capability limit decided before any
target is chosen.

Follow-up to #416.

Signed-off-by: Alejandro Soto Franco <[email protected]>
@alejandro-soto-franco
alejandro-soto-franco deleted the pr/target-arch-provenance branch September 9, 2026 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working codegen Device code-generation pipeline (Rust MIR to IR to PTX) interop Interop with external compilers / frontends QoL-improvement Quality-of-life improvement; not a correctness fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants