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
13 changes: 13 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ derived from that descriptor's clamped stride, and the raw OOB selector.
The compute fixture reads back all four store widths across two workgroups.
[Experiment and reproduction commands](experiments/native-bytewin-store.md)

## Scalar SCC liveness in direct compute

`uses_scc_after()` in `src/gfx1013_direct_compute.inl` decides whether the
SCC value leaving a symbolic descriptor field is still live. A field
extraction refuses with "numeric SCC from a symbolic descriptor field" when
SCC is live after it and no stride metadata was bound for that SRV; when SCC
is dead at the field, the extraction compiles. The classification now names
the remaining lowered scalar ops explicitly: `s_bfe_i32`, `s_addk_i32` and
all twelve `s_cmpk_*` comparisons overwrite SCC, `s_mulk_i32`, the counter
waits and the accepted mode writes preserve it, and `s_addc_u32`/`s_subb_u32`
read it.
[Experiment and reproduction commands](experiments/native-direct-scc-liveness.md)

## Sub-DWORD global loads in direct compute

`global_load_ubyte`, `global_load_sbyte`, `global_load_ushort` and `global_load_sshort` lower in the
Expand Down
91 changes: 91 additions & 0 deletions docs/experiments/native-direct-scc-liveness.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Scalar SCC liveness in direct compute

`uses_scc_after()` in `src/gfx1013_direct_compute.inl` is the backward walk
that decides whether the SCC value leaving a symbolic descriptor field is
still live. A field extraction refuses with "numeric SCC from a symbolic
descriptor field" when SCC is live after it and no stride metadata was bound
for that SRV; when SCC is dead at the field, the extraction compiles and SCC
is simply the value the next writer produces.

The walk previously treated every SOPK op except `s_movk_i32` as uncertain,
so a program that kills SCC with `s_bfe_i32`, `s_addk_i32` or any of the
twelve `s_cmpk_*` comparisons, or that preserves SCC through `s_mulk_i32`,
a counter wait (`s_waitcnt_vscnt`, `s_waitcnt_vmcnt`, `s_waitcnt_expcnt`,
`s_waitcnt_lgkmcnt`), or an accepted mode write (`s_setreg_b32`,
`s_setreg_imm32_b32`, `s_round_mode`, `s_denorm_mode`) and then kills it
with `s_and_b32`, was refused as if SCC were still live. The classification
now names those ops explicitly: the three overwriters join the overwrite
list, the SOPK preservers join a new `preserves_sopk` list, and
`s_addc_u32`/`s_subb_u32` join the readers (they consume SCC through
`read(128)` in `arithmetic()`).

Two classifications are deliberately conservative. `s_setreg_b32` is listed
as a preserver even though `consume_half_mode()` never accepts it in
compute: any program containing it still refuses at lowering, with
"unsupported binary16 rounding or denormal mode" when its MODE field
touches binary16 bits, so the liveness entry cannot admit a translation.
`s_wqm_b32`/`s_wqm_b64` stay in the `mask_scc` overwrite list although
`arithmetic()` does not lower them; they were overwriters before this change
and no program reaches them. Scalar ops DirectCompute does not lower at all
(`s_getpc_b64`, `s_setpc_b64`, `s_swappc_b64`, `s_rfe_b64`,
`s_movrels_b32`/`s_movrels_b64`, `s_movreld_b32`/`s_movreld_b64`,
`s_mov_fed_b32`, `s_movrelsd_2_b32`, `s_version`, `s_getreg_b32`,
`s_call_b64`, `s_subvector_loop_begin`, `s_subvector_loop_end`) stay
uncertain. SOPP ops are not listed anywhere: the uncertain test never
matches them, so the lowered ones (`s_nop`, `s_waitcnt`,
`s_waitcnt_depctr`, `s_clause`, `s_inst_prefetch`, `s_setprio`,
`s_barrier`, `s_branch`, the `s_cbranch_vccz`/`s_cbranch_vccnz` and
`s_cbranch_execz`/`s_cbranch_execnz` branches, `s_round_mode`,
`s_denorm_mode`) fall through to the successor edges and preserve SCC the
same way, while the unlowered ones still refuse at lowering.

The [source-built fixture](../../tests/native_scc_liveness_check.py) pins
the classification three ways without Windows. Its no-arg mode parses
`uses_scc_after()` out of the translator source and checks all 199 scalar
ops in the ISA tables against the census, runs a Python replica of the
backward walk over 53 straight-line and branching programs (including a
diamond where only one arm overwrites SCC, a branch over a reader, and
loops with and without a reader), and kills 25 of 25 single-condition
mutants of the new and modified conditions. It also disassembles every
controls program and the hardware program and pins their exact encodings:
every controls program starts from the same base (the descriptor loads, the
group base, `s_movk_i32 s24, 1` and `s_movk_i32 s27, 0`, then the field
extraction), so the field's SCC is the one the tested instruction sees and
no SGPR is read unbound. The field must come after the group base because
the base starts with `s_lshl_b32`, an SCC overwriter. The no-arg mode
further pins the pass/refusal split itself: between the field and the first
SCC reader, every pass program has an overwriter (except the
branch-over-reader, which passes by skipping the reader) and every SCC
refusal program has none (except the diamond that refuses on its preserving
arm, and the three mode refusals, which refuse on the mode op). The
`controls` mode compiles 8 pass programs and 21 refusal programs against
a root whose descriptor slot has no stride metadata: the pass programs
compile, every refusal program fails with the exact "numeric SCC from a
symbolic descriptor field" substring, and the direct read compiles once the
stride is bound. The `hardware` mode replays one shader with stride 0 and
stride 4 and checks all 16 output words and guards in both groups,
covering signed and unsigned imm16 sign-extension and boundary values for
`s_bfe_i32`, `s_addk_i32` and `s_cmpk_*`, and proving `s_mulk_i32` and
`s_waitcnt_vscnt null, 0` carry both SCC 0 and SCC 1 into `s_cselect_b32`.
Slot 15 proves the same for the mode writes (`s_round_mode 0`,
`s_denorm_mode 0xf`, `s_setreg_imm32_b32`) after a fresh field extraction;
the stride field value itself is symbolic, so it cannot be stored
numerically and is never read as a value.

Windows verification is pending on the current head. The first dev run
compiled the C++ cleanly and caught three fixture bugs in the check (the
field extracted before the group base, unbound SGPR reads in the controls
programs, and a numeric store of the symbolic field value in the hardware
program); those are fixed in this commit and re-verification is pending.

```powershell
cmake --build --preset dev --target compile_dxil native_store_replay
ctest --preset dev --no-tests=error -R '^native_scc_liveness_(controls|hardware)$' -V
```

```bash
python3 -B tests/native_scc_liveness_check.py
```

The instruction behavior follows the
[AMD RDNA 2 ISA, revision 1.0](https://docs.amd.com/v/u/en-US/rdna2-shader-instruction-set-architecture).
33 changes: 30 additions & 3 deletions src/gfx1013_direct_compute.inl
Original file line number Diff line number Diff line change
Expand Up @@ -3329,6 +3329,7 @@ struct DirectCompute {
const auto n = inst.name;
const bool reads_scc = n == "s_cselect_b32" || n == "s_cselect_b64" ||
n == "s_cmov_b32" || n == "s_cmov_b64" || n == "s_cmovk_i32" ||
n == "s_addc_u32" || n == "s_subb_u32" ||
std::find(inst.src.begin(), inst.src.end(), 253u) != inst.src.end();
// These scalar mask operations set SCC from their entire
// result and do not consume the incoming SCC. An explicit
Expand All @@ -3345,7 +3346,7 @@ struct DirectCompute {
const bool overwrites = mask_scc || n == "s_abs_i32" ||
n == "s_bcnt0_i32_b32" || n == "s_bcnt1_i32_b32" ||
n == "s_bcnt0_i32_b64" || n == "s_bcnt1_i32_b64" ||
inst.enc == Encoding::Sopc || n == "s_bfe_u32" || n == "s_and_b32" ||
inst.enc == Encoding::Sopc || n == "s_bfe_u32" || n == "s_bfe_i32" || n == "s_and_b32" ||
n == "s_or_b32" || n == "s_max_u32" || n == "s_min_u32" || n == "s_min_i32" ||
n == "s_max_i32" || n == "s_absdiff_i32" || n == "s_add_u32" || n == "s_sub_u32" ||
n == "s_add_i32" || n == "s_sub_i32" || n == "s_and_saveexec_b32" ||
Expand All @@ -3358,7 +3359,15 @@ struct DirectCompute {
n == "s_lshr_b32" || n == "s_lshl_b32" || n == "s_ashr_i32" ||
n == "s_lshl_b64" || n == "s_lshr_b64" || n == "s_ashr_i64" || n == "s_xor_b32" ||
n == "s_bfe_u64" || n == "s_bfe_i64" ||
n == "s_lshl1_add_u32" || n == "s_lshl2_add_u32" || n == "s_lshl3_add_u32" || n == "s_lshl4_add_u32";
n == "s_lshl1_add_u32" || n == "s_lshl2_add_u32" || n == "s_lshl3_add_u32" || n == "s_lshl4_add_u32" ||
// SOPK comparisons and the signed add write SCC from the
// comparison result or the signed overflow, respectively,
// and do not consume the incoming SCC (arithmetic()).
n == "s_addk_i32" ||
n == "s_cmpk_eq_i32" || n == "s_cmpk_lg_i32" || n == "s_cmpk_gt_i32" ||
n == "s_cmpk_ge_i32" || n == "s_cmpk_lt_i32" || n == "s_cmpk_le_i32" ||
n == "s_cmpk_eq_u32" || n == "s_cmpk_lg_u32" || n == "s_cmpk_gt_u32" ||
n == "s_cmpk_ge_u32" || n == "s_cmpk_lt_u32" || n == "s_cmpk_le_u32";
const bool preserves_pair = n == "s_mul_i32" || n == "s_mul_hi_i32" || n == "s_mul_hi_u32" ||
n == "s_bfm_b32" || n == "s_bfm_b64" || n == "s_pack_ll_b32_b16" || n == "s_pack_lh_b32_b16" ||
n == "s_pack_hh_b32_b16";
Expand All @@ -3371,7 +3380,25 @@ struct DirectCompute {
n == "s_sext_i32_i8" || n == "s_sext_i32_i16" ||
n == "s_bitset0_b32" || n == "s_bitset0_b64" ||
n == "s_bitset1_b32" || n == "s_bitset1_b64" || n == "s_bitreplicate_b64_b32";
const bool uncertain_scalar = (inst.enc == Encoding::Sopk && n != "s_movk_i32") ||
// SOPK operations that write only their destination (or, for
// the counter waits and the accepted mode writes, nothing
// at all) and never read or write SCC. s_mulk_i32 and
// s_movk_i32 only write the destination in arithmetic();
// the waits are handled as no-ops in lower() and the mode
// writes as no-ops in consume_half_mode(). A setreg or mode
// write that consume_half_mode() does not accept still
// refuses at lowering, so classifying it here cannot admit
// a translation. SOPP operations are not listed: the
// uncertain test below never matches them, so the lowered
// ones (s_nop, s_waitcnt, s_waitcnt_depctr, s_clause,
// s_inst_prefetch, s_setprio, s_barrier, s_round_mode,
// s_denorm_mode and the branches) fall through to the
// successor edges and preserve SCC the same way.
const bool preserves_sopk = n == "s_movk_i32" || n == "s_mulk_i32" ||
n == "s_setreg_b32" || n == "s_setreg_imm32_b32" ||
n == "s_waitcnt_vscnt" || n == "s_waitcnt_vmcnt" ||
n == "s_waitcnt_expcnt" || n == "s_waitcnt_lgkmcnt";
const bool uncertain_scalar = (inst.enc == Encoding::Sopk && !overwrites && !preserves_sopk) ||
(inst.enc == Encoding::Sop2 && !overwrites && !preserves_pair) ||
(inst.enc == Encoding::Sop1 && !overwrites && !preserves_sop1);
if (reads_scc || uncertain_scalar || (inst.branch() && (inst.op == 4 || inst.op == 5))) {
Expand Down
1 change: 1 addition & 0 deletions tests/checks/python-linux/native-scc-liveness.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/native_scc_liveness_check.py
11 changes: 11 additions & 0 deletions tests/cmake/native_scc_liveness.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
if(TARGET compile_dxil AND TARGET native_store_replay AND
EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${DXC_BIN_DIR}/dxil.dll")
add_test(NAME native_scc_liveness_controls COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/native_scc_liveness_check.py" controls
"$<TARGET_FILE:compile_dxil>" "${DXC_BIN_DIR}")
set_tests_properties(native_scc_liveness_controls PROPERTIES TIMEOUT 60)
add_test(NAME native_scc_liveness_hardware COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/native_scc_liveness_check.py" hardware
"$<TARGET_FILE:native_store_replay>" "${DXC_BIN_DIR}")
set_tests_properties(native_scc_liveness_hardware PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60)
endif()
Loading
Loading