Skip to content

feat(codegen): forward bounded dynamic result-bundle indexing - #1005

Merged
nihalpasham merged 2 commits into
NVIDIA:mainfrom
uurl:feat/bounded-compiler-result-indexing
Aug 20, 2026
Merged

nihalpasham merged 2 commits into
NVIDIA:mainfrom
uurl:feat/bounded-compiler-result-indexing

Conversation

@uurl

@uurl uurl commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1004

Summary

Extend compiler-result bundle forwarding to handle bounded dynamic array indexing.

forward_compiler_result_bundles previously required every array projection to use a constant index. As a result, accesses such as:

let regs = intrinsic_returning_array();
let value = regs[index % 4];

kept the importer-created aggregate on the memory path even though the bundle boundary had already been proven safe.

This PR recognizes a narrow bounded dynamic form and rewrites it to the existing SSA aggregate extraction operation.

What changed

forward_compiler_result_bundles now accepts a terminal dynamic array projection when:

  • the result bundle passes the existing compiler-owned boundary proof;
  • the index type is unsigned;
  • the index is produced by mir.rem(value, constant);
  • the constant is non-zero;
  • the constant does not exceed the array length;
  • the constant does not exceed MAX_SCALARIZED_CANDIDATES.

For accepted accesses, the pass emits:

mir.extract_array_element %array, %index

instead of preserving the alloca -> store -> array_element_addr -> load path.

The existing mir.extract_array_element lowering already recognizes bounded unsigned remainder indices and lowers them to fixed extractvalue candidates plus a select chain when profitable.

No second scalarization implementation is introduced here.

Source-level dynamic indices may initially reach this transform through temporary MIR slots, so the direct mir.rem shape is not always visible before mem2reg.

To make the optimization reachable from real compiler output, the same fail-closed compiler-result forwarding pass is run again immediately after mem2reg. At that point, promotable index temporaries have been converted to SSA and bounded forms such as index % C are directly visible to the existing recognition logic.

The original pre-mem2reg invocation remains in place for constant/static result bundle forwarding.

Fail-closed behavior

The pass still leaves the complete memory path untouched for:

  • arbitrary dynamic indices;
  • signed remainder indices;
  • candidate sets larger than MAX_SCALARIZED_CANDIDATES;
  • bounds larger than the source array;
  • volatile loads;
  • additional stores or escaping/unknown pointer users;
  • unsupported projection shapes.

Constant-index forwarding is unchanged.

Constructor lifetime

Dynamic extraction retains the SSA mir.construct_array needed by mir.extract_array_element.

Bundle constructors are now erased only when their result has no remaining uses, while constant forwarding continues to eliminate the complete aggregate construction when possible.

Tests

Added transform coverage for:

  • bounded dynamic indexing;
  • the maximum 16-candidate boundary;
  • candidate count greater than 16;
  • candidate count greater than the array length;
  • unbounded dynamic indexing;
  • signed remainder indexing.

Added an end-to-end generated_ldmatrix regression using a source-level bounded dynamic access:

let register_index = lane % 4;
let value = registers[register_index];

This exercises the complete rustc -> MIR -> mem2reg -> compiler-result forwarding -> LLVM/PTX pipeline.

Validation performed:

cargo test -p mir-transforms
25 passed

cargo test -p mir-lower
252 unit tests passed
117 lowering tests passed

cargo test -p cuda-oxide-codegen --all-targets
176 unit tests passed
integration tests passed

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

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

End-to-end validation on sm_120:

cargo oxide run generated_ldmatrix

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

With LLVM optimization disabled:

CUDA_OXIDE_NO_OPT=1 cargo oxide run generated_ldmatrix

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

The generated PTX for the bounded dynamic result-bundle path contains no local memory round trip:

st.local = 0
ld.local = 0

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

@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.

The select-chain rewrite is sound and everything fails closed, but the feature is unreachable from real compiler output:

  • validate_bounded_dynamic_index requires the index's defining op to be MirRemOp directly. The pass runs pre-mem2reg, where a source-level regs[i % 4] arrives as a mir.load from the index's temp slot, so the gate never fires outside the hand-built test IR. Confirmed against the importer's translate_place address path.
  • Two ways to fix:
    • trace the index through its slot (a load from an alloca with a single dominating rem store, mirroring the bundle proof this pass already does), or
    • run the recognition again post-mem2reg where the rem is a direct operand; canonicalize_bounded_borrowed_pointer_arguments already lives there.
  • Add an end-to-end test through the real pipeline showing st.local/ld.local disappear for a source-level modulo index, not just hand-built IR.
  • Another point while you're there: assert-derived bounds (the Asserted form the sibling pass supports) can carry the same proof when no rem is present.

@uurl
uurl force-pushed the feat/bounded-compiler-result-indexing branch from 856f63b to 0d1565b Compare August 19, 2026 22:47
@uurl

uurl commented Aug 19, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the reachability issue by rerunning compiler-result forwarding immediately after mem2reg, where the source-level modulo index is available directly in SSA.

Added an end-to-end generated_ldmatrix regression using registers[lane % 4].

Verified on sm_120:

normal pipeline:
PASS: all 128 ldmatrix x4 fragments matched
PASS: all 32 bounded dynamic ldmatrix selections matched

CUDA_OXIDE_NO_OPT=1:
PASS: all 128 ldmatrix x4 fragments matched
PASS: all 32 bounded dynamic ldmatrix selections matched

generated PTX:
st.local = 0
ld.local = 0

The existing unit tests and mir/codegen suites remain green.

I kept assert-derived bounds outside this PR for now so #1004 remains scoped to the % constant form.

@uurl
uurl force-pushed the feat/bounded-compiler-result-indexing branch from b87c6fb to df21553 Compare August 20, 2026 10:31
@nihalpasham

Copy link
Copy Markdown
Collaborator
  • Verified end to end on the current head (which added a post-mem2reg re-run of the forwarding pass plus a real end-to-end kernel): mir-transforms 25 tests green, cargo oxide run generated_ldmatrix passes on sm_120 in both the default and CUDA_OXIDE_NO_OPT=1 pipelines with all 32 bounded dynamic selections matched, per-example fmt/clippy clean, full compile-only smoketest 216/216.
  • The second call site is the same fail-closed proof, not a widening: non-load users, type-mismatched loads, and signed/oversized/over-length/unbounded indices all still decline scalarization, and the recognizer shares MAX_SCALARIZED_CANDIDATES and the urem-of-constant contract with the mir-lower select-chain lowering.
  • The deferred assert-derived bounds are a reasonable follow-up issue rather than a blocker.

@nihalpasham
nihalpasham merged commit f21bf76 into NVIDIA:main Aug 20, 2026
49 checks passed
@uurl
uurl deleted the feat/bounded-compiler-result-indexing branch August 22, 2026 06:18
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.

codegen: forward bounded dynamic indexing of compiler-owned result bundles

2 participants