Skip to content

rdna2spirv: literal source of v_rcp/rsq/sqrt_f64 is bits 63:32; run the f64 tests through the translator - #3642

Open
kapitche wants to merge 2 commits into
ItsNotRexed:mainfrom
kapitche:semf64
Open

kapitche wants to merge 2 commits into
ItsNotRexed:mainfrom
kapitche:semf64

Conversation

@kapitche

Copy link
Copy Markdown
Contributor

AI-assisted.

Claim: batch A of the model-only semantic tests (coordination issue #3381, claiming: tests/semantic batch A (float)); follow-up of the finding under #3472.

Translator fix (found by the rewrite). A 32-bit literal on v_rcp_f64, v_rsq_f64 or v_sqrt_f64 was read as the low dword of the binary64 operand. For a 64-bit float operand the literal supplies bits 63:32 (docs/design.md, "f64 literal sources"), and tools/shader_ir.py only flagged that for v_add_f64 and v_cvt_f32_f64. Repro: v_sqrt_f64 v[4:5], 0x40100000 (the word 4.0 is the high half of) translated to the square root of a 2^-1074-scale denormal; it must give 2.0. Same for v_rcp_f64 (0.25 expected) and v_rsq_f64 (0.5). One line: those three op names join the existing list in parse_line.

What was wrong with the tests. test_valu_{rcp,rsq,sqrt,cvt_f32}_f64.py, test_valu_{mul,fma,fract,minmax,trig}_f64.py and test_valu_frexp{,_mant,_exp_i32}_f64.py (1.1k to 1.8k lines each) never imported support or rdna2spirv; breaking the op in tools/rdna2spirv.py left them green. Beyond that:

Fix. test_valu_{rcp,rsq,sqrt,cvt_f32}_f64.py now translate each case and run it on tests/spirv_interp.py (the interpreter that evaluates binary64): hand-written expected words (special values, ties, overflow, signed zeros, the literal-is-high-dword rule), abs/neg, mul:2/mul:4/div:2/clamp with omod-before-clamp cases, SGPR-pair / inline / literal sources, and seeded random operands against a plain-Python reference (rcp and rsq allow the hardware's 1 ulp). Helpers in tests/semantic/f64_ref.py. tests/spirv_interp.py needed three additions: OpFDiv on binary64 (signed infinity for x / +-0, instead of the binary32 rounding), OpFConvert, and Sqrt/InverseSqrt returning NaN / +-inf where Python's math.sqrt raises.

Tested. Python 3.12.16, Linux (WSL): python -I -m unittest discover -s tests/semantic -p '*f64*' runs 173 tests, all pass; the whole tests/semantic suite (6697 tests, OK, 4 skipped and 1 expected failure as on main), and tests/test_f64_conversion.py, test_f64_transcendentals.py, test_spirv_interp_bits.py pass.

Negative control. With a mutated copy of tools/ (LEONIDA_TOOLS_DIR): v_sqrt_f64 lowered as InverseSqrt: new file fails (59 failures), old file OK; v_rcp_f64 as 2/x: 87, old OK; v_rsq_f64 as Sqrt: 66, old OK; v_cvt_f32_f64 converting a constant: 123, old OK; clamp bound 0.5 in clamp_mods_f64: 3 / 2 / 2 failures, old OK. Reverting the shader_ir.py literal fix: 6 failures in each of the three files.

@ramosbezzu

Copy link
Copy Markdown

Tested baf99b3 on CPU only (no GPU, no game files), Linux (WSL2), Python 3.12.3: pass

  • python -I -m unittest discover -s tests/semantic -p '*f64*' -> 173 tests OK.
  • Negative control: the rewritten test_valu_*_f64.py files with main f847748c's tools/shader_ir.py (through LEONIDA_TOOLS_DIR) -> FAILED (failures=18), so the new tests do reach the translator and catch the literal bug.
  • Code read: the one-line parse_line change marks the literal for the three unary ops, and Gen's existing f64_literal branch (lo = 0, hi = literal) then applies to them. Matches the "f64 literal sources" row.
  • Not run: spirv-val (no SPIRV-Tools on this machine) and the rest of the suite.

Two things for the merge:

  1. Overlap with BUILD-828: decode literal words in unary binary64 shader operations #2863 (@Vonsek, BUILD-828, open): it changes the same parse_line line to add v_rcp_f64, v_rsq_f64, v_sqrt_f64 and also v_frexp_mant_f64, v_frexp_exp_i32_f64, with one shared source decoder. The translator half of this PR is a subset of that one, so the two conflict on that line; the test rewrite here is independent of it.
  2. Same bug left on other f64 ops (in neither PR). On this head, v_frexp_exp_i32_f64 v0, 0x40100000 and v_frexp_mant_f64 v[0:1], 0x40100000 both give 0 through the translator and spirv_mini_interp (4.0 expected: exponent 3, mantissa 0.5); BUILD-828: decode literal words in unary binary64 shader operations #2863 covers those two. v_div_fmas_f64 and v_div_fixup_f64 are covered by nobody: v_div_fmas_f64 v[0:1], v[2:3], 0x40100000, v[4:5] emits OpCompositeConstruct %v2uint_d %c40100000 %c0 (literal in the low dword), and v_div_fixup_f64 uses the literal as its low word the same way. I am posting that as a finding in the coordination issue and will take the fix, stacked on whichever of the two PRs lands.

AI-assisted.

@kapitche

Copy link
Copy Markdown
Contributor Author

Rebased onto main aa2988c; conflict in tools/shader_ir.py (f64 literal opcode tuple: main added trunc/ceil/rndne/floor_f64, ours rcp/rsq/sqrt_f64), merged into one tuple; tests: full tests/semantic 6831 run (only the 10 failures that also fail on main), all *_f64 semantic files and test_spirv_interp_bits/f64_glsl OK.

…rce of v_rcp/rsq/sqrt_f64 is bits 63:32

test_valu_{rcp,rsq,sqrt,cvt_f32}_f64 now translate their cases and run them on tests/spirv_interp.py instead of a
self-built model. Doing so showed that a 32-bit literal on v_rcp_f64, v_rsq_f64 and v_sqrt_f64 was read as the low
dword of the operand; tools/shader_ir.py now flags it as the high dword like v_add_f64 and v_cvt_f32_f64.

The model-only mul/fma/fract/minmax/trig/frexp f64 files are removed: the ops are not translated (a refusal pin
replaces them), trig is not an RDNA2 instruction, and test_alu_frexp_f64.py already runs the frexp forms.
spirv_interp gains binary64 OpFDiv, OpFConvert and IEEE Sqrt/InverseSqrt edge cases.
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.

2 participants