Return from the raise site instead of branching to a Return block #407
Auto-sync is disabled for ready for review pull requests in this repository. Workflows must be run manually.
Contributors can view more details about this message here.
<!-- coderabbit-ai-generated:v1 -->
<!-- This is an auto-generated comment: summarize by coderabbit.ai -->
<!-- review_stack_entry_start -->
<a href="https://app.coderabbit.ai/change-stack/NVIDIA/numba-cuda-mlir/pull/407?scope=ghh_OusAJGA4HG8ljoOV0wJvVx8NmnZIjc2DdjmxOu0xZWA&cs_source=review_comment"><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-buttons-v1/explain-change-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-buttons-v1/explain-change.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-buttons-v1/explain-change.svg" alt="Explain Change →" height="32"></picture></a>
<!-- review_stack_entry_end -->
<!-- walkthrough_start -->
<details>
<summary><strong>Walkthrough</strong></summary>
<dl>
<dd>
<br>
important: Static raises now return directly from the current block and supply zero values for function results. Added tests cover kernel and device-function raise paths.
Changes: Static Raise Handling
|Layer / File(s)|Summary|
|:---|:---|
|Direct returns and result values <br> src/numba_cuda_mlir/mlir_lowering.py|Static raise lowering emits a GPU or function return with zero values for declared results. Zero-value construction now handles default-memory-space memrefs.|
|Kernel and device-function coverage <br> tests/test_kernel_exceptions.py|Tests cover raise paths followed by stores and branches, literal-unrolled loops, scalar and tuple results, and array-view returns. They check launch behavior and device-function PTX.|
Suggested reviewers: jiel-nv
</dd>
</dl>
</details>
<!-- walkthrough_end -->
<!-- change_assessment_start -->
<!-- change_assessment_commit:"8d740f04c1bf7fd9166fc0e33c5ec0e8cda87f63" -->
<!-- change_assessment_priority:{"version":1,"urgency":"p2","manual":false} -->
<!-- change_assessment_end -->
<details>
<summary><strong>Merge Risk:</strong> <strong><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/risk-low-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Low</strong> · up to <code>8d740</code></summary>
<dl>
<dd>
<br>
<!-- final_review_risk_start -->
<!-- final_review_risk_label
Merge Risk: 🔵 Low · up to 8d740
-->
<!-- final_review_risk_coverage:{"sourceCommitId":"8d740f04c1bf7fd9166fc0e33c5ec0e8cda87f63","coveredCommitId":"8d740f04c1bf7fd9166fc0e33c5ec0e8cda87f63","kind":"reviewed"} -->
The change appears mergeable with awareness that the new array-view tests do not check the zero-valued descriptor produced on a raise.
<!-- final_review_risk_end -->
</dd>
</dl>
</details>
<details>
<summary><strong>Security architecture risk:</strong> <strong><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/risk-low-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Low</strong> · up to <code>8d740</code></summary>
<dl>
<dd>
<br>
<!-- architecture_review_start -->
<!-- architecture_review_label
Security Architecture Review
Security architecture risk: 🔵 Low · up to 8d740
-->
Returning directly fixes execution after a raise. The remaining concern is bounded failure containment: an array-returning device function now produces a null descriptor on failure, while its caller can continue executing. A resulting GPU fault has not been demonstrated, and no broader privilege or data-isolation impact was established.
Retained concerns
<details>
<summary><strong>Security review details</strong></summary>
<dl>
<dd>
<br>
Security Blast Radius
Security Findings and Attack Paths
Trust Boundaries and Controls
Resilience and Maintainability Implications
Hardening Proposals
</dd>
</dl>
</details>
<!-- architecture_review_end -->
</dd>
</dl>
</details>
<!-- autopilot:start -->
<!-- autopilot:end -->
<!-- coderabbit-ai-generated:v1 -->
<details>
<summary>🧹 Nitpick comments: 1</summary><blockquote>
<dl>
<dd>
<details>
<summary>tests/test_kernel_exceptions.py: 1</summary><blockquote>
<dl>
<dd>
272-272: <picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/neutral-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Trivial · Functional Correctness · Quick win
suggestion: Assert the output from the negative-input launch.
For x[0] < 0, both device functions return zero-valued results at the raise site, so k should produce [0, 0] for both debug settings. _launch currently returns this output, but line 272 discards it.
- _launch(k, debug, -1, 1)
+ assert _launch(k, debug, -1, 1) == [0, 0]
<!-- cr-comment:v1:c7c192f177f8a2fc05e98288 -->
</dd>
</dl>
</blockquote></details>
</dd>
</dl>
</blockquote></details>
---
<details>
<summary><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/info.svg" width="16" height="16" align="absmiddle" alt=""></picture> Review info</summary>
<dl>
<dd>
<details>
<summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog.svg" width="16" height="16" align="absmiddle" alt=""></picture> Run configuration</summary>
<dl>
<dd>
653f4f2e-ba66-426e-967a-1ff8785019c6</dd>
</dl>
</details>
<details>
<summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal.svg" width="16" height="16" align="absmiddle" alt=""></picture> Commits</summary>
<dl>
<dd>
Reviewing files that changed from the base of the PR and between 2c40bb721c8110e9548b9ef33d1716e230170261 and 482bccce1314dec0f5a8848604f9b1846ee64bb0.
</dd>
</dl>
</details>
<details>
<summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner.svg" width="16" height="16" align="absmiddle" alt=""></picture> Files selected for processing: 2</summary>
<dl>
<dd>
src/numba_cuda_mlir/mlir_lowering.py
tests/test_kernel_exceptions.py
</dd>
</dl>
</details>
Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 10 remain after this review.
</dd>
</dl>
</details>
<!-- This is an auto-generated comment by CodeRabbit for review status -->
<!-- coderabbit-review-publication v1 publication=e635604a-8b02-48c5-b277-f884ffab06f7 attempt=fa70a7d6-c912-4f5b-a7da-aa11f67f3e81 batch=1/1 -->
This negative-input launch checks exception reporting through _launch, but discards the scalar and tuple results produced by the new raise-site return. Assert _launch(k, debug, -1, 1) == [0, 0] so both debug variants also protect the zero-result behavior.
Approved these changes.
/ok to test 1aa8937331c9fd47f207d7f0ae31002e3a45b9dc
<!-- coderabbit-ai-generated:v1 -->
<details>
<summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/brush-cleaning-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/brush-cleaning.svg" width="16" height="16" align="absmiddle" alt=""></picture> Nitpick comments: 1</summary><blockquote>
<dl>
<dd>
<br>
<details>
<summary>src/numba_cuda_mlir/mlir_lowering.py: 1</summary><blockquote>
<dl>
<dd>
<br>
2619-2621: <picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/neutral-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Trivial · Stability & Availability · Low value
suggestion: Check the strided descriptor on the raise path.
The existing sliced-view tests cover compilation, but they do not check the returned descriptor. Add an assertion that the raise path returns a zeroed descriptor for a strided view.
<!-- cr-comment:v1:300fd59acf59b2faca871b8e -->
_Source: Path instructions_
</dd>
</dl>
</blockquote></details>
</dd>
</dl>
</blockquote></details>
<details>
<summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/message-square-off-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/message-square-off.svg" width="16" height="16" align="absmiddle" alt=""></picture> Additional comments: 1</summary><blockquote>
<dl>
<dd>
<br>
<details>
<summary>tests/test_kernel_exceptions.py: 1</summary><blockquote>
<dl>
<dd>
<br>
297-298: <picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/neutral-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Trivial · Functional Correctness · Low value
<picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-warning.svg" width="20" height="20" align="absmiddle" alt=""></picture> Unverified finding
Verification ran but could not confirm this finding. It is shown for review, not as a verified issue.
suggestion: test_raise_in_array_returning_device_function checks only that the PTX contains ret;. Any compiled function passes this check. It does not verify that a raise returns from the raise site.
Impact: A regression back to the old branch-to-Return lowering that still verifies would pass this test. The test does guard against the verification failure and the hang that the PR names.
Smallest fix: Keep the test as a compile-only regression. Rename it or its docstring to say so. Alternatively, assert a property that the old lowering did not have.
_Source: Path instructions_
</dd>
</dl>
</blockquote></details>
</dd>
</dl>
</blockquote></details>
<h6>
<details>
<summary><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/info.svg" width="12" height="12" align="absmiddle" alt=""></picture> Review info</summary>
<dl>
<dd>
<br>
<details>
<summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog.svg" width="16" height="16" align="absmiddle" alt=""></picture> Run configuration</summary>
<dl>
<dd>
<br>
62a6b771-b873-4bd0-9fd4-342582d6c060</dd>
</dl>
</details>
<details>
<summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal.svg" width="16" height="16" align="absmiddle" alt=""></picture> Commits</summary>
<dl>
<dd>
<br>
Reviewing files that changed from the base of the PR and between 482bccce1314dec0f5a8848604f9b1846ee64bb0 and 8d740f04c1bf7fd9166fc0e33c5ec0e8cda87f63.
</dd>
</dl>
</details>
<details>
<summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner.svg" width="16" height="16" align="absmiddle" alt=""></picture> Files selected for processing: 2</summary>
<dl>
<dd>
<br>
src/numba_cuda_mlir/mlir_lowering.py
tests/test_kernel_exceptions.py
</dd>
</dl>
</details>
Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review.
</dd>
</dl>
</details>
</h6>
<!-- This is an auto-generated comment by CodeRabbit for review status -->
<!-- coderabbit-review-publication v1 publication=232d16bf-44b0-48ee-ac4f-7c9dab835f75 attempt=0540588b-bf25-4d64-88e3-d197eacba917 batch=1/1 -->
Doc Preview CI
:---:
| <p></p> :rocket: View preview at <br> https://nvidia.github.io/numba-cuda-mlir/pr-preview/pr-407/ <br><br>
| <h6><br> Preview will be ready when the GitHub Pages deployment is complete. <br><br></h6>
<!-- Sticky Pull Request Commentpr-preview -->
Doc Preview CI
:---:
Preview removed because the pull request was closed or merged.
<!-- Sticky Pull Request Commentpr-preview -->
atmnp
diptorupd
This PR fixes how a
raisein device code is lowered. Today a taken raise sets the error code and then jumps into the function's lastreturnblock, which can hold ordinary user code. That code then runs after the raise, and when it uses a value defined after the raise point, the module fails to verify. With this change a raise returns from the function at the raise site.Why
lower_static_raisesets the error code and branches to the Return block with the highest offset. That block is not an exit block: it holds whatever user code comes before the function's lastreturn. Two things go wrong.The kernel keeps running after the raise. With
debug=Truethe host sees theValueError, butout[0]is still written:@cuda.jit(debug=True, opt=False)
def k(out, x):
if x[0] < 0:
raise ValueError("negative")
out[0] = 5 # runs even when x[0] < 0
And when that block uses a value defined after the raise point, the module doesn't verify. The tail only needs to branch once so the store lands in a block of its own:
@cuda.jit
def k(out, x):
if x[0] < 0:
raise ValueError("negative")
v = x[0] 2
if x[1] > 0:
out[1] = 1
out[0] = v # InternalCompilerError: operand #0 does not dominate this use
The raise block branches straight to the block of
out[0] = v(^bb4), which uses%25(v) from^bb2:^bb1: // pred: ^bb0 (raise)
%14 = "llvm.cmpxchg"(%11, %13, %12) ... // error code 3 (ValueError)
"cf.br"()[^bb4] : () -> ()
^bb2: // pred: ^bb0
...
%25 = "arith.muli"(%23, %24) ... // v = x[0] 2
"cf.cond_br"(%36)[^bb3, ^bb4] ...
...
^bb4: // 3 preds: ^bb1, ^bb2, ^bb3
...
"memref.store"(%25, %arg0, %53) ... // out[0] = v: %25 does not dominate ^bb1 -> ^bb4
"gpu.return"() : () -> ()
The same happens without a
raisein the source.literal_unrolldispatches on the tuple index, and its unmatched-case branch raises, so everyliteral_unrollloop has a raise block. A tail after the loop that branches fails the same way:@cuda.jit
def k(out, x):
value = 0
for item in literal_unroll((1, 2)):
value += item
v = value * x[0]
if x[1] > 0:
out[1] = 1
out[0] = v # operand #0 does not dominate this use
Value-returning device functions are affected in the same way, and when the jump does verify there, the function returns whatever the tail computes from values the raise path never set.
Why cuda.coop triggers it
cuda.coop (NVIDIA/cccl#11200) handles cooperative calls today with a whole-function planner that rewrites the kernel before numba-cuda-mlir's type inference, and it compiles the kernel below. We're working toward moving coop's typing onto numba-cuda-mlir's type inference, which leaves the kernel's control flow to numba-cuda-mlir's lowering. There, a cooperative call after a
literal_unrollloop hits this bug:value = 0
for item in literal_unroll((1, 2)):
value += item
coop.store(coop.this_block(), destination, value)
The inlined
coop.storeadds a branch after the loop, so the store ofvaluesits in the Return block and the unmatched-case raise jumps to it.What changes
lower_static_raisereturns from the raise site:gpu.returnin a kernel,func.returnin a device function. A value-returning device function returns zero values of its result types (from_zero_value_for_type); the error code is what reports the raise, as before._zero_value_for_typehandles generic-address-space memrefs (a zero descriptor throughunrealized_conversion_cast, the way NCM builds memrefs from descriptors elsewhere), for device functions that return arrays.Behaviour
debug=Trueare unchanged.literal_unroll, now compile._zero_value_for_typeused to reject.Tests
tests/test_kernel_exceptions.py, all failing onmain: the code after a taken raise doesn't run (debug=FalseandTrue); a branching tail after a raise and afterliteral_unrollcompiles and computes the right values; a device function with scalar and tuple results raises and returns; device functions returning a 1-D and a 2-D array view compile.main, 5170 on this branch (the 9 new tests); 86 skipped and 295 xfailed on both. The one failure,test_cuda_gdb_debug_kernel, also fails onmainin this environment. No other test changes outcome.Not in this PR
Two problems I found while testing are unrelated to this change and also on
main:inline="never") device function that returns an array doesn't finish (one run grew to 9.8 GB in 12 minutes). It spins in_call_results_tuple, which callstuple()on the memref result and so iterates the memref's__getitem__. That happens with or without araise.debug=True, a kernel with aliteral_unrollloop fails inmlirTranslateModuleToLLVMIR.