From 54e730f8209f89ef5b38fffeed7e70d8349e63a9 Mon Sep 17 00:00:00 2001 From: Leyukaka <45659323+Leyukaka@users.noreply.github.com> Date: Tue, 1 Dec 2026 21:16:47 +0100 Subject: [PATCH 1/3] shader: isolate translation failures per request --- CMakeLists.txt | 2 +- docs/experiments/gfx1013-isa.md | 19 +++---- src/direct_compute_cache.cpp | 3 +- src/gfx1013_compute_ir.cpp | 6 +-- src/gfx1013_compute_native.cpp | 44 +++++++++++---- src/gfx1013_compute_native.h | 8 +-- src/gfx1013_isa.cpp | 12 +++-- src/gfx1013_isa.h | 2 + src/shader_translation_error.h | 12 +++++ tests/cmake/gfx1013_isa.cmake | 17 +++--- .../cmake/native_compute_pack_refusals.cmake | 38 +++++++++++++ tests/gfx1013_isa_check.cpp | 18 +++++-- tests/gfx1013_isa_meta_check.py | 18 ++++--- tests/native_compute_pack_check.cpp | 21 ++++++-- tests/native_compute_pack_check.py | 53 +++++++++++++++++++ 15 files changed, 212 insertions(+), 61 deletions(-) create mode 100644 src/shader_translation_error.h create mode 100644 tests/cmake/native_compute_pack_refusals.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index acd194dd2b..ea43f25aad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/docs/experiments/gfx1013-isa.md b/docs/experiments/gfx1013-isa.md index 06fb53ba74..12c42577aa 100644 --- a/docs/experiments/gfx1013-isa.md +++ b/docs/experiments/gfx1013-isa.md @@ -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: : ...` 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: : ...` 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. @@ -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`. @@ -138,6 +135,10 @@ 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. 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`, @@ -190,8 +191,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 diff --git a/src/direct_compute_cache.cpp b/src/direct_compute_cache.cpp index 00457a71a0..1d849ce6f8 100644 --- a/src/direct_compute_cache.cpp +++ b/src/direct_compute_cache.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -453,7 +454,7 @@ ComputePackCounts build_compute_frontend_pack(const fs::path &requests, const fs word(record.bytes, record.unsupported); word(record.bytes, static_cast(payload.size())); record.bytes += payload; - } catch (const std::runtime_error &e) { + } catch (const std::exception &e) { record.error = markers[i].filename().string() + ": " + e.what(); } }; diff --git a/src/gfx1013_compute_ir.cpp b/src/gfx1013_compute_ir.cpp index 964fe9b013..ea19130842 100644 --- a/src/gfx1013_compute_ir.cpp +++ b/src/gfx1013_compute_ir.cpp @@ -1,5 +1,5 @@ #include "gfx1013_compute_ir.h" -#include "gfx1013_compute_native.h" +#include "shader_translation_error.h" #include #include #include @@ -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; @@ -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; } diff --git a/src/gfx1013_compute_native.cpp b/src/gfx1013_compute_native.cpp index 42f19b7dbd..59bc2f1b6e 100644 --- a/src/gfx1013_compute_native.cpp +++ b/src/gfx1013_compute_native.cpp @@ -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(), "internal"); +} 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; @@ -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 @@ -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) { @@ -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)){ @@ -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) { @@ -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) { @@ -2451,7 +2467,7 @@ 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{}; @@ -2459,13 +2475,15 @@ x2w::NativeComputeShader x2w::translate_mesh_pixel_program(std::string_view code 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 first_stage, const std::array &target_components, - std::optional> stage_wave32) { + std::optional> 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()) { @@ -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) @@ -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){ diff --git a/src/gfx1013_compute_native.h b/src/gfx1013_compute_native.h index 45fef23541..dd20f9d629 100644 --- a/src/gfx1013_compute_native.h +++ b/src/gfx1013_compute_native.h @@ -1,9 +1,9 @@ #pragma once #include "mesh_pixel_heap.h" #include "shader_compute_request.h" +#include "shader_translation_error.h" #include #include -#include #include #include #include @@ -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, diff --git a/src/gfx1013_isa.cpp b/src/gfx1013_isa.cpp index f909250898..154b31e6d5 100644 --- a/src/gfx1013_isa.cpp +++ b/src/gfx1013_isa.cpp @@ -1,7 +1,6 @@ #include "gfx1013_isa.h" #include #include -#include #include namespace x2w::gfx1013 { @@ -28,7 +27,8 @@ Instruction instruction(std::string_view code, unsigned pc) { auto word = [&](unsigned index) { uint64_t at = uint64_t(pc) + 4 * index; if ((pc & 3) || at > code.size() || code.size() - at < 4) - throw std::runtime_error("truncated or misaligned gfx1013 instruction at byte " + std::to_string(pc)); + throw UnsupportedComputeShader("truncated or misaligned gfx1013 instruction at byte " + + std::to_string(pc), "invalid_isa"); uint32_t n; std::memcpy(&n, code.data() + at, 4); return n; @@ -98,7 +98,7 @@ Instruction instruction(std::string_view code, unsigned pc) { case 0x37: { unsigned segment = (a >> 14) & 3; if (segment == 3) - throw std::runtime_error("reserved FLAT segment at byte " + std::to_string(pc)); + throw UnsupportedComputeShader("reserved FLAT segment at byte " + std::to_string(pc), "invalid_isa"); form = segment == 0 ? Form::flat : segment == 1 ? Form::scratch : Form::global; op = (a >> 18) & 127; break; @@ -125,7 +125,8 @@ Instruction instruction(std::string_view code, unsigned pc) { op = 0; break; default: - throw std::runtime_error("unknown gfx1013 instruction format at byte " + std::to_string(pc)); + throw UnsupportedComputeShader("unknown gfx1013 instruction format at byte " + + std::to_string(pc), "invalid_isa"); } } word(count - 1); @@ -138,6 +139,9 @@ Instruction instruction(std::string_view code, unsigned pc) { else if (op >= 0x180 && op < 0x200) name = lookup(Form::vop1, op - 0x180); } + if (name.empty()) + throw UnsupportedComputeShader("unknown gfx1013 opcode " + std::to_string(op) + + " at byte " + std::to_string(pc), "unknown_isa_opcode"); return {form, op, 4 * count, name}; } } // namespace x2w::gfx1013 diff --git a/src/gfx1013_isa.h b/src/gfx1013_isa.h index 1200dd314f..58b128b457 100644 --- a/src/gfx1013_isa.h +++ b/src/gfx1013_isa.h @@ -1,4 +1,5 @@ #pragma once +#include "shader_translation_error.h" #include #include @@ -30,5 +31,6 @@ struct Instruction { unsigned opcode, size; std::string_view name; }; +// Throws UnsupportedComputeShader for malformed or unrecognized instructions. Instruction instruction(std::string_view code, unsigned pc); } // namespace x2w::gfx1013 diff --git a/src/shader_translation_error.h b/src/shader_translation_error.h new file mode 100644 index 0000000000..486ad59507 --- /dev/null +++ b/src/shader_translation_error.h @@ -0,0 +1,12 @@ +#pragma once +#include +#include + +namespace x2w { +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; +}; +} // namespace x2w diff --git a/tests/cmake/gfx1013_isa.cmake b/tests/cmake/gfx1013_isa.cmake index cdb722a71e..647cc22824 100644 --- a/tests/cmake/gfx1013_isa.cmake +++ b/tests/cmake/gfx1013_isa.cmake @@ -2,14 +2,13 @@ # (src/gfx1013_isa.cpp, Refs #1149): the check calls instruction() # directly on synthetic words. No GPU, no device, no dxc.exe and no # Windows header is needed: the check is portable C++17 and links -# only src/gfx1013_isa.cpp. 80 cases: all 20 forms with exact opcode, -# size and name (including the three VOP3 fallbacks to VOPC, VOP2 and -# VOP1 and the empty name for an out-of-range VOP3 opcode), the +# only src/gfx1013_isa.cpp. The cases cover all 20 forms with exact opcode, +# size and name (including the three VOP3 fallbacks to VOPC, VOP2 and VOP1), the # word-count extensions (VOP1/VOP2/VOPC on 233, 234, 249, 250 and # 255; VOP2 madmk/madak/fmamk/fmaak even without src 255; SOP1/SOP2/ # SOPC on 255, including src1; SOPK s_setreg_imm32_b32; VOP3/VOP3P # literal 255 in src0, src1 and src2), three non-zero pc slicings, -# and 17 refusals pinning the exact runtime_error substring, +# and refusals pinning the reason code and diagnostic, # including the pc (misaligned pc, empty/short code, truncated # extension words, the reserved FLAT segment, and unknown 6-bit # formats 0x39, 0x3b and 0x3f). Two-word forms (ds, flat, mubuf, @@ -23,7 +22,7 @@ target_compile_features(gfx1013_isa_check PRIVATE cxx_std_17) foreach(isa_case IN ITEMS valid_vop1_mov valid_vop2_add valid_vopc_cmp valid_sop1_mov valid_sop2_add valid_sopc_cmp valid_sopp_endpgm valid_sopk_movk valid_vintrp_p1 valid_vop3p_pk_add valid_vop3_native valid_vop3_fallback_vopc - valid_vop3_fallback_vop2 valid_vop3_fallback_vop1 valid_vop3_empty_name valid_ds_add valid_flat_load valid_scratch_load + valid_vop3_fallback_vop2 valid_vop3_fallback_vop1 valid_ds_add valid_flat_load valid_scratch_load valid_global_load valid_mubuf_load valid_mtbuf_load valid_mtbuf_bit3 valid_mimg_load valid_mimg_bit7 valid_mimg_nsa1 valid_mimg_nsa2 valid_mimg_nsa3 valid_smem_load valid_exp valid_vop1_ext_233 valid_vop1_ext_234 valid_vop1_ext_249 valid_vop1_ext_250 valid_vop1_ext_255 valid_vop2_ext_233 valid_vop2_ext_234 @@ -34,7 +33,7 @@ foreach(isa_case IN ITEMS valid_pc_offset_vop1 valid_pc_offset_mimg_nsa2 valid_pc_offset_vop3_literal refuse_pc_misaligned_1 refuse_pc_misaligned_2 refuse_pc_misaligned_6 refuse_empty_code refuse_short_code refuse_truncated_vop3 refuse_truncated_vop2_src255 refuse_truncated_vop1_src255 refuse_truncated_sop1_src255 refuse_truncated_mimg_nsa1 refuse_truncated_mtbuf refuse_flat_reserved_segment refuse_flat_reserved_segment_pc8 refuse_unknown_format_39 refuse_unknown_format_3b - refuse_unknown_format_3f refuse_unknown_format_30_pc4) + refuse_unknown_format_3f refuse_unknown_format_30_pc4 refuse_unknown_vop3 refuse_unknown_vopc_sdwa) add_test(NAME gfx1013_isa_${isa_case} COMMAND gfx1013_isa_check ${isa_case}) endforeach() @@ -44,7 +43,7 @@ set_tests_properties(gfx1013_isa_valid_vop1_mov gfx1013_isa_valid_vop2_add gfx1013_isa_valid_sopc_cmp gfx1013_isa_valid_sopp_endpgm gfx1013_isa_valid_sopk_movk gfx1013_isa_valid_vintrp_p1 gfx1013_isa_valid_vop3p_pk_add gfx1013_isa_valid_vop3_native gfx1013_isa_valid_vop3_fallback_vopc gfx1013_isa_valid_vop3_fallback_vop2 - gfx1013_isa_valid_vop3_fallback_vop1 gfx1013_isa_valid_vop3_empty_name + gfx1013_isa_valid_vop3_fallback_vop1 gfx1013_isa_valid_ds_add gfx1013_isa_valid_flat_load gfx1013_isa_valid_scratch_load gfx1013_isa_valid_global_load gfx1013_isa_valid_mubuf_load gfx1013_isa_valid_mtbuf_load gfx1013_isa_valid_mtbuf_bit3 gfx1013_isa_valid_mimg_load gfx1013_isa_valid_mimg_bit7 @@ -71,7 +70,7 @@ set_tests_properties(gfx1013_isa_valid_vop1_mov gfx1013_isa_valid_vop2_add gfx1013_isa_refuse_truncated_mtbuf gfx1013_isa_refuse_flat_reserved_segment gfx1013_isa_refuse_flat_reserved_segment_pc8 gfx1013_isa_refuse_unknown_format_39 gfx1013_isa_refuse_unknown_format_3b gfx1013_isa_refuse_unknown_format_3f - gfx1013_isa_refuse_unknown_format_30_pc4 + gfx1013_isa_refuse_unknown_format_30_pc4 gfx1013_isa_refuse_unknown_vop3 gfx1013_isa_refuse_unknown_vopc_sdwa gfx1013_isa_all PROPERTIES TIMEOUT 60) # Mutation controls, mirroring era_gpu_alias_open_base_control in @@ -122,7 +121,7 @@ set_tests_properties(gfx1013_isa_mimg_nsa_mut PROPERTIES WILL_FAIL TRUE TIMEOUT # (c) The VOP3 name fallbacks are dropped: a VOP3 opcode below 0x100 # no longer falls back to VOPC, one in 0x100..0x13f no longer falls # back to VOP2, and one in 0x180..0x1ff no longer falls back to -# VOP1, so valid_vop3_fallback_vop2 fails on the empty name. +# VOP1, so valid_vop3_fallback_vop2 fails with an unknown opcode. string(REPLACE "if (form == Form::vop3 && name.empty()) {" "if (false) {" gfx1013_isa_vop3_fallback_text "${gfx1013_isa_text}") if(gfx1013_isa_vop3_fallback_text STREQUAL gfx1013_isa_text) diff --git a/tests/cmake/native_compute_pack_refusals.cmake b/tests/cmake/native_compute_pack_refusals.cmake new file mode 100644 index 0000000000..dbc567f438 --- /dev/null +++ b/tests/cmake/native_compute_pack_refusals.cmake @@ -0,0 +1,38 @@ +if(TARGET native_compute_pack_check) + # Inject a fault at each of two boundaries in frontend sources built for this test. + set(pack_fault_dir "${CMAKE_CURRENT_BINARY_DIR}/native_compute_pack_refusals") + file(MAKE_DIRECTORY "${pack_fault_dir}") + foreach(source IN ITEMS gfx1013_compute_native.cpp direct_compute_cache.cpp) + set(pack_fault_source "${CMAKE_CURRENT_SOURCE_DIR}/src/${source}") + set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${pack_fault_source}") + file(READ "${pack_fault_source}" source_text) + if(source STREQUAL "gfx1013_compute_native.cpp") + set(anchor " return translate_program(request, entry, direct_dxil, nullptr, HeapViewContract::none);") + set(fault [=[ if (request.code.compare(0, 4, "\x01\x00\x80\xbf", 4) == 0) + throw std::logic_error("forced translation failure"); +]=]) + else() + set(anchor " auto program = cached_compute_ir(cache, digest, hit, [&] { return translate_compute(request, entry, true); });") + set(fault [=[ if (request.code.compare(0, 4, "\x02\x00\x80\xbf", 4) == 0) + throw std::logic_error("forced pack record failure"); +]=]) + endif() + string(FIND "${source_text}" "${anchor}" fault_at) + if(fault_at EQUAL -1) + message(FATAL_ERROR "${source} lost the native compute pack refusal test anchor") + endif() + string(REPLACE "${anchor}" "${fault}${anchor}" source_text "${source_text}") + file(CONFIGURE OUTPUT "${pack_fault_dir}/${source}" CONTENT "${source_text}" @ONLY) + endforeach() + add_executable(native_compute_pack_refusal_check tests/native_compute_pack_check.cpp + "${pack_fault_dir}/gfx1013_compute_native.cpp" "${pack_fault_dir}/direct_compute_cache.cpp") + target_include_directories(native_compute_pack_refusal_check PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/generated") + target_link_libraries(native_compute_pack_refusal_check PRIVATE x2w_shader_compiler) + set_property(TARGET native_compute_pack_refusal_check PROPERTY + MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") + + add_test(NAME native_compute_pack_refusals COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/native_compute_pack_check.py" refusals "$" + "$" "${DXC_BIN_DIR}") + set_tests_properties(native_compute_pack_refusals PROPERTIES TIMEOUT 120) +endif() diff --git a/tests/gfx1013_isa_check.cpp b/tests/gfx1013_isa_check.cpp index 8fa9d5875c..79bb9406c8 100644 --- a/tests/gfx1013_isa_check.cpp +++ b/tests/gfx1013_isa_check.cpp @@ -9,8 +9,8 @@ // (no case runs every case; --case is accepted too) // Every mismatch prints "FAIL: : ..." to stderr and exits // non-zero, so a WILL_FAIL mutation control fails loudly instead of -// passing silently. A refusal case pins the exact std::runtime_error -// substring instruction() throws, including the byte offset. +// passing silently. A refusal case checks the reason code and diagnostic, +// including the byte offset. // // Word layout (see instruction() in src/gfx1013_isa.cpp): // - VOP1/VOP2/VOPC have bit 31 clear; VOP1 is major 63 with the @@ -34,8 +34,8 @@ #include #include #include +#include #include -#include #include #include #include @@ -177,6 +177,7 @@ struct Case { unsigned size; const char* name_str; const char* refusal; // nullptr for a valid decode. + const char* refusal_code = "invalid_isa"; }; const Case kCases[] = { @@ -195,7 +196,6 @@ const Case kCases[] = { {"valid_vop3_fallback_vopc", {vop3_word(1), 0}, 0, 0, Form::vop3, 1, 8, "v_cmp_lt_f32", nullptr}, {"valid_vop3_fallback_vop2", {vop3_word(0x103), 0}, 0, 0, Form::vop3, 0x103, 8, "v_add_f32", nullptr}, {"valid_vop3_fallback_vop1", {vop3_word(0x181), 0}, 0, 0, Form::vop3, 0x181, 8, "v_mov_b32", nullptr}, - {"valid_vop3_empty_name", {vop3_word(0x200), 0}, 0, 0, Form::vop3, 0x200, 8, "", nullptr}, {"valid_ds_add", {ds_word(0), 0}, 0, 0, Form::ds, 0, 8, "ds_add_u32", nullptr}, {"valid_flat_load", {flat_word(0, 12), 0}, 0, 0, Form::flat, 12, 8, "flat_load_dword", nullptr}, {"valid_scratch_load", {flat_word(1, 12), 0}, 0, 0, Form::scratch, 12, 8, "scratch_load_dword", nullptr}, @@ -267,6 +267,10 @@ const Case kCases[] = { {"refuse_unknown_format_3b", {unknown_word(0x3b), 0}, 0, 0, Form::vop1, 0, 0, "", "unknown gfx1013 instruction format at byte 0"}, {"refuse_unknown_format_3f", {unknown_word(0x3f), 0}, 0, 0, Form::vop1, 0, 0, "", "unknown gfx1013 instruction format at byte 0"}, {"refuse_unknown_format_30_pc4", {sopp_word(1), unknown_word(0x30), 0}, 4, 0, Form::vop1, 0, 0, "", "unknown gfx1013 instruction format at byte 4"}, + {"refuse_unknown_vop3", {vop3_word(0x200), 0}, 0, 0, Form::vop3, 0, 0, "", + "unknown gfx1013 opcode 512 at byte 0", "unknown_isa_opcode"}, + {"refuse_unknown_vopc_sdwa", {vopc_word(0x40, 249), 0}, 0, 0, Form::vopc, 0, 0, "", + "unknown gfx1013 opcode 64 at byte 0", "unknown_isa_opcode"}, }; std::string pack_code(const Case& c) { @@ -302,13 +306,17 @@ void run_case(const Case& c) { if (got.name != std::string_view(c.name_str)) fail(c.name, "name is \"" + std::string(got.name) + "\", expected \"" + std::string(c.name_str) + "\""); - } catch (const std::runtime_error& error) { + } catch (const x2w::UnsupportedComputeShader& error) { if (c.refusal == nullptr) { fail(c.name, std::string("unexpected refusal: ") + error.what()); } else if (std::string(error.what()).find(c.refusal) == std::string::npos) { fail(c.name, std::string("refusal is \"") + error.what() + "\", expected \"" + c.refusal + "\""); + } else if (!error.code || std::string_view(error.code) != c.refusal_code) { + fail(c.name, std::string("expected refusal code ") + c.refusal_code); } + } catch (const std::exception& error) { + fail(c.name, std::string("unexpected exception: ") + error.what()); } } diff --git a/tests/gfx1013_isa_meta_check.py b/tests/gfx1013_isa_meta_check.py index c9ee00ae4d..16e7b07308 100644 --- a/tests/gfx1013_isa_meta_check.py +++ b/tests/gfx1013_isa_meta_check.py @@ -6,8 +6,8 @@ SOPC/SOPP/SOPK, VINTRP, VOP3P, VOP3, DS, FLAT/SCRATCH/GLOBAL, MUBUF, MTBUF, MIMG, SMEM, EXP), works out the opcode, the word count and the name from src/gfx1013_isa_tables.inl, and refuses a misaligned -or truncated instruction, a reserved FLAT segment and an unknown -6-bit format. Unlike the D3D12 checks, this one is portable C++17 +or truncated instruction, a reserved FLAT segment, an unknown +6-bit format or an unknown opcode. This check is portable C++17 (tests/gfx1013_isa_check.cpp plus src/gfx1013_isa.cpp only), so this no-arg check (Linux CI) does more than audit the contract: it compiles and runs the real C++ check with g++ -std=c++17, and it @@ -172,7 +172,6 @@ def unknown(top): "valid_vop3_fallback_vopc": ([vop3(1), 0], 0, 0, ("vop3", 1, 8, "v_cmp_lt_f32")), "valid_vop3_fallback_vop2": ([vop3(0x103), 0], 0, 0, ("vop3", 0x103, 8, "v_add_f32")), "valid_vop3_fallback_vop1": ([vop3(0x181), 0], 0, 0, ("vop3", 0x181, 8, "v_mov_b32")), - "valid_vop3_empty_name": ([vop3(0x200), 0], 0, 0, ("vop3", 0x200, 8, "")), "valid_ds_add": ([ds(0), 0], 0, 0, ("ds", 0, 8, "ds_add_u32")), "valid_flat_load": ([flat(0, 12), 0], 0, 0, ("flat", 12, 8, "flat_load_dword")), "valid_scratch_load": ([flat(1, 12), 0], 0, 0, ("scratch", 12, 8, "scratch_load_dword")), @@ -238,6 +237,8 @@ def unknown(top): "refuse_unknown_format_3b": ([unknown(0x3B), 0], 0, 0, "unknown gfx1013 instruction format at byte 0"), "refuse_unknown_format_3f": ([unknown(0x3F), 0], 0, 0, "unknown gfx1013 instruction format at byte 0"), "refuse_unknown_format_30_pc4": ([sopp(1), unknown(0x30), 0], 4, 0, "unknown gfx1013 instruction format at byte 4"), + "refuse_unknown_vop3": ([vop3(0x200), 0], 0, 0, "unknown gfx1013 opcode 512 at byte 0"), + "refuse_unknown_vopc_sdwa": ([vopc(0x40, 249), 0], 0, 0, "unknown gfx1013 opcode 64 at byte 0"), } # name: (source anchor, replacement, control case, model flags to drop). @@ -386,6 +387,8 @@ def word(index): name = lookup("vop2", op - 0x100) elif 0x180 <= op < 0x200: name = lookup("vop1", op - 0x180) + if not name: + raise RuntimeError(f"unknown gfx1013 opcode {op} at byte {pc}") return (form, op, 4 * count, name) @@ -410,8 +413,8 @@ def main(): fragment = CMAKE.read_text(encoding="utf-8") doc = DOC.read_text(encoding="utf-8") - if len(CASES) != 80 or len(set(CASES)) != 80: - errors.append(f"expected 80 distinct cases, found {len(CASES)} ({len(set(CASES))} distinct)") + if len(CASES) != 81 or len(set(CASES)) != 81: + errors.append(f"expected 81 distinct cases, found {len(CASES)} ({len(set(CASES))} distinct)") # The source keeps the contract the C++ check relies on. for needle in ("truncated or misaligned gfx1013 instruction at byte ", @@ -481,7 +484,7 @@ def main(): # The note documents the cases, the controls, and the Windows run. for needle in ("instruction(", "valid_vop3_fallback_vop2", "valid_vop3_fallback_vop1", - "valid_vop3_empty_name", "valid_mtbuf_bit3", "valid_mimg_nsa3", + "refuse_unknown_vop3", "refuse_unknown_vopc_sdwa", "valid_mtbuf_bit3", "valid_mimg_nsa3", "valid_mimg_bit7", "valid_vop2_madmk", "valid_sopk_setreg_imm32", "valid_vop3_literal_src0", "valid_pc_offset_vop1", "refuse_flat_reserved_segment", "refuse_unknown_format_39", @@ -525,7 +528,8 @@ def main(): if "gfx1013_isa_check.cpp" in built.stderr: errors.append(f"real C++ check has warnings in the check file: {built.stderr.strip()}") ran = run_cpp(binary, []) - if ran.returncode != 0 or "PASS: gfx1013_isa: 80/80 cases passed" not in ran.stdout: + total = len(CASES) + if ran.returncode != 0 or f"PASS: gfx1013_isa: {total}/{total} cases passed" not in ran.stdout: errors.append(f"real C++ check failed: rc={ran.returncode} stdout={ran.stdout!r} stderr={ran.stderr!r}") # Real C++ mutation audit: each CMake mutant must fail diff --git a/tests/native_compute_pack_check.cpp b/tests/native_compute_pack_check.cpp index 33d5922d69..6fa7742bbb 100644 --- a/tests/native_compute_pack_check.cpp +++ b/tests/native_compute_pack_check.cpp @@ -25,14 +25,25 @@ static std::vector requests(const fs::path &input) { std::sort(markers.begin(), markers.end()); return markers; } -// --build REQUESTS CACHE LIMIT: the writer with a lowered part limit, so a few requests cross it. -static int build(const fs::path &input, const fs::path &cache, const char *limit) { - auto counts = build_compute_frontend_pack(input, cache, 0, std::stoull(limit)); +// --build REQUESTS CACHE LIMIT [JOBS]: the writer with a configurable part limit. +static int build(const fs::path &input, const fs::path &cache, const char *limit, unsigned jobs) { + auto counts = build_compute_frontend_pack(input, cache, 0, std::stoull(limit), jobs); for (const auto &error : counts.errors) std::cerr << "unprepared " << error << '\n'; std::cout << "prepared=" << counts.prepared << " unsupported=" << counts.unsupported << " errors=" << counts.errors.size() << '\n'; return counts.errors.empty() ? 0 : 1; } +static int refusal_code(const fs::path &marker) { + try { + translate_compute(read_compute_request(marker), 0, true); + } catch (const UnsupportedComputeShader &error) { + if (error.code) { + std::cout << error.code << '\n'; + return 0; + } + } + return 1; +} // --same REQUESTS PACK_A PACK_B: every request gets the same lookup result from both packs. static int same(const fs::path &input, const fs::path &a_file, const fs::path &b_file) { const auto a = ComputeFrontendPack::load(a_file, 0), b = ComputeFrontendPack::load(b_file, 0); @@ -68,7 +79,9 @@ static int old_layout(const fs::path &file, const fs::path &out) { int main(int argc, char **argv) { try { const std::string mode = argc > 1 ? argv[1] : ""; - if (mode == "--build" && argc == 5) return build(argv[2], argv[3], argv[4]); + if (mode == "--build" && (argc == 5 || argc == 6)) + return build(argv[2], argv[3], argv[4], argc == 6 ? static_cast(std::stoul(argv[5])) : 1); + if (mode == "--refusal-code" && argc == 3) return refusal_code(argv[2]); if (mode == "--same" && argc == 5) return same(argv[2], argv[3], argv[4]); if (mode == "--old" && argc == 4) return old_layout(argv[2], argv[3]); if (argc < 3 || argc > 4) return 2; diff --git a/tests/native_compute_pack_check.py b/tests/native_compute_pack_check.py index 79483f14d8..d1e37ac00b 100644 --- a/tests/native_compute_pack_check.py +++ b/tests/native_compute_pack_check.py @@ -119,6 +119,57 @@ def with_records(payload, recs): return payload[:4] + struct.pack(" Date: Tue, 1 Dec 2026 21:26:24 +0100 Subject: [PATCH 2/3] shader: carry refusal categories through diagnostics and probes --- docs/experiments/gfx1013-isa.md | 5 +++-- src/direct_compute_cache.cpp | 16 +++++++++++----- src/direct_compute_cache.h | 5 ++++- src/gfx1013_compute_native.cpp | 2 +- src/gfx1013_direct_graphics.inl | 9 +++++++-- src/gfx1013_direct_mesh.inl | 4 ++-- src/gfx1013_isa.cpp | 13 +++++++++---- src/gfx1013_isa.h | 2 ++ src/shader_translation_error.h | 2 ++ tests/gfx1013_isa_check.cpp | 13 +++++++++++-- tests/native_compute_pack_check.cpp | 4 ++-- tests/native_compute_pack_check.py | 12 +++++++++--- tests/startup_triage_run_check.py | 7 +++++++ tools/startup/compile_dxil.cpp | 5 +++-- tools/startup/triage_run.py | 4 +++- 15 files changed, 76 insertions(+), 27 deletions(-) diff --git a/docs/experiments/gfx1013-isa.md b/docs/experiments/gfx1013-isa.md index 12c42577aa..8829b68399 100644 --- a/docs/experiments/gfx1013-isa.md +++ b/docs/experiments/gfx1013-isa.md @@ -137,8 +137,9 @@ 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. The other refusals - check the `invalid_isa` code. + 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`, diff --git a/src/direct_compute_cache.cpp b/src/direct_compute_cache.cpp index 1d849ce6f8..a7dfa31479 100644 --- a/src/direct_compute_cache.cpp +++ b/src/direct_compute_cache.cpp @@ -430,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 = RecordStatus::prepared; std::string error; }; std::vector records(markers.size()); auto prepare = [&](size_t i) { Record &record = records[i]; @@ -444,14 +445,16 @@ 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(request.key)); word(record.bytes, static_cast(request.key >> 32)); record.bytes += digest; - word(record.bytes, record.unsupported); + word(record.bytes, record.status != RecordStatus::prepared); word(record.bytes, static_cast(payload.size())); record.bytes += payload; } catch (const std::exception &e) { @@ -487,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. diff --git a/src/direct_compute_cache.h b/src/direct_compute_cache.h index 106eb8e62a..30091211af 100644 --- a/src/direct_compute_cache.h +++ b/src/direct_compute_cache.h @@ -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 errors; }; +struct ComputePackCounts { + size_t prepared{}, unsupported{}, internal{}; + std::vector errors; +}; class ComputeFrontendPack { struct Record { std::string digest, payload; bool unsupported; }; std::unordered_map records_; diff --git a/src/gfx1013_compute_native.cpp b/src/gfx1013_compute_native.cpp index 59bc2f1b6e..cc4aaa51ee 100644 --- a/src/gfx1013_compute_native.cpp +++ b/src/gfx1013_compute_native.cpp @@ -55,7 +55,7 @@ struct SdwaProfile { throw UnsupportedComputeShader(out.str(), code); } [[noreturn]] void refuse_internal(const std::logic_error &error) { - throw UnsupportedComputeShader(string("internal: ") + error.what(), "internal"); + throw UnsupportedComputeShader(string("internal: ") + error.what(), UnsupportedComputeShader::internal_code); } Word word(const string &bytes, unsigned at) { if (at > bytes.size() || bytes.size() - at < 4) diff --git a/src/gfx1013_direct_graphics.inl b/src/gfx1013_direct_graphics.inl index 3630c955d1..62d8a5e6ac 100644 --- a/src/gfx1013_direct_graphics.inl +++ b/src/gfx1013_direct_graphics.inl @@ -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) { diff --git a/src/gfx1013_direct_mesh.inl b/src/gfx1013_direct_mesh.inl index 4a2559c285..7db50ae375 100644 --- a/src/gfx1013_direct_mesh.inl +++ b/src/gfx1013_direct_mesh.inl @@ -429,7 +429,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout for (auto [offset,name] : {std::pair{4,"s_cbranch_execz"}, {8,"s_lshl_b32"}, {12,"s_andn2_b64"}, {16,"s_or_b32"}, {20,"s_mov_b32"}, {28,"s_waitcnt"}, {32,"s_cbranch_vccnz"}, {40,"v_bfrev_b32"}, {60,"s_waitcnt"}, {64,"s_mov_b64"}}) { - const auto op=gfx1013::instruction(code,pc+offset); + const auto op=gfx1013::probe_instruction(code,pc+offset); if (op.size!=4 || op.name!=name) return 0; } const auto empty=inst(4); @@ -2226,7 +2226,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout state[106]=state[107]={Symbol::Type::constant,set ? ~0u : 0u}; // Thread the known-zero predecessor through a mask test before // merging with paths that computed its optional buffer values. - if (!set && i.next+8<=code.size() && !branch && gfx1013::instruction(code,i.next).name=="s_and_b64") { + if (!set && i.next+8<=code.size() && !branch && gfx1013::probe_instruction(code,i.next).name=="s_and_b64") { auto test=decode(code,i.next,true,true); auto jump=decode(code,test.next,true,true); if (test.name=="s_and_b64" && test.dst<=104 && !(test.dst&1) && diff --git a/src/gfx1013_isa.cpp b/src/gfx1013_isa.cpp index 154b31e6d5..43b725312f 100644 --- a/src/gfx1013_isa.cpp +++ b/src/gfx1013_isa.cpp @@ -23,7 +23,7 @@ std::string_view lookup(Form form, unsigned index) { return index < t.size && t.data[index] ? t.data[index] : ""; } } // namespace -Instruction instruction(std::string_view code, unsigned pc) { +Instruction probe_instruction(std::string_view code, unsigned pc) { auto word = [&](unsigned index) { uint64_t at = uint64_t(pc) + 4 * index; if ((pc & 3) || at > code.size() || code.size() - at < 4) @@ -139,9 +139,14 @@ Instruction instruction(std::string_view code, unsigned pc) { else if (op >= 0x180 && op < 0x200) name = lookup(Form::vop1, op - 0x180); } - if (name.empty()) - throw UnsupportedComputeShader("unknown gfx1013 opcode " + std::to_string(op) + - " at byte " + std::to_string(pc), "unknown_isa_opcode"); return {form, op, 4 * count, name}; } + +Instruction instruction(std::string_view code, unsigned pc) { + const auto decoded = probe_instruction(code, pc); + if (decoded.name.empty()) + throw UnsupportedComputeShader("unknown gfx1013 opcode " + std::to_string(decoded.opcode) + + " at byte " + std::to_string(pc), "unknown_isa_opcode"); + return decoded; +} } // namespace x2w::gfx1013 diff --git a/src/gfx1013_isa.h b/src/gfx1013_isa.h index 58b128b457..966dc2a07a 100644 --- a/src/gfx1013_isa.h +++ b/src/gfx1013_isa.h @@ -31,6 +31,8 @@ struct Instruction { unsigned opcode, size; std::string_view name; }; +// A probe returns an empty mnemonic for an unknown opcode. Malformed encodings still throw. +Instruction probe_instruction(std::string_view code, unsigned pc); // Throws UnsupportedComputeShader for malformed or unrecognized instructions. Instruction instruction(std::string_view code, unsigned pc); } // namespace x2w::gfx1013 diff --git a/src/shader_translation_error.h b/src/shader_translation_error.h index 486ad59507..3fdbab3767 100644 --- a/src/shader_translation_error.h +++ b/src/shader_translation_error.h @@ -5,6 +5,8 @@ namespace x2w { class UnsupportedComputeShader : public std::runtime_error { public: + static constexpr char internal_code[] = "internal"; + explicit UnsupportedComputeShader(const std::string &message, const char *reason_code = nullptr) : std::runtime_error(message), code(reason_code) {} const char *code; diff --git a/tests/gfx1013_isa_check.cpp b/tests/gfx1013_isa_check.cpp index 79bb9406c8..aecb28aa71 100644 --- a/tests/gfx1013_isa_check.cpp +++ b/tests/gfx1013_isa_check.cpp @@ -267,9 +267,9 @@ const Case kCases[] = { {"refuse_unknown_format_3b", {unknown_word(0x3b), 0}, 0, 0, Form::vop1, 0, 0, "", "unknown gfx1013 instruction format at byte 0"}, {"refuse_unknown_format_3f", {unknown_word(0x3f), 0}, 0, 0, Form::vop1, 0, 0, "", "unknown gfx1013 instruction format at byte 0"}, {"refuse_unknown_format_30_pc4", {sopp_word(1), unknown_word(0x30), 0}, 4, 0, Form::vop1, 0, 0, "", "unknown gfx1013 instruction format at byte 4"}, - {"refuse_unknown_vop3", {vop3_word(0x200), 0}, 0, 0, Form::vop3, 0, 0, "", + {"refuse_unknown_vop3", {vop3_word(0x200), 0}, 0, 0, Form::vop3, 0x200, 8, "", "unknown gfx1013 opcode 512 at byte 0", "unknown_isa_opcode"}, - {"refuse_unknown_vopc_sdwa", {vopc_word(0x40, 249), 0}, 0, 0, Form::vopc, 0, 0, "", + {"refuse_unknown_vopc_sdwa", {vopc_word(0x40, 249), 0}, 0, 0, Form::vopc, 0x40, 8, "", "unknown gfx1013 opcode 64 at byte 0", "unknown_isa_opcode"}, }; @@ -318,6 +318,15 @@ void run_case(const Case& c) { } catch (const std::exception& error) { fail(c.name, std::string("unexpected exception: ") + error.what()); } + if (std::string_view(c.refusal_code) == "unknown_isa_opcode") { + try { + const auto probe = x2w::gfx1013::probe_instruction(code, c.pc); + if (!probe.name.empty() || probe.form != c.form || probe.opcode != c.opcode || probe.size != c.size) + fail(c.name, "expected a structurally decoded instruction with an empty mnemonic"); + } catch (const std::exception& error) { + fail(c.name, std::string("unexpected probe exception: ") + error.what()); + } + } } } // namespace diff --git a/tests/native_compute_pack_check.cpp b/tests/native_compute_pack_check.cpp index 6fa7742bbb..5de50b4dfa 100644 --- a/tests/native_compute_pack_check.cpp +++ b/tests/native_compute_pack_check.cpp @@ -30,8 +30,8 @@ static int build(const fs::path &input, const fs::path &cache, const char *limit auto counts = build_compute_frontend_pack(input, cache, 0, std::stoull(limit), jobs); for (const auto &error : counts.errors) std::cerr << "unprepared " << error << '\n'; std::cout << "prepared=" << counts.prepared << " unsupported=" << counts.unsupported - << " errors=" << counts.errors.size() << '\n'; - return counts.errors.empty() ? 0 : 1; + << " errors=" << counts.errors.size() << " internal=" << counts.internal << '\n'; + return counts.errors.empty() && counts.internal == 0 ? 0 : 1; } static int refusal_code(const fs::path &marker) { try { diff --git a/tests/native_compute_pack_check.py b/tests/native_compute_pack_check.py index d1e37ac00b..d86e49f6b4 100644 --- a/tests/native_compute_pack_check.py +++ b/tests/native_compute_pack_check.py @@ -41,7 +41,7 @@ def run(cli, checker, sdk): put_compute_request(inputs, bytes.fromhex("0000f8d8 00000000 000081bf"), root, state) build = [cli, "--prepare-compute-pack", inputs, base / "cache", "0"] result = subprocess.run(build, capture_output=True, text=True) - assert result.returncode == 0 and "prepared=1 unsupported=1 errors=0" in result.stdout, result + assert result.returncode == 0 and "prepared=1 unsupported=1 errors=0 internal=0" in result.stdout, result pack = base / "cache" / "compute-0.pack" subprocess.run([checker, inputs, pack], check=True) original = pack.read_bytes() @@ -151,7 +151,7 @@ def build(exe, name, jobs): assert result.returncode == 0 and result.stdout.strip() == reason, result result, clean, _ = build(checker, "clean", 1) - assert result.returncode == 0 and "prepared=4 unsupported=1 errors=0" in result.stdout, result + assert result.returncode == 0 and "prepared=4 unsupported=1 errors=0 internal=0" in result.stdout, result assert set(clean) == {*prepared, internal, unprepared, unknown}, clean.keys() assert struct.unpack_from(" None: "image_load_mip tfe", "native compute +0x1a: unsupported instruction v_acme_widget_7f3a 0xdeadbeefu", "native compute +0x1c: AcmeTitle secret reason", + "native compute +0x8: unsupported opcode 0xdeadbeefu", + "unknown gfx1013 opcode 64 at byte 8", + "internal: AcmeModule 0000018012345678 [secret](https://acme.example/leak)", ] PRIVATE_NEEDLES = [ "Acme", "acme", "ACME", "Private", "secret", "leak", "widget", "7f3a", "exploded", @@ -170,6 +173,8 @@ def check_private_text_never_echoed(failures: list[str]) -> None: "`undecodable word`", "`image_load_mip tfe`", "`v_* with modifier`", + "| 2 | `unsupported opcode` |", + "| 1 | `internal translator error` |", "`other (text omitted)`", ]: if want not in report: @@ -652,6 +657,8 @@ def check_every_shader_reason_entry_is_reachable(failures: list[str]) -> None: samples = ( ("native compute +0x14: unsupported instruction v_add3_u32 0xd76d0002u", "v_* instruction"), ("native compute +0x8: unsupported opcode 0xdeadbeefu", "unsupported opcode"), + ("unknown gfx1013 opcode 64 at byte 8", "unsupported opcode"), + ("internal: inconsistent DXIL declaration", "internal translator error"), ("native compute +0x10: unsupported pixel entry VGPR 2", "unsupported pixel entry VGPR"), ("unsupported startup runtime manifest bindings: SRV kind texture_srv 2d array", "runtime manifest: SRV kind texture_srv 2d array"), diff --git a/tools/startup/compile_dxil.cpp b/tools/startup/compile_dxil.cpp index e8819a2a04..15f3f02aeb 100644 --- a/tools/startup/compile_dxil.cpp +++ b/tools/startup/compile_dxil.cpp @@ -1631,8 +1631,9 @@ int wmain(int argc, wchar_t** argv) { x2w::compute_pack_part_limit, jobs); for (const auto &error : counts.errors) std::cerr << "unprepared " << error << "\n"; std::cout << "prepared=" << counts.prepared << " unsupported=" << counts.unsupported - << " errors=" << counts.errors.size() << " elapsed_ms=" << std::chrono::duration(Clock::now()-start).count() << "\n"; - return counts.errors.empty() ? 0 : 1; + << " errors=" << counts.errors.size() << " internal=" << counts.internal + << " elapsed_ms=" << std::chrono::duration(Clock::now()-start).count() << "\n"; + return counts.errors.empty() && counts.internal == 0 ? 0 : 1; } catch (const std::exception &error) { std::cerr << "pack not published: " << error.what() << "\n"; return 1; diff --git a/tools/startup/triage_run.py b/tools/startup/triage_run.py index 228c070573..47c541ab19 100644 --- a/tools/startup/triage_run.py +++ b/tools/startup/triage_run.py @@ -347,7 +347,9 @@ SHADER_REASONS: list[tuple[re.Pattern[str], str]] = [ (re.compile(r"native compute \+0x[0-9a-fA-F]+: unsupported instruction " + MNEMONIC_FAMILY + r"\b"), r"\1_* instruction"), - (re.compile(r"native compute \+0x[0-9a-fA-F]+: unsupported opcode\b"), "unsupported opcode"), + (re.compile(r"(?:native compute \+0x[0-9a-fA-F]+: unsupported opcode\b|" + r"unknown gfx1013 opcode [0-9]+ at byte [0-9]+\Z)"), "unsupported opcode"), + (re.compile(r"internal: "), "internal translator error"), (re.compile(r"native compute \+0x[0-9a-fA-F]+: unsupported pixel entry VGPR\b"), "unsupported pixel entry VGPR"), (re.compile(r"native compute \+0x[0-9a-fA-F]+: unproven resource descriptor\b"), "unproven resource descriptor"), (re.compile(r"native compute \+0x[0-9a-fA-F]+: unbound numeric SGPR\b"), "unbound numeric SGPR"), From c8cdc10abf33378404ebc8c2aa829c133af42535 Mon Sep 17 00:00:00 2001 From: Leyukaka <45659323+Leyukaka@users.noreply.github.com> Date: Tue, 1 Dec 2026 23:31:46 +0100 Subject: [PATCH 3/3] shader: build the compute pack record status with MSVC --- src/direct_compute_cache.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/direct_compute_cache.cpp b/src/direct_compute_cache.cpp index a7dfa31479..8cceb9747f 100644 --- a/src/direct_compute_cache.cpp +++ b/src/direct_compute_cache.cpp @@ -431,7 +431,7 @@ ComputePackCounts build_compute_frontend_pack(const fs::path &requests, const fs // 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. enum class RecordStatus { prepared, unsupported, internal }; - struct Record { Bytes bytes; RecordStatus status = RecordStatus::prepared; std::string error; }; + struct Record { Bytes bytes; RecordStatus status{}; std::string error; }; // status{} is prepared; MSVC rejects RecordStatus::prepared here std::vector records(markers.size()); auto prepare = [&](size_t i) { Record &record = records[i];