Repository navigation
Description
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_ptrwith no additional whole-array alloca, and one per field, not one per array.
Notes
- Both fields projected from the same element produce two separate copies, so the
cost scales with the number of fields read, not with how many elements are
touched. - I am not proposing a fix here; I ran into this while measuring Field-less enum array constants are still materialized per thread, and &[Op; N] is rejected outright #692 and it
seemed worth recording with the evidence rather than leaving it implicit in a
code comment.
Activity
Address a tuple field read instead of copying the whole array (NVIDIA…
perf(mir-importer): promote a field-less enum array constant like an …