mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 13:38:36 +00:00
RDNA2 ignores the VOP3 output modifier of the f64 VALU arithmetic and rounding ops while f64 denormals are kept, which is the MODE this translator models. I measured it with tools/hw_oracle raw kernels on a gfx1036 iGPU (the Radeon iGPU of a Ryzen 7 7800X3D, Windows driver 31.0.24002.92, code object v5), wave32 and wave64 identical, over the four combinations of MODE.IEEE and denorm_mode_16_64. mul:2 / mul:4 / div:2 changed nothing for v_add_f64, v_rcp_f64, v_rsq_f64, v_sqrt_f64, v_trunc_f64, v_ceil_f64, v_floor_f64 and v_rndne_f64 while f64 denormals were kept, applied only with MODE.IEEE off and the denormals flushed, and clamp applied in every one of those runs. The two f64 call sites that still forwarded a full modifier list now hand clamp_mods_f64 only the clamp they act on, and the rows that described the scaling for those ops are corrected to what the hardware does. This is SHADER-721, the binary64 counterpart of the f16 omod row (#4542, "an f16 destination ignores omod"). The row for v_add_f64 records the sweep and how it was confirmed and keeps the LLVM VOP3 citation main already had; the rcp/rsq/sqrt and rounding rows keep the measurements others contributed (the v_sqrt_f64 ulp errors, @timelery's sweep of the four rounding ops) and only change the sentence about the omod. One contradiction is removed on the way: main's rounding row said the translator applies the modifiers "as those rows do" while the code applied them to ops whose own row says they are ignored. Ops with their own rows are left exactly as they are - v_cvt_f64_i32/u32, v_cvt_f32_f64, v_frexp_mant_f64, v_div_fixup_f64 and v_ldexp_f64 keep what their rows measure. The same rule probably reaches the first three as well, and tests/semantic/test_alu_ldexp_f64.py already knows it for ldexp; I raised that in #5256 as a finding instead of folding unmeasured ops into this change. Counted over the whole corpus, not a sample: 216 v_add_f64 in 5 of the 26,503 listings, none carrying an omod or a clamp, and no f64 mnemonic in the corpus carries one, so no game shader changes behaviour here. The two translated fixtures that do carry a modifier move to the unmodified result, which is what the sweep predicts: cs_f64_add now reads 0x0 0x3ff40000. Expectations follow the rule in tests/semantic/test_valu_add_f64.py, tests/semantic/test_f64_round.py, tests/test_f64_output_modifiers.py, tests/test_f64_transcendentals.py and the two fixtures with their runners. tools/f64_diff.py keeps its f64-omod case as a known divergence and tests/test_f64_diff.py now expects it to mismatch the ISA reference rather than match it: the reference applies the omod the ISA text describes, the translator no longer emits it, and that case is the one MODE in which the hardware does apply it. ps_f64_mods, cs_f64_add and ps_f64_round pass on an NVIDIA RTX 4090 (driver 32.0.16.2124) and on the gfx1036 iGPU above, 3/3 on both; cs_f64_add reads the same words on Intel UHD 630 / Mesa ANV at this head (@gertify).
137 lines
6.3 KiB
Python
137 lines
6.3 KiB
Python
"""Check SPIR-V lowering and output modifiers for v_rcp_f64, v_rsq_f64 and v_sqrt_f64.
|
|
|
|
LLVM defines all three as VOP1Inst<..., VOP_F64_F64>, whose VOP3 (_e64) form carries input modifiers
|
|
(abs, neg) and output modifiers (mul:2, mul:4, div:2, clamp). Only clamp is lowered: RDNA2 ignores the omod of
|
|
these ops while f64 denormals are kept, the MODE this translator assumes (docs/design.md, "v_add_f64 output
|
|
modifiers").
|
|
"""
|
|
|
|
import re
|
|
import shutil
|
|
import struct
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
TEST_DIR = Path(__file__).resolve().parent
|
|
sys.path[:0] = [str(TEST_DIR.parent / "tools"), str(TEST_DIR)]
|
|
|
|
from listing import translate_listing
|
|
|
|
|
|
def words(x):
|
|
bits = struct.unpack("<Q", struct.pack("<d", x))[0]
|
|
return bits & 0xFFFFFFFF, bits >> 32
|
|
|
|
|
|
def load_double(lo_reg, hi_reg, value):
|
|
lo, hi = words(value)
|
|
return f"v_mov_b32 v{lo_reg}, {lo:#x}\nv_mov_b32 v{hi_reg}, {hi:#x}"
|
|
|
|
|
|
class F64TranscendentalsTest(unittest.TestCase):
|
|
def _check_store_pair(self, spv, value_id, dst_lo, dst_hi):
|
|
def result(instruction):
|
|
match = re.search(rf"^ *(%\w+) = {instruction}$", spv, re.M)
|
|
self.assertIsNotNone(match, f"Missing SPIR-V instruction: {instruction}")
|
|
return match.group(1)
|
|
|
|
pair = result(rf"OpBitcast %v2uint_d {value_id}")
|
|
for index, reg in ((0, f"%c{dst_lo}"), (1, f"%c{dst_hi}")):
|
|
word = result(rf"OpCompositeExtract %uint {pair} {index}")
|
|
dest = result(rf"OpAccessChain %_ptr_Function_uint %vgpr {reg}")
|
|
if re.search(rf"(?m)^ *OpStore {re.escape(dest)} {re.escape(word)}$", spv):
|
|
continue
|
|
old_word = result(rf"OpLoad %uint {dest}")
|
|
masked = result(rf"OpSelect %uint %\w+ {word} {old_word}")
|
|
self.assertRegex(spv, rf"(?m)^ *OpStore {dest} {masked}$")
|
|
|
|
def _double_const_id(self, spv, value):
|
|
def result(instruction):
|
|
match = re.search(rf"^ *(%\w+) = {instruction}$", spv, re.M)
|
|
self.assertIsNotNone(match, f"Missing SPIR-V instruction: {instruction}")
|
|
return match.group(1)
|
|
|
|
lo, hi = words(value)
|
|
pair = result(
|
|
rf"OpCompositeConstruct %v2uint_d {result(f'OpConstant %uint {lo}')} "
|
|
rf"{result(f'OpConstant %uint {hi}')}"
|
|
)
|
|
return result(rf"OpBitcast %double {pair}")
|
|
|
|
def test_v_rcp_rsq_sqrt_f64_lowering_and_modifiers(self):
|
|
# clamp is emitted as GLSL.std.450 FClamp; no omod is emitted, because RDNA2 ignores it here
|
|
# (docs/design.md, "v_add_f64 output modifiers").
|
|
ops = (
|
|
("v_rcp_f64_e32", "", r"OpFDiv %double %\w+ %\w+", False),
|
|
("v_rsq_f64_e32", "", r"OpExtInst %double %glsl InverseSqrt %\w+", False),
|
|
("v_sqrt_f64_e32", "", r"OpExtInst %double %glsl Sqrt %\w+", False),
|
|
("v_rcp_f64_e64", "mul:2", r"OpFDiv %double %\w+ %\w+", False),
|
|
("v_rsq_f64_e64", "clamp div:2", r"OpExtInst %double %glsl InverseSqrt %\w+", True),
|
|
("v_sqrt_f64_e64", "clamp mul:4", r"OpExtInst %double %glsl Sqrt %\w+", True),
|
|
)
|
|
prefix = load_double(0, 1, 4.0)
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
for op, mods, pattern, clamp in ops:
|
|
with self.subTest(op=op, mods=mods):
|
|
line = f"{op} v[4:5], v[0:1] {mods}".rstrip()
|
|
spv = translate_listing(Path(directory), f"{prefix}\n{line}")
|
|
self.assertIn("OpCapability Float64", spv)
|
|
m = re.search(rf"^ *(%\w+) = {pattern}$", spv, re.M)
|
|
self.assertIsNotNone(m, f"Missing {pattern} in:\n{spv}")
|
|
val = m.group(1)
|
|
self.assertNotRegex(spv, r"OpFMul %double")
|
|
if clamp:
|
|
zero_id = self._double_const_id(spv, 0.0)
|
|
one_id = self._double_const_id(spv, 1.0)
|
|
m_cl = re.search(
|
|
rf"^ *(%\w+) = OpExtInst %double %glsl FClamp {val} {zero_id} {one_id}$", spv, re.M
|
|
)
|
|
self.assertIsNotNone(m_cl)
|
|
val = m_cl.group(1)
|
|
else:
|
|
self.assertNotIn("FClamp", spv)
|
|
self._check_store_pair(spv, val, 4, 5)
|
|
if shutil.which("spirv-as") and shutil.which("spirv-val"):
|
|
spv_bin = Path(directory) / f"{op}.spv"
|
|
p_as = subprocess.run(
|
|
["spirv-as", "--target-env", "vulkan1.2", "-o", str(spv_bin), "-"],
|
|
input=spv, text=True, capture_output=True,
|
|
)
|
|
self.assertEqual(p_as.returncode, 0, p_as.stderr)
|
|
p_val = subprocess.run(["spirv-val", "--target-env", "vulkan1.2", str(spv_bin)],
|
|
text=True, capture_output=True)
|
|
self.assertEqual(p_val.returncode, 0, p_val.stderr)
|
|
|
|
def test_input_modifiers_abs_neg_and_inline_constant(self):
|
|
prefix = load_double(0, 1, -4.0)
|
|
body = "\n".join((
|
|
prefix,
|
|
"v_sqrt_f64_e64 v[2:3], |v[0:1]|",
|
|
"v_rcp_f64_e64 v[4:5], -v[2:3]",
|
|
"v_rsq_f64_e64 v[6:7], 4.0",
|
|
))
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
spv = translate_listing(Path(directory), body)
|
|
self.assertIn("OpExtInst %double %glsl FAbs", spv)
|
|
self.assertIn("OpFNegate %double", spv)
|
|
self.assertIn("OpExtInst %double %glsl Sqrt", spv)
|
|
self.assertIn("OpFDiv %double", spv)
|
|
self.assertIn("OpExtInst %double %glsl InverseSqrt", spv)
|
|
if shutil.which("spirv-as") and shutil.which("spirv-val"):
|
|
spv_bin = Path(directory) / "mods.spv"
|
|
p_as = subprocess.run(
|
|
["spirv-as", "--target-env", "vulkan1.2", "-o", str(spv_bin), "-"],
|
|
input=spv, text=True, capture_output=True,
|
|
)
|
|
self.assertEqual(p_as.returncode, 0, p_as.stderr)
|
|
p_val = subprocess.run(["spirv-val", "--target-env", "vulkan1.2", str(spv_bin)],
|
|
text=True, capture_output=True)
|
|
self.assertEqual(p_val.returncode, 0, p_val.stderr)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|