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
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1954,6 +1954,16 @@ if(TARGET compile_dxil AND EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${
"${CMAKE_SOURCE_DIR}/tests/native_compute_clear_check.py" "$<TARGET_FILE:compile_dxil>" "${DXC_BIN_DIR}")
add_test(NAME native_compute_raw_root COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/native_compute_raw_root_check.py" "$<TARGET_FILE:compile_dxil>" "${DXC_BIN_DIR}")
foreach(backend IN ITEMS hardware warp)
add_test(NAME native_descriptor_scc_${backend} COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/native_descriptor_scc_check.py" "$<TARGET_FILE:compile_dxil>"
"$<TARGET_FILE:native_store_replay>" "${DXC_BIN_DIR}" ${backend})
set_tests_properties(native_descriptor_scc_${backend} PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60)
add_test(NAME startup_compute_descriptor_scc_${backend} COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/native_descriptor_scc_check.py" runtime "$<TARGET_FILE:compile_dxil>"
"$<TARGET_FILE:startup_stream_consumer>" "${DXC_BIN_DIR}" ${backend})
set_tests_properties(startup_compute_descriptor_scc_${backend} PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60)
endforeach()
add_test(NAME native_direct_container
COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/native_direct_container_check.py"
"$<TARGET_FILE:compile_dxil>" "${DXC_BIN_DIR}")
Expand Down
32 changes: 32 additions & 0 deletions docs/experiments/native-descriptor-stride-scc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Buffer-stride condition code in direct compute

The direct DXIL frontend can carry a guest buffer descriptor symbolically
while calculating SCC from its bound stride. It recognizes a 14-bit stride
extraction from descriptor word 1 and the subsequent unsigned max with 1. The
first sets SCC when the extracted stride is nonzero. The max sets SCC when its
first operand wins strictly, so both operand orders have meaningful tests.

This applies to fixed, unambiguous buffer SRV slots covered by a declared
descriptor-table range. The compiler adds one stride word per descriptor,
after the regular root constants. Native dispatch and the published manifest
both use the stride from the input view the runtime has already resolved. The
symbolic descriptor fields still carry their slot identity for later buffer
addressing. A dynamic heap or CBV/UAV slot, unproven stride source, and numeric
reads of other symbolic fields retain their refusals.

The [source-built fixture](../../tests/native_descriptor_scc_check.py) checks
the layout and cache replay, then dispatches with strides of 0, 1, 4 and 16.
The runtime fixture changes guest descriptor fields between runs of the same
shader on the in-process and published paths. It compares the three status
words, the neighboring root constant, and every untouched output word.
The isolated replay also verifies 14-bit extraction and refuses to dispatch
if the stride input is missing. The fixtures use the repository's MIT license.

```powershell
cmake --build --preset release --target compile_dxil native_store_replay startup_stream_consumer
ctest --preset release --no-tests=error -R '^(native_descriptor_scc|startup_compute_descriptor_scc)_' -V
```

The direct shaders require Shader Model 6.6 and 32-lane waves. An available
adapter without wave32 reports a skip. The instruction behavior follows the
[AMD RDNA 2 ISA, revision 1.0](https://docs.amd.com/v/u/en-US/rdna2-shader-instruction-set-architecture).
13 changes: 13 additions & 0 deletions src/direct_compute_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ Bytes encode(const NativeComputeShader &program) {
for (const auto &c : program.constants)
for (unsigned n : {c.root_parameter, c.first_sgpr, c.count})
word(out, n);
word(out, static_cast<unsigned>(program.stride_srvs.size()));
for (unsigned srv : program.stride_srvs)
word(out, srv);
word(out, static_cast<uint32_t>(program.samplers.size()));
for (const auto &s : program.samplers) {
for (auto n : s.words)
Expand Down Expand Up @@ -144,6 +147,9 @@ NativeComputeShader decode(std::string_view bytes) {
throw std::runtime_error("invalid cached constants");
shader.constants.push_back(c);
}
n = in.count(32);
for (unsigned j = 0; j < n; ++j)
shader.stride_srvs.push_back(in.count(31));
n = in.count(2048);
for (unsigned j = 0; j < n; ++j) {
ComputeSampler sampler{};
Expand Down Expand Up @@ -729,6 +735,7 @@ std::optional<std::string> format_compute_runtime_manifest(const NativeComputeSh
return std::nullopt;
ud_count += block.count;
}
ud_count += static_cast<unsigned>(program.stride_srvs.size());
if (ud_count > 32)
return std::nullopt;

Expand Down Expand Up @@ -796,6 +803,12 @@ std::optional<std::string> format_compute_runtime_manifest(const NativeComputeSh
for (const auto &block : program.constants)
for (unsigned j = 0; j < block.count; ++j)
out << "udsrc " << ud_index++ << " rootconst " << block.root_parameter << ' ' << j << '\n';
for (unsigned srv : program.stride_srvs) {
if (srv >= srv_count || sorted[srv].kind != ComputeViewKind::raw_vector_srv ||
sorted[srv].array_count != 1)
return std::nullopt;
out << "udsrc " << ud_index++ << " stride " << srv << '\n';
}
out << srv_lines.str() << uav_lines.str() << sampler_lines.str();
return out.str();
}
Expand Down
2 changes: 2 additions & 0 deletions src/gfx1013_compute_native.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ struct GraphicsLinkage {
struct NativeComputeShader {
std::vector<GraphicsLinkage> inputs, outputs;
std::vector<ComputeConstants> constants;
// Additional b0 words, after root constants, read from these bound SRVs.
std::vector<unsigned> stride_srvs;
std::string hlsl, dxil_ir;
std::vector<ComputeViewBinding> bindings;
std::vector<ComputeSampler> samplers;
Expand Down
89 changes: 78 additions & 11 deletions src/gfx1013_direct_compute.inl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct DirectCompute {
bool half_mode_known = true;
unsigned mode_bits = 0xf0;
std::vector<bool> scc_live;
std::map<std::pair<unsigned,unsigned>,string> descriptor_strides;
unsigned label_number = 0;
bool pixel_stage = false;
// Straight-line graphics stages keep exact SSA definitions; a missing
Expand Down Expand Up @@ -256,7 +257,7 @@ struct DirectCompute {
initialize_constants();
}
void initialize_constants(const GraphicsConstantLayout *linked = nullptr) {
unsigned words = 0;
unsigned words = static_cast<unsigned>(descriptor_strides.size());
for (const auto &block : analysis.result.constants)
words += block.count;
if (words) {
Expand All @@ -265,18 +266,21 @@ struct DirectCompute {
auto h = ir.handle({2, 0, 13, 0, 0, (words + 3) / 4 * 16});
unsigned pos = 0;
std::map<unsigned, string> vectors;
const auto word_at = [&](unsigned at) {
auto &vec = vectors[at / 4];
if (vec.empty())
vec = ir.invoke("%dx.types.CBufRet.i32", "cbufferLoadLegacy.i32", 59,
{{"%dx.types.Handle", h}, {"i32", to_string(at / 4)}}, "nounwind readonly");
return ir.value("extractvalue %dx.types.CBufRet.i32 " + vec + ", " + to_string(at % 4));
};
for (const auto &block : analysis.result.constants) {
if (linked)
pos = linked->offset(block.root_parameter);
for (unsigned j = 0; j < block.count; ++j, ++pos) {
auto &vec = vectors[pos / 4];
if (vec.empty())
vec = ir.invoke("%dx.types.CBufRet.i32", "cbufferLoadLegacy.i32", 59,
{{"%dx.types.Handle", h}, {"i32", to_string(pos / 4)}}, "nounwind readonly");
write(block.first_sgpr + j,
ir.value("extractvalue %dx.types.CBufRet.i32 " + vec + ", " + to_string(pos % 4)));
}
for (unsigned j = 0; j < block.count; ++j, ++pos)
write(block.first_sgpr + j, word_at(pos));
}
for (auto &entry : descriptor_strides)
entry.second = ir.binary("and",word_at(pos++),"16383");
}
}
// Refuses a GPU-selected heap read that the contract does not cover. raw_buffer is the only class that is translated.
Expand Down Expand Up @@ -2070,6 +2074,56 @@ struct DirectCompute {
}
return scc_live[program.at.at(definition.next)];
}
void prepare_descriptor_strides() {
const auto &p = analysis.program;
const auto &layout = analysis.layout;
std::map<std::pair<unsigned,unsigned>,size_t> sources;
for (size_t n = 0; n < p.code.size(); ++n) {
if (!p.reached[n]) continue;
const auto &i = p.code[n];
const auto field = byte_descriptor_field(i,p.before[n],heap_window);
if (!field || (field->type != Symbol::Type::stride && field->type != Symbol::Type::byte_stride) ||
field->definition || field->value >= layout.NumParameters || !uses_scc_after(i))
continue;
const auto &parameter = layout.pParameters[field->value];
if (parameter.ParameterType != D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE ||
parameter.ShaderVisibility != D3D12_SHADER_VISIBILITY_ALL)
continue;
// The runtime provides the stride only for a fixed buffer SRV.
const auto &table = parameter.DescriptorTable;
const auto slot = field->offset / 32;
uint64_t next = 0;
unsigned srvs = 0, other = 0;
for (unsigned j = 0; j < table.NumDescriptorRanges; ++j) {
const auto &range = table.pDescriptorRanges[j];
const uint64_t start = range.OffsetInDescriptorsFromTableStart == D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND
? next : range.OffsetInDescriptorsFromTableStart;
next = start + range.NumDescriptors;
if (start <= slot && slot < next) {
if (range.RangeType == D3D12_DESCRIPTOR_RANGE_TYPE_SRV) ++srvs;
else ++other;
}
}
if (srvs == 1 && !other)
sources.try_emplace({field->value,field->offset},n);
}
for (const auto &[source, n] : sources) {
const auto [root, offset] = source;
if (read_write_raw_views.count(source)) continue;
State descriptor;
for (unsigned j=0; j<4; ++j)
descriptor[j] = {Symbol::Type::descriptor,root,offset+4*j};
analysis.binding(p.code[n],descriptor,0,4,ComputeViewKind::raw_vector_srv,4);
analysis.result.stride_srvs.push_back(analysis.result.bindings.back().shader_register);
descriptor_strides.try_emplace(source);
}
if (!descriptor_strides.empty()) {
size_t words = descriptor_strides.size();
for (const auto &block : analysis.result.constants) words += block.count;
if (words > 32)
refuse(p.code.front().pc,"descriptor stride metadata exceeds the 32-word compute entry layout");
}
}
string saturated_add(const string &a, const string &b, bool sign) {
auto sum = ir.binary("add", a, b);
if (!sign)
Expand Down Expand Up @@ -2335,8 +2389,20 @@ struct DirectCompute {
// Its SCC is known for every source descriptor.
if (field->type == Symbol::Type::byte_oob)
write(128, "1");
else if (n != "s_mul_i32" && uses_scc_after(i))
refuse(i.pc, "numeric SCC from a symbolic descriptor field");
else if (n != "s_mul_i32" && uses_scc_after(i)) {
const auto stride = descriptor_strides.find({field->value,field->offset});
if (stride == descriptor_strides.end() || field->definition ||
(field->type != Symbol::Type::stride && field->type != Symbol::Type::byte_stride))
refuse(i.pc, "numeric SCC from a symbolic descriptor field");
string condition;
if (field->type == Symbol::Type::stride)
condition = cmp("ne",stride->second,"0");
else if (source(i,i.src[0],state).type == Symbol::Type::stride)
condition = cmp("ugt",stride->second,"1");
else
condition = cmp("ult",stride->second,"1");
write(128,ir.value("zext i1 " + condition + " to i32"));
}
if (graphics_input) write(i.dst, {});
return;
}
Expand Down Expand Up @@ -3590,6 +3656,7 @@ struct DirectCompute {
[](const Inst &i) { return i.enc == Encoding::Lds; });
if (uses_lds)
ir.globals << "@lds = internal addrspace(3) global [" << lds_words << " x i32] undef, align 4\n";
prepare_descriptor_strides();
initialize(user, flags, x, y, z);
const auto &p = analysis.program;
mode_bits = 0xf0;
Expand Down
18 changes: 13 additions & 5 deletions src/native_compute_pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ NativeComputePipeline create_native_compute_pipeline(ID3D12Device *device, Shade
}
pipeline.uav_count = counts[1];
pipeline.constants = std::move(program.constants);
pipeline.stride_srvs = std::move(program.stride_srvs);
for (const auto &block : pipeline.constants)
pipeline.constant_words += block.count;
pipeline.srv_count = counts[0];
Expand All @@ -68,7 +69,8 @@ NativeComputePipeline create_native_compute_pipeline(ID3D12Device *device, Shade
D3D12_DESCRIPTOR_RANGE ranges[2]{};
for (const auto &b : pipeline.bindings)
pipeline.atomic_views += b.kind == ComputeViewKind::indexed_atomic_uav;
if (unsigned(bool(counts[0])) + unsigned(bool(counts[1])) + pipeline.constant_words +
const auto b0_words = pipeline.constant_words + static_cast<unsigned>(pipeline.stride_srvs.size());
if (unsigned(bool(counts[0])) + unsigned(bool(counts[1])) + b0_words +
2 * pipeline.atomic_views > D3D12_MAX_ROOT_COST)
throw UnsupportedComputeShader("indexed atomic view metadata exceeds root parameter budget");
std::vector<D3D12_ROOT_PARAMETER> params(3 + pipeline.atomic_views);
Expand All @@ -82,10 +84,10 @@ NativeComputePipeline create_native_compute_pipeline(ID3D12Device *device, Shade
param.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
param.DescriptorTable = {1, &ranges[i]};
}
if (pipeline.constant_words) {
if (b0_words) {
auto &param = params[param_count++];
param.ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS;
param.Constants.Num32BitValues = pipeline.constant_words;
param.Constants.Num32BitValues = b0_words;
}
for (const auto &b : pipeline.bindings) {
if (b.kind != ComputeViewKind::indexed_atomic_uav) continue;
Expand Down Expand Up @@ -113,11 +115,14 @@ NativeComputePipeline create_native_compute_pipeline(ID3D12Device *device, Shade

void NativeComputePipeline::bind(ID3D12GraphicsCommandList *list, ID3D12DescriptorHeap *heap,
D3D12_GPU_DESCRIPTOR_HANDLE first, UINT increment, const unsigned *words,
const std::vector<IndexedAtomicView> &atomic, ID3D12DescriptorHeap *sampler_heap) const {
const std::vector<IndexedAtomicView> &atomic, ID3D12DescriptorHeap *sampler_heap,
const std::vector<unsigned> &strides) const {
if (constant_words && !words)
throw std::runtime_error("compute root constants missing");
if (atomic.size() != atomic_views)
throw std::runtime_error("indexed atomic view metadata missing");
if (strides.size() != stride_srvs.size())
throw std::runtime_error("compute descriptor stride metadata missing");
const bool views = srv_count || uav_count || heap_indexed;
if (sampler_heap_indexed) {
if (!sampler_heap)
Expand All @@ -141,7 +146,10 @@ void NativeComputePipeline::bind(ID3D12GraphicsCommandList *list, ID3D12Descript
if (uav_count)
list->SetComputeRootDescriptorTable(parameter++, first);
if (constant_words)
list->SetComputeRoot32BitConstants(parameter++, constant_words, words, 0);
list->SetComputeRoot32BitConstants(parameter, constant_words, words, 0);
if (!strides.empty())
list->SetComputeRoot32BitConstants(parameter, static_cast<UINT>(strides.size()), strides.data(), constant_words);
if (constant_words || !strides.empty()) ++parameter;
for (const auto &shape : atomic) {
const unsigned pair[]{shape.stride, shape.count};
list->SetComputeRoot32BitConstants(parameter++, 2, pair, 0);
Expand Down
4 changes: 3 additions & 1 deletion src/native_compute_pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct NativeComputePipeline {
Microsoft::WRL::ComPtr<ID3D12PipelineState> state;
std::vector<ComputeViewBinding> bindings;
std::vector<ComputeConstants> constants;
std::vector<unsigned> stride_srvs;
unsigned srv_count = 0;
unsigned uav_count = 0;
unsigned constant_words = 0, atomic_views = 0;
Expand All @@ -28,7 +29,8 @@ struct NativeComputePipeline {

void bind(ID3D12GraphicsCommandList *list, ID3D12DescriptorHeap *heap, D3D12_GPU_DESCRIPTOR_HANDLE first,
UINT increment, const unsigned *words = nullptr,
const std::vector<IndexedAtomicView> &atomic = {}, ID3D12DescriptorHeap *sampler_heap = nullptr) const;
const std::vector<IndexedAtomicView> &atomic = {}, ID3D12DescriptorHeap *sampler_heap = nullptr,
const std::vector<unsigned> &strides = {}) const;
};

NativeComputePipeline create_native_compute_pipeline(ID3D12Device *device, ShaderDxilCompiler &compiler,
Expand Down
10 changes: 10 additions & 0 deletions src/startup_stubs/d3d12_compute_dispatch.inl
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@ bool translate_compute_natively(ComputeTranslation& tr)
for (const auto& block : program.constants)
for (unsigned word = 0; word < block.count; ++word)
tr.uds.push_back({static_cast<int>(block.root_parameter), static_cast<int>(word)});
for (unsigned srv : program.stride_srvs) {
const bool present = std::any_of(program.bindings.begin(), program.bindings.end(), [srv](const auto& view) {
return view.kind == x2w::ComputeViewKind::raw_vector_srv && view.shader_register == srv;
});
if (!present) throw std::runtime_error("descriptor stride source is not a bound SRV");
ComputeTranslation::Slot slot{};
slot.a = static_cast<int>(srv);
slot.source = ComputeTranslation::input_stride;
tr.uds.push_back(slot);
}
std::sort(program.bindings.begin(), program.bindings.end(),
[](const auto& a, const auto& b) { return a.shader_register < b.shader_register; });
for (const auto& view : program.bindings) {
Expand Down
Loading
Loading