Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/experiments/startup-gfx2hlsl-cselect-descnum.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ Nothing reads that symbol as a value:
refuses with `descriptor operand s[N:M]: sK holds a word joined at a branch target` (`Stage.joined_word`). A join
of different symbols leaves only that mark, so a select that reaches a descriptor word on one path, or through a
loop back edge, would otherwise be dropped. This also refuses compute joins that bring two different non-select
words there. Main translates those, ignoring the difference, as in #2901.
words there. Main translates those, ignoring the difference, as in #2901. A join where every path brings the
same word of the structured-to-raw conversion (stride cleared, byte count, OOB_SELECT 3) keeps it
(`raw_conversion_word`); `tests/startup_gfx2hlsl_load_desc_check.py` has the kept and refused joins.
- The select still refuses with `scc from descriptor data in s_cselect_b32` when its SCC came from an ALU
instruction on descriptor data, because that SCC is not emitted.

Expand Down
66 changes: 66 additions & 0 deletions tests/startup_gfx2hlsl_load_desc_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,72 @@ def check_refusals():
except G.Unsupported as e:
assert 'buffer load through a modified descriptor' in str(e), f'unexpected refusal: {e}'

# 6b. Joins after the conversion. Both arms of a uniform branch convert the load descriptor and join at the load:
# every path brings the same converted words, so the join keeps them and the load translates as on one path.
# A join that mixes a converted word with another word refuses, and so does a loop whose back edge rewrites one.
def sopp(op, simm):
return struct.pack('<I', 0xbf800000 | op << 16 | (simm & 0xffff))

def diamond(arm_a, arm_b):
# s_cmp_eq_u32 s4, 0; s_cbranch_scc0 over arm A and its s_branch; arm A; s_branch over arm B; arm B; load
return (struct.pack('<I', 0xbf068004) + sopp(4, len(arm_a) // 4 + 1) + arm_a + sopp(2, len(arm_b) // 4)
+ arm_b)

def joined_load(arms):
return translate_code(W[:LOAD_SITE] + arms + W[LOAD_SITE:])

clamped = (sop2(0x27, 21, 13, 255, 0xe0010) + sop2(0x09, 21, 21, 129) + sop2(0x0e, 13, 13, 255, 0xc000ffff) +
sop2(0x26, 14, 14, 21) + sop2(0x0e, 15, 15, 255, 0xcfffffff) + sop2(0x10, 15, 15, 255, 0x30000000))
for label, arms in (('raw conversion on both arms', diamond(raw_conv, raw_conv)),
('clamped conversion on both arms', diamond(clamped, clamped)),
# The two byte counts share one tag: no consumer reads the count (raw loads are unbounded).
('clamped and plain conversion', diamond(clamped, raw_conv)),
('stride clear on both arms', diamond(stride0_inst, stride0_inst))):
assert 'v5 = r0.Load(a + 0);' in joined_load(arms), label
# Nothing reads the count: a vector read of the joined byte-count word, or of a scalar computed from it, refuses.
def v_mov_s(vdst, sgpr):
return struct.pack('<I', 0x7e000000 | vdst << 17 | 1 << 9 | sgpr)

for label, read in (('v_mov_b32 v5, s14', v_mov_s(5, 14)),
('s_add_u32 s22, s14, 1 then v_mov_b32 v5, s22', sop2(0x00, 22, 14, 129) + v_mov_s(5, 22))):
try:
joined_load(diamond(clamped, raw_conv) + read)
raise AssertionError(f'{label} after the joined byte count translates')
except G.Unsupported as e:
assert 'numeric use of s' in str(e), f'unexpected refusal for {label}: {e}'
refused_joins = {
'one arm converts': diamond(raw_conv, sopp(0, 0)), # s_nop 0 on arm B
'one arm clears the stride only': diamond(raw_conv, stride0_inst),
}
for label, arms in refused_joins.items():
try:
joined_load(arms)
raise AssertionError(f'expected a join refusal for {label}, but translation succeeded')
except G.Unsupported as e:
assert 'joined at a branch target' in str(e), f'unexpected refusal for {label}: {e}'
# A loop at the load that enters with the loaded words and converts them before the back edge: the header
# mixes plain and converted words, so the load refuses.
loop_body = raw_conv + sop2(0x00, 22, 22, 129) + struct.pack('<I', 0xbf0a8216) # s_add_u32 s22; s_cmp_lt_u32 s22, 2
load_words = W[LOAD_SITE:LOAD_SITE + 8]
loop = (W[:LOAD_SITE] + sop1(0x03, 22, 128) + load_words + loop_body
+ sopp(5, -(len(load_words + loop_body) // 4 + 1)) + W[LOAD_SITE + 8:])
try:
translate_code(loop)
raise AssertionError('expected a refusal for the loop that converts before its back edge, but translated')
except G.Unsupported as e:
assert 'joined at a branch target' in str(e), f'unexpected refusal: {e}'
# Mutant control: without the join rule for converted words, the kept case refuses as before.
real_raw = G.raw_conversion_word
try:
G.raw_conversion_word = lambda sym: False
try:
joined_load(diamond(raw_conv, raw_conv))
raise AssertionError('the join of converted words translates without raw_conversion_word')
except G.Unsupported as e:
assert 'joined at a branch target' in str(e), f'unexpected refusal: {e}'
finally:
G.raw_conversion_word = real_raw

# 7. Formatted indexed buffer load through a modified descriptor is refused.
# Inserting `s_and_b32 s21, s13, 0xc000ffff` immediately before LOAD_L writes
# a derived descriptor word to s21, leaving the four operand registers s[12:15]
Expand Down
17 changes: 16 additions & 1 deletion tools/startup/gfx2hlsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,19 @@ def names_one_value(sym):
return sym[0] in ('const', 'ud', 'execcopy') or (sym[0] in ('ptr', 'ptrhi') and len(sym) == 2)


def raw_conversion_word(sym):
"""True for a loaded descriptor word after one step of the structured-to-raw conversion (raw_descriptor_alu): the
stride cleared, the byte count, or OOB_SELECT set to 3. The tag names what was applied to a word names_one_value
accepts, so a join where every path brings the same symbol keeps it. Two byte counts share a tag (stride, or stride
clamped to at least 1; they differ for a stride of 0), but no consumer reads the count: compute and vertex raw loads
and stores are unbounded, and a numeric read of a descriptor word is refused. Only compute joins use this; a spill
of a modified word stays refused."""
if not sym or sym[0] != 'desc':
return False
loaded = 5 if len(sym) > 4 and isinstance(sym[4], int) else 4
return len(sym) > loaded and sym[loaded] in ('stride0', 'byte_count', 'oob3') and names_one_value(sym[:loaded])


def descriptor_select(sym):
"""True for what Stage.scalar_select records for a compute s_cselect_b32 with a descnum arm:
('descnum', 'cselect', select offset, arm, arm). It has no run-time value, so no consumer may read it as one."""
Expand Down Expand Up @@ -4241,7 +4254,9 @@ def translate(self):
if any(reg not in w for _, w in arriving) and any(p and not run_time_symbol(p) for p in paths):
self.sym[reg] = ('joined at a branch target',)
continue
if force is None and paths and names_one_value(paths[0]) and all(p == paths[0] for p in paths):
if (force is None and paths and all(p == paths[0] for p in paths) and (
names_one_value(paths[0]) or getattr(self, 'kind', None) == 'cs'
and raw_conversion_word(paths[0]))):
self.sym[reg] = kept[off][reg] = paths[0]
continue
if force == 'refuse' or any(p and not run_time_symbol(p) for p in paths):
Expand Down
Loading