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

Open
opened 2026-11-26 20:21:49 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1585
Author: @whoalin1
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: nc-i16-clamp-nv


📝 Commits (3)

  • 526b434 rdna2spirv: emit OpBitFieldUExtract for the VOP3 16-bit result mask (NVIDIA And-&-0xFFFF miscompile)
  • b816a5e rdna2spirv: extend the BitFieldUExtract workaround to cvt_i16/snorm and d16 saturate
  • 69b363d docs: escape the preserved-half merge pipe in the design table

📊 Changes

4 files changed (+25 additions, -9 deletions)

View changed files

📝 docs/design.md (+1 -1)
📝 tests/semantic/test_alu_add_sub_i16.py (+4 -1)
📝 tests/semantic/test_valu_int16.py (+3 -1)
📝 tools/rdna2spirv.py (+17 -6)

📄 Description

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).


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1585 **Author:** [@whoalin1](https://github.com/whoalin1) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `nc-i16-clamp-nv` --- ### 📝 Commits (3) - [`526b434`](https://github.com/ubiquitarianism/leonida/commit/526b434a7ba24cca055934aa45b5cf925e52209f) rdna2spirv: emit OpBitFieldUExtract for the VOP3 16-bit result mask (NVIDIA And-&-0xFFFF miscompile) - [`b816a5e`](https://github.com/ubiquitarianism/leonida/commit/b816a5e121cfad0aa503852bfcdbcff1f9ff03a3) rdna2spirv: extend the BitFieldUExtract workaround to cvt_i16/snorm and d16 saturate - [`69b363d`](https://github.com/ubiquitarianism/leonida/commit/69b363d75963c2dfeae4b0f676b9a2eb05a0b643) docs: escape the preserved-half merge pipe in the design table ### 📊 Changes **4 files changed** (+25 additions, -9 deletions) <details> <summary>View changed files</summary> 📝 `docs/design.md` (+1 -1) 📝 `tests/semantic/test_alu_add_sub_i16.py` (+4 -1) 📝 `tests/semantic/test_valu_int16.py` (+3 -1) 📝 `tools/rdna2spirv.py` (+17 -6) </details> ### 📄 Description 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). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1604
No description provided.