Repository navigation
fix(mir-lower): trap on assert failure so opt cannot delete bounds checks - #405
Conversation
…ecks Fixes NVIDIA#396 Problem: `mir-lower` incorrectly translated the assert's failure branch into a block containing only LLVM `unreachable`. SimplifyCFG rewrites a block containing`unreachable` into `llvm.assume(cond)`, which causes every checked slice index in a kernel lose its bounds check Fix: Emit call @llvm.trap() ahead of the unreachable in the abort block. Add the slice_index_bounds_check example. An in-range gather must produce correct values. An out-of-range index must trap (or skip the access) without the load. Verified on sm_120a. Signed-off-by: SomeB1oody <[email protected]>
|
Verified on sm_89 (NVIDIA L4, driver 580.159.03, CUDA 12.8) using the original repro from #396 ( The fix works. Backend PTX (gather kernel):
Both bounds guards are back, and each abort path carries a Runtime under
One small note on the "0 errors" claim: on sm_89 the sanitizer still prints |
|
Thanks for the verification. Your reading of the error summary is exactly right. I can reproduce the same breakdown on sm_120a:
That cascade is inherent to trap semantics, not bug. A device-side trap poisons the context and the error is sticky, so every subsequent driver call on that context returns 719. I will change my PR's claim from "0 error" to "0 memory violations". |
The comment above convert_assert_creates_abort_block_with_trap ended in "llvm.unreachable=" instead of "llvm.unreachable.". Signed-off-by: nihalp <[email protected]>
|
Thanks for the PR. LGTM.
For example: Reviewer note: mir-lower tests pass; the example exits 0 with SUCCESS on an RTX 5090 after the deliberate trap; the NVVM-IR path accepts This closes #396. |
Summary
Fix #396. Checked slice indexing inside kernels (
src[j]) lost its bounds check. An out-of-bounds Rust index triggers a MIR Assert terminator.mir-lowerincorrectly translated the assert's failure branch into a block containing only LLVMunreachable(unreachablemeans "control flow can never reach this point", not "stop when control flow reaches here"). Based on that semantics,opt -O2(SimplifyCFG) folds the entire check intollvm.assume(j < len). The branch disappears and the optimizer treats the condition as always true. So an out-of-bounds index still executes, which is theInvalid __global__ readreported by compute-sanitizer.Changes
crates/mir-lower/src/convert/ops/control_flow.rs:convert_assertnow declares@llvm.trapand inserts a direct call to it at the head of the abort block, ahead ofllvm.unreachable.crates/dialect-mir/src/ops/control_flow.rs:MirAssertOpdocs updated: assert failure aborts the kernel via trap. The failure path must stay observable or the optimizer deletes the check.convert_assert_creates_abort_block_with_trapasserts the abort block is exactly[call @llvm_trap, llvm.unreachable]and that the trap intrinsic is declared in the module.slice_index_bounds_check: Regression test. A gather kernel indexes with runtime data. The in-range pass must produce correct values. An out-of-range index must trap (or skip the access) without the load reaching memory (CUDA_ERROR_ILLEGAL_ADDRESSfails the test).Testing
cargo oxide run slice_index_bounds_check(sm_120a):PTX inspection: the gather kernel regains both
setpbounds guard. The abort path is a PTXtrap.compute-sanitizer --tool memcheckon the issue reproducer: 0 memory violation (previously 6 invalid__global__reads of size 4).cargo oxide run <example>passescargo test --workspacepassesNew example added (if applicable)
Checklist
git commit -s)