Repository navigation
fix(mir-importer): trap on dropped diverging calls so opt cannot delete panic paths - #408
Conversation
|
I pushed a second commit extending the fix to the
before the fix it failed with the launch succeeding. After, the launch aborts with the kernel trapping. |
|
Verified #408 on sm_89 (NVIDIA L4, driver 580.159.03, CUDA 12.8) with your Before ( After ( PTX (panic path): Runtime: the program prints So on sm_89 the fix behaves exactly as on sm_120a: silent wrong data becomes a clean trap. |
…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]>
6a58392 to
9b575e7
Compare
…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]>
|
Thanks for the PR. LGTM.
For example: Reviewer note:
This closes #407. |
Summary
Fixes #407. When a kernel hits a diverging panic call (
Option::unwrapon a runtimeNone,panic!,unwrap_failed, etc.),mir-importerdrops the call (the panic machinery cannot run on the GPU) and terminated the block with only a baremir.unreachable. That marks the runtime-reachable panic path as undefined behavior, so theopt -O2stage 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 emitsnvvm.trapahead of themir.unreachableat that site, so the panic path stays observable throughoptand 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 usingemit_trap_unreachable_after(nvvm.trap+mir.unreachable).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.option_unwrap_trap: one thread runssrc.get(j).copied().unwrap()with an out-of-rangej. 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:
After the fix:
cargo oxide run option_unwrap_trappasses (also withCUDA_OXIDE_NO_OPT=1).Proven-unreachable paths are unaffected.
compute-sanitizer --tool memcheckreports no memory violations on the example.scripts/smoketest.sh: 134/134 PASS, including the new example.cargo oxide fmt --checkclean;cargo clippy -p mir-importer --all-targets -- -D warningsclean.cargo oxide run <example>passescargo test --workspacepassesNew example added (if applicable)
Checklist
git commit -s)