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
40 changes: 38 additions & 2 deletions tests/startup_gfx2hlsl_loop_constants_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@
A constant that no other path writes stays folded (control). Folding the counter froze it at 1, so the exit test
fired only for a trip count of 1 and every other loop ran until the dispatcher guard (65,536 iterations).

Mutations (each must turn the battery red): constants kept at every join (the behavior before this check), and
the loop body range left out (only forward branches handled).
"No other path writes the register" is judged from the constant's own write: the constant stays known at a branch
target when the block of that write dominates the target and no block on a path from the write to the target writes
the register again. A write in an earlier arm, before the constant was set, does not count (a register reused for a
constant sampler word after an earlier conditional block used it for something else). A later arm or a loop body
that writes it still drops it.

Mutation (must turn the battery red): constants kept at every join (the behavior before this check). Leaving the
loop body range out of join_writes alone does not: the joined set and the dominance rule see the loop body too.
"""
import os
import sys
Expand Down Expand Up @@ -67,6 +73,36 @@ def run(*instructions):
's_endpgm'),
['s8 = 0x00000003u + 0x00000001u;', 's14 = s14 + 0x00000001u;'],
['s8 = s7 + 0x00000001u;']),
# An earlier arm writes s7, then every path sets it to 3; the second conditional block does not write it.
('constant set after an earlier arm wrote the register',
('s_cmp_eq_u32 s2, 0', 's_cbranch_scc1 label_000c', 's_mov_b32 s7, 5', 's_mov_b32 s7, 3', 's_cmp_eq_u32 s2, 1',
's_cbranch_scc1 label_001c', 's_mov_b32 s9, 1', 's_add_u32 s8, s7, 1', 's_endpgm'),
['s8 = 0x00000003u + 0x00000001u;'],
['s8 = s7 + 0x00000001u;']),
# The same program with the second arm writing s7: two values reach the join.
('constant set after an earlier arm, later arm writes it',
('s_cmp_eq_u32 s2, 0', 's_cbranch_scc1 label_000c', 's_mov_b32 s7, 5', 's_mov_b32 s7, 3', 's_cmp_eq_u32 s2, 1',
's_cbranch_scc1 label_001c', 's_mov_b32 s7, 9', 's_add_u32 s8, s7, 1', 's_endpgm'),
['s8 = s7 + 0x00000001u;'],
['s8 = 0x00000003u + 0x00000001u;', 's8 = 0x00000009u + 0x00000001u;']),
# The constant is set in an arm only: its write does not dominate the join, whatever the arm before it did.
('constant set in an arm after an earlier arm wrote the register',
('s_cmp_eq_u32 s2, 0', 's_cbranch_scc1 label_000c', 's_mov_b32 s7, 5', 's_cmp_eq_u32 s2, 1',
's_cbranch_scc1 label_0018', 's_mov_b32 s7, 3', 's_add_u32 s8, s7, 1', 's_endpgm'),
['s8 = s7 + 0x00000001u;'],
['s8 = 0x00000003u + 0x00000001u;', 's8 = 0x00000005u + 0x00000001u;']),
# An earlier arm writes s7, every path sets it to 3, and a loop reads it: kept when the body leaves it alone,
# dropped when the body writes it after the read.
('constant set after an earlier arm, read in a loop',
('s_cmp_eq_u32 s2, 0', 's_cbranch_scc1 label_000c', 's_mov_b32 s7, 5', 's_mov_b32 s7, 3', 's_add_u32 s8, s7, 1',
's_cmp_eq_u32 s8, s2', 's_cbranch_scc0 label_0010', 's_endpgm'),
['s8 = 0x00000003u + 0x00000001u;'],
['s8 = s7 + 0x00000001u;']),
('constant set after an earlier arm, written in the loop',
('s_cmp_eq_u32 s2, 0', 's_cbranch_scc1 label_000c', 's_mov_b32 s7, 5', 's_mov_b32 s7, 3', 's_add_u32 s8, s7, 1',
's_mov_b32 s7, 4', 's_cmp_eq_u32 s8, s2', 's_cbranch_scc0 label_0010', 's_endpgm'),
['s8 = s7 + 0x00000001u;'],
['s8 = 0x00000003u + 0x00000001u;', 's8 = 0x00000004u + 0x00000001u;']),
]

for label, program, wanted, unwanted in CASES:
Expand Down
6 changes: 3 additions & 3 deletions tests/startup_gfx2hlsl_sop2_complement_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,12 @@ def lane_mask_controls():
check('an overwritten mask is a plain word: refused', got == 's_nand_b32 on a lane mask', got)

# 5. Mutations: each rule disabled accepts a program refused above.
saved = G.Stage.unjoined_masks
G.Stage.unjoined_masks = lambda self, target, last_write: set()
saved = G.Stage.unjoined
G.Stage.unjoined = lambda self, target, last_write, regs: set()
try:
got = [text_refusal(*skip, 's_nand_b32 s6, s5, s4', 's_endpgm'), text_refusal(*loop)]
finally:
G.Stage.unjoined_masks = saved
G.Stage.unjoined = saved
check('mutation without the join rule accepts both programs', all(t.startswith('accepted') for t in got), str(got))
saved = G.Stage.descriptor_scc_reads
G.Stage.descriptor_scc_reads = lambda self, branches, effects: iter(())
Expand Down
28 changes: 15 additions & 13 deletions tools/startup/gfx2hlsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1972,13 +1972,12 @@ def joined_registers(self, with_cfg=False):
self.flow = (index, block_of, succ, dom, writes, {})
return joined, forward_splits, div_targets

def unjoined_masks(self, target, last_write):
"""Marked SGPRs whose mark is not proven at a branch target. self.masks follows program order, so a mark
holds on every path into the target only if the block of the register's last write dominates the target and
no block on a path from that write to the target writes the register again."""
def unjoined(self, target, last_write, regs):
"""The registers of regs whose program-order state is not proven at a branch target. A lane-mask mark or a
constant follows program order, so it holds on every path into the target only if the block of the register's
last write dominates the target and no block on a path from that write to the target writes the register
again."""
index, block_of, succ, dom, writes, reached = self.flow
if target not in index:
return set(self.masks)
def after(block): # blocks reached from block over at least one edge
if block not in reached:
seen, todo = set(), list(succ[block])
Expand All @@ -1992,7 +1991,7 @@ def after(block): # blocks reached from block over at least one edge
t = block_of[index[target]]
into = {b for b in range(len(succ)) if t in after(b)}
out = set()
for reg in self.masks:
for reg in regs:
w = block_of[index[last_write[reg]]] if reg in last_write else 0
if w not in dom[t] or any(reg in writes[b] for b in (after(w) & into) - {w}):
out.add(reg)
Expand Down Expand Up @@ -2049,7 +2048,7 @@ def translate(self):
self.exec_known = init_exec_known
self.wqm_exec = init_wqm_exec
self.masks = set(init_masks)
self.join_masks = set() # marked SGPRs whose mark is not proven on every path (unjoined_masks)
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)
self.used_s, self.used_v = set(init_used_s), set(init_used_v)
Expand Down Expand Up @@ -2128,7 +2127,7 @@ def translate(self):
if reg != 'm0':
self.used_s.add(reg)
clobber = here if off in joined else every_sgpr
self.join_masks |= self.unjoined_masks(off, last_write)
self.join_masks |= self.unjoined(off, last_write, self.masks)
fwd_clobber = forward_splits.get(off, clobber)
blocked = force_unproven | force_at.get(off, set())
kept_u = {
Expand All @@ -2141,9 +2140,12 @@ def translate(self):
# A constant is only known here if no other path into the join writes the register: a loop counter
# set before the loop and incremented in it reaches the loop head with a new value on every pass.
# set_s also assigns constants at run time, so the register reads its real value once it is dropped.
for reg in join_writes.get(off, ()) | (clobber & every_sgpr):
if (self.sym.get(reg) or ('',))[0] == 'const':
del self.sym[reg]
# clobber also holds the writes of every earlier block off the dominator chain; a constant written
# after those, by a block every path runs, with no later write on any path, is still known.
consts = {reg for reg in join_writes.get(off, set()) | (clobber & every_sgpr)
if (self.sym.get(reg) or ('',))[0] == 'const'}
for reg in self.unjoined(off, last_write, consts):
del self.sym[reg]
self.defined_uniform_s = set(kept_u) | {
k for k, v in self.sym.items()
if isinstance(k, int) and v and (
Expand All @@ -2167,7 +2169,7 @@ def translate(self):
falls = name not in ('s_branch', 's_setpc_b64', 's_endpgm')
dests = scalar_destinations(name, asm)
written |= every_sgpr if 'any' in dests else dests
last_write.update(dict.fromkeys(dests, off))
last_write.update(dict.fromkeys(every_sgpr if 'any' in dests else dests, off))
try:
if off in refused:
raise Unsupported(refused[off])
Expand Down
Loading