Skip to content

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

Merged
R0Y00 merged 3 commits into
R0Y00:mainfrom
Leyukaka:quality/refusal-categories
Dec 2, 2026
Merged

R0Y00 merged 3 commits into
R0Y00:mainfrom
Leyukaka:quality/refusal-categories

Conversation

@Leyukaka

@Leyukaka Leyukaka commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

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

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

@Leyukaka
Leyukaka force-pushed the quality/refusal-categories branch from 40aab7e to c8cdc10 Compare December 2, 2026 04:12
@Leyukaka

Leyukaka commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator Author

Rebased onto main (0dd65da). 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 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@R0Y00
R0Y00 merged commit e958b8e into R0Y00:main Dec 2, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants