diff --git a/docs/experiments.md b/docs/experiments.md index be66c1b5fa..9837844cd6 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -11075,6 +11075,14 @@ Scope: decoding and scanner recognition only. None of these forms is lowered by measurement on hardware or in a Windows run backs the descriptions. `v_interp_p1_f32_e64` and `v_interp_p2_f32_e64` (0x200 and 0x201 on gfx1013) are not decoded by this change. +**The VINTRP spelling and the startup translator (#1890).** The 32-bit VINTRP `v_interp_mov_f32_e32` now prints +its selector the same way (`p10`, `p20`, `p0`) instead of a VGPR; any other value of the field becomes a +four-byte `v_interp_mov_f32_e32_unsupported` record, where llvm-objdump prints `invalid_param_`. +`tools/startup/gfx2hlsl.py` lowers only `p0`, the flat copy, in both encodings. `p10` (P1 - P0) and `p20` +(P2 - P0) need per-vertex attribute values, which the FXC path does not have, so they are refused with the +selector in the reason instead of being read as `p0`. `tests/startup_flat_param_check.py` replaces the first read +of its pixel stage with each selector in both encodings; its `binary` mode checks those words with llvm-mc. + ## Startup translator: conversion operations The previous draw fixtures did not run the plain conversion operations in `tools/startup/gfx2hlsl.py` before: `v_cvt_f32_u32`, `v_cvt_f32_i32`, `v_cvt_u32_f32`, `v_cvt_f32_ubyte0` to `3`, `v_cvt_f32_f16` and `v_cvt_f16_f32`. `tests/startup_gfx2hlsl_cvt_alu_check.py` draws them with the shared runner `tests/startup_gfx2hlsl_lane_alu.py`. Each draw applies one operation to each of four pixels' flat inputs, alternating the `_e32` and `_e64` spellings, and compares the four result words exactly. diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 034ce2a07b..2a436de482 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -815,7 +815,9 @@ These are measured on one executable; treat them as hypotheses for others. exported registers, so integer words reach the pixel stage unchanged. FXC folds `asfloat()` of a constant and flushes binary32 denormal patterns such as 1 or 2 to 0, which a float copy lost ([#1259](https://github.com/R0Y00/Xbox2Windows/issues/1259)). Colour exports of constant integer - words still go through `float4` and have the same loss. Reproduce with + words still go through `float4` and have the same loss. Only the `p0` selector is lowered; + `p10` and `p20` are refused in both encodings + ([#1890](https://github.com/R0Y00/Xbox2Windows/issues/1890)). Reproduce with `ctest --preset release --no-tests=error -R "^startup_flat_param_"`. - **Non-fused multiply-add.** The gfx1013 `v_mad_f32`, `v_mac_f32`, `v_madmk_f32`, `v_madak_f32` and `v_mad_legacy_f32` forms round the product before the sum and flush diff --git a/tests/rdna2_tools_check.py b/tests/rdna2_tools_check.py index 04a7719e6e..ca07e4e498 100644 --- a/tests/rdna2_tools_check.py +++ b/tests/rdna2_tools_check.py @@ -2764,6 +2764,54 @@ def test_vop3_mul_lo_i32_qsad_and_interp_mov_refuse_forms_llvm_rejects(self): plain = disasm_rdna2(self._vop3_word(op, **kwargs))[0]["assembly"] self.assertEqual(disasm_rdna2(self._vop3_word(op, op_sel=0xf, **kwargs))[0]["assembly"], plain) + # Issue #1890: the VINTRP (e32) v_interp_mov_f32 vsrc field is the parameter selector, not a VGPR. + # Assembled from these lines with LLVM 15.0.7 for gfx1013; llvm-objdump 15.0.7 prints the same text. + VINTRP_MOV_E32_VECTORS = ( + ("000006c8", "v_interp_mov_f32_e32 v1, p10, attr0.x"), + ("01050ac8", "v_interp_mov_f32_e32 v2, p20, attr1.y"), + ("020a0ec8", "v_interp_mov_f32_e32 v3, p0, attr2.z"), + ("02ff1ec8", "v_interp_mov_f32_e32 v7, p0, attr63.w"), + ) + + def test_vintrp_interp_mov_selector_golden_vectors(self): + """Issue #1890: the e32 selector prints as p10, p20 or p0, as LLVM prints it.""" + for encoding, assembly in self.VINTRP_MOV_E32_VECTORS: + with self.subTest(assembly=assembly): + record, = disasm_rdna2(bytes.fromhex(encoding)) + self.assertEqual(record["assembly"], assembly) + self.assertEqual(record["opcode"], "v_interp_mov_f32_e32") + self.assertEqual(record["length"], 4) + # p1 and p2 keep their VGPR operand in the same field. + record, = disasm_rdna2(bytes.fromhex("020004c8")) + self.assertEqual(record["assembly"], "v_interp_p1_f32_e32 v1, v2, attr0.x") + + def test_vintrp_interp_mov_selector_llvm_round_trip(self): + """Issue #1890: the e32 vectors assemble to these bytes and disassemble to these strings on LLVM.""" + if not os.environ.get("LLVM_LIB"): + self.skipTest("LLVM_LIB environment variable not set; verified against golden vectors") + resolved_lib = resolve_llvm_lib(os.environ["LLVM_LIB"]) + assembler = resolve_tool("LLVM_MC", ["llvm-mc-15", "llvm-mc"]) + objcopy = resolve_tool("LLVM_OBJCOPY", ["llvm-objcopy-15", "llvm-objcopy"]) + if not assembler or not objcopy: + self.fail(f"VINTRP round trip requires llvm-mc and llvm-objcopy (assembler={assembler}, objcopy={objcopy})") + source = [assembly for _, assembly in self.VINTRP_MOV_E32_VECTORS] + emitted = assemble_with_llvm_mc(source, assembler, objcopy) + self.assertEqual(emitted, bytes.fromhex("".join(encoding for encoding, _ in self.VINTRP_MOV_E32_VECTORS))) + decoded = [record["assembly"] for record in disasm_rdna2(emitted)] + self.assertEqual(decoded, llvm_disassemble(emitted, resolved_lib)) + self.assertEqual(decoded, source) + + def test_vintrp_interp_mov_selector_out_of_range_is_refused(self): + """Issue #1890: a selector LLVM prints as invalid_param_ is a four-byte unsupported record.""" + end = struct.pack("", records[0]["assembly"]) + self.assertEqual(records[1]["opcode"], "s_endpgm") + def test_vop3_mqsad_u32_u8_register_quads_stop_at_v255(self): """A destination or accumulator quad that would run past v255 is flagged, not printed as a register.""" last_quad = disasm_rdna2(self._vop3_word(0x175, vdst=252, src2=0x100 + 252))[0]["assembly"] diff --git a/tests/startup_flat_param_check.py b/tests/startup_flat_param_check.py index 90b01b6faf..7d985166e1 100644 --- a/tests/startup_flat_param_check.py +++ b/tests/startup_flat_param_check.py @@ -16,6 +16,9 @@ and flushed the three denormal patterns to 0. The draw keeps that form as a negative control, and the same form with the words computed at run time as a second control: that one is exact, so the loss is the constant fold, not the flat interface. + +The controls also replace the first read with each parameter selector in both encodings (#1890): p0 must +give the same pixel stage, and p10, p20 and an invalid selector must be refused, not read as p0. """ import re import struct @@ -71,6 +74,22 @@ """ LINEAR_PS_WORDS = bytes.fromhex('0203fcbe' '000008c8' '010009c8' '0f1800f802020202' '000081bf') +# The parameter selector of the first read, in both encodings (#1890). Only p0 is the flat copy; p10 (P1 - P0) +# and p20 (P2 - P0) need per-vertex attribute values, which the FXC path does not have, so they are refused. +# Each line replaces the first read of PS_WORDS; the decoded text must be the source line (LLVM's spelling). +SELECTOR_SOURCE = """\ +v_interp_mov_f32_e32 v0, p10, attr0.x +v_interp_mov_f32_e32 v0, p20, attr0.x +v_interp_mov_f32_e32 v0, p0, attr0.x +v_interp_mov_f32_e64 v0, p10, attr0.x +v_interp_mov_f32_e64 v0, p20, attr0.x +v_interp_mov_f32_e64 v0, p0, attr0.x +""" +SELECTOR_WORDS = tuple(bytes.fromhex(w) for w in ( + '000002c8', '010002c8', '020002c8', '000002d600000000', '000002d600020000', '000002d600040000')) +# VINTRP vsrc 3: llvm-objdump prints invalid_param_3 and llvm-mc accepts no such selector. +INVALID_SELECTOR_WORD = bytes.fromhex('030002c8') + WORDS = (0x00000001, 0x00000002, 0x7fc00001, 0x80000001) EXPECTED = ','.join('%08x' % w for w in WORDS) FLUSHED = '00000000,00000000,7fc00001,00000000' @@ -118,6 +137,8 @@ def controls(): assert 'U(i.fparam' not in ps, ps print('flat: VS o.fparam0 = uint4(v6, v7, v8, v9); PS v0..v3 = i.fparam0.xyzw, no float conversion') + check_selectors(ps) + # An attribute read only interpolated gets no flat copy in either stage. pipe = translate(LINEAR_PS_WORDS) vs, ps = pipe.hlsl_vs(), pipe.hlsl_ps() @@ -132,6 +153,38 @@ def controls(): print('controls: float4 flat copy (before #1259) and its run-time form') +def with_first_read(word): + """PS_WORDS with its first read (v_interp_mov_f32 v0, p0, attr0.x) replaced by `word`.""" + assert PS_WORDS[4:8] == SELECTOR_WORDS[2] + return PS_WORDS[:4] + word + PS_WORDS[8:] + + +def check_selectors(ps): + """p0 is lowered in both encodings; p10, p20 and an invalid selector are refused, never read as p0 (#1890).""" + for line, word in zip(SELECTOR_SOURCE.splitlines(), SELECTOR_WORDS): + (_, size, name, asm), = G.decode(word) + assert (size, asm) == (len(word), line), (word.hex(), size, asm) + selector = line.split(', ')[1] + try: + translated = translate(with_first_read(word)).hlsl_ps() + except G.Unsupported as error: + assert selector != 'p0', (line, error) + assert 'parameter selector %s' % selector in str(error), (line, error) + print('refused: %s (%s)' % (line, error)) + continue + assert selector == 'p0', 'translated %s as %s' % (line, re.findall(r'v0 = i\.[^;]*;', translated)) + assert translated == ps, translated + print('lowered: %s -> v0 = i.fparam0.x;' % line) + (_, _, name, asm), = G.decode(INVALID_SELECTOR_WORD) + assert name.endswith('_unsupported'), asm + try: + translate(with_first_read(INVALID_SELECTOR_WORD)) + except G.Unsupported as error: + print('refused: VINTRP selector 3 (%s)' % error) + else: + raise AssertionError('VINTRP selector 3 was translated') + + def assemble(llvm_mc, source): run = subprocess.run([llvm_mc, '-arch=amdgcn', '-mcpu=gfx1013', '-show-encoding'], input=source, capture_output=True, text=True, check=True) @@ -149,6 +202,10 @@ def check_binary(llvm_mc): built = assemble(llvm_mc, source) if built != words: raise AssertionError('%s fixture: assembled %s, recorded %s' % (name, built.hex(), words.hex())) + for line, words in zip(SELECTOR_SOURCE.splitlines(), SELECTOR_WORDS): + built = assemble(llvm_mc, line + '\n') + if built != words: + raise AssertionError('selector fixture %s: assembled %s, recorded %s' % (line, built.hex(), words.hex())) print('fixture words match llvm-mc') diff --git a/tools/disasm_rdna2.py b/tools/disasm_rdna2.py index 5c3d4c8116..afc87854e0 100644 --- a/tools/disasm_rdna2.py +++ b/tools/disasm_rdna2.py @@ -1029,7 +1029,8 @@ def format_literal32(value: int) -> str: "v_interp_mov_f32_e64": (True, True), } -# The v_interp_mov_f32_e64 parameter selector (src1 field): p10, p20 and p0. +# The v_interp_mov_f32 parameter selector (VINTRP vsrc field, VOP3A src1 field): p10, p20 and p0. +# LLVM prints any other value as invalid_param_ and its assembler accepts none of them. VINTRP_MOV_PARAMS = ("p10", "p20", "p0") @@ -2623,8 +2624,14 @@ def sopc_operand(reg_id: int, wide: bool) -> str: vsrc = w0 & 0xff op_names = {0: "v_interp_p1_f32_e32", 1: "v_interp_p2_f32_e32", 2: "v_interp_mov_f32_e32"} op_name = op_names.get(op, f"v_interp_{op}") - asm = f"{op_name} v{vdst}, v{vsrc}, attr{attr}.{chan}" comment = f"Parameter interpolation stage {op+1} for attribute {attr}.{chan}" + if op == 2: + # The mov form's vsrc field is the parameter selector, not a VGPR. + if vsrc >= len(VINTRP_MOV_PARAMS): + return (4, f"{op_name}_unsupported", f"{op_name} ", + f"v_interp_mov_f32 parameter selector {vsrc} is not p10, p20 or p0") + return (4, op_name, f"{op_name} v{vdst}, {VINTRP_MOV_PARAMS[vsrc]}, attr{attr}.{chan}", comment) + asm = f"{op_name} v{vdst}, v{vsrc}, attr{attr}.{chan}" return (4, op_name, asm, comment) # 9. EXP: 64-bit [31:26] == 0b111110 (0xf8000000) diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 36b2d21943..384495619a 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -1214,6 +1214,10 @@ def op_v_interp_p2_f32(self, ops, mods): self.set_v(reg_range(ops[0])[1], 'U(i.param%d.%s)' % (attr, comp)) def op_v_interp_mov_f32(self, ops, mods): + # Only p0 is the flat copy. p10 (P1 - P0) and p20 (P2 - P0) need the per-vertex attribute + # values, which the FXC path does not have, so they are refused rather than read as p0. + if ops[1] != 'p0': + raise Unsupported('v_interp_mov_f32 parameter selector %s: only p0 is lowered' % ops[1]) attr, comp = self.note_attr(ops[2], 'flat') self.set_v(reg_range(ops[0])[1], 'i.fparam%d.%s' % (attr, comp))