Skip to content

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

Closed
Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-dxil-prune-unused-constants
Closed

Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-dxil-prune-unused-constants

Conversation

@Decodere

Copy link
Copy Markdown
Contributor

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.

…in direct DXIL bitcode

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 xcl-dev#2508
Part of xcl-dev#1833
@DarkPanda69

Copy link
Copy Markdown

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.

bmjask pushed a commit to bmjask/xcl that referenced this pull request Nov 26, 2026
Compute transitive liveness for globals, declarations, and module constants from reachable instructions and metadata. Trace only live global initializers and aggregate elements, then compact the constant block through a stable old-to-new value map.

This removes dead constants and prevents an unreachable aggregate from retaining otherwise-unused internal symbols while preserving idempotent serialization and reachable Phi semantics.

GNU Windows-target cargo check passed here. Upstream validation includes native AMD wave64 and NVIDIA direct-DXIL GPU suites; Windows/MSVC and GPU execution were not repeated on this host.

Adapted from PR xcl-dev#2640.
Assisted-by: Google Antigravity (Gemini 4 Neon)
Assisted-by: Codex (GPT-5.6-sol)
bmjask pushed a commit to bmjask/xcl that referenced this pull request Nov 26, 2026
Compute transitive liveness for globals, declarations, and module constants from reachable instructions and metadata. Trace only live global initializers and aggregate elements, then compact the constant block through a stable old-to-new value map.

This removes dead constants and prevents an unreachable aggregate from retaining otherwise-unused internal symbols while preserving idempotent serialization and reachable Phi semantics.

GNU Windows-target cargo check passed here. Upstream validation includes native AMD wave64 and NVIDIA direct-DXIL GPU suites; Windows/MSVC and GPU execution were not repeated on this host.

Adapted from PR xcl-dev#2640.
Assisted-by: Google Antigravity (Gemini 4 Neon)
Assisted-by: Codex (GPT-5.6-sol)
bmjask pushed a commit to bmjask/xcl that referenced this pull request Nov 27, 2026
Compute transitive liveness for globals, declarations, and module constants from reachable instructions and metadata. Trace only live global initializers and aggregate elements, then compact the constant block through a stable old-to-new value map.

This removes dead constants and prevents an unreachable aggregate from retaining otherwise-unused internal symbols while preserving idempotent serialization and reachable Phi semantics.

GNU Windows-target cargo check passed here. Upstream validation includes native AMD wave64 and NVIDIA direct-DXIL GPU suites; Windows/MSVC and GPU execution were not repeated on this host.

Adapted from PR xcl-dev#2640.
Assisted-by: Google Antigravity (Gemini 4 Neon)
Assisted-by: Codex (GPT-5.6-sol)
bmjask pushed a commit to bmjask/xcl that referenced this pull request Nov 27, 2026
Compute transitive liveness for globals, declarations, and module constants from reachable instructions and metadata. Trace only live global initializers and aggregate elements, then compact the constant block through a stable old-to-new value map.

This removes dead constants and prevents an unreachable aggregate from retaining otherwise-unused internal symbols while preserving idempotent serialization and reachable Phi semantics.

GNU Windows-target cargo check passed here. Upstream validation includes native AMD wave64 and NVIDIA direct-DXIL GPU suites; Windows/MSVC and GPU execution were not repeated on this host.

Adapted from PR xcl-dev#2640.
Assisted-by: Google Antigravity (Gemini 4 Neon)
Assisted-by: Codex (GPT-5.6-sol)

Assisted-by: Codex (GPT-5.6-sol)
Prime1997 pushed a commit that referenced this pull request Nov 28, 2026
Compute transitive liveness for globals, declarations, and module constants from reachable instructions and metadata. Trace only live global initializers and aggregate elements, then compact the constant block through a stable old-to-new value map.

This removes dead constants and prevents an unreachable aggregate from retaining otherwise-unused internal symbols while preserving idempotent serialization and reachable Phi semantics.

GNU Windows-target cargo check passed here. Upstream validation includes native AMD wave64 and NVIDIA direct-DXIL GPU suites; Windows/MSVC and GPU execution were not repeated on this host.

Adapted from PR #2640.
Assisted-by: Google Antigravity (Gemini 4 Neon)
Assisted-by: Codex (GPT-5.6-sol)

Assisted-by: Codex (GPT-5.6-sol)
@Kalmarv

Kalmarv commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

