Skip to content

codegen: harden the standalone experimental backend (follow-up to #314) - #357

Closed
alejandro-soto-franco wants to merge 1 commit into
NVIDIA:mainfrom
alejandro-soto-franco:fix/codegen-extraction-review
Closed

alejandro-soto-franco wants to merge 1 commit into
NVIDIA:mainfrom
alejandro-soto-franco:fix/codegen-extraction-review

Conversation

@alejandro-soto-franco

Copy link
Copy Markdown
Contributor

Follow-up to #314 (the cuda-oxide-codegen rustc-independent MIR→PTX extraction, now merged). This gathers the fixes from a full review of that merged PR. No behaviour change for the existing env-driven rustc pipeline; the work hardens the standalone experimental Compiler/CompileOptions API and its diagnostics.

Correctness

  • No IR leak on panic. Compiler::compile clones the caller's module and runs the destructive pipeline on the clone. The previous "compile, then erase" sequence skipped the erase if compile_clone panicked, permanently leaking the clone in the shared Context arena. The erase now runs from an RAII EraseGuard, so it fires on every exit path including an unwinding panic. Covered by a new unit test.
  • tempfile floor. ScratchDirectory now uses tempfile::Builder with a 0o700 mode via Builder::permissions, which first shipped in tempfile 3.9. The workspace floor is raised 3.3 → 3.9 accordingly; a downstream consumer resolving 3.3–3.8 would otherwise fail to compile.

Diagnostics and error provenance

  • Standalone API no longer blames a legacy env var. Target/feature-floor validation failures from the standalone API used to reference CUDA_OXIDE_TARGET, which those callers never touch, and were classified as CompilationStage::Codegen. They now carry the caller's own provenance and are classified as input errors. BackendOptions gains a target_arch_source label so the env-driven rustc pipeline and the standalone API each describe where their target came from; a new PipelineError::TargetSelection distinguishes "bad requested target" from "llc failed".
  • Explicit Toolchain path keeps its diagnostics. The explicit-toolchain compile path discarded pipeline diagnostics, and CompileOptions had no way to request verbose output through the public surface. CompileOptions gains with_verbose/verbose, and Toolchain::from_paths now reports the tools it selected instead of always returning empty diagnostics.

API and cleanup

  • Compiler::compile_owned. Single-use callers can now consume the module and skip the defensive clone that compile always pays for, running passes directly on the owned IR. Useful for large, one-shot modules.
  • Deduplicated module-liveness check shared by mark_kernel_entry and Compiler::compile into one helper.
  • Documented the double LLVM-IR render on the libdevice path: the preview (pre-legalization) and final (post-legalization, different export config) renders are of genuinely different module states, so caching one for the other would be unsound. A real fix needs feature detection to move off rendered text entirely; this documents the constraint rather than attempting an unsound shortcut.

Testing

  • cargo test -p cuda-oxide-codegen — 75 passed across 5 suites, including new tests for the panic-safe erase guard and the source-label threading in resolve_ptx_target.
  • cargo clippy -p cuda-oxide-codegen --all-targets — clean.

Follow-up to NVIDIA#314's cuda-oxide-codegen extraction. Fixes from a full review
of the merged PR:

- Compiler::compile no longer leaks the cloned IR if compile_clone panics;
  the erase now runs from an RAII guard instead of a plain post-call.
- Add Compiler::compile_owned for single-use callers, so the module clone
  Compiler::compile always pays for is opt-in rather than unconditional.
- Target/feature-floor validation failures from the standalone API no
  longer reference the legacy CUDA_OXIDE_TARGET env var they never touched,
  and are classified as CompilationStage::Input instead of Codegen.
  BackendOptions carries a target_arch_source label so both the env-driven
  rustc pipeline and the standalone API describe their own provenance.
- The explicit-Toolchain compile path no longer discards pipeline
  diagnostics; CompileOptions gains with_verbose/verbose so the standalone
  API can actually request them (previously unreachable through the public
  surface). Toolchain::from_paths now also reports what it selected instead
  of always returning empty diagnostics.
- ScratchDirectory is now backed by tempfile::Builder instead of a
  hand-rolled RAII implementation. The workspace tempfile floor is raised
  from 3.3 to 3.9, the first release exposing Builder::permissions, which
  the 0o700 scratch-dir mode relies on; 3.3-3.8 would fail to compile.
- Deduplicate the module-liveness check shared by mark_kernel_entry and
  Compiler::compile into one helper.
- Document (rather than attempt) the double LLVM-IR-render on the libdevice
  path: the preview and final renders are of genuinely different module
  states through different export configs, so caching one for the other
  would be unsound; a real fix needs feature detection to move off rendered
  text entirely.

Signed-off-by: Alejandro Soto Franco <[email protected]>
@alejandro-soto-franco

Copy link
Copy Markdown
Contributor Author

Closing in favour of three smaller PRs. This one bundled eight unrelated fixes across 12 files, which put the small ones behind a dependency discussion and made the whole thing awkward to review in a sitting.

The three are independent. Each applies cleanly to main on its own and they merge cleanly in any order.

The split also picked up review fixes that were not in this PR: the reclassification here routed feature-floor rejections through CompileError::InvalidTarget, which called a valid sm_75 invalid and printed the architecture twice, so #416 adds a distinct variant. The env-var mis-attribution this PR set out to remove was still present on the mir-importer path, where PipelineConfig::target_arch overrode the target and left the CUDA_OXIDE_TARGET label behind; #416 fixes that too. The 0o700 scratch-dir mode, which is the sole justification for the tempfile floor, now has a test in #417.

@alejandro-soto-franco
alejandro-soto-franco deleted the fix/codegen-extraction-review 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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant