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
1 change: 1 addition & 0 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ jobs:
tests/startup_live_telemetry_check.py \
tests/startup_gfx2hlsl_cvt_alu_check.py \
tests/startup_gfx2hlsl_int_med3_check.py \
tests/startup_gfx2hlsl_cselect_mask_check.py \
tests/startup_gfx2hlsl_mask_overwrite_check.py \
tests/startup_gfx2hlsl_capture_check.py \
tests/startup_gfx2hlsl_watch_check.py \
Expand Down
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ python3 -m venv out/pe-env
./out/pe-env/bin/python -B tests/startup_gfx2hlsl_sop2_complement_check.py
./out/pe-env/bin/python -B tests/startup_gfx2hlsl_sdwa_cvt_check.py
./out/pe-env/bin/python -B tests/startup_gfx2hlsl_int_med3_check.py
./out/pe-env/bin/python -B tests/startup_gfx2hlsl_cselect_mask_check.py
./out/pe-env/bin/python -B tests/startup_gfx2hlsl_mask_overwrite_check.py
./out/pe-env/bin/python -B tests/startup_flat_param_check.py
./out/pe-env/bin/python -B tests/startup_inline_sampler_check.py
Expand Down
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2300,6 +2300,9 @@ if(EXISTS "${INTAKE_PYTHON}")
# Startup shader translator: signed and unsigned three-source integer min, max and median.
add_test(NAME startup_gfx2hlsl_int_med3_controls COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_int_med3_check.py")
# Startup shader translator: s_cselect_b32 into the wave32 lane masks vcc_lo and exec_lo.
add_test(NAME startup_gfx2hlsl_cselect_mask_controls COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_cselect_mask_check.py")
# Startup shader translator: a scalar load or select into a former lane-mask SGPR clears its mask mark.
add_test(NAME startup_gfx2hlsl_mask_overwrite_controls COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_mask_overwrite_check.py")
Expand Down
10 changes: 10 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -11658,6 +11658,16 @@ Tests: `startup_gfx2hlsl_cvt_alu_controls`, `startup_gfx2hlsl_cvt_alu_warp` and
ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_int_med3_controls$' -V
```

## Startup translator: scalar select into the wave32 lane masks

In wave32, `vcc_lo` and `exec_lo` hold the whole lane mask, and the generic scalar path in `tools/startup/gfx2hlsl.py` already writes them as `vcc` and `exec`. `s_cselect_b32` went through a separate select path that assumed an SGPR destination, so `s_cselect_b32 vcc_lo, -1, 0` stopped the translation with a `TypeError` instead of a lowering or an `Unsupported` refusal. The select path now writes the lane's bit of the selected word to `vcc` or `exec` for `vcc_lo` and `exec_lo` (an `exec_lo` write clears `exec_known`, as an `exec` write does), keeps the SGPR and `m0` destinations as before, and refuses any other destination with an `Unsupported` message.

`tests/startup_gfx2hlsl_cselect_mask_check.py` decodes synthetic SOP2 words, evaluates the emitted select for both SCC values with the restricted evaluator of `startup_gfx2hlsl_bit_alu_check.py`, and checks that an unreduced select (the `& 1` removed) gives a different `vcc`. It also checks the SGPR and `m0` lines and the refusal of the null SGPR as a destination. On the translator before this change the first case raises the `TypeError`. Test: `startup_gfx2hlsl_cselect_mask_controls`. Not covered: a draw through a translated pipeline, and the guard that refuses exec writes behind a fetch call, which still accepts only the `exec` spelling.

```powershell
ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_cselect_mask_controls$' -V
```

## Startup translator: lane-mask marks after a scalar overwrite

`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.
Expand Down
108 changes: 108 additions & 0 deletions tests/startup_gfx2hlsl_cselect_mask_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
"""s_cselect_b32 into vcc_lo and exec_lo in tools/startup/gfx2hlsl.py.

usage: startup_gfx2hlsl_cselect_mask_check.py

Synthetic SOP2 words only (op [29:23] = 10, sdst [22:16], ssrc1 [15:8], ssrc0 [7:0]; the names are asserted from the
translator's decoder). In wave32, vcc_lo and exec_lo hold the whole lane mask, and the generic scalar path already
writes them as `vcc` and `exec`. The select path assumed an SGPR destination, so `s_cselect_b32 vcc_lo, -1, 0`
raised a TypeError instead of a translation or an Unsupported refusal.

Checked: vcc_lo and exec_lo receive the lane's bit of the selected word for both SCC values (evaluated with the
restricted evaluator of startup_gfx2hlsl_bit_alu_check), exec_lo clears exec_known, an SGPR and an m0 destination
lower as before, and a destination that is neither (the null SGPR) is refused with an Unsupported message.
Negative control: the same evaluation with the selected word not reduced to the lane bit gives a different vcc.
"""
import struct
import sys
from pathlib import Path

HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE.parent / 'tools' / 'startup'))
sys.path.insert(0, str(HERE))
import gfx2hlsl as G # noqa: E402
from startup_gfx2hlsl_bit_alu_check import Expr # noqa: E402

failures = 0


def check(what, ok, detail=''):
global failures
print('%s %s%s' % ('ok ' if ok else 'FAIL', what, '' if ok or not detail else ': %s' % detail))
if not ok:
failures += 1


class Pipe:
root = {'params': [], 'flags': 0}

def ud(self, source):
return 0


SOP2_CSELECT_B32 = 0x80000000 | 10 << 23
INLINE_ZERO, INLINE_ONE, INLINE_MINUS_ONE = 128, 129, 193
VCC_LO, EXEC_LO, M0, NULL = 106, 126, 124, 125


def cselect(sdst, ssrc0, ssrc1):
return SOP2_CSELECT_B32 | sdst << 16 | ssrc1 << 8 | ssrc0


def one(word):
s = G.Stage(Pipe(), 'ps', b'')
s.lines = []
_, _, name, text = G.decode(struct.pack('<I', word))[0]
s.pc = 0
s.one(name, text)
return s, text, s.lines


def value(line, target, scc):
assert line.startswith(target + ' = ') and line.endswith(';'), line
return Expr(line[len(target) + 3:-1].replace('scc ?', '(scc != 0u) ?')).run({'scc': scc})[0]


def main():
for sdst, target in ((VCC_LO, 'vcc'), (EXEC_LO, 'exec')):
for ssrc0, ssrc1, words in ((INLINE_MINUS_ONE, INLINE_ZERO, (0xffffffff, 0)),
(INLINE_ZERO, INLINE_ONE, (0, 1))):
try:
s, text, lines = one(cselect(sdst, ssrc0, ssrc1))
except Exception as error: # the old select path raised TypeError here
check('s_cselect_b32 into %s translates' % target, False, '%s: %s' % (type(error).__name__, error))
continue
check('%s decodes as s_cselect_b32 %s_lo' % (target, target), text.startswith('s_cselect_b32 %s_lo, ' % target), text)
check('%s: one line writes the whole-mask variable' % text, len(lines) == 1 and lines[0].startswith(target + ' = '), lines)
got = [value(lines[0], target, scc) for scc in (1, 0)]
check('%s: lane bit of the selected word for scc 1 and 0' % text, got == [w & 1 for w in words], got)
if target == 'exec':
check('%s clears exec_known' % text, s.exec_known is False)

# Negative control: without the lane-bit reduction the vcc word would be the whole selected word.
_, _, lines = one(cselect(VCC_LO, INLINE_MINUS_ONE, INLINE_ZERO))
mutated = lines[0].replace(') & 1;', ');')
check('control: the unreduced select gives 0xffffffff, not the lane bit',
mutated != lines[0] and value(mutated, 'vcc', 1) == 0xffffffff and value(lines[0], 'vcc', 1) == 1, mutated)

_, text, lines = one(cselect(5, INLINE_ZERO, INLINE_MINUS_ONE))
check('SGPR destination unchanged', text == 's_cselect_b32 s5, 0, -1' and lines == ['s5 = scc ? 0x00000000u : 0xffffffffu;'], lines)
_, text, lines = one(cselect(M0, INLINE_MINUS_ONE, INLINE_ZERO))
check('m0 destination unchanged', lines == ['m0 = scc ? 0xffffffffu : 0x00000000u;'], (text, lines))
try:
_, text, lines = one(cselect(NULL, INLINE_MINUS_ONE, INLINE_ZERO))
except G.Unsupported as error:
check('null destination refused', 's_cselect into' in str(error), str(error))
except Exception as error:
check('null destination refused', False, '%s: %s' % (type(error).__name__, error))
else:
check('null destination refused', False, lines)

if failures:
print('FAILED: %d check(s)' % failures)
return 1
print('cselect mask: vcc_lo and exec_lo are the lane mask; SGPR and m0 unchanged; null refused')
return 0


if __name__ == '__main__':
sys.exit(main())
13 changes: 9 additions & 4 deletions tools/startup/gfx2hlsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -990,12 +990,17 @@ def scalar_select(self, ops, pair):
self.sym.pop('m0', None)
self.emit('m0 = %s;' % expr)
return
if low[0] in ('exec', 'vcc'):
if low[0] == 'exec':
if low[0] in ('exec', 'vcc', 'exec_lo', 'vcc_lo'):
# In wave32, exec_lo and vcc_lo hold the whole lane mask, as the generic scalar path reads them.
mask = 'exec' if low[0].startswith('exec') else 'vcc'
if mask == 'exec':
self.exec_known = False
self.emit('%s = (%s) & 1;' % (low[0], expr))
self.emit('%s = (%s) & 1;' % (mask, expr))
return
d = reg_range(low[0])[1]
r = reg_range(low[0])
if not r or r[0] != 's':
raise Unsupported('s_cselect into %s' % low[0])
d = r[1]
self.set_s(d, '(%s) & 1' % expr if pair else expr)
if pair:
self.masks.add(d)
Expand Down
Loading