Skip to content

rdna2spirv: v_cndmask_b32's abs/neg source modifiers are sign-bit moves - #4103

Merged
ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
Exopus-0x65f7ad0:shader/cndmask-sign-bit-mods
Nov 23, 2026
Merged

ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
Exopus-0x65f7ad0:shader/cndmask-sign-bit-mods

Conversation

@Exopus-0x65f7ad0

Copy link
Copy Markdown
Contributor

Claim: #3918 (comment) (new SHADER item)

Problem

v_cndmask_b32 (Gen in tools/rdna2spirv.py) read both sources with src_u, whose modify() applies a VOP3 abs/neg through SPIR-V OpExtInst FAbs / OpFNegate. v_cndmask only moves bits, so on hardware those modifiers are sign-bit moves; SPIR-V's float ops may quiet a signalling NaN and, under a flushing float mode, flush a denormal. The translator already handles exactly this for v_cmp_class_f32 (whose input modifiers are measured as sign-bit moves on gfx1036, all four MODE settings) and v_pack_b32_f16, but not for v_cndmask.

Through tests/semantic/spirv_mini_interp.py, on main:

v1 -v1 |v1| -|v1|
0x7F800001 (sNaN) 0xFFC00001 (want 0xFF800001) 0x7FC00001 (want 0x7F800001) 0xFFC00001 (want 0xFF800001)
0xFF800001 (sNaN) 0x7FC00001 (want 0x7F800001) 0x7FC00001 (want 0x7F800001) 0xFFC00001 (want 0xFF800001)

Change

  • New Gen.src_u_sign_mods(a): the source read without its abs/neg, then OpBitwiseAnd 0x7FFFFFFF for abs and OpBitwiseXor 0x80000000 for neg (abs first), the same sequence cmp_class emits.
  • v_cndmask_b32 reads src0 and src1 through it. Nothing else changes (SDWA selection still happens in src_u, before the modifiers).
  • docs/design.md: a contract row, "v_cndmask_b32 source modifiers", with how it's backed (below).

How the rule is confirmed

  • Not measured on hardware for v_cndmask itself (I have no RDNA2 GPU).
  • The same VOP3 input modifiers on v_cmp_class_f32 are hardware-measured as raw sign-bit moves on gfx1036 (672/672, all four IEEE/denorm modes; the "Floating-point class compare" row).
  • LLVM's AMDGPU backend moves fneg/fabs through a select into v_cndmask_b32_e64's source modifiers (AMDGPUTargetLowering::foldFreeOpFromSelect), which is only exact if they are sign-bit moves (LLVM's fneg/fabs never quiet).
    A GPU readback on an AMD card would be welcome.

Tests

tests/semantic/test_cndmask_sign_mods.py (new, hand-written assembly, no game files): 15 words (signalling and quiet NaNs of both signs, denormals, ±0, ±inf, normals) x v1, -v1, |v1|, -|v1| x the modified operand as src0 and as src1; the unselected modified operand doesn't leak; no FAbs/OpFNegate in the module; and spirv-as + spirv-val --target-env vulkan1.3 on each module when they're on PATH (they were, from Vulkan SDK 1.4.363.0).

test_cndmask_sign_mods.py
main's translator (LEONIDA_TOOLS_DIR) 21 failures (18 sign-bit cases on NaNs, 3 float-op emissions)
this branch 4/4 tests OK

Full python -m unittest discover -s tests/semantic (Windows 11, Python 3.12): 7,460 tests, the same 9 failures/errors as main aa2988cc (7,456 tests there): test_divergent_loop_wave64_emulate_and_native_cs (3), test_rejected_forms (2), test_each_mutation_is_caught, test_anchors_match_crlf_translator_copies, test_nested_divergent_loops_emulate, test_bug4b_loop_carried_split_sgpr_pair_ballot, all red on main and taken (TEST-857, #3995, ...). tests/test_design_contract_rows.py, test_docs_design_rows.py, test_docs_table_rows.py, test_docs_repo_paths.py: OK.

Limitations

  • No corpus count: I have no work folder, so how many game shaders use v_cndmask with abs/neg is unknown.
  • IEEE mode 1 quieting (SHADER-190) is not modelled, as for v_pack_b32_f16; for a non-arithmetic op like v_cndmask the v_cmp_class_f32 measurement suggests none.

AI-assisted (Claude Code). CPU and spirv-val only; not run on a GPU.

v_cndmask_b32 read its sources with src_u, whose modify() applies VOP3
abs/neg through SPIR-V FAbs/OpFNegate. Those may quiet a signalling NaN
(the mini interpreter does: -v1 of 0x7F800001 came out 0xFFC00001) or
flush a denormal, which a bit move never does. The sources now go
through src_u_sign_mods: abs clears bit 31 and neg flips it on the raw
bits, as the translator already does for v_cmp_class_f32, whose input
modifiers are measured as sign-bit moves on gfx1036.

tests/semantic/test_cndmask_sign_mods.py: 15 words (signalling/quiet
NaNs, denormals, zeros, infinities, normals) x v, -v, |v|, -|v| x both
operands through the interpreter, no FAbs/OpFNegate in the module, and
spirv-val when it is on PATH. docs/design.md: the contract row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran this at 1edc31b8 on Windows 11: python tests/semantic/test_cndmask_sign_mods.py -> 4 tests, OK; compileall and git diff --check clean.

The test distinguishes bit moves from float ops on signallings NaNs and denormals for both abs and neg on either source, and the ordinary select cases still match. Implementing the modifiers as sign-bit operations is the correct semantics for a bitwise select. No blocking issue.

@Atlonsky

Copy link
Copy Markdown

Windows x86-64 check of 1edc31b8 (Python 3.15.2, python -I, spirv-as/spirv-val from SPIRV-Tools v2026.4 on PATH):

  • python -I -m unittest discover -s tests\semantic -p test_cndmask_sign_mods.py → Ran 4 tests in 0.496s, OK — including test_modules_validate, which only runs when the SPIR-V tools are on PATH (without them it is skipped 'spirv-as/spirv-val not on PATH', so a green run without the tools does not cover validation).
  • Regression guard proven: with tools/rdna2spirv.py taken from main aa2988c and only the new test file kept, the same module reports 21 failures, and they land exactly on the words this PR is about — 0x7F800001 (signalling NaN, would come back quieted) and 0x7FBFFFFF, for both -v1 and -v1, src1 variants. So the test fails for the right reason, not only on the sign bit.

AI-assisted (Claude). No game files, no GPU.

@CaravelaLabs

Copy link
Copy Markdown
Contributor

Automated test: 1edc31b8 merged onto main aa2988cc on AMD Radeon 8060S (RADV GFX1151, RDNA 3.5), Mesa 25.2.8, Ubuntu 24.04, runtime rebuilt from this branch in a clean sandbox. Result: pass.

  • scripts/run_gpu_tests.py: 192/209 passed, 15 skipped (plain main: 192/209 passed, 15 skipped).
  • Python tests this PR touches: 4 passed, 132 subtests passed.

Same machine and setup as my earlier manual reports. AI-assisted (Claude Code).

@tupacfan04

Copy link
Copy Markdown
Contributor

Tested at the head on Windows 11 / Python 3.13.0: 4 tests / 120 subtests OK; reverting gives 21 failures, as claimed; test_cndmask 34, test_alu_cndmask* 62, test_vop3_const_mods 11, docs 20+4, regen_fixtures --check and compileall clean, merge clean.

The change is right: src_u_sign_mods (rdna2spirv.py:434) does & 0x7FFFFFFF then ^ 0x80000000, with no FAbs/OpFNegate, and a plain v_cndmask_b32 translates byte-identically to main (checked in both the e32 and e64 forms).

One gap: the condition operand is not covered. :9516 sends a[3] to mask_src (:4802), which silently ignores abs/neg (I confirmed -s4 and |s4| parse and are dropped), and docs/design.md:335 only claims src0/src1. Either reject that form or add a measured case and a doc sentence.

Approve. AI-assisted review, no game files, no GPU.

…enormal bits

Signalling NaNs and a denormal through -v, |v| and -|v| on src0 and src1.
On an RTX 5060 Ti (driver 621.14) the old FAbs path gave 0x7fffffff for
|0xff800001| (the driver's canonical NaN); with the sign-bit moves all
4096 pixels are exact.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Exopus-0x65f7ad0

Copy link
Copy Markdown
Contributor Author

Added a GPU test, runtime/test/shaders/ps_cndmask_sign_mods.asm (expected words in check.py and run_gpu_tests.py): signalling NaNs and a denormal through -v, |v| and -|v|, on src0 and on src1.

Run on an NVIDIA GeForce RTX 5060 Ti, driver 621.14, Windows 11, with llvm-mc 21.1.8 (scripts/build_llvm_mc.ps1), python -I scripts/run_gpu_tests.py --filter ps_cndmask_sign_mods:

translator result
main (FAbs/OpFNegate) FAIL, 4096/4096 pixels: |0xff800001| comes back 0x7fffffff, the driver's canonical NaN, not 0x7f800001
this branch (sign-bit moves) PASS, 4096/4096 pixels exact

So the quieting isn't just the mini interpreter's: NVIDIA's FAbs canonicalizes a NaN. The full GPU suite on main on this card is 164/210 passed, 0 failed (#166).

@CuteBooty

Copy link
Copy Markdown

Targeted Vulkan readback on an RTX 3070: aa2988cc matched the integer sign-bit expectation in 94/124 cases. PR head 1edc31b8 matched 124/124.

I translated the same synthetic wave32 v_cndmask_b32_e64 compute shader with each revision and ran both modules with the same vk_compute_test harness (a4fb7f3a). The shader selected 15 runtime input words in each source slot, applying identity, negate, absolute value, and negative absolute value. Four additional cases selected the other operand. I compared raw uint32 storage-buffer output with integer sign-bit operations. Both modules passed spirv-val --target-env vulkan1.3.

All 30 differences at base were modified NaNs: five input patterns × three modifiers × two source slots. Every one returned 0x7fffffff. At head all five preserved the expected payload and sign for every modifier. The tested subnormals, finite values, signed zeros, infinities, and unselected controls matched on both revisions. This establishes the behavior of the translated shader on Ampere. Native RDNA2 execution remains open.

Windows 11 build 26200, NVIDIA driver 617.14, subgroup size 32, CPython 3.14.10, LLVM 23.1.5, SPIRV-Tools v2026.4. Synthetic inputs only. AI-assisted with Codex.

@CuteBooty

Copy link
Copy Markdown

On an RTX 3070 with driver 617.14, the official ps_cndmask_sign_mods pixel test at 9668102f matched all four expected words in 4,096/4,096 pixels. With only the translator replaced by aa2988cc, 0/4,096 pixels matched. Both translations passed spirv-val.

The baseline difference was confined to the second channel of every pixel. Absolute value of the negative signaling NaN produced 0x7fc00001 (expected 0x7f800001). The other three channels matched throughout. This differs from the 0x7fffffff baseline reported for the same pixel fixture on the RTX 5060 Ti and from our earlier RTX 3070 compute fixture. These runs establish the observed bit patterns; the cause of the difference is still open.

Windows 11, CPython 3.14.10, LLVM 23.1.5, SPIRV-Tools v2026.4. Both runs used the same vk_harness built at a4fb7f3a and the same --filter ps_cndmask_sign_mods --require-pass fixture. Synthetic inputs, AI-assisted with Codex.

@tylerstraub

Copy link
Copy Markdown
Contributor

Tested 9668102f533d8053ca55c5825ac40de90377cc10 on NVIDIA GeForce RTX 5090, 595.99.02 (Linux).

Checked both choices of the conditional mask in ps_cndmask_sign_mods on Linux RTX 5090. The submitted fixture passed SPIR-V validation and matched all four expected words in 4,096/4,096 pixels.

For the second draw I reversed both masks, keeping the instructions unchanged, so each now selected the operand without a sign modifier. I gave the two source registers distinct payloads: v1 = 0x7fc12345 (positive quiet NaN) and v6 = 0xff812345 (negative signalling NaN). The validated shader returned ff812345 7fc12345 7fc12345 7fc12345 in 4,096/4,096 pixels. The selected values retained their sign and payload with each modifier on the unselected operand.

AI-assisted (Codex).

AI-assisted verification.

@bobcatchris

Copy link
Copy Markdown
Contributor

Independent Apple Silicon / MoltenVK check of head 9668102f (hardware reports Apple M2 / Mac14,3, not M1), macOS 26.4, MoltenVK 1.4.2, subgroup range 4..32, llvm-mc 23.1.6. Translator/Python-only change; one harness built from base 3eb4410b shared for comparison.

  • python -m unittest discover -s tests/semantic -p test_cndmask_sign_mods.py: 4/4 OK.
  • New GPU fixture ps_cndmask_sign_mods PASS: 4096/4096 pixels exact — NaN and denormal bits survive the abs/neg source modifiers on this Apple driver.
  • Full suite on the PR head: 145/211 passed, 57 skipped, 9 failed. Running the PR's base aa2988cc with the same harness gives 144/210, 57 skipped, 9 failed. The only difference is the new passing fixture; no common test changed status.

Caveat: the two extra failures beyond the usual seven on this box are cs_bvh and cs_bvh32, and they appear on the base too with this harness pairing (old run_gpu_tests.py/check.py from aa2988cc with the newer harness binaries). They are a harness/version mismatch artifact of my shared-build setup, not a regression from this PR. No code changes, no game data; synthetic fixture and counts only.

AI-assisted.

@kapitche

Copy link
Copy Markdown
Contributor

Tested 9668102f merged with main 3eb4410b on Windows 11, RTX 4080 SUPER + Intel UHD 770 (MSVC 14.51): pass.

  • scripts/run_gpu_tests.py --filter ps_cndmask_sign_mods: PASS on both GPUs, 4096/4096 pixels exact each.
  • tests/semantic/test_cndmask_sign_mods.py: 4 tests OK.
  • Negative control (tools/rdna2spirv.py from main, everything else from the PR): the fixture fails on the RTX 4080 SUPER (4096/4096 mismatches) and the unit test gives 21 failures in 4 tests. On the Intel UHD 770 the fixture still passes without the fix (4096/4096 exact), so on Intel this fixture does not tell the two translators apart.

The GPU test programs are the runtime build of main 7b7d73f8 (the same exes for every run); the PR's tools/, scripts/ and runtime/test/shaders come from the merge.

@Exopus-0x65f7ad0

Copy link
Copy Markdown
Contributor Author

Thanks @tylerstraub, @bobcatchris and @kapitche. The reversed-mask payload check and the negative control are good additions.

On the Intel result: that's expected, and it's why the PR has both checks. The old translation used OpFAbs/OpFNegate. SPIR-V doesn't pin down what those do to NaN payloads and denormals: a driver that implements them as sign-bit operations (Intel here) returns the same bits as the hardware's modifiers, and one that canonicalises or flushes (NVIDIA here: 4096/4096 mismatches) doesn't. So the GPU fixture tells the two translators apart only on drivers of the second kind. tests/semantic/test_cndmask_sign_mods.py checks the emitted OpBitwiseAnd/OpBitwiseXor themselves on any machine (21 failures on the old translator, as you measured), and that is the test that holds on every platform.

AI-assisted (Claude Code).

@sperm

sperm commented Nov 23, 2026

Copy link
Copy Markdown

Tested 9668102 (a re-test at the new head) on AMD Radeon RX 9070 XT (RADV GFX1201) and the Ryzen 7 9800X3D iGPU (RADV RAPHAEL_MENDOCINO, gfx1036), Mesa 26.2.6, Linux 7.3.4: pass, no regressions.

  • Full python -I scripts/run_gpu_tests.py (Linux build of this branch's runtime/, LEONIDA_VK_DEVICE per GPU): 197/211 passed, 14 skipped, 0 failed on both GPUs. The new ps_cndmask_sign_mods passes on both. Every other test has the same result as at the merge base aa2988c.
  • python -m unittest discover -s tests/semantic: 7460 run, 7 failures and 2 errors. That is exactly the set at the merge base aa2988c, compared by test name (test_cmp_class_f64, test_mutations, test_loop_ballot_barrier). The PR adds none.
  • Note: main has moved 921 commits past that merge base, so this doesn't cover the branch merged onto current main.

No game files. AI-assisted.

@lunarminus

Copy link
Copy Markdown

Tested 9668102f merged onto main d0835dd9 on an AMD Radeon 760M (RDNA3 iGPU, gfx1103, subgroup 32..64), RADV Mesa 26.3.1, CachyOS Linux: pass

  • scripts/run_gpu_tests.py with the d0835dd9 runtime build (the PR changes no C++): 197/211 passed, 14 skipped, 0 failed, both natively and with LEONIDA_W64_EMULATE=1. Without the PR, d0835dd9 gives 196/210 passed and 14 skipped in both modes. I compared every test line with that run. The only difference is the new ps_cndmask_sign_mods: PASS, 4096/4096 pixels exact. No existing test changed status. All 215 shaders translated and pass spirv-val.
  • --filter ps_cndmask_sign_mods with only tools/rdna2spirv.py taken from d0835dd9 (the rest of the merge unchanged): FAIL, 4096/4096 mismatches in both modes. It got 0xffc00001 0x7fc00001 0x80000001 0x7fc00001 where 0xff800001 0x7f800001 0x80000001 0x7f800001 is expected. On RADV the three signalling-NaN channels come back quieted with their payload kept, and the denormal channel matches. With the PR's translator: PASS, 4096/4096. This checks how RADV runs the translated OpFNegate/FAbs, not native v_cndmask_b32 execution.
  • python -I -m unittest discover -s tests/semantic -p test_cndmask_sign_mods.py: 4 tests OK, with spirv-as/spirv-val on PATH. -p 'test_*cndmask*': 100 tests OK, including the new 4. -p 'test_vop3_const_mods*': 11 OK. tests/test_design_contract_rows.py, test_docs_design_rows.py, test_docs_table_rows.py, test_docs_repo_paths.py: OK.
  • The new test against main's translator (LEONIDA_TOOLS_DIR set to d0835dd9's tools/) gives 3 failures, all in test_no_float_ops_are_emitted, where the PR reports 21 on Windows. On this CPython 3.14.10 (Linux), float32 sNaNs keep their bits through struct pack/unpack and negate/abs, so here the mini interpreter does not quiet them and the value subtests pass with either translator. On this machine only the "no float ops" check and the GPU fixture catch the old behaviour.

AI-assisted (Claude Code).

@Kev-Ball

Copy link
Copy Markdown

For the outstanding corpus count: 14,855 of 26,503 retail shader listings contain at least one v_cndmask_b32 with an abs or neg modifier on a selected data operand (src0 or src1), for 120,098 modified instruction sites on Xbox 1.0.2.1.

I found 22,553 listings with any v_cndmask_b32, containing 1,091,305 e64 instructions and 398,936 e32 instructions. I parsed src0/src1 with the PR's operand parser and independently checked the corresponding VOP3 ABS and NEG bits in every e64 instruction's encoded bytes. The two counts agreed site by site. These are static occurrences; the census does not establish how often a selected input carries a NaN or denormal at run time. CachyOS, CPU only. AI-assisted.

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.