rdna2spirv: emit OpBitFieldUExtract for the VOP3 16-bit result mask (NVIDIA And-&-0xFFFF miscompile) #1585
No reviewers
No assignees
Labels
No labels
bug
dependencies
documentation
enhancement
missing claim
needs rebase
needs-triage
question
ready-for-agent
submodules
No project
No milestone
Dependencies
No dependencies set
Notifications
1 participant
Due date
No due date set.
Reference
leonida/mandy!1585
Loading…
Reference in a new issue
No description provided.
Delete branch "nc-i16-clamp-nv"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Pull request successfully merged and closed
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, 0xFFFFmiscompiles whenXis 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] clampwrote0x1113AAA8for the correct0x0002AAA8.OpBitFieldUExtract %uint X, 0, 16is 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: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})v_cvt_i16_f16/v_cvt_norm_i16_f16conversions (aBitcastof an%intOpConvertFToS)saturate_int16feeding d16 image/buffer stores (aBitcastof an%intSClamp)The unsigned/unorm siblings, the
v_pk_*packed paths, and the f16put_f16_bitsidiom 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_f16andv_cvt_norm_u16_f16also gains oneOpBitFieldUExtracteach (like their signed siblings), andv_cvt_pk_i16_i32gains 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 ofv_cvt_{i16,u16}_f16/v_cvt_norm_{i16,u16}_f16plus 65,536v_cvt_pk_i16_i32input pairs found 0 mismatches in 327,680 outputs per revision, with all GPU buffers byte-identical between revisions.Verification
0x0002AAA8;v_sub_nc_i16 … clamp0x0002AAAC; 160 randomizedv_add/sub_nc_i16programs across allop_sel/clampcombos — 0 mismatches vs a Python reference.v_cvt_i16_f16/v_cvt_norm_i16_f16spot 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).--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).python -I scripts/run_semantic_tests.py tests/semantic— 487 files / 6,279 tests, 0 failures. Two tests that pinned theOpBitwiseAnd … 0xFFFFshape were updated to the extract form with their negative controls intact.compileallclean; the changed ops all passspirv-val.docs/design.mdcontract-table row forv_add_nc_i16/v_sub_nc_i16documents 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 … 0xFFFFemit sites.AI-assisted (Devin).
Claimed in the coordination issue as: RDNA2SPIRV-VOP3-U16-RESULT-MASK (claim).
Independent RTX 4090 check of the conversion changes in
7f85594b, compared with the preceding PR commit97535ad1(Windows, NVIDIA 616.64).One scope detail for the PR description: the shared result mask also changes the SPIR-V for
v_cvt_u16_f16andv_cvt_norm_u16_f16. Each gains oneOpBitFieldUExtract, just like its signed sibling.v_cvt_pk_i16_i32also 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:
v_cvt_{i16,u16}_f16andv_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.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.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: adocs/design.mdtable 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).
Thanks — fixed in
42ad738a6. The literal pipe in the preserved-half merge example is now escaped, andpython -I tests/test_docs_design_rows.pypasses locally (5 tests, OK). No code behavior changed.@QemmHD thanks for the catch. Rebased onto current main (
db1a22d29) as69b363d75; no conflicts. On the rebased tree the contract-table row is well-formed — the\|in the preserved-half merge is escaped exactly astests/test_docs_design_rowsexpects, 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.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 16is exactlyAND 0xFFFF— so unaffected hardware sees no behavioural change, only a different but equivalent instruction. AI-assisted with Devin.Follow-up verification on the current head 69b363d75 under the local coordination lock:
python -I tests/test_docs_design_rows.pypasses 5/5 in 0.064 s. The earlier table-shape concern is resolved on the rebased branch.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).
Thanks for this! Writing the VOP3 16-bit result mask as
OpBitFieldUExtractis equivalent and targets a reported NVIDIA Ada miscompile.During maintainer testing (local, 2026-11-28) (head 69b363d75, merged on main b1619e0a3 / 6dfca6428, Windows 11, Python tests via pytest/unittest) we found:
To get this merged:
AI-assisted (Claude Code), posted for DadaCap (maintainer).
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(tree10337471ac). 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:
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 main91bab934d7ee43e23c582bd8a18baa2b09a0a558, which includes the f16 conversion CI fixes. All 33 CPU tests acrosstest_alu_add_sub_i16,test_valu_int16,test_valu_cvt_f16_int16_modsandtest_docs_design_rowspassed on both main and the merged tree702395051ec9e4494455835430222630701b964e. AI-assisted with OpenAI Codex (GPT-6.2 Sol).I did not reproduce the preserved-high-half corruption on physical gfx906 (RADV Mesa 26.0.8, native wave64). Main
aa918e5d9and69b363d75merged with it each matched 9/9 readbacks: 5,952 data words correct, all dump pairs byte-identical, all guards intact. The reported clampedop_sel:[1,1,0]add returned0x0002aaa8on 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 × 257gives the same low half at widths 15 and 16, whereas384 × 256gives0x8000at width 16 and zero at width 15. I would keep the exact-width assertion or a numerical control that sets bit 15.AI-assisted.
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
aa918e5d9against the clean local merge of PR head69b363d75(tree7150cb046). 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.spirv-val --target-env vulkan1.2.v_add_nc_i16 v14, v3, v12 op_sel:[1,1,0] clamp, the fixture withv14=0x00021111,v3=0xAAAABEEF, andv12=0xFFFEABCDreturned0x0002AAA8on both revisions. The corresponding subtract returned0x0002AAAC. The add modules differed at the result mask feeding the preserved-half merge.0xFFFFAAA8in 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).
#1585 | head 599de2fc914eae22cbd12c9b7a7622dbf719c782 (merged cleanly onto origin/main 791899f652) | Windows 11 (10.0.26200.9974), Python 3.12.10 | AI-assisted: yes
Independent Verification (Windows 11)
599de2fc91acrosstools/rdna2spirv.py(emittingOpBitFieldUExtract %uint <src> 0 16instead ofOpBitwiseAnd %uint <src> 0xFFFFacross all 6 VOP3 16-bit integer,v_cvt_{norm_,}i16_f16, andsaturate_int16low-half sites),docs/design.md,tests/semantic/test_alu_add_sub_i16.py, andtests/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.pypassed (rc=0).origin/main791899f652tools/rdna2spirv.py): Running#1585'stests/semantic/test_alu_add_sub_i16.pyagainst unpatchedtools/rdna2spirv.pyfailed (FAILED (failures=1)—count == 0 != 1becauseorigin/mainstill emittedOpBitwiseAnd %uint ... 65535).#1585 + origin/main):python -I tests/semantic/test_alu_add_sub_i16.py->7/7passed (OK,1.590s, includingspirv-as+spirv-valand negative-control mutation check)python -I tests/semantic/test_valu_int16.py->16/16passed (OK,2.901s)python -I tests/semantic/test_valu_add_sub_nc_i16.py->7/7passed (OK,3.657s)test_docs_design_rows.py(5/5),test_docs_table_rows.py(2/2),test_docs_encoding.py(1/1) -> allOK.Ready for testing: merges cleanly with
main3535eb5c4. Not inmainyet:git cherryfinds 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).
RUN comment, PR #1585 (rdna2spirv: OpBitFieldUExtract for the VOP3 16-bit result mask)
Heads: PR 599de2fc9, main 91bab934d (local merge of the PR into it, not pushed).
Kind: translator (tools/rdna2spirv.py) + docs/design.md + two tests/semantic files. CPU only, no GPU run.
Commands (repo root):
python -I tests/semantic/test_alu_add_sub_i16.py
python -I tests/semantic/test_valu_int16.py
python -I scripts/run_semantic_tests.py tests/semantic -j 8
python -I tests/test_docs_.py and tests/test_design_contract_rows.py (14 files, one process each)
RED (behavioural, the PR adds no API): tools/rdna2spirv.py and docs/design.md put back to the merge-base version, the PR's tests kept.
test_alu_add_sub_i16.py: 1 failure (test_negative_controls_spirv_mutation), 6 pass.
test_valu_int16.py: 1 failure (test_sabotaged_spirv_is_detected, case "v_mul_lo_u16 drops 16-bit result mask"), 15 pass.
GREEN at the PR head: test_alu_add_sub_i16 7/7, test_valu_int16 16/16; full tests/semantic 491 files, 6312 tests, 0 failed; the 14 docs test files pass.
GREEN merged into main: both files pass (7/7, 16/16); the 14 docs test files pass; full tests/semantic 529 files, 6526 tests, 1 file failed: test_interp_computed_ij_fallback (2 failures, "0.07745361328125 != 0.0775146484375"). The same 2 failures happen on plain main (91bab934d) without the PR: pre-existing, not from this PR.
Not covered here: the NVIDIA And-&-0xFFFF miscompile is a GPU compiler behaviour. These tests check the emitted SPIR-V and the CPU interpreter; I did not re-run the GPU repro.
Machine: Windows 11 Pro, RTX 3090 24 GB, NVIDIA 621.40, Python 3.13.12 (uv build), run with
python -I, one process per test file.Verified by OreoFR on macOS 27.2.1 (26B131), Apple M5, APFS (case-insensitive), Python 3.12.16, spirv-tools + llvm-mc, main 7c330c76d (also merges cleanly on current main dd87aa405; does not force a rebase of #2566), PR head 599de2fc9 : red 2 failures / green 23 OK, 0 skipped
test_alu_add_sub_i166/7,test_valu_int1615/16. Structural red (regex on the emitted SPIR-V); interpreter behaviour is identical on both sides, as expected since the target is an NVIDIA codegen issue.scripts/run_semantic_tests.py, 529+ files, ~6 530 tests) and the CI translator-tests pytest selection: identical to main (main's known failures only:test_interp_computed_ij_fallbackx2,test_mesh_serial_wavesx6).OpBitwiseAnd 0xFFFFto the equivalentOpBitFieldUExtract 0,16; the existingdocs/design.mdrow is updated (no new rule, so no new fixture needed).Verdict: READY (subject to the maintainer accepting it without a hardware repro)
AI-assisted: Claude Code
"No game data" review by OreoFR (requested for maintainers), PR head 599de2fc9, merged locally on main 720bd3b4e:
scripts/check_no_game_data.pyon the merge: rc 0; theno-game-data.ymlextension filter: 0 files.tools/rdna2spirv.py, two tests and onedocs/design.mdrow: opcodes, synthetic constants and a one-instruction repro with made-up values only. No shader name, hash or corpus dump.Review: OK from a no-game-data point of view.
AI-assisted: Claude Code
I reproduced the reported docs-table concern against current
main(82fbcc0f7) by test-merging599de2fc9locally: the merge is clean andpython3 -I tests/test_docs_design_rows.pynow passes all 5 tests. The malformed-row failure appears to have been resolved by subsequent upstream doc-table cleanup rather than requiring a change in this PR.Rebased onto current
mainas96575316ato refresh the stale CI baseline; the branch reapplied cleanly with no conflicts. Validation after rebase:pytest tests/semantic/test_alu_add_sub_i16.py tests/semantic/test_valu_int16.pypasses 23 tests + 337 subtests under Python 3.12;py_compileandgit diff --checkare clean.Fixed the Linux Python failure in
3c5c86d1a: the existing typed-image gather test still expectedOpBitwiseAndfor the signed low-half mask even though the PR intentionally emits the NVIDIA-safeOpBitFieldUExtract. The test now asserts the extract offset/count. Validation:python3 tests/test_image_load_typed.pypasses (28 tests, 2 environment skips) andgit diff --checkis clean.The repaired head
3c5c86d1ais now mergeable with all visible checks green. The typed-image regression suite passes locally, and the NVIDIA-safeOpBitFieldUExtractbehavior is unchanged.Reviewed head diff against
main: replacingOpBitwiseAnd & 0xFFFFwithOpBitFieldUExtract(offset 0, count 16) across the 6 VOP3 16-bit integer/conversion/d16-saturate sites intools/rdna2spirv.pyis bit-equivalent, fixes the reported NVIDIA shader-compiler preserved-high-half miscompile, updates the structural negative-control tests anddocs/design.mdcontract table, is clean-room verified, and has all CI checks green.(AI-assisted review.)