Skip to content

rdna2spirv: v_cmp*_f64 small literals are upper-half dwords - #5104

Open
tupacfan04 wants to merge 1 commit into
ItsNotRexed:mainfrom
tupacfan04:fix/cmp-f64-small-literal
Open

tupacfan04 wants to merge 1 commit into
ItsNotRexed:mainfrom
tupacfan04:fix/cmp-f64-small-literal

Conversation

@tupacfan04

@tupacfan04 tupacfan04 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue: #5032 (comment)

Depends on: none. #2863, #3642 and #3791 extend f64_literal for arithmetic f64 ops in the same parser area; this is the compare path, so they should merge independently, but whichever lands second rebases.

What and why

A 32-bit literal on a binary64 compare supplies bits 63:32, with bits 31:0 zero. read_f64_words checked a dead "literal" key, and shader_ir marked f64_literal only for six arithmetic ops. Small hex literals on v_cmp*_f64 were therefore treated as inline integers: v_cmp_eq_f64_e64 s4, 0x1, v[4:5] compared against 5e-324 (inline 1, 2^-1074) instead of 2^-1042 (the high dword 0x1).

The parser now marks compare literals with the same hex/decimal rule it uses for arithmetic f64 sources, over both compare operands (e64 has a destination first; e32 does not), and read_f64_words reads the key the parser actually writes. v_cmp*_class_f64 is excluded because it has its own literal path.

Tested

  • python -I tests/semantic/test_cmp_f64.py: 6 tests, OK. The new case checks inequality with the inline 1 pair and exact equality with the (low 0, high 1) pair.
  • Negative control: the same test against clean main 561b65a's tools/shader_ir.py and tools/rdna2spirv.py fails the new case with got [1, 1, 0, 0], expected [0, 1, 1, 0].
  • Rebased onto 561b65a8 (head 886dcabee); the conflict was main's expanded f64 op list in shader_ir.py, resolved by keeping both. Re-ran: test_cmp_f64.py 6/6, test_cmp_class_f64.py 6/6, test_valu_rcp_f64.py 54/54, test_valu_rsq_f64.py 64/64, test_valu_sqrt_f64.py 53/53, test_valu_frexp_mant_f64.py 53/53, test_valu_frexp_exp_i32_f64.py 50/50.
  • Full semantic suite before the rebase (3faac55): 6,246 tests, 3 failures / 2 errors, all in tests/semantic/test_loop_ballot_barrier.py; clean main gave the same 15-test file result. The rebase only merged main's expanded f64 op list into the same helper.
  • python -m compileall -q make_game.py tools tests: exit 0.
  • No GPU or driver used.

Out of scope: tools/rdna2_decode.py prints a small f64 literal word as decimal, which loses the same distinction for Python-backend disassembly; that is a separate item.

AI-assisted: yes (OpenCode).

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. (no new rule; this restores the documented f64 literal-source rule)

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested f387d76d on Windows 11, CPython 3.14, CPU only (no GPU, no game files): pass for the translator change. All six tests in tests/semantic/test_cmp_f64.py pass. The new test fails with the parent translator at 3faac557.

Please correct the expected value in the new test comment. An upper dword of 1 and a lower dword of 0 encode 2^-1042. The comment says 2^-32. To check the value independently:

import struct
assert struct.unpack("<d", struct.pack("<II", 0, 1))[0] == 2**-1042

I also ran two synthetic v_cmp_eq_f64_e64 comparisons with the literal 0x1. It compares equal to the register pair (low 0, high 1). It compares unequal to (low 0, high 0x3df00000), which encodes 2^-32. A positive equality check against the first pair would also distinguish the correct upper-word value from any other value unequal to the inline integer.

AI-assisted.

@tupacfan04
tupacfan04 marked this pull request as ready for review November 23, 2026 08:24

@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 f387d76d1 on Windows 11: tests/semantic/test_cmp_f64.py -> 6 tests, OK; git diff --check clean.

v_cmp*_f64 with a small literal now reads the literal as the comparison's upper-half dword (the form shader_ir prints), instead of taking the low half and comparing the wrong value; the added cases cover the literal forms next to the register and pair forms. No blocking issue.

@tupacfan04
tupacfan04 force-pushed the fix/cmp-f64-small-literal branch from 22c31fa to 3ceecb6 Compare November 23, 2026 08:28
@tupacfan04

Copy link
Copy Markdown
Contributor Author

Thanks @shdann — corrected in 3ceecb6. A high dword of 1 is 2^-1042 (not 2^-32), and the test now also asserts exact equality with the (low 0, high 1) pair, not just inequality with the inline 1 pair. test_cmp_f64.py is 6/6 on the head; the negative control on main now gives [1, 1, 0, 0] vs expected [0, 1, 1, 0]. PR body updated.

@tylerstraub

Copy link
Copy Markdown
Contributor

Tested 3ceecb66c68e475e5ceff038a4c4daf49287b995 on NVIDIA GeForce RTX 5090, 595.99.02 (Linux).

