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

@Vonsek

Vonsek commented Nov 22, 2026

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

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.

8 participants