Proposal: evaluate the packed f16 special-value rules only on lanes that can need them #4042
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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 queeeef on 2026-10-11T21:58:13Z.
Problem
Every packed f16 instruction applies RDNA's special-value rules on both halves:
0xfe00for an invalid operation (inf * 0,inf - inf);FP16_OVFL;These rules are exact and tested against hardware. They are also most of the instructions the recompiler emits for packed f16 math, although they only change a result when an input is NaN, or when the result is NaN or infinite.
IR values per instruction (
APS5_LOCAL_IR_HISTOGRAM, a local counter), for the two most expensive shaders of Palworld (PPSA23126) on its title screen:v_pk_fma_f16v_pk_fmac_f16v_pk_min_f16,v_pk_max_f16v_pk_add_f16,v_pk_mul_f16v_min3_f16,v_max3_f16With native f16 math (#3531, and a follow-up using
OpFmaKHRfromVK_KHR_shader_fmafor the fma) the arithmetic itself is one instruction. Almost everything left is the rules. #3531 already measured this on an RTX 4060: "Most of what's left in each instruction is the NaN rule on the result bits."Measurement
Setup:
APS5_PROFILE_GPU, shader cache off.Builds:
v_pk_fma_f16andv_pk_fmac_f16as native f16x2 math.packedFloat16is skipped (with the clamp inside it). This is not correct: it is only the upper bound of what the rules cost.0x1a01…per frame0x1bc1…per frameThese two compute shaders take 25 of the 35 ms of GPU time per frame. Without the rules they take 14 ms, and the frame rate goes up by 22% with every other cost unchanged. The scene is GPU-bound (93% busy at 1860 MHz), so the saving shows up directly as frame rate.
Proposal
Keep the rules exactly as they are, but run them only for the lanes that can need them.
For each packed f16 instruction:
FP16_OVFLis set and the result half is infinite". With SWAR this is a few instructions on the packed words, for example for the NaN part: A 15-bit magnitude above0x7c00sets bit 15 of its half, and no carry crosses into the other half.if (guard) { result = rules(result) }. A thread whose inputs and result are ordinary numbers never runs it.Why the result is bit-identical: the rules return the fast-path result unchanged whenever the guard is clear. The rule code is today's code, so the existing hardware-oracle tests cover it, and #3531's differential test already generates NaN, infinity, denormal and overflow inputs on every lane.
Expected cost per packed instruction: the arithmetic plus about 10 guard instructions, instead of 100–227 IR values today. Most of the upper-bound gain above should remain; the prototype will measure how much.
Pros
v_fma_f16,v_rcp_f16;nanResultF32.OpFmaKHR). Those remove the arithmetic cost; this removes most of what they leave.Cons and mitigations
OpPhiinside one IR instruction (SpirvBdaRead.cpp,SpirvImageEmitter.cpp), so the branch can live entirely in the backend.Implementation options
Select(cond, fast, slow)as rarely taken, and the backend sinks the values used only byslowinto a branch. This is more general, so the f32 rules would come for free, but it's a larger change to the backend.Plan
Questions
AI-assisted (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by queeeef on 2026-10-11T22:43:38Z.
The
OpFmaKHRfollow-up mentioned above is #4072. In an A/B on the same scene, it takes about 2 ms per frame off one of the two shaders and leaves the other unchanged. Most of what remains in both is the special-value rules this proposal is about.AI-assisted (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-11T23:28:41Z.
Option 1 is fine as long as the output stays bit-identical: the rule code is unchanged and only reached less often. The PR needs the before/after for that change alone (#218) and the test that forces every lane through the slow path.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-12T04:13:40Z.
Are you planning to prototype the option 1 backend lowering? If not, would you welcome another contributor carrying it forward, including the bit-identical slow-path test and before/after measurements requested above?
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-12T06:42:17Z.
I checked the current test setup: the existing packed ALU, f16 FMA and FP16 overflow execution tests build and pass under lavapipe (3/3), so I can add an all-lanes slow-path differential case here. This environment has no hardware GPU or Palworld capture, so I cannot supply the requested Palworld before/after measurement. Are you planning to provide that benchmark, or would you prefer the prototype wait until someone with the title/GPU setup can measure it?
Imported from boykopovar/AnyPS5. Originally posted by queeeef on 2026-10-12T13:39:34Z.
@MarcZX100 Go ahead, thanks. I'll provide the Palworld measurement.
APS5_PROFILE_GPU), plus presents per 10 s. The frame rate varies more between runs than these changes do, so runs alternate with and without.Ping me when there's a branch.
Two things that touch the same code:
v_pk_fma_f16andv_pk_fmac_f16withOpFmaKHR, and leaves the NaN,FP16_OVFLand clamp rules on its result bits unchanged. The guard should cover that native fma path too, so both fma paths reach the rules the same way.I had only started a local sketch of option 2, so there's nothing to hand over.
AI-assisted (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-12T20:27:35Z.
The first reviewable branch is ready:
codex/f16-special-rules-4042-current, based on current upstream825620ae5.It adds a guarded packed-f16 special-value repair path and an all-lanes differential case against the existing rule expansion. On Linux with lavapipe, the Release build succeeded and these focused tests passed:
agc_driver_packed_float16_native,agc_driver_packed_alu,agc_driver_float16_fma_rounding, andagc_driver_fp16_overflow.tools/check_conventions.py --base upstream/mainandgit diff --checkpass.Could you run the proposed alternating Palworld A/B on this branch against its base and share the two shader GPU times plus presents/10 s? The code does not add a slow-path counter yet. I’m coordinating the overlapping #4072 FMA change before opening the PR; after its merge/rebase I’ll rerun the same checks on the final stack.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-13T10:22:34Z.
Following up on the alternating Palworld A/B you offered: #5057 is now open at
68ee4015b567953f98866d1433a2121717031b49with the guarded repair path and all-lanes differential test. Could you run the proposed comparison against its base and share the two shader GPU times plus presents per 10 s? This head retains the current f32 FMA lowering; #4072 remains a separate open change, so the result would measure this guard alone. No hardware-GPU performance claim is made until then.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T00:04:23Z.
Closing the loop on option 1: #5057 implemented the guarded path and an all-lanes differential test. The reported alternating Palworld A/B showed no measurable improvement in that scene, while shader 1 grew by about 17% in SPIR-V; the PR was closed pending evidence of a repeatable gain. The separate operand-reuse direction did land as #5811 (merged as
066b58f), reducing duplicateop_selrepacking without changing the repair rules. #5057 does not establish a general performance gain. A future guard proposal would need a workload showing a repeatable win and accounting for pipeline creation cost.AI-assisted: yes.