Skip to content

Projecting a field of a tuple array element copies the whole array, once per field #693

Closed

Description

@vyncint

Problem

Reading one field of a tuple element out of a local array copies the whole
array
to a fresh stack slot first — once per field projected.

#231 fixed the nested-array shape (scratch[row][col]). This is the
array-of-tuple shape, where the projection is a field rather than a second index,
and it still materializes the entire array per projection.

Reproducer

// (u8, u32): 8 bytes with three bytes of interior padding.
const PAIRS: [(u8, u32); 256] = /* ... */;

#[kernel]
pub fn tuple_table(input: &[u32], mut output: DisjointSlice<u32>) {
    let idx = thread::index_1d();
    let i = idx.get();
    if i >= input.len() { return; }
    let (a, b) = PAIRS[(input[i] >> 24) as usize & 255];
    if let Some(o) = output.get_mut(idx) { *o = a as u32 + b; }
}

Emitted LLVM IR contains three whole-array allocas — the table's own storage
plus one per destructured field:

%v11 = alloca [256 x { i8, [3 x i8], i32 }], align 4   ; the table
%v29 = alloca [256 x { i8, [3 x i8], i32 }], align 4   ; copy for field `a`
%v30 = getelementptr inbounds [256 x { i8, [3 x i8], i32 }], ptr %v29, i32 0, i64 %v26
%v34 = alloca [256 x { i8, [3 x i8], i32 }], align 4   ; copy for field `b`
%v35 = getelementptr inbounds [256 x { i8, [3 x i8], i32 }], ptr %v34, i32 0, i64 %v26

%v29 and %v34 survive opt -O2. On sm_86 this kernel is 925 PTX
instructions with a 4096-byte local depot and 878 st.local
— two 2 KiB
per-thread copies of a 2 KiB table, for a single element read.

Why it matters beyond the instruction count

The copies dominate, so they mask improvements to the table's own storage. While
extending #684 to more element types (#692) I confirmed that promoting such a
table to an immutable device global changes nothing measurable: the depot and the
878 stores stay exactly as they are, and the module merely gains a 2 KiB global.
That is why field-less enums are worth promoting and tuples are not — until these
copies are gone.

Measured on A10G (sm_86, driver 595.71.05), with and without the promotion, the
kernel is identical at 925 instructions / 4096-byte depot / 878 st.local.

Expected

A field projection of an array element should stay address-based to the final
scalar load, as #231 established for the nested-index case:

%elem = getelementptr inbounds [256 x { i8, [3 x i8], i32 }], ptr %table, i32 0, i64 %index
%a_ptr = getelementptr inbounds { i8, [3 x i8], i32 }, ptr %elem, i32 0, i32 0
%a = load i8, ptr %a_ptr

with no additional whole-array alloca, and one per field, not one per array.

Notes

Activity

  1. added
    bug
    perf
    codegen
    IR-lowering
    on Aug 8, 2026
  2. added a commit that references this issue on Aug 8, 2026
    dcf8b6c
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

    IR-loweringbugcodegenperf

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions