Skip to content

Translator: apply VOP3 clamp and output scaling to v_add_f64 - #503

Merged
ItsNotRexed merged 5 commits into
ItsNotRexed:mainfrom
tomgamer22:f64-output-modifiers
Nov 22, 2026
Merged

ItsNotRexed merged 5 commits into
ItsNotRexed:mainfrom
tomgamer22:f64-output-modifiers

Conversation

@tomgamer22

Copy link
Copy Markdown
Contributor

New SHADER item, claimed in #374 (#374 (comment)).

What this fixes

v_add_f64 ignored its VOP3 output modifiers. clamp and mul:2/mul:4/div:2 were parsed and then dropped, so the double sum went to the register pair unscaled and unclamped. LLVM AMDGPU defines it as VOP3Inst<"v_add_f64", VOP3_Profile<VOP_F64_F64_F64>, any_fadd> (VOP3Instructions.td), and VOPProfile (SIInstrInfo.td) gives that profile HasClamp = !or(isModifierType<Src0VT>.ret, EnableClamp) (f64 is a modifier type) and HasOMod = !if(IsVOP3P, 0, DstVT.isFP). The new clamp_mods_f64 applies omod first, then clamp to [0, 1], in that order, like the f32 clamp_mods and the v_cvt_f32_f64 fix in #380.

NaN and -0 follow the f32 helper: the clamp is GLSL.std.450 FClamp on 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 are OpFAdd/OpFMul %double, so no_contraction decorates both, and neither one is fused.

Scope: v_add_f64 and v_cvt_f32_f64 are 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), and clamp_mods_f64 is ready for them when they are added.

Tests

  • tests/test_f64_output_modifiers.py: translates v_add_f64 with no modifier, mul:2, mul:4, div:2, clamp, clamp div:2 and mul:4 clamp. It follows the SPIR-V data flow from OpFAdd %double through the scale and FClamp (with the double constants' exact words) into both destination registers, and checks NoContraction. 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 of tests/test_f64_conversion.py.

  • runtime/test/shaders/ps_f64_mods.asm: GPU test, wired into check.py, run_gpu_tests.py and the semantic fixtures (regen_fixtures.py needs 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 → 0x40040000
    • clamp min(a+b, 1) = 1.0 → 0x3ff00000
    • clamp div:2 (a+b)/2 = 0.625 → 0x3fe40000 (clamping before the scale would give 0x3fe00000)
    • mul:4 (a−b)×4 = 1.0 → 0x3ff00000

    Expected mrt0 bits: 0x40040000, 0x3ff00000, 0x3fe40000, 0x3ff00000. The old translator gives 0x3ff40000, 0x3ff40000, 0x3ff40000, 0x3fd00000.

  • runtime/test/vk_harness.cpp: no pixel test used f64 before, and the harness never enabled shaderFloat64. It now enables the feature when the fragment shader declares OpCapability 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.py reverted to origin/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 --check and compileall: OK.

CPU-checked only; it needs a GPU run: python scripts/run_gpu_tests.py --filter ps_f64_mods on a device with shaderFloat64. I did not run it (this machine has no llvm-mc, SPIR-V tools or MSVC). The expected words come from the ISA rule and the LLVM profiles above. The contract row in docs/design.md says "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.py and tests/semantic/regen_fixtures.py (one added line each).

No game files.
Not compiled here: this machine has no C++ toolchain, so the vk_harness.cpp change (enable shaderFloat64 when the fragment shader declares OpCapability Float64, else exit 77) is read-checked only. A build plus one run of ps_f64_mods on any GPU with shaderFloat64 would confirm both.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

tomgamer22 and others added 2 commits November 22, 2026 01:09
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 IAmNotSiob 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 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).

Comment thread tools/rdna2spirv.py
return g
return v

def clamp_mods_f64(self, v, mods):

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.

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.

Comment thread tools/rdna2spirv.py
bits = struct.unpack("<I", struct.pack("<f", x))[0]
return self.tof(self.cu(bits))

def dconst(self, x):

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.

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

Copy link
Copy Markdown
Contributor Author

Thanks @IAmNotSiob for the GPU run and the review. The docs/design.md row now records your pass in place of the "not yet run on a GPU" note. I've left both inline notes as follow-ups: omod is only honoured with IEEE mode off (this applies to the f32 clamp_mods too), and the dconst values should be shared, which makes sense once more f64 ops arrive. AI-assisted (Claude Code).

@Ayinaki

Ayinaki commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 212c52a on NVIDIA GeForce RTX 5070, driver 616.86, Windows 10.0.26200.9711: pass.

Built the vk_harness change with the VS 2026 C++ toolchain, then ran python -I scripts/run_gpu_tests.py --llvm-mc <llvm-mc-21.1.8> --build runtime/build/Release --work gpu-work --filter ps_f64_mods. Result: 1 shader translated and passed spirv-val; ps_f64_mods PASS — 4096/4096 pixels exact; 1/1 passed. The device supports shaderFloat64 and the expected mul:2, clamp, clamp div:2, and mul:4 bits all matched.

@esteban-dcp

Copy link
Copy Markdown

Tested 212c52a on two GPUs, Linux 7.3.5 (CachyOS): pass.

  • NVIDIA RTX 4050 Laptop (Ada), driver 620.73.04, subgroup 32: 70 shaders: 70 translated, 70 pass spirv-val; 41/67 passed, 26 wave64 skipped; with LEONIDA_W64_EMULATE=1: 67/67 passed
  • AMD Radeon 740M (RDNA3, RADV, Mesa 26.3.1), subgroup 32..64: 67/67 passed
  • New ps_f64_mods: 4096/4096 pixels exact on all three runs
  • CPU: unittest discover -s tests/semantic 462 OK; tests/test_f64_output_modifiers.py OK

Ran python -I scripts/run_gpu_tests.py --build runtime/build/linux on the PR head as-is, with the test programs built from this branch, VK_LOADER_DRIVERS_SELECT pinning each GPU (device line checked in each run) and VK_LOADER_LAYERS_DISABLE=~implicit~. 0 failures.

tomgamer22 and others added 2 commits November 22, 2026 01:55
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 IAmNotSiob 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.

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

@tomgamer22

Copy link
Copy Markdown
Contributor Author

Thanks @IAmNotSiob. The translator only handles two f64 ops: v_add_f64 and v_cvt_f32_f64. v_mul_f64 and v_fma_f64 aren't translated yet, so they can't drop the modifiers. Whoever adds them can apply clamp_mods_f64 the same way, and I've noted that in the follow-ups.

The conflicts are resolved in 2720df1: I merged main at 22590c7 (which has the #533 test_seeded fix) and kept both sides' entries in check.py, run_gpu_tests.py and regen_fixtures.py. Semantic suite OK, regen_fixtures.py --check OK.

The linux-gpu-tools failure is cs_w32_lane_wrap and cs_wqm_w32 on llvmpipe. main's own Runtime run at 22590c7 (44969381350) fails on the same two, so it doesn't come from this PR. AI-assisted (Claude Code).

@IAmNotSiob

Copy link
Copy Markdown
Contributor

Fair point, I missed that v_mul_f64/v_fma_f64 aren't translated at all yet, so there's nothing for them to drop. Noting it as a follow-up is right. Thanks for the rebase.

TL;DR: my question doesn't apply; nothing more needed here. AI-assisted review (Claude Code).

@ItsNotRexed
ItsNotRexed merged commit 6a55f98 into ItsNotRexed:main Nov 22, 2026
8 of 9 checks passed
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.

5 participants