diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index 40c77d58b..7ff0fe6e0 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -84,6 +84,8 @@ jobs: (["tests/startup_gfx2hlsl_shift64_check.py", "binary", "llvm-mc-15"], {}), (["tests/startup_gfx2hlsl_vopc_sdwa_check.py", "binary", "llvm-mc-15"], {}), (["tests/startup_gfx2hlsl_vopc_sdwa_check.py", "decode", "libLLVM-15.so.1"], {}), + (["tests/startup_gfx2hlsl_sop2_complement_check.py", "binary", "llvm-mc-15"], {}), + (["tests/startup_gfx2hlsl_sop2_complement_check.py", "decode", "libLLVM-15.so.1"], {}), (["tests/startup_inline_sampler_check.py", "binary", "llvm-mc-15"], {}), (["tests/startup_smem_address_check.py", "binary", "llvm-mc-15"], {}), (["tests/compute_entry_layout_check.py", "binary", "llvm-mc-15"], {}), @@ -197,6 +199,7 @@ jobs: tests/startup_gfx2hlsl_mad_check.py \ tests/startup_gfx2hlsl_shift64_check.py \ tests/startup_gfx2hlsl_vopc_sdwa_check.py \ + tests/startup_gfx2hlsl_sop2_complement_check.py \ tests/startup_inline_sampler_check.py \ tests/startup_buffer_address_check.py \ tests/startup_smem_address_check.py \ diff --git a/AGENTS.md b/AGENTS.md index 8e8dc9b0d..66e72acb3 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -155,6 +155,7 @@ python3 -m venv out/pe-env ./out/pe-env/bin/python -B tests/startup_translator_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_smem_offset_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_scalar_m0_check.py +./out/pe-env/bin/python -B tests/startup_gfx2hlsl_sop2_complement_check.py ./out/pe-env/bin/python -B tests/startup_flat_param_check.py ./out/pe-env/bin/python -B tests/startup_inline_sampler_check.py ./out/pe-env/bin/python -B tests/startup_buffer_address_check.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 26922d55c..b0b9bff11 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2023,6 +2023,9 @@ if(EXISTS "${INTAKE_PYTHON}") # A typed buffer load of BUF_FMT_16_16_16_16_FLOAT reads four halves from one 8-byte element (#1695). add_test(NAME startup_gfx2hlsl_tbuffer_16f COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_tbuffer_16f_check.py") + # 32-bit scalar complement logic: s_nand_b32, s_nor_b32 and s_xnor_b32 against an integer reference (#1919). + add_test(NAME startup_gfx2hlsl_sop2_complement COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_sop2_complement_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 c87096b51..894b6bbb1 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -6018,6 +6018,19 @@ the handler writes back in one statement. VGPR pairs, SGPR pairs and the integer sources; the low word is always shifted unsigned and only the arithmetic form signs the high word. Run `ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_b64_shift$' -V`. +**The 32-bit scalar complement logic operations.** The scalar table had `s_nand_b64`, `s_nor_b64` and +`s_xnor_b64` and the 32-bit `s_andn2_b32` and `s_orn2_b32`, but not `s_nand_b32`, `s_nor_b32` or `s_xnor_b32` +(#870, issue #1919). They now lower as `~(S0 & S1)`, `~(S0 | S1)` and `~(S0 ^ S1)` on 32 bits, the text +`pixel_aot` uses, and set SCC when the result is nonzero like the other logic operations. The check decodes +SGPR, in-place, inline-constant, literal and m0-destination forms and evaluates the emitted lines against an +integer reference over 0, all ones, alternating bits, every single bit and mixed words. Lane-mask forms (a +vcc_lo/exec_lo operand or destination, or an SGPR holding a compare mask) are refused: guest SCC reads the whole +32-bit mask, which the per-lane projection cannot show. A table +entry without the complement or without the SCC write must fail, and SOP2 operations with no lowering stay +refused by name. Run `ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_sop2_complement$' -V`; with +LLVM 15, `tests/startup_gfx2hlsl_sop2_complement_check.py binary ` and `decode ` round-trip +the same words. + ## 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_sop2_complement_check.py b/tests/startup_gfx2hlsl_sop2_complement_check.py new file mode 100644 index 000000000..03d9176a1 --- /dev/null +++ b/tests/startup_gfx2hlsl_sop2_complement_check.py @@ -0,0 +1,318 @@ +"""32-bit scalar complement logic operations in tools/startup/gfx2hlsl.py: s_nand_b32, s_nor_b32, s_xnor_b32 (#1919). + + (no arguments) translation controls, pure Python + binary assemble the decoded text of every form with llvm-mc and compare with the words built here + decode disassemble the same words with LLVM's C API and compare with tools/disasm_rdna2.py + +The instruction words are built here from the SOP2 layout that tools/disasm_rdna2.py reads: [31:30] = 0b10, +[29:23] opcode, [22:16] sdst, [15:8] ssrc1, [7:0] ssrc0, and one literal dword after the word when a source is 255. + +Each form is decoded and translated, and the emitted lines (the result, then SCC) are run in order on the CPU by the +integer evaluator of startup_gfx2hlsl_bit_alu_check.py over edge values: 0, all ones, alternating bits, every single +bit and its complement, and mixed words. The reference is written here with plain integer arithmetic from the ISA +meaning: D = ~(S0 op S1) on 32 bits and SCC = (D != 0). SCC starts at a value neither 0 nor 1, so a form that does not +write it fails. The value set gives SCC = 0 and SCC = 1 for every operation. + +The lowering text and SCC rule equal pixel_aot's SCALAR entries for the same operations (#561). Every numeric form +(SGPR, inline, literal or m0 operands) emits the lines of its non-complement sibling (s_and_b32, s_or_b32, s_xor_b32) +with the operation wrapped in ~( ). Lane-mask forms are refused: a vcc_lo or exec_lo operand or destination, or an +SGPR holding a compare mask, is the whole wave's mask, and SCC is set when any bit of the complemented 32-bit result +is; the translator's per-lane projection cannot show that, so no mask lowering is claimed. + +Mutations: the table entry with the complement dropped, and the entry with the SCC write dropped, must each read a +wrong result or SCC. Refused: every SOP2 operation the decoder names that still has no lowering. +""" +import re +import struct +import sys +from pathlib import Path + +HERE = Path(__file__).resolve().parent +sys.path.insert(0, str(HERE.parent / 'tools')) +sys.path.insert(0, str(HERE.parent / 'tools' / 'startup')) +sys.path.insert(0, str(HERE)) +import disasm_rdna2 as D # noqa: E402 +import gfx2hlsl as G # noqa: E402 +import pixel_aot as P # noqa: E402 +from startup_flat_param_check import assemble # noqa: E402 +from startup_gfx2hlsl_bit_alu_check import Expr # noqa: E402 + +MASK = 0xffffffff +OPCODE = {name: op for op, (name, _) in D.SOP2_OPS.items()} +# operation -> (non-complement sibling, reference) +OPS = { + 's_nand_b32': ('s_and_b32', lambda a, b: ~(a & b) & MASK), + 's_nor_b32': ('s_or_b32', lambda a, b: ~(a | b) & MASK), + 's_xnor_b32': ('s_xor_b32', lambda a, b: ~(a ^ b) & MASK), +} +SIBLING_OPERATOR = {'s_and_b32': '&', 's_or_b32': '|', 's_xor_b32': '^'} + +SPECIAL = [0, MASK, 0xaaaaaaaa, 0x55555555, 0x80000000, 0x7fffffff, 1, 0xfffffffe, 0xffff0000, 0x0000ffff, + 0x12345678, 0xedcba987, 0xdeadbeef, 0x0f0f0f0f] +VALUES = SPECIAL + [1 << k for k in range(32)] + [~(1 << k) & MASK for k in range(32)] +PAIRS = [(a, b) for a in SPECIAL for b in VALUES] + [(a, b) for a in VALUES for b in SPECIAL] +SCC_UNSET = 2 # not a value SCC can take: a form that leaves SCC alone keeps it + +VCC_LO, M0, EXEC_LO, LITERAL = 106, 124, 126, 255 +INLINE = {128: 0, 129: 1, 193: MASK} # ssrc codes: 0, 1, -1 + + +def sop2(name, sdst, ssrc0, ssrc1, literal=None): + word = (0b10 << 30) | (OPCODE[name] << 23) | (sdst << 16) | (ssrc1 << 8) | ssrc0 + return struct.pack(' 1) + + # 4. Every value form evaluates to the reference: result and SCC. + for name in OPS: + found = first_mismatch(name) + check('%s: %d forms x %d pairs equal the reference (result and SCC)' % (name, len(VALUE_FORMS), len(PAIRS)), + found is None, str(found)) + + # 5. Every numeric form emits the sibling's lines with the operation complemented. + for name, (sibling, _) in OPS.items(): + wrong = [] + for form in VALUE_FORMS: + asm, stage = attempt(code(name, form)) + plain_asm, plain = translate(code(sibling, form)) + operands = plain_asm.split(' ', 1)[1].split(', ') + probe = Probe() + inner = '%s %s %s' % (probe.u(operands[1]), SIBLING_OPERATOR[sibling], probe.u(operands[2])) + expected = [line.replace(inner, '~(%s)' % inner, 1) for line in plain.lines] + if (asm != plain_asm.replace(sibling, name, 1) or stage.lines != expected or expected == plain.lines + or stage.masks != plain.masks or stage.exec_known != plain.exec_known): + wrong.append((asm, stage.lines, plain.lines, stage.masks, plain.masks)) + check('%s: %d numeric forms emit the %s lines complemented, same mask and EXEC bookkeeping' + % (name, len(VALUE_FORMS), sibling), not wrong, str(wrong[:2])) + + # 5b. Lane-mask forms are refused: vcc_lo/exec_lo operands and destinations, and an SGPR holding a compare mask. + for name in OPS: + accepted = [] + for form in MASK_FORMS: + asm, stage = attempt(code(name, form)) + if stage.lines != ['refused: %s on a lane mask' % name]: + accepted.append((asm, stage.lines)) + stage = Probe() + stage.masks.add(1) # s1 holds a compare result + try: + stage.one(name, '%s s3, s1, s2' % name) + accepted.append(('%s s3, s1(mask), s2' % name, stage.lines)) + except G.Unsupported as error: + if str(error) != '%s on a lane mask' % name: + accepted.append(('%s s3, s1(mask), s2' % name, [str(error)])) + check('%s: %d lane-mask forms refused' % (name, len(MASK_FORMS) + 1), not accepted, str(accepted[:2])) + + # Symbolic descriptor sources cannot silently leave the previous SCC in place. + for name in OPS: + stage = Probe() + stage.sym[1] = ('ptr', 0, 0) + try: + stage.one(name, '%s s3, s1, s2' % name) + except G.Unsupported as error: + check('%s refuses a descriptor before writing SCC' % name, + str(error) == 'scc from descriptor data' and not stage.lines, str(error)) + else: + check('%s refuses a descriptor before writing SCC' % name, False, str(stage.lines)) + + # 6. Mutated table entries, built from the sibling's, must read a wrong word or SCC. + saved = dict(G.SCA) + try: + for name, (sibling, _) in OPS.items(): + G.SCA[name] = saved[sibling] + found = first_mismatch(name) + check('%s mutation without the complement fails: %s' % (name, found), found is not None) + G.SCA[name] = ('~(%s)' % saved[sibling][0], None) + found = first_mismatch(name) + check('%s mutation without the SCC write fails: %s' % (name, found), found is not None) + G.SCA.clear() + G.SCA.update(saved) + finally: + G.SCA.clear() + G.SCA.update(saved) + + # 7. SOP2 operations that still have no lowering are refused by name. + missing = [name for _op, (name, _) in sorted(D.SOP2_OPS.items()) + if name not in G.SCA and not hasattr(G.Stage, 'op_' + name)] + wrong = [] + for name in missing: + asm = decode_one(sop2(name, 2, 4, 6))[1] + try: + Probe().one(name, asm) + wrong.append((asm, 'accepted')) + except G.Unsupported as error: + if str(error) != name: + wrong.append((asm, str(error))) + check('%d SOP2 operations without a lowering are refused by name (%s)' % (len(missing), ', '.join(missing)), + missing and not wrong and not set(OPS) & set(missing), str(wrong)) + + +def all_codes(): + return [code(name, form) for name in list(OPS) + [s for s, _ in OPS.values()] for form in VALUE_FORMS + MASK_FORMS] + + +def check_binary(llvm_mc): + codes = all_codes() + text = ''.join(decode_one(c)[1] + '\n' for c in codes) + check('llvm-mc assembles %d decoded SOP2 forms to the same words' % len(codes), + assemble(llvm_mc, text) == b''.join(codes)) + + +def check_decode(library): + codes = all_codes() + texts = P.disassemble(b''.join(codes), library) + ours = [decode_one(c)[1] for c in codes] + wrong = [(a, b) for a, b in zip(texts, ours) if a != b] + check('LLVM disassembles %d SOP2 forms as tools/disasm_rdna2.py does' % len(codes), + len(texts) == len(codes) and not wrong, str(wrong[:3])) + + +def main(): + if len(sys.argv) == 1: + controls() + else: + mode, *args = sys.argv[1:] + if mode == 'binary': + check_binary(*args) + elif mode == 'decode': + check_decode(*args) + else: + raise SystemExit(__doc__) + if failures: + print('FAIL') + for text in failures: + print(' ' + text) + sys.exit(1) + print('PASS') + + +if __name__ == '__main__': + main() diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 36b2d2194..5f9994350 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -615,6 +615,15 @@ def low(tok): return ('s%d' % r[1]) if r and r[0] == 's' and r[1] != r[2] else tok srcs = [low(t) for t in srcs] dname = low(ops[0]) + word_complement = base in ('s_nand_b32', 's_nor_b32', 's_xnor_b32') + if word_complement: + # A lane mask here is the whole wave's mask, and SCC is set when any bit of the + # complemented 32-bit result is; the per-lane projection below cannot show that. + def lane_mask(t): + r = reg_range(t) + return t in ('exec', 'exec_lo', 'vcc', 'vcc_lo') or bool(r and r[0] == 's' and r[1] in self.masks) + if lane_mask(dname) or any(lane_mask(t) for t in srcs): + raise Unsupported('%s on a lane mask' % base) if pair and ('m0' in srcs or dname == 'm0'): raise Unsupported('64-bit scalar operand m0') def src_sym(t): @@ -624,6 +633,8 @@ def src_sym(t): return self.sym.get(r[1]) if r and r[0] == 's' else None syms = [src_sym(t) for t in srcs] if any(s and s[0] in ('desc', 'descnum', 'ptr', 'ptrhi') for s in syms): + if word_complement: + raise Unsupported('scc from descriptor data') if dname == 'm0': if scc: raise Unsupported('scc from descriptor data') @@ -1514,6 +1525,7 @@ def declarations(self): 's_mov_b32': ('{0}', None), 's_mov_b64': ('{0}', None), 's_movk_i32': ('{0}', None), 's_and_b32': ('{0} & {1}', '$ != 0'), 's_or_b32': ('{0} | {1}', '$ != 0'), 's_xor_b32': ('{0} ^ {1}', '$ != 0'), 's_andn2_b32': ('{0} & ~{1}', '$ != 0'), 's_orn2_b32': ('{0} | ~{1}', '$ != 0'), 's_not_b32': ('~{0}', '$ != 0'), + 's_nand_b32': ('~({0} & {1})', '$ != 0'), 's_nor_b32': ('~({0} | {1})', '$ != 0'), 's_xnor_b32': ('~({0} ^ {1})', '$ != 0'), 's_and_b64': ('{0} & {1}', '$ != 0'), 's_or_b64': ('{0} | {1}', '$ != 0'), 's_xor_b64': ('{0} ^ {1}', '$ != 0'), 's_andn2_b64': ('{0} & ~{1}', '$ != 0'), 's_orn2_b64': ('{0} | ~{1}', '$ != 0'), 's_not_b64': ('~{0}', '$ != 0'), 's_nand_b64': ('~({0} & {1})', '$ != 0'), 's_nor_b64': ('~({0} | {1})', '$ != 0'), 's_xnor_b64': ('~({0} ^ {1})', '$ != 0'),