Skip to content

fix(mir-importer): trap on dropped diverging calls so opt cannot delete panic paths - #408

Merged
nihalpasham merged 5 commits into
NVIDIA:mainfrom
SomeB1oody:fix/issue407-diverging-call-trap
Jul 22, 2026
Merged

nihalpasham merged 5 commits into
NVIDIA:mainfrom
SomeB1oody:fix/issue407-diverging-call-trap

Conversation

@SomeB1oody

Copy link
Copy Markdown
Contributor

Summary

Fixes #407. When a kernel hits a diverging panic call (Option::unwrap on a runtime None, panic!, unwrap_failed, etc.), mir-importer drops the call (the panic machinery cannot run on the GPU) and terminated the block with only a bare mir.unreachable. That marks the runtime-reachable panic path as undefined behavior, so the opt -O2 stage folds it into the surrounding code: unwrap(None) compiled into storing an uninitialized register into the output (silent wrong data, invisible to compute-sanitizer). This PR emits nvvm.trap ahead of the mir.unreachable at that site, so the panic path stays observable through opt and the GPU traps as the documented panic contract requires.

Changes

  • crates/mir-importer/src/translator/terminator/mod.rs: the diverging-call site now terminates the block using emit_trap_unreachable_after (nvvm.trap + mir.unreachable).
  • Add doc comments on emit_unreachable_after: it is only for paths that can never execute (rustc-proven unreachability) or code after a call that is emitted into the module and genuinely never returns. Those sites are deliberately unchanged, so proven-unreachable paths still fold as before.
  • New example crate option_unwrap_trap: one thread runs src.get(j).copied().unwrap() with an out-of-range j. The launch must fail with the kernel trapping.

Testing

sm_120a, CUDA 13.3, driver 595.71.05:

Before the fix, the example's launch succeeds with garbage output:

FAIL (unwrap(None) did not panic, out[0] = 0)

After the fix:

PASS (kernel trapped: DriverError(719, "unspecified launch failure"))
  • cargo oxide run option_unwrap_trap passes (also with CUDA_OXIDE_NO_OPT=1).

  • Proven-unreachable paths are unaffected.

  • compute-sanitizer --tool memcheck reports no memory violations on the example.

  • scripts/smoketest.sh: 134/134 PASS, including the new example.

  • cargo oxide fmt --check clean; cargo clippy -p mir-importer --all-targets -- -D warnings clean.

  • cargo oxide run <example> passes

  • cargo test --workspace passes

  • New example added (if applicable)

Checklist

  • All commits signed off (git commit -s)
  • SPDX headers on new source files

@SomeB1oody

Copy link
Copy Markdown
Contributor Author

I pushed a second commit extending the fix to the assert_inhabited intrinsic handler.

assert_inhabited::<T> with an uninhabited T is not a proven-unreachable marker. The commit lowers the uninhabited case through the same emit_trap_unreachable_after helper. It also adds the assume_init_uninhabited_trap regression example.

before the fix it failed with the launch succeeding. After, the launch aborts with the kernel trapping.

@midiareshadi

Copy link
Copy Markdown

Verified #408 on sm_89 (NVIDIA L4, driver 580.159.03, CUDA 12.8) with your unwrap_get reproducer (src.get(1_000_000).unwrap()).

Before (29396b7): reproduces the silent failure exactly — the launch succeeds and out[0] gets an uninitialized register. The program prints FAIL (unwrap(None) did not panic, out[0] = 0) and compute-sanitizer reports ERROR SUMMARY: 0 errors — completely silent, as you noted.

After (6a58392, both commits): the panic path is preserved and the kernel traps.

PTX (panic path): trap 0 → 1, total 38 → 44 — the None arm is no longer folded away.

Runtime: the program prints PASS (kernel trapped: DriverError(719, "unspecified launch failure")), and the sanitizer shows a Trace/breakpoint trap. No Invalid reads; the remaining ERROR SUMMARY: 5 errors are all the post-trap 719 cascade on cleanup (cuMemcpyDtoHAsync_v2, 2x cuMemFree_v2, cuModuleUnload), consistent with the sticky-context behavior from #405 — no memory violations.

So on sm_89 the fix behaves exactly as on sm_120a: silent wrong data becomes a clean trap.

SomeB1oody and others added 4 commits July 22, 2026 16:42
…te panic paths

Fixes NVIDIA#407

Problem: `mir-importer` drops diverging panic calls (`unwrap_failed`, `panic!`, etc.) and terminates the block with only `mir.unreachable`. `opt -O2` folds it into the surrounding code: `unwrap` on a runtime `None` compiles into storing an uninitialized register into the output instead of aborting the kernel.

Fix: Emit `nvvm.trap` ahead of the `mir.unreachable` at the diverging-call site (new `emit_trap_unreachable_after`). `emit_unreachable_after` is only for proven-unreachable paths.

Signed-off-by: SomeB1oody <[email protected]>
Problem: `assert_inhabited::<T>` with an uninhabited `T` panics at runtime ("attempted to instantiate uninhabited type"), but the importer, but the importer lowered it to a bare `mir.unreachable`.

Fix: Lower the uninhabited case through `emit_trap_unreachable_after`

Verified on sm_120a.

Signed-off-by: SomeB1oody <[email protected]>
…rap site

Any `-> !` callee that reaches the `target_usize.is_none()` branch in
translate_call is dropped and replaced by an immediate trap, including
a user `#[device]` fn that legitimately diverges (e.g. `loop {}`).
Full Rust fidelity would emit the call for resolvable callees the way
translate_function_item_call does. The drop is a pre-existing semantic
that NVIDIA#408's trap merely makes safe (the bare `unreachable` emitted
before was UB, letting opt delete the panic path), so record the
constraint at the site for future callers.

Signed-off-by: nihalp <[email protected]>
Since the generated-intrinsics refactor (NVIDIA#406), every generated
intrinsic operation must carry its exact ABI marker; the
target-requirements verifier otherwise rejects the module. The raw
Operation::new construction predated that contract on this branch's
original base and only surfaced once rebased onto current main: every
example whose device code reaches the emitted trap (option_unwrap_trap,
assume_init_uninhabited_trap, and pre-existing function_item_call,
slice_reslice, step_by) failed verification with
"generated intrinsic operation `nvvm.trap` is missing its exact ABI
marker".

Construct the trap through the typed TrapOp::build constructor and
attach marker v1:i0295 (trap's append-only id in intrinsics/abi-v1.toml)
exactly as the generated debug::trap dispatch does.

Signed-off-by: nihalp <[email protected]>
@nihalpasham
nihalpasham force-pushed the fix/issue407-diverging-call-trap branch from 6a58392 to 9b575e7 Compare July 22, 2026 11:30
@nihalpasham nihalpasham added bug Something isn't working miscompile Produces incorrect PTX/IR (wrong generated code) safety Memory safety, soundness, or undefined behavior codegen Device code-generation pipeline (Rust MIR to IR to PTX) labels Jul 22, 2026
…habited_trap

The example's entire point is an assume_init on an uninhabited type:
rustc's assert_inhabited guard must survive into PTX as a trap. clippy's
uninit_assumed_init and rustc's invalid_value correctly flag the
construct in ordinary code, so scope targeted allows to the one
statement with the rationale inline. Fixes the example-directory clippy
CI job.

Signed-off-by: nihalp <[email protected]>
@nihalpasham

Copy link
Copy Markdown
Collaborator

Thanks for the PR. LGTM.

  • The mir-importer now emits a side-effecting trap instead of bare unreachable when a diverging call is dropped (unwrap/panic! shims, and assert_inhabited for uninhabited types in the second commit), so opt cannot fold panic paths away. This closes the importer half of the bug class fix(mir-lower): trap on assert failure so opt cannot delete bounds checks #405 fixes in mir-lower.
  • The option_unwrap_trap and assume_init_uninhabited_trap examples pin both shapes with host-verified failed launches.
  • I pushed maintainer follow-ups: a code comment recording the call-drop semantic (a resolvable -> ! device callee is dropped and trapped rather than emitted — pre-existing, now safe), LICENSE files matching the example convention, and the rebase + ABI-marker fix described in the note below.

For example:

let v = src.get(j).copied().unwrap();   // None when j >= len

before: opt deletes the panic path; execution continues into UB
after:  PTX contains trap; the launch fails and the host observes it

Reviewer note:

  • since Generate CUDA intrinsic APIs and lowering from pinned LLVM 22 data #406, nvvm.trap is a generated intrinsic and module verification rejects it without its exact ABI marker — as submitted, every trap-reaching example (including pre-existing function_item_call, slice_reslice, step_by) failed to compile against current main.
  • A maintainer commit constructs the trap through the typed TrapOp::build and attaches marker v1:i0295, exactly as the generated debug::trap dispatch does.
  • A second follow-up scoped #[allow(invalid_value, clippy::uninit_assumed_init)] to the one deliberately-invalid statement in assume_init_uninhabited_trap — the construct is the example's entire point, and CI's per-example clippy gate rightly flags it in ordinary code.
  • After the fixes: mir-importer 803 tests pass, all five trap-path examples PASS on an RTX 5090, and both new example dirs pass cargo clippy --all-targets -- -D warnings. Merge together with companion fix(mir-lower): trap on assert failure so opt cannot delete bounds checks #405. All hosted CI checks passed on the final head.

This closes #407.

@nihalpasham
nihalpasham merged commit 6300b8d into NVIDIA:main Jul 22, 2026
33 checks passed
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) miscompile Produces incorrect PTX/IR (wrong generated code) safety Memory safety, soundness, or undefined behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

opt -O2 deletes the panic path and produces silent wrong results

3 participants