Files
metal2vulkan/crates
Anees Iqbal feba32b81f A narrow integer operation the AIR marks nuw keeps no mask
A narrow integer is held zero-extended in a u32 or u64 container, and an add, sub, mul or shl
that can carry above its width masks its result. One the AIR marks `nuw` no longer does: each
result that would carry is poison, so every other already fits the width. The common case is a
loop counter's increment: an i16-counted loop's latch read `((i + 1) & 0xFFFF) == N`, which hides
the trip count from RADV's loop analysis, so the loop stayed rolled and a private array it indexes
stayed in scratch. In the Super Resolution conv kernel 199ed62a647fd6f2 (a perfscout2 dump, read
on um790 only), removing those masks from the dumped module took the XTX from 12.5 to 0.69 ms a
dispatch and its scratch from 32768 to 0 bytes (job 25066), and the 780M from over 6 s to 6.0 ms
(25114). The inputs were synthetic and the kernel's stores never ran under them (25112, 25131),
so those times are loop cost only. `nsw` alone keeps the mask: `-1 + 1` carries out of the
zero-extended bits.

With that, a value's bits above its width are zero unless the value is poison. Where a poison
value meets something that must stay defined, it is masked:
- `freeze` of a narrow integer, which must answer a value of its type;
- the words a store writes (memory/leaf.rs words_of and vector_words), so a poison lane reaches
  no neighbouring byte or lane;
- a narrow atomic's operand (atomic/narrow.rs);
- a bitcast's packed integer lanes (ops::lane_bits, chunk::words_of).
The two-word form of i33..i63 without shaderInt64 keeps its masks.

Tests: engine_nuw, an i16 and an i8 loop counted by `add nuw` over a private array, with no
mask to the width in the module, against the same loops with a plain `add`, which keep it; both
sum to 18 on the interpreter, on four profiles. Kernels of a poison `add nuw`: its freeze stays in
range, and a store, a vector store, an atomic exchange and a bitcast of it keep the neighbouring
i16 0x1234. arith tests: nuw add, sub, mul and shl match LLVM wherever they are defined, at i5, i8,
i16 and i40.
2026-11-13 03:47:10 -08:00
..