rdna2spirv: emit OpBitFieldUExtract for the VOP3 16-bit result mask (NVIDIA And-&-0xFFFF miscompile) #1585

Open
github-bot wants to merge 3 commits from github-bot/gh-1585 into main AGit
Owner

@⁠whoalin1 on GitHub · 2026-11-25 05:12 UTC · original
whoalin1:nc-i16-clamp-nv → main · +25 −9 · 4 files · 3 commits · open

Finding from the overnight fuzz campaign (coordination issue #14 — the issue's comment thread is currently at its cap, so context lives here): on NVIDIA'''s shader compiler, OpBitwiseAnd %uint X, 0xFFFF miscompiles when X is a value that passed through %int-typed ops and the result feeds a preserve-high-half merge (old & 0xFFFF0000) | lo — the kept high half comes out corrupted while the low half is fine. Measured on an RTX 4070 SUPER (Windows, wave32): v_add_nc_i16 v14, v3, v12 op_sel:[1,1,0] clamp wrote 0x1113AAA8 for the correct 0x0002AAA8.

OpBitFieldUExtract %uint X, 0, 16 is spec-identical (zero-extended low 16) and does not trigger the miscompile. This PR emits it at every site where an int-derived value is masked for a preserved-16-bit-half merge:

  • the VOP3 16-bit integer results (v_add/sub_{nc_,}{i16,u16}, the _rev/shift forms, v_mul_lo_u16, v_{min,max}_{u16,i16}, v_mad_{u16,i16}, v_{min,max,med}3_{u16,i16})
  • the signed v_cvt_i16_f16 / v_cvt_norm_i16_f16 conversions (a Bitcast of an %int OpConvertFToS)
  • the signed saturate_int16 feeding d16 image/buffer stores (a Bitcast of an %int SClamp)

The unsigned/unorm siblings, the v_pk_* packed paths, and the f16 put_f16_bits idiom were audited and deliberately left: they either produce %uint-only values or build a packed dword rather than a preserve merge.

Scope note (thanks @⁠ecox982745): because the signed and unsigned conversions share the result-mask helper, the emitted SPIR-V for v_cvt_u16_f16 and v_cvt_norm_u16_f16 also gains one OpBitFieldUExtract each (like their signed siblings), and v_cvt_pk_i16_i32 gains two through the signed saturation helper, one per packed operand. These are the same spec-identical zero-extended-low-16 rewrite at zero cost; ecox982745's independent RTX 4090 sweep of all 65,536 binary16 encodings for each of v_cvt_{i16,u16}_f16 / v_cvt_norm_{i16,u16}_f16 plus 65,536 v_cvt_pk_i16_i32 input pairs found 0 mismatches in 327,680 outputs per revision, with all GPU buffers byte-identical between revisions.

Verification

  • NVIDIA RTX 4070 SUPER (Windows, wave32): the original repro now returns 0x0002AAA8; v_sub_nc_i16 … clamp 0x0002AAAC; 160 randomized v_add/sub_nc_i16 programs across all op_sel/clamp combos — 0 mismatches vs a Python reference. v_cvt_i16_f16/v_cvt_norm_i16_f16 spot checks produce correct results on both old and new SPIR-V (the miscompile did not reproduce on that shape, but the extract removes the risky pattern at zero cost).
  • AMD Radeon iGPU (gfx1036, Windows driver 24.10.24, --pc-tail-ubo, LEONIDA_PC_TAIL_UBO=1): repro, sub-clamp, mad_i16, min3 cases all match NVIDIA/reference — no AMD regression (the op is spec-identical).
  • Semantic suite: python -I scripts/run_semantic_tests.py tests/semantic — 487 files / 6,279 tests, 0 failures. Two tests that pinned the OpBitwiseAnd … 0xFFFF shape were updated to the extract form with their negative controls intact.
  • compileall clean; the changed ops all pass spirv-val.
  • docs/design.md contract-table row for v_add_nc_i16/v_sub_nc_i16 documents the workaround, the trigger shape, and the confirmation method.

Independent review: initial pass found three missed sites of the same idiom (now covered); re-review confirmed the six-site coverage is exhaustive against a full census of And … 0xFFFF emit sites.

AI-assisted (Devin).

**@⁠whoalin1** on GitHub · 2026-11-25 05:12 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1585) `whoalin1:nc-i16-clamp-nv` → `main` · +25 −9 · 4 files · 3 commits · open Finding from the overnight fuzz campaign (coordination issue #14 — the issue's comment thread is currently at its cap, so context lives here): on NVIDIA'\''s shader compiler, `OpBitwiseAnd %uint X, 0xFFFF` miscompiles when `X` is a value that passed through `%int`-typed ops and the result feeds a preserve-high-half merge `(old & 0xFFFF0000) | lo` — the kept high half comes out corrupted while the low half is fine. Measured on an RTX 4070 SUPER (Windows, wave32): `v_add_nc_i16 v14, v3, v12 op_sel:[1,1,0] clamp` wrote `0x1113AAA8` for the correct `0x0002AAA8`. `OpBitFieldUExtract %uint X, 0, 16` is spec-identical (zero-extended low 16) and does not trigger the miscompile. This PR emits it at every site where an int-derived value is masked for a preserved-16-bit-half merge: - the VOP3 16-bit integer results (`v_add/sub_{nc_,}{i16,u16}`, the `_rev`/shift forms, `v_mul_lo_u16`, `v_{min,max}_{u16,i16}`, `v_mad_{u16,i16}`, `v_{min,max,med}3_{u16,i16}`) - the signed `v_cvt_i16_f16` / `v_cvt_norm_i16_f16` conversions (a `Bitcast` of an `%int` `OpConvertFToS`) - the signed `saturate_int16` feeding d16 image/buffer stores (a `Bitcast` of an `%int` `SClamp`) The unsigned/unorm siblings, the `v_pk_*` packed paths, and the f16 `put_f16_bits` idiom were audited and deliberately left: they either produce `%uint`-only values or build a packed dword rather than a preserve merge. Scope note (thanks @⁠ecox982745): because the signed and unsigned conversions share the result-mask helper, the emitted SPIR-V for `v_cvt_u16_f16` and `v_cvt_norm_u16_f16` also gains one `OpBitFieldUExtract` each (like their signed siblings), and `v_cvt_pk_i16_i32` gains two through the signed saturation helper, one per packed operand. These are the same spec-identical zero-extended-low-16 rewrite at zero cost; ecox982745's independent RTX 4090 sweep of all 65,536 binary16 encodings for each of `v_cvt_{i16,u16}_f16` / `v_cvt_norm_{i16,u16}_f16` plus 65,536 `v_cvt_pk_i16_i32` input pairs found 0 mismatches in 327,680 outputs per revision, with all GPU buffers byte-identical between revisions. ## Verification - NVIDIA RTX 4070 SUPER (Windows, wave32): the original repro now returns `0x0002AAA8`; `v_sub_nc_i16 … clamp` `0x0002AAAC`; 160 randomized `v_add/sub_nc_i16` programs across all `op_sel`/`clamp` combos — **0 mismatches** vs a Python reference. `v_cvt_i16_f16`/`v_cvt_norm_i16_f16` spot checks produce correct results on both old and new SPIR-V (the miscompile did not reproduce on that shape, but the extract removes the risky pattern at zero cost). - AMD Radeon iGPU (gfx1036, Windows driver 24.10.24, `--pc-tail-ubo`, `LEONIDA_PC_TAIL_UBO=1`): repro, sub-clamp, mad_i16, min3 cases all match NVIDIA/reference — no AMD regression (the op is spec-identical). - Semantic suite: `python -I scripts/run_semantic_tests.py tests/semantic` — 487 files / 6,279 tests, 0 failures. Two tests that pinned the `OpBitwiseAnd … 0xFFFF` shape were updated to the extract form with their negative controls intact. - `compileall` clean; the changed ops all pass `spirv-val`. - `docs/design.md` contract-table row for `v_add_nc_i16`/`v_sub_nc_i16` documents the workaround, the trigger shape, and the confirmation method. Independent review: initial pass found three missed sites of the same idiom (now covered); re-review confirmed the six-site coverage is exhaustive against a full census of `And … 0xFFFF` emit sites. AI-assisted (Devin).
NVIDIA's shader compiler (RTX 4070 SUPER, wave32) corrupts the preserved
high half of a 16-bit VOP3 result when OpBitwiseAnd & 0xFFFF consumes a
value that passed through %int ops and the result merges with
(old & 0xFFFF0000): v_add_nc_i16 v14, v3, v12 op_sel:[1,1,0] clamp wrote
0x1113AAA8 for 0x0002AAA8. BitFieldUExtract reads the same low 16 bits
and dodges the peephole; SClamp on %int and select-based clamps do not.
AMD (gfx1036, Vulkan and hw_oracle) and the CPU interpreter are
unaffected.

Co-Authored-By: Devin <[email protected]>
The review of 97535ad found three more OpBitwiseAnd & 0xFFFF sites whose
operand is an %int-derived bitcast feeding a preserve-high-half merge,
the same shape NVIDIA's shader compiler miscompiles:

- v_cvt_i16_f16: OpConvertFToS %int -> Bitcast -> And, then write_lo16
  or the op_sel merge (the u16 sibling is %uint and safe)
- v_cvt_norm_i16_f16: the snorm path has the same structure (unorm safe)
- saturate_int16 signed path: SClamp %int -> Bitcast -> And reaches the
  preserve merges through the d16 image/buffer stores

All emit OpBitFieldUExtract %uint x, 0, 16 instead, spec-identical.
Audited every remaining And & 0xFFFF site: the rest are %uint-derived,
operand-side, packed-Or consumers, or constant masks.

RTX 4070 SUPER spot-checks of v_cvt_i16_f16/v_cvt_norm_i16_f16 (VOP2
form; llvm-mc rejects op_sel on these) produce the right words on both
the old and new modules, so the bug does not reproduce on this shape;
the extract removes the risky pattern regardless. Full semantic suite:
487 files, 6279 tests, 0 failures.

Co-Authored-By: Devin <[email protected]>
The literal pipe in the NVIDIA workaround row made tests/test_docs_design_rows.py
read the row as having more cells than the contract table header.

Co-Authored-By: SWE-2.1 Max <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:57:17 +00:00
Author
Owner

@⁠ecox982745 on GitHub · 2026-11-25 05:17 UTC · original

Independent RTX 4090 check of the conversion changes in 7f85594b, compared with the preceding PR commit 97535ad1 (Windows, NVIDIA 616.64).

One scope detail for the PR description: the shared result mask also changes the SPIR-V for v_cvt_u16_f16 and v_cvt_norm_u16_f16. Each gains one OpBitFieldUExtract, just like its signed sibling. v_cvt_pk_i16_i32 also gains two through the signed saturation helper, one per packed operand.

I dispatched synthetic wave32 compute shaders with runtime inputs and checked the full 32-bit destination against a CPU reference:

  • All 65,536 binary16 encodings for each of v_cvt_{i16,u16}_f16 and v_cvt_norm_{i16,u16}_f16, writing the low half with a varying previous destination. This checks the converted value and the preserved high half, including NaNs, infinities, subnormals, and both zeros.
  • 65,536 signed input pairs for v_cvt_pk_i16_i32, including in-range values and saturation in each half.

0 mismatches across 327,680 outputs on each revision. All five GPU buffers were byte-identical between revisions, and all ten translated modules passed spirv-val --target-env vulkan1.2. This covers the conversion follow-up. The add/sub driver reproducer is a separate case.

**@⁠ecox982745** on GitHub · 2026-11-25 05:17 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1585#issuecomment-7153626183) Independent RTX 4090 check of the conversion changes in `7f85594b`, compared with the preceding PR commit `97535ad1` (Windows, NVIDIA 616.64). One scope detail for the PR description: the shared result mask also changes the SPIR-V for `v_cvt_u16_f16` and `v_cvt_norm_u16_f16`. Each gains one `OpBitFieldUExtract`, just like its signed sibling. `v_cvt_pk_i16_i32` also gains two through the signed saturation helper, one per packed operand. I dispatched synthetic wave32 compute shaders with runtime inputs and checked the full 32-bit destination against a CPU reference: - All 65,536 binary16 encodings for each of `v_cvt_{i16,u16}_f16` and `v_cvt_norm_{i16,u16}_f16`, writing the low half with a varying previous destination. This checks the converted value and the preserved high half, including NaNs, infinities, subnormals, and both zeros. - 65,536 signed input pairs for `v_cvt_pk_i16_i32`, including in-range values and saturation in each half. **0 mismatches across 327,680 outputs on each revision.** All five GPU buffers were byte-identical between revisions, and all ten translated modules passed `spirv-val --target-env vulkan1.2`. This covers the conversion follow-up. The add/sub driver reproducer is a separate case.
Author
Owner

@⁠QemmHD on GitHub · 2026-11-25 18:41 UTC · original

Heads-up from a test run on an RTX 4070 SUPER: the code part looks fine, but merged onto current main this PR fails tests/test_docs_design_rows: a docs/design.md table row has more cells than the table header. Fixing that row should let it pass the consistency set.

AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠QemmHD** on GitHub · 2026-11-25 18:41 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1585#issuecomment-7171252182) Heads-up from a test run on an RTX 4070 SUPER: the code part looks fine, but merged onto current main this PR fails `tests/test_docs_design_rows`: a `docs/design.md` table row has more cells than the table header. Fixing that row should let it pass the consistency set. AI-assisted (Claude Code, Claude Opus 5.5).
Author
Owner

@⁠whoalin1 on GitHub · 2026-11-25 18:46 UTC · original

Thanks — fixed in 42ad738a6. The literal pipe in the preserved-half merge example is now escaped, and python -I tests/test_docs_design_rows.py passes locally (5 tests, OK). No code behavior changed.

**@⁠whoalin1** on GitHub · 2026-11-25 18:46 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1585#issuecomment-7171375377) Thanks — fixed in `42ad738a6`. The literal pipe in the preserved-half merge example is now escaped, and `python -I tests/test_docs_design_rows.py` passes locally (5 tests, OK). No code behavior changed.
Author
Owner

@⁠whoalin1 on GitHub · 2026-11-25 20:18 UTC · original

@⁠QemmHD thanks for the catch. Rebased onto current main (db1a22d29) as 69b363d75; no conflicts. On the rebased tree the contract-table row is well-formed — the \| in the preserved-half merge is escaped exactly as tests/test_docs_design_rows expects, and a scan with the test's own logic over all three pages (design.md, README.md, testing.md) finds no mis-shaped rows and no repeated topics, so the consistency set should now pass on CI. AI-assisted with Devin.

**@⁠whoalin1** on GitHub · 2026-11-25 20:18 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1585#issuecomment-7173481583) @⁠QemmHD thanks for the catch. Rebased onto current main (`db1a22d29`) as `69b363d75`; no conflicts. On the rebased tree the contract-table row is well-formed — the `\|` in the preserved-half merge is escaped exactly as `tests/test_docs_design_rows` expects, and a scan with the test's own logic over all three pages (`design.md`, `README.md`, `testing.md`) finds no mis-shaped rows and no repeated topics, so the consistency set should now pass on CI. AI-assisted with Devin.
Author
Owner

@⁠whoalin1 on GitHub · 2026-11-25 20:51 UTC · original

Useful scope data point from Sangonapou (coordination issue): the high-half corruption does not reproduce on Ampere (RTX 3080 Ti Laptop, 621.24) — 384/384 single-op cases match RDNA2. That is consistent with this PR's evidence, where the miscompile showed on Ada (RTX 4070 SUPER) with the surrounding fuzz-program instructions, and points at an Ada-generation compiler issue rather than something universal. Either way the transform here is bit-identical semantics on every device — OpBitFieldUExtract base 0 count 16 is exactly AND 0xFFFF — so unaffected hardware sees no behavioural change, only a different but equivalent instruction. AI-assisted with Devin.

**@⁠whoalin1** on GitHub · 2026-11-25 20:51 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1585#issuecomment-7174277185) Useful scope data point from Sangonapou ([coordination issue](https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608)): the high-half corruption does **not** reproduce on Ampere (RTX 3080 Ti Laptop, 621.24) — 384/384 single-op cases match RDNA2. That is consistent with this PR's evidence, where the miscompile showed on Ada (RTX 4070 SUPER) with the surrounding fuzz-program instructions, and points at an Ada-generation compiler issue rather than something universal. Either way the transform here is bit-identical semantics on every device — `OpBitFieldUExtract base 0 count 16` is exactly `AND 0xFFFF` — so unaffected hardware sees no behavioural change, only a different but equivalent instruction. AI-assisted with Devin.
Author
Owner

@⁠whoalin1 on GitHub · 2026-11-26 05:19 UTC · original

Follow-up verification on the current head 69b363d75 under the local coordination lock: python -I tests/test_docs_design_rows.py passes 5/5 in 0.064 s. The earlier table-shape concern is resolved on the rebased branch.

**@⁠whoalin1** on GitHub · 2026-11-26 05:19 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1585#issuecomment-7183388334) Follow-up verification on the current head 69b363d75 under the local coordination lock: `python -I tests/test_docs_design_rows.py` passes 5/5 in 0.064 s. The earlier table-shape concern is resolved on the rebased branch.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1585/head:github-bot/gh-1585'
git switch 'github-bot/gh-1585'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1585'
git switch 'github-bot/gh-1585'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1585'
git switch 'github-bot/gh-1585'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1585'
git switch 'main'
git merge --squash 'github-bot/gh-1585'
git switch 'main'
git merge --ff-only 'github-bot/gh-1585'
git switch 'main'
git merge 'github-bot/gh-1585'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.