Skip to content

BUILD-828: decode literal words in unary binary64 shader operations - #2863

Merged
ItsNotRexed merged 5 commits into
ItsNotRexed:mainfrom
evilclanker9000:codex/unary-f64-literals
Nov 23, 2026
Merged

ItsNotRexed merged 5 commits into
ItsNotRexed:mainfrom
evilclanker9000:codex/unary-f64-literals

Conversation

@evilclanker9000

@evilclanker9000 evilclanker9000 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as BUILD-828.

What and why

A pixel shader using 16.0 as a literal for a binary64 reciprocal, or 6.0 for a mantissa/exponent decomposition, read a tiny subnormal instead. The same values supplied through register pairs worked. The parser only marked literal dwords for addition and conversion, and the frexp paths separately decoded sources without inspecting this mark.

Recognize the literal form for the five unary operations on main and use one decoder for the low and high source words. Inline integers retain their sign-extended representation and inline floats keep their binary64 value. LLVM AMDGPU literal conversions

Tested

  • LLVM gfx1030 assembled two synthetic fragment shaders, one with literal dwords and one with the equivalent register pairs. Their canonical LLVM listings translated and passed Vulkan 1.2 spirv-val both before and after (2/2 each).
  • The expected RGBA values for the full fixture are (0.0625, 0.25, 0.5, 0.5625). The committed regression checks the emitted low/high source words for reciprocal, reciprocal square root and square root. It executes both frexp instructions through integer SPIR-V: literal 6.0 gives mantissa words (0, 0x3fe80000) and exponent 3. The full shader passes Vulkan 1.2 validation. The repository semantic interpreter stops on its Float64 type and does not execute the full RGBA export.
  • The committed regression failed four subtests on main. After the change, the five focused suites for source words, f64 math and parser behavior passed 59 tests and 110 subtests. No GPU or game data was used.

AI-assisted with Codex.

Checklist

  • No game data, keys or decryption, including in logs or reports quoted here.
  • The binary64 literal rule and the synthetic verification are in the contract table of docs/design.md.

GFX10 places the separate literal dword above 32 zero bits for a binary64 source. Use the same source interpretation for reciprocal, square root and exponent decomposition so literal and register-pair forms produce the same color.
@tupacfan04

Copy link
Copy Markdown
Contributor

Code read of 53ce204e (I didn't run it). Looks right. f64_source_words is one place for the source-word rules (register pair, high-dword literal, f64 inline float, sign-extended integer), and v_frexp_mant/exp_f64 now use it, so a literal reaches them as the high word instead of being treated as a 64-bit integer. Marking f64_literal for the five extra opcodes in shader_ir is what makes those operands take the literal path.

AI-assisted review (Claude Code).

@tylerstraub

Copy link
Copy Markdown
Contributor

Tested 53ce204ee656fdcb86ede72f00d077ae5e86784d on NVIDIA GeForce RTX 5090, 595.99.02 (Linux).

Independent synthetic Vulkan check of 53ce204ee656fdcb86ede72f00d077ae5e86784d on Linux 7.0.0-43 (RTX 5090, NVIDIA 595.99.02, LLVM 21.1.8, SPIRV-Tools 2026.1).

I drew the PR's unary binary64 literal fixture and a variant supplying the same operands through VGPR pairs. On the PR, both draws matched all 4,096 pixels exactly at (0.0625, 0.25, 0.5, 0.5625). With only the translator and parser diff reversed, the literal draw returned (+inf, +inf, 0, 0) at all 4,096 pixels. The register-pair control still matched exactly.

The fragment modules passed Vulkan 1.2 SPIR-V validation, and all 12 focused test_vop3_const_mods.py tests passed.

AI-assisted verification.

@Marukooh

Copy link
Copy Markdown
Contributor

Tested 53ce204e on CachyOS Linux, Python 3.14. tests/semantic/test_vop3_const_mods.py: 12 passed, 6 subtests passed.

The unary literal check fails on the base and passes on the head, confirming that the expected high dword reaches the translated binary64 operation.

AI-assisted, synthetic inputs only.

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

Verified on the PR head: tests/semantic/test_vop3_const_mods.py -> 12 tests, 6 subtests, OK; six related f64 suites -> 60 tests / 116 subtests, 0 fail. On current main the same suites give 25 tests / 105 subtests with 4 subtest failures, so the fix is exercised. Independent literal decode checks out (0x40300000 -> 16.0, 0x3fd00000 -> 0.25, 0x40180000 -> 6.0 in bits 63:32 with a zero low word), and the translated SPIR-V evaluates to 1/16, 0.25, 0.5 and frexp(6) -> 0.5625; spirv-val --target-env vulkan1.2 exit 0. Main puts the literal in the low dword and returns 0/0 for frexp, reproducing the reported failures. Approve.

Non-blocking: the body's 59 tests / 110 subtests were not reproduced (I get 60/116 across the six suites; 25/105 on main for the five f64 suites); rcp/rsq/sqrt are asserted structurally only, with only frexp executed.

@tupacfan04

Copy link
Copy Markdown
Contributor

Tested at the head on Windows 11 / Python 3.13.0: unittest discover -s tests/semantic 4138 tests OK (1216 s), the focused new test 4 subtests OK, and test_f64_{conversion,output_modifiers,transcendentals} 5 OK; merge clean. Negative control: restoring tools/ from main fails the new test in 4 places.

The rule matches docs/design.md:288 (literal dword -> bits 63:32 with the low zero; inline integers sign-extended; inline floats as binary64), and it is shared with v_add_f64/v_cvt_f32_f64. Executing the frexp literal case is convincing (0x40180000 -> mantissa 0x3fe80000, exp 3; 2.0 -> 0x3fe00000, 2; 1 -> 0,0,0).

Nits: tools/shader_ir.py:201's not -16 <= v <= 64 also labels an out-of-range decimal inline integer as a literal dword (LLVM prints those in hex, so the output matches, but the heuristic is broader than needed); and tests/semantic/test_vop3_const_mods.py:192-205 only string-asserts rcp/rsq/sqrt, so the doc's (0.0625, 0.25, 0.5, 0.5625) claim cannot be exercised because spirv_mini_interp.py:448 rejects Float64 - either execute those or soften the claim.

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

The semantic interpreter executes the integer frexp paths but rejects the full fixture at OpTypeFloat 64. Describe the source-word assertions and the executed results separately so readers can reproduce each check.
@evilclanker9000

Copy link
Copy Markdown
Contributor Author

I reproduced the interpreter limit on Windows. The full fixture assembled for gfx1013 and passed Vulkan 1.2 spirv-val, then spirv_mini_interp stopped at OpTypeFloat 64 with non-32-bit float type. My earlier contract wording overstated what the committed test executes.

The contract and PR description now distinguish the reciprocal, reciprocal-square-root and square-root source-word assertions from the executed frexp cases. For literal 6.0 the interpreter returned mantissa words (0, 0x3fe80000) and exponent 3. The RGBA tuple in the PR description is labeled as the expected result. Documentation correction

I also checked the spelling edge on gfx1030: LLVM prints decimal input 65 as 0x41 with a trailing literal dword, -17 as 0xffffffef with a trailing dword, and 64 as the four-byte inline form. The current parser gives these canonical listings the corresponding literal/inline classifications.

The four focused Python modules (test_vop3_const_mods, test_f64_conversion, test_f64_output_modifiers, test_f64_transcendentals) passed 17 tests and 25 subtests. No local GPU was used.

@JRamos1993

Copy link
Copy Markdown
Contributor

Tested 53ce204 merged onto main 1628210 on an RTX 5090 (driver 616.92), Windows 11, Vulkan SDK 1.4.363. The GPU test programs were built from the same tree:

  • scripts/run_gpu_tests.py: 143/189 passed, 46 skipped, 0 failed. main 1628210 gives 143/189, 46 skipped, 0 failed. All 194 test shaders translate and pass spirv-val.
  • tests/semantic/test_vop3_const_mods.py: ran 12 tests in 0.250s; OK.

AI-assisted (Claude Code).

Keep the targeted regression and the intervening upstream changes together for review.
Keep the targeted regression and the intervening upstream changes together for review.
@amarovndres

Copy link
Copy Markdown

Tested edb22940 on AMD Radeon RX 9060 XT (RDNA4, discrete, subgroup 32..64), AMD 26.10.1 (32.0.32015.7029, LLPC, Vulkan 1.4.359), Windows 11 Pro (build 26200): pass.

  • scripts/run_gpu_tests.py (full suite; test programs built from this head with llvm-mingw, llvm-mc 23.1.6): 196/210 passed, 14 skipped, 0 failed. main d0835dd9 on the same machine: 196/210 passed, 14 skipped.
  • tests/semantic/test_vop3_const_mods.py: 12 tests, OK
  • This PR adds no GPU fixture, so the GPU run is a regression check of the translator change on RDNA4 (native wave64), not a test of the new rule itself; the new rule is covered by the semantic test above.

AI-assisted (Claude Code). Synthetic shaders only, no game files.

@bobcatchris

Copy link
Copy Markdown
Contributor

Apple M2 / MoltenVK no-regression check of head 1f8888bd (hardware reports Apple M2 / Mac14,3, not M1), macOS 26.4, MoltenVK 1.4.2, subgroup 4..32, llvm-mc 23.1.6. Python/translator-only; harness built from this PR's base 2d3369a4.

python -I scripts/run_gpu_tests.py --build <harness>: 213 shaders translated, 213 pass spirv-val; 152/217 passed, 58 skipped, 7 failed — the known MoltenVK expected set. The f64 literal decode path is checked by the semantic suite rather than a GPU fixture here, so this is a no-regression data point for the GPU suite only. llvm-mc 23.1.6 assembled every fixture (no TEST-961-style rejection).

No code changes, no game data; counts only.

AI-assisted.

@jmorrison102-dev

Copy link
Copy Markdown

Tested edb22940 merged onto main 7b7d73f8 on an RTX 4090 (Windows 11): pass, no regressions from this PR (2 ctest timeouts also reproduce on main on this machine; see below), native and with LEONIDA_W64_EMULATE=1. A literal source of v_frexp_mant_f64 / v_frexp_exp_i32_f64 now reaches the op as bits 63:32: 17 of 17 cases match math.frexp, against 3 of 17 on main.

Conditions: NVIDIA GeForce RTX 4090, driver 621.24, Vulkan 1.4.357 (subgroup size 32), Windows 11 Home 26H2 build 26300.10075; llvm-mc 21.1.8 (gfx1013), SPIRV-Tools v2026.4 (Vulkan SDK 1.4.363.0), Python 3.14.7.

Commits: this PR edb22940, main 7b7d73f8. "merged" is 7b7d73f8 with this PR merged locally (git merge --no-commit, not pushed). 1f8888bd, pushed since, only merges main: it resolves the docs/design.md conflict the same way I did locally (both edits kept), and the tree I tested matches it except for main's unrelated mods commit db51b7d3.

Pass / fail / skip (build.py: translated / spirv-val valid, of all shaders):

main native main W64 merged native merged W64
ctest 162 / 0 / 10 162 / 0 / 10 161 / 1* / 10 160 / 2* / 10
run_gpu_tests.py 169 / 0 / 47 200 / 0 / 16 169 / 0 / 47 200 / 0 / 16
build.py 242 / 242 of 242 242 / 242 of 242 242 / 242 of 242 242 / 242 of 242
check.py 153 / 0 / 35 184 / 0 / 4 153 / 0 / 35 184 / 0 / 4
test_vop3_const_mods.py 11 / 0 / 0 not run 12 / 0 / 0 not run

* timeouts from a machine-wide start-up slowdown; main 7b7d73f times out on the same tests when rerun under the same conditions.

The merged ctest failures are d3d12_queue_wait_timeout (native and W64) and d3d12_queue_wait (W64) hitting their 10 s / 20 s ctest timeouts while every device test on this machine had gained 10-24 s of start-up time. Rerun back-to-back, main 7b7d73f8 times out on both in both modes too, so they come from the machine, not this PR; otherwise merged and main have no per-test differences. This PR's test_vop3_const_mods.py fails against main's tools/ (4 failures, all in test_unary_f64_literals_reach_the_operation_as_high_words), as expected.

frexp literal: a throwaway check (llvm-mc gfx1013, the translator, spirv-val, spirv_mini_interp, compared with math.frexp of the binary64 the hardware reads) of 14 literal sources (3 values x both ops x e32/e64, plus 3.0 spelled as a float) and 3 controls (inline 4.0 for both ops, v_cvt_f32_f64 v0, 0x40080000): 17 of 17 on merged; 3 of 17 on main, where the literal is read as bits 31:0 (v_frexp_exp_i32_f64 v0, 0x40080000 gives -1043 instead of 2). It used #3230's interpreter, which implements OpFConvert for the v_cvt control; main's gives the same 16 frexp results.

Overlap with #3230: both PRs add a binary64 source-word helper and call it at the same two sites (f64_source_words here, src_f64_words there, which also maps null to 0). Merged together they conflict at those two call lines, while both helper definitions merge without a conflict, so the two PRs need merging in a deliberate order: whichever lands second should keep one helper and carry over the null case. #3230's test_f64_null_src.py still errors on the merged tree (45 x Unsupported: <op> operand, as on main), so this PR doesn't cover #3230.

Scope: full tests/semantic not run; only test_vop3_const_mods.py (the file this PR changes) and #3230's test_f64_null_src.py, native.

Tested with AI assistance (Claude Code).

@cxchhh

cxchhh commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 1f8888bd on Tesla V100-SXM2-32GB (Volta, subgroup 32..32), NVIDIA 580.95.05, Ubuntu 24.04 (kernel 6.14.11), Python 3.12: pass, and the fix also checks out on a real GPU.

Setup: the PR diff applied to main 98fa503f (applies cleanly), Linux Vulkan test programs built from that tree (cmake -S runtime -G Ninja), compared against an untouched main 98fa503f built the same way.

  • scripts/run_gpu_tests.py: native 161/217 passed, 56 skipped; LEONIDA_W64_EMULATE=1 LEONIDA_BARY_FALLBACK=1 194/217 passed, 23 skipped. 0 failures, and the same per-test result as main in both modes (the skips are the 64-wide, mesh-shader (Volta has no VK_EXT_mesh_shader), barycentric-only and run by d3d12_* tests).
  • python -m unittest discover -s tests/semantic (llvm-mc 23.1.6, SPIRV-Tools 2026.4 on PATH): 6246 tests, failures=3, errors=2: the same five test_loop_ballot_barrier cases that fail on main here, nothing new.
  • tests/semantic/test_vop3_const_mods.py: 12 OK (main: 11). The PR's test file on main's tools/: 1 error.
  • GPU run of tests/semantic/fixtures/unary_f64_literals.asm (Volta has full-rate FP64). I copied it into runtime/test/shaders/ as a pixel shader, built it with build.py together with vs_ids, and drew it with vk_harness (64×64, RGBA32F):
    • this PR: all 4096 pixels (0.0625, 0.25, 0.5, 0.5625), the expected values from the PR description;
    • main's translator: all 4096 pixels (inf, inf, 0.0, -130.566), the literal-dword bug.

No game files. AI-assisted (Claude Code); I ran the commands above and checked the numbers against the logs.

@TreXito

TreXito commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 1f8888bd on CPU (Linux, Python 3.14.4, spirv_mini_interp; no GPU, no game files). Pass for the 32-bit literals, plus one new difference from main for a 64-bit literal written by hand.

32-bit literal, bits 63:32 (pass)

  • v_frexp_exp_i32_f64 v0, 0x3ff80000 gives 1. Main gives -1044.
  • v_frexp_mant_f64 v[0:1], 0x3ff80000 gives 0x3FE8000000000000 (0.75).
  • v_rcp_f64, v_rsq_f64 and v_sqrt_f64 with 0x3ff80000 build the constant 0x3FF8000000000000.
  • 0x1 versus inline 1 on frexp_exp gives -1041 versus -1073.

The whole binary64 word written by hand now reads as +0.0

Line main 3faac557 1f8888bd
v_sqrt_f64 v[0:1], 0x3ff8000000000000 constant 0x3FF8000000000000 0x0
v_rcp_f64 v[0:1], 0x4000000000000000 0x4000000000000000 0x0
v_frexp_exp_i32_f64 v0, 0x4000000000000000 2 0

Why

  • The "0x" in spelling test also marks a value wider than a dword.
  • f64_source_words then puts v & 0xFFFFFFFF, the written low word, in bits 63:32.
  • llvm-mc encodes such an operand as its high word, so a hand-written GPU test asm would disagree with the GPU.
  • LLVM's disassembly never prints a 64-bit literal, so translated game shaders are not affected. No fixture in the repo uses one today.

Fix: mark only values that fit a dword, by adding and -0x80000000 <= src["v"] <= 0xFFFFFFFF to the shader_ir condition. This also fixes v_add_f64 and v_cvt_f32_f64, which already read 0x4000000000000000 as +0.0 on main.

Overlap: #3642 edits the same shader_ir tuple for rcp/rsq/sqrt, so the second to merge conflicts on that line.

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.