perf(shader): emit image_bvh_intersect_ray as one shared function per variant #5455

Closed
overlordxrz-source wants to merge 1 commit from upstream-pr/5455 into main
overlordxrz-source commented 2026-10-13 20:08:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by overlordxrz-source on 2026-10-13T20:08:33Z.


What

Port of @MichonGoddijn231849's a25a7e9a from his gta-v/testing branch, with him as the commit author. He okayed this port in #1203: "yes, if it still applies to main's image emitter. A test that compiles an RT kernel and checks the function is emitted once would cover it."

A wave64 program on a 32-wide host emitted the image_bvh_intersect_ray query inline once per lane half. That means the box and triangle tests were emitted again for every instruction and every half. When BDA probes are available (the default), each variant is now emitted once as a DontInline function, and each instruction calls it. The variants are A16 and the 64-bit node pointer, so there are up to four functions. When APS5_BDA_BYTE_READS forces the byte-read path, the query stays inline as before, since those reads need the instruction.

The query body is unchanged. It moves into EmitBvhQuery, which takes the descriptor, the address VGPRs and the pc as values, so it can serve as a function body. DefineBvhIntersectFunctions runs next to DefineDot2F32F16Function before the entry function, and it only defines the variants the program uses.

Why

These numbers are Michon's, measured on his branch (#1203, Fidelity and Performance RT modes). GTA V's ray tracing kernel went from 706k to 626k SPIR-V words, and driver compile time dropped 17%, at the same speed.

On current main, using the new test's kernel (four BVH instructions, one per variant):

Module main This PR
wave64 on 32 lanes 82,236 words 50,352 words (-39%)
wave32 43,860 words 44,180 words (+320, call overhead)
APS5_BDA_BYTE_READS=1 (inline path) 99,665 / 51,085 words 99,665 / 51,085 words (identical to main)

Test

New test agc_spirv_bvh_intersect_function (tests/SpirvBvhIntersectFunction.cpp). It recompiles a compute kernel with all four BVH variants, once as wave32 and once as wave64 on 32 lanes. It checks:

  • each variant is exactly one DontInline function named image_bvh_intersect_ray
  • there is one call per instruction (wave32) and one per instruction and lane half (wave64)
  • the wave64 module stays under 1.5 times the wave32 one

The module is validated with SPIRV-Tools when ANYPS5_ENABLE_SPIRV_TOOLS is on. The test skips under APS5_RAYTRACING and APS5_BDA_BYTE_READS.

