Skip to content

Recompiler: replace tn[4]/id[4] local arrays in bvh_intersect with scalars - #2302

Open
fixgep wants to merge 1 commit into
xcl-dev:mainfrom
fixgep:perf/hlsl-bvh-intersect-scalar
Open

fixgep wants to merge 1 commit into
xcl-dev:mainfrom
fixgep:perf/hlsl-bvh-intersect-scalar

Conversation

@fixgep

@fixgep fixgep commented Nov 25, 2026

Copy link
Copy Markdown

Part of #2088. Replaces local array and vector-subscript indexing (tn[4], id[4], pairs[5], child[k]) and scalar ?: ternaries in PRELUDE_MEM's bvh_intersect helper (crates/xcl/src/gpu/recomp/emit.rs) with scalar registers and branchless select operations.

At -O0 (> 8 MB HLSL), DXC emitted 5 stack allocas and dynamic getelementptr sequences per bvh_intersect call site; at -O1 and -O3, per-k type == 5u branches inside the unrolled loop, if (tn[j] < tn[i]) swap diamonds, and scalar ?: ternaries on triangle offsets bloated control-flow graphs and slowed both DXC and cold NVIDIA PSO compilation.

What is in it

  • crates/xcl/src/gpu/recomp/emit.rs: replaces [unroll] for k + tn[4]/id[4]/pairs[5]/child[k] in bvh_intersect with a single type == 5u box-load branch (bvh_box16_bounds), 4 scalar bvh_box_t evaluations (tn0..tn3, id0..id3), a branchless 5-step bvh_swap sorting network inside if (sort), and select for GPURT triangle offsets and hit selection; adds bvh_intersect_uses_scalars_without_local_arrays.
  • crates/xcl/src/gpu/recomp/tests.rs: adds DXIL IR validation/zero-alloca test bvh_intersect_dxil_has_zero_allocas_and_validates (-O0, -O1, -O3), wave32/split-wave64 edge-case parity test gpu_bvh_intersect_scalar_matches_reference, and manual cold PSO benchmark bench_cold_bvh_intersect_pso.

Controlled synthetic benchmark (32 IMAGE_BVH(64)_INTERSECT_RAY calls in an S_CBRANCH_VCCNZ traversal loop, dynamic sort, mem_windows = 4, median of 3 timestamp-salted runs on NVIDIA GeForce RTX 3060 / driver 32.0.15.9144, DXC 1.9.2602.17, warm re-create verified 50–69 ms):

Mode DXIL bytes IR alloca / gep / br / phi DXC median (ms) Cold PSO median (ms)
-O3 baseline 268,916 0 / 5 / 2,399 / 3,147 11,047.8 5,606.79
-O3 scalar 254,500 (-5.4%) 0 / 5 / 1,311 (-45.4%) / 2,763 7,684.4 (-30.4%) 4,599.03 (-18.0%)
-O1 baseline 269,648 0 / 5 / 2,396 / 3,146 6,449.7 5,391.07
-O1 scalar 254,544 (-5.6%) 0 / 5 / 1,308 (-45.4%) / 2,762 5,236.0 (-18.8%) 4,665.43 (-13.5%)
-O0 baseline 372,396 162 / 3,013 / 5,746 / 3,470 1,042.9 6,801.85
-O0 scalar 318,152 (-14.6%) 2 / 5 / 3,186 (-44.6%) / 3,022 810.2 (-22.3%) 5,838.35 (-14.2%)

Tested

  • Hardware & toolchain: NVIDIA GeForce RTX 3060 (driver 32.0.15.9144), AMD Ryzen 5 7500F 6-Core Processor, Windows 11 x64, rustc 1.100.0 (0ef1bc386 2026-11-09), Windows SDK DXC 1.9.2602.17, debug & release.
  • cargo test -p xcl --lib gpu::recomp -- --test-threads=4: 369 passed, 1 failed (gpu_clamp_and_omod_per_instruction on v_pk_add_i16 clamp, verified failing identically on main 1a0bbe3), 20 ignored (SKIPPED: 21 no wave64, 3 refused #1426 f16 forms).
  • cargo test -p xcl --lib -- --test-threads=4: 1183 passed, 1 failed (gpu_clamp_and_omod_per_instruction, pre-existing on main), 27 ignored (SKIPPED lines: 21 no wave64, 3 refused #1426 f16 forms, 1 ShaderCacheControl / Developer Mode unavailable).
  • cargo test -p xcl --test '*' -- --test-threads=4 (after cargo build --workspace): 70 passed, 0 failed, 20 ignored (SKIPPED: 1 heap_window_out_of_memory_falls_back_to_the_switch_form: no heap windows; XCL_SAMPLE_* unset).
  • cargo test --release -p xcl --lib gpu::recomp::tests::bench_cold_bvh_intersect_pso -- --ignored --nocapture: 1 passed, 0 failed (reported above; ShaderCacheControl(DISABLE) unavailable 0x887A002D, cold misses verified via salted DXIL cache misses vs warm repeats).

Regression run

xcl-smoke v1
commit: 1a0bbe3a2ba91ea8f5b6fc29130a053868d3c9e6
gpu: NVIDIA GeForce RTX 3060
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 495 (PresentX 496)
presents per 2 s (last 3): 22 22 22
device removals: 0
crashes: 0
peak commit: 15410 MB
xcl-smoke v1
commit: 30985d3f48b605d1cdc9d4c39256afcb14ffea1a
gpu: NVIDIA GeForce RTX 3060
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 497 (PresentX 497)
presents per 2 s (last 3): 59 60 60
device removals: 0
crashes: 0
peak commit: 15331 MB

Not tested

  • Native wave64 GPU hardware paths (AMD RDNA2; skipped with no wave64 on RTX 3060).
  • Non-Windows builds (unsupported target OS) and XCL_SAMPLE_* retail shader packs (unset).
  • ID3D12Device9::ShaderCacheControl cache disable (Windows Developer Mode not active; cold PSO runs verified via salted DXIL cache misses vs 50–69 ms warm hits).

AI use
Antigravity (Gemini 4 Hydrogen): replaced bvh_intersect array indexing and scalar ternaries with scalar registers and select helpers in crates/xcl/src/gpu/recomp/emit.rs, wrote the synthetic IR/GPU parity tests and cold NVIDIA PSO benchmark, and ran the test, benchmark, and smoke checks.

…alars

At -O0 (>8 MB HLSL) bvh_intersect emitted 5 allocas per call site for tn[4], id[4], pairs[5], and child[k], while at -O1/-O3 the per-k type==5u branches, swap diamonds (tn[j] < tn[i]), and scalar ?: ternaries inflated control-flow graphs and DXC/driver compile times.

Unroll the 4 child boxes manually with a single type==5u branch, compute hit/t via scalar registers tn0..tn3 and id0..id3 using branchless select helpers, replace the sorting network branches with select swaps, and use select for triangle offset/hit selection.

Measured on RTX 3060 (32 BVH calls in traversal loop, mem_windows=4, DXC 1.9.2602.17): -O3 cold PSO -18.0% / DXC -30.4%, -O1 cold PSO -13.5% / DXC -18.8%, -O0 allocas 162->2 / cold PSO -14.2%.

Part of xcl-dev#2088.

Assisted-by: Antigravity (Gemini 4 Hydrogen)
@DarkPanda69

Copy link
Copy Markdown

The native wave64 run listed as untested (on RDNA4 rather than RDNA2), plus an RTX 3060: the new test passes on both.

Head 30985d3 applied on main 1a0bbe3, debug, Windows 11 Pro 26200, cargo test -p xcl --lib -- gpu:: --test-threads=4:

Not tested: RDNA2, XCL_SAMPLE_* shader packs, cold-cache PSO timing.
AI use: Claude Code (Claude Opus 5.5) ran the tests on this machine and wrote this report.

@jeroenhaentjens

jeroenhaentjens commented Nov 25, 2026 •

Copy link
Copy Markdown

Independent Turing validation of 30985d3f48b605d1cdc9d4c39256afcb14ffea1a (parent 1a0bbe3a2ba91ea8f5b6fc29130a053868d3c9e6). On this machine, DXC compilation improved in all three modes; median first PSO creation improved at O0 but was slower at O1/O3 in this run.

Two test-only commits based on this PR head are available for cherry-pick:

  • 8ba2877: exercise split-wave64 and check all triangle outputs: the new reference fixture now explicitly exercises wave32 and split-wave64 on NVIDIA, uses the HLSL route, and checks all four output words for hit/miss cases of triangle types 0..3. Native wave64 remains conditional on support.
  • 905fef6: balance the manual compilation benchmark: identical synthetic input within each pair, four AB/BA/AB/BA pairs per explicit optimization mode, fresh salts, DXIL validation/fingerprints, even-sample median, and first/repeat PSO labels. No runtime implementation changes.

Tested

Windows 11 Pro 26200, i7-9750H, 32 GB RAM, Quadro RTX 3000 6 GB (driver 32.0.15.9706 / 597.06), DXC 1.9.2609.5, Rust/Cargo 1.100.0, AC power. XCL_ADAPTER=nvidia; XCL_SAMPLE_*, XCL_DIRECT_DXIL, and XCL_HEAP_WINDOWS unset. Incremental compilation and profile debug symbols disabled.

  1. Unmodified PR head: cargo build --workspace --locked passed. cargo test --workspace --locked --no-fail-fast -- --test-threads=4 completed: 1252 passed, 1 failed, 47 ignored (exit 101). The new scalar-source, DXIL-validation and GPU-reference tests, plus the existing gpu_bvh_intersect_ray, passed. There were 27 explicit SKIPPED records: 20 native-wave64, 3 unsupported f16 forms (gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426), 2 missing D3D12 debug layer, 1 unavailable cache control, 1 disabled heap-window path. These records are separate from the harness totals.
  2. The failure was hle::d3d12x::bind::tests::execute_indirect_reads_the_arguments_written_before_it: 20/40 mismatches, zero results on even trials. This matches a historical full-suite log with unchanged crates/xcl relative to the parent, but that historical run enabled XCL_HEAP_WINDOWS=1. A fresh isolated parent-runtime attempt failed earlier in CreateComputePipelineState with 0x80070057, so it is inconclusive as a matched parent comparison; its adapter was not confirmed. I am not claiming a fresh baseline reproduction of the 20/40 failure.
  3. Supplemental test overlay: the strengthened reference test passed separately with the PR emitter and with the exact parent emitter substituted: 1 passed, 0 failed each, explicitly logging wave32 and split-wave64. The PR emitter was restored before benchmarking. These overlay runs are separate from the unmodified-head suite above.

Synthetic compilation measurements

The modified manual benchmark passed (release, 450.26 s). It retains the supplied 32-call BVH traversal workload, dynamic sort, four memory windows, and split-wave64. The embedded baseline helper matches the parent helper after accessor/comment normalization. No other compiler/GPU workload was launched by this validation.

Mode DXIL bytes, base → scalar Median DXC, ms Median first PSO, ms First PSO change
O3 268792 → 254380 8312.5 → 6321.0 11493.14 → 12596.01 +9.6%
O1 269524 → 254420 4989.4 → 4721.0 11018.91 → 11741.16 +6.6%
O0 372060 → 317804 826.6 → 659.4 22334.43 → 15705.37 −29.7%

DXC was faster in all 12 pairs. First PSO creation was slower in 3/4 pairs at both O3 and O1, faster in 4/4 at O0. First-PSO ranges, base/scalar in ms: O3 11467.27–13423.64 / 12152.26–12653.75; O1 10123.84–12353.89 / 10960.94–12429.38; O0 19661.46–24402.34 / 14159.85–18141.12. Repeat creation across all samples was 27.50–50.09 ms.

ShaderCacheControl(DISABLE) failed with 0x887A002D. Salts/fingerprints distinguish the inputs and direct compilation bypasses XCL's disk cache, but implicit driver-cache state is not proven cold. Four pairs are descriptive measurements, not statistical significance or a general NVIDIA speedup. Process-commit deltas are not peak-memory measurements.

Supplemental commands (with the linked test-only changes):

cargo test --locked -p xcl --lib gpu::recomp::tests::gpu_bvh_intersect_scalar_matches_reference -- --exact --nocapture --test-threads=1
cargo test --release --locked -p xcl --lib gpu::recomp::tests::bench_cold_bvh_intersect_pso -- --exact --ignored --nocapture --test-threads=1

Not tested: native wave64/AMD, retail shader packs or titles, GPU execution throughput, frame rate, time to first playable frame, or guaranteed cold-cache performance.

AI use: Codex and three agents prepared these test-only extensions, checked BVH semantics and benchmark arithmetic, ran the local validation, and wrote this report. The exact model identifier is unavailable in this session. This is a measured test report, not a formal code-review approval.

@luckyteagirl

Copy link
Copy Markdown
Contributor

Independent RX 6800M (RDNA2 native-wave64) check of exact PR head 30985d3f48b605d1cdc9d4c39256afcb14ffea1a.

Environment: Windows 11 build 26200; Ryzen 9 5980HX; Radeon RX 6800M driver 30.0.13002.13003; Rust 1.99.0 MSVC; DXC/DXIL 1.9.2609.5. I selected the discrete GPU with XCL_ADAPTER=6800M and required wave64 with XCL_REQUIRE_GPU=wave64.

I ran the clean-head bvh_intersect_ focused tests twice, once with XCL_DIRECT_DXIL=1 and once with XCL_DIRECT_DXIL=0. Each run finished 4 passed, 0 failed, 1 ignored, with no SKIPPED line. Passing tests were the scalar-source check, bvh_intersect_dxil_has_zero_allocas_and_validates, the existing GPU BVH ray fixture, and gpu_bvh_intersect_scalar_matches_reference. The manual cold-PSO benchmark was the ignored test.

The new reference fixture's gpu_waves() runs both native wave64 and wave32 on this selected adapter. It compares per-lane sorted and unsorted box32/box16 child IDs and GPURT triangle results, including ties, misses, invalid bounds, NaNs, axis-parallel rays, and negative tmax. The DXIL inspection fixture validated O0, O1, and default output and checked for zero local array allocations.

Limits: This is focused synthetic correctness evidence on RDNA2. I did not run the full workspace suite, title smoke, sampled shader packs, GPU throughput, or cold PSO timing on this head. I am not claiming a performance change.

AI use: OpenAI Codex (GPT-6; exact variant unavailable in this session) ran the tests, checked the exact head and clean worktree, and drafted this report. No game data was used.

@nnonno91

Copy link
Copy Markdown

Focused HLSL-path verification on exact head 30985d3f48b605d1cdc9d4c39256afcb14ffea1a with the Windows SDK's DXC 1.8.

Tested

  • Windows 11 25H2 build 26200.9974, Ryzen 7 9800X3D, Rust 1.100.0 MSVC, SDK DXC/DXIL 1.8.2502.11, unoptimized profile. AMD Radeon(TM) Graphics driver 32.0.21042.62 and RTX 5090 driver 32.0.16.1742.
  • cargo test --locked -p xcl --lib bvh_intersect -- --nocapture --test-threads=1 with XCL_DIRECT_DXIL=0 and each adapter selected via XCL_ADAPTER. Each run gave 4 passed, 0 failed, 1 ignored (the manual cold-PSO benchmark) and no skips.
  • DXIL validation and the no-local-array assertions passed in all three compiler modes. The GPU fixtures passed their ray intersection and scalar-versus-reference comparisons on both adapters.

Not tested

The benchmark, retail inputs, the parent, the full workspace suite and game regression runs.

AI use

OpenAI Codex (GPT-6.2 Sol) ran the tests and wrote this report.

@MarcosMDev

Copy link
Copy Markdown

Test report: 30985d3 on an RTX 4060: suite green, and this PR's own synthetic benchmark shows a driver gain only at -O0 on this card. No game data was used.

Suite, cargo build --workspace, then cargo test --workspace --locked --no-fail-fast -- --test-threads=4, debug, a separate target folder per commit:

  • base 1a0bbe3: 1250 passed, 0 failed, 46 ignored
  • head: 1253 passed, 0 failed, 47 ignored; the three new tests pass. 27 SKIPPED lines on both.

bench_cold_bvh_intersect_pso, release, nothing else of ours running, a new exe basename (ShaderCacheControl returned 0x887A002D, so the driver cache stayed on; the second call took 4 ms at most). Medians of the benchmark's 3 runs:

DXC DXIL DXC time cold PSO, base → scalar
default (O3) −5.4 % 7.39 → 5.86 s (−21 %) 8.73 → 9.08 s (+4.1 %)
-O1 −5.6 % 4.82 → 4.18 s (−13 %) 9.95 → 10.28 s (+3.3 %)
-O0 −14.6 % 0.94 → 0.68 s (−28 %) 19.32 → 14.80 s (−23.4 %)

The scalar form was slower in all 3 pairs at O3 and at -O1, and faster in all 3 at -O0. Allocas at O3 are 0 on both sides already. The benchmark builds the base form first in every pair, so an order effect is possible; I did not swap it. An earlier run overlapped other work, so I leave its numbers out.

So on this card the DXC time improves at every level, but I do not reproduce the −18 % cold PSO at -O3 reported from an RTX 3060.

RTX 4060 8 GB (32.0.16.2101), Ryzen 7 5700X, 24 GB, Windows 11 26200, rustc 1.100.0, DXC 1.9.2609.5. Not tested: any title, retail shaders, AMD / Intel, GPU time, the branch merged with today's main (it conflicts).

AI use: Claude Code (Claude Opus 5.5) read the diff, ran the tests and the benchmark, and wrote this.

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

Thanks!

@Aniseto

Aniseto commented Nov 25, 2026

Copy link
Copy Markdown

Ran the bvh_intersect tests at 30985d3:

Environment: Windows 11 Pro build 26200 x64, NVIDIA GeForce RTX 5070 Ti (Blackwell, wave32 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 bvh_intersect -- --test-threads=4 --nocapture
4 passed; 0 failed; 1 ignored; 1206 filtered out      (exit 0)

The new bvh_intersect_dxil_has_zero_allocas_and_validates and gpu_bvh_intersect_scalar_matches_reference pass, as does bvh_intersect_uses_scalars_without_local_arrays; the ignored one is the manual release benchmark.

What this adds: the author tested an Ampere RTX 3060 with Windows SDK DXC 1.9.2602.17; this is a Blackwell wave32 part with DXC 1.9.2609.5. The new GPU parity test ran on the real adapter here. I did not repeat the cold PSO timing benchmark.

Not tested: the cold PSO A/B timings, a paired 60 s smoke run, native wave64 hardware, AMD and Intel GPUs, Wine, release profile, retail 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.

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.

8 participants