Skip to content

codegen: tighten the standalone compiler's clone, liveness and diagnostic paths - #415

Merged
nihalpasham merged 1 commit into
NVIDIA:mainfrom
alejandro-soto-franco:pr/codegen-cleanups
Jul 22, 2026
Merged

nihalpasham merged 1 commit into
NVIDIA:mainfrom
alejandro-soto-franco:pr/codegen-cleanups

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: #416 (target selection provenance), #417 (scratch directory on tempfile).

Summary

Follow-up to #314's cuda-oxide-codegen extraction, covering the review findings that stand on their own: a leak on the panic path, an unavoidable module clone, a duplicated liveness check, and diagnostics that the standalone API could neither request nor receive.

Changes

  • EraseGuard owns the erase of the cloned module, so a panic inside compile_clone no longer leaks it into the shared Context arena.
  • Compiler::compile_owned consumes the module and runs the pipeline on the caller's IR, making the clone opt-in.
  • validate_live_module_op replaces the liveness check duplicated between mark_kernel_entry and the compile entry points.
  • CompileOptions::with_verbose/verbose, so the standalone API can ask for the diagnostics the pipeline has always produced behind BackendOptions::verbose.
  • generate_ptx_with_toolchain returns GeneratedPtx, matching the discovery path, and compile_clone forwards the diagnostics it receives.
  • Toolchain::discover and Toolchain::from_paths both record which llc and opt they selected.
  • The double LLVM-IR render on the libdevice path is documented in place.

Rationale

Two paths dropped diagnostics even when asked for them: generate_ptx_with_toolchain discarded everything but the resolved target string, and compile_clone never copied the pipeline's diagnostics into the Compilation. Reporting nothing about toolchain selection left a caller unable to tell which of several installed LLVM versions produced an artifact.

Operation::erase still runs from Drop, so a panic inside the erase during an unwind aborts. That is left alone and documented: an erase fails only once the arena is already inconsistent, and swallowing the second panic would trade a loud abort for a silent leak.

The double render is documented rather than removed. The preview renders pre-legalisation and the export renders post-legalisation through a different export config, so the two texts describe different module states and caching one for the other would be unsound. Removing the second render needs feature detection that works off the IR rather than off rendered text.

Validation

  • cargo check --workspace --all-targets
  • cargo test -p cuda-oxide-codegen (123 passed)
  • cargo test -p mir-importer (801 passed)
  • cargo clippy --workspace -- -D warnings
  • cargo fmt --all -- --check
  • (cd crates/rustc-codegen-cuda && cargo fmt --all -- --check)
  • RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --workspace

…stic paths

Follow-up to NVIDIA#314's cuda-oxide-codegen extraction, covering the review
findings that stand on their own.

Compiler::compile clones the caller's module and erases the clone once the
destructive pipeline has run. The erase ran as a plain statement after the
call, so a panic inside compile_clone unwound past it and leaked the clone
in the shared Context arena for the process lifetime. An RAII guard now owns
the erase. Operation::erase still runs from Drop, so a panic inside the erase
during an unwind aborts; that is left alone and documented, since an erase
fails only once the arena is already inconsistent and swallowing the second
panic would trade a loud abort for a silent leak.

Callers with no further use for a module paid for that clone regardless.
Compiler::compile_owned consumes the module and runs the pipeline on the
caller's IR directly.

mark_kernel_entry and the compile entry points each re-checked that a stored
Pliron pointer still names a live builtin.module, in duplicated form. The
check is now one helper, validate_live_module_op.

CompileOptions gains with_verbose/verbose. The pipeline has always produced
progress and target-selection diagnostics behind BackendOptions::verbose,
and the standalone API had no way to ask for them. Two paths dropped them
even when asked: generate_ptx_with_toolchain discarded everything but the
resolved target string, and compile_clone never copied the pipeline's
diagnostics into the Compilation. generate_ptx_with_toolchain now returns
GeneratedPtx, matching the discovery path, and compile_clone forwards what
it receives.

Toolchain::discover and Toolchain::from_paths both record which llc and opt
they settled on. Reporting nothing left a caller unable to tell which of
several installed LLVM versions produced an artifact.

The double LLVM-IR render on the libdevice path is now documented in place.
The preview renders pre-legalization and the export renders post-
legalization through a different export config, so the two texts describe
different module states and caching one for the other would be unsound.
Removing the second render needs feature detection that works off the IR
rather than off rendered text.

Signed-off-by: Alejandro Soto Franco <[email protected]>
@nihalpasham nihalpasham added bug Something isn't working enhancement New feature or request codegen Device code-generation pipeline (Rust MIR to IR to PTX) build-related Build scripts, toolchain discovery, or compilation setup labels Jul 22, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator

Thanks for the PR. LGTM as submitted.

  • The RAII EraseGuard fixes is good: a panic in the destructive pipeline left the cloned module in the shared pliron Context arena; it now erases on every exit path, unit-tested.
  • Explicit-toolchain callers get their diagnostics back: CompileOptions::with_verbose plus the GeneratedPtx return plumb through which llc/opt produced the artifact and why opt was skipped, previously discarded.
  • compile_owned consumes the module and skips the defensive clone; the module-liveness check is deduplicated into validate_live_module_op.

For example:

let compiler = Compiler::new(Toolchain::from_paths(llc, Some(opt))?);
// a panic mid-pipeline no longer leaks the cloned module;
// compilation.diagnostics() reports the exact toolchain selection

Reviewer note: all 123 cuda-oxide-codegen tests pass with LLVM 21/22 on PATH (the discover-diagnostic test genuinely executed rather than skipping); crate clippy and fmt are clean. Merge-order caution: siblings #416/#417 touch the same files and will need rebasing after this lands — #416 also rewords the target-selection diagnostic this PR's test greps for. Land this before #410. All hosted CI checks passed on the final head.

@nihalpasham
nihalpasham merged commit 9fa990e into NVIDIA:main Jul 22, 2026
31 checks passed
@alejandro-soto-franco
alejandro-soto-franco deleted the pr/codegen-cleanups 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 build-related Build scripts, toolchain discovery, or compilation setup codegen Device code-generation pipeline (Rust MIR to IR to PTX) enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants