Skip to content

Scalarize small local arrays consumed through iterator adapters #399

Closed

Description

@niklebedenko

Problem

Small fixed-size local arrays can still become NVPTX local memory when ordinary Rust iterator adapters carry their address through a loop.

A representative shape is:

let coefficients = [4.0_f32, -3.0, 4.0 / 3.0, -0.25];
for (index, coefficient) in coefficients.iter().copied().take(runtime_levels.min(4)).enumerate() {
    consume(index, coefficient);
}

After inlining and O2, LLVM retains an alloca [4 x float] and pointer-based iterator state. ScalarEvolution reports the loop trip count and constant maximum as unpredictable, so bounded loop-unroll with upperbound or runtime enabled does not expose the array to SROA. This produces ld.local / st.local despite the four-element bound.

Relationship to #397 / #398

#398 scalarizes direct runtime indexing and selected addresses for small compiler-owned arrays. Iterator pointer state is a separate case because the eventual load is visible only after helper inlining and its pointer provenance has passed through loop state.

Desired result

Keep small arrays used through standard iter, copied, take, skip, and enumerate chains in SSA without requiring source-level loop expansion or indexing rewrites. Add a GPU regression asserting correct runtime-level behavior and no PTX .local, ld.local, or st.local.

Activity

  1. added
    optional-opts
    codegen
    on Aug 6, 2026
  2. nihalpasham commented on Sep 3, 2026

    @nihalpasham
    Collaborator

    I built your repro on current main (nightly-2026-08-28, LLVM 23), and the picture has flipped since July.

    • .local here means per-thread stack memory in PTX (ld.local / st.local).
    • Your iterator form now compiles to register-only code. The index loop with a break, the form people usually reach for instead, is the one that keeps the array in memory.
    let coefficients = [4.0_f32, -3.0, 4.0 / 3.0, -0.25];
    let n = levels[i] as usize;
    
    // A: iterator adapters (this issue)   -> 0 ld/st.local, 50 PTX instructions
    for (k, c) in coefficients.iter().copied().take(n.min(4)).enumerate() { acc += c * (k as f32 + 1.0); }
    
    // B: index loop with break            -> 4 st.local + 1 ld.local, 62 PTX instructions
    for k in 0..4 { if k >= n { break; } acc += coefficients[k] * (k as f32 + 1.0); }

    I consider this covered on main. One ask for future performance issues, per https://discord.com/channels/1515530041767759993/1515561621156663487/1540970771323551775: include the kernel where the traffic costs time and a before/after number.

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

    codegenoptional-opts

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions