Skip to content

Recompiler: eliminate DXIL bitcode double-copy and intermediate allocations in container assembly - #2664

Open
Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-dxil-container-single-copy-and-exact-sizing
Open

Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-dxil-container-single-copy-and-exact-sizing

Conversation

@Decodere

Copy link
Copy Markdown
Contributor

What is in it

Part of #2508
Part of #1833

Eliminates the full-bitcode double allocation/copy and intermediate part/string Vec allocations during direct-DXIL DXBC container assembly in crates/xcl/src/gpu/dxil/container.rs (+219/-108 lines; L76–131 hash/md5_block completely untouched for zero conflict with #1878; all public signatures, assert!(bitcode_len % 4 == 0), and emitted container bytes remain 100% identical):

  • Replaced program_part (which allocated a 24 + bitcode.len() Vec<u8> and copied the entire bitcode before copying it a second time into out) with a stack-allocated program_header(stage, sm, dxil_version, bitcode_len) -> [u8; 24] (retaining assert!(bitcode_len % 4 == 0)).
  • Added const EMPTY_SIGNATURE: [u8; 8] = [0, 0, 0, 0, 8, 0, 0, 0] (removing empty_signature() -> Vec<u8>) and a shared assemble_container(prefix_parts, stage, sm, bitcode) helper that computes exact total, allocates Vec::with_capacity(total) once (debug_assert_eq!(out.len(), total)), writes prefix parts and DXIL header + bitcode directly (1x memcpy, zero intermediate bitcode-sized Vec), and signs out[4..20] via hash(&out[20..]).
  • Updated container() and container_with_signatures() to pass stack arrays of borrowed slices (SFI0, ISG1, OSG1, optional PSG1, PSV0) to assemble_container, removing the duplicated serialization loops and intermediate parts: Vec / features.to_le_bytes().to_vec() heap allocations.
  • Exact-sized psv_part (computing str_len = (psv.entry.len() + 2).next_multiple_of(4) and exact_len = 68 + res_len + str_len, writing the string table directly into p with zero temporary strings Vec).
  • Updated signature_part to borrow offs: Vec<(&str, u32)> (zero String clones) and exact-size p = Vec::with_capacity(8 + 32 * elems.len() + strings.len()).
  • Exact-sized psv_part_full, removing the temporary all: Vec<&PsvSigElement> allocation by chaining psv.inputs/outputs/prims, pre-sizing strings, name_off, idx_off, and allocating p = Vec::with_capacity(p_len) once.
  • Kept digest_matches_dxc_signed_container, digest_covers_both_tail_layouts, and psv_matches_dxc_layout_for_compute_with_two_raw_uavs untouched, and added parts_and_containers_are_exact_sized_and_match_reference_layout and program_header_rejects_unaligned_bitcode.

Tested

  • Hardware & toolchain: Windows 11 Home (build 26200), Intel Core i5-11400F (6C/12T), 32 GB RAM, NVIDIA GeForce RTX 4060 8 GB (driver 621.14), rustc 1.100.0, DXC 1.9.2609.5 (01b62ad4), release builds.
  • Failing-first reproduction on unmodified b49be183: exact-capacity check failed with psv_part=104/160, signature_part=52/64, psv_part_full=76/80 (all over-allocated by Vec::new() growth).
  • Release container assembly micro-benchmark (standalone rustc -O back-to-back comparison of pre-fix program_part + parts Vec vs post-fix program_header + assemble_container, excluding untouched hash()):
    • 1 MiB bitcode (400 iterations): 533.0 µs/call -> 216.0 µs/call (2.47x faster, -59.5%, eliminating a 1 MiB transient allocation + copy).
    • 2 MiB bitcode (200 iterations): 974.6 µs/call -> 446.5 µs/call (2.18x faster, -54.2%, eliminating a 2 MiB transient allocation + copy).
  • WARP (XCL_ADAPTER=warp, XCL_WARP=1): cargo test --release -p xcl --lib -- gpu::dxil::container::tests:: --nocapture -> 5 passed; 0 failed; 0 ignored (0 skipped, 0.00s).
  • cargo clippy --all-targets: 0 new warnings in container.rs (only the 4 pre-existing chunks_exact / % 4 lints on untouched L82/L111 and intentionally kept L139/L281).
  • RTX 4060 targeted: cargo test --release -p xcl --lib -- gpu::dxil:: --nocapture -> 186 passed; 0 failed; 3 ignored (5.38s; container::tests:: 5/0/0 with 0 skipped; 1 pre-existing wave64 skip dispatch_chain_native_wave64_builds_whole).
  • Full release workspace on RTX 4060: cargo test --release --workspace --no-fail-fast -- --test-threads=4 --nocapture: 37 binaries, 1437 passed, 0 failed, 47 ignored (0 compiler warnings in container.rs), 29 pre-existing environment-gated SKIPPED lines (none in container):
    • 21 wave64-only GPU tests (no wave64 on RTX 4060 32-lane waves)
    • 3 per-lane wave32 shader tests (refused)
    • 3 D3D12 debug layer tests (no d3d12SDKLayers.dll / debug layer unavailable)
    • 1 heap-window OOM fallback test (no heap windows)
    • 1 PSO driver-cache measurement test (cannot disable the driver cache)

Regression run

Paired 60 s retail smoke (scripts/smoke.ps1, RTX 4060): both base and HEAD sustained 60 60 60 presents per 2 s with 0 device removals and 0 crashes (1536 vs 1562 total frames, 14023 MB vs 14009 MB peak commit).

Base (origin/main):

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

HEAD (fix-dxil-container-single-copy-and-exact-sizing):

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

Not tested

  • Cold-cache retail title compilation of thousands of uncached shaders end-to-end (this host's retail smoke runs against a warm shader cache; assembly speedup and exact sizing are measured directly in the 1 MiB / 2 MiB benchmark and verified byte-for-byte in unit tests).
  • The 29 hardware/environment-skipped workspace tests listed above (native wave64 GPUs, D3D12 debug layer SDK, heap windows, driver-cache-disabled PSO measurement) and non-Windows hosts.

AI use

Google Antigravity (Gemini 4 Neon) authored the container.rs single-copy / exact-sizing refactoring, parity tests, micro-benchmark, and verification runs; verified via failing-first check, WARP and RTX 4060 test suites, cargo clippy --all-targets, full RTX 4060 release workspace tests, and paired 60 s retail smoke runs.

…ations in container assembly

Replace `program_part` (which allocated a `24 + bitcode.len()` Vec and
copied the entire DXIL bitcode before copying it a second time into the
final container) with a stack-allocated 24-byte `program_header` and a
shared `assemble_container` helper that pre-sizes the output container
once and copies `bitcode` directly in a single pass. Eliminate heap
allocations for fixed 8-byte `SFI0`/`ISG1`/`OSG1` chunks and the
intermediate `parts` Vec, exact-size `psv_part`, `signature_part`
(borrowing `&str` instead of cloning `String`s), and `psv_part_full`
(removing the temporary `all` Vec). Keeps `hash`/`md5_block`, all public
signatures, `assert!(bitcode_len % 4 == 0)`, and emitted DXBC bytes
100% identical.

Part of xcl-dev#2508
Part of xcl-dev#1833

Assisted-by: Google Antigravity (Gemini 4 Neon)

@luckyteagirl luckyteagirl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could the full-PSV fixture compare against known pre-change PSV bytes? The container reference receives pfull from psv_part_full() under test, so the equality check verifies container placement and hashing but cannot catch a same-length PSV serialization change. In an isolated copy of this exact-head module, swapping psv.info1[0] and [1] in the serializer left the new test passing; an independent expected-byte check rejected the mutation. A golden PSV fixture or standalone reference serialization would support the PR's byte-identical guarantee. This is a test-oracle gap, not evidence that the current serializer emits wrong bytes. Verified with a focused standalone Rust harness at 902a8dc966ea7119f9c259701bb6819b1dcaa206; no full workspace or GPU run. AI-assisted review with Codex.

@DarkPanda69

Copy link
Copy Markdown

Native wave64 run on an AMD RX 9070 XT, plus an RTX 3060: the 2 new tests pass, and nothing else changes in the direct-DXIL GPU suite, so every direct-DXIL container the suite builds still validates and runs with this assembly code.

Head 902a8dc 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 710 passed, 2 failed, 6 SKIPPED
RTX 3060 (32.0.16.1088) 710 passed, 0 failed, 24 SKIPPED 712 passed, 0 failed, 24 SKIPPED

On both cards, the only result changes against main are the new parts_and_containers_are_exact_sized_and_match_reference_layout and program_header_rejects_unaligned_bitcode (should panic), both ok, with no new skips. The 2 RX 9070 XT failures are the AMD fuzz seeds tracked in #2115, the same on main.

Not tested: cold-cache compilation 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.

@SlopTart

SlopTart commented Nov 26, 2026 •

Copy link
Copy Markdown

Tested on the Linux/Wine lane, full standard env (AMD rig: Ryzen 9 9900X, 96 GB DDR5, RX 7900 XT 20 GB, Arch kernel 7.2.15, Mesa 26.2.5 RADV, wine-11.0 Proton, rustc 1.94.0; d3d12,d3d12core,dxgi,d3d11,d3d10core=n,b overrides, official DXC kit, VKD3D_CONFIG=ignore_shared_fence, threads=2), head 902a8dc9, mergeable against main b49be183:
non-gpu suite (--skip gpu::): 596 passed, 3 failed, 4 ignored, 735 filtered.
The failure set is identical to bare main b49be183 measured on the same rig the same day (596 passed, 3 failed): gpu_promoted_uav_clear (our #2490), xma2_source_voice (#2481, vendored-FAudio gate). The third red, the sparse tile video-memory test, is a known batch flake (green solo).
Zero delta from this PR; every test it adds passes inside the suite.
Not tested: gpu suite, Windows native.
Correction: an earlier version of this row reported three extra reds; those runs lacked the d3d12,d3d12core=n,b DLL overrides and hit stub-DLL NOTIMPL artifacts. Re-ran with the standard env, artifacts gone.
AI use: Hermes agent (local Qwen-class) ran the tests and drafted this; I checked it before posting.

@Aniseto

Aniseto commented Nov 26, 2026

Copy link
Copy Markdown

Ran the DXIL container tests at 902a8dc9:

Environment: Windows 11 Pro build 26200 x64, NVIDIA GeForce RTX 5070 Ti (Blackwell, 32-lane waves only, 16 GB) driver 32.0.16.2124, Intel Core i5-12400F, 64 GB RAM, rustc 1.100.0 (0ef1bc386 2026-11-09) MSVC 14.44.35207, DXC 1.9.2609.5, debug build.

XCL_ADAPTER=nvidia cargo test -p xcl --lib gpu::dxil::container::tests:: -- --nocapture
5 passed; 0 failed; 0 ignored; 1333 filtered out      (exit 0)

All five tests pass, including the two new ones (parts_and_containers_are_exact_sized_and_match_reference_layout and program_header_rejects_unaligned_bitcode). The panic line in the output is the expected should panic case.

What this adds: the author tested on an Ampere RTX 4060 in release mode with the same DXC version; this is a Blackwell wave32 part in the debug test profile. The thread has no report from another machine.

Not tested: the release micro-benchmark timings, a paired 60 s smoke run, the full workspace suite, AMD and Intel GPUs, native wave64, Wine, release profile, title data.

AI-assisted (OpenCode), model: DeepSeek V4.1 Flash Free - checked out the PR's head commit in a detached worktree, built it, ran the tests, wrote this report; the worktree was not modified; no game data was used.

bmjask pushed a commit to bmjask/xcl that referenced this pull request Nov 26, 2026
Write the DXIL program header and bitcode directly into one exactly sized container allocation. Remove intermediate part, string, and signature allocations while preserving all public formats and digest coverage.

Keep an independent golden full-PSV byte fixture from the prior serializer so a same-length field reordering cannot hide behind the new assembly helpers.

The upstream release microbenchmark reported 2.18-2.47x faster 1-2 MiB container assembly. GNU Windows-target cargo check passed here; Windows/MSVC, GPU tests, and title smoke were not run on this host.

Adapted from PR xcl-dev#2664.
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
Write the DXIL program header and bitcode directly into one exactly sized container allocation. Remove intermediate part, string, and signature allocations while preserving all public formats and digest coverage.

Keep an independent golden full-PSV byte fixture from the prior serializer so a same-length field reordering cannot hide behind the new assembly helpers.

The upstream release microbenchmark reported 2.18-2.47x faster 1-2 MiB container assembly. GNU Windows-target cargo check passed here; Windows/MSVC, GPU tests, and title smoke were not run on this host.

Adapted from PR xcl-dev#2664.
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
Write the DXIL program header and bitcode directly into one exactly sized container allocation. Remove intermediate part, string, and signature allocations while preserving all public formats and digest coverage.

Keep an independent golden full-PSV byte fixture from the prior serializer so a same-length field reordering cannot hide behind the new assembly helpers.

The upstream release microbenchmark reported 2.18-2.47x faster 1-2 MiB container assembly. GNU Windows-target cargo check passed here; Windows/MSVC, GPU tests, and title smoke were not run on this host.

Adapted from PR xcl-dev#2664.
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
Write the DXIL program header and bitcode directly into one exactly sized container allocation. Remove intermediate part, string, and signature allocations while preserving all public formats and digest coverage.

Keep an independent golden full-PSV byte fixture from the prior serializer so a same-length field reordering cannot hide behind the new assembly helpers.

The upstream release microbenchmark reported 2.18-2.47x faster 1-2 MiB container assembly. GNU Windows-target cargo check passed here; Windows/MSVC, GPU tests, and title smoke were not run on this host.

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

Assisted-by: Codex (GPT-5.6-sol)
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.

5 participants