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
2 changes: 2 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ BOUND_CTRL to unusable sources. [Experiment and reproduction commands](experimen
Lane reads may refill `vcc_lo` and, in wave32, `vcc_hi` as ordinary scalar data.
[Experiment and reproduction commands](experiments/native-lane-access-compute.md)

[Descriptor lane spill experiment](experiments/native-lane-spill-descriptor.md)

## 64-bit scalar operations in direct compute

`s_bfm_b64`, `s_cselect_b64` and `s_not_b64` lower to two scalar words.
Expand Down
35 changes: 35 additions & 0 deletions docs/experiments/native-lane-spill-descriptor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Descriptor lane spills in direct compute DXIL

The direct C++ path can carry an immediate table descriptor, a table
pointer word, a root address word or an unbound value through a constant
guest VGPR lane. An unbound value refilled is still unbound and is refused
where it is used.
The CFG tracks the identity per VGPR and guest lane. A symbolic spill or
refill emits no DXIL. A refilled descriptor still has to pass the usual
binding checks. A numeric write clears only its lane's identity. Any
other write to a VGPR holding symbolic lanes is refused.
Joins require the same identity on each path. If any path has a symbol
but the identity differs, the refill is refused.

Numeric lane operations use the existing DXIL lowering. In a split
guest wave64, that lowering selects the physical lane and exchanges
values between the wave32 halves as needed. Symbolic slots use the guest
lane before the physical mapping. Indexed descriptors that carry a
runtime index remain outside this experiment.

The source-built fixture spills four SRV descriptor words into lanes
0 through 3 and two numeric words into lanes 4 and 5, then clobbers the
descriptor SGPRs. It refills the six words and stores the loaded SRV
value and both numeric values for every lane. The CPU reference covers
192 written words across two wave32 workgroups and 1,872 guard and
unwritten words. Compile controls cover VGPR and SGPR lane selectors,
a VALU overwrite followed by descriptor refill, a conflicting CFG
join, and a direct VALU read of a VGPR with symbolic lanes.

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

The fixture is registered for Microsoft WARP and the default hardware
adapter. It requires shader model 6.6 and wave32 support.
123 changes: 111 additions & 12 deletions src/gfx1013_compute_native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,82 @@ struct Symbol {
bool operator==(const Symbol &r) const { return type == r.type && value == r.value && offset == r.offset &&
index_mask == r.index_mask && definition == r.definition; }
};
using State = std::array<Symbol, 128>;
struct State {
std::array<Symbol, 128> scalar{};
// Encoded VGPR and guest lane. A join can lose the identity while
// retaining the fact that some path omitted the runtime lane write.
std::map<std::pair<unsigned, unsigned>, std::optional<Symbol>> lane_spills;
Symbol &operator[](unsigned n) { return scalar[n]; }
const Symbol &operator[](unsigned n) const { return scalar[n]; }
const Symbol &at(unsigned n) const { return scalar.at(n); }
size_t size() const { return scalar.size(); }
auto begin() { return scalar.begin(); }
auto end() { return scalar.end(); }
auto begin() const { return scalar.begin(); }
auto end() const { return scalar.end(); }
bool has_spill(unsigned reg) const {
const auto it = lane_spills.lower_bound({reg, 0});
return it != lane_spills.end() && it->first.first == reg;
}
void clear_spills(unsigned reg, unsigned count = 1) {
lane_spills.erase(lane_spills.lower_bound({reg, 0}), lane_spills.lower_bound({reg + count, 0}));
}
};
static std::optional<Word> constant32(const Inst &i, unsigned r) {
static constexpr Word floats[]{0x3f000000, 0xbf000000, 0x3f800000, 0xbf800000, 0x40000000,
0xc0000000, 0x40800000, 0xc0800000, 0x3e22f983};
if (r >= 128 && r <= 192) return r - 128;
if (r >= 193 && r <= 208) return Word(192) - r;
if (r >= 240 && r <= 248) return floats[r - 240];
if (r == 255) return i.literal;
return {};
}
static std::optional<unsigned> guest_spill_lane(const Inst &i, bool wave64) {
if (auto selector = constant32(i, i.src[1]))
return *selector & (wave64 ? 63 : 31);
return {};
}
static bool identity_spill(const Symbol &s) {
// Indexed descriptors and their derived fields still use an SGPR's
// runtime index. Erasing a lane operation would lose that payload.
// An unbound value stays unbound after a refill, so it is refused at its use.
return !s.numeric() &&
s.type != Symbol::Type::indexed_descriptor && !s.definition;
}
static void clear_vector_spills(const Inst &i, State &s) {
if (s.lane_spills.empty()) return;
switch (i.enc) {
case Encoding::Vop1: case Encoding::Vop2: case Encoding::Vop3: case Encoding::Mix:
if (i.dst >= 256 && i.name != "v_nop") {
const bool pair = i.name == "v_mad_u64_u32" || i.name == "v_mad_i64_i32" || i.name == "v_lshlrev_b64" ||
i.name == "v_lshrrev_b64" || i.name == "v_ashrrev_i64";
s.clear_spills(i.dst, pair ? 2 : 1);
if (i.name == "v_swap_b32") s.clear_spills(i.src[0]);
}
break;
case Encoding::Mubuf:
if (i.name == "buffer_load_dword") {
constexpr unsigned widths[]{1, 2, 4, 3};
s.clear_spills(256 + i.dst, widths[i.op & 3]);
} else if (i.name == "buffer_load_format" || i.name.substr(0, 8) == "tbuffer_")
s.clear_spills(256 + i.dst, (i.op & 3) + 1);
else if (i.name == "global_load_dword" ||
(i.name.substr(0, 14) == "buffer_atomic_" && (i.a & 0x4000)))
s.clear_spills(256 + i.dst);
break;
case Encoding::Image:
if (i.op != 8) {
unsigned count = 0;
for (unsigned mask = (i.a >> 8) & 15; mask; mask >>= 1)
count += mask & 1;
if (i.op == 68) count = 4;
if (i.b >> 31) count = (count + 1) / 2;
s.clear_spills(256 + i.dst, count);
}
break;
default: break;
}
}
// FSSM root descriptors carry a guest address in two SGPRs. Preserve the
// identity of each half through scalar moves and CFG joins (Victory resource.rs).
void bind_root_address(State &state, const ShaderRootInput &input, unsigned limit) {
Expand Down Expand Up @@ -486,12 +561,32 @@ static std::optional<Symbol> byte_descriptor_field(const Inst &i, const State &s
return field(a,T::byte_oob);
return {};
}
State transfer(const Inst &i, State s, bool wave64, bool graphics, bool heap_window = false) {
State transfer(const Inst &i, State s, bool wave64, bool graphics, bool heap_window, bool direct) {
auto set = [&](unsigned n, Symbol v) {
if (n >= s.size())
refuse(i.pc, "invalid SGPR");
s[n] = v;
};
if (direct && !graphics) {
if (i.name == "v_writelane_b32") {
if (auto lane = guest_spill_lane(i, wave64)) {
const auto key = std::make_pair(i.dst, *lane);
const auto value = source(i, i.src[0], s);
if (i.src[0] <= 105 && identity_spill(value))
s.lane_spills[key] = value;
else
s.lane_spills.erase(key);
}
return s;
}
if (i.name == "v_readlane_b32") {
const auto lane = guest_spill_lane(i, wave64);
const auto slot = lane ? s.lane_spills.find({i.src[0], *lane}) : s.lane_spills.end();
set(i.dst, slot != s.lane_spills.end() && slot->second ? *slot->second : Symbol{});
return s;
}
clear_vector_spills(i, s);
}
auto mask = carry_destination(i);
const bool compared = graphics && i.enc == Encoding::Vopc && i.dst < 106 && i.name.substr(0,6) == "v_cmp_";
if (mask || compared) {
Expand Down Expand Up @@ -696,14 +791,14 @@ Program parse(const string &code, unsigned entry, const State &initial, bool gra
auto k = work.front();
work.pop_front();
auto &i = p.code[k];
auto after = transfer(i, p.before[k], wave64, graphics, heap_window);
auto after = transfer(i, p.before[k], wave64, graphics, heap_window, graph);
auto merge = [&](size_t t) {
bool changed = !p.reached[t];
auto &in = p.before[t];
if (changed) {
in = after;
p.reached[t] = true;
} else
} else {
for (unsigned n = 0; n < in.size(); ++n)
if (!(in[n] == after[n])) {
Symbol merged{};
Expand All @@ -714,6 +809,16 @@ Program parse(const string &code, unsigned entry, const State &initial, bool gra
changed = true;
}
}
for (auto &[key, value] : in.lane_spills) {
const auto other = after.lane_spills.find(key);
if (value && (other == after.lane_spills.end() || !(other->second == value))) {
value.reset();
changed = true;
}
}
for (const auto &slot : after.lane_spills)
changed |= in.lane_spills.emplace(slot.first, std::nullopt).second;
}
if (changed)
work.push_back(t);
};
Expand All @@ -732,16 +837,10 @@ string reg(unsigned n) { return n >= 256 ? "v" + to_string(n - 256) : "s" + to_s
string operand(const Inst &i, unsigned n) {
if ((n <= 105) || n == 106 || n == 126 || n >= 256)
return reg(n);
if (n >= 128 && n <= 208)
return hexword(n <= 192 ? n - 128 : 192u - n);
if (n == 253)
return "uint(scc)";
if (n == 255)
return hexword(i.literal);
constexpr Word f[] = {0x3f000000, 0xbf000000, 0x3f800000, 0xbf800000, 0x40000000,
0xc0000000, 0x40800000, 0xc0800000, 0x3e22f983};
if (n >= 240 && n <= 248)
return hexword(f[n - 240]);
if (auto constant = constant32(i, n))
return hexword(*constant);
refuse(i.pc, "unsupported operand " + to_string(n));
}
enum class AluType { Bits, Float, ToFloat };
Expand Down
59 changes: 42 additions & 17 deletions src/gfx1013_direct_compute.inl
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
// Lower the shared decoded CFG to DXIL operations. A scalar register holds one
// 32-bit word per wave. Ordinary vector writes retain their old value on inactive lanes.
struct DirectCompute {
static constexpr Word inline_float_words[]{0x3f000000, 0xbf000000, 0x3f800000, 0xbf800000, 0x40000000,
0xc0000000, 0x40800000, 0xc0800000, 0x3e22f983};
// The same nine constants as binary16, which an f16 operand reads instead of the f32 pattern.
// The nine inline float constants as binary16, which an f16 operand reads instead of the f32 pattern.
static constexpr Word inline_half_words[]{0x3800, 0xb800, 0x3c00, 0xbc00, 0x4000,
0xc000, 0x4400, 0xc400, 0x3118};
static std::optional<Word> constant32(const Inst &i, unsigned r) {
if (r >= 128 && r <= 192) return r - 128;
if (r >= 193 && r <= 208) return Word(192) - r;
if (r >= 240 && r <= 248) return inline_float_words[r - 240];
if (r == 255) return i.literal;
return {};
}
static void validate_lane_shape(const Inst &i, unsigned threads, unsigned width) {
const auto constant = constant32(i, i.src[1]);
const auto remaining = threads % width;
Expand Down Expand Up @@ -50,6 +41,8 @@ struct DirectCompute {
std::map<unsigned, string> graphics_registers;
string graphics_predicate;
unsigned graphics_pc = 0;
const State *compute_state = nullptr;
unsigned compute_pc = 0;
bool perspective_center_pair = false;
bool exact_pixel_entry = false;
bool helper_result_unobserved = false;
Expand All @@ -72,7 +65,7 @@ struct DirectCompute {
ir.setup << " " << name << " = alloca i32, align 4\n";
return name;
}
string read(unsigned r) {
string read_storage(unsigned r) {
if (graphics_input) {
auto guarded = guarded_ij.find(r);
if (guarded != guarded_ij.end())
Expand All @@ -89,7 +82,17 @@ struct DirectCompute {
}
return ir.value("load i32, i32* " + address(r) + ", align 4");
}
string read(unsigned r) {
if (compute_state && compute_state->has_spill(r))
refuse(compute_pc, "symbolic VGPR lane read of " + reg(r));
return read_storage(r);
}
bool lane_write = false; // v_writelane_b32 updates one lane of a VGPR that may hold symbolic lanes
void write(unsigned r, const string &value) {
// A VGPR write the analysis did not model as clearing its symbolic lanes
// would let a later v_readlane_b32 return a stale identity.
if (compute_state && !lane_write && compute_state->has_spill(r))
refuse(compute_pc, "write to a VGPR holding symbolic lanes: " + reg(r));
if (graphics_input) {
guarded_ij.erase(r);
graphics_registers[r] = value;
Expand Down Expand Up @@ -123,7 +126,7 @@ struct DirectCompute {
// subsequent implicit LOD or quad operation can inspect helpers.
write(r, value);
} else
write(r, select(graphics_input ? graphics_predicate : active(), value, read(r)));
write(r, select(graphics_input ? graphics_predicate : active(), value, read_storage(r)));
}
string operand32(const Inst &i, unsigned n) {
if (graphics_input && n == 106)
Expand Down Expand Up @@ -2159,19 +2162,37 @@ struct DirectCompute {
refuse(i.pc, "lane read/write register operand");
const unsigned width = wave64 ? 64 : 32;
const unsigned selector = i.src[1];
const auto guest = guest_spill_lane(i, wave64);
const unsigned vector = reading ? i.src[0] : i.dst;
if (guest && reading) {
const auto slot = state.lane_spills.find({vector, *guest});
if (slot != state.lane_spills.end()) {
if (!slot->second)
refuse(i.pc, "symbolic VGPR lane read has conflicting incoming values");
if (i.dst > 105)
refuse(i.pc, "symbolic VGPR lane read needs an ordinary SGPR destination");
return; // Transfer associated the destination SGPR with this identity.
}
}
if (!reading && i.src[0] <= 105 && identity_spill(state[i.src[0]])) {
if (!guest)
refuse(i.pc, "symbolic VGPR lane write needs a constant lane");
return;
}
string selected;
if (selector <= 105 || (!wave64 && selector == 107)) {
if (state.has_spill(vector))
refuse(i.pc, "symbolic VGPR lane read or write needs a constant lane");
selected = ir.binary("and", val(1), to_string(width - 1));
} else {
const auto constant = constant32(i, selector);
if (!constant)
if (!guest)
refuse(i.pc, "lane read/write needs a constant or SGPR lane");
selected = to_string(*constant & (width - 1));
selected = to_string(*guest);
}
if (reading) {
const auto physical = wave64 ? ir.binary("and", selected, "31") : selected;
auto value = ir.invoke("i32", "waveReadLaneAt.i32", 117,
{{"i32", val(0)}, {"i32", physical}}, "nounwind");
{{"i32", read_storage(vector)}, {"i32", physical}}, "nounwind");
if (wave64) {
const auto halves = exchange_halves(value);
value = select(cmp("uge", selected, "32"), halves[1], halves[0]);
Expand All @@ -2180,7 +2201,9 @@ struct DirectCompute {
} else {
const auto guest_lane = wave64 ? ir.binary("or", lane, select(upper_half, "32", "0")) : lane;
// The lane selector is the sole write predicate, independent of guest EXEC.
write(i.dst, select(cmp("eq", guest_lane, selected), val(0), read(i.dst)));
lane_write = true;
write(i.dst, select(cmp("eq", guest_lane, selected), val(0), read_storage(i.dst)));
lane_write = false;
}
return;
}
Expand Down Expand Up @@ -3152,6 +3175,8 @@ struct DirectCompute {
}
for (size_t k = 0; k < p.code.size(); ++k) {
const auto &i = p.code[k];
compute_pc = i.pc;
compute_state = &p.before[k];
if (labels.count(i.pc)) {
if (k && p.code[k - 1].name != "s_endpgm" && !p.code[k - 1].branch())
ir.body << " br label %pc" << i.pc << "\n";
Expand Down
9 changes: 9 additions & 0 deletions tests/cmake/native_lane_spill_descriptor.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
if(TARGET compile_dxil AND TARGET native_store_replay AND
EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${DXC_BIN_DIR}/dxil.dll")
foreach(adapter IN ITEMS warp hardware)
add_test(NAME native_lane_spill_descriptor_${adapter} COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/native_lane_spill_descriptor_check.py" "$<TARGET_FILE:compile_dxil>"
"$<TARGET_FILE:native_store_replay>" "${DXC_BIN_DIR}" "${adapter}")
set_tests_properties(native_lane_spill_descriptor_${adapter} PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60)
endforeach()
endif()
Loading
Loading