diff --git a/CMakeLists.txt b/CMakeLists.txt index 89c2cd4eb1..695875318a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1686,6 +1686,9 @@ if(EXISTS "${INTAKE_PYTHON}") # SDWA forms of the float-result conversions (#1727). add_test(NAME startup_gfx2hlsl_sdwa_cvt COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_sdwa_cvt_check.py") + # A negated source on the vector select flips that source's sign bit (#1709). + add_test(NAME startup_gfx2hlsl_cndmask_neg COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_cndmask_neg_check.py") # Output modifiers on float-result conversions (#1705). add_test(NAME startup_gfx2hlsl_vcvt_omod COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_vcvt_omod_check.py") diff --git a/docs/experiments.md b/docs/experiments.md index c1e88ddba9..0601f90f80 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -5737,6 +5737,16 @@ 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. 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`. + **Output modifiers on float-result conversions.** The float-result conversions (`v_cvt_f32_i32`, `v_cvt_f32_u32`, `v_cvt_f32_ubyte0..3`, `v_cvt_f32_f16`) sat in the uint-result table, whose dispatch refused every modifier, so `v_cvt_f32_i32_e64 v1, v1 mul:2` did not translate - 5 of the 29 refused diff --git a/tests/startup_gfx2hlsl_cndmask_neg_check.py b/tests/startup_gfx2hlsl_cndmask_neg_check.py new file mode 100644 index 0000000000..642205d91d --- /dev/null +++ b/tests/startup_gfx2hlsl_cndmask_neg_check.py @@ -0,0 +1,140 @@ +"""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, clamp=0, omod=0): + """Encode a VOP2 instruction in its VOP3A (e64) form.""" + 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('> (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) @@ -217,6 +227,15 @@ def u(self, tok): except ValueError: raise Unsupported('operand %r' % tok) + def u_src(self, tok): + """uint expression for an integer-context source that the disassembler may print negated.""" + # In an integer instruction the `-` modifier is the source's sign bit flipped: the same bit + # pattern a float negation produces. Only handlers that model it ask for this; other integer + # 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:]) + return self.u(tok) + def f(self, tok): """float expression, with the inline neg/abs operand modifiers.""" neg = False @@ -883,8 +902,13 @@ def op_v_mov_b32(self, ops, mods): self.set_v(reg_range(ops[0])[1], self.u(ops[1])) def op_v_cndmask_b32(self, ops, mods): + # The select takes no output modifier: a clamp or mul on it would change the value, so a + # modifier that reaches here is refused rather than dropped from the emitted text. + if mods: + raise Unsupported('v_cndmask_b32 with %s' % mods) mask = self.u(ops[3]) if len(ops) > 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')