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
7 changes: 6 additions & 1 deletion docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -14900,10 +14900,15 @@ ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_cselect_mask_controls$

`tools/startup/gfx2hlsl.py` records the SGPRs that hold a lane mask (a `v_cmp` destination, a saveexec copy) in `Stage.masks`, and lowers a scalar logic op with a mask source as a mask op whose result is the lane's bit. The generic scalar path cleared the mark when it wrote ordinary data into such a register, but the other scalar 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 lowered as `s8 = (s6 | s7) & 1;`. Every write through `set_s` now clears the mark, the writers of a mask mark the register again after the write, and `s_load` clears the mark on its destination registers, also in the forms that only record a descriptor symbol.

`tests/startup_gfx2hlsl_mask_overwrite_check.py` writes a mask with a compare, overwrites it with an `s_buffer_load`, an `s_load` through a root pointer, an `s_load` of a descriptor or an `s_cselect_b32`, and evaluates the following `s_or_b32` on a buffer word with the restricted evaluator of `startup_gfx2hlsl_bit_alu_check.py`. Controls: without the overwrite the OR stays a mask OR, and a compare or a mask logic op after a load marks the register again. On the translator before this change seven checks fail (the mark stays and the OR is cut to bit 0). Test: `startup_gfx2hlsl_mask_overwrite_controls`. Not covered: a draw through a translated pipeline; the other symbol-only scalar writes (for example an `s_mov` of a descriptor) still keep a mark they find.
`tests/startup_gfx2hlsl_mask_overwrite_check.py` writes a mask with a compare, overwrites it with an `s_buffer_load`, an `s_load` through a root pointer, an `s_load` of a descriptor or an `s_cselect_b32`, and evaluates the following `s_or_b32` on a buffer word with the restricted evaluator of `startup_gfx2hlsl_bit_alu_check.py`. Controls: without the overwrite the OR stays a mask OR, and a compare or a mask logic op after a load marks the register again. On the translator before this change seven checks fail (the mark stays and the OR is cut to bit 0). Test: `startup_gfx2hlsl_mask_overwrite_controls`. Not covered: a draw through a translated pipeline.

Scalar ALU on descriptor data (an `s_mov` of a descriptor word, a field extract or mask of one, an `s_add_u32` to a descriptor pointer) records only a symbol for its destination and did not clear the mark. A register that had held a mask and then a descriptor field kept the mark, so in a captured pixel program the stride clamp of a structured-to-raw conversion (`s_bfe_u32 s23, s45, 0xe0010`, then `s_max_u32 s23, s23, 1`) read as a lane-mask read. The descriptor-data path now clears the mark on its destination SGPRs before it writes them. The sources are classified before that, so a mask read by the same instruction is treated as before. The check runs these four writes after a compare in a pixel and a vertex stage and the clamp after the extract; a descriptor write into another SGPR keeps the mark on s6 (control). On the translator before this change (700522ef) six checks fail: the `s_bfe_u32` and `s_add_u32` clearing checks and the stride clamp, which is refused as `s_max_u32 reads lane mask s6 as numeric data`, each in both stages.

The extract and the clamp emit no HLSL: the clamped stride is a symbol, and a numeric read of it is refused as before (`numeric use of s6 holding descnum`), with or without the earlier mask. It changes output only where it feeds the structured-to-raw conversion of a buffer store, which is recognized in compute and vertex programs. Case I-raw-mask of `tests/startup_gfx2hlsl_compute_check.py` covers that on the GPU: `v_cmp_gt_u32_e64 s21, 3, v0` in a 24-lane group, then the clamped conversion into s21 and the store of case A. The dispatch compares all 320 words with the CPU reference. The translation differs from case I-raw only by the dead compare. Control: `s_max_u32 s21, s21, 1` straight after the compare is still refused. On the translator before this change the case is refused (`s_max_u32 reads lane mask s21 as numeric data`), so the controls and the dispatch fail.

```powershell
ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_mask_overwrite_controls$' -V
ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_compute_(controls|warp|hardware)$' -V
```

