Skip to content

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

Closed

Description

@uurl

Problem

forward_compiler_result_bundles currently forwards reads from compiler-owned multi-result bundles only when every field/array projection uses a constant index.

This leaves bounded dynamic accesses on the memory path even when the compiler can prove that the index has a small finite candidate set.

For example:

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

The importer-created result bundle is already known to be compiler-owned, immutable, non-escaping, and backed by the results of a single producer, but the dynamic mir.array_element_addr prevents the existing forwarding pass from recovering the SSA aggregate.

Existing infrastructure

The MIR dialect and lowering already support bounded dynamic extraction through mir.extract_array_element.

When its index has the form:

unsigned_value % constant

and the candidate count is small enough, lowering scalarizes the access into fixed extractvalue candidates plus a select chain.

MAX_SCALARIZED_CANDIDATES currently limits this expansion to 16 candidates.

Proposed scope

Extend forward_compiler_result_bundles to accept a terminal dynamic array projection when all of the following hold:

  • the aggregate is an importer-marked compiler result bundle;
  • the existing single-producer / single-store / non-escaping boundary proof succeeds;
  • the load is non-volatile;
  • the dynamic projection is an array element access;
  • the index is an unsigned mir.rem(value, constant);
  • constant > 0;
  • constant <= array length;
  • constant <= MAX_SCALARIZED_CANDIDATES;
  • the dynamic array projection is terminal.

Instead of resolving the access to one statically selected producer result, rewrite the load to:

mir.extract_array_element %ssa_array, %index

and let the existing MIR-to-LLVM lowering decide how to scalarize it.

Unsupported or unsafe cases should continue to fail closed and preserve the complete memory path.

Expected behavior

Accepted:

bundle[index % 4]
bundle[index % 16]

when the corresponding array is large enough.

Rejected:

bundle[index]       // unbounded
bundle[index % 17]  // exceeds scalarization limit
bundle[index % 5]   // array length is 4
signed_index % 4    // signed remainder

Existing escape, mutation, volatile, and additional-store rejection rules should remain unchanged.

Acceptance criteria

  • bounded unsigned remainder indexing is forwarded to mir.extract_array_element;
  • constant-index forwarding remains unchanged;
  • candidate count 16 is accepted;
  • candidate count 17 is rejected;
  • a bound greater than the array length is rejected;
  • unbounded dynamic indices are rejected;
  • signed remainder indices are rejected;
  • rejected cases leave the original memory path intact;
  • existing mir-transforms, mir-lower, and cuda-oxide-codegen tests continue to pass.

Activity

  1. added
    perf
    enhancement
    codegen
    on Aug 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    codegenenhancementperf

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions