Skip to content

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

Closed
overlordxrz-source wants to merge 1 commit into
boykopovar:mainfrom
overlordxrz-source:perf/bvh-shared-function
Closed

overlordxrz-source wants to merge 1 commit into
boykopovar:mainfrom
overlordxrz-source:perf/bvh-shared-function

Conversation

@overlordxrz-source

Copy link
Copy Markdown
Contributor

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, Grand Theft Auto V (PPSA04264) #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

… variant

A wave64 program on a 32-wide host emitted the query inline once per lane
half: GTA V's ray tracing kernel carried its box and triangle tests four
times. With BDA probes the query is now one DontInline function per
variant (A16, 64-bit node pointer), called at each instruction; the
byte-read path keeps the inline form. GTA V's RT kernel: 706k -> 626k
SPIR-V words, driver compile -17%, same speed.

Test: agc_spirv_bvh_intersect_function recompiles a kernel with all four
variants as wave32 and as wave64 on 32 lanes and checks that each variant
is one DontInline function, called once per instruction and lane half.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-authored-by: overlordxrz-source <overlordxrz-source@users.noreply.github.com>
@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

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

@CnRJay CnRJay 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.

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.

RINNECODER added a commit to RINNECODER/AnyPS5 that referenced this pull request Oct 14, 2026
…time CPU (#482)

* perf(shader): rewrite high-fan-out IR uses in linear time

Ported from upstream PR boykopovar#4035

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(shader): index specialization tables by SPIR-V id

Ported from upstream PR boykopovar#4772

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(shader): honor disabled optimization during specialization

Ported from upstream PR boykopovar#4827

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(shader): discard helper functions unreachable after specialization

Ported from upstream PR boykopovar#5064

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(shader): drop module constants nothing references after specialization

Ported from upstream PR boykopovar#5315

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(shader): cap SPIR-V scalar replacement

Ported from upstream PR boykopovar#4288

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(recompiler): iterate structurizer fixpoints in post-order and bound shared merge splitting growth

Includes the stacked upstream commit that structurizes refused control flow
with reach flags, which this change builds on.

Ported from upstream PR boykopovar#5028

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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

Ported from upstream PR boykopovar#5455

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(shader): avoid redundancy traversal map copies

Patches the bundled SPIRV-Tools redundancy elimination (only built with
ANYPS5_ENABLE_SPIRV_TOOLS=ON).

Ported from upstream PR boykopovar#5511

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(metal): accept a specialized BDA module that dropped one of its two BDA descriptors

Specialization removes descriptor variables the final module no longer
references. With helper functions now discarded after specialization, a
compute module can drop its page-table variable while keeping the fault
buffer, and the bridge then rejected the dispatch ("BDA ABI requires
page-table and fault-buffer descriptor metadata"). Require at least one of
the two roles instead of both; the module's own descriptors are still
checked against the prepared plan.

Not a port; needed by the port of upstream PR boykopovar#5064.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@oneandonlydean

Copy link
Copy Markdown
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* build shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants