Recompiler: prune unused module constants and dead-aggregate globals in direct DXIL bitcode #2640

Open
github-bot wants to merge 1 commit from github-bot/gh-2640 into main AGit
Owner

@⁠Decodere on GitHub · 2026-11-26 04:24 UTC · original
Decodere:fix-dxil-prune-unused-constants → main · +159 −35 · 1 files · 1 commits · open

What is in it

Prunes unused module constants and fixes dead-aggregate global/function retention during direct DXIL bitcode serialization in crates/xcl/src/gpu/dxil/module.rs:

  • Transitive liveness worklist (Module::live_values): Computes (gused, fused, cused) over reachable blocks (order) and metadata (MdDef::Val), seeding Op::Call callees + args, only reachable incoming edges of Op::Phi (matching write_inst), and all other Op::for_each_operand operands. Traces GlobalDef::init and ConstDef::Aggregate elements only once the owning global or constant is marked live.
  • Fixes dead-aggregate retention of unused declarations: Previously write_bitcode scanned every ConstDef::Aggregate in self.consts, so an unreferenced aggregate referencing an otherwise-unused internal global or function kept that declaration alive in the emitted module.
  • Compacts BLOCK_CONSTANTS without mutating Module: Builds cmap: Vec<Option<u64>> in original constant order (preserving elem_idx < agg_idx ordering for CST_CODE_AGGREGATE), maps Value::Const(c) through const_base + cmap[c], omits BLOCK_CONSTANTS when n_consts == 0, and emits only cused entries while keeping write_bitcode idempotent.

Part of #2508
Part of #1833

Tested

  • Failing-first verification against pre-fix live_values semantics (cargo test --release -p xcl --lib "gpu::dxil::module::tests::" -- --test-threads=1 --nocapture): 3 passed; 2 failed; 0 ignored:
    • unused_scalar_constants_produce_identical_bitcode failed (dead constant 3 retained)
    • dead_aggregate_does_not_retain_unused_global_while_live_global_init_is_kept failed (unexpected global liveness: [true, true, false], confirming dead_agg retained g_dead_inner)
  • Post-fix WARP targeted tests (XCL_ADAPTER=warp, XCL_WARP=1, cargo test --release -p xcl --lib "gpu::dxil::module::tests::" -- --test-threads=1 --nocapture): 5 passed; 0 failed; 0 ignored; 0 skipped (0.01s, DXC 1.9.2609.5 validated and disassembled the pruned shader container; zero warnings in module.rs).
  • Post-fix RTX 4060 targeted DXIL suite (cargo test --release -p xcl --lib "gpu::dxil::" -- --test-threads=1 --nocapture): 187 passed; 0 failed; 3 ignored (36.91s, gpu::dxil::module::tests:: 5/0/0, 0 skipped; 1 pre-existing SKIPPED: gpu::dxil::kernel::dispatch_chain_native_wave64_builds_whole: no wave64).

Regression run

  • Full release workspace on RTX 4060 (cargo test --release --workspace --no-fail-fast -- --test-threads=4 --nocapture): 37 test binaries, 1438 passed; 0 failed; 47 ignored, 29 pre-existing environment-gated SKIPPED lines:

    • 21 skipped: no wave64 (RTX 4060 is wave32)
    • 3 skipped: wave32 per-lane form: refused by driver (f16 arithmetic, f16 packed, f32 conversions)
    • 3 skipped: no D3D12 debug layer (d3d12SDKLayers.dll unavailable on host: debug-layer dred: false/true and uploads_start_from_each_subresources_state)
    • 1 skipped: pso_measurement_compiles_an_exact_synthetic_record (cannot disable driver cache on Windows)
    • 1 skipped: heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows)
  • Paired 60 s RTX 4060 smoke capture (scripts/smoke.ps1):

Base (origin/main b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7):

xcl-smoke v1
commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1570 (PresentX 1570)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14020 MB

PR (b7cfe9b224f31ddf6ba06c2d614bc446132b6062):

xcl-smoke v1
commit: b7cfe9b224f31ddf6ba06c2d614bc446132b6062
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1555 (PresentX 1555)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14018 MB

Not tested

  • Direct wall-clock pipeline-rebuild benchmarking over a full cold shader cache beyond the 60 s smoke capture.
  • The 29 host-gated workspace tests skipped on this RTX 4060 machine (wave64 hardware, wave32 per-lane driver support, D3D12 SDK debug layer, driver cache disable, and heap windows).

AI use

Google Antigravity (Gemini 4 Neon) wrote the patch, unit tests, and PR description; all commands, test runs, and paired 60 s GPU smoke captures were executed directly in this workspace.

**@⁠Decodere** on GitHub · 2026-11-26 04:24 UTC · [original](https://github.com/xcl-dev/xcl/pull/2640) `Decodere:fix-dxil-prune-unused-constants` → `main` · +159 −35 · 1 files · 1 commits · open **What is in it** Prunes unused module constants and fixes dead-aggregate global/function retention during direct DXIL bitcode serialization in `crates/xcl/src/gpu/dxil/module.rs`: - **Transitive liveness worklist (`Module::live_values`):** Computes `(gused, fused, cused)` over reachable blocks (`order`) and metadata (`MdDef::Val`), seeding `Op::Call` callees + args, only reachable incoming edges of `Op::Phi` (matching `write_inst`), and all other `Op::for_each_operand` operands. Traces `GlobalDef::init` and `ConstDef::Aggregate` elements only once the owning global or constant is marked live. - **Fixes dead-aggregate retention of unused declarations:** Previously `write_bitcode` scanned every `ConstDef::Aggregate` in `self.consts`, so an unreferenced aggregate referencing an otherwise-unused internal global or function kept that declaration alive in the emitted module. - **Compacts `BLOCK_CONSTANTS` without mutating `Module`:** Builds `cmap: Vec<Option<u64>>` in original constant order (preserving `elem_idx < agg_idx` ordering for `CST_CODE_AGGREGATE`), maps `Value::Const(c)` through `const_base + cmap[c]`, omits `BLOCK_CONSTANTS` when `n_consts == 0`, and emits only `cused` entries while keeping `write_bitcode` idempotent. Part of #2508 Part of #1833 **Tested** - **Failing-first verification** against pre-fix `live_values` semantics (`cargo test --release -p xcl --lib "gpu::dxil::module::tests::" -- --test-threads=1 --nocapture`): `3 passed; 2 failed; 0 ignored`: - `unused_scalar_constants_produce_identical_bitcode` failed (`dead constant 3 retained`) - `dead_aggregate_does_not_retain_unused_global_while_live_global_init_is_kept` failed (`unexpected global liveness: [true, true, false]`, confirming `dead_agg` retained `g_dead_inner`) - **Post-fix WARP targeted tests** (`XCL_ADAPTER=warp`, `XCL_WARP=1`, `cargo test --release -p xcl --lib "gpu::dxil::module::tests::" -- --test-threads=1 --nocapture`): `5 passed; 0 failed; 0 ignored; 0 skipped` (`0.01s`, DXC `1.9.2609.5` validated and disassembled the pruned shader container; zero warnings in `module.rs`). - **Post-fix RTX 4060 targeted DXIL suite** (`cargo test --release -p xcl --lib "gpu::dxil::" -- --test-threads=1 --nocapture`): `187 passed; 0 failed; 3 ignored` (`36.91s`, `gpu::dxil::module::tests::` `5/0/0`, `0` skipped; `1` pre-existing `SKIPPED`: `gpu::dxil::kernel::dispatch_chain_native_wave64_builds_whole: no wave64`). **Regression run** - **Full release workspace on RTX 4060** (`cargo test --release --workspace --no-fail-fast -- --test-threads=4 --nocapture`): `37` test binaries, `1438 passed; 0 failed; 47 ignored`, `29` pre-existing environment-gated `SKIPPED` lines: - `21` skipped: `no wave64` (RTX 4060 is wave32) - `3` skipped: wave32 `per-lane form: refused` by driver (`f16 arithmetic`, `f16 packed`, `f32 conversions`) - `3` skipped: no D3D12 debug layer (`d3d12SDKLayers.dll` unavailable on host: debug-layer `dred: false`/`true` and `uploads_start_from_each_subresources_state`) - `1` skipped: `pso_measurement_compiles_an_exact_synthetic_record` (cannot disable driver cache on Windows) - `1` skipped: `heap_window_out_of_memory_falls_back_to_the_switch_form` (no heap windows) - **Paired 60 s RTX 4060 smoke capture (`scripts/smoke.ps1`):** Base (`origin/main` `b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7`): ``` xcl-smoke v1 commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7 gpu: NVIDIA GeForce RTX 4060 title: 29640639 seconds: 60 exit: 0x0 frames shown: 1570 (PresentX 1570) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14020 MB ``` PR (`b7cfe9b224f31ddf6ba06c2d614bc446132b6062`): ``` xcl-smoke v1 commit: b7cfe9b224f31ddf6ba06c2d614bc446132b6062 gpu: NVIDIA GeForce RTX 4060 title: 29640639 seconds: 60 exit: 0x0 frames shown: 1555 (PresentX 1555) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14018 MB ``` **Not tested** - Direct wall-clock pipeline-rebuild benchmarking over a full cold shader cache beyond the 60 s smoke capture. - The `29` host-gated workspace tests skipped on this RTX 4060 machine (wave64 hardware, wave32 per-lane driver support, D3D12 SDK debug layer, driver cache disable, and heap windows). **AI use** Google Antigravity (Gemini 4 Neon) wrote the patch, unit tests, and PR description; all commands, test runs, and paired 60 s GPU smoke captures were executed directly in this workspace.
Compute transitive reachability over globals, function declarations, and
module constants from live blocks and metadata before numbering values in
Module::write_bitcode. Drop dead constants from BLOCK_CONSTANTS via a
compacted value-id map, ignore unreachable Op::Phi incoming edges when
seeding roots, and only trace ConstDef::Aggregate elements and global
initializers once their owning constant or global is reachable.

Part of #2508
Part of #1833
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:14:22 +00:00
Author
Owner

@⁠DarkPanda69 on GitHub · 2026-11-26 04:37 UTC · original

Native wave64 run (the wave64 tests skipped on the RTX 4060) on an AMD RX 9070 XT, plus an RTX 3060: the 3 new tests pass, and nothing else changes in the direct-DXIL GPU suite.

Head b7cfe9b applied on main b49be18, debug, Windows 11 Pro 26200, XCL_DIRECT_DXIL=1 cargo test -p xcl --lib -- gpu:: --test-threads=4 on main and with the PR, card chosen with XCL_ADAPTER:

main this PR
RX 9070 XT (RDNA4, 32.0.32029.2010) 708 passed, 2 failed, 6 SKIPPED 711 passed, 2 failed, 6 SKIPPED
RTX 3060 (32.0.16.1088) 710 passed, 0 failed, 24 SKIPPED 713 passed, 0 failed, 24 SKIPPED

On both cards, the only result changes against main are the new dead_aggregate_does_not_retain_unused_global_while_live_global_init_is_kept, pruned_direct_dxil_shader_with_live_table_and_dead_constants_validates and unused_scalar_constants_produce_identical_bitcode, all ok, with no new skips. So every direct-DXIL program in the suite, including the wave64 ones that skip on a 32-lane card, went through the pruned bitcode on the RX 9070 XT and still validates and runs. The 2 RX 9070 XT failures are the AMD fuzz seeds tracked in #2115, the same on main.

Not tested: cold-cache rebuild timings, the D3D12 debug-layer tests (no d3d12SDKLayers.dll here either), a title.
AI use: Claude Code (Claude Opus 5.5) ran the tests on this machine and wrote this report.

**@⁠DarkPanda69** on GitHub · 2026-11-26 04:37 UTC · [original](https://github.com/xcl-dev/xcl/pull/2640#issuecomment-7182757693) Native wave64 run (the wave64 tests skipped on the RTX 4060) on an AMD RX 9070 XT, plus an RTX 3060: the 3 new tests pass, and nothing else changes in the direct-DXIL GPU suite. Head `b7cfe9b` applied on `main` `b49be18`, debug, Windows 11 Pro 26200, `XCL_DIRECT_DXIL=1 cargo test -p xcl --lib -- gpu:: --test-threads=4` on `main` and with the PR, card chosen with `XCL_ADAPTER`: | | `main` | this PR | |---|---|---| | RX 9070 XT (RDNA4, 32.0.32029.2010) | 708 passed, 2 failed, 6 SKIPPED | 711 passed, 2 failed, 6 SKIPPED | | RTX 3060 (32.0.16.1088) | 710 passed, 0 failed, 24 SKIPPED | 713 passed, 0 failed, 24 SKIPPED | On both cards, the only result changes against `main` are the new `dead_aggregate_does_not_retain_unused_global_while_live_global_init_is_kept`, `pruned_direct_dxil_shader_with_live_table_and_dead_constants_validates` and `unused_scalar_constants_produce_identical_bitcode`, all ok, with no new skips. So every direct-DXIL program in the suite, including the wave64 ones that skip on a 32-lane card, went through the pruned bitcode on the RX 9070 XT and still validates and runs. The 2 RX 9070 XT failures are the AMD fuzz seeds tracked in #2115, the same on `main`. Not tested: cold-cache rebuild timings, the D3D12 debug-layer tests (no `d3d12SDKLayers.dll` here either), a title. AI use: Claude Code (Claude Opus 5.5) ran the tests on this machine and wrote this report.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2640/head:github-bot/gh-2640'
git switch 'github-bot/gh-2640'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2640'
git switch 'github-bot/gh-2640'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2640'
git switch 'github-bot/gh-2640'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2640'
git switch 'main'
git merge --squash 'github-bot/gh-2640'
git switch 'main'
git merge --ff-only 'github-bot/gh-2640'
git switch 'main'
git merge 'github-bot/gh-2640'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.