At 3ceecb66 on Linux RTX 5090, I translated a hand-written fragment listing containing four v_cmp_eq_f64_e64 comparisons, validated the resulting SPIR-V for Vulkan 1.2, and drew a 32×32 triangle with the rebuilt Vulkan harness.

Against a register pair (low 1, high 0), the hexadecimal literal 0x1 compared false and inline 1 compared true. Against (low 0, high 1), the same two comparisons gave true and false respectively. All 1,024 pixels returned RGBA (0, 1, 1, 0). This checks the listing-to-GPU path with the literal in the first source position. Binary instruction decoding and native RDNA2 execution were outside this draw.

AI-assisted verification.

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

Integrated #5115 cb6bcea with #5104 3ceecb6 in private scratch. Six hand-encoded ordinary f64 equality compares distinguished inline 1 from literal dword 1 through formatting, parsing, translation and CPU interpretation, including e64 right-hand operands. All six focused compare tests passed. Across 26,503 cached listings there were no static f64 compares, and all 468 source operands at the identified f64 sites were registers. Applicability of the literal case comes from the synthetic tests. AI-assisted, CPU only.

@Atomic258

Copy link
Copy Markdown
Contributor

Tested 3ceecb6 on Windows Server 2022, CPython 3.13.12: pass

  • python -I tests/test_shader_ir.py -> Ran 37 tests, OK; python -I -m unittest discover -s tests/semantic -p "test_cmp_f64.py" -> Ran 6 tests, OK
  • Negative control (rdna2spirv.py + shader_ir.py swapped to upstream/main): FAILED (failures=1)

git diff --check clean; python -m compileall -q exit 0. No game files, no GPU. AI-assisted (Devin).

@kotekompotek

Copy link
Copy Markdown

Independent Windows CPU verification on 3ceecb66:

  • python -m pytest tests/semantic/test_cmp_f64.py --basetemp D:\temp\pytest_tmp -q: 6 passed in 1.60s

A 32-bit literal on a binary64 compare supplies bits 63:32, but read_f64_words checked a dead literal key and shader_ir marked f64_literal only for six arithmetic ops. Small hex literals such as 0x1 were therefore treated as inline integers and compared against 5e-324 instead of 2^-32. Mark compare literals with the same hex/decimal rule and read the key the parser writes.
@tupacfan04
tupacfan04 force-pushed the fix/cmp-f64-small-literal branch from 3ceecb6 to 886dcab Compare November 23, 2026 12:06
@tupacfan04

Copy link
Copy Markdown
Contributor Author

Rebased onto 561b65a8 to clear the conflict; head is now 886dcabee. The conflict was main's expanded f64 op list in shader_ir.py; I kept both it and this PR's helper.

Re-ran after the rebase: test_cmp_f64.py 6/6, test_cmp_class_f64.py 6/6, test_valu_rcp_f64.py 54/54, test_valu_rsq_f64.py 64/64, test_valu_sqrt_f64.py 53/53, test_valu_frexp_mant_f64.py 53/53, test_valu_frexp_exp_i32_f64.py 50/50. Negative control on 561b65a8 still fails the new case with [1,1,0,0] vs [0,1,1,0]. compileall and git diff --check clean. No GPU.

@cynbun

cynbun commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Gap found while working on the same rule (checked against llvm-mc 23.1.3, gfx1030, on top of main 561b65a8 + #5832): v_cmp(x)_class_f64 is not covered here — its literal src0 is still read by value, so a small hex literal is taken as the inline constant rather than bits 63:32. v_div_fixup_f64 / v_div_fmas_f64 read every literal as the low word (that half is #3791's). One way that worked here: a shader_ir.f64_sources() that marks every binary64 source so the value-based readers (cmp, cmp_class, div_fixup/fmas, ldexp) honour the literal flag; constants wider than one dword stay unmarked so test_alu_ldexp_f64 keeps passing. A fixture family where literal vs inline give different results passed spirv-val and 4096/4096 pixels on an RTX 5080. Happy to share the patch if useful. AI-assisted (Claude Code).

@tupacfan04

Copy link
Copy Markdown
Contributor Author

Thanks. I checked the class path before scoping it out: v_cmp_class_f64 with literal 0x1 and inline 1 both hit +denormal (mask 0x80 -> [1, 1, 0, 0]) and both miss +normal (mask 0x100 -> [0, 0, 0, 0]). Every small literal (|N| <= 64) is subnormal either way, so the class result does not distinguish them; if you have a class case where it does, please share it.

v_div_fixup_f64 / v_div_fmas_f64 are #3791's scope, as you note. The f64_sources() refactor sounds useful, but it would overlap #3791 and the class PRs, so I kept this PR to v_cmp*_f64 as the description says.

@cynbun

cynbun commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

You're right, thanks — I withdraw the v_cmp_class_f64 point. Every value that LLVM can print as either an inline constant or a literal lands in the same class both ways: 1..64 is a subnormal (inline 0x…NN vs literal 0x000000NN_00000000), -1..-16 is NaN either way (0xFFFF…), and the inline floats' high words equal the inline doubles. So class results can't distinguish them and there's nothing to add here. 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