Skip to content

perf(shader): fold integer compares by value sets and known bits - #6176

Merged
adriadam10 merged 3 commits into
boykopovar:mainfrom
mitul72:perf/shader-integer-folding
Oct 18, 2026
Merged

adriadam10 merged 3 commits into
boykopovar:mainfrom
mitul72:perf/shader-integer-folding

Conversation

@mitul72

@mitul72 mitul72 commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

What

Two commits.

Integer compare folding: IEqual32 and INotEqual32 against a constant fold when the other operand has a value set of at most 64 values (through shifts, bitfield extracts, and/or/xor/mul/umin with a constant, add and select) that excludes the constant or holds only it, or when a known bit of the operand contradicts the constant. Known bits come from constants, shifts and bitfield extracts by constants, and/or/xor, add (up to the first unknown bit), mul (trailing zeros), umin, select, phi and lane reads. ULessThan32 against a constant folds from the known-bit and value-set bounds. A fold pass computes known bits and value set once per compared operand, and a sum is not enumerated when its two operand sets hold more than 65 values together. All folds are identities of the operand's possible values; out-of-range M0 handling of the relative indexing lowerings is unchanged. With M0 = 4i + k, three of every four entries of an s_movrels/s_movreld compare chain fold away.

SPIR-V constant sharing: specialization emitted a new OpConstant per folded result. A folded value now reuses an existing constant of the same type and value for the operands the specialization decodes; other consumers (subgroup, extended instructions) keep their own definitions. An OpBitcast to its operand's type, or back to the type before a previous bitcast, becomes a copy. A compute program with one formatted load shrinks from 10730 to 1117 words, so the formatted-load sharing check now compares the words two more loads add with the words the first added.

Tests: IntegerFolding, additions to SpirvOptimizer, and the execution test RelativeIndexingKnownBits.

Tested

Linux, RX 9060 XT (RADV GFX12, local Mesa). Full ctest passes except agc_driver_tessellation, which segfaults inside RADV while linking pipeline libraries on unmodified upstream 703aa8f as well.

