perf(shader): speculate NaN repair per vector region (opt-in APS5_NAN_SPECULATION) #23344
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/23344"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by chitters2004 on 2026-11-18T22:08:36Z.
What
Every f32/f16 vector ALU result is repaired to match RDNA's NaN sources and default NaN. This is the largest single cost in GTA's heavy shaders: the no-repair bound (
APS5_FAST_F32-style, not exact) saves 7.18 ms GPU busy per present, so repair is worth attacking while staying exact.With
APS5_NAN_SPECULATION=1, a run of pure vector instructions (at leastAPS5_NAN_SPECULATION_MIN, at mostAPS5_NAN_SPECULATION_MAX) is translated twice: a fast copy without the repair that records whether any operand or result was a NaN, and the exact copy. The wave branches to the exact copy, from the register and exec state saved before the run, whenever a NaN was seen. Results are unchanged by construction; only a NaN-carrying wave pays for both copies.NanFlagPruner).APS5_NAN_SPECULATION=1alone reproduces it:APS5_NAN_SPECULATION_STAGES=compute,vertex,pixel,APS5_NAN_SPECULATION_MIN=compute:32,pixel:8,vertex:8(32 for the other stages) andAPS5_NAN_SPECULATION_MIN_OPS=32. Each variable overrides its default;APS5_NAN_SPECULATION_MINtakes a number for every stage or a per-stage list that overrides only the stages it names.[nan-spec-compile]lines (programs, speculated, declined, fell back by stage) print every 10 s in a boot withAPS5_PROFILE_DRAWand at the end ofagc_frame_replay. There are no run-time counters: speculation writes no guest-visible memory, andagc_nan_speculation_pixelchecks that no speculated program gets a GDS entry. How often a wave takes the exact copy in game is therefore not measured.All switches are startup-only and every setting (each stage's minimum, bounds, stages, fast-op and density limits) is mixed into the shader disk cache key.
Default and cost. Off by default (
APS5_NAN_SPECULATION=1to enable); this PR does not change the default. Apart from the disk-cache invalidation, nothing changes unless the switch is on: the SRT walker andConstantFolder::Foldfixes that run for every shader are in the PR this one depends on. The only code here that runs with the switch off is a smallResourceTrackerhardening (an invariant phi is used only where it is available, and lane selectors respect phi order); theagc_shader_replay --spvoutput with the switch off is byte-identical between the head before this PR's defaults change and this head, on the 319 replay requests (316 compile). Every disk-cache entry from before this change is invalidated, as for any recompiler source change.agc_shader_replay --spvover GTA's 319 requests (shader disk cache off, this head), summed emitted SPIR-V words: off 4,632,513 (316 produce SPIR-V);APS5_NAN_SPECULATION=1alone 4,696,921 (+1.4%, 317 produce SPIR-V). SettingAPS5_NAN_SPECULATION_STAGES=compute,vertex,pixel,APS5_NAN_SPECULATION_MIN=compute:32,pixel:8,vertex:8andAPS5_NAN_SPECULATION_MIN_OPS=32as well gives the same totals and byte-identical SPIR-V for every request, and the same SPIR-V as the previous headfd140217a4with those three set (317 of 317); the switch off is byte-identical tofd140217a4(316 of 316). Earlier measurement of the same switch on a previous base:MIN_OPS=64+0.7%, noMIN_OPS+12.3%. The three requests that fail with speculation off (25a5d4600, 25a9a0c00, 25a9e2600) fail on main since #12458 (bb00000fda), not here; with speculation on, 25a5d4600 compiles.[recompile] nan speculation failedlines over the 319. A compile or capture that still fails turns speculation off for that program, logs that line, and recompiles or prepares it again without speculation (NanSpeculationRetryon the prepared path).[nan-spec-compile]lines (every 10 s in a boot withAPS5_PROFILE_DRAW, and at the end ofagc_frame_replay) give programs, speculated, declined and fell back by stage. Each compiled program also logs one[recompile] nan speculationline with its candidates, speculated, declined and pruned counts.Tested
agc_driver_nan_speculation_region,agc_driver_pixel_nan_speculation(compile only below subgroup 32),agc_nan_speculation_pixel,agc_nan_speculation_stages,agc_nan_flag_pruning,agc_driver_f32_arithmetic_nan{,_fast},agc_f32_nan_result_selects,agc_driver_nan_quieting,agc_driver_f16_fma_invalid_nanpass.agc_nan_speculation_defaultsandagc_nan_speculation_overridespin the defaults and the overrides.agc_driver_nan_speculation_regionforces a NaN in one lane and checks the result against the exact program and the lane model; forv_fma_mixlo_f16it requires only a NaN when an operand is a NaN, because that instruction's NaN payload is the host's. Mutants on the fallback branches are killed.10a0263300(r4, before the audit fixes): run 44088501878, green. On this headbcbd2deecf: run 44402064158, green: 21 tests on Linux and 23 on Windows passed, 0 validation-layer VUIDs, nothing failing that passes on main. The tests are the NaN tests above,agc_nan_speculation_defaultsand_overrides, the shader disk-cache tests, and the first PR'sagc_constant_phiandagc_shader_memory*. An earlier run onbff39ce9ea(44191195433) hadagc_driver_nan_speculation_regionred on Linux, where the host LLVM orders the operands ofv_fma_mixlo_f16differently and so returns another NaN payload;fd140217a4fixes the test only, and its code outside tests is unchanged from08a5b80467.10a0263300) (RTX 3080, NVIDIA 621.24, Windows 11 26320, PCIe Gen4 x16, 3840x2160, Defender on; content 01.011.001; city scene, scripted walk,APS5_TRACE_SECONDS=108:30: a 30 s window from 108 s, which is 31 to 38 s after the player stands still (70.1 to 76.9 s in these boots), the same window in both arms; warm caches, one warm-up boot per build that is not a sample; order M S S M S M M S, 4 + 4 boots, all clean on the quiet check). Main is0859a63ae9, the PR is10a0263300; both carry #20845 and #14561, which GTA needs to boot, and nothing else. Both arms run the same environment, so the summary lines carry no configuration difference:APS5_DRAW_PIPELINE=1 APS5_PIPELINED_DRAWS=32 APS5_PIPELINE_DISPATCH=1 APS5_NAN_SPECULATION=1 APS5_NAN_SPECULATION_STAGES=compute,vertex,pixel APS5_NAN_SPECULATION_MIN=compute:32,pixel:8,vertex:8 APS5_NAN_SPECULATION_MIN_OPS=32(main has no reader of the speculation switches).[recompile] nan speculationlines (309 programs speculated); each timed PR boot logged 57 to 88 (variants come from the warm cache); every main boot logged none.aps5-summary.jsonlwindow line of the first main boot and of the first PR boot are below; all eight lines and the eighttools/trace_digest.pysummaries are attached.08a5b80467): same method, hardware and environment as run 1, exceptAPS5_TRACE_SECONDS=92:30(a 30 s window from 92 s, younger game state) and Windows Defender real-time protection off (on in run 1); 4 + 4 boots, all clean, order M S S M S M M S, main0859a63ae9with #20845 and #14561 against the same plus this PR.10a0263300. Main is the same in both runs (27.29 against 27.34 ms); the PR boots moved from 25.09 to 25.84 ms. Between the two heads nothing changes what the GPU runs: replaying GTA's 319 requests with and without the EXEC-pruning fix gives the same 978 pruned checks and 317 of 317 byte-identical SPIR-V, and no program falls back. What differs between the runs is the window (108:30 against 92:30) and Defender (on against off); the PR boots span 25.47 to 26.51 ms.Vulkan.GraphicsPipeline.create1887 and 842 ms, against 0 to 230 ms in main and 59 to 169 ms in the other two PR boots); across the eight boots p50 follows that creation time (correlation 0.90), and with it removed the difference is +0.9 ms. I read it as a cold pipeline-cache transient of the first PR boots plus noise, not a CPU cost of speculation: shader compile-and-link, capture and materialize time in the window are not higher in the PR boots.[recompile] nan speculation failedlines in the four PR boots: no program fell back.08a5b80467with the switch off against on (compute,vertex,pixel,MIN compute:32,pixel:8,vertex:8,MIN_OPS=32), order off on on off, all four boots clean: GPU busy per present 39.7 / 39.8 ms off against 39.7 / 39.7 ms on, fps 17.24 / 17.55 off against 16.53 / 17.37 on, frame p50 57.0 / 56.5 ms off against 57.5 / 57.3 ms on (+0.6 ms, within noise). Speculation ran: 52[recompile] nan speculationlines in each on boot, none in the off boots. The switch is neutral on this title: its GPU-heavy shaders gain nothing and nothing regresses. The GPU code is the same as in08a5b80467(fd140217a4changes only a test, and this head only the defaults, which equal the measured settings, and comments).APS5_NAN_SPECULATION=1 APS5_NAN_SPECULATION_STAGES=compute,vertex,pixel APS5_NAN_SPECULATION_MIN=compute:32,pixel:8,vertex:8, noCOUNT): GPU busy per present 26.05 / 26.14 ms off against 24.35 / 24.42 ms on (-1.71 ms, -6.6%); 0 fatal errors, 0 misspeculation lines. Frame p50 is unchanged (the scene is CPU-bound): 91.3 / 91.7 ms off, 90.4 ms on. Results: lab-notes results/ns3-1116. Picture equality: frame 268 is pixel-identical across the two off boots and one on boot (1A, 4A, 3B), and frame 206 across 1A and 3B. The per-program GPU split follows as a comment.c76fd68c56e): the split-point calls run inside the per-instruction translation, so a save point inside a speculation region is translated with it.agc_nan_speculation_stagesnow translates a save before and after each of two f32 adds and requires exactly oneStoreSplitStatefor every placement, a declined region when the save falls inside it, and a kept region for the save after the last add. The test is @JJenkx's (commit0c2e20d4be9, authored by them), from their AMD integration run in the comments; thank you. Windows, this head: passes; with the split call before each instruction removed it fails (stores=0, region kept).ns5 aps5-summary.jsonl window lines: main (boot 1) and this PR (boot 2)
main
0859a63ae9+ #20845 + #14561:this PR
10a0263300(run 1) + #20845 + #14561:ns7 aps5-summary.jsonl window lines: main (boot 1) and this PR (boot 2)
main
0859a63ae9+ #20845 + #14561:this PR
08a5b80467+ #20845 + #14561:Checklist
main; no other open PR implements the same functions.mdfiles or images added to the repository10a0263300and ns7 on08a5b80467)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-19T05:34:24Z.
Review: COMMENTED
Reviewed — comments
This review covers the prepared diff at head
bcbd2deecfa5414542389dceae58e6b6c3b32416; the broad change set and several key implementation files were inspected, but the review is incomplete.What I ran
No build or tests were run. I read the prepared description, commits, manifest, diff, file listing, and selected source files through
repo_read.py.Results
Findings
Blocking
None verified in the inspected portions.
Non-blocking
No specific non-blocking code issue verified.
Not verified
The review is incomplete, so no conclusion is offered on the full change. The manifest lists 44 changed files; only
NanFlagPruner.cpp,Recompiler.cpp(lines 1–180),InstructionTranslator.cpp(lines 1–280), andResourceTracker.cpp(lines 1–100) were read from repository files. The prepared description, commits, manifest, and first 300 lines of the diff were also read. The remaining changed files and source ranges were not read, including the large excluded filestests/Recorder.cppandtests/ShaderMemory.cpp;files.jsonwas not available through the reader. No tests, builds, or runtime checks were performed by this review.To get this to a good state
Complete the review of the remaining changed files and relevant source ranges, then run the applicable checks before reaching a formal decision.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-19T21:32:43Z.
Review: APPROVED
Reviewed — looks good
The reviewed changes at
aa1fd87ae987c4f52709ca807b48cbb8ed7b9c6fimplement opt-in speculative NaN repair with exact recomputation on flagged waves; I found no verified blocking defect in the inspected paths.What I ran
No build or tests were run. I inspected the prepared diff and read changed implementation and test hunks using
repo_read.pyat the pinned head. The PR description reports Linux and Windows CI, replay checks, and NVIDIA gameplay measurements; these were not run by me.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
I did not run a build, tests, replay, or console/game execution. The two excluded full files,
core/libs/prx/libSceAgcDriver/tests/Recorder.cppandcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp, were read only at their changed hunks. I did not inspect every included test file or every changed implementation line; the review focused on region construction, NaN check pruning, fallback/retry, cache versioning, and excluded test hunks. The author-reported CI and performance figures remain unverified here.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T21:38:24Z.
Review: COMMENTED
Reviewed — no blocking findings
The change at head
c76fd68c56e26cd750cdc7b35dd3d3e6c4cf6ea5adds opt-in NaN-repair speculation; the core translation, pruning, and retry paths inspected support its stated behavior, but review coverage is incomplete.What I ran
No commands or tests were run. I read the prepared description, commit list, manifest, diff, and targeted repository source through
repo_read.pyat the pinned head. The author reports CI and runtime measurements in the description; these were not independently run or verified.Results
Findings
Blocking
None identified in the code inspected.
Non-blocking
None.
Not verified
tests/Recorder.cppandtests/ShaderMemory.cppwere excluded from the review bundle; I read only their changed hunks throughrepo_read.py. I did not read every changed file listed in the manifest, run checks, verify the author-reported CI or runtime results, or assess hardware and console behavior beyond those reports.To get this to a good state
Nothing is required. This review is incomplete, so it does not support approval.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T22:39:34Z.
Review: COMMENTED
Reviewed — no blocking findings
Nothing blocks this change; the behavior claims are supported by the code inspected, but the review is incomplete because several changed files were not read.
What I ran
Reviewed head
0c2e20d4be99dbc5a86304d9ba2d190a2211e0dd. I inspected the prepared diff and targeted repository files withrepo_read.py-backed review tools; I ran no build or tests.Results
Findings
Blocking
None verified.
Non-blocking
None.
Not verified
Did not read every changed line. In particular, changed lines in
core/libs/prx/libSceAgcDriver/tests/Recorder.cppandcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppwere not reviewed; these files are listed as excluded by the review folder size rule. Other changed files were also not inspected line by line. No tests or build were run, and hardware behavior was not independently verified. The author-reported test and performance results in the description were not run by this review.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T14:03:03Z.
Review: COMMENTED
Reviewed — comments
The reviewed code adds opt-in NaN speculation; one unbounded shader-address registry may add lookup and memory costs over a long session, and the full change was not reviewed.
What I ran
Results
Findings
Blocking
None.
Non-blocking
core/shader/recompiler/Translation/src/InstructionTranslator.cpp:962records every shader address that successfully speculates. TheAppliedAddressesregistry inDispatchInstructions.cpphas no success-path removal, so it grows with unique shader addresses; its linear searches also make later checks progressively slower. Consider removing the address after the fallback window ends or using a bounded, lifecycle-scoped registry.Not verified
Recompiler.cpp,Recompiler.hpp,InstructionTranslator.cpp,DispatchInstructions.cpp, andNanFlagPruner.cpp; inspected portions ofreview/diff.patchand the prepared description, commits, and manifest.tests/Recorder.cppandtests/ShaderMemory.cpp; other changed files were not all inspected.To get this to a good state
Nothing is required. Consider bounding the successful-address registry.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T22:53:02Z.
Review: COMMENTED
Reviewed — comments
The change adds opt-in NaN speculation and fallback handling; this review is incomplete because most changed files and the full translation path were not read.
What I ran
Read the prepared description, commit list, manifest, and selected hunks from
diff.patchfor headcd558bd4690cd606b37abab8a2f7f7f21deac1dc. No commands or tests were run.Results
Findings
Blocking
None identified in the portions inspected.
Non-blocking
InstructionTranslator.cpp, changed recompiler files, and changed tests need review before a formal approval decision.Not verified
The manifest lists 44 changed files. I read the description, commits, manifest, and selected diff hunks touching
Recompiler.cpp,InstructionTranslator.cpp, andNanFlagPruner.cpp; I did not read every changed file or all of those files in full.Recorder.cppandShaderMemory.cppwere excluded from the review folder because they were too large. No test or build results were independently verified; performance and gameplay results are author-reported.To get this to a good state
Nothing is required. Optional: complete the review of the remaining changed files and run the relevant tests before deciding whether to approve.