Repository navigation
rdna2spirv: outline image_bvh(_64)_intersect_ray and f32_{sub,mul}_rtz into OpFunction helpers (#884, #4315) - #8615
Conversation
…z into OpFunction helpers (ItsNotRexed#884, ItsNotRexed#4315) Each image_bvh_intersect_ray / image_bvh64_intersect_ray previously inlined ~3,500 SPIR-V lines (~63 KB binary, including 48 inline copies of f32_sub_rtz/f32_mul_rtz, 6 OpLabels, and 2 OpSelectionMerges) directly into %main. Emit three shared module-level helpers once per module on first use: - %f32_sub_rtz_fn(%float, %float) -> %float (Pure) - %f32_mul_rtz_fn(%float, %float) -> %float (Pure) - %image_bvh_fn(%ulong host_addr, %uint ntype, %uint sort_u, %float tmax, %v3float org, %v3float dirv, %v3float inv) -> %v4uint_bvh Both 32-bit and 64-bit BVH intersection callsites share %image_bvh_fn and collapse to a single OpFunctionCall + 4 OpCompositeExtract in %main (0 branches/merges and 0 float ops in %main). Helper construction isolates body, _active_ssa, and bitcast_from, declares %v4uint_bvh after %v4uint_st (preserving SHADER-1016 type naming), and keeps NoContraction on every float op inside all three helpers. Measured on synthetic --wave64-emulate compute shaders vs main 7be7c4c (Vulkan 1.2): - 1 callsite: 69,596 B -> 26,024 B (-62.6%, 2.7x) - 5 callsites: 318,096 B -> 31,036 B (-90.2%, 10.2x) - 10 callsites: 628,732 B -> 37,312 B (-94.1%, 16.9x; --opt 324,816 B -> 24,500 B) - 25 callsites: 1,560,496 B -> 55,996 B (-96.4%, 27.9x; --opt 805,296 B -> 29,840 B)
|
Independent Vulkan comparison of 4b1b7191 and its direct parent 7be7c4c8. Windows 11, RTX 3080 Ti (621.35), subgroup size 32, LLVM 23.1.6, Vulkan SDK 1.4.363. Both stock fixtures,
For the optimized builds I added |
|
Tested
AI-assisted (Claude Code). |
|
Independent Linux / NVIDIA Turing check of Ubuntu, RTX 2060 6 GB, NVIDIA 595.91.07, subgroup 32, NVIDIA ICD explicitly selected. Python 3.13, LLVM 23.1.6, SPIRV-Tools 2026.4, Vulkan-Headers 1.4.335.
Not covered: Vulkan validation layers (not installed), AMD, full GPU/semantic suites, code review, or optimized/emulated GPU execution. Synthetic inputs only, no game files. AI-assisted (Claude Code). |
|
macOS arm64 verification (Apple M3 Pro) + Tested head Host: macOS 27.2.1, Apple M3 Pro (arm64), CPython 3.15.2, SPIRV-Tools v2026.4, Apple clang 21.0.0.
Negative control: with The risk this refactor carries, and why it is covered Outlining a body into an
Also checked by hand: The compactness assertions (4 No functional issues found. No game files. AI-assisted. |
|
Independent mixed-node Vulkan A/B on an RTX 4070 Ti SUPER (617.14, subgroup 32), Windows 11, All 8 modules (raw / |
|
Tested 4b1b719 on AMD, the first AMD run here: a Radeon iGPU (Ryzen 5 7600, RDNA2, subgroup 32..64, AMD Windows driver 32.0.21042.62) plus an RTX 5070 (617.42). Windows 11 26200, llvm-mc 23.1.6. The test programs were built from main 867d922. Main 867d922 was the control. pass, no regression On the Radeon this is RDNA2's own driver compiling the outlined
All are translated and The module size drops as the description says: AI-assisted: yes |
|
Tested
AI-assisted (Codex). |
|
Follow-up: the outlined helper executes correctly on a real GPU (MoltenVK), and the KosmicKrisp failure is the documented one I can build So the KosmicKrisp fails both, and that is expected, not this PR. Running the same
Nothing to change in the PR — I just wanted the pass to be on record from an independent driver, and to be explicit that the KosmicKrisp red is the known compiler limitation rather than a regression from outlining. No game files. AI-assisted. |
Claimed in: #8504 (comment)
Problem (#884, #4315)
Every
image_bvh_intersect_ray/image_bvh64_intersect_rayinstruction intools/rdna2spirv.pypreviously inlined ~3,500 SPIR-V lines (~63 KB binary) directly into%main, including:f32_sub_rtz+ 24×f32_mul_rtzacross the 4×6 box slabs),OpLabels and 2OpSelectionMerges per callsite inside%main.In raytracing compute shaders with multiple BVH traversal sites (such as
ScreenProbes ActivateRaysin #884 / #4315 andRaytracingResolve), this inflates%mainby tens of thousands ofNoContractionfloat ops and extra basic blocks before driver LLVM even starts.What this PR changes
f32_sub_rtzandf32_mul_rtzinto sharedPurehelpers (%f32_sub_rtz_fn,%f32_mul_rtz_fn) emitted once per module on first use (OpTypeFunction %float %float %float).%image_bvh_fn(OpFunction %v4uint_bvh None %fn_image_bvh) taking (%ulong host_addr, %uint ntype, %uint sort_u, %float tmax, %v3float org, %v3float dirv, %v3float inv) and returning %v4uint_bvh via two single-value OpPhi merges and one OpReturnValue.image_bvh_intersect_ray(32-bit) andimage_bvh64_intersect_ray(64-bit) share the single%image_bvh_fn, collapsing every callsite in%mainto one OpFunctionCall + four OpCompositeExtracts (0 branches, 0 OpSelectionMerge, 0 float ops in%main).o_contraction() continues to decorate 100% of float arithmetic ops inside all three helpers.
Synthetic A/B benchmark vs
upstream/main(7be7c4c8)Synthetic --wave64-emulate compute shader with $ live alternating image_bvh_intersect_ray / image_bvh64_intersect_ray calls (Vulkan 1.2 binary size via ools/spirv_asm.py):
Verification (CPU-only, Windows 11, Python 3.12, synthetic inputs only, no game data)
tests/semantic/test_image_bvh_{box,box_hw,tri,tri_hw}.py(default): 16/16 tests PASS (all 1,610 RDNA2 hardware-measured subtests bit-exact, 8.16 s vs 10.84 s before)tests/semantic/test_image_bvh_{box,box_hw,tri,tri_hw}.py(OPT_SPIRV=True): 16/16 tests PASS (all 1,610 subtests bit-exact)tests/semantic/test_image_bvh_outline.py: 3/3 PASS (mixed 32/64-bit box32 + tri0 + unknown-node execution parity, --opt parity, --pc-tail-ubo Vulkan 1.2 binary assembly, NoContraction coverage, SHADER-1016 %v4uint_st preservation)upstream/main7be7c4c8(LEONIDA_TOOLS_DIR): 3/3 FAILtests/test_image_bvh_census.py(15/15),tests/test_bvh_tri_probe.py(17/17),tests/test_bvh_shaders_stage.py(13/13),tests/test_ci_registration.py(14/14),scripts/check_no_game_data.py(PASS)Not run locally
spirv-val(SPIRV-Tools) and Vulkan GPU cs_bvh / cs_bvh32 / RTX 4080 / NVIDIA 621.01: the corpus's two largest compute pipelines fail to compile (NVVM), a few take 8-16 GB and up to 26 min, and --jobs 14 crashed the driver #884 ActivateRays pipeline compilation (independent spirv-val + GPU A/B check would be welcome).AI-assisted.