Skip to content

Recompiler: outline repeated direct-DXIL helpers into internal functions - #3654

Draft
Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:pr/dxil-helper-outlining
Draft

Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:pr/dxil-helper-outlining

Conversation

@Kalmarv

@Kalmarv Kalmarv commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

What is in it

Outlines repeated direct-DXIL helper sequences into shared internal LLVM functions with inherited BLOCKINFO function abbreviations, rebased directly onto main (5eaadb54c) on top of #3655's HelperBody representation (+968/-150 across 10 files in crates/xcl/src/gpu/dxil/: +547/-143 production code and +421/-7 unit/DXC-validation tests). This PR is now independent of #3645 (no longer stacked on it).

  • Shared BLOCKINFO function abbreviations and internal helper builders (module.rs, bitreader_tests.rs):
    • Reuses #3655's existing HelperBody / FuncDecl.helper_body representation (no duplicate struct or emission path).
    • Emits BLOCK_BLOCKINFO / BLOCKINFO_CODE_SETBID function-block abbreviations once per module so main and every internal helper inherit the same abbreviations; updates the bitreader test helper to decode inherited BLOCKINFO abbreviations.
    • Adds Op::CallOutput (reads a private alloca slot written by a multi-word helper call) and internal helper builders for heap loads (xcl.sbuf.word, xcl.sbuf.word2, xcl.sbuf.word4, xcl.raw.word4), dynamic format decode (xcl.fmt.decode), legacy multiply (xcl.mul.legacy), word-result fmad (xcl.fmad.words), and carried flat read windows (xcl.read.carry).
  • Lowering integration and opt-out switch (kernel.rs, lower.rs, lower_mem.rs, lower_window.rs, prelude.rs, shader.rs):
    • Routes repeated heap-window scalar buffer loads, wide raw heap loads, dynamic format decodes, v_mul_legacy_f32, non-compute/compute word-result fmad, and carried flat read windows through the shared internal helpers.
    • Leaves fmt_encode, scalar_flat_read, and f16 RNE rounding inline (dropping the overlapping #3650 hunks and xcl.f16.rne — which also resolves the RTX 2070 SUPER gpu_s_round_mode_controls_f16_rounding_across_modes driver failure — as well as the unrelated ballot_ps_quad/waterfall hunks).
    • Adds XCL_DXIL_OUTLINE=0 (kernel::outline_enabled()) to keep all outlined helpers inline and appends xcl-dxil-outline-0 to the translator cache tag.
  • Optimizer and dispatch-splitter accounting (module/opt.rs, module/split.rs):
    • Handles Op::CallOutput in operand traversal/rewriting and DCE, marks ReadNone helper bodies lane_local, and includes all four heap-load helpers (SCALAR_HEAP_WORD, SCALAR_HEAP_PAIR, SCALAR_HEAP_QUAD, RAW_HEAP_QUAD) in both DCE removable and the flagged raw-load preservation guard.
    • Updates infer_piece_features to infer Feature::ResourceHeapIndexing for all four heap-load helpers and updates dispatch_block_cost to charge every f.helper_body against piece instruction budgets.
  • Tests (module.rs, module/opt.rs, module/split.rs, lower_mem.rs, bitreader_tests.rs):
    • Adds unit and DXC-validation tests covering BLOCKINFO abbreviation inheritance, xcl.fmad.words, xcl.read.carry, xcl.mul.legacy, xcl.fmt.decode, xcl.sbuf.word* / xcl.raw.word4, SCALAR_HEAP_WORD DCE + flag preservation, split-piece feature inference and helper-body budget accounting, and XCL_DXIL_OUTLINE=0 inline opt-out.
  • Why this is kept as one PR: The non-test diff is +547/-143 (with +421/-7 in tests). The BLOCKINFO abbreviations, Op::CallOutput / helper builders, lowering call sites, DCE/split accounting, and XCL_DXIL_OUTLINE=0 opt-out form one cohesive outlining change.

Tested

  • Hardware & toolchain: NVIDIA GeForce RTX 5070 Ti (driver 610.74), AMD Ryzen 9 9950X 16-Core, Windows 11 Pro 26200, rustc 1.99.0 (b940084d7 2026-09-28), DXC 1.9.2609.5 (debug + release).
  • cargo build --release -p xcl: clean (39 warnings; 0 new warnings vs main 5eaadb54c).
  • scripts/test.ps1 (cargo build --workspace, cargo bench -p xcl --bench '*' -- --test, cargo test --workspace -- --test-threads=4, with XCL_DXC_DIR set to DXC 1.9.2609.5): 2988 passed, 0 failed, 52 ignored across all 43 test targets (24 SKIPPED GPU tests on this adapter: 22 no wave64, 2 no heap windows; 0 DXC skips).
  • gpu::recomp::tests::gpu_s_round_mode_controls_f16_rounding_across_modes, scalar_heap_word_is_dce_removable_and_preserves_raw_load_flag, piece_budget_counts_every_outlined_helper_body, piece_feature_inference_marks_resource_heap_indexing_for_all_heap_load_helpers, and dxil_outline_opt_out_keeps_helpers_inline all passed with DXC 1.9.2609.5.

Regression run

Regression check only (both runs warm on the same title after an initial cache-warming run for the PR head, since changing crates/xcl/src/gpu/dxil/*.rs and emitted bitcode bytes busts the translated and driver shader caches on the first cold run, which recorded 1004 frames with 0 removals / 0 crashes). No frame-rate or memory speedup is claimed from the smoke delta.

Baseline (main):

xcl-smoke v1
commit: 5eaadb54c14e0fc29597b2a1689d7dbd83491303
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 60
exit: 0x0
frames shown: 1640 (PresentX 1640)
presents per 2 s (last 3): 60 61 62
device removals: 0
crashes: 0
peak commit: 17384 MB

PR head (warm):

xcl-smoke v1
commit: 29440ec740c65458b9421a107d78aac9a1a8cdbd
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 60
exit: 0x0
frames shown: 1694 (PresentX 1694)
presents per 2 s (last 3): 61 61 60
device removals: 0
crashes: 0
peak commit: 15177 MB

Not tested

  • AMD and Intel GPUs/drivers (including native wave64 hardware paths; 22 wave64 GPU tests skipped on RTX 5070 Ti).
  • Direct re-verification on RTX 2070 SUPER / driver 621.01 (though xcl.f16.rne has been removed and gpu_s_round_mode_controls_f16_rounding_across_modes passes on RTX 5070 Ti).
  • Opt-in XCL_HEAP_WINDOWS=1 GPU tests (2 skipped under default scripts/test.ps1).
  • Offline corpus bitcode-size and compile-time benchmarks were not re-measured on this rebased commit 29440ec74.

AI use

  • Codex (GPT-6) wrote the initial direct-DXIL helper-outlining changes.
  • Antigravity (Gemini 4 Neon) rebased the change directly onto main (5eaadb54c) atop #3655's HelperBody, dropped the #3650-overlapping and unrelated hunks (including xcl.f16.rne), addressed the opt.rs / split.rs / XCL_DXIL_OUTLINE=0 review findings and added unit/DXC-validation tests, ran cargo build --release -p xcl, scripts/test.ps1, and the 60 s smoke pair, and drafted this description.

@github-actions github-actions Bot added area:recompiler RDNA2 decoder, HLSL/direct-DXIL recompiler, wave64/wave32 area:tests Unit, integration, e2e tests, or testtitle gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) labels Dec 2, 2026
@Kalmarv Kalmarv closed this Dec 2, 2026
@Kalmarv Kalmarv reopened this Dec 2, 2026
@Foniass

Foniass commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Review (head 03d8ade, diff read, not a merge-check):

  • Too big, and it overlaps two other open PRs. This PR's own commit is +992/-180 on top of Recompiler: compact DXIL bitcode tables, types, and interned constants #3645. That is over the ~800-line limit, and the description does not say why it cannot be split. Part of it is the same code as other PRs. The HelperBody setup in module.rs (~96, ~1430, ~1595, plus the Value::Func work-list in liveness) is nearly line for line the same as Recompiler: eliminate redundant split-wave64 compute barriers and share exchange helpers #3655's commit b995046, down to the comment "The entry is declared when the Shader is created, before any helper". The xcl.f16.rne body (module.rs ~845) is the same RNE sequence that Recompiler: simplify direct DXIL binary16, D16, and buffer store lowering #3650 puts inline in f32_to_f16_rne. Recompiler: simplify direct DXIL binary16, D16, and buffer store lowering #3650 also reworks the dynamic fmt_pack/fmt_encode path and the carried flat-read window, as this PR does with xcl.fmt.encode and xcl.read.carry. Suggested split: (1) the helper infrastructure as one PR that both this and Recompiler: eliminate redundant split-wave64 compute barriers and share exchange helpers #3655 stack on, (2) the arithmetic helpers, (3) the heap-load helpers, (4) the format helpers. Each should leave out what Recompiler: simplify direct DXIL binary16, D16, and buffer store lowering #3650 already covers.
  • Unrelated changes are bundled in. These change no outlining: the PS quad compare now masks by the EXEC word (lower_valu.rs ~1345, lanes.rs ~879), scalar liveness in waterfall.rs ~166/~330 is now computed lazily, and f32_to_f16_rne swaps the old f32_to_f16_rm(f, 0) for a different algorithm (lower_wide.rs ~708). By reading, the quad change and the new RNE sequence look equivalent to the old code, but each belongs in its own PR.
  • Split budget misses two helpers. dispatch_block_cost (module/split.rs ~608) adds body sizes for xcl.sbuf.word, xcl.fmt.encode and xcl.read.carry only. xcl.f16.rne (21-instruction body; the old inline conversion was about 60) and xcl.mul.legacy (8) are not gated to whole entries, so split compute uses them too. Each call counts as one instruction. A piece heavy in v_cvt_f16_f32 or v_mul_legacy_f32 can then be several times over XCL_DIRECT_SPLIT_INSTRUCTIONS once the driver inlines the helper. This contradicts the description's claim that split boundaries stay within budget, and the test (~768) covers only xcl.sbuf.word.
  • Riskiest for drivers, and on by default. Non-inlined define internal functions with arguments in non-library cs/ps DXIL are something DXC never emits. Here they include createHandleFromHeap/annotateHandle plus pointer out-arguments to entry allocas (xcl.sbuf.word2/4, xcl.raw.word4, lower_mem.rs ~1289-1360). The evidence is the DXC validator and NVIDIA only. The maintainer's batch runs on AMD RDNA4, whose compiler this codebase already notes as sensitive (opt.rs ~730, shader.rs llvm.ident). There is no switch to turn outlining off. Please add an AMD run or an opt-out. Also, the smoke run shows 12% fewer frames than main (1215 vs 1387, still over the 80% gate), which the description does not explain.
  • Merge hazard with Recompiler: keep the raw-load shader flag guard in the DCE after block merging #3664. This PR widens the DCE raw-load guard flagged (opt.rs ~1473) to the heap-load helpers. Recompiler: keep the raw-load shader flag guard in the DCE after block merging #3664 moves that block into eliminate_dead. Whichever lands second must keep the helper names there, or a shader whose only raw loads are dead helper calls loses the load but keeps RawAndStructuredBuffers.

Fine: function records mark helpers internal and non-proto. Bodies follow the entry in declaration order, as the LLVM 3.7 reader expects. BLOCKINFO abbreviations are defined before any local ones, so their IDs match. CallOutput keeps the call live and is neither CSE'd nor forwarded. Live types skip entry-only lookups for helper-local operands. Feature scans and uses_fmad know the helper names. The helpers in this PR contain no wave, quad, derivative or barrier ops, so convergence and helper-lane behaviour inside them is unchanged.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

@Decodere

Decodere commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Diff review (head 03d8adeb8, read-only code review — not a test report):

Clean BLOCKINFO shared-abbreviation encoding (crates/xcl/src/gpu/dxil/module.rs:1572-1581), solid CallOutput non-CSE/non-forwarding design (opt.rs:523, :1555), and Value::Func work-list propagation for helper liveness (module.rs:1633-1666). Reading the full diff (+1677/-334 across 17 files) turned up 3 concrete issues worth addressing before merge:

  1. SCALAR_HEAP_WORD (xcl.sbuf.word) is omitted from opt.rs removable and the flagged raw-load guard (crates/xcl/src/gpu/dxil/module/opt.rs:526-534, :1473; crates/xcl/src/gpu/dxil/module.rs:81, :633-644):
    finish_heap_load marks 1-word scalar loads FnAttrs::ReadOnly (module.rs:637-638), whereas multi-word loads (width > 1) use FnAttrs::NoUnwind (module.rs:641). In removable (opt.rs:528-533), helper calls are only removable when d.attrs == FnAttrs::ReadNone or d.name matches SCALAR_HEAP_PAIR | SCALAR_HEAP_QUAD | RAW_HEAP_QUAD, and the ReadOnly fallback requires a "dx.op." prefix — so dead xcl.sbuf.word calls are never dead-code eliminated. flagged (opt.rs:1473) similarly omits SCALAR_HEAP_WORD. Adding SCALAR_HEAP_WORD to both removable and flagged enables DCE of dead 1-word outlined loads while still preserving RawAndStructuredBuffers validity when every raw load is dead.

  2. split.rs feature inference and piece-budget accounting miss several outlined helpers (crates/xcl/src/gpu/dxil/module/split.rs:298-309, :608-624):

    • split.rs:301-302 checks only SCALAR_HEAP_WORD for Feature::ResourceHeapIndexing, omitting SCALAR_HEAP_PAIR, SCALAR_HEAP_QUAD, and RAW_HEAP_QUAD (implicitly relying on whole_entry_loads preventing them in split pieces).
    • dispatch_block_cost (split.rs:608-624) adds helper body instruction counts only for SCALAR_HEAP_WORD, FORMAT_ENCODER, and CARRIED_READ_WINDOW, omitting xcl.f16.rne (module.rs:845, 21 instructions) and xcl.mul.legacy (module.rs:811-812, 8 instructions), both of which are emitted in split compute shaders (lower_wide.rs:709-710, prelude.rs:233). Pieces heavy in half-conversion or legacy multiply count each call as 1 instruction instead of the inlined body size.
  3. Stacking, overlap with #3645 / #3650 / #3655, and default-ON driver risk:

    • Commit bc4792159 (+685/-154) embeds open PR #3645 verbatim; commit 03d8adeb8 (+992/-180) also duplicates #3650's RNE half-conversion logic as xcl.f16.rne (module.rs:845), introduces a competing struct HelperBody (module.rs:96-101) that conflicts with #3655, and bundles unrelated ballot_ps_quad EXEC masking (lower_valu.rs:1345, lanes.rs:879) and lazy scalar_live_in (waterfall.rs:166, :330).
    • Rebasing after #3645 and #3650, splitting shared HelperBody infrastructure (shared with #3655) from the individual outlining passes, and adding an opt-out or AMD validation for default-ON define internal helpers with alloca out-pointers will keep review and bisecting clean.

AI use: Antigravity (Gemini 4 Neon) audited the full diff against main and drafted this review.

@Foniass

Foniass commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

merge-check
base: 1c8b551
head: 03d8ade
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2930 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none
review: read by one reviewer at this same head; the review is at #3654 (comment) and its findings stand. Most serious: its own commit is +992/-180 with no split, the split budget misses two new helpers, and outlining is on by default with no AMD run.
performance: 60 s smoke of a retail title, main 1c8b551 vs this merge (03d8ade): 1652 -> 1434 frames shown (86 %; last presents per 2 s: 60 60 60), peak commit 18228 -> 16099 MB, 0 device removals, 0 crashes. This run is lower than most today: the PC was in use at the time (a browser and Discord running), and main's own runs on this machine today ranged from 1370 to 1652 frames, so the difference isn't clearly from this PR.
verdict: not yet: its own commit is +992/-180 with no split, the split budget misses two new helpers, and outlining is on by default with no AMD run (see the review)
hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5

AI-written and posted by Foniass without a human check of each point.

@Royalflamejlh

Copy link
Copy Markdown
Contributor

With this PR in, main's gpu_s_round_mode_controls_f16_rounding_across_modes fails on native Windows with an NVIDIA card: RTX 2070 SUPER 8 GB (driver 621.01), Windows 11 26200, debug, 1 run each. v_mul_f16 under round-toward-zero gets the result of the v_pk_mul_f16 high half next to it (0xFBFF where 0x7BFF is expected).

This was found on our integration branch, which carries this PR at 44e1860a, not on main plus this PR alone. A first-parent bisect there passes at the commit before the merge (11d46940) and fails at the merge.

The translated module looks right: disassembled, each xcl.f16.rne call takes its own product. With the f32→f16 round-to-nearest-even inline again (as #3650 writes it) and the other helpers still outlined, the test passes on the same PC. So it looks like the driver mixing up two calls of the outlined helper. That is a guess from the symptom, not shown.

Not tested: main + this PR alone; AMD; other NVIDIA drivers.

AI use: Claude Code (Claude Opus 5.5) ran the bisect and wrote this comment.

Rebase helper outlining directly onto main (reusing xcl-dev#3655's HelperBody
representation) and outline repeated direct-DXIL helper sequences into
shared internal functions with inherited BLOCKINFO function
abbreviations:

- Heap raw buffer loads: xcl.sbuf.word, xcl.sbuf.word2, xcl.sbuf.word4,
  and xcl.raw.word4, reusing private alloca output slots for multi-word
  results in whole-entry dispatches.
- Dynamic buffer format decode: xcl.fmt.decode (leaving fmt_encode,
  scalar_flat_read, and f16 RNE rounding inline).
- Arithmetic and address helpers: xcl.mul.legacy, xcl.fmad.words, and
  xcl.read.carry.
- Optimizer and dispatch-splitter accounting: treat all four heap-load
  helpers as DCE-removable and raw-load flagged, infer
  ResourceHeapIndexing for them across split pieces, and charge every
  outlined HelperBody against dispatch_block_cost.
- Opt-out switch: XCL_DXIL_OUTLINE=0 keeps all outlined helpers inline
  and appends xcl-dxil-outline-0 to the translator cache tag.

Co-Authored-By: Codex (GPT-6)
Co-Authored-By: Antigravity (Gemini 4 Neon)
@Kalmarv
Kalmarv force-pushed the pr/dxil-helper-outlining branch from 03d8ade to 29440ec Compare December 2, 2026 15:46
@Kalmarv

Kalmarv commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Rebased #3654 directly onto main (5eaadb54c) as 29440ec74 (no longer stacked on #3645, and reusing #3655's HelperBody directly) and addressed all points from @Foniass, @Decodere, and @Royalflamejlh:

  • Dropped overlapping and unrelated hunks (+2437/-482 across 15 files -> +968/-150 across 10 files; +547/-143 production, +421/-7 tests):
    • Dropped xcl.f16.rne, fmt_encode, and scalar_flat_read edits so there is no overlap with #3650 (instead shared_scalar_words temporarily binds uma_base_val to the helper's base argument).
    • Dropping xcl.f16.rne also addresses @Royalflamejlh's RTX 2070 SUPER report on gpu_s_round_mode_controls_f16_rounding_across_modes, which passes in scripts/test.ps1 here (not yet re-verified on RTX 2070 SUPER / driver 621.01).
    • Dropped the unrelated ballot_ps_quad / waterfall hunks (core_tests.rs, lanes.rs, lower_valu.rs, lower_wide.rs, waterfall.rs now have zero diff).
  • opt.rs DCE & raw-load flag guard (@Foniass): Added SCALAR_HEAP_WORD alongside SCALAR_HEAP_PAIR, SCALAR_HEAP_QUAD, and RAW_HEAP_QUAD in both removable and flagged, covered by scalar_heap_word_is_dce_removable_and_preserves_raw_load_flag (validated with DXC 1.9.2609.5).
  • split.rs feature inference & piece budgets (@Foniass): Updated infer_piece_features to infer Feature::ResourceHeapIndexing for all four heap-load helpers and updated dispatch_block_cost to charge every f.helper_body (including #3655's helpers and xcl.mul.legacy) against piece budgets, covered by piece_feature_inference_marks_resource_heap_indexing_for_all_heap_load_helpers and piece_budget_counts_every_outlined_helper_body.
  • Opt-out switch (@Decodere): Added XCL_DXIL_OUTLINE=0 (kernel::outline_enabled(), cache tag xcl-dxil-outline-0) to keep all outlined helpers inline, covered by dxil_outline_opt_out_keeps_helpers_inline.
  • Verification on 29440ec74 (RTX 5070 Ti, driver 610.74, DXC 1.9.2609.5):
    • cargo build --release -p xcl: clean, 39 warnings (0 new vs main 5eaadb54c).
    • scripts/test.ps1: 2988 passed, 0 failed, 52 ignored across 43 test targets (24 SKIPPED GPU tests: 22 no wave64, 2 no heap windows; 0 DXC skips; gpu_s_round_mode_controls_f16_rounding_across_modes passed; cargo bench -p xcl --bench '*' -- --test passed).
    • 60 s scripts/smoke.ps1 regression check (title: ea3b5e4d): main 5eaadb54c 1640 frames (0 removals, 0 crashes, 17384 MB peak commit) vs 29440ec74 warm 1694 frames (0 removals, 0 crashes, 15177 MB peak commit; initial cold-cache compile run was 1004 frames, 0 removals, 0 crashes). Updated the PR description with both xcl-smoke v1 blocks and regression-only framing (AMD/Intel and RTX 2070 SUPER still untested).

(Updated by Antigravity, Gemini 4 Neon, building on Codex, GPT-6's initial changes.)

@Kalmarv
Kalmarv marked this pull request as draft December 2, 2026 16:26
@Kalmarv

Kalmarv commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Moving to Draft until we verify compatibility with AMD (amdxc64.dll, #3694) and Linux vkd3d-proton (dxil-spirv, #3707).

In #3707, @lucsoft found that dxil-spirv aborts (std::terminate()) on internal DXIL functions that take pointer parameters (Cannot reliably convert LLVM pointer type), and #3694 shows AMD's Windows compiler crashing (0xc0000409 / 0x887A0005) on #3655's internal @xcl.spin.wait / @xcl.barrier.arrive helpers. This PR's xcl.sbuf.word2, xcl.sbuf.word4, and xcl.raw.word4 helpers currently pass private alloca i32* output pointers and emit internal DXIL functions as well, so it should not merge until we test whether value-only internal helpers (without pointer parameters) are accepted by both amdxc64.dll and dxil-spirv — or drop internal functions if not.

AI use: Antigravity (Gemini 4 Neon) cross-checked #3654 against #3694 and #3707 and drafted this note.

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 area:tests Unit, integration, e2e tests, or testtitle 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.

4 participants