Skip to content

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

Open
Exopus-0x65f7ad0 wants to merge 2 commits into
ItsNotRexed:mainfrom
Exopus-0x65f7ad0:shader/cndmask-sign-bit-mods
Open

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

Conversation

@Exopus-0x65f7ad0

Copy link
Copy Markdown

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

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.

6 participants