Skip to content

fix(shader): apply the f32 output modifier on the bits like the hardware - #567

Merged
oneandonlydean merged 5 commits into
boykopovar:mainfrom
adriadam10:fix/f32-omod-zero
Oct 5, 2026
Merged

oneandonlydean merged 5 commits into
boykopovar:mainfrom
adriadam10:fix/f32-omod-zero

Conversation

@adriadam10

@adriadam10 adriadam10 commented Oct 4, 2026 •

Copy link
Copy Markdown
Collaborator

What

The VOP3 output modifier (mul:2, mul:4, div:2) on f32 results was a plain FMul by 2, 4 or 0.5. That differs from the hardware in three places, and the NaN result was left to the driver:

  • a zero result becomes +0 (main kept -0, e.g. v_mul_f32_e64 v, -0, 1.0 mul:2);
  • with div:2, a result that drops below the smallest normal becomes a zero with the sign of the result (0x80800000 → 0x80000000, and 0x00ffffff → +0 where round to nearest gives 0x00800000);
  • a NaN result passes through unchanged, signaling NaNs included. FMul on a NaN lets the driver choose the payload (NVIDIA returns 0x7fffffff).

applyF32ResultModifiers now picks these cases on the bits of the unmodified result. Other results still go through FMul, which is exact for these factors, overflow to infinity included. Clamp is still applied after the output modifier.

f16 results follow #323: applyF16ResultModifiers ignores omod, as the hardware does while f16 denormals are enabled (the LLVM default), so this PR only changes the f32 path. I added a line to TechnicalDebt for the mode assumption of the f32 rule and for the f16 case with denormals flushed, which isn't modelled. packedFloat16 (VOP3P) has no output modifier field, so its call sites don't change.

Behaviour measured on HW

The measurements were taken on a gfx1035 GPU through the HSA runtime, with MODE set to IEEE=0 and f32 denormals flushed. That is the mode the recompiler already assumes for f32 (the LLVM graphics default). With IEEE=1, or with f32 output denormals on, the hardware ignores the output modifier.

  • 3072 rows, each run through v_mul/add/fma/min/max/fract/cvt_f32_i32 and each of the three modifiers. Every cell matches the rule above when it is applied to the hardware's own unmodified result:
    • NaN: unchanged;
    • magnitude below 0x00800000: +0;
    • div:2 with magnitude below 0x01000000: signed zero;
    • otherwise the f32 product.
  • The modifier is applied after the operation has rounded, so double rounding is possible: v_mul_f32 2.0, 0xff61b1e6 div:2 overflows to -inf and stays -inf.

Tested

  • New agc_driver_f32_output_modifier test (tests/execution/F32OutputModifier.cpp): 16 forms (mul, add, sub, fma, min, max, floor, cvt_f32_i32 with each modifier, and clamp after the modifier), with 32 rows of expected values from the hardware. A cell is checked only when AnyPS5's result without the modifier is defined on every driver: no subnormal inputs or results, and fma only where the product is exact. For max with two identical NaN sources the NaN bits are chosen by our code, so those cells are checked bit for bit (sNaN included). For add/mul/fma NaN cells the driver chooses the NaN, so they only require a NaN.
  • The test fails without the change ("lane 0 v_mul_f32_e64 v10, v4, v5 mul:2 is 0x80000000, expected 0x00000000") on RADV and on lavapipe. Each select was mutated separately and each mutation fails the test. The NaN select fails on lavapipe. The div:2 underflow select passes both local drivers when it is removed, because they flush the same way, so I checked that the rows reach that branch by changing its value.
  • Passes on RADV (Mesa 26.2), local lavapipe and CI's lavapipe (Mesa 25.2.8, cold run under 1 s). Full Release ctest on Linux: everything passes except agc_driver_pixel_helper_lanes, agc_driver_global_load_addtid and agc_driver_vop3_carry_out, which also fail on main on this RADV (wave32 lane 32 on RADV's 64-wide subgroup).

Checklist

@github-actions github-actions Bot added shader core/shader docs agc libSceAgc* labels Oct 4, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thanks, this is the follow-up from #508. The rule reads right to me: the picks are made on the bits of the unmodified result, the div:2 underflow select runs before the general "below 0x00800000 gives +0" select (so a zero input still gives +0), NaN is kept as is, consistent with IEEE=0 as in #508, and everything else goes through an FMul that is exact for these factors. Clamp after the modifier matches the ISA's order, and the TechnicalDebt line states the mode assumption.

The one thing to sort out is the order with #323 and #537, which change the same three files:

agc_driver_f32_output_modifier passes on my NVIDIA machine at this head, and on main's code it fails at lane 0 (v_mul_f32 ... mul:2 gives 0x80000000 where 0x00000000 is expected).

Output modifiers are on PPSA21564's shader path, so once it's rebased I'll run it through my PPSA21564 check before merging.

@github-actions github-actions Bot mentioned this pull request Oct 4, 2026
9 tasks done
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 4, 2026
@adriadam10

Copy link
Copy Markdown
Collaborator Author

Rebased on current main: the only conflict was the neighbouring TechnicalDebt line (now sceAjmDecMp3ParseFrame); the omod line is unchanged. agc_driver_f32_output_modifier_tests passes on RADV and lavapipe, full ctest green on RADV apart from the known agc_driver_pixel_helper_lanes.

adriadam10 and others added 4 commits October 5, 2026 03:38
writeF16 dropped the clamp bit, so VOP3/SDWA f16 results with clamp
(v_rcp_f16, v_fma_f16, v_cvt_f16_f32, v_add_f16_sdwa, ...) were not
saturated. Clamp now limits to [0, 1] with NaN and -0 going to +0.
The f16 sqrt/rsq/log and sin/cos paths applied omod, which the
hardware ignores on f16 results in the default float mode, and
v_cvt_pkrtz_f16_f32 applied clamp and omod to its inputs, which the
hardware ignores as well.

Co-Authored-By: Claude <noreply@anthropic.com>
…yload

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Claude <noreply@anthropic.com>
…A does

v_ceil/floor/trunc/rndne/fract/sqrt/rsq/rcp/rcp_iflag/log/exp/sin/cos
(f32 and f16) and v_frexp_mant_f32 left NaN, invalid, zero and infinite
results to the driver: signalling NaNs came back unquieted, lavapipe lost
the NaN sign and payload, invalid results were +NaN instead of -NaN, f32
sin/cos at whole and half cycles returned tiny values instead of +0, and
v_fract_f16 of a tiny negative rounded up to 1.0.

Adds an execution test for the 27 opcodes with expected values from
hardware; approximate ops are compared with tolerances from the Vulkan
precision table.

Co-Authored-By: Claude <noreply@anthropic.com>
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 5, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thanks for the rebase. The TechnicalDebt conflict is resolved the right way, and the commit is otherwise the same as before.

One thing I noticed while merging the open shader PRs together: this now conflicts with #323 (and so with #537, which carries #323's commits) in TranslationContext.hpp, FloatInstructions.cpp and ConvertInstructions.cpp, and it isn't only textual. #323 adds applyF16ResultModifiers, which applies clamp but not omod to f16 results, while this PR adds a float16Result flag to applyF32ResultModifiers and your TechnicalDebt line says omod on f16 results "is still a plain multiply". Whichever lands second has to pick one rule for f16 (and the TechnicalDebt line should match it). Since #323 and #537 are ahead of this one, could you rebase it onto #537 and keep #323's f16 handling?

The f32 part is unchanged and still looks right to me. PPSA21564 uses mul:2, mul:4 and div:2 in about 2,500 of its shaders, so this one goes through my PPSA21564 check before merging.

@adriadam10

Copy link
Copy Markdown
Collaborator Author

Rebased onto #537 (carries #323's commits unchanged) and kept #323's f16 rule:

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thanks, this is what I was after. The branch is now #537's head (f3b74e2, with #323's three commits unchanged) plus only this commit, and the range-diff against the previous version shows just the f16 parts gone: no float16Result flag, the f16 unary and trig paths and v_cvt_pkrtz_f16_f32 keep #323's handling (applyF16ResultModifiers, unmodified inputs for pkrtz), and the f32 bit rule is the same as before. The TechnicalDebt line now matches #323's rule.

#323, then #537, then this one merges cleanly at every step here, and the full ctest passes on main with the stack merged (agc_driver_f32_output_modifier, agc_driver_float16_result_modifiers and agc_driver_vop1_float_unary included).

As before, PPSA21564 uses mul:2, div:2 and mul:4 in about 2,500 of the 5,688 shaders I have dumped, so the stack goes through my PPSA21564 check before merging. Ready to merge from my side after #537 once that's done.

Gated on PPSA21564 on top of #537: no visual or FPS change.

@oneandonlydean
oneandonlydean merged commit 0087fb6 into boykopovar:main Oct 5, 2026
3 checks passed
@adriadam10
adriadam10 deleted the fix/f32-omod-zero branch October 5, 2026 06:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* docs shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants