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: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ if(WIN32)
gfx1013_isa_tables.inl gfx1013_isa.cpp gfx1013_isa.h gfx1013_compute_alu.inl gfx1013_direct_compute.inl gfx1013_compute_ir.cpp gfx1013_compute_ir.h
gfx1013_compute_mtbuf.inl gfx1013_direct_graphics.inl gfx1013_direct_mesh.inl gfx1013_pixel_waterfall.inl gfx1013_rect_geometry.cpp guest_shader_container.cpp guest_shader_container.h
dxil_module.h mesh_pixel_heap.h direct_compute_cache.cpp direct_compute_cache.h
shader_compute_request.cpp shader_compute_request.h startup_stubs/inflate.h)
shader_compute_request.cpp shader_compute_request.h shader_translation_error.h startup_stubs/inflate.h)
set(native_shader_file "${CMAKE_CURRENT_SOURCE_DIR}/src/${source}")
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${native_shader_file}")
file(SHA256 "${native_shader_file}" native_shader_part)
Expand Down
20 changes: 11 additions & 9 deletions docs/experiments/gfx1013-isa.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ gfx1013 instruction at byte `pc` in a code buffer: it classifies
the form, works out the opcode, the word count and the name from
[`src/gfx1013_isa_tables.inl`](../../src/gfx1013_isa_tables.inl),
and refuses a misaligned or truncated instruction, a reserved
FLAT segment and an unknown 6-bit format. The source itself is
unchanged by this change; only tests are added.
FLAT segment, an unknown 6-bit format or an unknown opcode.

[`tests/gfx1013_isa_check.cpp`](../../tests/gfx1013_isa_check.cpp)
calls `instruction()` directly on synthetic words. No GPU, no
device and no dxc.exe is needed: the check is portable C++17 and
links only `src/gfx1013_isa.cpp`. Every mismatch prints
`FAIL: <case>: ...` to stderr and exits non-zero, and a refusal
case pins the exact `std::runtime_error` substring, including the
byte offset. [`tests/gfx1013_isa_meta_check.py`](../../tests/gfx1013_isa_meta_check.py)
`FAIL: <case>: ...` to stderr and exits non-zero. A refusal case
checks the `UnsupportedComputeShader` reason code and diagnostic,
including the byte offset. [`tests/gfx1013_isa_meta_check.py`](../../tests/gfx1013_isa_meta_check.py)
audits the contract, compiles and runs the real C++ check with
`g++ -std=c++17` on Linux, and compiles each of the five CMake
mutation copies the same way.
Expand Down Expand Up @@ -64,8 +63,6 @@ All 20 forms, each with exact opcode, size and name:
`v_add_f32` (opcode `0x100..0x13f` falls back to VOP2).
- `valid_vop3_fallback_vop1`: VOP3 opcode `0x181`, 8 bytes,
`v_mov_b32` (opcode `0x180..0x1ff` falls back to VOP1).
- `valid_vop3_empty_name`: VOP3 opcode `0x200`, 8 bytes, empty
name (out of range for every fallback, so the name stays empty).
- `valid_ds_add`: DS opcode 0, 8 bytes, `ds_add_u32`.
- `valid_flat_load`: FLAT segment 0, opcode 12, 8 bytes,
`flat_load_dword`.
Expand Down Expand Up @@ -138,6 +135,11 @@ Non-zero `pc` slicing:

Refusals, each pinning its exact error substring:

- `refuse_unknown_vop3` and `refuse_unknown_vopc_sdwa` check
the `unknown_isa_opcode` code for VOP3 opcode `0x200` and
VOPC opcode `0x40` with an SDWA extension. They also check
that a structural probe returns the form, opcode and size with
an empty mnemonic. The other refusals check the `invalid_isa` code.
- `refuse_pc_misaligned_1`, `refuse_pc_misaligned_2`,
`refuse_pc_misaligned_6`: `pc` 1, 2 and 6 throw
`truncated or misaligned gfx1013 instruction at byte 1`,
Expand Down Expand Up @@ -190,8 +192,8 @@ fails because the decode is wrong, not because the build fails.
`valid_mimg_nsa3` then decodes as 8 bytes instead of 20.
- `gfx1013_isa_vop3_fallback_mut`: drops the VOP3 fallback
guard (`if (form == Form::vop3 && name.empty()) {` becomes
`if (false) {`); `valid_vop3_fallback_vop2` then has an empty
name instead of `v_add_f32`.
`if (false) {`); `valid_vop3_fallback_vop2` then reports an
unknown opcode.
- `gfx1013_isa_vop2_madmk_mut`: drops the `madmk` name check;
`valid_vop2_madmk` then decodes as 4 bytes instead of 8.
- `gfx1013_isa_flat_segment_mut`: drops the segment 3 throw
Expand Down
19 changes: 13 additions & 6 deletions src/direct_compute_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <exception>
#include <fstream>
#include <future>
#include <charconv>
Expand Down Expand Up @@ -429,7 +430,8 @@ ComputePackCounts build_compute_frontend_pack(const fs::path &requests, const fs
// The requests are translated independently (the frontend keeps no state between them and the IR cache
// publishes through unique temporary files), on up to `jobs` threads. Their records are placed in parts in
// the sorted order afterwards, so the pack does not depend on the thread count.
struct Record { Bytes bytes; bool unsupported = false; std::string error; };
enum class RecordStatus { prepared, unsupported, internal };
struct Record { Bytes bytes; RecordStatus status{}; std::string error; }; // status{} is prepared; MSVC rejects RecordStatus::prepared here
std::vector<Record> records(markers.size());
auto prepare = [&](size_t i) {
Record &record = records[i];
Expand All @@ -443,17 +445,19 @@ ComputePackCounts build_compute_frontend_pack(const fs::path &requests, const fs
payload = encode(program);
} catch (const UnsupportedComputeShader &error) {
payload = error.what();
record.unsupported = true;
const bool internal = error.code &&
std::string_view(error.code) == UnsupportedComputeShader::internal_code;
record.status = internal ? RecordStatus::internal : RecordStatus::unsupported;
}
if (payload.empty() || part_limit < pack_header + 80 || payload.size() > part_limit - pack_header - 80)
throw std::runtime_error("invalid compute pack size");
word(record.bytes, static_cast<uint32_t>(request.key));
word(record.bytes, static_cast<uint32_t>(request.key >> 32));
record.bytes += digest;
word(record.bytes, record.unsupported);
word(record.bytes, record.status != RecordStatus::prepared);
word(record.bytes, static_cast<uint32_t>(payload.size()));
record.bytes += payload;
} catch (const std::runtime_error &e) {
} catch (const std::exception &e) {
record.error = markers[i].filename().string() + ": " + e.what();
}
};
Expand Down Expand Up @@ -486,8 +490,11 @@ ComputePackCounts build_compute_frontend_pack(const fs::path &requests, const fs
parts.back() += record.bytes;
Bytes().swap(record.bytes); // the part holds it now
++part_counts.back();
if (record.unsupported) ++counts.unsupported;
else ++counts.prepared;
switch (record.status) {
case RecordStatus::prepared: ++counts.prepared; break;
case RecordStatus::unsupported: ++counts.unsupported; break;
case RecordStatus::internal: ++counts.internal; break;
}
}
if (parts.empty()) throw std::runtime_error(counts.errors.at(0));
// Publish the last part first and part 0 last, so a reader never finds a new part 0 naming parts not yet written.
Expand Down
5 changes: 4 additions & 1 deletion src/direct_compute_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
// The result includes the emitted DXIL module and its exact desktop/guest binding layout.
namespace x2w {
enum class ComputePackResult { miss, prepared, unsupported };
struct ComputePackCounts { size_t prepared{}, unsupported{}; std::vector<std::string> errors; };
struct ComputePackCounts {
size_t prepared{}, unsupported{}, internal{};
std::vector<std::string> errors;
};
class ComputeFrontendPack {
struct Record { std::string digest, payload; bool unsupported; };
std::unordered_map<uint64_t, Record> records_;
Expand Down
6 changes: 3 additions & 3 deletions src/gfx1013_compute_ir.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "gfx1013_compute_ir.h"
#include "gfx1013_compute_native.h"
#include "shader_translation_error.h"
#include <charconv>
#include <cstdint>
#include <optional>
Expand All @@ -9,7 +9,7 @@
namespace x2w {
LocalDataAccess decode_local_data(uint32_t instruction, uint32_t operands) {
if ((instruction & 0xfc030000u) != 0xd8000000u)
throw std::runtime_error("expected a local DS instruction with GDS clear");
throw UnsupportedComputeShader("expected a local DS instruction with GDS clear", "invalid_isa");
const unsigned op = (instruction >> 18) & 255, offset = instruction & 65535;
const unsigned addr = operands & 255, data0 = (operands >> 8) & 255, data1 = (operands >> 16) & 255,
dst = operands >> 24;
Expand Down Expand Up @@ -63,7 +63,7 @@ LocalDataAccess decode_local_data(uint32_t instruction, uint32_t operands) {
if (access.read ? data0 || data1 || dst + access.count > 256 :
(!access.return_old && dst) || (!access.compare_exchange && !access.pair_words && data1) ||
data0 + first_width > 256 || data1 + access.pair_words > 256)
throw std::runtime_error("invalid local DS register tuple");
throw UnsupportedComputeShader("invalid local DS register tuple", "invalid_isa");
return access;
}

Expand Down
44 changes: 33 additions & 11 deletions src/gfx1013_compute_native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ struct SdwaProfile {
out << "native compute +0x" << std::hex << pc << ": " << reason;
throw UnsupportedComputeShader(out.str(), code);
}
[[noreturn]] void refuse_internal(const std::logic_error &error) {
throw UnsupportedComputeShader(string("internal: ") + error.what(), UnsupportedComputeShader::internal_code);
}
Word word(const string &bytes, unsigned at) {
if (at > bytes.size() || bytes.size() - at < 4)
refuse(at, "truncated word");
refuse(at, "truncated word", "invalid_isa");
Word n;
std::memcpy(&n, bytes.data() + at, 4);
return n;
Expand Down Expand Up @@ -408,7 +411,8 @@ Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=fa
const bool float_profile = floats.count(name) || (!graphics && compute_floats.count(name));
const bool fp_input = half_alu || float_profile || name == "v_cvt_u32_f32" || name == "v_cvt_i32_f32" ||
name == "v_cvt_f16_f32" || name == "v_cvt_f32_f16" || name == "v_cvt_pkrtz_f16_f32" ||
(sdwa_compare && (name.substr(name.size()-4) == "_f32" || name.substr(name.size()-4) == "_f16"));
(sdwa_compare && name.size() >= 4 &&
(name.substr(name.size()-4) == "_f32" || name.substr(name.size()-4) == "_f16"));
const bool conversion = name == "v_cvt_f32_i32" || name == "v_cvt_f32_u32" ||
name.substr(0,15) == "v_cvt_f32_ubyte";
// New profiles are compute-only: graphics keeps its existing
Expand Down Expand Up @@ -2243,11 +2247,13 @@ void x2w::apply_scalar_table_layout(ComputeViewBinding &binding, const D3D12_ROO
if (cbv != srv) binding.scalar_table = cbv ? Table::declared_cbv : Table::srv;
}

x2w::NativeComputeShader x2w::translate_compute(const ComputeRequest &request, unsigned entry, bool direct_dxil) {
x2w::NativeComputeShader x2w::translate_compute(const ComputeRequest &request, unsigned entry, bool direct_dxil) try {
return translate_program(request, entry, direct_dxil, nullptr, HeapViewContract::none);
} catch (const std::logic_error &error) {
refuse_internal(error);
}

x2w::NativeComputeShader x2w::translate_compute_container(ComputeContainer container, HeapViewContract heap_view) {
x2w::NativeComputeShader x2w::translate_compute_container(ComputeContainer container, HeapViewContract heap_view) try {
auto stages = read_guest_shader_container(container.bytes);
GuestShaderStage *compute = nullptr;
for (auto &stage : stages) {
Expand All @@ -2260,11 +2266,13 @@ x2w::NativeComputeShader x2w::translate_compute_container(ComputeContainer conta
request.code = std::move(compute->code);
request.root = std::move(container.root);
return translate_program(request, 0, true, compute, heap_view);
} catch (const std::logic_error &error) {
refuse_internal(error);
}

x2w::NativeComputeShader x2w::translate_graphics_container(const std::string &bytes,bool pixel,
const D3D12_ROOT_SIGNATURE_DESC1 *root,
bool quad_coverage_branch) {
bool quad_coverage_branch) try {
auto stages=read_guest_shader_container(bytes);
GuestShaderStage *selected=nullptr;
for(auto &stage:stages)if(stage.stage==(pixel?GpuStage::ps:GpuStage::vs)){
Expand All @@ -2274,19 +2282,25 @@ x2w::NativeComputeShader x2w::translate_graphics_container(const std::string &by
if(!selected)refuse(0,"missing native graphics stage");
return root ? lower_graphics(*selected,0,0,nullptr,*root,0,pixel,pixel,quad_coverage_branch) :
lower_graphics(*selected);
} catch (const std::logic_error &error) {
refuse_internal(error);
}

x2w::NativeComputeShader x2w::translate_mesh_program(const std::string &code, const MeshStageLayout &layout) {
x2w::NativeComputeShader x2w::translate_mesh_program(const std::string &code, const MeshStageLayout &layout) try {
return lower_mesh(code,layout);
} catch (const std::logic_error &error) {
refuse_internal(error);
}

x2w::NativeComputeShader x2w::translate_mesh_program(const std::string &code, const MeshStageLayout &layout,
const D3D12_ROOT_SIGNATURE_DESC1 &root) {
const D3D12_ROOT_SIGNATURE_DESC1 &root) try {
return lower_mesh(code,layout,nullptr,root);
} catch (const std::logic_error &error) {
refuse_internal(error);
}

x2w::NativeComputeShader x2w::translate_mesh_container(const std::string &bytes, bool validate_uint_heap_reads,
bool texture_heap_slot_map) {
bool texture_heap_slot_map) try {
const auto stages=read_guest_shader_container(bytes);
const GuestShaderStage *mesh=nullptr;
for (const auto &s : stages) if (s.stage==GpuStage::ms) {
Expand All @@ -2299,6 +2313,8 @@ x2w::NativeComputeShader x2w::translate_mesh_container(const std::string &bytes,
layout.validate_uint_heap_reads=validate_uint_heap_reads;
layout.texture_heap_slot_map=texture_heap_slot_map;
return lower_mesh(mesh->code,layout,mesh);
} catch (const std::logic_error &error) {
refuse_internal(error);
}

x2w::DirectGraphicsPair x2w::translate_graphics_pair(std::string_view a,std::string_view b) {
Expand Down Expand Up @@ -2451,21 +2467,23 @@ static GuestShaderStage read_raw_graphics(std::string_view bytes, const D3D12_RO
}

x2w::NativeComputeShader x2w::translate_mesh_pixel_program(std::string_view code, unsigned enable, unsigned address,
unsigned wave_size, const D3D12_ROOT_SIGNATURE_DESC1 *root) {
unsigned wave_size, const D3D12_ROOT_SIGNATURE_DESC1 *root) try {
if (address > 0xffff || (enable & ~address) || (wave_size != 32 && wave_size != 64))
refuse(0,"invalid fixed mesh pixel entry");
const D3D12_ROOT_SIGNATURE_DESC1 layout = root ? *root : D3D12_ROOT_SIGNATURE_DESC1{};
auto ps = read_raw_graphics(code, layout, enable, address, true);
if (ps.stage != GpuStage::ps) refuse(0,"mesh pixel program has no color export");
ps.wave32 = wave_size == 32;
return lower_graphics(ps, 0, 0, nullptr, layout, 0, root != nullptr);
} catch (const std::logic_error &error) {
refuse_internal(error);
}

x2w::DirectGraphicsPair x2w::translate_graphics_pair(std::string_view a,std::string_view b,
const D3D12_ROOT_SIGNATURE_DESC1 &layout, std::string_view pixel_entry,
std::optional<GpuStage> first_stage,
const std::array<unsigned char, 8> &target_components,
std::optional<std::array<bool,2>> stage_wave32) {
std::optional<std::array<bool,2>> stage_wave32) try {
const bool raw = a.substr(0,4)!="DXBC" && b.substr(0,4)!="DXBC";
unsigned enable_mask=0, address_mask=0;
if (!pixel_entry.empty()) {
Expand All @@ -2492,9 +2510,11 @@ x2w::DirectGraphicsPair x2w::translate_graphics_pair(std::string_view a,std::str
ps->wave32 = (*stage_wave32)[1];
}
return translate_reflected_graphics(*vs,*ps,layout);
} catch (const std::logic_error &error) {
refuse_internal(error);
}
x2w::DirectGraphicsPair x2w::translate_reflected_graphics(GuestShaderStage &vs, GuestShaderStage &ps,
const D3D12_ROOT_SIGNATURE_DESC1 &layout) {
const D3D12_ROOT_SIGNATURE_DESC1 &layout) try {
GraphicsConstantLayout constants;
for (const auto *stage:{&vs,&ps})
for(const auto &input:stage->inputs)
Expand All @@ -2508,6 +2528,8 @@ x2w::DirectGraphicsPair x2w::translate_reflected_graphics(GuestShaderStage &vs,
for (auto &binding : pixel.bindings) apply_reflected_table_use(binding, ps);
validate_raster_link(vertex, pixel);
return {std::move(vertex),std::move(pixel)};
} catch (const std::logic_error &error) {
refuse_internal(error);
}
void x2w::validate_raster_link(const NativeComputeShader &vertex, const NativeComputeShader &pixel) {
for(const auto &input:pixel.inputs){
Expand Down
8 changes: 1 addition & 7 deletions src/gfx1013_compute_native.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once
#include "mesh_pixel_heap.h"
#include "shader_compute_request.h"
#include "shader_translation_error.h"
#include <array>
#include <optional>
#include <stdexcept>
#include <string>
#include <string_view>
#include <vector>
Expand All @@ -13,12 +13,6 @@ struct D3D12_ROOT_DESCRIPTOR_TABLE1;
namespace x2w {
enum class GpuStage;
struct GuestShaderStage;
class UnsupportedComputeShader : public std::runtime_error {
public:
explicit UnsupportedComputeShader(const std::string& message, const char* reason_code = nullptr)
: std::runtime_error(message), code(reason_code) {}
const char* code;
};
enum class ComputeViewKind {
buffer_srv,
typed_buffer_srv,
Expand Down
9 changes: 7 additions & 2 deletions src/gfx1013_direct_graphics.inl
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@ struct GraphicsStage {
// An input untouched on one predecessor still has its entry value.
// Its load must dominate every edge of the phi at the join.
alu.ir.setup.swap(alu.ir.body);
struct RestoreBody {
DxilModule &ir;
~RestoreBody() { ir.setup.swap(ir.body); }
} restore{alu.ir};
try {
it->second = input(reg);
} catch (const UnsupportedComputeShader &) {
} catch (const UnsupportedComputeShader &error) {
if (error.code)
throw;
// A temporary may legitimately be undefined on a dead edge.
// A subsequent read of this merged register reports the refusal.
}
alu.ir.setup.swap(alu.ir.body);
return it->second;
}
void interpolate(Word w) {
Expand Down
Loading
Loading