Skip to content
Open
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
18 changes: 18 additions & 0 deletions docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,24 @@ rebuilds stale native outputs and leaves Python publications in place. The stand
absent manifest on subsequent dispatches on the ManifestRetry schedule (1 s, doubling up to 32 s), so a running process can
pick up a rebuilt output (by the code path; no test covers the reload). Compute request keys stay at `29cs`, and revision-28 archives are still read.

A compute program may read the width, base mip and emptiness of a 2D texture descriptor from a table
slot that it also samples with an image op: `s_bfe_u32` word 1 `0x2001e` (width low bits), `s_and_b32` word 2
`0x3fff` (width high bits), `s_bfe_u32` word 3 `0x4000c` (base level) and `s_cmp_eq/lg_i32/u32` word 1 against
inline 0. The bit positions follow the public AMD PAL `sq_img_rsrc_t` (gfx10 member for words 1 and 2, common
member for the base level). The translator replaces these reads with entry words (`udsrc <n> imgwidth <srv>` holds
width - 1, `udsrc <n> imgbase <srv>` the base mip, after the stride words) and the null check with a constant,
since the dispatch runs only with a resolved input texture. At Dispatch the stand-in fills the words from the
bound view: the resource's mip-0 width, and the described view's MostDetailedMip (0 for a view without
description). Only a 2D `texture_srv` is accepted (`image field words from a view that is not one 2D texture SRV`
otherwise). 96-bit and block-compressed formats are refused (`an image field source has a format whose
descriptor extent is not its width`). The runtime cannot see a padded view that PAL would program and assumes the
unpadded mip-0 extent. A read is replaced only when its word sits at the expected offset of a 32-byte aligned
descriptor record. Any other read of an image descriptor word keeps its refusal, and `NativeComputePipeline`
does not bind these words. Check with `ctest --preset dev -R startup_compute_dispatch_image_fields`
([`tests/startup_compute_dispatch_image_fields_check.py`](../tests/startup_compute_dispatch_image_fields_check.py)).
On WARP the translated module cannot run (wave32), so that test substitutes an HLSL program for the published
`.cs.cso` and covers the manifest, the reader and the fill there; the translation itself runs on hardware.

Check with `ctest --preset dev -R '^startup_compute_dispatch_native_(warp|hardware)$' -V`
([`tests/startup_compute_dispatch_native_check.py`](../tests/startup_compute_dispatch_native_check.py);
[route, controls and results](experiments/startup-compute-native-dispatch.md)).
Expand Down
18 changes: 17 additions & 1 deletion src/direct_compute_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ Bytes encode(const NativeComputeShader &program) {
word(out, static_cast<unsigned>(program.stride_srvs.size()));
for (unsigned srv : program.stride_srvs)
word(out, srv);
word(out, static_cast<unsigned>(program.image_fields.size()));
for (const auto &[srv, field] : program.image_fields) {
word(out, srv);
word(out, field);
}
word(out, static_cast<uint32_t>(program.samplers.size()));
for (const auto &s : program.samplers) {
for (auto n : s.words)
Expand Down Expand Up @@ -157,6 +162,11 @@ NativeComputeShader decode(std::string_view bytes) {
n = in.count(32);
for (unsigned j = 0; j < n; ++j)
shader.stride_srvs.push_back(in.count(31));
n = in.count(32);
for (unsigned j = 0; j < n; ++j) {
const unsigned srv = in.count(31);
shader.image_fields.emplace_back(srv, in.count(1));
}
n = in.count(2048);
for (unsigned j = 0; j < n; ++j) {
ComputeSampler sampler{};
Expand Down Expand Up @@ -936,7 +946,7 @@ std::optional<std::string> format_compute_runtime_manifest(const NativeComputeSh
return refuse("more than 8 root constants in one root parameter");
ud_count += block.count;
}
ud_count += static_cast<unsigned>(program.stride_srvs.size());
ud_count += static_cast<unsigned>(program.stride_srvs.size() + program.image_fields.size());
if (ud_count > 32)
return refuse("more than 32 user data words");

Expand Down Expand Up @@ -1069,6 +1079,12 @@ std::optional<std::string> format_compute_runtime_manifest(const NativeComputeSh
return refuse("stride words from a view that is not one raw vector SRV");
out << "udsrc " << ud_index++ << " stride " << srv << '\n';
}
for (const auto &[srv, field] : program.image_fields) {
if (srv >= srv_count || sorted[srv].kind != ComputeViewKind::texture_srv || sorted[srv].array_count != 1 ||
sorted[srv].image_dimension != 1 || field > 1)
return refuse("image field words from a view that is not one 2D texture SRV");
out << "udsrc " << ud_index++ << (field ? " imgbase " : " imgwidth ") << srv << '\n';
}
out << srv_lines.str() << uav_lines.str() << sampler_lines.str()
<< "native " << native_compute_output_revision << '\n';
return out.str();
Expand Down
3 changes: 3 additions & 0 deletions src/gfx1013_compute_native.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ struct NativeComputeShader {
std::vector<ComputeConstants> constants;
// Additional b0 words, after root constants, read from these bound SRVs.
std::vector<unsigned> stride_srvs;
// After the stride words: {SRV register, field} of the sampled 2D texture whose descriptor dimension field is
// read. Field 0 is width - 1, field 1 the most detailed mip of the view.
std::vector<std::pair<unsigned, unsigned>> image_fields;
std::string hlsl, dxil_ir;
std::vector<ComputeViewBinding> bindings;
std::vector<ComputeSampler> samplers;
Expand Down
119 changes: 119 additions & 0 deletions src/gfx1013_direct_compute.inl
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ struct DirectCompute {
unsigned mode_bits = 0xf0;
std::vector<bool> scc_live;
std::map<std::pair<unsigned,unsigned>,string> descriptor_strides;
// Dimension fields of a 2D texture descriptor in a table slot, read by scalar ops (see image_field_read). The
// runtime fills one entry word per field from the bound view: width - 1 and the most detailed mip.
struct ImageFieldWords { bool width = false, base = false; string width_word, base_word; };
std::map<std::pair<unsigned,unsigned>,ImageFieldWords> image_fields;
unsigned label_number = 0;
bool pixel_stage = false;
bool pixel_heap_enabled = false;
Expand Down Expand Up @@ -343,6 +347,8 @@ struct DirectCompute {
}
void initialize_constants(const GraphicsConstantLayout *linked = nullptr) {
unsigned words = static_cast<unsigned>(descriptor_strides.size());
for (const auto &entry : image_fields)
words += unsigned(entry.second.width) + unsigned(entry.second.base);
for (const auto &block : analysis.result.constants)
words += block.count;
if (words) {
Expand All @@ -366,6 +372,10 @@ struct DirectCompute {
}
for (auto &entry : descriptor_strides)
entry.second = ir.binary("and",word_at(pos++),"16383");
for (auto &entry : image_fields) {
if (entry.second.width) entry.second.width_word = word_at(pos++);
if (entry.second.base) entry.second.base_word = word_at(pos++);
}
}
}
// Refuses a GPU-selected heap read that the contract does not cover. raw_buffer, typed_dword and raw_and_typed_dword
Expand Down Expand Up @@ -3241,6 +3251,92 @@ struct DirectCompute {
}
return scc_live[program.at.at(definition.next)];
}
// The scalar reads of a texture descriptor's dimension fields that the runtime answers from the bound view:
// s_bfe_u32 word1 0x2001e (width_lo), s_and_b32 word2 0x3fff (width_hi), s_bfe_u32 word3 0x4000c (base level) and
// s_cmp_{eq,lg}_{i32,u32} word1, 0 (null check). Layout: AMD PAL gfx10_sq_ko_reg.h sq_img_rsrc_t (public, MIT).
// slot is (root parameter, byte offset of descriptor word 0); the caller decides whether it names an image slot.
enum class ImageFieldOp { width_lo, width_hi, base, null_eq, null_lg };
struct ImageFieldRead { std::pair<unsigned,unsigned> slot; ImageFieldOp op; };
static std::optional<ImageFieldRead> image_field_read(const Inst &i, const State &s) {
using T = Symbol::Type;
if ((i.enc != Encoding::Sop2 && i.enc != Encoding::Sopc) || (i.enc == Encoding::Sop2 && i.dst > 105))
return {};
const auto a = source(i, i.src[0], s), b = source(i, i.src[1], s);
const auto word = [](const Symbol &v, unsigned w) {
return v.type == T::descriptor && v.value != compute_resource_heap && v.offset % 32 == 4 * w;
};
const auto at = [](const Symbol &v, unsigned w) {
return std::make_pair(unsigned(v.value), unsigned(v.offset - 4 * w));
};
if (i.name == "s_bfe_u32" && b.type == T::constant) {
if (word(a, 1) && b.value == 0x2001e) return ImageFieldRead{at(a, 1), ImageFieldOp::width_lo};
if (word(a, 3) && b.value == 0x4000c) return ImageFieldRead{at(a, 3), ImageFieldOp::base};
}
if (i.name == "s_and_b32") {
if (word(a, 2) && b.type == T::constant && b.value == 0x3fff)
return ImageFieldRead{at(a, 2), ImageFieldOp::width_hi};
if (word(b, 2) && a.type == T::constant && a.value == 0x3fff)
return ImageFieldRead{at(b, 2), ImageFieldOp::width_hi};
}
if (i.enc == Encoding::Sopc && i.src[1] == 128 && word(a, 1)) {
if (i.name == "s_cmp_eq_i32" || i.name == "s_cmp_eq_u32")
return ImageFieldRead{at(a, 1), ImageFieldOp::null_eq};
if (i.name == "s_cmp_lg_i32" || i.name == "s_cmp_lg_u32")
return ImageFieldRead{at(a, 1), ImageFieldOp::null_lg};
}
return {};
}
// Keeps a field read only when its slot is the base of a sampled image op of this program and no image store or
// atomic names it. Everything else keeps the numeric-SGPR refusal.
void prepare_image_fields() {
const auto &p = analysis.program;
std::set<std::pair<unsigned,unsigned>> sampled, written;
for (size_t n = 0; n < p.code.size(); ++n) {
const auto &i = p.code[n];
if (!p.reached[n] || i.enc != Encoding::Image) continue;
const auto &symbol = p.before[n].at(((i.b >> 16) & 31) * 4);
if (symbol.type != Symbol::Type::descriptor || symbol.value == compute_resource_heap) continue;
const std::pair<unsigned,unsigned> key{symbol.value, symbol.offset};
(i.op == 8 || is_image_atomic(i) ? written : sampled).insert(key);
}
for (size_t n = 0; n < p.code.size(); ++n) {
if (!p.reached[n]) continue;
const auto field = image_field_read(p.code[n], p.before[n]);
if (!field || !sampled.count(field->slot) || written.count(field->slot)) continue;
auto &entry = image_fields[field->slot];
if (field->op == ImageFieldOp::width_lo || field->op == ImageFieldOp::width_hi) entry.width = true;
if (field->op == ImageFieldOp::base) entry.base = true;
}
if (image_fields.empty()) return;
size_t words = descriptor_strides.size();
for (const auto &entry : image_fields)
words += size_t(entry.second.width) + size_t(entry.second.base);
for (const auto &block : analysis.result.constants) words += block.count;
if (words > 32)
refuse(p.code.front().pc, "more than 32 entry words with image fields");
}
// After lowering: names the sampled-texture binding behind each slot, in the order initialize_constants laid out
// the words (width, then base, per slot).
void bind_image_fields() {
auto &r = analysis.result;
for (const auto &[slot, entry] : image_fields) {
const ComputeViewBinding *found = nullptr;
unsigned matches = 0;
for (const auto &b : r.bindings)
if (b.kind == ComputeViewKind::texture_srv && b.root_parameter == slot.first &&
b.byte_offset == slot.second) {
found = &b;
++matches;
}
if (!matches)
refuse(analysis.program.code.front().pc, "an image descriptor field has no sampled texture");
if (matches > 1)
refuse(analysis.program.code.front().pc,
"an image descriptor field names a slot with more than one texture binding");
if (entry.width) r.image_fields.emplace_back(found->shader_register, 0u);
if (entry.base) r.image_fields.emplace_back(found->shader_register, 1u);
}
}
void prepare_descriptor_strides() {
const auto &p = analysis.program;
const auto &layout = analysis.layout;
Expand Down Expand Up @@ -3369,6 +3465,27 @@ struct DirectCompute {
}
void arithmetic(const Inst &i, const State &state, const string *permuted = nullptr, bool compute = false) {
auto n = i.name;
if (compute && !image_fields.empty()) {
const auto field = image_field_read(i, state);
const auto entry = field ? image_fields.find(field->slot) : image_fields.end();
if (entry != image_fields.end()) {
string result;
switch (field->op) {
case ImageFieldOp::width_lo: result = ir.binary("and", entry->second.width_word, "3"); break;
case ImageFieldOp::width_hi:
result = ir.binary("and", ir.binary("lshr", entry->second.width_word, "2"), "16383");
break;
case ImageFieldOp::base: result = ir.binary("and", entry->second.base_word, "15"); break;
// Word 1 holds the 9-bit format at bit 20, nonzero for any texture; the runtime refuses a dispatch
// whose texture is not resolved.
case ImageFieldOp::null_eq: write(128, "0"); return;
case ImageFieldOp::null_lg: write(128, "1"); return;
}
write(i.dst, result);
write(128, ir.value("zext i1 " + cmp("ne", result, "0") + " to i32"));
return;
}
}
if (compute) {
if (const auto offset = heap_offset_add(i, state)) {
const auto base = i.src[1 - *offset];
Expand Down Expand Up @@ -5152,6 +5269,7 @@ struct DirectCompute {
if (uses_lds)
ir.globals << "@lds = internal addrspace(3) global [" << lds_words << " x i32] undef, align 4\n";
prepare_descriptor_strides();
prepare_image_fields();
const auto &p = analysis.program;
mode_bits = 0xf0;
bool mode_write = false, conditional = false;
Expand Down Expand Up @@ -5279,6 +5397,7 @@ struct DirectCompute {
else
arithmetic(i, p.before[k], nullptr, true);
}
bind_image_fields();
auto &r = analysis.result;
r.instruction_count = unsigned(p.code.size());
const unsigned rank_words = !virtual_waves && x * y * z > 64 ? x * y * z : 0;
Expand Down
2 changes: 2 additions & 0 deletions src/native_compute_pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ NativeComputePipeline create_native_compute_pipeline(ID3D12Device *device, Shade
}
pipeline.uav_count = counts[1];
pipeline.constants = std::move(program.constants);
if (!program.image_fields.empty())
throw std::runtime_error("image descriptor field words are not bound by NativeComputePipeline");
pipeline.stride_srvs = std::move(program.stride_srvs);
for (const auto &block : pipeline.constants)
pipeline.constant_words += block.count;
Expand Down
Loading
Loading