Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
74 changes: 64 additions & 10 deletions tests/startup_gfx2hlsl_cndmask_neg_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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('<I', 0x12345678)))
# 6. Every edge of the refused immediate ranges, plus the constants just inside them: a mutation moving a
# range boundary by one has to fail one of these.
for edge in (128, 208, 240, 248):
check('NEG on the immediate at %d is refused' % edge,
refused(encode_vop2_e64(CNDMASK, 31, edge, V30, neg0=1)))
asm, emitted = translate(encode_vop2_e64(CNDMASK, 31, 240, V30))
check('plain 0.5 still translates',
asm == ['v_cndmask_b32_e64 v31, 0.5, v30, s0'] and '0x3f000000u' in emitted, emitted)
literal = struct.pack('<I', 0x12345678)
asm, emitted = translate(encode_vop2_e64(CNDMASK, 31, V30, 255) + literal)
check('plain literal still translates',
asm == ['v_cndmask_b32_e64 v31, v30, 0x12345678, s0'] and
emitted == 'v31 = (s0 & 1) ? 0x12345678u : v30;', emitted)

# 6. Check the exact sign-flipped words at the integer encoding boundaries and for every inline float.
for source, expected in ((128, '80000000'), (129, '80000001'), (192, '80000040'),
(193, '7fffffff'), (208, '7ffffff0'), (240, 'bf000000'),
(241, '3f000000'), (242, 'bf800000'), (243, '3f800000'),
(244, 'c0000000'), (245, '40000000'), (246, 'c0800000'),
(247, '40800000'), (248, 'be22f983')):
asm, emitted = translate(encode_vop2_e64(CNDMASK, 31, source, V30, neg0=1))
check('NEG on src0 encoding %d' % source,
asm == [f'v_cndmask_b32_e64 v31, 0x{expected}, v30, s0'] and
emitted == f'v31 = (s0 & 1) ? v30 : 0x{expected}u;', emitted)

neg_literal = encode_vop2_e64(CNDMASK, 31, V30, 255, neg1=1) + literal
asm, emitted = translate(neg_literal)
check('NEG on a literal in src1',
asm == ['v_cndmask_b32_e64 v31, v30, 0x92345678, s0'] and
emitted == 'v31 = (s0 & 1) ? 0x92345678u : v30;', emitted)
decoded = G.decode(neg_literal + encode_vop2_e64(CNDMASK, 31, V30, V30))
check('the next instruction starts after the literal dword',
[(offset, size) for offset, size, _, _ in decoded] == [(0, 12), (12, 8)], repr(decoded))

both_neg = encode_vop2_e64(CNDMASK, 31, 129, 240, neg0=1, neg1=1)
asm, emitted = translate(both_neg)
check('NEG on two immediates in the same instruction',
asm == ['v_cndmask_b32_e64 v31, 0x80000001, 0xbf000000, s0'] and
emitted == 'v31 = (s0 & 1) ? 0xbf000000u : 0x80000001u;', emitted)

# 7. The select takes no output modifier; one that reaches it is refused rather than dropped from the text.
check('a clamp on the select is refused', refused(encode_vop2_e64(CNDMASK, 31, V30, V30, clamp=1)))
Expand All @@ -145,6 +168,37 @@ def refused(code):
refused(encode_vop2_e64(CNDMASK, 31, V30, V30, abs0=1, abs1=1)))
check('a negated mask is refused', refused(encode_vop2_e64(CNDMASK, 31, V30, V30, neg_mask=1)))

check('ABS on an inline immediate is refused',
refused(encode_vop2_e64(CNDMASK, 31, 193, V30, abs0=1)))
check('ABS with NEG on an inline immediate is refused',
refused(encode_vop2_e64(CNDMASK, 31, V30, 240, abs1=1, neg1=1)))
check('ABS with NEG on a literal is refused',
refused(encode_vop2_e64(CNDMASK, 31, V30, 255, abs1=1, neg1=1) + literal))


# 9. Restore the old encoded-source refusal in-process and require the previously accepted cases to fail.
decode = G.decode


def decode_refusing_neg_immediates(code):
decoded = decode(code)
for offset, _size, name, _asm in decoded:
if name == 'v_cndmask_b32_e64':
word1, = struct.unpack_from('<I', code, offset + 4)
for i in range(2):
source = (word1 >> (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')
Expand Down
34 changes: 29 additions & 5 deletions tools/startup/gfx2hlsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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('<I', code, off + 4)
word0, word1 = struct.unpack_from('<2I', code, off)
for i in range(2):
source = (word1 >> (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('<I', code, off + 8)
# Preserve the other operand spellings and any modifiers on the final operand.
fields = asm.split(',', 3)
if len(fields) != 4:
raise Unsupported('malformed v_cndmask_b32_e64 operands')
operand = fields[i + 1]
fields[i + 1] = operand[:len(operand) - len(operand.lstrip())] + '0x%08x' % (bits ^ 0x80000000)
asm = ','.join(fields)
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)
Expand Down
Loading