shader: categorise refusals and isolate translation failures per request #3799

Merged
Leyukaka merged 3 commits from upstream-pr/3799 into main 2026-12-02 05:25:12 +00:00
Leyukaka commented 2026-12-01 23:29:44 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-12-01T23:29:44Z.


Problem

The shader translator reports an input it cannot handle by throwing UnsupportedComputeShader, optionally with a reason code. Three paths did not follow that contract:

  • The gfx1013 decoder threw a plain std::runtime_error for a truncated, misaligned or malformed instruction, and returned an instruction with an empty mnemonic for an unknown opcode. Callers then had to treat an empty name as a special case, and one of them (name.substr(name.size()-4) in the SDWA compare path) indexed before the start of an empty name.
  • A broken lowering invariant surfaced as a std::logic_error. While a compute pack was being prepared, the per-record handler caught only std::runtime_error, so such an exception was not confined to its record.
  • Graphics input merging swallowed every refusal raised while loading a merged input, including coded ones, and did not restore the setup/body swap when another exception type passed through.

This PR is part of making the translator base more solid: every refusal has a category, and one failing request does not affect the others.

Change

Refusal categories:

  • UnsupportedComputeShader moves to its own header, src/shader_translation_error.h, so gfx1013_isa and gfx1013_compute_ir can throw it without including the native translator header.
  • Malformed encodings (truncated or misaligned instruction, reserved FLAT segment, unknown 6-bit format, invalid local DS instruction or register tuple, truncated word) are refused with code invalid_isa.
  • gfx1013::instruction() now refuses an unknown opcode with code unknown_isa_opcode and the message unknown gfx1013 opcode <n> at byte <pc>. The new gfx1013::probe_instruction() keeps the previous structural decode (form, opcode, size, empty mnemonic) for the two mesh lookahead sites that only inspect the next instruction.
  • The SDWA compare suffix test checks the name length first.
  • Graphics input merging rethrows a coded refusal and restores the setup/body swap through a scope guard. An uncoded refusal on a dead edge is still tolerated, as before.

Failure isolation:

  • The public translation entry points (translate_compute, translate_compute_container, translate_graphics_container, translate_mesh_program, translate_mesh_container, translate_mesh_pixel_program, translate_graphics_pair, translate_reflected_graphics) convert a std::logic_error into a refusal with code internal and the message prefix internal: .
  • build_compute_frontend_pack tracks each record as prepared, unsupported or internal, and catches std::exception per record. An internal failure is written to the pack as a refusal record like any other, the remaining records are published unchanged, and ComputePackCounts gains an internal count.
  • compile_dxil --prepare-compute-pack prints internal=<n> after errors=<n> and returns a failing status when internal is non-zero. Scripts that parse this line need to accept the extra field.
  • tools/startup/triage_run.py groups the new unknown-opcode message with the existing unsupported opcode reason and reports internal: messages as internal translator error.

Tests and documentation:

  • gfx1013_isa: valid_vop3_empty_name becomes refuse_unknown_vop3, and refuse_unknown_vopc_sdwa is added (VOPC opcode 0x40 with an SDWA extension word). Refusal cases now check the reason code as well as the diagnostic, and the two unknown-opcode cases also check the probe result. The case count goes from 80 to 81; the Python model, the CMake fragment and docs/experiments/gfx1013-isa.md are updated to match.
  • New test native_compute_pack_refusals (tests/cmake/native_compute_pack_refusals.cmake). It builds a second copy of the pack check from sources with two injected faults, one in the translator and one in the per-record pack code, each selected by a distinct s_nop prefix. It checks the reported refusal codes, the counts and exit status, that the record with the forced invariant failure carries internal: forced translation failure, that the unaffected records are byte-identical to those from the unmodified build, and that the pack is identical with 1 and 3 preparation threads.
  • startup_triage_run covers the two new report groups and checks that the text of an internal error is not echoed.

The third commit is a build fix: MSVC rejected a qualified local enumerator used as a default member initializer of a local struct (RecordStatus status = RecordStatus::prepared, error C2653). The member is now value-initialized with status{}, which selects the first enumerator, prepared.

Validation

Built on Windows with MSVC and run on the exact head 40aab7ea6:

ctest --preset dev --no-tests=error -R '^(gfx1013_isa_.*|gfx1013_compute_ir_all|native_compile_dxil_refusals|native_compute_pack(_parts|_refusals)?|native_direct_(container|mesh)|native_fixed_graphics_psi1|startup_triage_run)$' --output-on-failure

Result: PASS.

The first attempt, on the second commit, failed to build with MSVC (C2653, described above). The third commit fixes it, and the result above is from the head that includes it.

Not covered by this run: hardware GPU tests, and a comparison of generated DXIL or IR against the base commit for inputs that translate successfully.

  • #3792 (frontend cache results) touches eight of the same files. A trial merge of the two heads gives one textual conflict, in CMakeLists.txt: both PRs add an entry to the same native shader source list line. The other shared files merge cleanly.
  • #3794 (MSVC warnings) touches five of the same files. A trial merge gives one textual conflict, in src/gfx1013_compute_native.h, where #3794 renames a constructor parameter of UnsupportedComputeShader and this PR moves that class to src/shader_translation_error.h. The resolution is to apply the rename in the new header.
  • #3791 (translator corpus hash) shares tools/startup/compile_dxil.cpp and merges cleanly.

None of these must land first. I will rebase this branch onto whichever of them is merged before it.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5596576216 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3799). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-12-01T23:29:44Z. --- ## Problem The shader translator reports an input it cannot handle by throwing `UnsupportedComputeShader`, optionally with a reason code. Three paths did not follow that contract: - The gfx1013 decoder threw a plain `std::runtime_error` for a truncated, misaligned or malformed instruction, and returned an instruction with an empty mnemonic for an unknown opcode. Callers then had to treat an empty name as a special case, and one of them (`name.substr(name.size()-4)` in the SDWA compare path) indexed before the start of an empty name. - A broken lowering invariant surfaced as a `std::logic_error`. While a compute pack was being prepared, the per-record handler caught only `std::runtime_error`, so such an exception was not confined to its record. - Graphics input merging swallowed every refusal raised while loading a merged input, including coded ones, and did not restore the setup/body swap when another exception type passed through. This PR is part of making the translator base more solid: every refusal has a category, and one failing request does not affect the others. ## Change Refusal categories: - `UnsupportedComputeShader` moves to its own header, `src/shader_translation_error.h`, so `gfx1013_isa` and `gfx1013_compute_ir` can throw it without including the native translator header. - Malformed encodings (truncated or misaligned instruction, reserved FLAT segment, unknown 6-bit format, invalid local DS instruction or register tuple, truncated word) are refused with code `invalid_isa`. - `gfx1013::instruction()` now refuses an unknown opcode with code `unknown_isa_opcode` and the message `unknown gfx1013 opcode <n> at byte <pc>`. The new `gfx1013::probe_instruction()` keeps the previous structural decode (form, opcode, size, empty mnemonic) for the two mesh lookahead sites that only inspect the next instruction. - The SDWA compare suffix test checks the name length first. - Graphics input merging rethrows a coded refusal and restores the setup/body swap through a scope guard. An uncoded refusal on a dead edge is still tolerated, as before. Failure isolation: - The public translation entry points (`translate_compute`, `translate_compute_container`, `translate_graphics_container`, `translate_mesh_program`, `translate_mesh_container`, `translate_mesh_pixel_program`, `translate_graphics_pair`, `translate_reflected_graphics`) convert a `std::logic_error` into a refusal with code `internal` and the message prefix `internal: `. - `build_compute_frontend_pack` tracks each record as prepared, unsupported or internal, and catches `std::exception` per record. An internal failure is written to the pack as a refusal record like any other, the remaining records are published unchanged, and `ComputePackCounts` gains an `internal` count. - `compile_dxil --prepare-compute-pack` prints `internal=<n>` after `errors=<n>` and returns a failing status when `internal` is non-zero. Scripts that parse this line need to accept the extra field. - `tools/startup/triage_run.py` groups the new unknown-opcode message with the existing `unsupported opcode` reason and reports `internal: ` messages as `internal translator error`. Tests and documentation: - `gfx1013_isa`: `valid_vop3_empty_name` becomes `refuse_unknown_vop3`, and `refuse_unknown_vopc_sdwa` is added (VOPC opcode `0x40` with an SDWA extension word). Refusal cases now check the reason code as well as the diagnostic, and the two unknown-opcode cases also check the probe result. The case count goes from 80 to 81; the Python model, the CMake fragment and `docs/experiments/gfx1013-isa.md` are updated to match. - New test `native_compute_pack_refusals` (`tests/cmake/native_compute_pack_refusals.cmake`). It builds a second copy of the pack check from sources with two injected faults, one in the translator and one in the per-record pack code, each selected by a distinct `s_nop` prefix. It checks the reported refusal codes, the counts and exit status, that the record with the forced invariant failure carries `internal: forced translation failure`, that the unaffected records are byte-identical to those from the unmodified build, and that the pack is identical with 1 and 3 preparation threads. - `startup_triage_run` covers the two new report groups and checks that the text of an internal error is not echoed. The third commit is a build fix: MSVC rejected a qualified local enumerator used as a default member initializer of a local struct (`RecordStatus status = RecordStatus::prepared`, error C2653). The member is now value-initialized with `status{}`, which selects the first enumerator, `prepared`. ## Validation Built on Windows with MSVC and run on the exact head `40aab7ea6`: ``` ctest --preset dev --no-tests=error -R '^(gfx1013_isa_.*|gfx1013_compute_ir_all|native_compile_dxil_refusals|native_compute_pack(_parts|_refusals)?|native_direct_(container|mesh)|native_fixed_graphics_psi1|startup_triage_run)$' --output-on-failure ``` Result: PASS. The first attempt, on the second commit, failed to build with MSVC (C2653, described above). The third commit fixes it, and the result above is from the head that includes it. Not covered by this run: hardware GPU tests, and a comparison of generated DXIL or IR against the base commit for inputs that translate successfully. ## Related PRs - #3792 (frontend cache results) touches eight of the same files. A trial merge of the two heads gives one textual conflict, in `CMakeLists.txt`: both PRs add an entry to the same native shader source list line. The other shared files merge cleanly. - #3794 (MSVC warnings) touches five of the same files. A trial merge gives one textual conflict, in `src/gfx1013_compute_native.h`, where #3794 renames a constructor parameter of `UnsupportedComputeShader` and this PR moves that class to `src/shader_translation_error.h`. The resolution is to apply the rename in the new header. - #3791 (translator corpus hash) shares `tools/startup/compile_dxil.cpp` and merges cleanly. None of these must land first. I will rebase this branch onto whichever of them is merged before it.
Leyukaka commented 2026-12-02 04:13:44 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-12-02T04:13:44Z.


Rebased onto main (0dd65da11). ShaderTranslationError now lives in src/shader_translation_error.h; this PR keeps the reason_code parameter name from main, which /WX needs for gfx1013_isa.cpp. The CTest selection from the description passes again with MSVC on the new head.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7334691977 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3799#issuecomment-7334691977). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-12-02T04:13:44Z. --- Rebased onto `main` (0dd65da11). `ShaderTranslationError` now lives in `src/shader_translation_error.h`; this PR keeps the `reason_code` parameter name from `main`, which `/WX` needs for `gfx1013_isa.cpp`. The CTest selection from the description passes again with MSVC on the new head.
R0Y00 commented 2026-12-02 05:24:41 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-12-02T05:24:41Z.

Review: APPROVED


Confirmed the rebase keeps the reason_code constructor spelling required by the Windows build. At c8cdc10a I built compile_dxil, native_compute_pack_check and native_compute_pack_refusal_check in Windows Debug. ctest --preset dev --no-tests=error -j 2 -R '^(native_compile_dxil_refusals|native_compute_pack(_parts|_refusals)?|startup_triage_run)$' --output-on-failure passed 5/5, including refusal isolation and the injected pack failure control.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6145226254 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3799#pullrequestreview-6145226254). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-12-02T05:24:41Z. **Review:** APPROVED --- Confirmed the rebase keeps the `reason_code` constructor spelling required by the Windows build. At `c8cdc10a` I built `compile_dxil`, `native_compute_pack_check` and `native_compute_pack_refusal_check` in Windows Debug. `ctest --preset dev --no-tests=error -j 2 -R '^(native_compile_dxil_refusals|native_compute_pack(_parts|_refusals)?|startup_triage_run)$' --output-on-failure` passed 5/5, including refusal isolation and the injected pack failure control.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!3799
No description provided.