Skip to content

rdna2spirv: a literal source of v_div_fixup_f64 / v_div_fmas_f64 is bits 63:32 - #3791

Open
ramosbezzu wants to merge 1 commit into
ItsNotRexed:mainfrom
ramosbezzu:shader-f64-literal-div
Open

ramosbezzu wants to merge 1 commit into
ItsNotRexed:mainfrom
ramosbezzu:shader-f64-literal-div

Conversation

@ramosbezzu

Copy link
Copy Markdown

Claimed in the coordination issue as: a new SHADER item, finding and claim (no ID assigned yet). Found while reviewing #3642.

AI-assisted: yes (Claude Code).

Depends on: nothing. Overlaps #2863 (@Vonsek, BUILD-828) and #3642 (@kapitche) on one line of tools/shader_ir.py (the list of ops whose literal is marked f64_literal); the lists just need to be merged, and I will rebase on whichever lands first.

What and why

A 32-bit literal on a 64-bit float operand supplies bits 63:32, with bits 31:0 zero (docs/design.md, "f64 literal sources"). tools/shader_ir.py marked that only for v_add_f64 and v_cvt_f32_f64, and the source decoders of v_div_fixup_f64 and v_div_fmas_f64 took an immediate as the low dword. So v_div_fmas_f64 v[0:1], v[2:3], 0x40100000, v[4:5] (0x40100000 is the high word of 4.0) emitted OpCompositeConstruct %v2uint_d %c40100000 %c0, a 5e-315 denormal, and v_div_fixup_f64 used the literal as its low word the same way.

  • tools/shader_ir.py: parse_line marks the literal for the two ops.
  • tools/rdna2spirv.py: d64_words (v_div_fixup_f64) and f64 (v_div_fmas_f64) build (lo 0, hi literal) for a marked source. Inline integers stay sign-extended and inline floats keep their binary64 value, as for the other ops in the row.
  • docs/design.md: the "f64 literal sources" row names the two ops and the tests.

#2863 and #3642 fix the same thing for v_rcp/rsq/sqrt_f64 (and #2863 for the two v_frexp_*_f64); neither touches the division helpers.

Tested

CPU only, Linux (WSL2), Python 3.12.3, no game files, no GPU.

python tests/semantic/test_alu_div_fixup.py     # 7 tests OK
python tests/semantic/test_alu_div_fmas.py      # 8 tests OK

New tests, through rdna2spirv:

  • test_alu_div_fixup.py, test_literal_source_is_the_high_dword: the literal in each of the three source positions gives the same word as that value in a register pair (translator + spirv_mini_interp), and -2.0 as a literal denominator flips the quotient's sign. test_inline_integer_source_is_not_a_literal: inline 1 against the printed literal 0x1.

  • test_alu_div_fmas.py, test_f64_literal_source_is_the_high_dword: for a literal in each source position the module builds (lo 0, hi literal) and never (lo literal, hi 0); 0x1 against inline 1. This one checks the emitted operand words rather than a result, because spirv_mini_interp does not evaluate binary64.

  • Red before: with main f847748c's tools/ both files fail (4 failures each). Green after.

  • All 332 tests/semantic/test_*.py files pass with the change; tests/test_shader_ir.py, tests/test_f64_conversion.py and tests/test_docs_design_rows.py pass.

Not done:

  • spirv-val not run locally (no SPIRV-Tools on this machine); the tests call it when present, so CI covers it. No corpus counts: I have no work folder.
  • No hardware word. The rule is the documented encoding rule for a literal on a binary64 operand (the row's LLVM reference), not a new measurement for these two ops; a gfx1036 row through tools/hw_oracle would confirm it.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

🤖 Generated with Claude Code

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

At a2747ce all 15 focused tests passed on Windows. FIXUP CPU results matched equivalent register-pair operands for literals in all three positions. FMAS emitted the literal as bits 63:32 in all positions, with distinct inline integer behavior; SPIR-V passed Vulkan 1.2 validation. Binary64 FMA was not executed. #2863 and #3642 overlap both the opcode list and the design contract row. AI-assisted.

@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 a2747ce3 on Windows 11:

  • python -I tests/semantic/test_alu_div_fixup.py -> 7 tests, OK.
  • python -I tests/semantic/test_alu_div_fmas.py -> 8 tests, OK.
  • compileall and git diff --check clean.

The f64_literal marking now covers v_div_fixup_f64 / v_div_fmas_f64, matching the documented rule that a 32-bit literal on a 64-bit float source supplies bits 63:32 with the low half zero. The two new cases drive the translator, so they would fail on the parent; the shared shader_ir line is the only overlap with #2863/#3642, as the description says. No blocking issue.

@tupacfan04

Copy link
Copy Markdown
Contributor

Tested at the head on Windows 11 / Python 3.13.0: the 15 div tests (7 + 8) pass, plus test_shader_ir 35, test_f64_conversion 1, test_vop3_const_mods 11 and the two docs-row tests; regen_fixtures --check clean, spirv-val clean, merge clean. Negative control: main's tools/ gives 4 failures per file (8 total), all in the new tests.

Both decoders emit lo = self.cu(0), hi = literal (rdna2spirv.py:7087,7423), and the tests assert OpCompositeConstruct %v2uint zero lit - never the reverse - with inline 1 staying low. That matches the binary64 literal row in docs/design.md.

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

@TheMrBeez

TheMrBeez commented Nov 22, 2026 •

Copy link
Copy Markdown

Hardware word for this PR, plus the translated result on two GPUs, at a2747ce3.

Native gfx1036 (Ryzen 9 9950X3D iGPU, AMD driver 32.0.21030.2001, raw tools/hw_oracle kernels; wave32 and wave64, IEEE mode on and off, all the same). Each op runs three ways:

  • with a 32-bit literal (llvm-mc encodes 0xff plus the literal dword; I used values that aren't inline constants, since 0x40100000 is re-encoded as inline 4.0)
  • with a register pair (hi = literal, lo = 0)
  • with a pair (hi = 0, lo = literal)

VCC = 0, so v_div_fmas_f64 doesn't scale.

op, literal position literal pair (hi = lit) pair (lo = lit)
v_div_fixup_f64 S0 = 0x40140000, S1 = 2.0, S2 = 10.0 5.0 5.0 5.3e-315
v_div_fixup_f64 S1 = 0xc0040000, S0 = 4.0, S2 = 10.0 -4.0 -4.0 4.0
v_div_fixup_f64 S2 = 0xc0240000, S0 = 4.0, S1 = 2.5 -4.0 -4.0 4.0
v_div_fmas_f64 S0 = 0x40140000, S1 = 2.0, S2 = 1.0 11.0 11.0 1.0
v_div_fmas_f64 S1 = 0x40080000, S0 = 2.0, S2 = 1.0 7.0 7.0 1.0
v_div_fmas_f64 S2 = 0x40140000, S0 = 2.0, S1 = 3.0 11.0 11.0 6.0

So on hardware the literal is bits 63:32 in all three source positions of both ops, as the PR says.

Translated (compute .asm, vk_compute_test, spirv-val clean), word for word against the native words:

  • This PR: all 12 literal-result words match the hardware on an RTX 5080 (621.01) and on the Radeon gfx1036.
  • main (4c52ce27): the literal behaves as the low dword in all six rows. For example, v_div_fmas_f64 gives 1.0 instead of 11.0, and v_div_fixup_f64 with the literal denominator gives +inf instead of -4.0.

test_alu_div_fixup.py and test_alu_div_fmas.py pass here too.

Side note, not about this PR: one control word differs from hardware at both heads. It's the (lo = lit) pair in row 2, a denormal denominator, where the translation gives +inf and the hardware gives 4.0. That is the existing v_div_fixup_f64 overflow rule (exp(S2) - exp(S1) >= 1024 → ±inf). On a 63-case exponent grid, the hardware returns S0 in all 17 cases where that rule gives ±inf, while the translation follows the rule on both GPUs. That is already measured and fixed in open #2477 ("no overflow cutoff", @timelery's gfx1036 words), so nothing for this PR.

Windows 11, llvm-mc AMD LLVM 23.0.0git, Vulkan SDK 1.4.363. Synthetic programs only, no game files. The scripts are local. AI-assisted (Claude Code).

@evilclanker9000

Copy link
Copy Markdown
Contributor

Measured on gfx1036 (RDNA2, driver 26.10.1, game MODE) through raw hw_oracle kernels, one lane, vcc_lo = 0: a 32-bit literal on a 64-bit float operand is bits 63:32 (low word zero) in v_div_fmas_f64 and v_div_fixup_f64, as this PR says. v_div_fmas_f64 v[8:9], v[2:3], 0x40100000, v[4:5] with S0 = 2.0, S2 = 1.0 gives 4022000000000000 = 9.0 (2.0 * 4.0 + 1.0, so the literal is the double 4.0); the literal in the S0 position (0x40100000, v[2:3], v[4:5]) gives 9.0 too, and as S2 (v[2:3], v[4:5], 0x40100000, 2.0 * 1.0 + literal) gives 4018000000000000 = 6.0. v_div_fixup_f64 v[8:9], 0x40100000, v[2:3], v[4:5] (S1 = 2.0, S2 = 8.0) returns the quotient operand, 4010000000000000 = 4.0; with the literal in S1 or S2 the result is S0 (2.0); a literal 0x7ff00000 as S0 gives 7ff0000000000000 (+inf). A low-word reading would make the literal a 5e-315 denormal and these results ~1.0 / ~0, so they discriminate. This PR's translation was not part of this run; the script is local, not pushed. AI-assisted.

…its 63:32

A 32-bit literal on a binary64 operand supplies bits 63:32, with bits 31:0 zero (docs/design.md,
"f64 literal sources"). shader_ir only marked it for v_add_f64 and v_cvt_f32_f64, and the two division
helpers' own source decoders took an immediate as its low dword: `v_div_fmas_f64 v[0:1], v[2:3],
0x40100000, v[4:5]` built a 5e-315 denormal instead of 4.0.

parse_line now marks the literal for both ops and their decoders honour it. Inline integers stay
sign-extended and inline floats keep their binary64 value.

tests/semantic/test_alu_div_fixup.py: the literal in each of the three source positions against the
same value in a register pair (through the translator and the interpreter), and printed 0x1 against
inline 1. tests/semantic/test_alu_div_fmas.py: the operand words built for a literal in each position,
and 0x1 against 1.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@ramosbezzu
ramosbezzu force-pushed the shader-f64-literal-div branch from a2747ce to d017a5b Compare November 22, 2026 22:27
@ramosbezzu

Copy link
Copy Markdown
Author

Rebased on main aa2988cc (now d017a5b8). The only conflict was the op list in tools/shader_ir.py, where main added the four binary64 rounding ops; the list now holds both sets. tests/semantic/test_alu_div_fixup.py (7), test_alu_div_fmas.py (8), tests/test_shader_ir.py and tests/test_docs_design_rows.py: OK.

Not from this PR: on plain main aa2988cc the same four tests/semantic files fail on this machine (test_cmp_class_f64, test_loop_ballot_barrier, test_mutations, and test_vcmp16_opsel, the last because it calls spirv-as without checking that it is installed); the other 361 files pass on main and on this branch alike. CPU only, Linux (WSL2), Python 3.12.3. AI-assisted.

@JRamos1993

Copy link
Copy Markdown
Contributor

Tested d017a5b merged onto main 03b09a0 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: 164/210 passed, 46 skipped, 0 failed. main 03b09a0 gives 164/210, 46 skipped, 0 failed. All 214 test shaders translate and pass spirv-val.
  • tests/semantic/test_alu_div_fixup.py: ran 7 tests in 0.805s; OK.
  • tests/semantic/test_alu_div_fmas.py: ran 8 tests in 0.846s; OK.

AI-assisted (Claude Code).

@TreXito

TreXito commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

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

Pass

v_div_fixup_f64 with S0 = 2.0, S1 = 1.0 and S2 = 1.0, and a literal in one source (bit-exact in the interpreter):

Literal Result
S0 0x3ff80000 0x3FF8000000000000 (1.5)
S1 0xbff00000 0xC000000000000000 (-2.0)
S2 0x7ff00000 0x7FF0000000000000 (+inf)
S2 inline 1 2.0, as on main

v_div_fmas_f64 with 0x3ff80000 in S0 or in S2 builds the constant 0x3FF8000000000000.

New difference: v_div_fixup_f64 v[0:1], v[2:3], v[4:5], 0x7ff0000000000000 (the whole binary64 word, written by hand) now gives 0x0, where main gives +inf. The "0x" in spelling test also marks a value wider than a dword, and the reader then puts its low word in bits 63:32.

LLVM's disassembly never prints a 64-bit literal, so translated game shaders are not affected. A hand-written GPU test asm would disagree with the GPU, since llvm-mc encodes the operand as its high word.

Fix: add and -0x80000000 <= src["v"] <= 0xFFFFFFFF to the shader_ir condition. It also fixes v_add_f64/v_cvt_f32_f64 on main.

Merging into main 3faac557 conflicts only in docs/design.md, on the "f16 transcendentals" and "f64 literal sources" rows (main edited the f16 row next to yours). tools/rdna2spirv.py and tools/shader_ir.py merge cleanly.

AI-assisted (Claude Code).

@sh1shu1

sh1shu1 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

FYI #5534 (SHADER-865) does the same for the five other f64 ops (rcp, rsq, sqrt, frexp_mant, frexp_exp) and touches the same shader_ir.py tuple line and the same design.md row; whichever lands second takes the one-line union. AI-assisted (Claude Code, Claude Fable 5.1).

@milas6669

Copy link
Copy Markdown

Independent check — pass on d017a5b8, from a test written before I saw this PR.

I claimed the SHADER-865 division half at 11:20 today, wrote tests/semantic/test_f64_div_literal_sources.py from the item description, and only the pre-PR overlap check showed me this PR already fixes it. No PR from me; the branch is deleted. The test is a useful cross-check, so here is what it does and what it says about this head:

  • v_div_fixup_f64, executed end to end by the semantic interpreter:
    • v[0:1], 0x3ff80000, 1.0, 1.0 → words (0x00000000, 0x3FF80000) (1.5, not the subnormal 0x000000003FF80000),
    • v[0:1], 1.5, 1.0, 1.0 → the same (an inline float keeps its whole value),
    • v[0:1], 1.5, 0x7ff00000, 1.0 → +0: the literal is +inf in bits 63:32, and 1.5/inf fixes up to zero. On main the same instruction makes the denominator a subnormal and returns 1.5. This infinity case is the one my file has that this PR's diff does not — it exercises the special-case path rather than the copy-through path; the last two lines of your fixup test cover sign flips, not class changes.
  • v_div_fmas_f64, emitted constants: 0x3ff80000 → 0x3FF8000000000000 present and the subnormal absent; literal 0x7ff00000 → 0x7FF0000000000000 present; inline 1.5 keeps 0x3FF8000000000000.

On main 3faac557: 4 of the 5 cases fail exactly as SHADER-865 describes. On this head: 5/5, plus your own test_alu_div_fixup.py and test_alu_div_fmas.py pass. Your positional loop and the 0x1-vs-1 distinction cover more than mine; the infinity-in-the-denominator case above is the only thing mine adds, and you are welcome to it.

AI-assisted. No game data.

@sovljanskii

Copy link
Copy Markdown
Contributor

Tested d017a5b8 merged onto main 3faac557 on an Apple M4 (macOS 27.2.1, MoltenVK 1.4.2), CPU only (spirv_mini_interp, no GPU or game files needed): pass, no new failures.

The merge conflicts again, only in docs/design.md: main changed the f16 transcendentals row just above yours (it escaped |x| as \|x\|), and git takes the two rows as one hunk. Keeping main's f16 row and your f64 literal sources row resolves it, and the rest merges cleanly. After that, git diff origin/main -- docs/design.md touches only the f64 literal sources row.

  • python -I -m unittest discover -s tests/semantic -p 'test_alu_div_f*.py': 21/21 OK on Python 3.15.2 and 3.12.16.
  • Negative control: the same two test files against main's tools/ give 8 failures, in test_literal_source_is_the_high_dword and test_f64_literal_source_is_the_high_dword (sources 0, 1 and 2 and the whole test, in each). So the tests catch the old low-dword reading.
  • Full tests/semantic on Python 3.15.2, merged tree vs plain main side by side: 6,248 vs 6,245 tests, and the same 34 failure and error IDs on both (27 test_alu_vop3_mods_hw and 2 test_alu_sdwa_hw NaN rows, 5 test_loop_ballot_barrier). That is the existing arm64 baseline on this machine. None is new, and none goes away.
  • Corpus: none of the 26,503 listings in my work folder uses v_div_fixup_f64 or v_div_fmas_f64, with a literal or without. So the shader_ir.py change can't change any translated module, and translated and spirv-val counts stay the same by construction.

For whoever merges: #3642 (rcp/rsq/sqrt) and #5534 edit the same shader_ir.py op tuple line, so whichever lands after this one needs the one-line union there.

AI-assisted.

@cynbun

cynbun commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested d017a5b8 on RTX 5080, NVIDIA 615.78.08, Linux: pass. The branch no longer merges cleanly with main 561b65a8. The conflict is the op tuple in tools/shader_ir.py, since #2863 is now merged, plus the "f64 literal sources" row in docs/design.md.

Encoding reference (llvm-mc 23.1.3, -mcpu=gfx1030): v_div_fixup_f64 v[0:1], 0x40140000, v[2:3], v[4:5] encodes as [0x00,0x00,0x60,0xd5,0xff,0x04,0x12,0x04,0x00,0x00,0x14,0x40], and v_div_fmas_f64 v[0:1], v[2:3], 0x40140000, v[4:5] as [...,0x02,0xff,0x11,0x04,0x00,0x00,0x14,0x40]. The source field is 0xff (literal) and is followed by a single dword, so the hardware gets 5.0 only if that dword is bits 63:32.

GPU, synthetic compute fixture. Not committed; the literal values are not inline constants, because llvm-mc re-encodes 0x40100000 as inline 4.0. Built and run with this tree's scripts/run_gpu_tests.py and vk_compute_test (-DLEONIDA_VULKAN_HEADERS_FETCH=ON). The expected values agree with the gfx1036 hardware words TheMrBeez posted above.

want main 3faac557 this PR
v_div_fixup_f64 S0 = 0x40140000, 2.0, 10.0 5.0 5.3e-315 5.0
v_div_fmas_f64 2.0, S1 = 0x40080000, 1.0 (VCC 0) 7.0 1.0 7.0
v_div_fixup_f64 4.0, S1 = 0xc0040000, 10.0 -4.0 4.0 -4.0
v_div_fmas_f64 2.0, 3.0, S2 = 0x40140000 11.0 6.0 11.0

The same fixture also has rcp/rsq/sqrt/frexp rows. This PR leaves those as on main, which is expected because #2863 covers them. With this PR merged onto the new main 561b65a8, which contains #2863 (I joined the tuple by hand), all 18 words match. #5268's cs_f64_trans and the other f64 GPU tests (--filter f64) also pass with this PR merged in: 4/4.

CPU

  • python -m unittest discover -s tests/semantic at the head: 7459 tests, 7 failures and 2 errors. 5 of them are the test_loop_ballot_barrier emulate cases, which also fail on main 3faac557. The other 4 (test_cmp_class_f64 x2, test_mutations x2) come from this branch's old base aa2988cc: they fail there too, and pass once merged onto main.
  • On the merge with 561b65a8: test_alu_div_fixup.py 7 OK, test_alu_div_fmas.py 8 OK, test_vop3_const_mods.py OK, and -p '*f64*' OK.

Still open at this head, as TreXito noted: a binary64 word wider than a dword that is written by hand is still flagged f64_literal. For example, v_div_fixup_f64 v[0:1], v[2:3], v[4:5], 0x7ff0000000000000 reads as 0. #3642 has the guard (-0x80000000 <= v <= 0xFFFFFFFF), and main 561b65a8 doesn't have it yet. llvm-mc 23.1.3 rejects such a non-inline 64-bit spelling ("invalid operand"), so only hand-written semantic fixtures hit this.

Overlap: #5534 and milas6669's claim in #5361 also cover these two ops. #2863, now merged, and #3642 edit the same tuple line.

Local fixture (synthetic, not committed): `cs_f64_lit865.asm`

Registers: v[0:1] = 2.0, v[2:3] = 10.0, v[4:5] = 1.0, v[6:7] = 4.0, v[22:23] = 3.0, vcc_lo = 0; each result stored low word first.

v_rcp_f64 v[8:9], 0x40300000                          ; 1/16 = 0.0625
v_rsq_f64 v[10:11], 0x40300000                        ; 0.25
v_sqrt_f64 v[12:13], 0x40300000                       ; 4.0
v_frexp_mant_f64 v[14:15], 0x40180000                 ; 0.75
v_frexp_exp_i32_f64 v16, 0x40180000                   ; 3
v_div_fixup_f64 v[18:19], 0x40140000, v[0:1], v[2:3]  ; 5.0
v_div_fmas_f64 v[20:21], v[0:1], 0x40080000, v[4:5]   ; 7.0
v_div_fixup_f64 v[24:25], v[6:7], 0xc0040000, v[2:3]  ; -4.0
v_div_fmas_f64 v[26:27], v[0:1], v[22:23], 0x40140000 ; 11.0

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

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.