Shaders: run DXIL IR optimization pass in direct translator #1547

Closed
github-bot wants to merge 26 commits from gh-pr/1547/head into gh-pr/1547/base
Owner

@⁠Roroca-1 on GitHub · 2026-11-24 17:29 UTC · original
Roroca-1:dxil-opt-4-enable → main · +22517 −30 · 74 files · 26 commits · closed without merging

Retired after #1426 merged. Compute and graphics shader finalization already call the optimizer, and main already advanced the translator cache version to 4. Enabling this second pass would run both optimizers. The original branch is preserved.

Turns on the DXIL IR optimization and compaction pass in kernel::translate_on_this_thread and bumps kernel::TRANSLATOR_VERSION to 3 so cached unoptimized containers are invalidated.

Part of #1269.
Stacked on #1546; only the top commit belongs here.

The merged #1426 already invokes Module::optimize during shader finalization and sets TRANSLATOR_VERSION to 4. This branch conflicts with main and its proposed version 3 predates that change.

What is in it

  • crates/xcl/src/gpu/dxil/kernel.rs: bump TRANSLATOR_VERSION from 2 to 3 and call lw.s.optimize(); after checking exchange_error in translate_on_this_thread.

Tested

  • AMD Radeon RX 9070 XT (RADV GFX1201, radv 26.3.1) under Wine + vkd3d-proton (proton-cachyos-slr) + DXVK, cross-built from Linux to x86_64-pc-windows-msvc via cargo-xwin, debug, commit f349ef0bbe60:
  • TARGET_CFLAGS="/FIstring.h" cargo xwin build --tests -p xcl --target x86_64-pc-windows-msvc: 0 errors, 0 warnings
  • wine xcl.exe gpu::dxil::opt --nocapture --test-threads=1: 9 passed, 0 failed, 0 ignored (809 filtered out; 0 SKIPPED)
  • wine xcl.exe gpu::dxil::module::tests --nocapture: 1 passed, 0 failed, 0 ignored
  • wine xcl.exe gpu::dxil::bitreader_tests --test-threads=4: 3 passed, 0 failed, 0 ignored
  • wine xcl.exe gpu::dxil::robustness_tests --test-threads=4: 7 passed, 1 failed (translation_is_byte_identical_across_runs_and_threads: dxcompiler.dll not found), 0 ignored
  • wine xcl.exe gpu::dxil --test-threads=4: 32 passed, 54 failed, 2 ignored (all 54 failures solely due to dxcompiler.dll not found in Wine; SKIPPED: direct_and_hlsl_graphics_stages_link: no DXC, double_unary_legality_matches_dxil_tables)

Regression run

Pending native Windows GPU workspace tests and paired scripts/smoke.ps1 runs on the same retail title and duration at clean main and PR head. Path coverage must be recorded with the results.

Not tested

  • Windows native MSVC build and full .\scripts\test.ps1 workspace test suite
  • dxcompiler.dll DXIL validation and disassembly (unavailable locally under Wine)
  • Direct-translator gpu::recomp tests with XCL_DIRECT_DXIL=1 in debug/test mode (requires dxcompiler.dll in kernel.rs)
  • scripts/smoke.ps1 regression run on retail game titles (marked draft until run on Windows)

AI use

  • T3 Code (Antigravity harness, gemini-pro-orchestrator-neon): wired lw.s.optimize() and TRANSLATOR_VERSION bump in kernel.rs, drafted this description, and ran the cargo-xwin / Wine + RX 9070 XT tests.
  • T3 Code (Codex, gpt-6.2-astra) checked upstream overlap and updated the review status.
