Floating-point modes: the non-fused MAD/MAC family needs precise, and the s_round_mode NOOPs are not free #73

Closed
opened 2026-11-26 04:08:02 +00:00 by github-bot · 3 comments
Owner

@⁠MoonYueya on GitHub · 2026-11-23 00:47 UTC · original

Floating-point modes: the non-fused MAD/MAC family needs precise, and the s_round_mode NOOPs are not free

Two results from running the same DXIL on an RDNA2 iGPU and on WARP, both about the parts of emit.rs that a desk check cannot settle. Counts only, no game data. The corpus numbers come from a retail shader pack on my side; the GPU numbers come from a synthetic shader whose inputs are bit patterns I chose.

Machine: AMD Radeon(TM) Graphics (Raphael iGPU, gfx1036, wave lanes 32..64), driver 32.0.11024.2 (2024-06-25), Windows 11 build 26300. DXC 1.8.2502.11 from Windows SDK 10.0.26100.0. The reference column throughout is WARP running the identical DXIL module, not a second GPU.

1. The non-fused MAD/MAC family goes through mad_nf, and its precise is load-bearing

The four console-only opcodes the decoder names V_MAC_F32 (VOP2 0x1f / VOP3 0x11f), V_MAD_F32 (VOP3 0x141), V_MADMK_F32 (VOP2 0x20) and V_MADAK_F32 (VOP2 0x21) emit mad_nf at emit.rs:2729, :2732, :2736 and :2737. mad_nf (emit.rs:4534) is the two-rounding pair, and on this GPU its precise is what decides the result:

float mad_nf(float a, float b, float c) { precise float p = a * b; precise float r = p + c; return r; }
  • One vector pins it. a = 0x7f7fffff, b = 0x3f800001, c = 0xff7fffff gives 0x7f800000 with precise (the intermediate product overflows to +inf) and 0x73ffffff without it. A written-out t = a*b; r = t+c compiled at -Od gave the fused 0x73ffffff here, so one golden vector in the GPU tests is enough to keep this from regressing.
  • 4 of the 64 boundary vectors move when precise is present (indices 32, 34, 59, 60) - small enough to disappear in a sweep, large enough to matter.
  • Open: v_mad_f16 (emit.rs:2377) takes the same mad_nf path, where precise rounds in binary32 after f16tof32 rather than to binary16 per step. Intended, or a separate lowering?

2. s_round_mode is dropped 4,663 times in one pack

emit.rs:51 NOOPs s_denorm_mode, s_round_mode, s_setreg_imm32_b32 and s_setreg_b32. Decoded to the end of every program in this pack:

instruction occurrences distinct programs programs, % of 13,962
s_round_mode 4,663 3,305 23.7 %
s_denorm_mode 0 0 -
s_setreg_imm32_b32 67 35 0.25 %
s_setreg_b32 0 0 -

Of the 67 s_setreg_imm32_b32, 3 to 8 write MODE (in the decoded corpus: 3 instructions in 3 programs, hwreg id=1, offset=2, width=2, value 0x0; five more candidate words with the same encoding sit after the 40 programs the decoder does not get through, hence the upper bound). The other 64 write the flat-scratch base pair (id 20 = 0x0, id 21 = 0x400, 32 programs, always adjacent). No decoded write touches MODE[1:0], [5:4] or [7:6]. So in the part we decode the s_setreg NOOPs are harmless; s_round_mode is the one whose immediates decide it.

Three counting facts if you reproduce this on your pack:

  • s_round_mode carries its value in the instruction word. ENC_SOPP, opcode 36, one SIMM16 operand; in this pack every occurrence reads 0xBFA4xxxx, so the value is w & 0xFFFF, and 0xBFA5xxxx (s_denorm_mode) is 0. A raw count of 0xBFA4xxxx gives the identical 4,663 - occurrence count and value distribution are one pass.
  • s_setreg_imm32_b32 cannot be counted by byte scan. It is SOPK-with-literal, so of 1,221 candidate dwords only 67 were at an instruction start; the other 1,141 sat inside another instruction's literal slot (469 of them inside one v_add_f32 form). A grep-shaped count inflates by 18×.
  • Program starts are 12 bytes of prologue: v_nop 0xD5800000, then two dwords that are not instructions; the first real instruction is at dword 3, s_inst_prefetch 0xBFA00003, present in 13,905 of 13,962 programs, with the remaining 57 (all compute) starting on s_inst_prefetch directly. Decoding from the record start instead rejects at dword 2 and reports ~0 of these instructions.

These counts come from the PSO cache layer: 21,522 program records, 13,962 distinct contents, each decoded to its end (50.98 M dwords, 36.86 M instructions, 40 programs rejected). The 26,503-record / 26,698-program layer discussed in #36 is a different container, so the two are not comparable term for term.

Why the NOOP deserves a decision rather than a comment: MODE is per-wave state that shapes every float operation after the write, and dropping a write is equivalent only when the value written equals the value already in force. For the 3..8 s_setreg MODE writes that holds provided 0 selects the mode already in force (0 = round-to-nearest-even, the D3D default). For the 4,663 s_round_mode the immediate is exactly what decides, and a non-default immediate NOOPed silently costs nothing at translation, nothing at compile, and shows up only as a wrong image.

Cheapest first:

  1. Count w & 0xFFFF over the s_round_mode words in your pack. If every value is 0, and 0 selects the default rounding mode, the NOOP is equivalent for programs that start in that mode and the NOOPS entry can carry that in one line.
  2. If any value is non-zero, record a reason in unsupported / unsupported_why so the program is reported rather than NOOPed. You already report translation errors per program, so a non-default round mode becomes a visible skip instead of a wrong picture.
  3. The entry mode is not in the program at all. The AMD RDNA2 ISA carries it in SPI_SHADER_PGM_RSRC1_* (ISA §3.12), so reading it at pipeline creation and handing it to dxc.rs as a compile option would cover programs that inherit a non-default mode without ever writing it - proposed from that mechanism, and unconfirmed for this target (below).

What this does not settle

  • Which stage contracted the multiply and add. The measurement says the written-out form produced the fused value; nothing was disassembled, so whether DXC or the driver did the contracting is not established.
  • The imm16-to-MODE mapping. Not decoded on our side. The ISA text for s_round_mode only says set floating point round mode using an immediate constant, so value 0 = default rounding mode above is an assumption, not a decoded fact.
  • Whether this target exposes the same entry-mode field. SPI_SHADER_PGM_RSRC1_* is the AMD RDNA2 mechanism (ISA §3.12); that the Xbox target carries the entry mode the same way is unconfirmed, which is why item 3 above is a proposal.

Note on #67 / #70

Both PRs are about NVIDIA, whose D3D12 wave range is 32..32. Related from this side, for the opposite reason: DXC rejects [WaveSize] on pixel and vertex shaders (attribute WaveSize only allowed on compute or node shaders), and on the RDNA2 iGPU whose suite results are in #42 a pixel shader with no attribute runs 64 lanes - so the VS/PS case #67 lists as still open on NVIDIA has no hook on RDNA2 either.

🤖 AI-assisted: drafted by an LLM agent from the measurements above; every number was taken from a run, not from the model.

**@⁠MoonYueya** on GitHub · 2026-11-23 00:47 UTC · [original](https://github.com/xcl-dev/xcl/issues/73) # Floating-point modes: the non-fused MAD/MAC family needs `precise`, and the `s_round_mode` NOOPs are not free Two results from running the same DXIL on an RDNA2 iGPU and on WARP, both about the parts of `emit.rs` that a desk check cannot settle. Counts only, no game data. The corpus numbers come from a retail shader pack on my side; the GPU numbers come from a synthetic shader whose inputs are bit patterns I chose. **Machine:** AMD Radeon(TM) Graphics (Raphael iGPU, gfx1036, wave lanes 32..64), driver 32.0.11024.2 (2024-06-25), Windows 11 build 26300. DXC 1.8.2502.11 from Windows SDK 10.0.26100.0. The reference column throughout is WARP running the identical DXIL module, not a second GPU. ## 1. The non-fused MAD/MAC family goes through `mad_nf`, and its `precise` is load-bearing The four console-only opcodes the decoder names `V_MAC_F32` (VOP2 0x1f / VOP3 0x11f), `V_MAD_F32` (VOP3 0x141), `V_MADMK_F32` (VOP2 0x20) and `V_MADAK_F32` (VOP2 0x21) emit `mad_nf` at `emit.rs:2729`, `:2732`, `:2736` and `:2737`. `mad_nf` (`emit.rs:4534`) is the two-rounding pair, and on this GPU its `precise` is what decides the result: ```hlsl float mad_nf(float a, float b, float c) { precise float p = a * b; precise float r = p + c; return r; } ``` - **One vector pins it.** `a = 0x7f7fffff`, `b = 0x3f800001`, `c = 0xff7fffff` gives `0x7f800000` with `precise` (the intermediate product overflows to `+inf`) and `0x73ffffff` without it. A written-out `t = a*b; r = t+c` compiled at `-Od` gave the fused `0x73ffffff` here, so one golden vector in the GPU tests is enough to keep this from regressing. - **4 of the 64 boundary vectors move** when `precise` is present (indices 32, 34, 59, 60) - small enough to disappear in a sweep, large enough to matter. - **Open:** `v_mad_f16` (`emit.rs:2377`) takes the same `mad_nf` path, where `precise` rounds in binary32 after `f16tof32` rather than to binary16 per step. Intended, or a separate lowering? ## 2. `s_round_mode` is dropped 4,663 times in one pack `emit.rs:51` NOOPs `s_denorm_mode`, `s_round_mode`, `s_setreg_imm32_b32` and `s_setreg_b32`. Decoded to the end of every program in this pack: | instruction | occurrences | distinct programs | programs, % of 13,962 | |---|---:|---:|---:| | `s_round_mode` | **4,663** | **3,305** | 23.7 % | | `s_denorm_mode` | 0 | 0 | - | | `s_setreg_imm32_b32` | 67 | 35 | 0.25 % | | `s_setreg_b32` | 0 | 0 | - | Of the 67 `s_setreg_imm32_b32`, **3 to 8 write MODE** (in the decoded corpus: 3 instructions in 3 programs, `hwreg id=1, offset=2, width=2`, value `0x0`; five more candidate words with the same encoding sit after the 40 programs the decoder does not get through, hence the upper bound). The other 64 write the flat-scratch base pair (id 20 = `0x0`, id 21 = `0x400`, 32 programs, always adjacent). No decoded write touches MODE[1:0], [5:4] or [7:6]. So in the part we decode the `s_setreg` NOOPs are harmless; `s_round_mode` is the one whose immediates decide it. Three counting facts if you reproduce this on your pack: - **`s_round_mode` carries its value in the instruction word.** ENC_SOPP, opcode 36, one SIMM16 operand; in this pack every occurrence reads `0xBFA4xxxx`, so the value is `w & 0xFFFF`, and `0xBFA5xxxx` (`s_denorm_mode`) is 0. A raw count of `0xBFA4xxxx` gives the identical 4,663 - occurrence count and value distribution are one pass. - **`s_setreg_imm32_b32` cannot be counted by byte scan.** It is SOPK-with-literal, so of 1,221 candidate dwords only 67 were at an instruction start; the other 1,141 sat inside another instruction's literal slot (469 of them inside one `v_add_f32` form). A grep-shaped count inflates by 18×. - **Program starts are 12 bytes of prologue**: `v_nop 0xD5800000`, then two dwords that are not instructions; the first real instruction is at dword 3, `s_inst_prefetch 0xBFA00003`, present in 13,905 of 13,962 programs, with the remaining 57 (all compute) starting on `s_inst_prefetch` directly. Decoding from the record start instead rejects at dword 2 and reports ~0 of these instructions. These counts come from the PSO cache layer: 21,522 program records, 13,962 distinct contents, each decoded to its end (50.98 M dwords, 36.86 M instructions, 40 programs rejected). The 26,503-record / 26,698-program layer discussed in #36 is a different container, so the two are not comparable term for term. Why the NOOP deserves a decision rather than a comment: MODE is per-wave state that shapes every float operation after the write, and dropping a write is equivalent only when the value written equals the value already in force. For the 3..8 `s_setreg` MODE writes that holds provided 0 selects the mode already in force (0 = round-to-nearest-even, the D3D default). For the 4,663 `s_round_mode` the immediate is exactly what decides, and a non-default immediate NOOPed silently costs nothing at translation, nothing at compile, and shows up only as a wrong image. Cheapest first: 1. **Count `w & 0xFFFF` over the `s_round_mode` words in your pack.** If every value is 0, and 0 selects the default rounding mode, the NOOP is equivalent for programs that start in that mode and the `NOOPS` entry can carry that in one line. 2. **If any value is non-zero, record a reason in `unsupported` / `unsupported_why` so the program is reported rather than NOOPed.** You already report translation errors per program, so a non-default round mode becomes a visible skip instead of a wrong picture. 3. **The entry mode is not in the program at all.** The AMD RDNA2 ISA carries it in `SPI_SHADER_PGM_RSRC1_*` (ISA §3.12), so reading it at pipeline creation and handing it to `dxc.rs` as a compile option would cover programs that inherit a non-default mode without ever writing it - proposed from that mechanism, and unconfirmed for this target (below). ## What this does not settle - **Which stage contracted the multiply and add.** The measurement says the written-out form produced the fused value; nothing was disassembled, so whether DXC or the driver did the contracting is not established. - **The imm16-to-MODE mapping.** Not decoded on our side. The ISA text for `s_round_mode` only says `set floating point round mode using an immediate constant`, so `value 0 = default rounding mode` above is an assumption, not a decoded fact. - **Whether this target exposes the same entry-mode field.** `SPI_SHADER_PGM_RSRC1_*` is the AMD RDNA2 mechanism (ISA §3.12); that the Xbox target carries the entry mode the same way is unconfirmed, which is why item 3 above is a proposal. ## Note on #67 / #70 Both PRs are about NVIDIA, whose D3D12 wave range is 32..32. Related from this side, for the opposite reason: DXC rejects `[WaveSize]` on pixel and vertex shaders (`attribute WaveSize only allowed on compute or node shaders`), and on the RDNA2 iGPU whose suite results are in #42 a pixel shader with no attribute runs 64 lanes - so the VS/PS case #67 lists as still open on NVIDIA has no hook on RDNA2 either. 🤖 AI-assisted: drafted by an LLM agent from the measurements above; every number was taken from a run, not from the model.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:08:02 +00:00
Author
Owner

@⁠MoonYueya on GitHub · 2026-11-23 01:28 UTC · original

Follow-up with the numbers the first post left out.

s_round_mode immediate values (same corpus and decoder as above: 13,962 distinct programs, walked instruction by instruction from the real first instruction, so no literal-slot hits):

simm16 instructions share
0xC 4,449 95.4%
0x0 214 4.6%
anything else 0 —
  • All 3,305 programs that contain it are graphics programs; 198 contain both values, and no program contains only 0x0.
  • None of the 4,663 sits in the first 16 instructions, and none sits inside a backward-branch range (3,347 of them are in programs that do have loops, so this is not vacuous).

How sure the bit meaning is. The MODE field layout is confirmed from LLVM SIDefines.h (FP_ROUND_MODE_SP = bits 1:0, FP_ROUND_MODE_DP = bits 3:2, covering f64 and f16). That s_round_mode's simm16 writes MODE[3:0] directly is an inference: the ISA XML gives the operand only as SIMM16, and LLVM never generates the instruction. Under that reading, 0xC keeps f32 at round-to-nearest-even and sets f64/f16 to round-toward-zero. So an f32-only check cannot clear the programs that write 0xC; the f16/f64 work after each write is what matters (we have not yet paired each write with a later f16 instruction).

What precise does not cover. precise only stops reassociation and contraction. DXIL has no dynamic rounding-mode control (the mode is fixed at compile time and no instruction sets it at runtime). -Gis does not set a mode either; it only changes code generation (on a 160-program sample, dxc accepted all 160 and the DXIL changed for 158). So a non-default mode can only be reproduced by emitting the rounding explicitly, for example f32→f16 round-toward-zero done with integer operations. On our side:

  • Already implemented and unit-tested in our fork: the default value is a counted NOOP, and any non-default value is reported through unsupported / unsupported_why, not dropped.
  • Being measured now:
    1. A data-flow check per write: if no f16/f64 operation runs before the next write or the end of the program, the NOOP is exact.
    2. Otherwise, explicit round-toward-zero emission for the affected operations, with unit tests at the edges (±0, subnormals, Inf/NaN, ties).

We will post the split and send a PR once it is measured.

Accounting correction for s_setreg_imm32_b32. Of the 1,221 candidate words, 67 are instruction starts and 1,141 sit inside another instruction. The remaining 13 sit in programs where our decode stopped before them: 6 of the 451 candidate-bearing programs (the full-corpus pass rejects 40 programs, a different denominator). Five of those 13 are the same instruction word as the three confirmed MODE writes, so the MODE-write count is 3 in the decoded part and at most 8 overall. The first post should have said "5 of the 13" rather than "after the 40 programs"; the numbers 3 and 8 are unchanged.

The f16 question about v_mad_f16 going through the f32 mad_nf is still open on our side; we have not measured it.

🤖 AI-assisted: drafted by an LLM agent from our measurements; counts are from runs, not from the model.

**@⁠MoonYueya** on GitHub · 2026-11-23 01:28 UTC · [original](https://github.com/xcl-dev/xcl/issues/73#issuecomment-7089164712) Follow-up with the numbers the first post left out. **`s_round_mode` immediate values** (same corpus and decoder as above: 13,962 distinct programs, walked instruction by instruction from the real first instruction, so no literal-slot hits): | `simm16` | instructions | share | |---|---:|---:| | `0xC` | 4,449 | 95.4% | | `0x0` | 214 | 4.6% | | anything else | 0 | — | - All 3,305 programs that contain it are graphics programs; 198 contain both values, and no program contains only `0x0`. - None of the 4,663 sits in the first 16 instructions, and none sits inside a backward-branch range (3,347 of them are in programs that do have loops, so this is not vacuous). **How sure the bit meaning is.** The MODE field layout is confirmed from LLVM `SIDefines.h` (`FP_ROUND_MODE_SP` = bits 1:0, `FP_ROUND_MODE_DP` = bits 3:2, covering f64 and f16). That `s_round_mode`'s `simm16` writes MODE[3:0] directly is an inference: the ISA XML gives the operand only as `SIMM16`, and LLVM never generates the instruction. Under that reading, `0xC` keeps f32 at round-to-nearest-even and sets f64/f16 to round-toward-zero. So an f32-only check cannot clear the programs that write `0xC`; the f16/f64 work after each write is what matters (we have not yet paired each write with a later f16 instruction). **What `precise` does not cover.** `precise` only stops reassociation and contraction. DXIL has no dynamic rounding-mode control (the mode is fixed at compile time and no instruction sets it at runtime). `-Gis` does not set a mode either; it only changes code generation (on a 160-program sample, dxc accepted all 160 and the DXIL changed for 158). So a non-default mode can only be reproduced by emitting the rounding explicitly, for example f32→f16 round-toward-zero done with integer operations. On our side: - Already implemented and unit-tested in our fork: the default value is a counted NOOP, and any non-default value is reported through `unsupported` / `unsupported_why`, not dropped. - Being measured now: 1. A data-flow check per write: if no f16/f64 operation runs before the next write or the end of the program, the NOOP is exact. 2. Otherwise, explicit round-toward-zero emission for the affected operations, with unit tests at the edges (±0, subnormals, Inf/NaN, ties). We will post the split and send a PR once it is measured. **Accounting correction for `s_setreg_imm32_b32`.** Of the 1,221 candidate words, 67 are instruction starts and 1,141 sit inside another instruction. The remaining 13 sit in programs where our decode stopped before them: 6 of the 451 candidate-bearing programs (the full-corpus pass rejects 40 programs, a different denominator). Five of those 13 are the same instruction word as the three confirmed MODE writes, so the MODE-write count is 3 in the decoded part and at most 8 overall. The first post should have said "5 of the 13" rather than "after the 40 programs"; the numbers 3 and 8 are unchanged. The f16 question about `v_mad_f16` going through the f32 `mad_nf` is still open on our side; we have not measured it. 🤖 AI-assisted: drafted by an LLM agent from our measurements; counts are from runs, not from the model.
Author
Owner

@⁠ivall on GitHub · 2026-11-23 10:48 UTC · original

Fixed by #91 (merged, ecbad66 on main): s_round_mode / s_setreg* writes of MODE[3:0] are scanned first; default writes stay counted NOOPs, a non-default f16 / f64 mode (your 0xC) in a program with f16 ops is tracked and the f16 results rounded accordingly, and a non-default f32 / f64 mode is reported unsupported instead of dropped (none occur in our pack – it still translates 26 698 / 26 698). v_mad_f16 now rounds per step, and the non-fused MAD/MAC precise is pinned by a GPU test with your overflow vector. Thanks for the careful numbers – the entry mode from SPI_SHADER_PGM_RSRC1 is still open if someone finds it on this target.

**@⁠ivall** on GitHub · 2026-11-23 10:48 UTC · [original](https://github.com/xcl-dev/xcl/issues/73#issuecomment-7098510754) Fixed by #91 (merged, ecbad66 on `main`): `s_round_mode` / `s_setreg*` writes of MODE[3:0] are scanned first; default writes stay counted NOOPs, a non-default f16 / f64 mode (your `0xC`) in a program with f16 ops is tracked and the f16 results rounded accordingly, and a non-default f32 / f64 mode is reported unsupported instead of dropped (none occur in our pack – it still translates 26 698 / 26 698). `v_mad_f16` now rounds per step, and the non-fused MAD/MAC `precise` is pinned by a GPU test with your overflow vector. Thanks for the careful numbers – the entry mode from `SPI_SHADER_PGM_RSRC1` is still open if someone finds it on this target.
Author
Owner

@⁠MoonYueya on GitHub · 2026-11-23 14:43 UTC · original

Thanks for landing this in #91, including the non-fused MAD/MAC overflow vector as a GPU test. Nothing further from our side; we'll only come back if we can read SPI_SHADER_PGM_RSRC1 from a real executable.

AI-assisted: drafted with an AI assistant, reviewed before posting.

**@⁠MoonYueya** on GitHub · 2026-11-23 14:43 UTC · [original](https://github.com/xcl-dev/xcl/issues/73#issuecomment-7104447839) Thanks for landing this in #91, including the non-fused MAD/MAC overflow vector as a GPU test. Nothing further from our side; we'll only come back if we can read `SPI_SHADER_PGM_RSRC1` from a real executable. _AI-assisted: drafted with an AI assistant, reviewed before posting._
This discussion has been locked. Commenting is limited to contributors.
No description provided.