diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index 924cd1483f..6c465262be 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -271,6 +271,7 @@ jobs: tests/startup_gfx2hlsl_interp_f16_check.py \ tests/startup_gfx2hlsl_sgpr_spill_check.py \ tests/startup_gfx2hlsl_mad_check.py \ + tests/startup_gfx2hlsl_neg_check.py \ tests/startup_gfx2hlsl_compute_check.py \ tests/startup_compute_lds_b64_check.py \ tests/startup_compute_lds_b128_check.py \ diff --git a/AGENTS.md b/AGENTS.md index 28e2f1cb54..c2cea0b610 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -165,6 +165,7 @@ python3 -m venv out/pe-env ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_scalar_arity_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_scalar_table_arity_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_mad_check.py +./out/pe-env/bin/python -B tests/startup_gfx2hlsl_neg_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_compute_check.py ./out/pe-env/bin/python -B tests/startup_compute_image_store_check.py ./out/pe-env/bin/python -B tests/startup_compute_image_store_d16_check.py diff --git a/CMakeLists.txt b/CMakeLists.txt index ef2ef512da..2c84933585 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3525,6 +3525,14 @@ if(EXISTS "${INTAKE_PYTHON}") set_tests_properties(startup_gfx2hlsl_fma_mix_dispatch_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77) endforeach() + add_test(NAME startup_gfx2hlsl_neg_controls COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_neg_check.py") + foreach(adapter IN ITEMS warp hardware) + add_test(NAME startup_gfx2hlsl_neg_dispatch_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_neg_check.py" dispatch "${DXC_EXECUTABLE}" + "$" ${adapter}) + set_tests_properties(startup_gfx2hlsl_neg_dispatch_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77) + endforeach() # gfx2hlsl compute stage: entry registers, numthreads and the offen buffer store, dispatched on a plain device (#1412). add_test(NAME startup_gfx2hlsl_compute_controls COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_compute_check.py") diff --git a/docs/experiments.md b/docs/experiments.md index c3604295fd..c2e91ca7d7 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -13940,7 +13940,9 @@ ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_cmp16_' -V python -B tests/startup_gfx2hlsl_cmp16_check.py binary ``` -`tools/startup/gfx2hlsl.py` also refuses the NEG bit of a VOP3 source that is a non-negative inline integer (0 to 64) or a literal. The disassembler prints inline 1 with NEG as `-1`, the same text as inline -1, so the text cannot tell the two encodings apart. The exception is the two data operands of `v_cndmask_b32_e64`, which model NEG as a flip of bit 31. The hardware behavior of NEG on integer sources is not measured. Check: `tests/startup_gfx2hlsl_refusals_check.py`. +`tools/startup/gfx2hlsl.py` refuses VOP3 NEG on a non-negative inline integer (0 to 64) or a literal in integer and unclassified source slots. The disassembler prints inline 1 with NEG as `-1`, the same text as inline -1, so the text cannot tell the two encodings apart. The two data operands of `v_cndmask_b32_e64` model NEG as a flip of bit 31. `tests/startup_gfx2hlsl_refusals_check.py` covers these rules. + +For binary32 arithmetic and compare sources, the translator reads the inline integer (0 to 64) or literal dword from the VOP3 encoding, flips bit 31, and emits `F(0x........u)` using the existing `asfloat` macro. This follows LLVM AMDGPUAsmParser's immediate sign XOR and the RDNA 2 ISA's description of NEG as a float input modifier. The class mask and immediate ABS combined with NEG still refuse. RDNA hardware behavior for these immediate sources has not been measured. The D3D12 readback in `tests/startup_gfx2hlsl_neg_check.py` checks normal FMA and MED3 results, signed zero and normal MUL results, and allows signed flush-to-zero for subnormal MUL results. ## Startup translator: explicit-LOD image sampling diff --git a/tests/startup_gfx2hlsl_neg_check.py b/tests/startup_gfx2hlsl_neg_check.py new file mode 100644 index 0000000000..8ef9e44080 --- /dev/null +++ b/tests/startup_gfx2hlsl_neg_check.py @@ -0,0 +1,114 @@ +"""Translate encoded binary32 VOP3 NEG fixtures and optionally dispatch them on D3D12.""" +import struct +import subprocess +import sys +import tempfile +from pathlib import Path + +from startup_flat_param_check import root_blob +from startup_gfx2hlsl_bit_alu_check import G, POST_WORDS, PRE_WORDS, vs_words +from startup_gfx2hlsl_refusals_check import vop3 + +SIGN = 0x80000000 +ONE = 0x3f800000 +MIN_NORMAL = 0x00800000 +# (encoded selector, optional literal dword) +IMMEDIATES = ((128, None), (129, None), (192, None), (255, 0x40a00000)) +# (encoded opcode, arity, runtime src1, runtime src2, scalar reference) +OPS = { + 'v_fma_f32': (0x14b, 3, ONE, SIGN | ONE, lambda a, b, c: a * b + c), + 'v_mul_f32': (0x108, 2, ONE, 0, lambda a, b, c: a * b), + 'v_med3_f32': (0x157, 3, SIGN | ONE, 0xc0000000, lambda a, b, c: sorted((a, b, c))[1]), +} + + +def float_value(word): + return struct.unpack(' ]') diff --git a/tests/startup_gfx2hlsl_refusals_check.py b/tests/startup_gfx2hlsl_refusals_check.py index f2f22d9662..a596aa4a31 100644 --- a/tests/startup_gfx2hlsl_refusals_check.py +++ b/tests/startup_gfx2hlsl_refusals_check.py @@ -114,30 +114,45 @@ def check_neg_integer_sources(): cndmask = 0x101 accepted = [op for op in range(0x400) if not isinstance(translate_one(vop3(op, sentinels)), str)] check('the sweep reaches the accepted VOP3 opcodes', len(accepted) > 100 and cndmask in accepted) - loose = [] + mismatches = [] for op in accepted: for slot in range(3): - srcs = list(sentinels) - srcs[slot] = inline(1) - plain = translate_one(vop3(op, srcs)) - result = translate_one(vop3(op, srcs, neg=1 << slot)) - modeled = op == cndmask and slot < 2 - if isinstance(plain, str) or modeled: - continue - # Ops with their own, earlier refusal of NEG and ABS (the 16-bit compares, v_sad_*) keep it. - if not (isinstance(result, str) and ('NEG on an integer or literal source %d' % slot in result - or ' with neg/abs' in result)): - loose.append((hex(op), slot, result)) - check('NEG on inline 1 is refused for every accepted opcode and slot except v_cndmask_b32_e64 data sources', - not loose) - if loose: - print(' first misses: %s' % loose[:4]) + for source, literal, expected in ((inline(1), None, '0x80000001'), (255, 5, '0x80000005')): + srcs = list(sentinels) + srcs[slot] = source + code = vop3(op, srcs, literal=literal) + if literal is not None and G.D.decode_instruction(code, 0)[0] != len(code): + # An unused source field does not consume a literal dword. + continue + plain = translate_one(code) + if isinstance(plain, str) or (op == cndmask and slot < 2): + continue + result = translate_one(vop3(op, srcs, neg=1 << slot, literal=literal)) + name = plain[0].split()[0].removesuffix('_e64') + compare = name.startswith(('v_cmp_', 'v_cmpx_')) and name.endswith('_f32') + float_slot = (name in G.VEC_F and slot < G.VEC_F[name][0] + or compare and slot < (1 if '_class_' in name else 2)) + if float_slot: + # Always/never compares consume no source in the emitted predicate. + constant_compare = name.split('_')[-2] in ('t', 'f') + ok = (not isinstance(result, str) and expected in result[0] + and (constant_compare or expected + 'u' in ' '.join(result[1]))) + else: + # Dedicated encoding refusals run before the general NEG check. + ok = (isinstance(result, str) and ('NEG on an integer or literal source %d' % slot in result + or ' with neg/abs' in result)) + if not ok: + mismatches.append((hex(op), slot, expected, result)) + check('VOP3 NEG on inline 1 and a literal follows the source type in every lowerable slot', not mismatches) + if mismatches: + print(' first misses: %s' % mismatches[:4]) def refused(code, slot): text = translate_one(code) return isinstance(text, str) and 'NEG on an integer or literal source %d' % slot in text - iadd, fadd, cmp_lt, add_co, shl64 = names['v_mad_u32_u24'], 0x103, 0x81, names['v_add_co_u32'], names['v_lshlrev_b64'] + iadd, fadd, add_co, shl64 = names['v_mad_u32_u24'], 0x103, names['v_add_co_u32'], names['v_lshlrev_b64'] + cmp_lt = next(op for op, entry in G.D.VOPC_OPS.items() if entry[0] == 'v_cmp_lt_f32_e32') for slot in range(3): srcs = list(sentinels) srcs[slot] = inline(1) @@ -150,10 +165,36 @@ def refused(code, slot): check('literal with NEG refused', refused(vop3(iadd, [255, sentinels[1], sentinels[2]], neg=1, literal=5), 0)) check('literal without NEG translates', not isinstance( translate_one(vop3(iadd, [255, sentinels[1], sentinels[2]], literal=5)), str)) - check('v_add_f32_e64 with an integer inline and NEG refused', refused(vop3(fadd, [inline(1), v(12), 0], neg=1), 0)) - check('compare with inline 1 and NEG refused', refused(vop3(cmp_lt, [inline(1), v(12), 0], sdst=4, neg=1), 0)) - check('compare slot 1 with literal and NEG refused', refused( - vop3(cmp_lt, [v(10), 255, 0], sdst=4, neg=2, literal=7), 1)) + def exact(code, word): + result = translate_one(code) + return not isinstance(result, str) and 'F(0x%08xu)' % word in ' '.join(result[1]) + + check('v_add_f32_e64 inline 0 with NEG is negative zero', + exact(vop3(fadd, [inline(0), v(12), 0], neg=1), 0x80000000)) + check('v_add_f32_e64 inline 64 with NEG stays subnormal', + exact(vop3(fadd, [inline(64), v(12), 0], neg=1), 0x80000040)) + check('v_add_f32_e64 NEG flips the sign of an encoded NaN payload', + exact(vop3(fadd, [255, v(12), 0], neg=1, literal=0xffc12345), 0x7fc12345)) + check('v_add_f32_e64 immediate ABS with NEG is refused', + refused(vop3(fadd, [inline(1), v(12), 0], absolute=1, neg=1), 0)) + word0, word1 = struct.unpack('<2I', vop3(fadd, [v(10), inline(1), 0], neg=2)) + # The rewritten final operand must keep both supported and refused output modifiers. + clamp = translate_one(struct.pack('<2I', word0 | (1 << 15), word1)) + check('rewriting the last source preserves clamp', + not isinstance(clamp, str) and 'saturate(F(v10) + F(0x80000001u))' in ' '.join(clamp[1])) + op_sel = translate_one(struct.pack('<2I', word0 | (1 << 11), word1)) + check('rewriting the last source preserves the op_sel refusal', + isinstance(op_sel, str) and 'modifier op_sel' in op_sel) + check('float compare slot 0 with inline 1 and NEG reads its word', + exact(vop3(cmp_lt, [inline(1), v(12), 0], neg=1), 0x80000001)) + check('float compare slot 1 with literal and NEG reads its word', + exact(vop3(cmp_lt, [v(10), 255, 0], neg=2, literal=7), 0x80000007)) + check('class compare slot 0 with literal and NEG reads its word', + exact(vop3(0x88, [255, inline(1), 0], neg=1, literal=7), 0x80000007)) + check('class compare slot 1 with literal and NEG is refused', + refused(vop3(0x88, [v(10), 255, 0], neg=2, literal=7), 1)) + check('integer compare slot 0 with inline and NEG is refused', + refused(vop3(0x81, [inline(1), v(12), 0], neg=1), 0)) check('v_add_co_u32 (carry out) with inline 1 and NEG refused', refused(vop3(add_co, [inline(1), v(12), 0], sdst=4, neg=1), 0)) check('v_lshlrev_b64 with NEG on the shift inline refused', refused(vop3(shl64, [inline(1), v(12), 0], neg=1), 0)) diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 17126c7ffd..3359788bc7 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -411,19 +411,45 @@ def check_encoding(code, off, n, name, asm): fields[i + 1] = operand[:len(operand) - len(operand.lstrip())] + '0x%08x' % (bits ^ 0x80000000) asm = ','.join(fields) if n >= 8 and struct.unpack_from('> 26 == 0b110101: - # VOP3 NEG (w1[31:29], bit 29 + i for source i) on a non-negative inline integer (128..192) or a literal (255). - # The decoder prints inline 1 with NEG as `-1`, the text of the plain inline -1 (193), so both would translate to - # 0xffffffff, and a literal 5 would become 0xfffffffb. Only v_cndmask_b32_e64 models NEG on its two data - # sources (above, as the sign bit flipped). Inline floats, registers and negative inlines are not affected. - # ABS is not tested: on the VOP3b forms w0[10:8] is SDST, not a modifier. - word1, = struct.unpack_from('> (9 * i)) & 0x1ff - if 128 <= source <= 192 or source == 255: + if not (128 <= source <= 192 or source == 255): + continue + # The selected float forms are VOP3A, where w0[10:8] holds ABS. Keep the + # refusal for NEG combined with ABS on this source. + if i >= float_sources or word0 & (1 << (8 + i)): raise Unsupported('%s with NEG on an integer or literal source %d: the printed sign would read as inline -N' % (name, i)) + if source == 255: + if n != 12 or off + n > len(code): + raise Unsupported('%s missing literal dword' % name) + bits, = struct.unpack_from('