From 3ef8e9bdfdeb378c981f867ec1d1cebb63ea73e3 Mon Sep 17 00:00:00 2001 From: Leyukaka <45659323+Leyukaka@users.noreply.github.com> Date: Tue, 24 Nov 2026 22:29:01 +0100 Subject: [PATCH] startup: lower NEG on an immediate source of v_cndmask_b32_e64 from its encoding AI-assisted. --- docs/experiments.md | 11 +-- tests/startup_gfx2hlsl_cndmask_neg_check.py | 74 ++++++++++++++++++--- tools/startup/gfx2hlsl.py | 34 ++++++++-- 3 files changed, 100 insertions(+), 19 deletions(-) diff --git a/docs/experiments.md b/docs/experiments.md index be66c1b5fa..bc4c6b314d 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -5963,10 +5963,13 @@ control mismatching. The D3D12 debug layer was unavailable. 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 +through a helper that flips bit 31 of a negated register. For a negated inline integer, inline binary32 +constant or literal dword on either data source of `v_cndmask_b32_e64`, the decoder gets the 32-bit +word from the encoding and rewrites that operand as a hex literal with bit 31 flipped. The literal +dword still counts toward the 12-byte instruction. Plain inline -1 and inline 1 with NEG both +disassemble as `-1`, and they emit `0xffffffffu` and `0x80000001u`, respectively. Inline 0.5 with NEG +emits `0xbf000000u`. ABS on an immediate and a missing literal dword are refused. 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`, diff --git a/tests/startup_gfx2hlsl_cndmask_neg_check.py b/tests/startup_gfx2hlsl_cndmask_neg_check.py index f7c1daaae7..60ca4b7267 100644 --- a/tests/startup_gfx2hlsl_cndmask_neg_check.py +++ b/tests/startup_gfx2hlsl_cndmask_neg_check.py @@ -10,6 +10,7 @@ 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 + [9:8] abs for src1 and src0 w1: [8:0] src0, [17:9] src1, [28:27] omod, [31:29] neg """ import os @@ -108,7 +109,7 @@ def refused(code): check('a negation on another integer op stays refused', refused(encode_vop2_e64(AND_B32, 1, V30, V30, neg1=1))) -# Plain negative inline constants keep their bit patterns even when the printed spelling is identical. +# 5. Plain immediates keep their bit patterns even when their printed spelling is ambiguous. 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 @@ -117,18 +118,40 @@ def refused(code): 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 word1 & (1 << (29 + i)) and (128 <= source <= 208 or 240 <= source <= 248 or source == 255): + raise G.Unsupported('v_cndmask_b32_e64 NEG on an immediate') + return decoded + + +G.decode = decode_refusing_neg_immediates +try: + check('restoring the old refusal rejects both negated inline sources', refused(both_neg)) + check('restoring the old refusal rejects the negated literal', refused(neg_literal)) +finally: + G.decode = decode + if failures: print('FAIL') diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 36b2d21943..777c0404ea 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -178,6 +178,12 @@ def stream_fetch(vs_container, layout_text): return fetch +# Binary32 words for inline source encodings 240 through 248, in encoding order. +CNDMASK_INLINE_FLOAT_BITS = (0x3f000000, 0xbf000000, 0x3f800000, 0xbf800000, + 0x40000000, 0xc0000000, 0x40800000, 0xc0800000, + 0x3e22f983) # 1/(2*pi) + + def decode(code): out, off = [], 0 while off + 4 <= len(code): @@ -194,14 +200,32 @@ def decode(code): raise Unsupported('image_load_mip a16') if name == 'v_cndmask_b32_e64': # The printed -1 is ambiguous: it can be plain inline -1 or inline 1 with NEG. - # Preserve plain immediates and refuse NEG on immediate data operands by encoding. - word1, = struct.unpack_from('> (9 * i)) & 0x1ff + if not (128 <= source <= 208 or 240 <= source <= 248 or source == 255): + continue + if word0 & (1 << (8 + i)): + raise Unsupported('v_cndmask_b32_e64 ABS on an immediate') if not word1 & (1 << (29 + i)): continue - source = (word1 >> (9 * i)) & 0x1ff - if 128 <= source <= 208 or 240 <= source <= 248 or source == 255: - raise Unsupported('v_cndmask_b32_e64 NEG on an immediate') + if source <= 192: + bits = source - 128 + elif source <= 208: + bits = (192 - source) & 0xffffffff + elif source <= 248: + bits = CNDMASK_INLINE_FLOAT_BITS[source - 240] + else: + if n != 12 or off + n > len(code): + raise Unsupported('v_cndmask_b32_e64 missing literal dword') + bits, = struct.unpack_from('