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 src/frozen_compute_requests.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class FrozenComputeRequests {
void put(ComputeRequest request) {
const auto bytes = size(request);
if (!max_entries_ || bytes > max_bytes_) return;
request.wave32 = compute_request_wave32(request.sa);
std::lock_guard guard(lock_);
if (index_.count(request.key)) return;
while (requests_.size() >= max_entries_ || bytes_ > max_bytes_ - bytes) {
Expand Down
10 changes: 7 additions & 3 deletions src/shader_compute_request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,7 @@ x2w::ComputeRequest x2w::read_compute_request(const fs::path &marker) {
throw std::runtime_error("compute request key mismatch");
ComputeRequest request{std::move(code), std::move(data), std::move(root), std::move(desc), std::move(sa), std::move(sb), expected};
validate_compute_request_shape(request);
// Bit 20 of dword 4 (bits 15:0 are Z): set for wave32, clear for wave64. Inferred from captured requests
// (docs/experiments/native-slot67-wave-size.md). Read only after the shape check has proved 28 bytes.
request.wave32 = (static_cast<unsigned char>(request.sa[18]) & 0x10) != 0;
request.wave32 = compute_request_wave32(request.sa);
return request;
}

Expand All @@ -334,6 +332,12 @@ void x2w::validate_compute_request_shape(const ComputeRequest &r) {
throw std::runtime_error(r.sa.size() != 28 ? "compute state block A is not 28 bytes" : "inconsistent compute request");
}

bool x2w::compute_request_wave32(std::string_view state_a) noexcept {
// Bit 20 of dword 4 (bits 15:0 are Z): set for wave32, clear for wave64. Inferred from captured requests
// (docs/experiments/native-slot67-wave-size.md). An ill-sized state block is rejected by shape validation.
return state_a.size() != 28 || (static_cast<unsigned char>(state_a[18]) & 0x10) != 0;
}

std::string x2w::read_shader_container_bytes(const fs::path &file) {
std::ifstream stream(file, std::ios::binary | std::ios::ate);
auto length = stream.tellg();
Expand Down
1 change: 1 addition & 0 deletions src/shader_compute_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ ComputeContainer read_compute_container(const std::filesystem::path &file);
// Verify the content key over all six immutable capture parts before exposing any bytes.
ComputeRequest read_compute_request(const std::filesystem::path &marker);
void validate_compute_request_shape(const ComputeRequest &request);
bool compute_request_wave32(std::string_view state_a) noexcept;
} // namespace x2w
25 changes: 17 additions & 8 deletions tests/native_frozen_request_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
#include <stdexcept>

namespace {
x2w::ComputeRequest fixture(uint64_t key) {
x2w::ComputeRequest r{"code", "data", "root", std::string(96, 0), std::string(28, 0), {}, key};
constexpr std::string_view program{"\x00\x00\x81\xbf", 4}; // s_endpgm

x2w::ComputeRequest fixture(uint64_t key, bool wave32) {
x2w::ComputeRequest r{std::string(program), "data", "root", std::string(96, 0), std::string(28, 0), {}, key};
const uint32_t state[] = {0, 0, 0, 64 | (1 << 16), wave32 ? 1u | (1u << 20) : 1u, 0, 0};
std::memcpy(r.sa.data(), state, sizeof(state));
const auto length = [&](size_t offset, uint64_t n) { std::memcpy(r.description.data() + offset, &n, sizeof(n)); };
length(48, r.code.size());
length(64, r.data.size());
Expand All @@ -20,18 +24,23 @@ void require(bool ok, const char *reason) {
int main() {
try {
x2w::FrozenComputeRequests pool(4096, 2);
auto first = fixture(1);
auto first = fixture(1, false);
pool.put(first);
first.code[0] = 'x';
auto saved = pool.take(1);
require(saved && saved->code == "code", "snapshot changed with caller");
require(saved && saved->code == program, "snapshot changed with caller");
x2w::validate_compute_request_shape(*saved);
require(!saved->wave32, "wave64 state was lost in the frozen request");
require(!pool.take(1), "consumed snapshot still present");
pool.put(first);
pool.put(fixture(2));
pool.put(fixture(2));
pool.put(fixture(3));
require(!pool.take(1) && pool.take(2) && pool.take(3), "FIFO eviction or duplicate key failed");
pool.put(fixture(2, true));
pool.put(fixture(2, true));
pool.put(fixture(3, true));
require(!pool.take(1), "FIFO eviction failed");
auto wave32 = pool.take(2);
require(wave32 && pool.take(3), "FIFO eviction or duplicate key failed");
x2w::validate_compute_request_shape(*wave32);
require(wave32->wave32, "wave32 state was lost in the frozen request");
auto stats = pool.stats();
require(stats.hits == 3 && stats.misses == 2 && stats.evicted == 1 && !stats.bytes && !stats.entries,
"handoff accounting mismatch");
Expand Down
Loading