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
46 changes: 46 additions & 0 deletions docs/experiments/native-direct-salu-cmov.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Scalar conditional moves in the direct C++ compute path (#3070)

`src/gfx1013_direct_compute.inl` now lowers `s_cmov_b32`, `s_cmov_b64` (SOP1) and `s_cmovk_i32` (SOPK) in the direct
compute path:

- **Operation:** `D = SCC ? S0 : D`, leaving `SCC` unchanged.
- **32-bit forms (`s_cmov_b32`, `s_cmovk_i32`):** destination is a general SGPR (`s0..s105`), `vcc_lo` (`106`), or wave32
`vcc_hi` (`107`). `s_cmovk_i32` sign-extends its 16-bit immediate; `s_cmov_b32` accepts a general SGPR, `vcc_lo`, wave32
`vcc_hi`, inline constant, or 32-bit literal.
- **64-bit form (`s_cmov_b64`):** destination is an aligned SGPR pair (`s0..s104` even) or `vcc` (`106`). `S0` accepts an
aligned SGPR pair, inline integer (`-16..64`, sign-extended when negative), or 32-bit literal (`pair_high()` zero-extends
32-bit literals consistently with `s_mov_b64`, `s_cselect_b64`, `s_not_b64`, and `s_lshr_b64`; unmeasured on wave32
hardware here).
- **Differences from [`tools/startup/gfx2hlsl.py`](../../tools/startup/gfx2hlsl.py) `scalar_cmov`:** the HLSL startup path
refuses `vcc` and lane-mask operands and takes only an SGPR pair as the 64-bit `s_cmov_b64` source, whereas the direct
compute path accepts `vcc` as the `s_cmov_b64` destination, wave32 `vcc_lo`/`vcc_hi` on the 32-bit forms, and inline
integer or 32-bit literal sources on `s_cmov_b64`.
- **Analysis & liveness:** reading the previous destination through `operand32()` refuses symbolic-descriptor destinations.
`transfer()` in `src/gfx1013_compute_native.cpp` clears constant/symbolic state on `s_cmovk_i32` (observed by refusing a
table descriptor load whose high word had been constant zero prior to `s_cmovk_i32`). `uses_scc_after()` in
`src/gfx1013_direct_compute.inl` marks all three instructions in `reads_scc` (matching the existing `uncertain_scalar`
coverage).

## Fixture

`tests/native_salu_cmov_check.py ir|controls|hardware` (`ctest --preset dev --no-tests=error -R '^native_salu_cmov_' -V`).
Encodings were verified with LLVM 15.0.7 (`clang -cc1as -triple amdgcn -target-cpu gfx1013 -filetype asm -show-encoding`).

- **IR (`native_salu_cmov_ir`):** dumps DXIL IR via `native_compute_ir_dump` for all 24 cases (`SCC = 0` and `SCC = 1` across
all 12 accepted forms: `s_cmov_b32` SGPR, literal, `1.0`, wave32 `vcc_hi` dst and src; `s_cmovk_i32` `-1234`, `0x7FFF`,
wave32 `vcc_hi` dst; `s_cmov_b64` pair, `-1`, literal `0x80000001`, `vcc` dst), evaluates lane 0 to verify exact
`D = SCC ? S0 : D`, preserved `SCC`, untouched neighbor words, and sign-extended `-1234` (`4294966062` rather than
`64302`), verifies the 8 invalid operand refusals, verifies 3 no-epilogue symbolic-descriptor destination refusals at exact
pc `+0x20` (`unbound numeric SGPR s24`), and verifies the `transfer()` `s_cmovk_i32` positive baseline + refusal at `+0x10`
(`direct descriptor address outside bound table`).
- **Controls (`native_salu_cmov_controls`):** compiles all 12 accepted forms into DXIL via `compile_dxil` and checks
`dxc.exe -dumpbin` for `select i1`, runs the same `transfer()` baseline and 12 exact diagnostic/pc refusals, and checks
the 5 mutant references differ.
- **Hardware (`native_salu_cmov_hardware`):** dispatches all 24 cases via `native_store_replay --direct` and checks output
words, preserved `SCC`, and guard words.

Measured on 27.11.2026, `main` `429f11d5` plus this change, Windows 10.0.26100 (`dev` preset): `native_salu_cmov_ir`,
`native_salu_cmov_controls`, and all 47 `native_*_(controls|ir)` tests passed; `native_salu_cmov_hardware` skipped (`77`,
Quadro M1000M does not support shader model 6.6 with wave32, so GPU readback is not measured). Six deliberate broken
builds (`ignores_scc`, `inverted_select`, `writes_nothing`, `zero_ext_cmovk`, `keeps_old_high_word`, and dropping
`s_cmovk_i32` from `transfer()`) each failed `native_salu_cmov_ir`.
4 changes: 2 additions & 2 deletions src/gfx1013_compute_native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,8 @@ State transfer(const Inst &i, State s, bool wave64, bool graphics, bool heap_win
} else if (i.name == "s_movk_i32") {
set(i.dst, {Symbol::Type::constant, Word(int16_t(i.a))});
} else if (i.enc == Encoding::Sop1 || i.enc == Encoding::Sop2 || i.enc == Encoding::Vopc ||
i.name == "s_mulk_i32" || i.name == "s_addk_i32" || i.name == "v_readfirstlane_b32" ||
i.name == "v_readlane_b32")
i.name == "s_mulk_i32" || i.name == "s_addk_i32" || i.name == "s_cmovk_i32" ||
i.name == "v_readfirstlane_b32" || i.name == "v_readlane_b32")
{
set(i.dst, {});
if ((wave64 && i.enc == Encoding::Vopc) || ((i.enc == Encoding::Sop1 || i.enc == Encoding::Sop2) &&
Expand Down
29 changes: 29 additions & 0 deletions src/gfx1013_direct_compute.inl
Original file line number Diff line number Diff line change
Expand Up @@ -1628,6 +1628,7 @@ struct DirectCompute {
const auto &inst = program.code[j];
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" ||
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 Down Expand Up @@ -2050,6 +2051,34 @@ struct DirectCompute {
write(i.dst, to_string(Word(int16_t(i.a))));
return;
}
if (compute && (n == "s_cmov_b32" || n == "s_cmov_b64" || n == "s_cmovk_i32")) {
// RDNA 2 ISA SOP1/SOPK (#3070): D = SCC ? S0 : D; SCC is unchanged.
const bool pair = n == "s_cmov_b64";
if (pair ? ((i.dst & 1) || (i.dst > 104 && i.dst != 106))
: (i.dst > 106 && !(!wave64 && i.dst == 107)))
refuse(i.pc, "invalid " + string(n) + " destination");
const unsigned s0 = i.src[0];
if (pair) {
if (!((s0 <= 104 && !(s0 & 1)) || (s0 >= 128 && s0 <= 208) || s0 == 255))
refuse(i.pc, "invalid s_cmov_b64 source");
} else if (n == "s_cmov_b32") {
if (s0 > 106 && !(!wave64 && s0 == 107) && !constant32(i, s0))
refuse(i.pc, "invalid s_cmov_b32 source");
}
auto cond = cmp("ne", read(128), "0");
auto old_lo = operand32(i, i.dst);
if (pair) {
auto old_hi = operand32(i, i.dst + 1);
auto new_lo = operand32(i, s0);
auto new_hi = pair_high(i, s0);
write(i.dst, select(cond, new_lo, old_lo));
write(i.dst + 1, select(cond, new_hi, old_hi));
} else {
auto next = n == "s_cmovk_i32" ? to_string(Word(int16_t(i.a))) : operand32(i, s0);
write(i.dst, select(cond, next, old_lo));
}
return;
}
if (compute && (n == "v_readlane_b32" || n == "v_writelane_b32")) {
if (i.enc != Encoding::Vop3 || i.dpp_control || (i.a & 0x7800u) || i.src[2])
refuse(i.pc, "lane read/write encoding or operand modifiers");
Expand Down
17 changes: 17 additions & 0 deletions tests/cmake/native_salu_cmov.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
if(TARGET native_compute_ir_dump AND EXISTS "${INTAKE_PYTHON}")
add_test(NAME native_salu_cmov_ir COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/native_salu_cmov_check.py" ir
"$<TARGET_FILE:native_compute_ir_dump>")
set_tests_properties(native_salu_cmov_ir PROPERTIES TIMEOUT 60)
endif()
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_salu_cmov_controls COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/native_salu_cmov_check.py" controls
"$<TARGET_FILE:compile_dxil>" "${DXC_BIN_DIR}")
set_tests_properties(native_salu_cmov_controls PROPERTIES TIMEOUT 60)
add_test(NAME native_salu_cmov_hardware COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/native_salu_cmov_check.py" hardware
"$<TARGET_FILE:native_store_replay>" "${DXC_BIN_DIR}")
set_tests_properties(native_salu_cmov_hardware PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60)
endif()
Loading
Loading