## Startup translator: lane masks used as vector data
Expand Down
19 changes: 18 additions & 1 deletion tests/startup_gfx2hlsl_compute_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,7 @@ def signed(x):
's_bfe_u32 s21, s29, 0xe0010': '1dff959310000e00',
's_max_u32 s21, s21, 1': '15819584',
's_max_u32 s21, s21, 2': '15829584',
'v_cmp_gt_u32_e64 s21, 3, v0': '1500c4d483000200',
's_bfe_u32 s21, s17, 0xe0011': '11ff959311000e00',
's_mul_i32 s18, s18, s21': '12151293',
's_mul_i32 s18, s18, 4': '12841293',
Expand Down Expand Up @@ -769,6 +770,15 @@ def replace_store(*lines):
CASES['I-raw'] = (SOURCE_A.replace('buffer_store_dword', '\n'.join((*RAW_CONVERSION, 'buffer_store_dword'))),
splice(*RAW_CONVERSION), *CASES['A'][2:])

# Case I-raw-mask: s21 first holds a lane mask (a compare in a 24-lane group), then the clamped stride of the same
# conversion. The descriptor-data write ends the mark, so the clamp is not a lane-mask read and the store lands as
# in case A. The translator before that change refused it: s_max_u32 reads lane mask s21 as numeric data.
MASK_S21 = 'v_cmp_gt_u32_e64 s21, 3, v0'
CLAMPED_CONVERSION = (STRIDE_EXTRACT, 's_max_u32 s21, s21, 1', *RAW_CONVERSION[1:])
CASES['I-raw-mask'] = (SOURCE_A.replace('buffer_store_dword',
'\n'.join((MASK_S21, *CLAMPED_CONVERSION, 'buffer_store_dword'))),
splice(MASK_S21, *CLAMPED_CONVERSION), *CASES['A'][2:])

