A signed saturating add through NAK
I was browsing Mesa issues when add.sat.s32 caught my
eye. It's signed addition with saturation instead of wraparound.
NVIDIA's compiler knew what to do with it; NAK didn't have a native
path. There was already a five-instruction solution in the issue,
which gave me something to start with. I wanted to understand how
those instructions would fit into NAK and try implementing it.
Faith Ekstrand had already posted that five-instruction sequence in
the
Mesa issue. So I had a sequence to work from (the arithmetic came from there,
I wasn't figuring all of that out from scratch). What I needed to
understand was how to get NAK to express those .SIGN
operands and encode them, and then what the rest of the compiler
would do with them.. that last part mattered more than I expected.
Some names that will come up:
PTX NVIDIA's portable assembly language
SASS the machine instructions executed by an NVIDIA GPU
NIR Mesa's shared compiler intermediate representation
NAK Mesa's NVIDIA shader backend: NIR → SASS
GPR a 32-bit general-purpose register, one value per GPU lane
Pred a one-bit predicate register used for conditions
Starting with those five instructions
The issue contained this SASS:
IADD3 R0, R2, R5, RZ
PLOP3.LUT P0, PT, R5.SIGN, R2.SIGN, R0.SIGN, 0x2, 0x0
PLOP3.LUT P1, PT, R5.SIGN, R2.SIGN, R0.SIGN, 0x40, 0x0
SEL R9, R0, 0x7fffffff, !P0
SEL R9, R9, 0x80000000, !P1
There is an add, two Boolean lookups and two selects. But the part I
needed to understand was R5.SIGN. It reads the sign bit
of a GPR (so we're taking one bit from a normal register here,
not reading a predicate register). NAK already had
PLOP3 with predicate sources, which was useful, but
I couldn't just give that code a GPR and expect it to work. The IR
needed to describe what bit of that register we actually wanted.
Why NAK never saw the instruction
There was another part to this before even getting to the encoding.
NIR already had nir_op_iadd_sat, but NAK was asking NIR
to lower it unconditionally:
lower_uadd_sat: dev.sm < 70,
lower_usub_sat: dev.sm < 70,
lower_iadd_sat: true, // TODO
So even if I added the encoding, this operation would already be gone by the time we got there (lowered into other operations, not deleted ofc). I let it survive on SM70+, then followed it through NIR translation, NAK IR, legalization and encoding. That's why this touched several places for what eventually becomes five instructions.
What are those two LUT values?
The two LUT constants looked like magic numbers until I wrote down the
three signs: x, y, and the wrapping sum. Two
positive inputs with a negative sum mean positive overflow; two
negative inputs with a nonnegative sum mean negative overflow. Only
two rows of the truth table matter.
x.sign y.sign sum.sign result
0 0 1 positive overflow → INT_MAX
1 1 0 negative overflow → INT_MIN
Those rows are 001 and 110 in the chosen
input order. Bits 1 and 6: 0x02 and 0x40.
With that I could follow what the sequence was calculating. The next
part was getting NAK to generate it.
sum = IADD3(x, y, 0)
pos_overflow = PLOP3(x.sign, y.sign, sum.sign, LUT=0x02)
neg_overflow = PLOP3(x.sign, y.sign, sum.sign, LUT=0x40)
tmp = SEL(pos_overflow, INT_MAX, sum)
dst = SEL(neg_overflow, INT_MIN, tmp)
Looking at what ptxas generates
I used NVIDIA's tools for this, compiled a small PTX kernel with
ptxas and looked at the instructions and their encoding
with nvdisasm. That gave me something concrete to compare
with NAK's encoder.
.version 8.8
.target sm_89
.address_size 64
.visible .entry test_iadd_sat(
.param .u64 out_ptr,
.param .u64 in_ptr)
{
.reg .b64 %rd<4>;
.reg .s32 %r<4>;
ld.param.u64 %rd1, [out_ptr];
ld.param.u64 %rd2, [in_ptr];
add.u64 %rd3, %rd2, 4;
ld.global.s32 %r1, [%rd2];
ld.global.s32 %r2, [%rd3];
add.sat.s32 %r3, %r1, %r2;
st.global.u32 [%rd1], %r3;
ret;
}
The corresponding ptxas and
nvdisasm commands are:
ptxas -arch=sm_89 iadd_sat.ptx -o iadd_sat.cubin
nvdisasm --print-code --print-instruction-encoding iadd_sat.cubin |
rg 'IADD3|PLOP3|SEL'
The relevant output was the same five-instruction core shown in the issue:
IADD3 R2, R0, R3, RZ
PLOP3.LUT P0, PT, R0.SIGN, R3.SIGN, R2.SIGN, 0x2, 0x0
PLOP3.LUT P1, PT, R0.SIGN, R3.SIGN, R2.SIGN, 0x40, 0x0
SEL R7, R2, 0x7fffffff, !P0
SEL R7, R7, 0x80000000, !P1
The five-instruction core matched. I briefly considered reconstructing
the whole 128-bit PLOP3 format, but most of it was
already in NAK (the ordinary predicate variant, common ALU fields,
destinations..). So I didn't need to figure out every bit again,
I could compare that existing code with the .SIGN
encoding from CUDA and work on the differences.
The first encoder version reused NAK's common three-source ALU
encoding. The comparison left only the PLOP3 form, LUT,
and two predicate destinations to fill in:
e.encode_alu(0x01f, None,
Some(&self.srcs[0]),
Some(&self.srcs[1]),
Some(&self.srcs[2]));
e.set_field(72..80, self.ops[0].lut);
e.set_pred_dst(81..84, &self.dsts[0]);
e.set_pred_dst(84..87, &self.dsts[1]);
I then made NAK's disassembly test encode the IR and ask NVIDIA's decoder for the expected text:
export NAK_TEST=/path/to/mesa-build/src/nouveau/compiler/nak
PATH=/path/to/cuda/bin:$PATH \
"$NAK_TEST" \
--test --exact nvdisasm_tests::test_plop3 --test-threads 1
nvdisasm could read the new encoding, which was useful
to check that part. But it hadn't calculated anything yet, you know..
I still needed to run it on the GPU and compare the result (especially
near signed overflow, regular small numbers wouldn't tell me much
about whether the saturation worked).
My first version
On the IR side, my first version used one flag to choose between all-predicate sources and all-GPR-sign sources. Both overflow lookups needed three signs, so that covered what I was implementing. But it also meant that every source had to use the same kind of input.. I hadn't figured out the mixed-source case yet.
So the path through the compiler was:
NIR iadd_sat
→ NAK builder: IADD3 + two PLOP3 + two SEL
→ OpPLop3 with sign-mode GPR sources
→ legalization
→ SM70+ encoding
→ AD107
The local hardware test stores the GPU result and compares it with
Rust's saturating_add for boundary inputs and 100
deterministic random pairs:
PATH=/path/to/cuda/bin:$PATH \
"$NAK_TEST" \
--test --exact hw_tests::test_iadd_sat --test-threads 1
Then I tried CTS
The focused GPU test passed. So did the decoder test. Then I ran a focused 996-case CTS filter: 152 failures, all at a compiler assertion. So.. the small test passed but I had missed something.
844 pass
152 fail
failure: NAK assertion during pipeline compilation
These were failing while compiling the shaders, before they even ran on the GPU.
The difference was in the inputs. My small test used ordinary per-lane
GPRs, while CTS brought in values that NAK could put in uniform
registers (shared across lanes, so the compiler can represent them
differently). But NAK still considered every PLOP3
uniform-capable, including my new form. My .SIGN
encoding only accepted GPRs.. so the encoder was getting sources
it couldn't encode, even though the arithmetic itself worked in the
small test.
That explained why I needed to change more than the encoder.
I marked .SIGN PLOP3 non-uniform and
copied inputs to GPRs when needed (the value can still be the same
in every lane, we're changing where it is stored so this instruction
can read it). I also stopped the predicate optimizations from
treating these sources as predicates. After that the same CTS filter
passed all 996 cases.
// Ordinary predicate PLOP3 may be uniform; .SIGN PLOP3 may not.
Op::PLop3(op) => !op.src_sign,
// The encoder's .SIGN form consumes normal GPRs.
if self.src_sign {
for src in &mut self.srcs {
b.copy_alu_src_if_not_reg(src, RegFile::GPR, SrcType::GPR);
}
}
uniform value
→ copy to a normal GPR in each lane
→ warp PLOP3 R?.SIGN, R?.SIGN, R?.SIGN
export MESA_PREFIX=/path/to/patched-mesa-install
export VK_CTS=/path/to/VK-GL-CTS/build/external/vulkancts/modules/vulkan/deqp-vk
env VK_DRIVER_FILES="$MESA_PREFIX/share/vulkan/icd.d/nouveau_icd.x86_64.json" \
LD_LIBRARY_PATH="$MESA_PREFIX/lib" \
MESA_SHADER_CACHE_DISABLE=true \
"$VK_CTS" -n \
'dEQP-VK.spirv_assembly.instruction.compute.op*dotaccsatkhr.*' \
--deqp-log-filename=iadd_sat.qpa \
--deqp-log-images=disable
Passed: 996/996
Failed: 0
Is .SIGN really an instruction-wide switch?
Then I read Faith's proposed direction and Máté's work-in-progress.
They treated .SIGN as a modifier on each source. I had
used one switch for the whole instruction, which was enough for
iadd_sat but couldn't express those mixed sources. That
brought me back to the instruction itself: what combinations could it
actually take, and how would I represent them?
With three sources, each predicate (P) or sign bit (S), there are eight logical layouts. I knew the all-predicate and all-sign forms. For the mixed ones I didn't yet know whether each layout needed its own encoding. That's what I wanted to find out next.
I kept a known instruction fixed, varied its 12-bit opcode field
through all 4,096 possibilities, and kept the candidates that
nvdisasm decoded as PLOP3. Since the field
was only twelve bits, I could just try all of them.
PPP PPS PSP PSS SPP SPS SSP SSS
The scan only changed those 12 bits, so it could tell me about decoded opcode forms for that template—not every possible encoding or its runtime semantics.
0x81c PPP: predicate-only PLOP3
0x21d PSP: one .SIGN source
0x21e PSS: two .SIGN sources
0x21f SSS: three .SIGN sources
0xa1d–0xa1f constant-buffer source variants
0x89c uniform UPLOP3
The scanner kept the other 116 bits fixed, replaced bits 0–11, and
retained anything nvdisasm recognized as
PLOP3:
let base = [0x0200721f_u32, 0x00000003,
0x00700204, 0x000fc000];
for opcode in 0_u32..0x1000 {
let code = [
(base[0] & !0xfff) | opcode,
base[1], base[2], base[3],
];
// Write code as little-endian bytes, then:
nvdisasm -b SM89 --print-raw candidate.bin
}
For example, the controlled template changes like this:
logical form four 32-bit words low 12 bits
PPP 0200781c 00000003 00700204 000fc000 0x81c
SSS 0200721f 00000003 00700204 000fc000 0x21f
^ the scanned field is here
That showed what the decoder recognized. I still needed to check what these forms actually did on my AD107.
I found four useful forms for those eight layouts. The remaining layouts could use them too, by changing the source order. But that also changes which input goes into which position in the truth table (this is the part where just making the operands fit isn't enough). So I had to reorder the LUT along with the sources, otherwise I would have an instruction that encodes fine but calculates something different.
For a concrete reorder, suppose the logical operation is
SPP(a, b, c, lut): a is a GPR sign bit,
while b and c are predicates. The hardware
form puts the one sign source in the middle slot:
logical: SPP(a, b, c, lut)
physical: PSP(b, a, c, permute_xy(lut))
Only swapping a and b would make LUT input
x receive the old y. Applying the same
permutation to the LUT preserves all eight rows of the original
boolean function.
Now make the GPU check our reordering
To check that, I supplied Booleans as predicates or as GPR sign bits
using 0 and INT_MIN, and tested all eight
layouts. I chose asymmetric LUTs (0x1b and
0xe4) because the source order needs to affect the
answer here (if swapping inputs gives the same answer, my reorder
could be wrong and the test could still pass). That's 128 result
checks.
8 source layouts × 8 truth-table rows × 2 LUTs = 128 result checks
I also tried persuading ptxas to generate mixed-source
PLOP3 itself. It kept getting in the way, optimizing
simple probes into ISETP. Adding PTX
vote got me a PLOP3, but
ptxas materialized the sign as a predicate first. Still
no mixed-source example.
sx = (x < 0)
out = LUT(sx, p, q)
sign test + predicate logic → ISETP
opaque predicates via vote → predicate materialization → PPP PLOP3
I didn't keep those exact PTX files, so this is just the idea of what
I tried. Also, ptxas choosing this sequence doesn't tell
us that the hardware can't do mixed sources. For that I had the
decoder experiment and the truth-table tests on the GPU.
Where I stopped
The narrow iadd_sat path passed the AD107 hardware test,
decoder test, and 996 CTS cases. I also tried a Fossilize corpus: no
shader-output changes, because none of those shaders contained a
surviving nir_op_iadd_sat. So that corpus didn't actually
exercise this path, I can't use it to say anything about the
performance of the change.
The broader per-source model isn't ready to merge. Its 128 truth-table checks passed, but two of 62 NAK tests still failed. I posted the findings on the issue and asked the maintainers about the representation and patch split. No MR yet, I still need to understand and fix those two failures before this is ready :)
References
- Mesa work item #14153 — original CUDA sequence, LUT values, Vulkan property scope, and follow-up discussion.
-
Mesa NIR documentation
— the shared IR in which
nir_op_iadd_satlives. -
PTX integer
add— semantics ofadd.sat.s32. -
CUDA Binary Utilities
—
nvdisasmusage and instruction-encoding output. - SPIR-V specification — signed, unsigned, and mixed-signedness dot-accumulate-saturate operations exercised by CTS.