Repository navigation
Description
Problem
#684 stopped bare array constants being built per thread, but its element gate
admits only primitive scalars and nested arrays of those. A field-less enum
array is left out, even though such an array holds nothing but discriminants and
so is byte-identical to the [u32; N] of its values.
const OPS: [Op; 256] where Op is a field-less #[repr(u32)] enum is still
materialized into the per-thread local depot — one st.local per element in
every thread, for data the module image already carries — and the lookup then
reads thread-private memory.
There is also no way to spell around it. The reference form that works for scalar
tables is rejected outright:
const R: &[Op; 256] = &OPS;
// error: Compilation error: invalid input program.validate_ptr_to_array_constant_type keeps enums out of the pointer-to-array
path too, so a field-less enum table has no fast spelling at all.
Reproducer
#[derive(Clone, Copy)]
#[repr(u32)]
pub enum Op { Add = 3, Sub = 5, Mul = 7, Div = 11 }
const OPS: [Op; 256] = /* ... */;
#[kernel]
pub fn lut(input: &[u32], mut output: DisjointSlice<u32>) {
let idx = thread::index_1d();
let i = idx.get();
if i >= input.len() { return; }
let v = OPS[(input[i] >> 24) as usize & 255] as u32;
if let Some(o) = output.get_mut(idx) { *o = v; }
}Measured
A10G (sm_86, driver 595.71.05), 64 dependent lookups per thread over 8388608
threads, 50 timed launches after 10 warmup, divergent index. Each enum kernel is
paired with a [u32; N] kernel over the same discriminants, so the pair isolates
the element type; all four are bit-identical against a host reference.
| entries | [Op; N] |
[u32; N] |
enum / u32 |
|---|---|---|---|
| 16 | 1309.4 us | 361.4 us | 3.62x |
| 256 | 16073.3 us | 327.3 us | 49.11x |
PTX for the 256-entry pair: 309 instructions with a 1 KiB local depot and 256
st.local for the enum against 70 instructions with no depot for the
integer table.
Proposed fix
Widen the gate to admit field-less enums and nested arrays of them. The byte
image is rustc's evaluated allocation, so the discriminants are already exact;
what the gate has to establish is that the element is cheap to read back, which a
field-less enum is — the whole element is its discriminant.
Payload-carrying enums, tuples and structs should stay out, and not because the
byte image would be wrong — see the separate report I am filing about per-field
array copies, which dominate those shapes and would leave a promoted global as
dead weight beside an unchanged depot.
I have this working and measured, and will open a PR.
Note on array_constants
array_constants' verify-code-shape.sh pins the enum array's current
insertvalue chain, so it needs updating to the new shape. Its runtime
assertions pass unchanged; the values were never in question.
Activity
fix(mir-importer): admit a field-less enum table through its referenc…
perf(mir-importer): promote a field-less enum array constant like an …