From 9d013a48d6d62817f08bfec2638ac09ab533268f Mon Sep 17 00:00:00 2001 From: Maledetto <20729264+Makto94@users.noreply.github.com> Date: Wed, 25 Nov 2026 01:06:23 +0100 Subject: [PATCH] shader: keep s_bfe_u32 wide fields in gfx2hlsl s_bfe_u32 takes the width from S1[22:16]. HLSL reduces a shift count to 5 bits, so the old mask (1u << (width & 127)) - 1 read 0 at width 32 and truncated at widths 33 to 127. The mask is now all ones for widths of 32 or more. The sop2_complement check covers the forms against an ISA reference, with negative controls. --- docs/experiments.md | 8 ++ .../startup_gfx2hlsl_sop2_complement_check.py | 90 ++++++++++++++++++- tools/startup/gfx2hlsl.py | 2 +- 3 files changed, 98 insertions(+), 2 deletions(-) diff --git a/docs/experiments.md b/docs/experiments.md index ab4d77ad3d..d6befddd6a 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -6395,6 +6395,14 @@ bits. Run `ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_s_bitcmp$' The fixture evaluates the emitted expression on the CPU and checks both operand positions for mask refusal. It does not execute a GPU draw. +**Scalar bitfield extract with a wide width.** `s_bfe_u32` takes the offset from S1[4:0] and the width from +S1[22:16]. HLSL reduces a shift count to 5 bits, so the old mask `(1u << (width & 127)) - 1` was 0 at width 32 and +truncated at widths 33 to 127, where the ISA gives `S0 >> offset`. The mask is now all ones for widths of 32 or +more, and every shift count stays in 0..31. Width 0 still gives 0, and SCC is still the nonzero test of the result. +`startup_gfx2hlsl_sop2_complement` checks SGPR, in-place, `m0`, inline and literal forms against an ISA reference. +The old formula, a 6-bit width, a dropped field mask and a dropped SCC write each fail it. It runs on the CPU and +does not draw on a GPU. + ## 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 index 598837df81..2cb1128068 100644 --- a/tests/startup_gfx2hlsl_sop2_complement_check.py +++ b/tests/startup_gfx2hlsl_sop2_complement_check.py @@ -25,6 +25,12 @@ The `s_pack_hh_b32_b16` forms use the same encoder and evaluator. The reference places the high half of src0 in the low result half and the high half of src1 in the high result half, preserving SCC. Swapping the inputs must fail the value check. + +The `s_bfe_u32` forms use the same encoder and evaluator. The reference reads the ISA text: offset = S1[4:0], width = +S1[22:16], a width of 0 gives 0, offset + width < 32 gives the field, and any wider field gives S0 >> offset. SCC is +D != 0. HLSL reduces a shift count to 5 bits, so a lowering that shifts by the 7-bit width gives 0 at width 32 and a +truncated mask at 33 to 127. The inputs reach widths 0 to 127 with the source sign bit set, as SGPR, inline and +literal controls. The mutated table entries must read a wrong word. """ import re import struct @@ -44,6 +50,7 @@ MASK = 0xffffffff OPCODE = {name: op for op, (name, _) in D.SOP2_OPS.items()} PACK_HH = 's_pack_hh_b32_b16' +BFE = 's_bfe_u32' # operation -> (non-complement sibling, reference) OPS = { 's_nand_b32': ('s_and_b32', lambda a, b: ~(a & b) & MASK), @@ -225,6 +232,85 @@ def reference(a, b): G.SCA[PACK_HH] = saved +BFE_OFFSETS = [0, 1, 4, 8, 16, 24, 31] +BFE_WIDTHS = [0, 1, 8, 16, 31, 32, 33, 40, 48, 63, 64, 65, 96, 127] +BFE_SOURCES = [MASK, 0x80000000, 0x9a345f50, 0xdeadbeef, 0x12345678, 1, 0, 0xaaaaaaaa, 0x55555555] +# Control bits 15:5 and 31:23 are ignored; the second spelling of each control sets them all. +BFE_CONTROLS = sorted({(w << 16) | o | junk for o in BFE_OFFSETS for w in BFE_WIDTHS for junk in (0, 0xff80ffe0)} + | {0xffffffff, 0x007f0000, 0x00200000, 0x00400000}) +BFE_PAIRS = [(a, c) for a in BFE_SOURCES for c in BFE_CONTROLS] +# Pairs that separate the old lowering from the ISA: width 32 and 64 read 0 before, a wide field loses its mask. +BFE_WIDE = [(0x80000000, 0x00200000), (MASK, 0x00400000), (0x9a345f50, 0x00280000), (0x80000001, 0x007f0000), + (0x80000000, 0x003f0000), (0xdeadbeef, 0x00200008)] + + +def bfe_reference(src, ctrl): + offset, width = ctrl & 31, (ctrl >> 16) & 127 + if width == 0: + return 0 + if offset + width < 32: + return (src >> offset) & ((1 << width) - 1) + return src >> offset + + +def bfe_first_mismatch(reference=bfe_reference, pairs=BFE_PAIRS): + """The first (form, source, control, got, want) of s_bfe_u32 that differs from the ISA reference, or None. + + Control forms: SGPR (separate, in place on either source), m0 destination, inline 0, 1 and -1, and a literal + for the control and for the source, so each operand kind of the translator is evaluated.""" + cases = [((3, 1, 2, None), pairs), ((1, 1, 2, None), pairs), ((2, 1, 2, None), pairs), ((M0, 1, 2, None), pairs)] + cases += [((3, 1, code_, None), [(a, c) for a, c in pairs if c == INLINE[code_]]) for code_ in INLINE] + cases += [((3, 1, LITERAL, c), [(a, x) for a, x in pairs if x == c]) for c in sorted({x for _, x in pairs})] + cases += [((3, LITERAL, 2, a), [(x, c) for x, c in pairs if x == a]) for a in sorted({x for x, _ in pairs})] + for form, inputs in cases: + sdst, ssrc0, ssrc1, literal = form + asm, stage = attempt(code(BFE, form)) + if stage.lines[0].startswith('refused: '): + return asm, stage.lines[0] + destination = 'm0' if sdst == M0 else 's%d' % sdst + for a, c in inputs: + env = {'s1': a, 's2': c, 'scc': SCC_UNSET} + want = reference(source_value(ssrc0, literal, env), source_value(ssrc1, literal, env)) + out = run_lines(stage.lines, env) + got = (out.get(destination), out['scc']) + if got != (want, int(want != 0)): + return asm, hex(a), hex(c), got, (want, int(want != 0)) + return None + + +def bfe_controls(): + check('s_bfe_u32 matches the LLVM 15 SOP2 encoding', sop2(BFE, 0, 1, 2).hex() == '01028093') + widths = {(c >> 16) & 127 for _, c in BFE_PAIRS} + wide = [bfe_reference(a, c) for a, c in BFE_WIDE] + check('s_bfe_u32 inputs reach widths 0 to 127, and wide fields with a nonzero result', + widths >= {0, 31, 32, 33, 40, 63, 64, 127} and all(wide), str(wide)) + mismatch = bfe_first_mismatch() + check('s_bfe_u32 result and SCC across %d (source, control) pairs and all operand kinds' % len(BFE_PAIRS), + mismatch is None, str(mismatch)) + mismatch = bfe_first_mismatch(pairs=BFE_WIDE) + check('s_bfe_u32 wide fields (width 32 and above)', mismatch is None, str(mismatch)) + + saved = G.SCA[BFE] + old = '({0} >> ({1} & 31)) & ((1u << (({1} >> 16) & 127)) - 1)' + try: + # The reference itself would pass a formula that agrees with it; the old table entry must not. + G.SCA[BFE] = (old, saved[1]) + found = bfe_first_mismatch() + check('s_bfe_u32 negative control: the width taken mod 32 by the shift fails: %s' % (found,), + found is not None) + G.SCA[BFE] = (saved[0], None) + found = bfe_first_mismatch() + check('s_bfe_u32 negative control: no SCC write fails: %s' % (found,), found is not None) + G.SCA[BFE] = ('({0} >> ({1} & 31)) & ((1u << (({1} >> 16) & 63)) - 1)', saved[1]) + found = bfe_first_mismatch() + check('s_bfe_u32 negative control: a 6-bit width fails: %s' % (found,), found is not None) + G.SCA[BFE] = ('{0} >> ({1} & 31)', saved[1]) + found = bfe_first_mismatch() + check('s_bfe_u32 negative control: no field mask fails: %s' % (found,), found is not None) + finally: + G.SCA[BFE] = saved + + def controls(): # 1. The encoder writes what the decoder reads, checked on the LLVM 15 encodings in tests/rdna2_tools_check.py. known = [(sop2('s_nand_b32', 0, 1, 2), '0102008c'), (sop2('s_nor_b32', 0, 1, 2), '0102008d'), @@ -330,11 +416,13 @@ def controls(): 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)) pack_controls() + bfe_controls() def all_codes(): logic = [code(name, form) for name in list(OPS) + [s for s, _ in OPS.values()] for form in VALUE_FORMS + MASK_FORMS] - return logic + [code(PACK_HH, form) for form in VALUE_FORMS] + return (logic + [code(PACK_HH, form) for form in VALUE_FORMS] + + [code(BFE, form) for form in VALUE_FORMS]) def check_binary(llvm_mc): diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 6491fa63bd..502a7454e9 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -1960,7 +1960,7 @@ def declarations(self): 's_add_i32': ('{0} + {1}', None), 's_add_u32': ('{0} + {1}', None), 's_sub_i32': ('{0} - {1}', None), 's_sub_u32': ('{0} - {1}', None), 's_mul_i32': ('{0} * {1}', None), 's_min_u32': ('min({0}, {1})', None), 's_max_u32': ('max({0}, {1})', None), 's_min_i32': ('uint(min(int({0}), int({1})))', None), 's_max_i32': ('uint(max(int({0}), int({1})))', None), - 's_bfe_u32': ('({0} >> ({1} & 31)) & ((1u << (({1} >> 16) & 127)) - 1)', '$ != 0'), + 's_bfe_u32': ('({0} >> ({1} & 31)) & ((({1} >> 16) & 127) >= 32 ? 0xffffffffu : ((1u << (({1} >> 16) & 31)) - 1))', '$ != 0'), 's_brev_b32': ('reversebits({0})', None), 's_pack_hh_b32_b16': ('({0} >> 16) | ({1} & 0xffff0000u)', None), # s_bfm_b32: a mask of S0[4:0] ones shifted left by S1[4:0]; SCC unchanged.