Repository navigation
Translator: apply VOP3 clamp and output scaling to v_add_f64 - #503
Conversation
v_add_f64 is VOP3Inst<"v_add_f64", VOP3_Profile<VOP_F64_F64_F64>> in LLVM AMDGPU; VOPProfile gives it HasClamp (f64 is a modifier type) and HasOMod (floating-point destination). The translator dropped both. The double sum is now scaled by mul:2/mul:4/div:2, then clamped to [0, 1] (FClamp, as the f32 clamp_mods does), before it is split into the register pair. - tests/test_f64_output_modifiers.py: SPIR-V data-flow check per modifier combination, plus NoContraction on the f64 add/scale. - runtime/test/shaders/ps_f64_mods.asm: GPU test (registered in check.py, run_gpu_tests.py and regen_fixtures.py). vk_harness enables shaderFloat64 when the fragment shader declares Float64, else exits 77 (SKIP). - docs/design.md: contract row with the LLVM profile names. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lit the string literal) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
IAmNotSiob
left a comment
There was a problem hiding this comment.
Ran the GPU test this PR was missing. RTX 6000 Ada, NVIDIA 615.71.09, Linux, on the PR head. vk_harness.cpp builds with GCC, and the new shaderFloat64 path works:
python3 scripts/run_gpu_tests.py --filter ps_f64_mods
PASS ps_f64_mods 4096/4096 pixels exact
Negative control, with only tools/rdna2spirv.py swapped back to main:
FAIL ps_f64_mods got ['0x3ff40000', '0x3ff40000', '0x3ff40000', '0x3fd00000'], want ['0x40040000', '0x3ff00000', '0x3fe40000', '0x3ff00000']
That's exactly the "old translator" words you predicted. So the "not yet run on a GPU" note in the docs/design.md row can go, for NVIDIA at least. Two inline notes below, neither blocking. AI-assisted review (Claude Code).
| return g | ||
| return v | ||
|
|
||
| def clamp_mods_f64(self, v, mods): |
There was a problem hiding this comment.
Non-blocking, and it applies to the existing f32 clamp_mods too: on hardware, omod is only honoured when the shader runs with IEEE mode off. LLVM's SIFoldOperands won't fold omod for that reason ("omod is ignored by hardware if IEEE bit is enabled"), and it also avoids it when denormals are enabled for the type, and f64/f16 denormals are on by default. Game pixel shaders normally have IEEE off, so applying it unconditionally is right for the corpus. But it might be worth reading IEEE_MODE from COMPUTE_PGM_RSRC1/SPI_SHADER_PGM_RSRC1 and either dropping omod or refusing when it's set, or at least noting the assumption in the contract row.
| bits = struct.unpack("<I", struct.pack("<f", x))[0] | ||
| return self.tof(self.cu(bits)) | ||
|
|
||
| def dconst(self, x): |
There was a problem hiding this comment.
Nit: this emits a fresh OpCompositeConstruct + OpBitcast at every use, so each clamp costs four extra instructions per call site, inside the dispatch loop. SPIR-V lets you write %d1 = OpConstant %double 1.0 directly (the 64-bit literal takes two words), and dedupe_types (or a small cache keyed by the bits, like cu) would then share them module-wide. That doesn't matter for correctness, and it's fine as a follow-up when more f64 ops arrive.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks @IAmNotSiob for the GPU run and the review. The |
|
Tested 212c52a on NVIDIA GeForce RTX 5070, driver 616.86, Windows 10.0.26200.9711: pass. Built the |
|
Tested 212c52a on two GPUs, Linux 7.3.5 (CachyOS): pass.
Ran |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> # Conflicts: # runtime/test/shaders/check.py # scripts/run_gpu_tests.py # tests/semantic/regen_fixtures.py
IAmNotSiob
left a comment
There was a problem hiding this comment.
The clamp_mods_f64 helper looks right (omod, then FClamp in double). Question: v_mul_f64 and v_fma_f64 use the same VOP3_Profile with HasClamp/HasOMod, so they'd drop clamp/omod the same way v_add_f64 did. Worth covering them here while the helper is fresh, or is that a follow-up?
Also, this now conflicts with main (runtime/test/shaders/check.py, scripts/run_gpu_tests.py, tests/semantic/regen_fixtures.py). The only semantic failure on the old base is test_seeded, which #533 fixed on main. AI-assisted review (Claude Code).
|
Thanks @IAmNotSiob. The translator only handles two f64 ops: The conflicts are resolved in 2720df1: I merged The |
|
Fair point, I missed that TL;DR: my question doesn't apply; nothing more needed here. AI-assisted review (Claude Code). |
New SHADER item, claimed in #374 (#374 (comment)).
What this fixes
v_add_f64ignored its VOP3 output modifiers.clampandmul:2/mul:4/div:2were parsed and then dropped, so the double sum went to the register pair unscaled and unclamped. LLVM AMDGPU defines it asVOP3Inst<"v_add_f64", VOP3_Profile<VOP_F64_F64_F64>, any_fadd>(VOP3Instructions.td), andVOPProfile(SIInstrInfo.td) gives that profileHasClamp = !or(isModifierType<Src0VT>.ret, EnableClamp)(f64 is a modifier type) andHasOMod = !if(IsVOP3P, 0, DstVT.isFP). The newclamp_mods_f64applies omod first, then clamp to [0, 1], in that order, like the f32clamp_modsand thev_cvt_f32_f64fix in #380.NaN and -0 follow the f32 helper: the clamp is GLSL.std.450
FClampon the double, so a NaN result and the sign of a -0 result are left to the driver, as they are for f32 today. The f64 add and the omod multiply areOpFAdd/OpFMul %double, sono_contractiondecorates both, and neither one is fused.Scope:
v_add_f64andv_cvt_f32_f64are the only f64 ops the translator handles. The other f64 ops (v_mul/fma/min/max/ldexp_f64,v_rcp/sqrt/fract/trunc/ceil/floor/rndne_f64,v_cvt_f64_*) are not translated at all, so this PR has nothing to fix there. They have the same LLVM profiles (VOP3_Profile<VOP_F64_F64_F64>,VOP3_Profile<VOP_F64_F64_I32>for ldexp), andclamp_mods_f64is ready for them when they are added.Tests
tests/test_f64_output_modifiers.py: translatesv_add_f64with no modifier,mul:2,mul:4,div:2,clamp,clamp div:2andmul:4 clamp. It follows the SPIR-V data flow fromOpFAdd %doublethrough the scale andFClamp(with the double constants' exact words) into both destination registers, and checksNoContraction. It also recomputes the GPU fixture's expected words in Python. The mini interpreter has no f64, so this is a structure check in the style oftests/test_f64_conversion.py.runtime/test/shaders/ps_f64_mods.asm: GPU test, wired intocheck.py,run_gpu_tests.pyand the semantic fixtures (regen_fixtures.pyneeds no llvm-mc). a = 0.75, b = 0.5; each word is the high word of one double result (the low words are 0):mul:2(a+b)×2 = 2.5 →0x40040000clampmin(a+b, 1) = 1.0 →0x3ff00000clamp div:2(a+b)/2 = 0.625 →0x3fe40000(clamping before the scale would give0x3fe00000)mul:4(a−b)×4 = 1.0 →0x3ff00000Expected mrt0 bits:
0x40040000, 0x3ff00000, 0x3fe40000, 0x3ff00000. The old translator gives0x3ff40000, 0x3ff40000, 0x3ff40000, 0x3fd00000.runtime/test/vk_harness.cpp: no pixel test used f64 before, and the harness never enabledshaderFloat64. It now enables the feature when the fragment shader declaresOpCapability Float64, and exits 77 (SKIP) on a device that lacks it. Not compiled here (this machine has no C++ toolchain).Negative control: with only
tools/rdna2spirv.pyreverted toorigin/main, 6 of the 7 modifier subtests fail. The no-modifier case still passes, as it should.Run on Windows 11, Python 3.11, on
4abd545:python -I -m unittest discover -s tests/semantic: 462 OK.python -I -m pytest -q tests: 1749 passed, 178 skipped, 1 xfailed.regen_fixtures.py --checkandcompileall: OK.CPU-checked only; it needs a GPU run:
python scripts/run_gpu_tests.py --filter ps_f64_modson a device withshaderFloat64. I did not run it (this machine has nollvm-mc, SPIR-V tools or MSVC). The expected words come from the ISA rule and the LLVM profiles above. The contract row indocs/design.mdsays "not yet run on a GPU" until someone runs it.Other open PRs touch the same files:
runtime/test/shaders/check.py,scripts/run_gpu_tests.pyandtests/semantic/regen_fixtures.py(one added line each).No game files.
Not compiled here: this machine has no C++ toolchain, so the
vk_harness.cppchange (enableshaderFloat64when the fragment shader declaresOpCapability Float64, else exit 77) is read-checked only. A build plus one run ofps_f64_modson any GPU withshaderFloat64would confirm both.AI-assisted (Claude Code).
🤖 Generated with Claude Code