Tested

  • Linux aarch64 (zig 0.17.0 clang, the recompiler built from source): the test passes. With main's SpirvImageEmitter.cpp and SpirvModuleEmitter.cpp it fails ("expected one image_bvh_intersect_ray function per variant, found 0").
  • The changed sources and the test compile for x86_64-linux-gnu and x86_64-windows-gnu.
  • SPIRV-Tools validation was not run here (built without SPIRV-Tools). The existing Bvh64IntersectRay execution test needs a GPU and was not run either.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none (compile size and time of ray tracing kernels; measured on Grand Theft Auto V, #1203)
  • GPUs tested: none here (no GPU in this environment); SPIR-V structure checked by the unit test, the GTA V numbers are Michon's on an RTX 5080
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4848836304 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5455). Originally posted by [overlordxrz-source](https://github.com/overlordxrz-source) on 2026-10-13T20:08:33Z. --- ### What Port of @MichonGoddijn231849's `a25a7e9a` from his [`gta-v/testing`](https://github.com/MichonGoddijn231849/AnyPS5-fork/tree/gta-v/testing) branch, with him as the commit author. He okayed this port in [#1203](https://github.com/boykopovar/AnyPS5/discussions/1203#discussioncomment-18905059): "yes, if it still applies to `main`'s image emitter. A test that compiles an RT kernel and checks the function is emitted once would cover it." A wave64 program on a 32-wide host emitted the `image_bvh_intersect_ray` query inline once per lane half. That means the box and triangle tests were emitted again for every instruction and every half. When BDA probes are available (the default), each variant is now emitted once as a `DontInline` function, and each instruction calls it. The variants are A16 and the 64-bit node pointer, so there are up to four functions. When `APS5_BDA_BYTE_READS` forces the byte-read path, the query stays inline as before, since those reads need the instruction. The query body is unchanged. It moves into `EmitBvhQuery`, which takes the descriptor, the address VGPRs and the pc as values, so it can serve as a function body. `DefineBvhIntersectFunctions` runs next to `DefineDot2F32F16Function` before the entry function, and it only defines the variants the program uses. ### Why These numbers are Michon's, measured on his branch (#1203, Fidelity and Performance RT modes). GTA V's ray tracing kernel went from 706k to 626k SPIR-V words, and driver compile time dropped 17%, at the same speed. On current `main`, using the new test's kernel (four BVH instructions, one per variant): | Module | `main` | This PR | |---|---|---| | wave64 on 32 lanes | 82,236 words | 50,352 words (-39%) | | wave32 | 43,860 words | 44,180 words (+320, call overhead) | | `APS5_BDA_BYTE_READS=1` (inline path) | 99,665 / 51,085 words | 99,665 / 51,085 words (identical to `main`) | ### Test New test `agc_spirv_bvh_intersect_function` (`tests/SpirvBvhIntersectFunction.cpp`). It recompiles a compute kernel with all four BVH variants, once as wave32 and once as wave64 on 32 lanes. It checks: - each variant is exactly one `DontInline` function named `image_bvh_intersect_ray` - there is one call per instruction (wave32) and one per instruction and lane half (wave64) - the wave64 module stays under 1.5 times the wave32 one The module is validated with SPIRV-Tools when `ANYPS5_ENABLE_SPIRV_TOOLS` is on. The test skips under `APS5_RAYTRACING` and `APS5_BDA_BYTE_READS`. ### Tested - Linux aarch64 (zig 0.17.0 clang, the recompiler built from source): the test passes. With `main`'s `SpirvImageEmitter.cpp` and `SpirvModuleEmitter.cpp` it fails ("expected one image_bvh_intersect_ray function per variant, found 0"). - The changed sources and the test compile for x86_64-linux-gnu and x86_64-windows-gnu. - SPIRV-Tools validation was not run here (built without SPIRV-Tools). The existing `Bvh64IntersectRay` execution test needs a GPU and was not run either. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none (compile size and time of ray tracing kernels; measured on Grand Theft Auto V, #1203) - [x] GPUs tested: none here (no GPU in this environment); SPIR-V structure checked by the unit test, the GTA V numbers are Michon's on an RTX 5080 - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-13 20:32:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-13T20:32:15Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#4171 core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterHelpers.hpp L145
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp L128
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp L1032
#4891 core/libs/prx/libSceAgcDriver/CMakeLists.txt L1225
#4932 core/libs/prx/libSceAgcDriver/CMakeLists.txt L1225
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp L128
#4964 core/libs/prx/libSceAgcDriver/CMakeLists.txt L1225
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6154623067 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5455#issuecomment-6154623067). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-13T20:32:15Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #4171 | `core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterHelpers.hpp` [L145](https://github.com/boykopovar/AnyPS5/blob/71c71f50b7a42ad1a62b78d9cd24f5a356965557/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterHelpers.hpp#L145)<br>`core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp` [L128](https://github.com/boykopovar/AnyPS5/blob/71c71f50b7a42ad1a62b78d9cd24f5a356965557/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp#L128)<br>`core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp` [L1032](https://github.com/boykopovar/AnyPS5/blob/71c71f50b7a42ad1a62b78d9cd24f5a356965557/core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp#L1032) | | | | #4891 | `core/libs/prx/libSceAgcDriver/CMakeLists.txt` [L1225](https://github.com/boykopovar/AnyPS5/blob/71c71f50b7a42ad1a62b78d9cd24f5a356965557/core/libs/prx/libSceAgcDriver/CMakeLists.txt#L1225) | | | | #4932 | `core/libs/prx/libSceAgcDriver/CMakeLists.txt` [L1225](https://github.com/boykopovar/AnyPS5/blob/71c71f50b7a42ad1a62b78d9cd24f5a356965557/core/libs/prx/libSceAgcDriver/CMakeLists.txt#L1225)<br>`core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp` [L128](https://github.com/boykopovar/AnyPS5/blob/71c71f50b7a42ad1a62b78d9cd24f5a356965557/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp#L128) | | | | #4964 | `core/libs/prx/libSceAgcDriver/CMakeLists.txt` [L1225](https://github.com/boykopovar/AnyPS5/blob/71c71f50b7a42ad1a62b78d9cd24f5a356965557/core/libs/prx/libSceAgcDriver/CMakeLists.txt#L1225) | | |
CnRJay commented 2026-10-14 00:58:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-14T00:58:26Z.

Review: COMMENTED


Splitting a large repeated body into a function is a reasonable way to cut SPIR-V size for wave64-on-32 programs, and the table shows the expected shape: −39% for wave64, +0.7% call overhead for wave32, the byte-read path unchanged.

A few points:

  • What DontInline buys depends on the driver. It is a hint. NVIDIA's compiler and Mesa (NIR inlines every function before ACO/LLVM) generally inline anyway, so the final ISA and the register allocation are the same as before. The gain is in parsing and the front-end passes before inlining, which matches the 17% compile-time drop with unchanged speed on Michon's branch. On a driver that honours the hint, the call also forces the 9-ish argument values through the calling convention, and the GPU-time side would need a check there. Worth saying in the description which driver the 17% was measured on.
  • The wave32 case gets only cost. For wave32 there is one call per instruction, the same count as the inline body, so the function only adds call overhead (+320 words in the test). Emitting the function only when an instruction would expand into more than one copy (wave64 on 32 lanes), and keeping wave32 inline, would make the change a pure win.
  • Testing. CONTRIBUTING asks shader-recompiler changes for the GPUs the GPU tests ran on, and this was tested on aarch64 without a GPU or SPIRV-Tools validation. Bvh64IntersectRay, plus a run of the new test with ANYPS5_ENABLE_SPIRV_TOOLS=ON, on at least one desktop driver would cover the moved body. The template's GPUs tested, Unblocks and AI-assisted lines are also missing.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5511842863 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5455#pullrequestreview-5511842863). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-10-14T00:58:26Z. **Review:** COMMENTED --- Splitting a large repeated body into a function is a reasonable way to cut SPIR-V size for wave64-on-32 programs, and the table shows the expected shape: −39% for wave64, +0.7% call overhead for wave32, the byte-read path unchanged. A few points: - **What `DontInline` buys depends on the driver.** It is a hint. NVIDIA's compiler and Mesa (NIR inlines every function before ACO/LLVM) generally inline anyway, so the final ISA and the register allocation are the same as before. The gain is in parsing and the front-end passes before inlining, which matches the 17% compile-time drop with unchanged speed on Michon's branch. On a driver that honours the hint, the call also forces the 9-ish argument values through the calling convention, and the GPU-time side would need a check there. Worth saying in the description which driver the 17% was measured on. - **The wave32 case gets only cost.** For wave32 there is one call per instruction, the same count as the inline body, so the function only adds call overhead (+320 words in the test). Emitting the function only when an instruction would expand into more than one copy (wave64 on 32 lanes), and keeping wave32 inline, would make the change a pure win. - **Testing.** CONTRIBUTING asks shader-recompiler changes for the GPUs the GPU tests ran on, and this was tested on aarch64 without a GPU or SPIRV-Tools validation. `Bvh64IntersectRay`, plus a run of the new test with `ANYPS5_ENABLE_SPIRV_TOOLS=ON`, on at least one desktop driver would cover the moved body. The template's `GPUs tested`, `Unblocks` and `AI-assisted` lines are also missing.
oneandonlydean commented 2026-10-14 02:26:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:26:33Z.


Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159514294 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5455#issuecomment-6159514294). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T02:26:33Z. --- Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!5455
No description provided.