**@⁠Roroca-1** on GitHub · 2026-11-24 17:29 UTC · [original](https://github.com/xcl-dev/xcl/pull/1547) `Roroca-1:dxil-opt-4-enable` → `main` · +22517 −30 · 74 files · 26 commits · closed without merging Retired after #1426 merged. Compute and graphics shader finalization already call the optimizer, and main already advanced the translator cache version to 4. Enabling this second pass would run both optimizers. The original branch is preserved. Turns on the DXIL IR optimization and compaction pass in `kernel::translate_on_this_thread` and bumps `kernel::TRANSLATOR_VERSION` to `3` so cached unoptimized containers are invalidated. Part of #1269. Stacked on #1546; only the top commit belongs here. The merged #1426 already invokes `Module::optimize` during shader finalization and sets `TRANSLATOR_VERSION` to 4. This branch conflicts with `main` and its proposed version 3 predates that change. **What is in it** - `crates/xcl/src/gpu/dxil/kernel.rs`: bump `TRANSLATOR_VERSION` from `2` to `3` and call `lw.s.optimize();` after checking `exchange_error` in `translate_on_this_thread`. **Tested** - AMD Radeon RX 9070 XT (RADV GFX1201, radv 26.3.1) under Wine + vkd3d-proton (`proton-cachyos-slr`) + DXVK, cross-built from Linux to `x86_64-pc-windows-msvc` via `cargo-xwin`, debug, commit `f349ef0bbe60`: - `TARGET_CFLAGS="/FIstring.h" cargo xwin build --tests -p xcl --target x86_64-pc-windows-msvc`: 0 errors, 0 warnings - `wine xcl.exe gpu::dxil::opt --nocapture --test-threads=1`: 9 passed, 0 failed, 0 ignored (`809 filtered out`; 0 `SKIPPED`) - `wine xcl.exe gpu::dxil::module::tests --nocapture`: 1 passed, 0 failed, 0 ignored - `wine xcl.exe gpu::dxil::bitreader_tests --test-threads=4`: 3 passed, 0 failed, 0 ignored - `wine xcl.exe gpu::dxil::robustness_tests --test-threads=4`: 7 passed, 1 failed (`translation_is_byte_identical_across_runs_and_threads`: `dxcompiler.dll not found`), 0 ignored - `wine xcl.exe gpu::dxil --test-threads=4`: 32 passed, 54 failed, 2 ignored (all 54 failures solely due to `dxcompiler.dll not found` in Wine; `SKIPPED`: `direct_and_hlsl_graphics_stages_link: no DXC`, `double_unary_legality_matches_dxil_tables`) **Regression run** Pending native Windows GPU workspace tests and paired `scripts/smoke.ps1` runs on the same retail title and duration at clean `main` and PR head. Path coverage must be recorded with the results. **Not tested** - Windows native MSVC build and full `.\scripts\test.ps1` workspace test suite - `dxcompiler.dll` DXIL validation and disassembly (unavailable locally under Wine) - Direct-translator `gpu::recomp` tests with `XCL_DIRECT_DXIL=1` in debug/test mode (requires `dxcompiler.dll` in `kernel.rs`) - `scripts/smoke.ps1` regression run on retail game titles (marked draft until run on Windows) **AI use** - T3 Code (Antigravity harness, gemini-pro-orchestrator-neon): wired `lw.s.optimize()` and `TRANSLATOR_VERSION` bump in `kernel.rs`, drafted this description, and ran the `cargo-xwin` / Wine + RX 9070 XT tests. - T3 Code (Codex, gpt-6.2-astra) checked upstream overlap and updated the review status.
Emits signed DXIL containers without HLSL or DXC: LLVM 3.7 bitcode writer,
DXBC container with the retail digest, dx.op library (math, memory,
atomics, textures, wave ops), VS/PS/MS signatures and PSV0 matching DXC
byte for byte. DXC's validator is used as a test oracle only.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
LLVM's reader only accepted 7 because unknown codes fall back to seq_cst.
Adds a test-only reader for our unabbreviated bitstream to check record
layouts the validator cannot distinguish.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The validator derives these flags from the code; kernels that built i64 or
double instructions with plain Module methods (rather than the Shader
helpers that call require) failed with "Flags must match usage". Both
finish paths now scan the entry for i64 / double operands and results,
and for double division and int <-> double conversions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Seeded xorshift generator of random-but-valid kernels (integer / float /
i64 / double arithmetic with guards, phis, diamonds, switches, loops with
back-edge phis, selects, dx.op integer ops, group-shared exchange and
atomics with barriers, wave reads, raw buffer I/O). 2000 modules pass the
validator; the exactly-comparable ones (~1200) run on the GPU against a
CPU interpreter. Includes the regression test for shader-flag inference.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Empty DXC blobs may report a null buffer, which slice::from_raw_parts
forbids (UB); error-buffer casts no longer unwrap, and blobs over 4 GiB
are an error instead of a truncated length. Adds a malformed-input test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Garbage above a half / float constant's width made equal constants
intern twice (and relied on the reader truncating the record).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Port of the translator from dxil/core (olympus-based) onto main:
CFG/SSA, the EXEC/ballot lane model, SALU/VALU/memory/image/cross-lane
lowering, compute/VS/PS/mesh entry points (gpu::dxil::kernel).

Hooks re-done against main's recomp: runner (direct_dxil, direct_kernel,
direct_kernel_full, switch in run_code/run_code_with), pso (direct compute,
VS/PS and mesh pass with logged HLSL fallback; the on-disk translation
cache is bypassed while the switch is on), tests (direct_or/direct_check),
constant_samplers pub(crate).

Translator fixes for tests only main has: f64/cvt output modifiers and
clamp, integer/packed/dot clamp, wave32 src_vccz/src_execz, b32 saveexec
with m0, SDWA f16 inline constants, cube T#s as cube arrays.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The translated cache identity now includes the backend (HLSL / direct
compute / direct all) and gpu::dxil::kernel::TRANSLATOR_VERSION when
XCL_DIRECT_DXIL is set; the HLSL identity is unchanged, so existing
entries stay valid. Caching is re-enabled for the direct path.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Translator state is only looked up through hash maps (never iterated
into output), so no fix was needed; the test pins it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
act() and the cross-lane instructions' wave mask can be overridden, so an
external control-flow analysis can reuse the per-instruction lowering while
deciding EXEC itself. Default behaviour unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Module records the first misuse (forward reference outside phis,
instruction after a terminator, GEP/extractvalue/load into wrong types,
function redeclared with another type, bad call arity, bad constants,
add_incoming to a non-phi, use of a void value) and carries on with a
placeholder. New additive API: Module::fail/error/try_to_bitcode,
Shader::try_finish, Shader::try_finish_graphics; existing finish /
finish_graphics / to_bitcode keep their signatures (panic on a recorded
error). The translator uses the try_ variants, so pso.rs routing falls
back to HLSL with a log. Regression tests for each former panic.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The PR check only accepts binaries under tests/data/.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
On the no-GPU CI runner (WARP / Basic Render Driver) even a DXC-compiled
VS/PS draw through runner::draw removes the device, which then fails every
later GPU test in the process (mesh_dispatch included). Gate the draw tests
the way recomp's gpu_ready() does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
NVIDIA Blackwell drivers fail CreateComputePipelineState with E_OUTOFMEMORY
for every fuzz module containing a groupshared atomicrmw sub (82/82 failing
seeds on an RTX 5070 Ti, 0 of 1141 without it). Neither DXC nor the
translator emits that form. debug_seed now prints size and GPU-runs the seed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
AMD's driver compiler (amdxc64.dll) crashes at PSO creation on modules
without it (RX 9070 / RX 7800 XT reports).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The Windows SDK's DXC 1.6/1.7 rejects dx.valver 1.8 before checking
anything else. valver stays 1.8 (PSV0 carries PSVRuntimeInfo3, new in 1.8);
with an older validator validate() reports a SKIPPED line per test and the
GPU parts still run. Malformed containers still fail.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The translator creates the exit block before the program's blocks and
wrote the epilogue into it. The module writer numbers values in block
order and refuses a reference to a later value outside a phi, so a
vertex or pixel shader whose exports come after a branch or a guarded
memory access failed with "direct DXIL builder error: forward reference
to value 128 at instruction 90 (blocks must be in dominance order)" and
the stage went to the HLSL path. The kernel epilogue was not affected:
its stores go into a guard block that is created last.

The epilogue now has a block of its own, created after the program's
blocks; the exit block keeps the phis and branches to it.

The test translates a vertex and a pixel shader that branch on s0
before their exports and compares their draws with the HLSL path's for
both directions of the branch.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
DXC 1.9.2609 declares dx.op.waveGetLaneIndex "nounwind readonly"
(DXC 1.7.2308 declared it readnone); the emitter declared it readnone.
dxil.dll 1.7.2308 and 1.9.2609 accept either, so this only brings the
declaration in line with current DXC output. The other dx.op
declarations already match DXC 1.9.2609's table.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
The two commits before this one change the containers the direct
translator emits (an epilogue block of its own; waveGetLaneIndex
readonly), and kernel::TRANSLATOR_VERSION is part of the identity of
the translation disk cache: entries made by version 1 are not reused.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
The writer lays the entry's blocks out in reverse post-order of its CFG and
numbers values in that order, so a lowering may create blocks in any order
(requested for the per-lane mode). Unreachable blocks and their phi edges are
dropped. replace_all_uses rewrites instruction and metadata operands (e.g. to
remove trivial phis).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Part of #1269.

Expose DXIL Module IR types and helpers within gpu::dxil for the
optimization passes, and fix Module::uses_double_extensions to scan only
live block instructions rather than deleted/unattached entries in insts.

Assisted-by: T3 Code / Antigravity (gemini-pro-orchestrator-neon)
Part of #1269.

Add the core DXIL SSA optimization module (Shader::optimize /
opt::optimize_module, not yet called by kernel translation) covering
iterative CFG simplification and RPO re-indexing, dominance-checked
trivial/duplicate PHI elimination, iterative mark-and-sweep DCE, dense
table compaction, and Shader::features synchronization.

Assisted-by: T3 Code / Antigravity (gemini-pro-orchestrator-neon)
Part of #1269.

Add constant folding, integer algebraic/reassociation identities, cast
chain and bitcast(select) unwrapping, set_scc comparison simplification,
pure dx.op folding (unaryBits/bfrev/min/max), and dominator-tree scoped
CSE (excluding implicit quad derivatives and non-readnone intrinsics).

Assisted-by: T3 Code / Antigravity (gemini-pro-orchestrator-neon)
Part of #1269.

Invoke lw.s.optimize() after lowering finishes in
translate_on_this_thread and bump kernel::TRANSLATOR_VERSION from 2 to 3
so cached containers from unoptimized runs are not reused.

Assisted-by: T3 Code / Antigravity (gemini-pro-orchestrator-neon)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:57:08 +00:00
github-bot closed this pull request 2026-11-26 04:57:09 +00:00
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-24 19:19 UTC · original

Verification on RTX 3060 Ti (Windows 11): merge conflict with current main — needs a rebase before it can be tested.

**@⁠DavidM1911** on GitHub · 2026-11-24 19:19 UTC · [original](https://github.com/xcl-dev/xcl/pull/1547#issuecomment-7141922596) Verification on RTX 3060 Ti (Windows 11): **merge conflict with current `main`** — needs a rebase before it can be tested.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.