From 710fb44d6b1ffe9b1a39bb6b87858fcf956e069c Mon Sep 17 00:00:00 2001 From: nanyoudegege <132808593+nanyoudegege@users.noreply.github.com> Date: Sun, 29 Nov 2026 16:14:35 +0800 Subject: [PATCH] shader: clear gfx2hlsl scalar min/max SCC on ties --- CMakeLists.txt | 2 +- tests/startup_gfx2hlsl_compute_check.py | 27 +++++++++++++-------- tests/startup_gfx2hlsl_scalar_null_check.py | 8 +++--- tools/startup/gfx2hlsl.py | 4 +-- 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a43c3ae223..e487526747 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4638,7 +4638,7 @@ if(EXISTS "${INTAKE_PYTHON}") foreach(control IN ITEMS swapped-axes dropped-store b-offset c-unpredicated f-peer f-broadcast f-second-fill g-swapped-offsets g-overlap j-scalar-dropped h-inner-unpredicated k-block-entered m-else-unswitched n-unmasked o-mask-kept o-subset-remask-dropped n-dropped-broadcast p-dropped-andn2 p-dropped-or p-block-entered q-block-entered q-not-all-ones - s-min-strict s-borrow-flipped) + s-min-inclusive s-borrow-flipped) add_test(NAME startup_gfx2hlsl_compute_${control}_${adapter} COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_compute_check.py" control ${control} "${DXC_EXECUTABLE}" "$" ${adapter}) diff --git a/tests/startup_gfx2hlsl_compute_check.py b/tests/startup_gfx2hlsl_compute_check.py index 928589a0c9..65bb32b031 100644 --- a/tests/startup_gfx2hlsl_compute_check.py +++ b/tests/startup_gfx2hlsl_compute_check.py @@ -4,7 +4,7 @@ binary reassemble the fixture sources and compare them with the words below dispatch translate, compile with DXC cs_6_0, dispatch and compare every word - control + control the same dispatch with a mutated translation, which must mismatch The fixtures are source-built gfx1013 compute programs. The root signature puts two root constants (key, stride) @@ -643,7 +643,7 @@ def scc_fixture(): """Group (x, y) of the 4 x 4 grid takes S0 = edge[x], S1 = edge[y] (0, -1, INT_MAX, INT_MIN); the runner accepts at most 5 groups per axis. Each s_add/s_sub/s_min/s_max forms is run twice (the destination apart from S0, then the destination that is S0) and its SCC read right after; word 0 of the group holds the 16 SCC bits, word 1 a hash of the 16 results. RDNA 2 ISA: s_add_u32 carry out, s_sub_u32 borrow, - s_add_i32 / s_sub_i32 signed overflow, s_min / s_max SCC = (D == S0).""" + s_add_i32 / s_sub_i32 signed overflow, s_min / s_max SCC is set only when S0 is strictly smaller / larger.""" lines = ['s_mov_b32 s10, s6', 's_mov_b32 s11, 0', 's_load_dwordx8 s[12:19], s[10:11], 0x0', 's_waitcnt lgkmcnt(0)', 's_lshl_b32 s24, s8, 2', 's_add_u32 s24, s24, s7', 'v_lshlrev_b32 v4, 3, s24', 's_mov_b32 s22, s7', 's_mov_b32 s23, s8'] @@ -686,9 +686,8 @@ def signed(x): scc = not -(1 << 31) <= signed(x) - signed(b) < (1 << 31) else: view = signed if op.endswith('i32') else (lambda v: v) - pick = min if 'min' in op else max - result = pick(x, b, key=view) - scc = result == x + scc = view(x) < view(b) if 'min' in op else view(x) > view(b) + result = x if scc else b bits |= int(scc) << (2 * n + alias) hashed = ((hashed ^ result) << 1) & MASK return bits, hashed @@ -1590,7 +1589,8 @@ def check_scc_modelled(): # its sources, ahead of the branch. words = {'s_add_u32 s23, s7, -1': '07c11780', 's_sub_u32 s23, s7, 1': '07819780', 's_add_i32 s23, s7, 0x7fffffff': '07ff1781ffffff7f', 's_sub_i32 s23, s7, 1': '07819781', - 's_min_u32 s23, s7, 4': '07849783', 's_max_i32 s23, s7, 4': '07841784', 's_wqm_b32 s23, s7': '070997be', + 's_min_u32 s23, s7, 4': '07849783', 's_max_u32 s23, s7, 4': '07849784', + 's_min_i32 s23, s7, 4': '07841783', 's_max_i32 s23, s7, 4': '07841784', 's_wqm_b32 s23, s7': '070997be', 's_cselect_b32 s24, 1, 0': '81801885', 's_cbranch_scc1 1': '010085bf'} SPLICE.update(words) for line, word in words.items(): # the decoder prints each hand-assembled word as its line (literals in hex) @@ -1603,10 +1603,16 @@ def check_scc_modelled(): ('s_add_i32 s23, s7, 0x7fffffff', 'scc = (((((s7) ^ (s7 + 0x7fffffffu)) & ((0x7fffffffu) ^ (s7 + 0x7fffffffu))) >> 31) != 0);'), ('s_sub_i32 s23, s7, 1', 'scc = (((((s7) ^ (0x00000001u)) & ((s7) ^ (s7 - 0x00000001u))) >> 31) != 0);'), - ('s_min_u32 s23, s7, 4', 'scc = ((s7) <= (0x00000004u));'), - ('s_max_i32 s23, s7, 4', 'scc = (int(s7) >= int(0x00000004u));')): + ('s_min_u32 s23, s7, 4', 'scc = ((s7) < (0x00000004u));'), + ('s_max_u32 s23, s7, 4', 'scc = ((s7) > (0x00000004u));'), + ('s_min_i32 s23, s7, 4', 'scc = (int(s7) < int(0x00000004u));'), + ('s_max_i32 s23, s7, 4', 'scc = (int(s7) > int(0x00000004u));')): text = translate('A', code=splice(op, 's_cbranch_scc1 1', 'v_mov_b32 v3, 0')).hlsl_cs() assert scc in text and text.index(scc) < text.index('pc = (scc)'), (op, text) + # Case S packs two SCC bits each for min_u, max_u, min_i, max_i in bits 8..15 (separate and aliased destination). + assert all(scc_reference(x, x)[0] >> 8 == 0 for x in SCC_EDGES) + assert scc_reference(0, MASK)[0] >> 8 == 0xc3 # 0 < UINT_MAX, but 0 > -1 + assert scc_reference(MASK, 0)[0] >> 8 == 0x3c # A reader whose last SCC writer the lowering still does not model is refused by name (s_wqm is not lowered). refused('scc from an SCC write that is not modelled in s_cbranch_scc1', lambda: translate( 'A', code=splice('s_wqm_b32 s23, s7', 's_cbranch_scc1 1', 'v_mov_b32 v3, 0'))) @@ -2551,8 +2557,8 @@ def check_binary(llvm_mc): MBCNT_COUNT = f'countbits(0x85241249u & ((1u << {MBCNT_LANE}) - 1u))' CONTROLS = { 'join-load-zero': ('join-load', 's24 = ul24_7;', 's24 = 0u;'), - # s_min_u32 sets SCC when S0 is chosen, equal operands included: a strict compare differs at S0 == S1. - 's-min-strict': ('S', '(s20) <= (s21)', '(s20) < (s21)'), + # Setting SCC on a tie changes bit 8 of the group's first word; the min result is unchanged. + 's-min-inclusive': ('S', 'scc = ((s20) < (s21));', 'scc = ((s20) <= (s21));'), # s_sub_u32 borrow: the first source is the smaller one. 's-borrow-flipped': ('S', '(s20) < (s21);\n s24 = s20 - s21;', '(s21) < (s20);\n s24 = s20 - s21;'), 'swapped-axes': ('A', ' s7 = gid.x;\n s8 = gid.y;\n', ' s7 = gid.y;\n s8 = gid.x;\n'), @@ -2594,6 +2600,7 @@ def check_binary(llvm_mc): # First bad word for controls with a pinned mismatch. CONTROL_MISMATCH = { + 's-min-inclusive': 'word=0 expected=00000000 actual=00000100', 'join-load-zero': 'word=0 expected=8b02a7f1 actual=00000000', 'f-second-fill': 'word=1 expected=00000094 actual=00000000', 'd-mask': 'word=8 expected=00000008 actual=00000009', diff --git a/tests/startup_gfx2hlsl_scalar_null_check.py b/tests/startup_gfx2hlsl_scalar_null_check.py index 416904bbfa..50c92ed00d 100644 --- a/tests/startup_gfx2hlsl_scalar_null_check.py +++ b/tests/startup_gfx2hlsl_scalar_null_check.py @@ -451,10 +451,10 @@ def run_stage(*instructions, varying=()): ('s_sub_u32 null, s0, s1', 'scc = (s0) < (s1);'), ('s_add_i32 null, s0, s1', 'scc = (((((s0) ^ (s0 + s1)) & ((s1) ^ (s0 + s1))) >> 31) != 0);'), ('s_sub_i32 null, s0, s1', 'scc = (((((s0) ^ (s1)) & ((s0) ^ (s0 - s1))) >> 31) != 0);'), - ('s_min_u32 null, s0, s1', 'scc = ((s0) <= (s1));'), - ('s_max_u32 null, s0, s1', 'scc = ((s0) >= (s1));'), - ('s_min_i32 null, s0, s1', 'scc = (int(s0) <= int(s1));'), - ('s_max_i32 null, s0, s1', 'scc = (int(s0) >= int(s1));')): + ('s_min_u32 null, s0, s1', 'scc = ((s0) < (s1));'), + ('s_max_u32 null, s0, s1', 'scc = ((s0) > (s1));'), + ('s_min_i32 null, s0, s1', 'scc = (int(s0) < int(s1));'), + ('s_max_i32 null, s0, s1', 'scc = (int(s0) > int(s1));')): st = ProbeStage() st.one(asm.split()[0], asm) check(f'{asm} computes only its SCC {st.lines}', st.lines == [scc_line]) diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 5b2775b10d..102884e0ce 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -11482,7 +11482,7 @@ def low(n): def add_sub_min_max_scc(base, a, b, result): """The SCC expression of s_add/s_sub/s_min/s_max_{i,u}32 over the source texts a and b and the result text. s_add_u32: carry out; s_sub_u32: borrow (S0 < S1); s_add_i32 / s_sub_i32: signed overflow (the sign of the result - differs from the sign the operands imply); s_min / s_max: SCC = (D == S0), the first source was chosen.""" + differs from the sign the operands imply); s_min / s_max: S0 < S1 / S0 > S1 (RDNA 2 ISA, SOP2 opcodes 6-9).""" if base == 's_add_u32': return '(%s) < (%s)' % (result, a) if base == 's_sub_u32': @@ -11492,7 +11492,7 @@ def add_sub_min_max_scc(base, a, b, result): if base == 's_sub_i32': return '(((((%s) ^ (%s)) & ((%s) ^ (%s))) >> 31) != 0)' % (a, b, a, result) cast = 'int' if base.endswith('_i32') else '' - return '(%s(%s) %s %s(%s))' % (cast, a, '<=' if base.startswith('s_min') else '>=', cast, b) + return '(%s(%s) %s %s(%s))' % (cast, a, '<' if base.startswith('s_min') else '>', cast, b) PACK_B16 = frozenset(('s_pack_ll_b32_b16', 's_pack_lh_b32_b16', 's_pack_hh_b32_b16')) SCA = {