merge-check
base: fa20459
head: b7cfe9b
merge: clean, but empty: the trial merge ea280e32 has no diff against main. This PR's commit has the same patch-id (d250b5ff) as main's 09a65d63 ("Recompiler: prune unreachable direct-DXIL module values", bmjask, 2026-11-26, "Adapted from PR #2640"), which came in with bmjask's stack.
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 (XCL_DXC_DIR = DXC 1.9.2609) -> 2608 passed, 0 failed, 49 ignored; SKIPPED: 26 (21 no wave64 on this 32-lane GPU, 3 per-lane f16/f32 forms refused (#1426 semantics), 2 no heap windows); failing: none. The 3 tests (unused_scalar_constants_produce_identical_bitcode, dead_aggregate_does_not_retain_unused_global_while_live_global_init_is_kept, pruned_direct_dxil_shader_with_live_table_and_dead_constants_validates) ran and passed. They are main's own copies: cargo test -p xcl --lib -- --list gives the same 2,498 names on main and on the merge.
review: whole diff read before I found it on main; no findings. Every constant id goes through cmap. Phi incomings from unreachable blocks are skipped in both the liveness walk and the writer (same pos filter). for_each_operand covers every Op, including Switch cases, Alloca sizes and GEP indices. Live globals pull in their initializers, and dead aggregates no longer keep globals alive. All of this already holds on main.
performance: nothing to measure; merging changes no file.
verdict: not yet: already on main as 09a65d63 (same patch); merging adds nothing. This should be closed as merged (#1607: a PR that duplicates one already merged is closed).
hardware / tool and model: NVIDIA GeForce RTX 5070 Ti 16 GB (driver 610.74, wave32 only), AMD Ryzen 9 9950X, Windows 11 build 26200; rustc 1.99.0, debug test profile, DXC 1.9.2609 via XCL_DXC_DIR. Claude Code, Claude Opus 5.5: merge, build, suite, patch-id comparison and diff review; this report.

@github-actions github-actions Bot added area:recompiler RDNA2 decoder, HLSL/direct-DXIL recompiler, wave64/wave32 gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) needs-1st-check Passes gates; needs first independent merge-check (#3041) needs:amd-test Needs a test report or merge-check on an AMD GPU needs:nvidia-test Needs a test report or merge-check on an NVIDIA GPU and removed needs-1st-check Passes gates; needs first independent merge-check (#3041) needs:amd-test Needs a test report or merge-check on an AMD GPU needs:nvidia-test Needs a test report or merge-check on an NVIDIA GPU labels Nov 29, 2026
@throny

throny commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

merge-check claim
head: b7cfe9b

@throny

throny commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

merge-check
base: fa20459
head: b7cfe9b
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2608 passed, 0 failed, 49 ignored: exactly the base's result, because the merge changes nothing (27 SKIPPED lines). Base alone at fa20459: 2608 passed, 0 failed, 49 ignored.
review: the whole change is already on main: commit 09a65d6 ("Recompiler: prune unreachable direct-DXIL module values") is an ancestor of fa20459, the PR's diff reverses cleanly on main's module.rs, and a trial merge of this head gives main's own tree. This agrees with the other check in the thread. The code itself had no finding (every constant id is renumbered through one map, liveness walks reachable blocks and metadata, and the pass iterates vectors only, so its output is deterministic).
performance: nothing to add on main.
verdict: not yet: nothing to merge; already in main through 09a65d6, so this PR can be closed.
hardware / tool and model: RTX 4080 16 GB (621.40), i9-12900K, Windows 11 26300, Rust 1.100.0, debug. Claude Code; merge, build and suite run by Claude Fable 5.2, diff review by Claude Opus 5.5 (every hunk read; nothing run on the GPU beyond the suite).

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head b7cfe9b

main has this change. Commit 09a65d6 ("Recompiler: prune unreachable direct-DXIL module values") says it was adapted from this PR. gpu/dxil/module.rs on main has Module::live_values with the same transitive liveness over global initializers and aggregates, the cmap compaction of BLOCK_CONSTANTS, and the three tests (unused_scalar_constants_produce_identical_bitcode, dead_aggregate_does_not_retain_unused_global_while_live_global_init_is_kept, pruned_direct_dxil_shader_with_live_table_and_dead_constants_validates). The other PRs for #2508 (#2613, #2635, #2641, #2642, #2664, #2819) change other things.
Recommendation: close (superseded by main commit 09a65d6).

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

@throny

throny commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Closing as superseded: this change is already on main (09a65d6). git patch-id of the PR diff is identical to that of main commit 09a65d6 (ancestor of main)

Two independent checks found this: Foniass's close list on #1607 and ours against current main. If something here is still missing on main, say so and reopen it, or ask and I will.

AI use: Claude Code (Claude Opus 5.5); the per-PR comparison was done by Claude Sonnet 5.5 sub-agents.

@throny throny added the duplicate This issue or pull request already exists label Dec 1, 2026
@throny throny closed this Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:recompiler RDNA2 decoder, HLSL/direct-DXIL recompiler, wave64/wave32 duplicate This issue or pull request already exists gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants