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
48 changes: 48 additions & 0 deletions docs/experiments/native-saveexec-wrexec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Remaining SOP1 `*_saveexec_*` and `*_wrexec_*` instructions in direct Compute DXIL

Issue #3296 (Refs #2935) lowers the remaining 12 SOP1 EXEC-mask instructions in
the direct Compute DXIL path (`src/gfx1013_direct_compute.inl`), updates
`uses_scc_after()` so `SCC` liveness treats all scalar mask and SAVEEXEC/WREXEC
definitions consistently, and updates `transfer()` in
`src/gfx1013_compute_native.cpp` so all wave32 `*_saveexec_b32` and
`*_wrexec_b32` instructions clear symbolic provenance on both the destination
register and `EXEC_LO`.

## Lowered Instructions

- **Wave32 (`SOP1`, 32-bit):**
- `s_xor_saveexec_b32` (`0x3e`): `D = EXEC_LO; EXEC_LO = S0 ^ EXEC_LO`
- `s_andn2_saveexec_b32` (`0x3f`): `D = EXEC_LO; EXEC_LO = S0 & ~EXEC_LO`
- `s_orn2_saveexec_b32` (`0x40`): `D = EXEC_LO; EXEC_LO = S0 | ~EXEC_LO`
- `s_nand_saveexec_b32` (`0x41`): `D = EXEC_LO; EXEC_LO = ~(S0 & EXEC_LO)`
- `s_nor_saveexec_b32` (`0x42`): `D = EXEC_LO; EXEC_LO = ~(S0 | EXEC_LO)`
- `s_xnor_saveexec_b32` (`0x43`): `D = EXEC_LO; EXEC_LO = ~(S0 ^ EXEC_LO)`
- `s_andn1_saveexec_b32` (`0x44`): `D = EXEC_LO; EXEC_LO = ~S0 & EXEC_LO`
- `s_orn1_saveexec_b32` (`0x45`): `D = EXEC_LO; EXEC_LO = ~S0 | EXEC_LO`
- `s_andn1_wrexec_b32` (`0x46`): `EXEC_LO = ~S0 & EXEC_LO; D = EXEC_LO`
- **Wave64 (`SOP1`, 64-bit):**
- `s_andn1_saveexec_b64` (`0x37`): `D = EXEC; EXEC = ~S0 & EXEC`
- `s_orn1_saveexec_b64` (`0x38`): `D = EXEC; EXEC = ~S0 | EXEC`
- `s_andn1_wrexec_b64` (`0x39`): `EXEC = ~S0 & EXEC; D = EXEC`

All 12 instructions update `SCC` from `(new_exec != 0)` across all participating
bits (`EXEC_LO` in wave32; both `EXEC_LO` and `EXEC_HI` in wave64).

## Destination & Mode Rules

- Newly lowered wave32 ops require `dst <= 107` (`SGPR` / `VCC`), refusing
`M0`/`EXEC_LO` destinations with `unsupported wave32 SAVEEXEC destination`, and
are restricted to wave32 compute shaders.
- `s_andn1_wrexec_b64` matches `s_andn2_wrexec_b64` rules (`compute && wave64`,
even SGPR pair `<= 104` for both `dst` and `src`).

## Verification

- [native_saveexec_wrexec_check.py](../../tests/native_saveexec_wrexec_check.py)
covers all 12 instructions across non-zero (`SCC = 1`), zero-result
(`SCC = 0`), high-word-only (`SCC = 1` from `EXEC_HI`), `dst == src` aliasing,
`vcc_lo`/`exec_lo`/inline/literal sources, partial-wave dispatches, and 8
negative operand/mode refusal cases.
- CMake/CTest targets (`tests/cmake/native_saveexec_wrexec.cmake`):
`native_saveexec_wrexec_ir`, `native_saveexec_wrexec_controls`, and
`native_saveexec_wrexec_hardware`.
6 changes: 4 additions & 2 deletions src/gfx1013_compute_native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,10 @@ State transfer(const Inst &i, State s, bool wave64, bool graphics, bool heap_win
set(i.dst, v);
if (i.name == "s_mov_b64")
set(i.dst + 1, high);
} else if (!graphics && !wave64 && (i.name == "s_and_saveexec_b32" || i.name == "s_or_saveexec_b32")) {
// Both the saved EXEC and the updated EXEC are numeric mask words.
} else if (!graphics && !wave64 && i.enc == Encoding::Sop1 &&
((i.name.size() > 13 && i.name.substr(i.name.size() - 13) == "_saveexec_b32") ||
i.name == "s_andn1_wrexec_b32" || i.name == "s_andn2_wrexec_b32")) {
// Both the saved/written destination and the updated EXEC are numeric mask words.
set(i.dst, {});
set(126, {});
} else if (i.name == "s_and_b32" || i.name == "s_lshl_b32") {
Expand Down
56 changes: 37 additions & 19 deletions src/gfx1013_direct_compute.inl
Original file line number Diff line number Diff line change
Expand Up @@ -2349,20 +2349,26 @@ struct DirectCompute {
// result and do not consume the incoming SCC. An explicit
// SRC_SCC operand still takes precedence below.
const bool mask_scc = n == "s_andn2_b64" || n == "s_and_b64" || n == "s_or_b64" ||
n == "s_xor_b64" || n == "s_not_b64" || n == "s_wqm_b32" || n == "s_wqm_b64" ||
n == "s_xor_b64" || n == "s_orn2_b64" || n == "s_nand_b64" || n == "s_nor_b64" ||
n == "s_xnor_b64" || n == "s_not_b64" || n == "s_wqm_b32" || n == "s_wqm_b64" ||
n == "s_quadmask_b32" || n == "s_quadmask_b64" ||
n == "s_and_saveexec_b64" || n == "s_or_saveexec_b64" || n == "s_xor_saveexec_b64" ||
n == "s_andn2_saveexec_b64" || n == "s_orn2_saveexec_b64" ||
n == "s_nand_saveexec_b64" || n == "s_nor_saveexec_b64" || n == "s_xnor_saveexec_b64" ||
n == "s_andn2_wrexec_b64";
n == "s_andn1_saveexec_b64" || n == "s_orn1_saveexec_b64" ||
n == "s_andn1_wrexec_b64" || n == "s_andn2_wrexec_b64";
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" ||
n == "s_or_b32" || n == "s_max_u32" || n == "s_add_u32" || n == "s_sub_u32" ||
n == "s_add_i32" || n == "s_sub_i32" || n == "s_and_saveexec_b32" ||
n == "s_or_saveexec_b32" || n == "s_andn2_wrexec_b32" || n == "s_andn2_b32" || n == "s_orn2_b32" ||
n == "s_nor_b32" || n == "s_nand_b32" || n == "s_xnor_b32" || n == "s_not_b32" ||
n == "s_or_saveexec_b32" || n == "s_xor_saveexec_b32" || n == "s_andn2_saveexec_b32" ||
n == "s_orn2_saveexec_b32" || n == "s_nand_saveexec_b32" || n == "s_nor_saveexec_b32" ||
n == "s_xnor_saveexec_b32" || n == "s_andn1_saveexec_b32" || n == "s_orn1_saveexec_b32" ||
n == "s_andn1_wrexec_b32" || n == "s_andn2_wrexec_b32" || n == "s_andn2_b32" ||
n == "s_orn2_b32" || n == "s_nor_b32" || n == "s_nand_b32" || n == "s_xnor_b32" ||
n == "s_not_b32" ||
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" ||
Expand Down Expand Up @@ -3386,9 +3392,21 @@ struct DirectCompute {
write(128, ir.value("zext i1 " + carry + " to i32"));
return;
}
auto combine_scalar_mask = [&](Name op, const string &left, const string &right) -> string {
if (op == "and" || op == "or" || op == "xor")
return ir.binary(string(op), left, right);
if (op == "andn2" || op == "orn2")
return ir.binary(op == "andn2" ? "and" : "or", left, ir.binary("xor", right, "4294967295"));
if (op == "andn1" || op == "orn1")
return ir.binary(op == "andn1" ? "and" : "or", ir.binary("xor", left, "4294967295"), right);
if (op == "nand" || op == "nor" || op == "xnor")
return ir.binary("xor", ir.binary(op == "nand" ? "and" : op == "nor" ? "or" : "xor", left, right),
"4294967295");
return {};
};
if ((i.enc == Encoding::Sop1 || i.enc == Encoding::Sop2) && n.size() > 6 && n.substr(n.size() - 4) == "_b64") {
bool save = n.find("_saveexec_") != Name::npos;
const bool wrexec = compute && wave64 && n == "s_andn2_wrexec_b64";
const bool wrexec = compute && wave64 && (n == "s_andn2_wrexec_b64" || n == "s_andn1_wrexec_b64");
auto op = n.substr(2, n.find('_', 2) - 2);
if (wrexec ? ((i.dst | i.src[0]) & 1) || i.dst > 104 || i.src[0] > 104
: (i.dst & 1) || (i.dst > 104 && i.dst != 106 && i.dst != 126) ||
Expand All @@ -3398,31 +3416,31 @@ struct DirectCompute {
const bool updates_exec = save || wrexec;
auto bl = updates_exec ? read(126) : b;
auto bh = updates_exec ? read(127) : pair_high(i, i.src[1]);
auto combine = [&](const string &left, const string &right) -> string {
if (op == "and" || op == "or" || op == "xor") return ir.binary(string(op), left, right);
if (op == "andn2" || op == "orn2")
return ir.binary(op == "andn2" ? "and" : "or", left, ir.binary("xor", right, "4294967295"));
if (op == "nand" || op == "nor" || op == "xnor")
return ir.binary("xor", ir.binary(op == "nand" ? "and" : op == "nor" ? "or" : "xor", left, right), "4294967295");
refuse(i.pc, "unsupported wave64 scalar mask operation");
};
// SAVEEXEC and WREXEC combine the source with old EXEC in ISA operand order.
// WREXEC stores the new mask in the destination pair.
auto lo = combine(a, bl), hi = combine(ah, bh);
auto lo = combine_scalar_mask(op, a, bl), hi = combine_scalar_mask(op, ah, bh);
if (lo.empty())
refuse(i.pc, "unsupported wave64 scalar mask operation");
write(i.dst, save ? bl : lo);
write(i.dst + 1, save ? bh : hi);
if (updates_exec) { write(126, lo); write(127, hi); }
write(128, ir.value("zext i1 " + cmp("ne", ir.binary("or", lo, hi), "0") + " to i32"));
return;
}
const bool or_saveexec = n == "s_or_saveexec_b32" && compute && !wave64;
if (or_saveexec || n == "s_and_saveexec_b32" || n == "s_andn2_wrexec_b32") {
const bool save = n != "s_andn2_wrexec_b32";
if (or_saveexec && i.dst > 107)
const bool saveexec32 = i.enc == Encoding::Sop1 && n.size() > 13 && n.substr(n.size() - 13) == "_saveexec_b32";
const bool wrexec32 = n == "s_andn2_wrexec_b32" || (compute && !wave64 && n == "s_andn1_wrexec_b32");
if (n == "s_and_saveexec_b32" || n == "s_andn2_wrexec_b32" ||
(compute && !wave64 && (saveexec32 || wrexec32))) {
const bool save = saveexec32;
const bool strict_dst = n != "s_and_saveexec_b32" && n != "s_andn2_wrexec_b32";
if (strict_dst && i.dst > 107)
refuse(i.pc, "unsupported wave32 SAVEEXEC destination");
auto op = n.substr(2, n.find('_', 2) - 2);
auto old = read(126);
// Like s_mov_b32 exec_lo, EXEC keeps every bit; a lane runs only if its own bit is set.
auto mask = ir.binary(or_saveexec ? "or" : "and", a, save ? old : ir.binary("xor", old, "4294967295"));
auto mask = combine_scalar_mask(op, a, old);
if (mask.empty())
refuse(i.pc, "unsupported wave32 scalar mask operation");
write(i.dst, save ? old : mask);
write(126, mask);
write(128, ir.value("zext i1 " + cmp("ne", mask, "0") + " to i32"));
Expand Down
17 changes: 17 additions & 0 deletions tests/cmake/native_saveexec_wrexec.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_saveexec_wrexec_ir COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/native_saveexec_wrexec_check.py" ir
"$<TARGET_FILE:native_compute_ir_dump>")
set_tests_properties(native_saveexec_wrexec_ir PROPERTIES TIMEOUT 60)
endif()
if(TARGET compile_dxil AND TARGET native_store_replay AND EXISTS "${INTAKE_PYTHON}" AND
EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${DXC_BIN_DIR}/dxil.dll")
add_test(NAME native_saveexec_wrexec_controls COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/native_saveexec_wrexec_check.py" controls
"$<TARGET_FILE:compile_dxil>" "${DXC_BIN_DIR}")
set_tests_properties(native_saveexec_wrexec_controls PROPERTIES TIMEOUT 60)
add_test(NAME native_saveexec_wrexec_hardware COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/native_saveexec_wrexec_check.py" hardware
"$<TARGET_FILE:native_store_replay>" "${DXC_BIN_DIR}")
set_tests_properties(native_saveexec_wrexec_hardware PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 120)
endif()
Loading
Loading