Performance, old base (upstream d539606 + #454 + the 1D fix), Astro Bot hub, GPU busy per frame: 166 ms vs 172-179 ms (-4 to -5%); with APS5_LOOP_GUARD=64, 136 ms vs 141-143 ms. Not re-measured individually on 703aa8f.

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 (the measurement base also included fix(libSceAgcDriver): retire a depth surface once another writer covers its memory #454 and the 1D fix; the change itself is independent)
  • Unblocks: none
  • GPUs tested: AMD Radeon RX 9060 XT (RADV GFX12, local Mesa build), Linux
  • AI-assisted: yes

🤖 Generated with Claude Code

@github-actions github-actions Bot added build shader core/shader agc libSceAgc* labels Oct 15, 2026
@github-actions

github-actions Bot commented Oct 15, 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
PR 6644 core/libs/prx/libSceAgcDriver/tests/SpirvOptimizer.cpp L550

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 1341 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 1368 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 1395 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 2434 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 4650 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 4891 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 5021 core/libs/prx/libSceAgcDriver/CMakeLists.txt
core/shader/recompiler/Optimization/include/Optimization/ConstantFolder.hpp
core/shader/recompiler/Optimization/src/ConstantFolder.cpp
PR 5371 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 5426 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 5499 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 5500 core/shader/recompiler/Optimization/src/ConstantFolder.cpp
PR 5925 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6044 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6185 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6186 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6192 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6234 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6280 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6300 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6320 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6360 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6412 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6481 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6486 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6499 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6501 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6503 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6535 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6747 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6763 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6788 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6808 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6868 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6937 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6941 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6946 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6947 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6963 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 7201 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 7223 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 7332 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 7395 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 7615 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 7642 core/libs/prx/libSceAgcDriver/CMakeLists.txt

This was referenced Oct 15, 2026
@Neithennn

Copy link
Copy Markdown
Contributor

Windows NVIDIA run, since the description lists RADV only.

Windows 11 Pro 26200, NVIDIA GeForce RTX 4080 (driver 617.42), AMD Ryzen 7 5800X, WinLibs GCC 15.2.0 r7, Ninja, Release, bundled dependencies, BUILD_TESTING=ON, ANYPS5_NO_SHADER_CACHE=1. Head 088f9509b merged onto main 92e50418f; full build succeeds.

  • agc_integer_folding, agc_driver_relative_indexing_known_bits, agc_driver_relative_indexing, agc_driver_scalar_relative_indexing, agc_formatted_buffer_once, agc_formatted_buffer_byte_stores: pass.
  • Full ctest -j 8: 1296/1296, including agc_driver_tessellation. Unchanged main on the same machine: 1294/1294.

Correctness only; no performance measurement and no title run.

AI-assisted: yes (runs done with Claude Code).

@nicolasfalesy

Copy link
Copy Markdown
Contributor

Tested on Linux (Arch), c++ (GCC) 16.2.1 20260810, Release, BUILD_TESTING=ON, main at 5d9dcc42d2ec5401aebd089028b8d4d7410a77b4. GPU tests ran on lavapipe (Mesa 26.2.2, llvmpipe with LLVM 22.1.8).

agc_driver_relative_indexing_known_bits (this PR's test) fails on main plus this PR (2 of 2):

AGC graphics: relative indexing with known M0 bits: wave32 lane 8 movrels_readfirstlane_add is 0x00000008, expected 0x0000180b

Most likely cause: lavapipe runs subgroups of 8 here (other tests print "subgroup size 8 does not hold exactly one wave32"), so a wave32 v_readfirstlane cannot see lane 0 from lane 8. Other wave32 tests skip on such devices, for example BitfieldMask.cpp:85 (if (device->Target().subgroupSize < Threads)). RelativeIndexingKnownBits.cpp:107-112 runs both Run(*device, 32, 32) and Run(*device, 64, 64) without that check.

Suggested fix: skip with VulkanTestSkipped when device->Target().subgroupSize is smaller than the wave.

Separately, this PR breaks two existing tests when merged with an older open PR (same main, lavapipe). Each PR alone passes, main alone passes, and each pair fails (2 of 2):

  1. With fix(shader): drop module constants nothing references after specialization #5315, agc_shader_disk_cache fails:
variants that differ only in a folded constant specialized to different SPIR-V

This is the check at ShaderDiskCache.cpp:1381 of #5315 (require(rgba == bgra, ...)). Both PRs change SpirvSpecialization.cpp. Most likely cause: the new shareConstants() of this PR (called at SpirvSpecialization.cpp:558) merges constants with the same type and value, so which constant id survives can depend on the folded selector (4 or 6 in that test), and the two specialized modules then differ. Whichever PR lands second needs to keep the output independent of the selector or adjust the test.

  1. With perf(recompiler): read the buffer stride and presence at run time in compute modules #5353, agc_redundant_value_elimination fails:
  2 pure instructions repeat an earlier one in their block of the 1832-word module
a wave64 shader that reads one register in several float operations computes nothing twice in a block

The two PRs share no file (#5353 changes DescriptorBindingBuilder.cpp and SpirvRuntimeBuffer.cpp), so this is a behaviour interaction. I did not find the exact cause.

AI-assisted: yes

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 17, 2026
@adriadam10 adriadam10 self-assigned this Oct 18, 2026
mitul72 and others added 3 commits October 18, 2026 21:06
IEqual32 and INotEqual32 against a constant fold when the other
operand has a value set of at most 64 values (built through shifts,
bitfield extracts, and/or/xor/mul/umin with a constant, add and
select) that excludes the constant or holds only it, or when a known
bit of the operand contradicts the constant. Known bits come from
constants, shifts and bitfield extracts by constants, and/or/xor, add
(up to the first unknown bit), mul (trailing zeros), umin, select, phi
and lane reads. The value set of x & mask leaves out the known bits of
x. ULessThan32 against a constant folds from the known-bit and
value-set bounds.

A fold pass computes the known bits and value set of each compared
operand once, so a chain of compares against one operand does not
repeat the analysis, and a sum is not enumerated when its two operand
sets hold more than 65 values together, since without wraparound it
then has more than 64 values.

All folds are identities of the operand's possible values; out-of-range
M0 handling of the relative indexing lowerings is unchanged. With
M0 = 4i + k, three of every four entries of an s_movrels/s_movreld
compare chain fold away.
SPIR-V specialization emitted a new OpConstant for every folded result.
A folded value now reuses the id of an existing constant of the same
type and value for the operands the specialization decodes
(VisitInputs); consumers it does not decode, such as subgroup and
extended instructions, keep their own constant definitions, and the
unused-constant removal keeps those definitions while anything names
them. An OpBitcast to its operand's type or back to the type before a
previous bitcast becomes a copy. A compute program with one formatted
load shrinks from 10730 to 1117 words, so the formatted-load sharing
check compares the words two more loads add with the words the first
one added instead of with the module size.
@adriadam10
adriadam10 force-pushed the perf/shader-integer-folding branch from 088f950 to 6d73220 Compare October 18, 2026 19:28
@github-actions github-actions Bot added perf Performance change perf numbers missing perf pull request without a before and after summary line labels Oct 18, 2026
@github-actions

Copy link
Copy Markdown

This pull request is labeled perf, so it needs the summary line of a capture on main and one with the change, from the same title, scene and machine, as the Performance report form asks. Please paste both aps5-summary.jsonl lines, attach the two traces and add the output of tools/trace_digest.py.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 18, 2026
@adriadam10
adriadam10 merged commit b6403dd into boykopovar:main Oct 18, 2026
10 checks passed
TheBlenDed pushed a commit to TheBlenDed/AnyPS5 that referenced this pull request Oct 25, 2026
…R-V constant sharing) at 088f950

# Conflicts:
#	core/shader/recompiler/SpirvBackend/src/SpirvSpecialization.cpp
TheBlenDed pushed a commit to TheBlenDed/AnyPS5 that referenced this pull request Oct 25, 2026
…onstant of its value

boykopovar#6176 aliases every folded result to the first module constant of the same
type and value. A select that boykopovar#5315's dead-constant test folds to a copy of
one constant then aliased to the variant's own selector constant whenever the
two held the same value, so variants that differ only in that folded constant
kept it (and its name) and specialized to different SPIR-V
(agc_shader_disk_cache). A folded OpCopyObject now aliases to its source
constant (or that constant's alias); other folds keep boykopovar#6176's canonical choice.

AI-assisted: yes
TheBlenDed pushed a commit to TheBlenDed/AnyPS5 that referenced this pull request Oct 25, 2026
boykopovar#5353 reads the buffer stride at run time in compute modules, so the
linear and swizzled buffer addresses multiply the stride instead of a
specialization constant. With index 0 the swizzled index MSB folds to 0, and
boykopovar#6176 shares that folded 0 with the module's own 0, so both address forms
emitted the same OpIMul 0, stride in one block (agc_redundant_value_elimination:
2 repeats). The specializer now folds OpIMul with a known 0 operand to 0,
which removes the dead address arithmetic of both forms.

AI-assisted: yes
TheBlenDed pushed a commit to TheBlenDed/AnyPS5 that referenced this pull request Oct 25, 2026
…ts referenced constants

agc_spirv_optimizer (built with ANYPS5_ENABLE_SPIRV_TOOLS, as upstream's Linux
job does; build-local has it off) failed on this branch before today's local
fixes as well. boykopovar#6176's test counted five module constants: %one and %three
stayed because nothing removed unreferenced module constants on its head.
boykopovar#5315 now drops them, so the module keeps %two, 7 and 8, the three the stored
lane + 23 reads. The test expects 3.

AI-assisted: yes
Pipodi pushed a commit to Pipodi/AnyPS5 that referenced this pull request Nov 14, 2026
…R-V constant sharing) at 088f950

# Conflicts:
#	core/shader/recompiler/SpirvBackend/src/SpirvSpecialization.cpp
Pipodi pushed a commit to Pipodi/AnyPS5 that referenced this pull request Nov 14, 2026
…onstant of its value

boykopovar#6176 aliases every folded result to the first module constant of the same
type and value. A select that boykopovar#5315's dead-constant test folds to a copy of
one constant then aliased to the variant's own selector constant whenever the
two held the same value, so variants that differ only in that folded constant
kept it (and its name) and specialized to different SPIR-V
(agc_shader_disk_cache). A folded OpCopyObject now aliases to its source
constant (or that constant's alias); other folds keep boykopovar#6176's canonical choice.

AI-assisted: yes
Pipodi pushed a commit to Pipodi/AnyPS5 that referenced this pull request Nov 14, 2026
boykopovar#5353 reads the buffer stride at run time in compute modules, so the
linear and swizzled buffer addresses multiply the stride instead of a
specialization constant. With index 0 the swizzled index MSB folds to 0, and
boykopovar#6176 shares that folded 0 with the module's own 0, so both address forms
emitted the same OpIMul 0, stride in one block (agc_redundant_value_elimination:
2 repeats). The specializer now folds OpIMul with a known 0 operand to 0,
which removes the dead address arithmetic of both forms.

AI-assisted: yes
Pipodi pushed a commit to Pipodi/AnyPS5 that referenced this pull request Nov 14, 2026
…ts referenced constants

agc_spirv_optimizer (built with ANYPS5_ENABLE_SPIRV_TOOLS, as upstream's Linux
job does; build-local has it off) failed on this branch before today's local
fixes as well. boykopovar#6176's test counted five module constants: %one and %three
stayed because nothing removed unreferenced module constants on its head.
boykopovar#5315 now drops them, so the module keeps %two, 7 and 8, the three the stored
lane + 23 reads. The test expects 3.

AI-assisted: yes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* build perf numbers missing perf pull request without a before and after summary line perf Performance change shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants