From 5ff13510281aaa629851db35bd43b9c1d3be5bf5 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Thu, 26 Nov 2026 02:49:18 +0100 Subject: [PATCH 01/19] startup: refuse a vector ALU form that reads a lane mask as numeric data in gfx2hlsl (cherry picked from commit 6a91c26539918a6906104084ac638b05d8a267bb) --- tests/startup_gfx2hlsl_compute_check.py | 20 +++++++++++++++++++ ..._gfx2hlsl_readfirstlane_recompute_check.py | 3 ++- ...up_gfx2hlsl_readfirstlane_uniform_check.py | 2 ++ tests/startup_gfx2hlsl_shift64_check.py | 6 +++--- tools/startup/gfx2hlsl.py | 18 +++++++++++++++++ 5 files changed, 45 insertions(+), 4 deletions(-) diff --git a/tests/startup_gfx2hlsl_compute_check.py b/tests/startup_gfx2hlsl_compute_check.py index 0880330261..5a5b30f3dd 100644 --- a/tests/startup_gfx2hlsl_compute_check.py +++ b/tests/startup_gfx2hlsl_compute_check.py @@ -804,6 +804,25 @@ def check_mbcnt(): assert 'uav 0 table 1 0' in translate('D').manifest() +LANE_MASK_DATA = ( + ('v_cvt_f32_u32 v5, vcc_lo', '6a0c0a7e'), # the llvm.amdgcn.ballot word converted to float + ('v_add_nc_u32 v5, vcc_lo, v5', '6a0a0a4a'), +) + + +def check_lane_mask_data(): + """A wave mask read as a number holds every lane's bit on hardware; the one-lane model has only its own.""" + compare = bytes.fromhex('8500027d') # v_cmp_lt_i32 vcc_lo, 5, v0 + select = bytes.fromhex('800a0a02') # v_cndmask_b32 v5, 0, v5, vcc_lo: the mask as a per-lane select + assert 'vcc = ' in translate('A', code=splice_words(compare, select)).hlsl_cs() + for _, word in LANE_MASK_DATA: + refused('as numeric data', lambda: translate('A', code=splice_words(compare, bytes.fromhex(word)))) + + +def splice_words(*chunks, at=STORE_A): + return WORDS_A[:at] + b''.join(chunks) + WORDS_A[at:] + + def check_descriptor_scc(): # Case A loads the input descriptor into s[12:15] and stores through s[16:19]. # Read s20's lane bit into the stored value. The SCC branches skip both dwords of the lane select. @@ -1064,6 +1083,7 @@ def check_controls(): check_lds() check_descriptor_scc() check_mbcnt() + check_lane_mask_data() check_formatted_loads() a = translate('A') # This buffer-only compute program has no typed UAV declaration or image manifest entry. diff --git a/tests/startup_gfx2hlsl_readfirstlane_recompute_check.py b/tests/startup_gfx2hlsl_readfirstlane_recompute_check.py index 538adf7899..4ddfbcdb61 100644 --- a/tests/startup_gfx2hlsl_readfirstlane_recompute_check.py +++ b/tests/startup_gfx2hlsl_readfirstlane_recompute_check.py @@ -41,6 +41,7 @@ def run(*instructions): REFUSAL = 'v_readfirstlane_b32 requires a proven uniform source' +EARLIER = {'lane mask source': 'v_mul_f32 reads lane mask s3 as numeric data'} READ = 'v_readfirstlane_b32 s9, v7' # Offset 0x8 is a join: the branch at 0x0 skips the instruction at 0x4. JOIN = ('s_cbranch_scc0 label_0008', 's_nop 0') @@ -127,7 +128,7 @@ def read_line(lines): ] for label, instructions in REFUSED: got = run(*instructions) - check(f'{label} refused', got == REFUSAL, repr(got)) + check(f'{label} refused', got == EARLIER.get(label, REFUSAL), repr(got)) # Writes the translator does not route through the first operand. probe = G.Stage.__new__(G.Stage) diff --git a/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py b/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py index b52af84b24..407fb8c9cc 100644 --- a/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py +++ b/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py @@ -187,6 +187,8 @@ def run(*instructions, exec_known=True, kind=None): 'compare into SGPR': 'v_mov_b32 data operand s2 is a lane mask', 'saved exec': 'v_mov_b32 data operand s2 is a lane mask', 'mask in the entry block': 'v_mov_b32 data operand s2 is a lane mask', + 'median of three of a lane mask': 'v_med3_f32 reads lane mask s2 as numeric data', + 'unsigned to float conversion of a mask': 'v_cvt_f32_u32 reads lane mask s2 as numeric data', } for label, instructions in REFUSED: got = run(*instructions) diff --git a/tests/startup_gfx2hlsl_shift64_check.py b/tests/startup_gfx2hlsl_shift64_check.py index 71a51c295c..cd85e480eb 100644 --- a/tests/startup_gfx2hlsl_shift64_check.py +++ b/tests/startup_gfx2hlsl_shift64_check.py @@ -232,7 +232,7 @@ def controls(): % (op, corrupted_hi, new_hi), corrupted_hi != new_hi) # Accepted operand forms (VGPR pair, unaligned VGPR pair, _e64 spelling, aligned SGPR pair, inline -16..64, - # SGPR/literal/m0/vcc_lo/exec_lo shift count) and inline sign-extension check. + # SGPR/literal/m0 shift count) and inline sign-extension check. accepted = [ 'v_lshrrev_b64 v[0:1], v2, v[4:5]', 'v_lshrrev_b64_e64 v[1:2], v0, v[1:2]', @@ -240,8 +240,6 @@ def controls(): 'v_ashrrev_i64 v[0:1], v2, s[4:5]', 'v_lshrrev_b64 v[0:1], 0x80000020, v[2:3]', 'v_lshrrev_b64 v[0:1], m0, v[2:3]', - 'v_lshrrev_b64 v[0:1], vcc_lo, v[2:3]', - 'v_lshrrev_b64 v[0:1], exec_lo, v[2:3]', 'v_lshrrev_b64 v[0:1], 32, -1', 'v_lshlrev_b64 v[0:1], 1, 64', 'v_ashrrev_i64 v[0:1], 1, -16', @@ -320,6 +318,8 @@ def controls(): 'v_lshrrev_b64 v[0:1]', 'v_lshrrev_b64 v[0:1], v2', 'v_lshrrev_b64 v[0:1], v2, v[4:5], v6', + 'v_lshrrev_b64 v[0:1], vcc_lo, v[2:3]', # a wave mask is not a shift count in the one-lane model + 'v_lshrrev_b64 v[0:1], exec_lo, v[2:3]', ] check('modifiers, invalid operands and wrong counts refused (%d cases)' % len(refusals), all(refused(a) for a in refusals)) diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 925ca2ffb0..e4e2b72ee5 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -1401,6 +1401,9 @@ def mesh_run(ins, at, what, patterns, env): MESH_VCC_BRANCHES = ('s_cbranch_vccz', 's_cbranch_vccnz') +# Vector ALU forms that take a lane mask as a per-lane select or carry, which the one-lane model handles. +MASK_AWARE_VALU = frozenset({'v_cndmask_b32', 'v_cndmask_b16', 'v_readlane_b32', 'v_writelane_b32', 'v_readfirstlane_b32'}) +MASK_AWARE_VALU_RE = re.compile(r'^v_cmpx?_|_co(_ci)?_|addc|subb|_co$|^v_mad_[iu]64_[iu]32$') VCC_KEYS = {'vcc', 'vcc_hi'} @@ -3175,6 +3178,21 @@ def forget_writes(self, name, ops, drop_since=True): return regs def lower(self, base, asm, ops, mods): + """Lower one instruction; a vector ALU form that reads a lane mask as a number is refused. + + The model keeps one lane's bit of a wave mask. A vector ALU source that reads the mask as a number (the + llvm.amdgcn.ballot word) sees every lane's bit on hardware, so the lowered word would be silently wrong. + The mask-aware forms (select, carry, readlane) and v_mov_b32 (which the readfirstlane proofs handle) pass. + The check runs after the form's own refusals, so those keep their messages.""" + masks = [] + if base.startswith('v_') and not base.startswith('v_mov_b32') and base not in MASK_AWARE_VALU and not MASK_AWARE_VALU_RE.search(base): + masks = [t for t in (o.strip().lstrip('-|').rstrip('|') for o in ops[1:]) if self.is_lane_mask(t)] + result = self._lower(base, asm, ops, mods) + if masks: + raise Unsupported('%s reads lane mask %s as numeric data' % (base, masks[0])) + return result + + def _lower(self, base, asm, ops, mods): pc = getattr(self, 'pc', None) calculated = getattr(self, 'calculated_pc', {}) if pc in calculated: From a82a044ca172f8b3331068a0ccccba85edb81cc5 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Thu, 26 Nov 2026 02:55:41 +0100 Subject: [PATCH 02/19] startup: refuse a move of the high word of a wave64 lane mask in gfx2hlsl (cherry picked from commit 72945939f8e39c9948aa26fbbf1760f21832ba3c) --- tests/startup_gfx2hlsl_compute_check.py | 10 ++++++++++ tools/startup/gfx2hlsl.py | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/tests/startup_gfx2hlsl_compute_check.py b/tests/startup_gfx2hlsl_compute_check.py index 5a5b30f3dd..e839a2df50 100644 --- a/tests/startup_gfx2hlsl_compute_check.py +++ b/tests/startup_gfx2hlsl_compute_check.py @@ -819,6 +819,15 @@ def check_lane_mask_data(): refused('as numeric data', lambda: translate('A', code=splice_words(compare, bytes.fromhex(word)))) +def check_lane_mask_high_word(): + """The high word of a wave64 mask (the ballot's upper 32 bits) is modelled as 0: a move of it is refused.""" + chunks = [bytes.fromhex(w) for w in ('0000 84d4 9100 0200'.replace(' ', ''), # v_cmp_gt_i32 s[0:1], 17, v0 + '8004027d', # v_cmp_lt_i32 vcc, 0, v2 + '6a008089')] # s_xor_b64 s[0:1], vcc, s[0:1] + refused('as numeric data', lambda: translate('A', code=splice_words(*chunks, bytes.fromhex('01020a7e')))) + assert 'v5 = ud0;' in translate('A', code=splice_words(*chunks, bytes.fromhex('04020a7e'))).hlsl_cs() + + def splice_words(*chunks, at=STORE_A): return WORDS_A[:at] + b''.join(chunks) + WORDS_A[at:] @@ -1084,6 +1093,7 @@ def check_controls(): check_descriptor_scc() check_mbcnt() check_lane_mask_data() + check_lane_mask_high_word() check_formatted_loads() a = translate('A') # This buffer-only compute program has no typed UAV declaration or image manifest entry. diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index e4e2b72ee5..641a6a91cc 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -2249,6 +2249,7 @@ def set_s(self, n, expr, sym=None, scc=None, uniform=None): if getattr(self, 'since_join', None) is not None: (self.since_join.add if uniform else self.since_join.discard)(n) getattr(self, 'masks', set()).discard(n) # unit probes build a Stage without __init__ + getattr(self, 'mask_high', set()).discard(n) getattr(self, 'float_const_s', {}).pop(n, None) if sym is not None: self.sym[n] = sym @@ -3187,6 +3188,9 @@ def lower(self, base, asm, ops, mods): masks = [] if base.startswith('v_') and not base.startswith('v_mov_b32') and base not in MASK_AWARE_VALU and not MASK_AWARE_VALU_RE.search(base): masks = [t for t in (o.strip().lstrip('-|').rstrip('|') for o in ops[1:]) if self.is_lane_mask(t)] + if base.startswith('v_mov_b32'): # the high word of a wave64 mask has no one-lane value + masks = [t for t in ops[1:] if reg_range(t) and reg_range(t)[0] == 's' + and reg_range(t)[1] in getattr(self, 'mask_high', ())] result = self._lower(base, asm, ops, mods) if masks: raise Unsupported('%s reads lane mask %s as numeric data' % (base, masks[0])) @@ -4013,6 +4017,9 @@ def known(tok): self.masks.add(d) if pair and base != 's_mov_b64': self.set_s(d + 1, '0u') + if is_mask: + # The modelled high word is 0, but the hardware's holds the other lanes' bits: no numeric read of it. + self.mask_high = getattr(self, 'mask_high', set()) | {d + 1} elif pair: self.set_s(d + 1, self.S(high) if high is not None else '0u', uniform=high_uniform) return None From 07c245fdc2009b9bf5c789f0c747261eb69baa5e Mon Sep 17 00:00:00 2001 From: tedin7 Date: Thu, 26 Nov 2026 03:32:32 +0100 Subject: [PATCH 03/19] startup: keep the select, readlane and writelane forms of a lane-mask read allowed, SDWA spellings included --- tools/startup/gfx2hlsl.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 641a6a91cc..5fa8ac9f4a 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -1403,7 +1403,7 @@ def mesh_run(ins, at, what, patterns, env): MESH_VCC_BRANCHES = ('s_cbranch_vccz', 's_cbranch_vccnz') # Vector ALU forms that take a lane mask as a per-lane select or carry, which the one-lane model handles. MASK_AWARE_VALU = frozenset({'v_cndmask_b32', 'v_cndmask_b16', 'v_readlane_b32', 'v_writelane_b32', 'v_readfirstlane_b32'}) -MASK_AWARE_VALU_RE = re.compile(r'^v_cmpx?_|_co(_ci)?_|addc|subb|_co$|^v_mad_[iu]64_[iu]32$') +MASK_AWARE_VALU_RE = re.compile(r'^v_cmpx?_|_co(_ci)?_|addc|subb|_co$|^v_mad_[iu]64_[iu]32$|^v_(cndmask|readlane|writelane|readfirstlane)_') VCC_KEYS = {'vcc', 'vcc_hi'} From b90e4d18057b2876426ad13b481dafd24bb73d07 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Thu, 26 Nov 2026 03:23:53 +0100 Subject: [PATCH 04/19] startup: refuse a scalar ALU op that reads a lane mask as numeric data in gfx2hlsl A scalar compare, shift or add on vcc/exec/a mask SGPR (s_cmpk_gt_u32 vcc_lo, s_add_u32 s6, vcc_lo, 1) reads the whole wave's word on hardware; the one-lane model has only the lane's own bit, so the lowering was silently wrong (the ballot word compared with a constant selected the wrong value on WARP). Mask-only scalar ops (mov, and/or/xor/andn2/orn2/not, cselect, wqm, saveexec, cbranch, s_ff1 which has its own checks) are unchanged. Test: check_lane_mask_scalar_data (fails before, passes after); three readfirstlane_uniform controls now name the earlier refusal. (cherry picked from commit af4cf6e7e1423a84fe473fdeb5d1076290e74ec7) --- tests/startup_gfx2hlsl_compute_check.py | 17 +++++++++++++++++ ...rtup_gfx2hlsl_readfirstlane_uniform_check.py | 6 ++++++ tools/startup/gfx2hlsl.py | 8 ++++++++ 3 files changed, 31 insertions(+) diff --git a/tests/startup_gfx2hlsl_compute_check.py b/tests/startup_gfx2hlsl_compute_check.py index e839a2df50..3fd41f18fd 100644 --- a/tests/startup_gfx2hlsl_compute_check.py +++ b/tests/startup_gfx2hlsl_compute_check.py @@ -819,6 +819,22 @@ def check_lane_mask_data(): refused('as numeric data', lambda: translate('A', code=splice_words(compare, bytes.fromhex(word)))) +LANE_MASK_SCALAR_DATA = ( + ('s_cmpk_gt_u32 vcc_lo, 0x64', '6400eab5'), # c7_ballot_cmp: the ballot word compared as a number + ('s_cmp_lg_u32 vcc_lo, 0', '6a8007bf'), + ('s_lshr_b32 s6, vcc_lo, 1', '6a810690'), + ('s_add_u32 s6, vcc_lo, 1', '6a810680'), +) + + +def check_lane_mask_scalar_data(): + """A scalar op that reads a wave mask as a number sees every lane's bit on hardware; mask-only scalar ops stay.""" + compare = bytes.fromhex('8500027d') # v_cmp_lt_i32 vcc_lo, 5, v0 + for _, word in LANE_MASK_SCALAR_DATA: + refused('as numeric data', lambda: translate('A', code=splice_words(compare, bytes.fromhex(word)))) + translate('A', code=splice_words(compare, bytes.fromhex('6a7e0687'))).hlsl_cs() # s_and_b32 s6, vcc_lo, exec_lo + + def check_lane_mask_high_word(): """The high word of a wave64 mask (the ballot's upper 32 bits) is modelled as 0: a move of it is refused.""" chunks = [bytes.fromhex(w) for w in ('0000 84d4 9100 0200'.replace(' ', ''), # v_cmp_gt_i32 s[0:1], 17, v0 @@ -1094,6 +1110,7 @@ def check_controls(): check_mbcnt() check_lane_mask_data() check_lane_mask_high_word() + check_lane_mask_scalar_data() check_formatted_loads() a = translate('A') # This buffer-only compute program has no typed UAV declaration or image manifest entry. diff --git a/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py b/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py index 407fb8c9cc..146eee35e6 100644 --- a/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py +++ b/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py @@ -189,6 +189,12 @@ def run(*instructions, exec_known=True, kind=None): 'mask in the entry block': 'v_mov_b32 data operand s2 is a lane mask', 'median of three of a lane mask': 'v_med3_f32 reads lane mask s2 as numeric data', 'unsigned to float conversion of a mask': 'v_cvt_f32_u32 reads lane mask s2 as numeric data', + # A scalar op that reads a lane mask as a number is refused where it is lowered (before the read). + 'aliased load first word': 's_buffer_load_dwordx2 reads lane mask s2 as numeric data', + 'aliased load second word': 's_buffer_load_dwordx2 reads lane mask s2 as numeric data', + 'varying scalar buffer offset': 's_buffer_load_dword reads lane mask s2 as numeric data', + 'mask through scalar arithmetic': 's_add_u32 reads lane mask s2 as numeric data', + 'mask through scalar condition': 's_cmp_eq_u32 reads lane mask vcc_lo as numeric data', } for label, instructions in REFUSED: got = run(*instructions) diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 5fa8ac9f4a..d330456a82 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -1404,6 +1404,12 @@ def mesh_run(ins, at, what, patterns, env): # Vector ALU forms that take a lane mask as a per-lane select or carry, which the one-lane model handles. MASK_AWARE_VALU = frozenset({'v_cndmask_b32', 'v_cndmask_b16', 'v_readlane_b32', 'v_writelane_b32', 'v_readfirstlane_b32'}) MASK_AWARE_VALU_RE = re.compile(r'^v_cmpx?_|_co(_ci)?_|addc|subb|_co$|^v_mad_[iu]64_[iu]32$|^v_(cndmask|readlane|writelane|readfirstlane)_') +# Scalar ALU forms that move or combine lane masks bit by bit, which the one-lane model handles (s_ff1_i32_b32 refuses +# anything but exec_lo at the entry mask itself). Any other scalar op that +# reads a lane mask (s_cmp*, s_cmpk*, s_bcnt*, s_lshr, s_add ...) reads the whole wave's word as a number. +MASK_AWARE_SALU_RE = re.compile(r'^s_(mov|and|or|xor|andn2|orn2|nand|nor|xnor|not|cselect|wqm|cbranch|and_saveexec|or_saveexec|' + r'xor_saveexec|andn2_saveexec|orn2_saveexec|nand_saveexec|nor_saveexec|xnor_saveexec|' + r'ff1|bitset[01]|setpc|getpc|swappc|branch|waitcnt|endpgm|nop)(_|$)') VCC_KEYS = {'vcc', 'vcc_hi'} @@ -3191,6 +3197,8 @@ def lower(self, base, asm, ops, mods): if base.startswith('v_mov_b32'): # the high word of a wave64 mask has no one-lane value masks = [t for t in ops[1:] if reg_range(t) and reg_range(t)[0] == 's' and reg_range(t)[1] in getattr(self, 'mask_high', ())] + if base.startswith('s_') and not MASK_AWARE_SALU_RE.match(base): + masks = [t for t in (ops if base.startswith(('s_cmp', 's_bitcmp')) else ops[1:]) if self.is_lane_mask(t.strip())] result = self._lower(base, asm, ops, mods) if masks: raise Unsupported('%s reads lane mask %s as numeric data' % (base, masks[0])) From 075307bfa68b8d4540847537582e7acdd9df56ff Mon Sep 17 00:00:00 2001 From: tedin7 Date: Thu, 26 Nov 2026 03:33:20 +0100 Subject: [PATCH 05/19] startup: refuse a lane-mask word derived by a bitwise op when a vector compare reads it as a number --- tests/startup_gfx2hlsl_compute_check.py | 10 ++++++++++ tools/startup/gfx2hlsl.py | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/startup_gfx2hlsl_compute_check.py b/tests/startup_gfx2hlsl_compute_check.py index 3fd41f18fd..263e928782 100644 --- a/tests/startup_gfx2hlsl_compute_check.py +++ b/tests/startup_gfx2hlsl_compute_check.py @@ -835,6 +835,15 @@ def check_lane_mask_scalar_data(): translate('A', code=splice_words(compare, bytes.fromhex('6a7e0687'))).hlsl_cs() # s_and_b32 s6, vcc_lo, exec_lo +def check_lane_mask_derived_word(): + """A bitwise result of a lane mask is still a mask word: a later numeric read of it is refused.""" + compare = bytes.fromhex('8500027d') # v_cmp_lt_i32 vcc_lo, 5, v0 + mask_and = bytes.fromhex('6aff0687ff000000') # s_and_b32 s6, vcc_lo, 0xff + for use in ('060a887d', '060a0a4a'): # v_cmp_gt_u32 vcc_lo, s6, v5 ; v_add_nc_u32 v5, s6, v5 + refused('as numeric data', lambda: translate('A', code=splice_words(compare, mask_and, bytes.fromhex(use)))) + refused('as numeric data', lambda: translate('A', code=splice_words(compare, bytes.fromhex('6a0c0a7e')))) # v_cvt_f32_u32 v5, vcc_lo + + def check_lane_mask_high_word(): """The high word of a wave64 mask (the ballot's upper 32 bits) is modelled as 0: a move of it is refused.""" chunks = [bytes.fromhex(w) for w in ('0000 84d4 9100 0200'.replace(' ', ''), # v_cmp_gt_i32 s[0:1], 17, v0 @@ -1110,6 +1119,7 @@ def check_controls(): check_mbcnt() check_lane_mask_data() check_lane_mask_high_word() + check_lane_mask_derived_word() check_lane_mask_scalar_data() check_formatted_loads() a = translate('A') diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index d330456a82..cd0f90d733 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -1403,7 +1403,7 @@ def mesh_run(ins, at, what, patterns, env): MESH_VCC_BRANCHES = ('s_cbranch_vccz', 's_cbranch_vccnz') # Vector ALU forms that take a lane mask as a per-lane select or carry, which the one-lane model handles. MASK_AWARE_VALU = frozenset({'v_cndmask_b32', 'v_cndmask_b16', 'v_readlane_b32', 'v_writelane_b32', 'v_readfirstlane_b32'}) -MASK_AWARE_VALU_RE = re.compile(r'^v_cmpx?_|_co(_ci)?_|addc|subb|_co$|^v_mad_[iu]64_[iu]32$|^v_(cndmask|readlane|writelane|readfirstlane)_') +MASK_AWARE_VALU_RE = re.compile(r'_co(_ci)?_|addc|subb|_co$|^v_mad_[iu]64_[iu]32$|^v_(cndmask|readlane|writelane|readfirstlane)_') # Scalar ALU forms that move or combine lane masks bit by bit, which the one-lane model handles (s_ff1_i32_b32 refuses # anything but exec_lo at the entry mask itself). Any other scalar op that # reads a lane mask (s_cmp*, s_cmpk*, s_bcnt*, s_lshr, s_add ...) reads the whole wave's word as a number. From bd8dfdbc491fbee86a1ef16e4626cd58d387e7fb Mon Sep 17 00:00:00 2001 From: tedin7 Date: Thu, 26 Nov 2026 03:49:59 +0100 Subject: [PATCH 06/19] startup: refuse every numeric read of the high word of a wave64 lane mask in gfx2hlsl Only v_mov_b32 refused a read of s1 after a wave64 mask pair was written; v_add_nc_u32, v_cvt_f32_u32, v_and_b32, s_add_u32, s_cmp_eq_u32 and s_mov_b32 of it still lowered the high word as 0. Every vector or scalar op that takes the high word as a single-register operand now goes through the same refusal; the pair forms (select, saveexec) are unchanged. Test: check_lane_mask_high_word covers one form per class and keeps the non-mask SGPR control. --- tests/startup_gfx2hlsl_compute_check.py | 11 ++++++++++- tools/startup/gfx2hlsl.py | 26 ++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/tests/startup_gfx2hlsl_compute_check.py b/tests/startup_gfx2hlsl_compute_check.py index 263e928782..ce48b10d39 100644 --- a/tests/startup_gfx2hlsl_compute_check.py +++ b/tests/startup_gfx2hlsl_compute_check.py @@ -849,7 +849,16 @@ def check_lane_mask_high_word(): chunks = [bytes.fromhex(w) for w in ('0000 84d4 9100 0200'.replace(' ', ''), # v_cmp_gt_i32 s[0:1], 17, v0 '8004027d', # v_cmp_lt_i32 vcc, 0, v2 '6a008089')] # s_xor_b64 s[0:1], vcc, s[0:1] - refused('as numeric data', lambda: translate('A', code=splice_words(*chunks, bytes.fromhex('01020a7e')))) + for word in ('01020a7e', # v_mov_b32 v5, s1 + '010a0a4a', # v_add_nc_u32 v5, s1, v5 + '010c0a7e', # v_cvt_f32_u32 v5, s1 + '010a0a36', # v_and_b32 v5, s1, v5 + '01810780', # s_add_u32 s7, s1, 1 + '018006bf', # s_cmp_eq_u32 s1, 0 + '010387be'): # s_mov_b32 s7, s1 + refused('as numeric data', lambda: translate('A', code=splice_words(*chunks, bytes.fromhex(word)))) + # The compare alone leaves its high word unknown: refused too (earlier, by the unknown-SGPR rule). + refused('s1', lambda: translate('A', code=splice_words(chunks[0], bytes.fromhex('010a0a4a')))) assert 'v5 = ud0;' in translate('A', code=splice_words(*chunks, bytes.fromhex('04020a7e'))).hlsl_cs() diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index cd0f90d733..3e3cecdc4a 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -3184,6 +3184,21 @@ def forget_writes(self, name, ops, drop_since=True): del self.uniform_expr[v] return regs + def mark_mask_high(self, reg): + """A wave64 lane mask was written into the SGPR pair reg (reg_range): the one-lane model holds only the low word's + bit, the high word is the other lanes' bits, so no numeric read of it is lowered (see lower()).""" + if reg and reg[0] == 's' and reg[2] == reg[1] + 1: + self.mask_high = getattr(self, 'mask_high', set()) | {reg[1] + 1} + + def reads_mask_high(self, toks): + """First single-register operand that is the high word of a wave64 lane mask, else None.""" + high = getattr(self, 'mask_high', ()) + for t in toks: + r = reg_range(t.strip().lstrip('-|').rstrip('|')) + if r and r[0] == 's' and r[1] == r[2] and r[1] in high: + return t.strip() + return None + def lower(self, base, asm, ops, mods): """Lower one instruction; a vector ALU form that reads a lane mask as a number is refused. @@ -3194,11 +3209,11 @@ def lower(self, base, asm, ops, mods): masks = [] if base.startswith('v_') and not base.startswith('v_mov_b32') and base not in MASK_AWARE_VALU and not MASK_AWARE_VALU_RE.search(base): masks = [t for t in (o.strip().lstrip('-|').rstrip('|') for o in ops[1:]) if self.is_lane_mask(t)] - if base.startswith('v_mov_b32'): # the high word of a wave64 mask has no one-lane value - masks = [t for t in ops[1:] if reg_range(t) and reg_range(t)[0] == 's' - and reg_range(t)[1] in getattr(self, 'mask_high', ())] if base.startswith('s_') and not MASK_AWARE_SALU_RE.match(base): masks = [t for t in (ops if base.startswith(('s_cmp', 's_bitcmp')) else ops[1:]) if self.is_lane_mask(t.strip())] + if base.startswith(('v_', 's_')) and not masks: # the high word of a wave64 mask has no one-lane value + high = self.reads_mask_high(ops if base.startswith(('s_cmp', 's_bitcmp')) else ops[1:]) + masks = [high] if high else [] result = self._lower(base, asm, ops, mods) if masks: raise Unsupported('%s reads lane mask %s as numeric data' % (base, masks[0])) @@ -4258,6 +4273,7 @@ def scalar_select(self, ops, pair): if pair: self.masks.add(d) self.set_s(d + 1, '0u') + self.mark_mask_high(r if r[2] > r[1] else (r[0], d, d + 1)) def smem_soffset(self, tok, record_use=True): """Runtime expression for an SMEM scalar offset operand (s0..s105 or m0).""" @@ -6785,6 +6801,7 @@ def carry_out_op(self, ops, mods, kind): # The carry is a per-lane bit: never a uniform scalar, whatever the scratch name hides. self.set_s(sdst[1], predicate, uniform=False) self.masks.add(sdst[1]) + self.mark_mask_high(sdst) def op_v_add_co_u32(self, ops, mods): self.carry_out_op(ops, mods, 'add') @@ -6844,6 +6861,7 @@ def carry_in_out_op(self, ops, mods, kind): else: self.set_s(sdst[1], predicate, uniform=False) self.masks.add(sdst[1]) + self.mark_mask_high(sdst) def op_v_add_co_ci_u32(self, ops, mods): self.carry_in_out_op(ops, mods, 'add') @@ -7016,6 +7034,7 @@ def compare(self, base, ops, mods=()): d = reg[1] self.set_s(d, predicate) self.masks.add(d) + self.mark_mask_high(reg) return None m = re.fullmatch(r'v_cmp(x?)_(\w+?)_(f32|u32|i32|i16|u16)(_sdwa)?', base) @@ -7085,6 +7104,7 @@ def word(tok): d = reg[1] self.set_s(d, expr) self.masks.add(d) + self.mark_mask_high(reg) def export(self, asm): # disasm_rdna2 prints a compressed EN that is not a whole channel pair (0x0, 0x3, 0xC or 0xF) as From 7e52493e4e3546c2f8b0531bd19f905dc56d2700 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Thu, 26 Nov 2026 03:51:36 +0100 Subject: [PATCH 07/19] tests: name the earlier lane-mask refusal in the thread id base from a varying scalar case After merging main (#2455) that case meets the numeric-data refusal of v_lshl_add_u32 before the readfirstlane refusal; the expected text names it. --- tests/startup_gfx2hlsl_readfirstlane_uniform_check.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py b/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py index 2b5d3ab0d4..f078936867 100644 --- a/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py +++ b/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py @@ -292,7 +292,10 @@ def run(*instructions, exec_known=True, kind=None, group=None): ('thread id base rewritten after', (GID, 'v_add_nc_u32_e32 v0, s3, v0', WAVE, READ1), (64, 1, 1)), ('thread id base scaled by 32', ('v_lshl_add_u32 v0, s2, 5, v0', WAVE, READ1), (64, 1, 1))): got = run(*instructions, kind='cs', group=group) - check(f'{label} refused', got == REFUSAL, repr(got)) + # The compare's mask in s2 is refused where v_lshl_add_u32 reads it as a number, before the read. + expected = ('v_lshl_add_u32 reads lane mask s2 as numeric data' + if label == 'thread id base from a varying scalar' else REFUSAL) + check(f'{label} refused', got == expected, repr(got)) # Older probes may provide either no tracker or only part of the tracking state. for present in ((), ('uniform_v',), ('uniform_v', 'varying_s')): From 5d79c9aae197cbe32e654005c19c0ff970c086de Mon Sep 17 00:00:00 2001 From: tedin7 Date: Thu, 26 Nov 2026 03:59:04 +0100 Subject: [PATCH 08/19] startup: carry the lane-mask tag through every scalar copy and combination in gfx2hlsl The high word of a wave64 mask was tagged at five separate write sites, so a copy (s_mov_b64 s[8:9], s[0:1] or vcc) lost the tag and its numeric reads translated as 0. The tag now moves in one place, Stage.propagate_mask_tags, after every instruction: a mask-only scalar op (mov, bitwise, cselect, saveexec) gives its destination the tag of its sources, both words of a b64 pair; any other write clears it (set_s); every numeric read of a tagged word is refused in Stage.lower. Test: check_lane_mask_tag_propagation has one case per way a tagged word is made or copied plus a clearing control; disabling each rule fails it. Two readfirstlane_uniform controls (copied exec high word) now name the earlier refusal. --- tests/startup_gfx2hlsl_compute_check.py | 44 ++++++++++++++ ...up_gfx2hlsl_readfirstlane_uniform_check.py | 3 + tools/startup/gfx2hlsl.py | 59 +++++++++++++++---- 3 files changed, 93 insertions(+), 13 deletions(-) diff --git a/tests/startup_gfx2hlsl_compute_check.py b/tests/startup_gfx2hlsl_compute_check.py index ce48b10d39..639dd362f1 100644 --- a/tests/startup_gfx2hlsl_compute_check.py +++ b/tests/startup_gfx2hlsl_compute_check.py @@ -862,6 +862,49 @@ def check_lane_mask_high_word(): assert 'v5 = ud0;' in translate('A', code=splice_words(*chunks, bytes.fromhex('04020a7e'))).hlsl_cs() +LANE_MASK_TAG = { + 'zero_s1': '800381be', # s_mov_b32 s1, 0: the high word the one-lane model holds as 0 + 'cmp_pair': '000084d491000200', # v_cmp_gt_i32 s[0:1], 17, v0 (writes only the low word of the pair) + 'cselect_pair': 'c1808085', # s_cselect_b64 s[0:1], -1, 0 + 'cmp_vcc': '8004027d', # v_cmp_lt_i32 vcc, 0, v2 + 'saveexec': '6a2480be', # s_and_saveexec_b64 s[0:1], vcc + 'copy_pair': '000488be', # s_mov_b64 s[8:9], s[0:1] + 'copy_vcc': '6a0488be', # s_mov_b64 s[8:9], vcc + 'copy_copy': '08048abe', # s_mov_b64 s[10:11], s[8:9] + 'xor_pair': '6a008089', # s_xor_b64 s[0:1], vcc, s[0:1] + 'clear_s9': '800389be', # s_mov_b32 s9, 0: a plain write ends the tag + 'read_s1': '01020a7e', # v_mov_b32 v5, s1 + 'read_s9': '09020a7e', # v_mov_b32 v5, s9 + 'read_s11': '0b020a7e', # v_mov_b32 v5, s11 + 'add_s9': '09810780', # s_add_u32 s7, s9, 1 +} + + +def check_lane_mask_tag_propagation(): + """The mask tag is set where a mask is written and moves with every copy: one case per way a tagged word is made, each + read back through a numeric use (a no-op tag at any of them lets a case translate).""" + w = {k: bytes.fromhex(v) for k, v in LANE_MASK_TAG.items()} + cases = [ + ('cselect writes a pair', ['zero_s1', 'cselect_pair', 'read_s1']), + ('saveexec writes a pair', ['zero_s1', 'cmp_vcc', 'saveexec', 'read_s1']), + ('bitwise op writes a pair', ['zero_s1', 'cmp_pair', 'cmp_vcc', 'xor_pair', 'read_s1']), + ('b64 copy of a pair', ['cmp_pair', 'cmp_vcc', 'xor_pair', 'copy_pair', 'read_s9']), + ('b64 copy of vcc', ['cmp_vcc', 'copy_vcc', 'read_s9']), + ('copy of a copy', ['cmp_pair', 'cmp_vcc', 'xor_pair', 'copy_pair', 'copy_copy', 'read_s11']), + ('scalar use of a copy', ['cmp_pair', 'cmp_vcc', 'xor_pair', 'copy_pair', 'add_s9']), + ] + for label, steps in cases: + try: + translate('A', code=splice_words(*[w[s] for s in steps])) + except G.Unsupported as reason: + assert 'as numeric data' in str(reason), (label, str(reason)) + else: + raise AssertionError("%s accepted, expected a refusal naming as numeric data" % label) + # A plain write ends the tag: the copy is overwritten with a constant before the read. + assert 'v5 = ' in translate('A', code=splice_words(*[w[s] for s in ( + 'cmp_pair', 'cmp_vcc', 'xor_pair', 'copy_pair', 'clear_s9', 'read_s9')])).hlsl_cs() + + def splice_words(*chunks, at=STORE_A): return WORDS_A[:at] + b''.join(chunks) + WORDS_A[at:] @@ -1130,6 +1173,7 @@ def check_controls(): check_lane_mask_high_word() check_lane_mask_derived_word() check_lane_mask_scalar_data() + check_lane_mask_tag_propagation() check_formatted_loads() a = translate('A') # This buffer-only compute program has no typed UAV declaration or image manifest entry. diff --git a/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py b/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py index f078936867..67571adc9f 100644 --- a/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py +++ b/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py @@ -182,6 +182,9 @@ def run(*instructions, exec_known=True, kind=None, group=None): # These instructions refuse before v_readfirstlane_b32 is reached. EARLIER = { 'op_sel overwrite': 'modifier op_sel:[0,0]', + # The high word of a copied wave64 mask carries the mask tag: its numeric read is refused where it is lowered. + 'overlapping 64-bit move high word': 'v_mov_b32 reads lane mask s4 as numeric data', + 'high word of saved exec': 'v_mov_b32 reads lane mask s3 as numeric data', 'modified move overwrite': "v_mov_b32 with ['clamp']", 'mask from uniform compare': 'v_mov_b32 data operand s2 is a lane mask', 'vcc': 'v_mov_b32 data operand vcc_lo is a lane mask', diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 712781bf98..354cde6d85 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -1410,6 +1410,8 @@ def mesh_run(ins, at, what, patterns, env): MASK_AWARE_SALU_RE = re.compile(r'^s_(mov|and|or|xor|andn2|orn2|nand|nor|xnor|not|cselect|wqm|cbranch|and_saveexec|or_saveexec|' r'xor_saveexec|andn2_saveexec|orn2_saveexec|nand_saveexec|nor_saveexec|xnor_saveexec|' r'ff1|bitset[01]|setpc|getpc|swappc|branch|waitcnt|endpgm|nop)(_|$)') +# Scalar ops whose result is a lane mask when a source is one (a copy or a bitwise combination), unlike s_ff1 or s_bitset. +MASK_COPY_SALU_RE = re.compile(r'^s_(mov|and|or|xor|andn2|orn2|nand|nor|xnor|not|wqm)_b(32|64)$') VCC_KEYS = {'vcc', 'vcc_hi'} @@ -3501,11 +3503,48 @@ def forget_writes(self, name, ops, drop_since=True): del self.uniform_expr[v] return regs - def mark_mask_high(self, reg): - """A wave64 lane mask was written into the SGPR pair reg (reg_range): the one-lane model holds only the low word's - bit, the high word is the other lanes' bits, so no numeric read of it is lowered (see lower()).""" - if reg and reg[0] == 's' and reg[2] == reg[1] + 1: - self.mask_high = getattr(self, 'mask_high', set()) | {reg[1] + 1} + MASK_NAMES_LOW = ('vcc', 'vcc_lo', 'exec', 'exec_lo') + MASK_NAMES_HIGH = ('vcc', 'vcc_hi', 'exec', 'exec_hi') + + def mask_source_tags(self, base, ops): + """(low, high): whether a source operand of the instruction carries a lane-mask word. low = a mask's low word + (vcc/exec, an SGPR in self.masks, a pair that starts in one); high = the high word of a wave64 mask (vcc_hi, + exec_hi, a pair holding one, an SGPR in self.mask_high). Read before the instruction runs: it may overwrite + its own source.""" + dests = destinations(base, ops) + srcs = ops[len(dests):] if dests and ops[:len(dests)] == dests else list(ops) + masks, high_regs = getattr(self, 'masks', ()), getattr(self, 'mask_high', ()) + low = high = False + for tok in (t.strip().lstrip('-|').rstrip('|') for t in srcs): + low |= tok in self.MASK_NAMES_LOW + high |= tok in self.MASK_NAMES_HIGH and tok not in ('vcc_lo', 'exec_lo') + r = reg_range(tok) + if r and r[0] == 's': + start_tagged = r[1] in masks + low |= start_tagged + high |= any(n in high_regs for n in range(r[1], r[2] + 1)) or (start_tagged and r[2] > r[1]) + return low, high + + def propagate_mask_tags(self, base, ops, tags): + """After an instruction ran: the SGPRs it wrote carry the lane-mask tag of what they were made from. This is the one + place the tag moves. A compare or carry writes its single-word mask itself (the decoder + names a wave64 compare destination s0, not s[0:1]); a mask-only scalar op (mov, bitwise, cselect, saveexec) + copies the tag of its sources to its destination, both words of a b64 pair; any other write cleared the tag + already (set_s). self.masks holds the low word, self.mask_high the high word of a wave64 pair: no numeric read of + either is lowered (lower()).""" + low, high = tags + saveexec = base.startswith('s_') and 'saveexec' in base + scalar = bool(MASK_COPY_SALU_RE.match(base)) and (low or high) + pair_select = base.startswith('s_cselect') and base.endswith('b64') + if not (saveexec or scalar or pair_select): + return + for tok in destinations(base, ops): + r = reg_range(tok) + if not r or r[0] != 's': + continue + self.masks.add(r[1]) + if r[2] > r[1]: + self.mask_high = getattr(self, 'mask_high', set()) | {r[1] + 1} def reads_mask_high(self, toks): """First single-register operand that is the high word of a wave64 lane mask, else None.""" @@ -3531,9 +3570,11 @@ def lower(self, base, asm, ops, mods): if base.startswith(('v_', 's_')) and not masks: # the high word of a wave64 mask has no one-lane value high = self.reads_mask_high(ops if base.startswith(('s_cmp', 's_bitcmp')) else ops[1:]) masks = [high] if high else [] + tags = self.mask_source_tags(base, ops) result = self._lower(base, asm, ops, mods) if masks: raise Unsupported('%s reads lane mask %s as numeric data' % (base, masks[0])) + self.propagate_mask_tags(base, ops, tags) return result def _lower(self, base, asm, ops, mods): @@ -4379,9 +4420,6 @@ def known(tok): self.masks.add(d) if pair and base != 's_mov_b64': self.set_s(d + 1, '0u') - if is_mask: - # The modelled high word is 0, but the hardware's holds the other lanes' bits: no numeric read of it. - self.mask_high = getattr(self, 'mask_high', set()) | {d + 1} elif pair: self.set_s(d + 1, self.S(high) if high is not None else '0u', uniform=high_uniform) return None @@ -4614,7 +4652,6 @@ def scalar_select(self, ops, pair): if pair: self.masks.add(d) self.set_s(d + 1, '0u') - self.mark_mask_high(r if r[2] > r[1] else (r[0], d, d + 1)) def smem_soffset(self, tok, record_use=True): """Runtime expression for an SMEM scalar offset operand (s0..s105 or m0).""" @@ -7214,7 +7251,6 @@ def carry_out_op(self, ops, mods, kind): # The carry is a per-lane bit: never a uniform scalar, whatever the scratch name hides. self.set_s(sdst[1], predicate, uniform=False) self.masks.add(sdst[1]) - self.mark_mask_high(sdst) def op_v_add_co_u32(self, ops, mods): self.carry_out_op(ops, mods, 'add') @@ -7274,7 +7310,6 @@ def carry_in_out_op(self, ops, mods, kind): else: self.set_s(sdst[1], predicate, uniform=False) self.masks.add(sdst[1]) - self.mark_mask_high(sdst) def op_v_add_co_ci_u32(self, ops, mods): self.carry_in_out_op(ops, mods, 'add') @@ -7447,7 +7482,6 @@ def compare(self, base, ops, mods=()): d = reg[1] self.set_s(d, predicate) self.masks.add(d) - self.mark_mask_high(reg) return None m = re.fullmatch(r'v_cmp(x?)_(\w+?)_(f32|u32|i32|i16|u16)(_sdwa)?', base) @@ -7517,7 +7551,6 @@ def word(tok): d = reg[1] self.set_s(d, expr) self.masks.add(d) - self.mark_mask_high(reg) def export(self, asm): # disasm_rdna2 prints a compressed EN that is not a whole channel pair (0x0, 0x3, 0xC or 0xF) as From 12fd0ac847d61f91837ce661781f625581ed712b Mon Sep 17 00:00:00 2001 From: tedin7 Date: Thu, 26 Nov 2026 04:04:07 +0100 Subject: [PATCH 09/19] startup: keep a numeric lane-mask read lowered in a one-thread compute group A group of one thread has one participating lane per wave, so the lane bit is the whole mask and its high word is 0: the one-lane model is exact and dispatch case E (saveexec and exec_lo copies read as numbers) translates again. With more than one thread, or in any other stage, the numeric read stays refused. Test: check_lane_mask_single_thread_group covers both sides of the boundary for the low word and the wave64 high word; forcing the boundary either way fails the compute check or the dispatch cases. --- tests/startup_gfx2hlsl_compute_check.py | 16 ++++++++++++++++ tools/startup/gfx2hlsl.py | 9 ++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/tests/startup_gfx2hlsl_compute_check.py b/tests/startup_gfx2hlsl_compute_check.py index 639dd362f1..db40ca3b03 100644 --- a/tests/startup_gfx2hlsl_compute_check.py +++ b/tests/startup_gfx2hlsl_compute_check.py @@ -844,6 +844,21 @@ def check_lane_mask_derived_word(): refused('as numeric data', lambda: translate('A', code=splice_words(compare, bytes.fromhex('6a0c0a7e')))) # v_cvt_f32_u32 v5, vcc_lo +def check_lane_mask_single_thread_group(): + """A one-thread group has one participating lane per wave: its lane bit is the whole mask (high word 0), so the one-lane + model is exact and a numeric read stays lowered (dispatch case E). With two threads the same program is refused.""" + compare = bytes.fromhex('8500027d') # v_cmp_lt_i32 vcc_lo, 5, v0 + cvt = bytes.fromhex('6a0c0a7e') # v_cvt_f32_u32 v5, vcc_lo: the mask as a number + code = splice_words(compare, cvt) + assert 'v5 = ' in translate('A', code=code, group=(1, 1, 1)).hlsl_cs() + for group in ((2, 1, 1), (1, 2, 1), (1, 1, 2), (4, 1, 1)): + refused('as numeric data', lambda: translate('A', code=code, group=group)) + xor_chunks = [bytes.fromhex(w) for w in ('000084d491000200', '8004027d', '6a008089')] + high = splice_words(*xor_chunks, bytes.fromhex('01020a7e')) # v_mov_b32 v5, s1: the high word + assert 'v5 = ' in translate('A', code=high, group=(1, 1, 1)).hlsl_cs() + refused('as numeric data', lambda: translate('A', code=high, group=(2, 1, 1))) + + def check_lane_mask_high_word(): """The high word of a wave64 mask (the ballot's upper 32 bits) is modelled as 0: a move of it is refused.""" chunks = [bytes.fromhex(w) for w in ('0000 84d4 9100 0200'.replace(' ', ''), # v_cmp_gt_i32 s[0:1], 17, v0 @@ -1171,6 +1186,7 @@ def check_controls(): check_mbcnt() check_lane_mask_data() check_lane_mask_high_word() + check_lane_mask_single_thread_group() check_lane_mask_derived_word() check_lane_mask_scalar_data() check_lane_mask_tag_propagation() diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 354cde6d85..5cd6b9de89 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -3546,6 +3546,13 @@ def propagate_mask_tags(self, base, ops, tags): if r[2] > r[1]: self.mask_high = getattr(self, 'mask_high', set()) | {r[1] + 1} + def lane_mask_exact(self): + """True where the one-lane model of a wave mask is exact: a compute group of one thread has one participating lane + per wave, so the lane's bit is the whole mask and its high word is 0. Anywhere else the mask holds other lanes' + bits that a numeric read would see.""" + group = getattr(getattr(self, 'pipe', None), 'group', None) + return getattr(self, 'kind', None) == 'cs' and bool(group) and group[0] * group[1] * group[2] == 1 + def reads_mask_high(self, toks): """First single-register operand that is the high word of a wave64 lane mask, else None.""" high = getattr(self, 'mask_high', ()) @@ -3572,7 +3579,7 @@ def lower(self, base, asm, ops, mods): masks = [high] if high else [] tags = self.mask_source_tags(base, ops) result = self._lower(base, asm, ops, mods) - if masks: + if masks and not self.lane_mask_exact(): raise Unsupported('%s reads lane mask %s as numeric data' % (base, masks[0])) self.propagate_mask_tags(base, ops, tags) return result From f837dc29872f021f46db2af3c097760117ede958 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Thu, 26 Nov 2026 04:37:12 +0100 Subject: [PATCH 10/19] fix: lane-mask high word tag stays a high word, ends on overwrite, resets per pass A copy of a wave64 mask's high word was tagged as a low word (numeric use got '& 1'); a wide write over a tagged pair left the high-word tag (false refusal); the high-word set was not reset when a translation pass restarts. --- tests/startup_gfx2hlsl_compute_check.py | 15 +++++++++++++++ tools/startup/gfx2hlsl.py | 13 +++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/tests/startup_gfx2hlsl_compute_check.py b/tests/startup_gfx2hlsl_compute_check.py index db40ca3b03..c304f25d56 100644 --- a/tests/startup_gfx2hlsl_compute_check.py +++ b/tests/startup_gfx2hlsl_compute_check.py @@ -877,6 +877,20 @@ def check_lane_mask_high_word(): assert 'v5 = ud0;' in translate('A', code=splice_words(*chunks, bytes.fromhex('04020a7e'))).hlsl_cs() +def check_lane_mask_high_word_copy_and_overwrite(): + """A copy of a mask's high word is itself only a high word (no low-word tag, so no '& 1' on a numeric use in a one-thread + group), and a wide write over a tagged pair ends the tag (no false refusal of the new value).""" + save = bytes.fromhex('7e0494be') # s_mov_b64 s[20:21], exec + copy = bytes.fromhex('150396be') # s_mov_b32 s22, s21: the high word + use = bytes.fromhex('16821788') # s_or_b32 s23, s22, 2 + cvt = bytes.fromhex('170c0a7e') # v_cvt_f32_u32 v5, s23 + one = translate('A', code=splice_words(save, copy, use, cvt), group=(1, 1, 1)).hlsl_cs() + assert 's23 = s22 | 0x00000002u;' in one, one + refused('as numeric data', lambda: translate('A', code=splice_words(save, copy, use, cvt), group=(2, 1, 1))) + shifted = splice_words(save, bytes.fromhex('0407948f'), bytes.fromhex('15020a7e')) # s_lshl_b64 s[20:21], s[4:5], s7 ; v_mov_b32 v5, s21 + assert 'v5 = ' in translate('A', code=shifted, group=(4, 1, 1)).hlsl_cs() + + LANE_MASK_TAG = { 'zero_s1': '800381be', # s_mov_b32 s1, 0: the high word the one-lane model holds as 0 'cmp_pair': '000084d491000200', # v_cmp_gt_i32 s[0:1], 17, v0 (writes only the low word of the pair) @@ -1187,6 +1201,7 @@ def check_controls(): check_lane_mask_data() check_lane_mask_high_word() check_lane_mask_single_thread_group() + check_lane_mask_high_word_copy_and_overwrite() check_lane_mask_derived_word() check_lane_mask_scalar_data() check_lane_mask_tag_propagation() diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index a58859decf..93aaace882 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -2978,6 +2978,7 @@ def translate(self): self.derivative_sites = [] # (offset, name) of image_sample_b / image_get_lod, for the back-edge proof self.divergent_back_edges = [] # (target, branch offset) of divergent backward branches self.masks = set(init_masks) + self.mask_high = set() self.join_masks = set() # marked SGPRs whose mark is not proven on every path (unjoined) last_write = {} # SGPR -> offset of its last write in program order self.attrs = dict(init_attrs) @@ -3555,9 +3556,13 @@ def propagate_mask_tags(self, base, ops, tags): r = reg_range(tok) if not r or r[0] != 's': continue - self.masks.add(r[1]) - if r[2] > r[1]: + if r[2] > r[1]: # a pair: low word, and the high word it carries + self.masks.add(r[1]) self.mask_high = getattr(self, 'mask_high', set()) | {r[1] + 1} + elif low or saveexec or pair_select: + self.masks.add(r[1]) + if r[2] == r[1] and high and not (saveexec or pair_select): + self.mask_high = getattr(self, 'mask_high', set()) | {r[1]} def lane_mask_exact(self): """True where the one-lane model of a wave mask is exact: a compute group of one thread has one participating lane @@ -3592,6 +3597,10 @@ def lower(self, base, asm, ops, mods): masks = [high] if high else [] tags = self.mask_source_tags(base, ops) result = self._lower(base, asm, ops, mods) + for tok in destinations(base, ops): # an overwritten register no longer holds a mask's high word + r = reg_range(tok) + if r and r[0] == 's': + getattr(self, 'mask_high', set()).difference_update(range(r[1], r[2] + 1)) if masks and not self.lane_mask_exact(): raise Unsupported('%s reads lane mask %s as numeric data' % (base, masks[0])) self.propagate_mask_tags(base, ops, tags) From 240ae3cd07e02211a90023f2887e39cdff0139a2 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Thu, 26 Nov 2026 05:15:40 +0100 Subject: [PATCH 11/19] fix: tag the high word of a wave64 compare or carry-out pair A v_cmp or carry-out into an even SGPR pair is printed by its low word only, so a numeric read of the other 32 lanes' word (s5 after s[4:5]) translated as a user constant. tag_compare_pair marks s(N+1) as the mask's high word, in the one place the tags move; tests cover direct reads, pair and word copies, and a pre-compare read that stays translatable. --- tests/startup_gfx2hlsl_compute_check.py | 20 ++++++++++++++++++++ tools/startup/gfx2hlsl.py | 16 ++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/tests/startup_gfx2hlsl_compute_check.py b/tests/startup_gfx2hlsl_compute_check.py index 7a12c69e21..56aabd9e94 100644 --- a/tests/startup_gfx2hlsl_compute_check.py +++ b/tests/startup_gfx2hlsl_compute_check.py @@ -877,6 +877,25 @@ def check_lane_mask_high_word(): assert 'v5 = ud0;' in translate('A', code=splice_words(*chunks, bytes.fromhex('04020a7e'))).hlsl_cs() +def check_lane_mask_compare_pair(): + """A wave64 compare or carry-out into an even SGPR pair s[N:N+1] (the decoder names only sN) leaves the other 32 lanes in + s(N+1). Here s[4:5] are the two root constants, so s5 is a known register: a numeric read of it after the compare must + be refused, directly, through a copy of the pair, and through a copy of the high word.""" + reads = ('05020a7e', '050c0a7e', '050a0a36', '050a0a4a', # v_mov / v_cvt_f32_u32 / v_and / v_add with s5 + '05810781', '058006bf', '050387be') # s_add_i32 s7, s5, 1 / s_cmp_eq_u32 s5, 0 / s_mov_b32 s7, s5 + for write in ('040084d491000200', # v_cmp_gt_i32 s[4:5], 17, v0 + '06040fd700030200'): # v_add_co_u32 v6, s[4:5], v0, v1 + for read in reads: + refused('as numeric data', lambda write=write, read=read: translate('A', code=splice_words( + bytes.fromhex(write), bytes.fromhex(read)))) + for copy, use in (('040488be', '09810781'), # s_mov_b64 s[8:9], s[4:5]; s_add_i32 s7, s9, 1 + ('050388be', '08810781')): # s_mov_b32 s8, s5; s_add_i32 s7, s8, 1 + refused('as numeric data', lambda write=write, copy=copy, use=use: translate('A', code=splice_words( + bytes.fromhex(write), bytes.fromhex(copy), bytes.fromhex(use)))) + # before the compare s5 is the root constant: still a number the model has + translate('A', code=splice_words(bytes.fromhex('05810781'), bytes.fromhex('040084d491000200'))).hlsl_cs() + + def check_lane_mask_high_word_copy_and_overwrite(): """A copy of a mask's high word is itself only a high word (no low-word tag, so no '& 1' on a numeric use in a one-thread group), and a wide write over a tagged pair ends the tag (no false refusal of the new value).""" @@ -1200,6 +1219,7 @@ def check_controls(): check_mbcnt() check_lane_mask_data() check_lane_mask_high_word() + check_lane_mask_compare_pair() check_lane_mask_single_thread_group() check_lane_mask_high_word_copy_and_overwrite() check_lane_mask_derived_word() diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 5af3c5e3ff..3ab3655b12 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -3566,6 +3566,7 @@ def propagate_mask_tags(self, base, ops, tags): already (set_s). self.masks holds the low word, self.mask_high the high word of a wave64 pair: no numeric read of either is lowered (lower()).""" low, high = tags + self.tag_compare_pair(base, ops) saveexec = base.startswith('s_') and 'saveexec' in base scalar = bool(MASK_COPY_SALU_RE.match(base)) and (low or high) pair_select = base.startswith('s_cselect') and base.endswith('b64') @@ -3583,6 +3584,21 @@ def propagate_mask_tags(self, base, ops, tags): if r[2] == r[1] and high and not (saveexec or pair_select): self.mask_high = getattr(self, 'mask_high', set()) | {r[1]} + def tag_compare_pair(self, base, ops): + """A compare or carry-out into SGPR sN (the decoder prints a wave64 pair as s[N:N+1] by its low word only). In + wave64 the destination is an even-aligned pair and s(N+1) holds the other 32 lanes, which no one-lane value + has: tag s(N+1) as the high word. An odd sN is a wave32 destination (no high word); an even one that is a wave32 + destination loses the numeric reads of s(N+1), a refusal and never a wrong value.""" + if base.startswith('v_cmp') and not base.startswith('v_cmpx'): + tok = ops[0] if ops else '' + elif VOP3B_SDST.match(base) and len(ops) > 1: + tok = ops[1] + else: + return + r = reg_range(tok.strip()) + if r and r[0] == 's' and r[1] == r[2] and r[1] in self.masks and r[1] % 2 == 0: + self.mask_high = getattr(self, 'mask_high', set()) | {r[1] + 1} + def lane_mask_exact(self): """True where the one-lane model of a wave mask is exact: a compute group of one thread has one participating lane per wave, so the lane's bit is the whole mask and its high word is 0. Anywhere else the mask holds other lanes' From 4ee78032aab5e3fe0da4e2669e196d8cd85884df Mon Sep 17 00:00:00 2001 From: tedin7 Date: Thu, 26 Nov 2026 05:44:12 +0100 Subject: [PATCH 12/19] fix: keep the high-word tag off a neighbouring wave32 mask and off a carry-out destination tag_compare_pair does not tag s(N+1) when it already holds its own mask (a wave32 compare or carry next to it); the high-word read check skips the VOP3b carry-out operand, which is a destination. The mask-overwrite check uses s20 as the OR's second operand (s7 is the high word of the s6 compare in wave64) and gains pixel and vertex stage refusals of a mask read as a number. --- .../startup_gfx2hlsl_mask_overwrite_check.py | 27 ++++++++++++++++--- tools/startup/gfx2hlsl.py | 8 +++--- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/tests/startup_gfx2hlsl_mask_overwrite_check.py b/tests/startup_gfx2hlsl_mask_overwrite_check.py index dc3fcfaf6b..6dc037cc62 100644 --- a/tests/startup_gfx2hlsl_mask_overwrite_check.py +++ b/tests/startup_gfx2hlsl_mask_overwrite_check.py @@ -6,7 +6,7 @@ scalar logic op with a mask source is lowered as a mask op: its result is reduced to the lane's bit. The generic scalar path cleared the mark when it overwrote a register with ordinary data, but the other writers did not: after `v_cmp_eq_u32_e64 s6, v0, v1` and `s_buffer_load_dword s6, s[16:19], 0x0`, s6 holds a buffer word, yet -`s_or_b32 s8, s6, s7` was still lowered as a mask OR and cut to bit 0. Every scalar write through `set_s` now clears +`s_or_b32 s8, s6, s20` was still lowered as a mask OR and cut to bit 0. Every scalar write through `set_s` now clears the mark (the writers of a mask mark the register again after it), and `s_load` clears it on its destination registers, also in the forms that only record a descriptor symbol. @@ -66,11 +66,11 @@ def run(s, *texts): def or_value(s, s6, s7): - """Value of s8 after s_or_b32 s8, s6, s7, from the emitted line.""" + """Value of s8 after s_or_b32 s8, s6, s20, from the emitted line.""" s.lines = [] - run(s, 's_or_b32 s8, s6, s7') + run(s, 's_or_b32 s8, s6, s20') line = next(l for l in s.lines if l.startswith('s8 = ')) - return Expr(line[5:-1]).run({'s6': s6, 's7': s7})[0], line + return Expr(line[5:-1]).run({'s6': s6, 's20': s7})[0], line COMPARE = 'v_cmp_eq_u32_e64 s6, v0, v1' @@ -122,6 +122,25 @@ def main(): continue check('control: %s after the load marks s6 again' % writer, 6 in s.masks, (s.masks, s.lines)) + # A lane mask read as a number is refused in the pixel and vertex stages too (the wave's ballot word is every lane's + # bit, the model holds one): the compare into vcc_lo then v_cvt_f32_u32, and the compare into s0 then s_add_i32. + for kind in ('ps', 'vs'): + for compare, use in (('v_cmp_lt_u32_e32 vcc_lo, 5, v0', 'v_cvt_f32_u32_e32 v1, vcc_lo'), + ('v_cmp_lt_u32_e64 s0, 5, v0', 's_add_i32 s1, s0, 0')): + s = stage() + s.kind = kind + run(s, compare) + try: + run(s, use) + check('%s: %s after %s is refused' % (kind, use, compare), False, s.lines) + except G.Unsupported as why: + check('%s: %s after %s is refused' % (kind, use, compare), 'as numeric data' in str(why), str(why)) + s = stage() + s.kind = kind + run(s, 'v_cmp_lt_u32_e64 s0, 5, v0', 's_mov_b32 s0, 7') # overwritten: a number again + run(s, 's_add_i32 s1, s0, 0') + check('%s: a mask register overwritten by a constant is a number again' % kind, 0 not in s.masks, s.masks) + if failures: print('FAILED: %d check(s)' % failures) return 1 diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 39dd9c1371..f008d5b8de 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -3605,7 +3605,8 @@ def tag_compare_pair(self, base, ops): """A compare or carry-out into SGPR sN (the decoder prints a wave64 pair as s[N:N+1] by its low word only). In wave64 the destination is an even-aligned pair and s(N+1) holds the other 32 lanes, which no one-lane value has: tag s(N+1) as the high word. An odd sN is a wave32 destination (no high word); an even one that is a wave32 - destination loses the numeric reads of s(N+1), a refusal and never a wrong value.""" + destination loses the numeric reads of s(N+1), a refusal and never a wrong value. If s(N+1) already holds its own lane + mask (a wave32 compare or carry in the neighbouring register) the destination is not a pair: nothing is tagged.""" if base.startswith('v_cmp') and not base.startswith('v_cmpx'): tok = ops[0] if ops else '' elif VOP3B_SDST.match(base) and len(ops) > 1: @@ -3613,7 +3614,7 @@ def tag_compare_pair(self, base, ops): else: return r = reg_range(tok.strip()) - if r and r[0] == 's' and r[1] == r[2] and r[1] in self.masks and r[1] % 2 == 0: + if r and r[0] == 's' and r[1] == r[2] and r[1] in self.masks and r[1] % 2 == 0 and r[1] + 1 not in self.masks: self.mask_high = getattr(self, 'mask_high', set()) | {r[1] + 1} def lane_mask_exact(self): @@ -3645,7 +3646,8 @@ def lower(self, base, asm, ops, mods): if base.startswith('s_') and not MASK_AWARE_SALU_RE.match(base): masks = [t for t in (ops if base.startswith(('s_cmp', 's_bitcmp')) else ops[1:]) if self.is_lane_mask(t.strip())] if base.startswith(('v_', 's_')) and not masks: # the high word of a wave64 mask has no one-lane value - high = self.reads_mask_high(ops if base.startswith(('s_cmp', 's_bitcmp')) else ops[1:]) + skip = 2 if VOP3B_SDST.match(base) else 1 # the carry-out of a VOP3b form is a destination, not a read + high = self.reads_mask_high(ops if base.startswith(('s_cmp', 's_bitcmp')) else ops[skip:]) masks = [high] if high else [] tags = self.mask_source_tags(base, ops) result = self._lower(base, asm, ops, mods) From 5bb8bf71220112f0d0a0650375f415eed712b402 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Thu, 26 Nov 2026 05:58:58 +0100 Subject: [PATCH 13/19] fix: a copy of plain data clears the lane-mask tag of its destination A scalar mask-copy op whose sources carry no tag left the old tag on its destination when an earlier path or a loop back edge had set it, so a later s_load through s[14:15] was refused as a read of a lane mask. The destination words now drop the tag; a loop fixture checks the load translates and that a copy of the mask itself is still refused. --- tests/startup_gfx2hlsl_compute_check.py | 16 ++++++++++++++++ tools/startup/gfx2hlsl.py | 6 ++++++ 2 files changed, 22 insertions(+) diff --git a/tests/startup_gfx2hlsl_compute_check.py b/tests/startup_gfx2hlsl_compute_check.py index 079624ec99..de49bfe32c 100644 --- a/tests/startup_gfx2hlsl_compute_check.py +++ b/tests/startup_gfx2hlsl_compute_check.py @@ -971,6 +971,21 @@ def check_lane_mask_compare_pair(): translate('A', code=splice_words(bytes.fromhex('05810781'), bytes.fromhex('040084d491000200'))).hlsl_cs() +def check_lane_mask_copy_of_data_clears_tag(): + """A copy of plain data into a register that held a lane mask on an earlier path (a loop back edge) leaves no mask tag: + `s_load` through that pair is not a read of a mask. A copy of the mask itself is refused too.""" + loop = bytes.fromhex('0e00c4d483000200' # v_cmp_gt_u32 s14, 3, v0 + '800394be' # s_mov_b32 s20, 0 + '06038ebe' # L: s_mov_b32 s14, s6 (the table pointer) + '80038fbe' # s_mov_b32 s15, 0 + '070608f4000000fa' # s_load_dwordx4 s[24:27], s[14:15], 0x0 + '7fc08cbf' '14811481' '14830abf' # s_waitcnt; s_add_i32 s20, s20, 1; s_cmp_lt_u32 s20, 3 + 'f7ff85bf') # s_cbranch_scc1 L + translate('A', code=splice_words(loop)).hlsl_cs() + mask_copy = loop.replace(bytes.fromhex('06038ebe'), bytes.fromhex('0e038ebe')) # s_mov_b32 s14, s14: the mask itself + refused('s_load through s14', lambda: translate('A', code=splice_words(mask_copy)).hlsl_cs()) + + def check_lane_mask_high_word_copy_and_overwrite(): """A copy of a mask's high word is itself only a high word (no low-word tag, so no '& 1' on a numeric use in a one-thread group), and a wide write over a tagged pair ends the tag (no false refusal of the new value).""" @@ -1295,6 +1310,7 @@ def check_controls(): check_lane_mask_data() check_lane_mask_high_word() check_lane_mask_compare_pair() + check_lane_mask_copy_of_data_clears_tag() check_lane_mask_single_thread_group() check_lane_mask_high_word_copy_and_overwrite() check_lane_mask_derived_word() diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 4259313211..f70b1b56e7 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -3697,6 +3697,12 @@ def propagate_mask_tags(self, base, ops, tags): low, high = tags self.tag_compare_pair(base, ops) saveexec = base.startswith('s_') and 'saveexec' in base + if MASK_COPY_SALU_RE.match(base) and not (low or high): + # a copy of plain data: the destination no longer holds a lane mask, whatever an earlier path or join left + for tok in destinations(base, ops): + r = reg_range(tok) + if r and r[0] == 's': + self.masks.difference_update(range(r[1], r[2] + 1)) scalar = bool(MASK_COPY_SALU_RE.match(base)) and (low or high) pair_select = base.startswith('s_cselect') and base.endswith('b64') if not (saveexec or scalar or pair_select): From 1ec4094e588c0b75b5c1c95d60e79a8b4060d34b Mon Sep 17 00:00:00 2001 From: tedin7 Date: Thu, 26 Nov 2026 06:09:48 +0100 Subject: [PATCH 14/19] test: keep the bfm_b64 uniformity check independent of the wave64 mask-word rule --- tests/startup_gfx2hlsl_bfm_b64_check.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/startup_gfx2hlsl_bfm_b64_check.py b/tests/startup_gfx2hlsl_bfm_b64_check.py index eefb41baa3..b7185e6e1a 100644 --- a/tests/startup_gfx2hlsl_bfm_b64_check.py +++ b/tests/startup_gfx2hlsl_bfm_b64_check.py @@ -373,6 +373,9 @@ def __init__(self): def run(self, *instructions): for asm in instructions: self.one(asm.split()[0], asm) + # The exec copy's high word is a wave64 mask word (a numeric read of it is refused, #2702); this check is about + # the uniformity of the sources, which s3 keeps as a varying number, so the mask-word tag is dropped here. + self.mask_high = set() return self From 745d2c34c761ead6f6755f40ebacedd006dc0417 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Thu, 26 Nov 2026 06:34:16 +0100 Subject: [PATCH 15/19] fix: a 64-bit copy of plain data keeps the lane-mask tag scalar() lowers every b64 move as a mask copy (lane bit only), so clearing the tag on a pair copy of plain data let its truncated low word be read as a number (v_mov of s20 after s_mov_b64 s[20:21], s[4:5] stored 1, not the root constant). Only a 32-bit copy of plain data clears the tag now. --- tests/startup_gfx2hlsl_compute_check.py | 12 ++++++++++++ tools/startup/gfx2hlsl.py | 8 +++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/tests/startup_gfx2hlsl_compute_check.py b/tests/startup_gfx2hlsl_compute_check.py index de49bfe32c..17e881e812 100644 --- a/tests/startup_gfx2hlsl_compute_check.py +++ b/tests/startup_gfx2hlsl_compute_check.py @@ -986,6 +986,17 @@ def check_lane_mask_copy_of_data_clears_tag(): refused('s_load through s14', lambda: translate('A', code=splice_words(mask_copy)).hlsl_cs()) +def check_lane_mask_pair_copy_of_data_stays_refused(): + """A 64-bit copy of plain data (root constants s[4:5]) is lowered as a mask copy (the lane bit only), so its low word + must not be read as a number in a multi-thread group: the store would write 1, not the root constant.""" + copy = bytes.fromhex('040494be') # s_mov_b64 s[20:21], s[4:5] + use = bytes.fromhex('14020a7e') # v_mov_b32 v5, s20 + refused('is a lane mask', lambda: translate('A', code=splice_words(copy, use), group=(2, 1, 1)).hlsl_cs()) + # a 32-bit copy of the same plain word is a full-word copy and stays readable + word = bytes.fromhex('040394be') # s_mov_b32 s20, s4 + assert 'v5 = s20' in translate('A', code=splice_words(word, use), group=(2, 1, 1)).hlsl_cs() + + def check_lane_mask_high_word_copy_and_overwrite(): """A copy of a mask's high word is itself only a high word (no low-word tag, so no '& 1' on a numeric use in a one-thread group), and a wide write over a tagged pair ends the tag (no false refusal of the new value).""" @@ -1311,6 +1322,7 @@ def check_controls(): check_lane_mask_high_word() check_lane_mask_compare_pair() check_lane_mask_copy_of_data_clears_tag() + check_lane_mask_pair_copy_of_data_stays_refused() check_lane_mask_single_thread_group() check_lane_mask_high_word_copy_and_overwrite() check_lane_mask_derived_word() diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 40454ca8c7..1029047888 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -3700,11 +3700,13 @@ def propagate_mask_tags(self, base, ops, tags): self.tag_compare_pair(base, ops) saveexec = base.startswith('s_') and 'saveexec' in base if MASK_COPY_SALU_RE.match(base) and not (low or high): - # a copy of plain data: the destination no longer holds a lane mask, whatever an earlier path or join left + # a 32-bit copy of plain data: the destination no longer holds a lane mask, whatever an earlier path or join + # left. A 64-bit pair copy keeps its tag: scalar() lowers every b64 move as a mask copy (the lane bit only), + # so the truncated word must stay refused as numeric data. for tok in destinations(base, ops): r = reg_range(tok) - if r and r[0] == 's': - self.masks.difference_update(range(r[1], r[2] + 1)) + if r and r[0] == 's' and r[2] == r[1]: + self.masks.discard(r[1]) scalar = bool(MASK_COPY_SALU_RE.match(base)) and (low or high) pair_select = base.startswith('s_cselect') and base.endswith('b64') if not (saveexec or scalar or pair_select): From 44586d0b715089e5b6e35aab599bccde6d623658 Mon Sep 17 00:00:00 2001 From: c1 Date: Thu, 26 Nov 2026 06:53:03 +0100 Subject: [PATCH 16/19] test: the lane-mask-in-chain readfirstlane control now meets the mask rule first --- tests/startup_compute_readfirstlane_check.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/startup_compute_readfirstlane_check.py b/tests/startup_compute_readfirstlane_check.py index 486de594c8..5c3eaf42b7 100644 --- a/tests/startup_compute_readfirstlane_check.py +++ b/tests/startup_compute_readfirstlane_check.py @@ -132,9 +132,10 @@ def pack_hex(*ws): ('integer chain from the thread id', [(pack_hex(*vop1('v_mov_b32_e32', 'v10', 's27')), pack_hex(*vop1('v_mov_b32_e32', 'v10', 'v0')))], 'v_readfirstlane_b32 requires a proven uniform source'), + # the lane-mask read as a number is refused first (#2702); the chain is not reached ('integer chain with a lane-mask scalar', [(pack_hex(*vop2('v_add_nc_u32_e32', 'v10', 's27', 'v10')), pack_hex(*vop2('v_add_nc_u32_e32', 'v10', 'vcc_lo', 'v10')))], - 'v_readfirstlane_b32 requires a proven uniform source'), + 'v_add_nc_u32 reads lane mask vcc_lo as numeric data'), ) From 7d3472d8d29782d305b61062463b478db0541891 Mon Sep 17 00:00:00 2001 From: c1 Date: Thu, 26 Nov 2026 07:21:44 +0100 Subject: [PATCH 17/19] fix: s_wqm keeps the lane-mask tag of its destination s_wqm is a no-op in the lowering, so the plain-data cleanup must not clear the tag of a mask it nominally rewrites. --- tests/startup_gfx2hlsl_compute_check.py | 10 ++++++++++ tools/startup/gfx2hlsl.py | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/startup_gfx2hlsl_compute_check.py b/tests/startup_gfx2hlsl_compute_check.py index be1836594f..f5a4ece27c 100644 --- a/tests/startup_gfx2hlsl_compute_check.py +++ b/tests/startup_gfx2hlsl_compute_check.py @@ -1008,6 +1008,15 @@ def check_lane_mask_pair_copy_of_data_stays_refused(): assert 'v5 = s20' in translate('A', code=splice_words(word, use), group=(2, 1, 1)).hlsl_cs() +def check_lane_mask_wqm_keeps_tag(): + """s_wqm_b32 is a no-op here (it writes nothing), so a mask it nominally rewrites from plain data keeps its tag: + the v_mov after it still reads a lane mask in a multi-thread group.""" + cmp_ = bytes.fromhex('140084d491000200') # v_cmp_gt_i32_e64 s20, 17, v0 + wqm = bytes.fromhex('800994be') # s_wqm_b32 s20, 0 + use = bytes.fromhex('14020a7e') # v_mov_b32 v5, s20 + refused('is a lane mask', lambda: translate('A', code=splice_words(cmp_, wqm, use), group=(4, 1, 1)).hlsl_cs()) + + def check_lane_mask_high_word_copy_and_overwrite(): """A copy of a mask's high word is itself only a high word (no low-word tag, so no '& 1' on a numeric use in a one-thread group), and a wide write over a tagged pair ends the tag (no false refusal of the new value).""" @@ -1354,6 +1363,7 @@ def check_controls(): check_lane_mask_compare_pair() check_lane_mask_copy_of_data_clears_tag() check_lane_mask_pair_copy_of_data_stays_refused() + check_lane_mask_wqm_keeps_tag() check_lane_mask_single_thread_group() check_lane_mask_high_word_copy_and_overwrite() check_lane_mask_derived_word() diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index b857338c3a..a9c3f3e1b6 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -3901,7 +3901,8 @@ def propagate_mask_tags(self, base, ops, tags): low, high = tags self.tag_compare_pair(base, ops) saveexec = base.startswith('s_') and 'saveexec' in base - if MASK_COPY_SALU_RE.match(base) and not (low or high): + if MASK_COPY_SALU_RE.match(base) and not base.startswith('s_wqm') and not (low or high): + # (s_wqm is a no-op in _lower(): it writes nothing, so the old mask stays in its destination.) # a 32-bit copy of plain data: the destination no longer holds a lane mask, whatever an earlier path or join # left. A 64-bit pair copy keeps its tag: scalar() lowers every b64 move as a mask copy (the lane bit only), # so the truncated word must stay refused as numeric data. From 0cd845d7871b8ffddf9408cbd8a440e4c22b52b6 Mon Sep 17 00:00:00 2001 From: c1 Date: Thu, 26 Nov 2026 08:50:26 +0100 Subject: [PATCH 18/19] chore: advance the startup shader cache revision to 23 / 23cs #2702 now refuses programs that main translates (a lane mask read as a number), so translations cached under the old revision would be reused with wrong HLSL. Runtime, offline keys, tests and docs spell the new revision; the pinned known-answer keys are recomputed with the independent hash loops the tests already carry. --- docs/startup-runtime.md | 2 +- src/startup_stubs/d3d12_draw.inl | 4 ++-- tests/startup_compute_request_offline_check.py | 4 ++-- tests/startup_gfx2hlsl_buffer_store_check.py | 2 +- tests/startup_gfx2hlsl_const_copy_join_check.py | 2 +- tests/startup_gfx2hlsl_fract_check.py | 4 ++-- tests/startup_indirect_draw_indexed.cpp | 2 +- tests/startup_packed_position_provenance_check.py | 2 +- tests/startup_resource_retire_title.cpp | 2 +- tests/startup_shader_cache_key_check.py | 6 +++--- tools/startup/gfx2hlsl.py | 4 ++-- 11 files changed, 17 insertions(+), 17 deletions(-) diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 8d821d3965..7adaac137f 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -208,7 +208,7 @@ The translation is refused when: - either header has another size; - the code reads an attribute the table does not list. -Graphics and compute use cache revisions `x2w-startup-shader-22` and `x2w-startup-shader-22cs`. +Graphics and compute use cache revisions `x2w-startup-shader-23` and `x2w-startup-shader-23cs`. Revision 22 invalidates compute shaders that assumed the entry EXEC mask at a load reached on a later loop iteration after an EXEC write. The two cache keys advance together. Revision 21 refuses a compute lane-mask form that revision 20 translated (`s_and_b32 M, X, exec_lo` with X data). diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 22a8ca3132..7ec2cc7245 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -353,7 +353,7 @@ Translation* cached_translation(const void* a, std::size_t a_bytes, const void* { // Keep pre-fix desktop bytecode and manifests out of this ABI's cache. // tools/startup/gfx2hlsl.py hashes the same bytes, without a trailing NUL. - constexpr char revision[] = "x2w-startup-shader-22"; + constexpr char revision[] = "x2w-startup-shader-23"; const u64 seed = fnv_bytes(revision, sizeof(revision) - 1); u64 key = fnv_bytes(root_blob.data(), root_blob.size(), fnv_bytes(b, b_bytes, fnv_bytes(a, a_bytes, seed))); // A stream's input layout (stream_input_layout) feeds the vertex fetch, so it is part of the key. @@ -474,7 +474,7 @@ u64 publish_compute_request(const void* desc) std::vector fields(sizeof(words)); words[0] = words[1] = words[3] = words[5] = words[7] = words[9] = words[10] = 0; std::memcpy(fields.data(), words, sizeof(words)); - constexpr char compute_revision[] = "x2w-startup-shader-22cs"; // tools/startup/gfx2hlsl.py compute_key + constexpr char compute_revision[] = "x2w-startup-shader-23cs"; // tools/startup/gfx2hlsl.py compute_key u64 key = fnv_bytes(compute_revision, sizeof(compute_revision) - 1); const std::vector* order[] = {&code, &data, &root_blob, &fields, &state_a, &state_b}; for (const auto* part : order) key = fnv_part(*part, key); diff --git a/tests/startup_compute_request_offline_check.py b/tests/startup_compute_request_offline_check.py index cb5c4c0f58..415dcc7f41 100644 --- a/tests/startup_compute_request_offline_check.py +++ b/tests/startup_compute_request_offline_check.py @@ -25,12 +25,12 @@ DESC = struct.pack('<12Q', 0, 0, 24, 0, 20, 0, len(CODE), 0, len(DATA), 0, 0, 11) STATE_A = struct.pack('<3Q', 1, 0x2c, 0xfff0000000000003) STATE_B = struct.pack('<5I', 0x1234abcd, 0xffff0000, 0, 0, 0) -KEY = 0x5159f6a85d77858e # framed key at revision 22cs +KEY = 0x76dc3fbef14b59e7 # framed key at revision 23cs def framed_key(): h = 1469598103934665603 - for b in b'x2w-startup-shader-22cs': + for b in b'x2w-startup-shader-23cs': h = ((h ^ b) * 1099511628211) & 0xffffffffffffffff for part in (CODE, DATA, ROOT, DESC, STATE_A, STATE_B): for b in struct.pack(' translated_pipeline(ID3D12Device* guest, ID3D12RootSignature* r bool needs_start, bool table_buffer = false, bool indexed = false, bool rect = false) { // The opaque guest programs identify synthetic cache entries. The actual bound shaders are source-built DXBC. - constexpr char revision[] = "x2w-startup-shader-22"; + constexpr char revision[] = "x2w-startup-shader-23"; static std::uint32_t vertex_ids[]{0xe0000101, 0xe0000102, 0xe0000103}; static std::uint32_t pixel_ids[]{0xe0000202, 0xe0000203, 0xe0000204}; auto& vertex_id = vertex_ids[indexed ? 2 : needs_start]; diff --git a/tests/startup_packed_position_provenance_check.py b/tests/startup_packed_position_provenance_check.py index 03b27c1950..ff87a03554 100644 --- a/tests/startup_packed_position_provenance_check.py +++ b/tests/startup_packed_position_provenance_check.py @@ -206,7 +206,7 @@ def refused(label, cache, key, reason): cache = Path(tmp) bare = G.pipeline_key(VS_WORDS, ps_words(4), root_blob()) check('without a description the key is unchanged', - bare == G.fnv(root_blob(), G.fnv(ps_words(4), G.fnv(VS_WORDS, G.fnv(b'x2w-startup-shader-22'))))) + bare == G.fnv(root_blob(), G.fnv(ps_words(4), G.fnv(VS_WORDS, G.fnv(b'x2w-startup-shader-23'))))) keys = [put_request(cache, ps_words(4), masks) for masks in (TARGET, SAME_V4, DECOY)] check('same stages, root and layout with three mask pairs: three request identities', len(set(keys)) == 3 and bare not in keys diff --git a/tests/startup_resource_retire_title.cpp b/tests/startup_resource_retire_title.cpp index 4e02039b3a..93422b8335 100644 --- a/tests/startup_resource_retire_title.cpp +++ b/tests/startup_resource_retire_title.cpp @@ -407,7 +407,7 @@ constexpr UINT draw_side = 4; // 4x4 R8G8B8A8_UNORM target constexpr UINT draw_covered = 3; // scissor leaves the last row and column untouched as the sentinel constexpr int draw_seed = 77; constexpr UINT draw_clear_pixel = 0xffff00ffu; // ClearRenderTargetView({1, 0, 1, 1}) in R8G8B8A8_UNORM -constexpr char draw_revision[] = "x2w-startup-shader-22"; // d3d12_draw.inl cached_translation +constexpr char draw_revision[] = "x2w-startup-shader-23"; // d3d12_draw.inl cached_translation constexpr char draw_vertex_shader[] = R"( struct Varying { float4 position : SV_Position; }; diff --git a/tests/startup_shader_cache_key_check.py b/tests/startup_shader_cache_key_check.py index 670adeb669..f7c61c8c10 100644 --- a/tests/startup_shader_cache_key_check.py +++ b/tests/startup_shader_cache_key_check.py @@ -49,10 +49,10 @@ def check_request(blobs, key, diagnostic=None, rtv_request=None, layout=None): def main(): # Independently executed C++ runtime fnv_bytes/cached_translation known-answer vector. - # Revision x2w-startup-shader-22 excludes NUL. Stored order is a, b, root, then optional input layout. + # Revision x2w-startup-shader-23 excludes NUL. Stored order is a, b, root, then optional input layout. vector = (b"\x00VS\xff", b"PS\x00\x80", b"RTS0\x00") - assert gfx2hlsl.pipeline_key(*vector) == 0x30009a5d8295d679 - assert gfx2hlsl.pipeline_key(*vector, b"ia\x00\xfe") == 0xdae7d34c534837e5 + assert gfx2hlsl.pipeline_key(*vector) == 0x5b4228f0459cf6fc + assert gfx2hlsl.pipeline_key(*vector, b"ia\x00\xfe") == 0x7f467f3465edf080 blobs = (VERTEX, PIXEL, ROOT) canonical = gfx2hlsl.pipeline_key(*blobs) check_request(blobs, canonical) diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 6f61e1676c..60074f178c 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -10174,7 +10174,7 @@ def pipeline_key(vs_code, ps_code, root_blob, layout=b'', ps_inputs=None): SPI_PS_INPUT_ENA and SPI_PS_INPUT_ADDR as little-endian u32. Two pipelines with the same stages, root signature and input layout but different masks are then two requests with their own results. Without a description the key is unchanged.""" - key = fnv(root_blob, fnv(ps_code, fnv(vs_code, fnv(b'x2w-startup-shader-22')))) + key = fnv(root_blob, fnv(ps_code, fnv(vs_code, fnv(b'x2w-startup-shader-23')))) key = fnv(layout, key) if layout else key return key if ps_inputs is None else fnv(b'psin1' + struct.pack(' Date: Thu, 26 Nov 2026 08:53:03 +0100 Subject: [PATCH 19/19] test: a lane-mask OR source meets the mask rule before the uniformity rule --- tests/startup_gfx2hlsl_readfirstlane_uniform_check.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py b/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py index a0e9119ef8..9598b19861 100644 --- a/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py +++ b/tests/startup_gfx2hlsl_readfirstlane_uniform_check.py @@ -355,7 +355,9 @@ def run(*instructions, exec_known=True, kind=None, group=None): ('signed field', ('v_bfe_i32 v2, v0, 2, 3', SHIFT1, READ1), 8), ('unknown group for a masked field', (AND_OR, SHIFT, READ1), None)): got = run(*instructions, kind='cs', group=(size, 1, 1) if size else None) - check(f'{label} refused', got == REFUSAL, repr(got)) + # a lane mask as the OR source meets the mask rule (#2702) before the uniformity rule: either refusal keeps it refused + mask_first = label == 'predicate as OR source' and got == 'v_and_or_b32 reads lane mask s2 as numeric data' + check(f'{label} refused', got == REFUSAL or mask_first, repr(got)) # Older probes may provide either no tracker or only part of the tracking state. for present in ((), ('uniform_v',), ('uniform_v', 'varying_s')):