Skip to content

feat(codegen): forward assert-bounded result-bundle indexing - #1143

Merged
nihalpasham merged 1 commit into
NVIDIA:mainfrom
uurl:feat/codegen-result-bundle-assert-bounds
Aug 28, 2026
Merged

nihalpasham merged 1 commit into
NVIDIA:mainfrom
uurl:feat/codegen-result-bundle-assert-bounds

Conversation

@uurl

@uurl uurl commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1142

Summary

Extend compiler result-bundle forwarding to support terminal dynamic array indexing whose candidate set is proven by an exact bounds assertion:

assert(index < C);
bundle[index]

This complements the existing unsigned index % C path added in #1005.

Implementation

forward_compiler_result_bundles now accepts an assertion-derived dynamic index only when all of the following are proven:

  • the index has an unsigned integer type;
  • the load block has exactly one predecessor;
  • that predecessor terminates with mir.assert;
  • the assertion's success edge targets the load block;
  • the asserted condition is exactly mir.lt(index, C);
  • the comparison uses the exact same index value;
  • C has the same type as the index;
  • C is a constant;
  • 0 < C <= array length;
  • C <= MAX_SCALARIZED_CANDIDATES.

For this case the pass materializes:

bounded_index = mir.rem(index, C)
result = mir.extract_array_element(array, bounded_index)

This reuses the existing bounded mir.extract_array_element lowering rather than adding a second scalarization mechanism.

The proof intentionally mirrors the narrow assertion shape already used by borrowed aggregate read scalarization from #650.

Fail-closed behavior

The pass still rejects unsupported forms, including:

  • unrelated assertions;
  • dynamic assertion bounds;
  • signed indices;
  • bounds larger than the array;
  • candidate sets larger than MAX_SCALARIZED_CANDIDATES;
  • unbounded dynamic indices.

No general range analysis is introduced.

End-to-end coverage

The generated_ldmatrix example now exercises both bounded dynamic forms:

  • the existing remainder-bounded selection;
  • an assertion-bounded selection using a runtime kernel argument.

Both paths select one register from the compiler-owned ldmatrix.m8n8.x4.b16 result bundle and verify the result on the host.

Validation

cargo fmt --all
cargo fmt --all -- --check
git diff --check

cargo test -p mir-transforms
cargo test -p mir-lower
cargo test -p cuda-oxide-codegen --all-targets

cargo clippy \
  -p mir-transforms \
  -p mir-lower \
  -p cuda-oxide-codegen \
  --all-targets -- \
  -D warnings

cargo oxide run generated_ldmatrix

CUDA_OXIDE_NO_OPT=1 \
  cargo oxide run generated_ldmatrix

Results:

  • mir-transforms: all tests passed, including the new assertion-bound positive and fail-closed cases.
  • mir-lower: all tests passed.
  • cuda-oxide-codegen: all tests passed.
  • clippy passed with -D warnings.
  • generated_ldmatrix passed on sm_120 with normal optimization.
  • generated_ldmatrix passed on sm_120 with CUDA_OXIDE_NO_OPT=1.

Runtime output included:

PASS: all 128 ldmatrix x4 fragments matched on sm_120
PASS: all 32 remainder-bounded dynamic ldmatrix selections matched on sm_120
PASS: all 32 assert-bounded dynamic ldmatrix selections matched on sm_120

PTX inspection also reports no local-memory round trip for the generated
result-bundle selection:

st.local: 0
ld.local: 0

Related

@nihalpasham nihalpasham added enhancement New feature or request perf Performance of generated code or of the compiler itself codegen Device code-generation pipeline (Rust MIR to IR to PTX) labels Aug 24, 2026
@nihalpasham

nihalpasham commented Aug 28, 2026 •

Copy link
Copy Markdown
Collaborator

Verified sound, meets every #1142 criterion:

The win:

let frag = ldmatrix_x4(...);   // 4-register bundle
let v = frag[i];               // i is a runtime kernel arg

before: bundle spilled, st.local + ld.local on the hot path
after:  assert(i < 4) proves the bound -> select chain, no .local

Follow-ups we'll take post-merge: an entry-block guard for the hand-built-IR corner (same exposure exists in #650; unreachable from rustc MIR), and consolidating the duplicated assert-proof helper. At landing we rerun tests, the per-example lint gates for generated_ldmatrix, and the 5090 run with and without CUDA_OXIDE_NO_OPT=1, since the recorded evidence predates #1186.

@nihalpasham nihalpasham left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-validated on the merged tree: 4,269 workspace tests green, generated_ldmatrix passes on the 5090 with and without CUDA_OXIDE_NO_OPT=1, zero .local in the PTX in both variants; CI green. Merging.

@nihalpasham
nihalpasham merged commit 6bc513b into NVIDIA:main Aug 28, 2026
51 checks passed
@uurl
uurl deleted the feat/codegen-result-bundle-assert-bounds branch August 31, 2026 14:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

codegen Device code-generation pipeline (Rust MIR to IR to PTX) enhancement New feature or request perf Performance of generated code or of the compiler itself

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Forward assert-bounded compiler result-bundle indexing

2 participants