From 3d0618a2dc757fa07b3b6eb6f1c3c833a8a13b2b Mon Sep 17 00:00:00 2001 From: triggered02 <153171514+triggered02@users.noreply.github.com> Date: Tue, 24 Nov 2026 23:04:48 +0530 Subject: [PATCH 1/4] startup: flip the sign bit for a negated source on the vector select Fixes #1709. In an integer instruction the '-' source modifier is that source's sign bit flipped; the select passed the printed operand to the uint helper, so v_cndmask_b32_e64 v31, v30, -v30 did not translate - 2 of the 29 refused shaders in the loading-screen cache (#870). The select now reads both sources through a helper that flips the sign bit for a '-' operand and is identical otherwise, and the helper is used only where the modifier is modelled: another integer instruction carrying it still refuses. Test builds VOP2-as-VOP3 encodings from the decoder layout, decodes, then translates; both presets pass; fail-first reverts the call and the test fails. Census A/B: 'operand -v' 2 -> 0. --- CMakeLists.txt | 3 + docs/experiments.md | 9 ++ tests/startup_gfx2hlsl_cndmask_neg_check.py | 112 ++++++++++++++++++++ tools/startup/gfx2hlsl.py | 12 ++- 4 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 tests/startup_gfx2hlsl_cndmask_neg_check.py diff --git a/CMakeLists.txt b/CMakeLists.txt index b6d0c0a25d..aeb6cfb804 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1635,6 +1635,9 @@ if(EXISTS "${INTAKE_PYTHON}") # lowering of the same selections. add_test(NAME startup_gfx2hlsl_sdwa COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_sdwa_check.py") + # A negated source on the vector select flips that source's sign bit (#1706). + add_test(NAME startup_gfx2hlsl_cndmask_neg COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_cndmask_neg_check.py") # gfx2hlsl run-time sampler heap indexing (#1143): translation controls, then a desktop draw with # DXC ps_6_0 and, when the Windows SDK's FXC is found, the ps_5_1 the startup tools compile with. find_program(STARTUP_FXC fxc HINTS "${DXC_BIN_DIR}") diff --git a/docs/experiments.md b/docs/experiments.md index e8cc8ca863..5cd7ec0df0 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -5649,6 +5649,15 @@ control mismatching. The D3D12 debug layer was unavailable. [Leonida RDNA2 measurements](https://github.com/ItsNotRexed/Leonida/pull/1685) ยท [AMD RDNA 2 instruction set](https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/instruction-set-architectures/rdna2-shader-instruction-set-architecture.pdf) +**Negated sources on the vector select.** A VOP3 `v_cndmask_b32` can carry the `-` source modifier, +which in an integer instruction is that source's sign bit flipped. The select handler passed the printed +operand to the uint helper, so `v_cndmask_b32_e64 v31, v30, -v30` was refused - 2 of the 29 refused +shaders in the first main-game loading-screen cache (#870, issue #1709). The select now reads both sources +through a helper that flips the sign bit for a `-` operand and emits the same text as before when there is +none. The helper is used only where the translation models the modifier: another integer instruction +carrying the same `-` operand is still refused rather than silently flipping a bit. Run +`ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_cndmask_neg$' -V`. + ## Pixel color without a texture sample For an unsampled pixel program, a matched P1/P2 interpolation in the entry mask can supply attr0.x/y/z/w from a four-component TEXCOORD0. The profile binds entry v0/v1 to the barycentric pair and the observed m0 source to the interpolation base. Each completed P2 produces a distinct value, including when the destination overwrites v0. The test vertex shader varies all four channels across a full-screen triangle. CPU references check all four output words at 1,024 pixel centers; an incorrect channel at an interior pixel must fail. Run `ctest --preset dev --no-tests=error -R '^pixel_aot_attribute_'`. diff --git a/tests/startup_gfx2hlsl_cndmask_neg_check.py b/tests/startup_gfx2hlsl_cndmask_neg_check.py new file mode 100644 index 0000000000..34df2634c4 --- /dev/null +++ b/tests/startup_gfx2hlsl_cndmask_neg_check.py @@ -0,0 +1,112 @@ +"""Negated sources on the vector select in tools/startup/gfx2hlsl.py (issue #1709). + +usage: startup_gfx2hlsl_cndmask_neg_check.py + +A VOP3 `v_cndmask_b32` can carry the `-` source modifier, which in an integer instruction is the source's +sign bit flipped. The select handler passed the printed operand straight to the uint helper, so +`v_cndmask_b32_e64 v31, v30, -v30` was refused with `Unsupported: operand '-v30'` - 2 of the 29 refused +shaders in the first main-game loading-screen cache (#870). + +The instruction bytes are **constructed here** from the field layout the checked-in decoder reads +(`tools/disasm_rdna2.py`, VOP3A branch): + w0: [31:26] = 0b110101, [25:16] opcode (VOP2 e64 = 0x100 + VOP2 opcode), [7:0] vdst + w1: [8:0] src0, [17:9] src1, [28:27] omod, [31:29] neg +""" +import os +import struct +import sys + +here = os.path.dirname(os.path.abspath(__file__)) +sys.path.insert(0, os.path.join(here, '..', 'tools')) +sys.path.insert(0, os.path.join(here, '..', 'tools', 'startup')) +import gfx2hlsl as G # noqa: E402 + +failures = [] + + +def check(label, condition, detail=''): + print(f"{label}: {'ok' if condition else 'MISMATCH'}") + if not condition: + failures.append(label) + if detail: + print(' ' + detail) + + +VOP2_E64_BASE = 0x100 +CNDMASK = 0x01 +AND_B32 = 0x1b +V30 = 256 + 30 # VGPR operands are encoded from 256 upwards + + +def encode_vop2_e64(op, vdst, src0, src1, *, neg0=0, neg1=0): + """Encode a VOP2 instruction in its VOP3A (e64) form.""" + w0 = (0b110101 << 26) | ((VOP2_E64_BASE + op) << 16) | (vdst & 0xff) + w1 = ((src0 & 0x1ff) | ((src1 & 0x1ff) << 9) | ((neg0 & 1) << 29) | ((neg1 & 1) << 30)) + return struct.pack(' 3 else 'vcc' - self.set_v(reg_range(ops[0])[1], '(%s & 1) ? %s : %s' % (mask, self.u(ops[2]), self.u(ops[1]))) + self.set_v(reg_range(ops[0])[1], + '(%s & 1) ? %s : %s' % (mask, self.u_src(ops[2]), self.u_src(ops[1]))) def op_v_interp_p1_f32(self, ops, mods): self.note_attr(ops[2], 'linear') From b019275d007fd31ef5621c2061a968fbf849a0ce Mon Sep 17 00:00:00 2001 From: triggered02 <153171514+triggered02@users.noreply.github.com> Date: Tue, 24 Nov 2026 23:12:59 +0530 Subject: [PATCH 2/4] startup: refuse an ambiguous negated integer constant on the select Review on #1710 (P2/P3): the disassembler prints both 'src 193' (literal -1, 0xffffffff) and 'src 129' with NEG (inline 1 with its sign bit flipped, 0x80000001) as '-1', so the printed operand does not identify the value. u_src now refuses that spelling instead of picking one reading, and the test pins it with both encodings plus a check that they decode identically; a double negation '--1' (src 193 with NEG) stays unambiguous and translates. Dropping the guard fails the test. --- tests/startup_gfx2hlsl_cndmask_neg_check.py | 17 +++++++++++++++++ tools/startup/gfx2hlsl.py | 4 ++++ 2 files changed, 21 insertions(+) diff --git a/tests/startup_gfx2hlsl_cndmask_neg_check.py b/tests/startup_gfx2hlsl_cndmask_neg_check.py index 34df2634c4..d556ce5b71 100644 --- a/tests/startup_gfx2hlsl_cndmask_neg_check.py +++ b/tests/startup_gfx2hlsl_cndmask_neg_check.py @@ -104,6 +104,23 @@ def refused(code): check('a negation on another integer op stays refused', refused(encode_vop2_e64(AND_B32, 1, V30, V30, neg1=1))) +# 5. A negated integer *constant* prints the same as a negative literal: `src 129` with NEG is the inline +# constant 1 with its sign bit flipped (0x80000001), while `src 193` is the literal -1 (0xffffffff), and +# the disassembler prints both as `-1`. The printed operand does not identify the value, so the select +# refuses it instead of picking one reading. +check('a negated inline constant is refused as ambiguous', + refused(encode_vop2_e64(CNDMASK, 31, 129, V30, neg0=1))) +check('a negative literal source is refused as ambiguous too', + refused(encode_vop2_e64(CNDMASK, 31, 193, V30))) +check('both spellings decode identically', + G.decode(encode_vop2_e64(CNDMASK, 31, 129, V30, neg0=1))[0][3] == + G.decode(encode_vop2_e64(CNDMASK, 31, 193, V30))[0][3] == + 'v_cndmask_b32_e64 v31, -1, v30, s0') +# A double negation (`--1`, src 193 with NEG) is unambiguous and still translates. +asm, emitted = translate(encode_vop2_e64(CNDMASK, 31, 193, V30, neg0=1)) +check('a double negation still translates', + 'v31 = (s0 & 1) ? v30 : (0xffffffffu ^ 0x80000000u);' in emitted, emitted) + if failures: print('FAIL') for text in failures: diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index df3e8d29e9..0426af49da 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -224,6 +224,10 @@ def u_src(self, tok): # sources keep using u(), so an unexpected negation stays a refusal rather than a silent flip. if tok.startswith('-') and not re.match(r'-[\d.]', tok): return '(%s ^ 0x80000000u)' % self.u(tok[1:]) + if tok.startswith('-') and re.match(r'-[\d.]', tok): + # The disassembler prints both `src 193` (the literal -1) and `src 129` with NEG (inline 1 with + # its sign bit flipped, 0x80000001) as `-1`, so the printed operand does not identify the value. + raise Unsupported('negated integer constant %r is ambiguous' % tok) return self.u(tok) def f(self, tok): From dfc73a1be49de78d68a87232df7e8879556147f4 Mon Sep 17 00:00:00 2001 From: Royoo <3230853833@qq.com> Date: Wed, 25 Nov 2026 01:50:06 +0800 Subject: [PATCH 3/4] Preserve unmodified negative select immediates --- docs/experiments.md | 5 ++-- tests/startup_gfx2hlsl_cndmask_neg_check.py | 29 +++++++++------------ tools/startup/gfx2hlsl.py | 14 +++++++--- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/docs/experiments.md b/docs/experiments.md index 5cd7ec0df0..b9b8e17a7e 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -5654,8 +5654,9 @@ which in an integer instruction is that source's sign bit flipped. The select ha operand to the uint helper, so `v_cndmask_b32_e64 v31, v30, -v30` was refused - 2 of the 29 refused shaders in the first main-game loading-screen cache (#870, issue #1709). The select now reads both sources through a helper that flips the sign bit for a `-` operand and emits the same text as before when there is -none. The helper is used only where the translation models the modifier: another integer instruction -carrying the same `-` operand is still refused rather than silently flipping a bit. Run +none. Plain negative constants retain the existing bit pattern. NEG on inline or literal constants is +refused from the encoded source fields, because plain inline -1 and inline 1 with NEG both disassemble +as `-1`. Another integer instruction carrying the same register modifier still refuses it. Run `ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_cndmask_neg$' -V`. ## Pixel color without a texture sample diff --git a/tests/startup_gfx2hlsl_cndmask_neg_check.py b/tests/startup_gfx2hlsl_cndmask_neg_check.py index d556ce5b71..090f4ab82d 100644 --- a/tests/startup_gfx2hlsl_cndmask_neg_check.py +++ b/tests/startup_gfx2hlsl_cndmask_neg_check.py @@ -104,22 +104,19 @@ def refused(code): check('a negation on another integer op stays refused', refused(encode_vop2_e64(AND_B32, 1, V30, V30, neg1=1))) -# 5. A negated integer *constant* prints the same as a negative literal: `src 129` with NEG is the inline -# constant 1 with its sign bit flipped (0x80000001), while `src 193` is the literal -1 (0xffffffff), and -# the disassembler prints both as `-1`. The printed operand does not identify the value, so the select -# refuses it instead of picking one reading. -check('a negated inline constant is refused as ambiguous', - refused(encode_vop2_e64(CNDMASK, 31, 129, V30, neg0=1))) -check('a negative literal source is refused as ambiguous too', - refused(encode_vop2_e64(CNDMASK, 31, 193, V30))) -check('both spellings decode identically', - G.decode(encode_vop2_e64(CNDMASK, 31, 129, V30, neg0=1))[0][3] == - G.decode(encode_vop2_e64(CNDMASK, 31, 193, V30))[0][3] == - 'v_cndmask_b32_e64 v31, -1, v30, s0') -# A double negation (`--1`, src 193 with NEG) is unambiguous and still translates. -asm, emitted = translate(encode_vop2_e64(CNDMASK, 31, 193, V30, neg0=1)) -check('a double negation still translates', - 'v31 = (s0 & 1) ? v30 : (0xffffffffu ^ 0x80000000u);' in emitted, emitted) +# Plain negative inline constants keep their bit patterns even when the printed spelling is identical. +asm, emitted = translate(encode_vop2_e64(CNDMASK, 31, 193, V30)) +check('plain inline -1 still translates', + asm == ['v_cndmask_b32_e64 v31, -1, v30, s0'] and + 'v31 = (s0 & 1) ? v30 : 0xffffffffu;' in emitted, emitted) +asm, emitted = translate(encode_vop2_e64(CNDMASK, 31, 243, V30)) +check('plain inline -1.0 still translates', + asm == ['v_cndmask_b32_e64 v31, -1.0, v30, s0'] and + 'v31 = (s0 & 1) ? v30 : 0xbf800000u;' in emitted, emitted) +check('NEG on inline integer remains refused', refused(encode_vop2_e64(CNDMASK, 31, 129, V30, neg0=1))) +check('NEG on inline float remains refused', refused(encode_vop2_e64(CNDMASK, 31, V30, 242, neg1=1))) +check('NEG on literal remains refused', refused(encode_vop2_e64(CNDMASK, 31, V30, 255, neg1=1) + + struct.pack('> (9 * i)) & 0x1ff + if 128 <= source <= 208 or 240 <= source <= 248 or source == 255: + raise Unsupported('v_cndmask_b32_e64 NEG on an immediate') out.append((off, n, name, asm)) off += n last = max((i for i, x in enumerate(out) if x[2] == 's_endpgm'), default=len(out) - 1) @@ -224,10 +234,6 @@ def u_src(self, tok): # sources keep using u(), so an unexpected negation stays a refusal rather than a silent flip. if tok.startswith('-') and not re.match(r'-[\d.]', tok): return '(%s ^ 0x80000000u)' % self.u(tok[1:]) - if tok.startswith('-') and re.match(r'-[\d.]', tok): - # The disassembler prints both `src 193` (the literal -1) and `src 129` with NEG (inline 1 with - # its sign bit flipped, 0x80000001) as `-1`, so the printed operand does not identify the value. - raise Unsupported('negated integer constant %r is ambiguous' % tok) return self.u(tok) def f(self, tok): From 9bed7ffc99365ddba77354c5c670448159d54d64 Mon Sep 17 00:00:00 2001 From: triggered02 <153171514+triggered02@users.noreply.github.com> Date: Tue, 24 Nov 2026 23:35:15 +0530 Subject: [PATCH 4/4] startup: refuse output modifiers on the vector select and cover the immediate range edges Review nits on #1710. (1) op_v_cndmask_b32 now refuses clamp/mul:2 instead of dropping them from the emitted text: the select takes no output modifier, so a modifier that reaches the handler was silently changing nothing. (2) The test exercises every edge of the refused immediate ranges (128, 208, 240, 248) and a plain 0.5 that must still translate, so moving a range boundary by one no longer survives mutation. (3) The synthetic v_and_b32_e64 negation encoding is annotated as not assembler-producible, with the property it actually checks. Fail-first: removing the modifier refusal fails 'a mul:2 on the select is refused'; moving the 240 edge to 241 fails 'NEG on the immediate at 240 is refused'. Python-level ctest subset green; the dev tree has no built binaries here, so the executable-backed tests were not run. --- tests/startup_gfx2hlsl_cndmask_neg_check.py | 22 +++++++++++++++++---- tools/startup/gfx2hlsl.py | 4 ++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/tests/startup_gfx2hlsl_cndmask_neg_check.py b/tests/startup_gfx2hlsl_cndmask_neg_check.py index 090f4ab82d..642205d91d 100644 --- a/tests/startup_gfx2hlsl_cndmask_neg_check.py +++ b/tests/startup_gfx2hlsl_cndmask_neg_check.py @@ -38,10 +38,10 @@ def check(label, condition, detail=''): V30 = 256 + 30 # VGPR operands are encoded from 256 upwards -def encode_vop2_e64(op, vdst, src0, src1, *, neg0=0, neg1=0): +def encode_vop2_e64(op, vdst, src0, src1, *, neg0=0, neg1=0, clamp=0, omod=0): """Encode a VOP2 instruction in its VOP3A (e64) form.""" - w0 = (0b110101 << 26) | ((VOP2_E64_BASE + op) << 16) | (vdst & 0xff) - w1 = ((src0 & 0x1ff) | ((src1 & 0x1ff) << 9) | ((neg0 & 1) << 29) | ((neg1 & 1) << 30)) + w0 = ((0b110101 << 26) | ((VOP2_E64_BASE + op) << 16) | ((clamp & 1) << 15) | (vdst & 0xff)) + w1 = ((src0 & 0x1ff) | ((src1 & 0x1ff) << 9) | ((omod & 0x3) << 27) | ((neg0 & 1) << 29) | ((neg1 & 1) << 30)) return struct.pack(' 3 else 'vcc' self.set_v(reg_range(ops[0])[1], '(%s & 1) ? %s : %s' % (mask, self.u_src(ops[2]), self.u_src(ops[1])))