# Replace the nonzero stride constant in s5 with the first active lane of a
# fresh 24-thread group. Ignoring FF1 leaves a different word at every i > 0.
CASES['A-ff1'] = ('s_ff1_i32_b32 s5, exec_lo\n' + SOURCE_A,
Expand Down Expand Up @@ -873,7 +883,7 @@ def formatted_input():


def expected(name):
if name in ('I', 'I-raw'):
if name in ('I', 'I-raw', 'I-raw-mask'):
return expected('A')
out = [SENTINEL] * WORDS
if name == 'join-load':
Expand Down Expand Up @@ -1711,6 +1721,13 @@ def check_controls():
# Captured programs clamp the extracted stride to at least 1 before scaling NUM_RECORDS.
clamped = (STRIDE_EXTRACT, 's_max_u32 s21, s21, 1', *RAW_CONVERSION[1:])
assert translate('A', code=splice(*clamped)).hlsl_cs() == raw.hlsl_cs()
# After a lane mask in s21 the clamped conversion still matches case I-raw apart from the compare itself; the
# dispatch of case I-raw-mask stores through it. Control: a clamp that reads the mask itself stays refused.
masked = translate('I-raw-mask').hlsl_cs().splitlines()
assert [line for line in masked if line not in raw.hlsl_cs().splitlines()] == [
' s21 = ((0x00000003u > v0) ? 1u : 0u);'], masked
refused('s_max_u32 reads lane mask s21 as numeric data', lambda: translate(
'A', code=splice(MASK_S21, 's_max_u32 s21, s21, 1', *RAW_CONVERSION[1:])).hlsl_cs())
refused('buffer store through a modified descriptor', lambda: translate(
'A', code=splice(STRIDE_EXTRACT, 's_max_u32 s21, s21, 2', *RAW_CONVERSION[1:])).hlsl_cs())
reload = ('s_load_dwordx8 s[24:31], s[10:11], 0x0', 's_waitcnt lgkmcnt(0)')
Expand Down
36 changes: 36 additions & 0 deletions tests/startup_gfx2hlsl_mask_overwrite_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
buffer word with the restricted evaluator of startup_gfx2hlsl_bit_alu_check: an s_buffer_load, an s_load through a
root pointer, an s_load of a descriptor, and an s_cselect_b32 into the former mask register. Controls: without the
overwrite the OR stays a mask OR, and a compare or a mask logic op written after a load marks the register again.

Scalar ALU on descriptor data (an s_mov of a descriptor word, the s_bfe_u32 and s_and_b32 of a structured-to-raw
conversion, an s_add_u32 to a descriptor pointer) records only a symbol for its destination. It now clears the mark
as well, in a pixel and a vertex stage, so the s_max_u32 that clamps an extracted stride is not taken as a mask read.
Control: such a write into another SGPR keeps the mark on s6.
"""
import struct
import sys
Expand All @@ -39,6 +44,9 @@ class Pipe:
sampler_heap = False
root = {'params': [{'type': 2}], 'flags': 0}

def __init__(self):
self.modified = set() # descriptors written by scalar ALU (the structured-to-raw conversion marks them)

def resource(self, key, kind):
return 'r0'

Expand Down Expand Up @@ -104,6 +112,34 @@ def main():
run(s, 'v_cmp_eq_u32_e64 s5, v0, v1', 's_load_dwordx4 s[4:7], s[12:13], 0x20')
check('s_load_dwordx4 of a descriptor clears the mark on s5', 5 not in s.masks, s.masks)

# Scalar ALU on descriptor data writes the destination's symbol without set_s. The register then holds
# descriptor data, so a later numeric read of it is not a lane-mask read (the stride clamp of a
# structured-to-raw conversion: s_bfe_u32 of DW1[29:16], then s_max_u32 with 1).
for kind in ('ps', 'vs'):
for text in ('s_mov_b32 s6, s17', 's_bfe_u32 s6, s17, 0xe0010', 's_and_b32 s6, s19, 0xcfffffff',
's_add_u32 s6, s2, 16'):
s = stage()
s.kind = kind
try:
run(s, COMPARE, text)
except G.Unsupported as error:
check('%s %s after a compare translates' % (kind, text), False, error)
continue
check('%s %s (descriptor data) clears the mark on s6' % (kind, text), 6 not in s.masks, s.masks)
s = stage()
s.kind = kind
try:
run(s, COMPARE, 's_bfe_u32 s6, s17, 0xe0010', 's_max_u32 s6, s6, 1')
check('%s s_max_u32 of the extracted stride is not refused as a lane-mask read' % kind, True)
except G.Unsupported as error:
check('%s s_max_u32 of the extracted stride is not refused as a lane-mask read' % kind, False, error)
# Control: only the destination loses its mark; a descriptor write to another SGPR leaves the mask s6.
s = stage()
s.kind = kind
run(s, COMPARE, 's_bfe_u32 s8, s17, 0xe0010')
check('control: %s s_bfe_u32 into s8 keeps the mask mark on s6' % kind, 6 in s.masks and 8 not in s.masks,
s.masks)

# A mask written after a load marks the register again.
s = stage()
run(s, 's_buffer_load_dword s6, s[16:19], 0x0', COMPARE)
Expand Down
8 changes: 8 additions & 0 deletions tools/startup/gfx2hlsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5310,6 +5310,14 @@ def src_sym(t):
raise Unsupported('scc from descriptor data')
if word_pack:
raise Unsupported('%s on descriptor data' % base)
# Every path below writes the destination's symbol directly, not through set_s, or refuses. The
# written word is descriptor data, so a lane-mask mark the SGPR held from an earlier write ends here as
# set_s ends it. lower() has already classified the sources, so a mask read by this instruction is
# still refused.
if dst and dst[0] == 's':
for reg in range(dst[1], dst[2] + 1):
getattr(self, 'masks', set()).discard(reg)
getattr(self, 'mask_high', set()).discard(reg)
if self.pointer_add(base, dname, srcs, syms):
return None
if dname == 'm0':
Expand Down
Loading