Skip to content

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

Open
whoalin1 wants to merge 4 commits into
ProjectLeonida:mainfrom
whoalin1:nc-i16-clamp-nv
Open

whoalin1 wants to merge 4 commits into
ProjectLeonida:mainfrom
whoalin1:nc-i16-clamp-nv

Conversation

@whoalin1

@whoalin1 whoalin1 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

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

@ecox982745

Copy link
Copy Markdown

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.

@QemmHD

QemmHD commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

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

@whoalin1

Copy link
Copy Markdown
Contributor Author

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 and others added 3 commits November 25, 2026 22:18
…NVIDIA And-&-0xFFFF miscompile)

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 <noreply@cognition.ai>
…nd d16 saturate

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 <noreply@cognition.ai>
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 <noreply@google.com>
@whoalin1

Copy link
Copy Markdown
Contributor Author

@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

Copy link
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown
Contributor Author

Follow-up verification on the current head 69b363d 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.

@MoonEyes

Copy link
Copy Markdown
Collaborator

Hi @whoalin1, thanks for this pull request.

CONTRIBUTING.md asks contributors to claim their work in the coordination issue ("Coordinating who works on what", currently #1608) before opening a pull request: post claiming: <ID> there, or a one-line description if the item is not listed yet, and put the ID in the pull request. I could not find a claim from you for this one.

The claim matters because "it is claimed in the coordination issue by the pull request's author" is the first item on the maintainers' checklist for a mergeable pull request, and because it lets others see that the work is taken before they write it a second time.

Could you post the claim in #1608 and put its ID (or a link to it) on the pull request's "Claimed in the coordination issue as:" line? If you already claimed it in another form, a link to your comment is enough.

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

@DadaCap

DadaCap commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

Thanks for this! Writing the VOP3 16-bit result mask as OpBitFieldUExtract is equivalent and targets a reported NVIDIA Ada miscompile.

During maintainer testing (local, 2026-11-28) (head 69b363d, merged on main b1619e0 / 6dfca64, Windows 11, Python tests via pytest/unittest) we found:

To get this merged:

AI-assisted (Claude Code), posted for DadaCap (maintainer).

@ecox982745

ecox982745 commented Nov 28, 2026 •

Copy link
Copy Markdown

The commit now on this PR incorporates the patch for the maintainer’s test-regex review. Its Git tree matches the originally tested patch exactly. I also merged it locally onto main ba8b77b390 (tree 10337471ac). All 23 tests in the two affected files and all five documentation-table tests passed there on Windows, CPU only.

It changes the two semantic tests. Both now require the low-half extract to start at bit 0 and take exactly 16 bits. The add test targets the value stored in v0; the multiply test keeps its link to the product.

I checked two cases that distinguish the old and revised tests:

CPU check Original PR head With patch
Change the multiply extract to 15 bits. The chosen operands still produce the same low-half result. Accepted Rejected
Add an unused, valid 16-bit extract after the signed add’s output store. Rejected (picked the unused extract) Accepted

Both example modules passed SPIR-V validation for Vulkan 1.2. All 23 tests in the two affected files passed on the patched head. This supplies the requested test change. It adds no hardware evidence about the reported driver miscompilation.

Coordination claim. AI-assisted with OpenAI Codex (GPT-6).

Integration check after #2593. I merged this exact head (599de2fc9) locally with main 91bab934d7ee43e23c582bd8a18baa2b09a0a558, which includes the f16 conversion CI fixes. All 33 CPU tests across test_alu_add_sub_i16, test_valu_int16, test_valu_cvt_f16_int16_mods and test_docs_design_rows passed on both main and the merged tree 702395051ec9e4494455835430222630701b964e. AI-assisted with OpenAI Codex (GPT-6.2 Sol).

@zmasi

zmasi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

I did not reproduce the preserved-high-half corruption on physical gfx906 (RADV Mesa 26.0.8, native wave64). Main aa918e5d9 and 69b363d75 merged with it each matched 9/9 readbacks: 5,952 data words correct, all dump pairs byte-identical, all guards intact. The reported clamped op_sel:[1,1,0] add returned 0x0002aaa8 on both. No shader-run validation errors or synchronization hazards.

The structural change is present: in the sampled modules, 16 integer result sites and six conversion result sites changed from AND-with-65535 to an extract at offset 0, width 16. These were result paths feeding stores, not source-operand extracts. Removing just the clamped add’s result mask in a test-only module produced 25 wrong lane words, so the readback really checks the preserved half.

The test-width concern is independent of the driver result: 257 × 257 gives the same low half at widths 15 and 16, whereas 384 × 256 gives 0x8000 at width 16 and zero at width 15. I would keep the exact-width assertion or a numerical control that sets bit 15.

AI-assisted.

@ecox982745

Copy link
Copy Markdown

For the maintainer’s hardware question, I could not reproduce the signed add/subtract corruption on a desktop RTX 4090, Windows, NVIDIA 616.64, Vulkan subgroup 32. This covers the add/subtract case that the earlier conversion sweep left open.

I compared main aa918e5d9 against the clean local merge of PR head 69b363d75 (tree 7150cb046). Each translator produced 32 synthetic compute shaders covering signed add and subtract, all eight source/destination half selections, and clamp on and off. Each shader processed the same 4,096 runtime input records, including saturation boundaries and varying previous destination values.

  • Both revisions matched the integer reference on all 131,072 full destination words. The 32 pairs of GPU readback buffers were byte-identical, all neighboring input words were unchanged, and all 64 modules passed spirv-val --target-env vulkan1.2.
  • For v_add_nc_i16 v14, v3, v12 op_sel:[1,1,0] clamp, the fixture with v14=0x00021111, v3=0xAAAABEEF, and v12=0xFFFEABCD returned 0x0002AAA8 on both revisions. The corresponding subtract returned 0x0002AAAC. The add modules differed at the result mask feeding the preserved-half merge.
  • As a sensitivity check, I removed just that result mask from the main module and dispatched it again. The comparison caught 2,038 corrupted high halves out of 4,096, with every low half still correct. The example add returned 0xFFFFAAA8 in this control.

This was an independently constructed shader around the reported instruction. The full failing shader and driver version from the RTX 4070 SUPER would help distinguish a driver difference from a surrounding-code difference.

AI-assisted verification with OpenAI Codex (GPT-6).

Check offset zero and width 16 on the product and the value stored by the signed sum. Ignore unrelated extracts in the fragment.

AI-assisted with OpenAI Codex (GPT-6).
@zmasi

zmasi commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

#1585 | head 599de2f (merged cleanly onto origin/main 791899f) | Windows 11 (10.0.26200.9974), Python 3.12.10 | AI-assisted: yes

Independent Verification (Windows 11)

  • Diff & safety audit: Inspected 100% of the diff on current head 599de2fc91 across tools/rdna2spirv.py (emitting OpBitFieldUExtract %uint <src> 0 16 instead of OpBitwiseAnd %uint <src> 0xFFFF across all 6 VOP3 16-bit integer, v_cvt_{norm_,}i16_f16, and saturate_int16 low-half sites), docs/design.md, tests/semantic/test_alu_add_sub_i16.py, and tests/semantic/test_valu_int16.py. python -I scripts/check_no_game_data.py docs/design.md tools/rdna2spirv.py tests/semantic/test_alu_add_sub_i16.py tests/semantic/test_valu_int16.py passed (rc=0).
  • Red -> Green on Windows 11:
    • Red (origin/main 791899f652 tools/rdna2spirv.py): Running #1585's tests/semantic/test_alu_add_sub_i16.py against unpatched tools/rdna2spirv.py failed (FAILED (failures=1) — count == 0 != 1 because origin/main still emitted OpBitwiseAnd %uint ... 65535).
    • Green (#1585 + origin/main):
      • python -I tests/semantic/test_alu_add_sub_i16.py -> 7/7 passed (OK, 1.590s, including spirv-as + spirv-val and negative-control mutation check)
      • python -I tests/semantic/test_valu_int16.py -> 16/16 passed (OK, 2.901s)
      • python -I tests/semantic/test_valu_add_sub_nc_i16.py -> 7/7 passed (OK, 3.657s)
      • Doc suites: test_docs_design_rows.py (5/5), test_docs_table_rows.py (2/2), test_docs_encoding.py (1/1) -> all OK.

@TheGeogeo

Copy link
Copy Markdown
Contributor

Ready for testing: merges cleanly with main 3535eb5c4. Not in main yet: git cherry finds 4 commit(s) not there, and 0 of the 26 lines it adds are already present. No duplicate found: it carries no claim ID, and no other open pull request has the same title.

AI-assisted (Claude Code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants