fix(shader): apply the f16 output modifier after rounding and the FP16_OVFL clamp #19605
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/19605"
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 Nawid3333 on 2026-11-13T15:44:53Z.
What
This builds on #18313 (+0 for a zero or denormal f16/f64 result under the output modifier), which is merged; this pull request is one commit on current
main.Where the VOP3 output modifier applies to an f16 result (IEEE mode off and bit 1 of the
FLOAT_MODEf16/f64 denormal field clear), it now scales the f16 result after it is rounded to f16 and clamped byFP16_OVFL, as RDNA2 does. Before,mul:2,mul:4anddiv:2scaled the unrounded f32 value, and rounding and theFP16_OVFLclamp came after. On gfx1036,v_mul_f160x7a00 · 0x3e00 (73728) withdiv:2gives 0x7c00 (0x77ff withFP16_OVFL); the translation gave 0x7880.writeF16,writeF16Conversion(SubwordPacking.cpp) andfloat16Unary(FloatInstructions.cpp) round first (theFP16_OVFLclamp, the tiny-result flush, thev_fract_f160x3bff cap and the NaN selection stay where they were), then apply the modifier and clamp on the f16 bits withclampF16Bits, as the min/max,v_div_fixup_f16,v_ldexp_f16andv_frexp_mant_f16paths already did. So the clamp runs on the f16 bits for every result of these three paths, also without a modifier, and a clamped NaN becomes 0 underDX10_CLAMPinclampF16Bitsinstead of infloat16Unary; the values don't change.scaleF16Result, bothapplyF16ResultModifiersoverloads andclampF16Resulthave no caller left and are removed.scaleF16Bits(OperandAccess.cpp): a finite result thatmul:2ormul:4takes past 65504 becomes ±0x7bff underFP16_OVFL(it gave infinity), and otherwise follows the f16 round mode: infinity when rounding to nearest even, 0x7bff toward zero, +infinity / -0x7bff toward +infinity, 0x7bff / -infinity toward -infinity.v_rcp_f16andv_exp_f16ignore the round mode on hardware, also for this overflow, so the seven transcendental instructions (v_rcp,v_rsq,v_sqrt,v_log,v_exp,v_sin,v_cos_f16) always use the round-to-nearest value (newclampF16Bitsargument); the other five can't reach the overflow (|sqrt| <= 256, |rsq| <= 4096, |log2| <= 24, |sin|, |cos| <= 1).checkRoundingUse(RoundingMode.cpp):v_min/max/min3/max3/med3_f16andv_floor/ceil/trunc/rndne_f16now read the f16 round bits when the modifier applies, so a shader whose paths reach them with different f16 round modes throws "reads MODE bits" instead of using one mode. The arithmetic,v_fract_f16,v_div_fixup_f16, conversion,v_ldexp_f16, interpolation, mixed and packed instructions already registered these bits; the seven transcendental instructions don't read them, andv_frexp_mant_f16's result (below 1.0) can't overflow.tests/execution/F16OutputModifierOrder.cpp: one kernel withv_mul_f16,v_add_f16,v_fma_f16,v_cvt_f16_f32,v_fract_f16,v_max_f16andv_rcp_f16, each with a modifier, run withFLOAT_MODE0x00 andFP16_OVFL0 and 1, and withFLOAT_MODE0x0c (f16 round toward zero). The 16 rows are the hardware results (Tested, item 3). The base fails it (item 4); thev_rcp_f16row checks the transcendental round-mode rule (the first mutation in item 4).core/libs/tests/VectorHalfDoubleOutputModifiers.cpp:v_add_f16,v_mul_f16andv_fma_f16with a modifier are now checked on the f16 bits (0x0400/0x0800 steps, the 0x0400 flush, the 0x7bff passthrough and the 0x3c00 clamp), asv_div_fixup_f16was.ShaderModeRegisters.cppadds the conflicting-round-mode diamond for the nine instructions above, with accepted controls (same mode on both paths, no modifier, IEEE mode on, f16 output denormals kept) and the eight instructions that don't read the round bits.Source: RDNA2 ISA §6.2.2 ("the result can apply an output modifier (OMOD) that multiplies the result by 0.5, 1.0, 2.0 or 4.0") and §3.5 (
FP16_OVFL) don't state the order against f16 rounding; the order is measured on RDNA2 (Tested).The fragment
docs/dev/debt/changes/fix-f16-omod-after-rounding-r2.mdchanges theshaderdebt entry on the output modifier: it drops "while the ordering against theFP16_OVFLclamp remains unmeasured", which this closes.Tested
Hardware: AMD Ryzen 5 7600X iGPU (RDNA2, gfx1036), AMD Windows driver 32.0.21045.16010, Windows 11 Pro build 26200, raw ISA through AnyPS5's
tools/hw-oracle(HIP runtime, wave32; called bytools/hw_oracle_igpu.pyin the evidence), IEEE 0, DX10_CLAMP 1, the f32 denormal field 0 and f32 round to nearest even. Items 1 and 2 compare the hardware with a model of the base translation and a model of this change; item 3 records the hardware values.Arithmetic, conversion,
v_fract_f16,v_max_f16(prb_expect.py): 12 kernel runs,FP16_OVFL0 and 1, the f16 round mode 0-3 forv_cvt_f16_f32: 2,880 values. This change matches all of them; the base differs in 198.The other paths (
prb_sweep2.py;v_fract_f16andv_fma_f16are also in item 1):v_rcp/sqrt/rsq/log/exp/floor/ceil/trunc/rndne/sin/cos/fract_f16,v_min/min3/max3/med3_f16,v_div_fixup_f16,v_ldexp_f16,v_fma_f16,v_fmac_f16(VOP3), omod 0-3 and clamp, near 65504, near 2^-14 and zero/denormal inputs; the f16 denormal field 0 and 1,FP16_OVFL0 and 1, the f16 round mode 0-3: 144 runs, 68,016 values. This change matches all of them; the base differs in 2,830. The transcendental round-mode rule comes from here:v_rcp_f16(0x0400)mul:4gives 0x7c00 with round toward zero,v_floor_f16(0x7bff)mul:2gives 0x7bff with round toward zero.v_mul_f16andv_max_f16withmul:2in all four round modes and bothFP16_OVFLvalues (omod_directed_check.py, 128 values) also match.The new test's rows (
prb_test_expect.py): the kernel ofF16OutputModifierOrder.cpprun in its three modes with its lane rows; every row in the test is the hardware result.ctest (CONTRIBUTING: same focused tests on the base and this change,
ctest -V,VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,ANYPS5_NO_SHADER_CACHE=1,ANYPS5_REQUIRE_VULKAN=1, both GPUs below, MinGW GCC Release). 85 tests: the 61 f16, output modifier and f16 conversion execution tests (68 ctest entries with their wave64 and SPIR-V variants) and 17 recompiler tests that translate f16 instructions (list in the evidence).agc_driver_float16_unused_nan_payloadsskips itself on both GPUs ("the device has no native f16 conversions with RTE rounding and kept f16 denormals"), so 84 run. Base = this change's parent (#18313's three commits) with this change's three test files.agc_driver_f16_output_modifier_order(v_mul_f160x7a00 · 0x3e00div:2gives 0x7880, expected 0x7c00),agc_shader_mode_register(the diamond is accepted),vector_half_double_output_modifiers(v_add_f16 mul:2emitted 0 step instructions, expected 1) failv_ldexp_f16) is written as +0No
VUID-message and no missing-layer message in any log. The 8 RX 9070 XT failures are the same on the base, line for line.Mutations: always passing the round mode to the transcendental instructions makes
agc_driver_f16_output_modifier_orderfail on both GPUs (v_rcp_f160x0400mul:4, round toward zero: 0x7bff, expected 0x7c00); droppingv_floor_f16fromcheckRoundingUsemakesagc_shader_mode_registerfail on both GPUs (outputModifierRounding: unsupported MODE operation was accepted: reads MODE bits). Every other result of each mutation is as with this change.Not measured:
v_interp_p2_f16with a modifier (the oracle has no attribute data; it takes the round-mode rule of the arithmetic instructions),DX10_CLAMP0, and the PS5's gfx1013.Scripts, raw hardware results and the ctest logs: https://github.com/Nawid3333/AnyPS5/releases/download/evidence-f16-omod-order/evidence-f16-omod-order.zip
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T04:01:16Z.
Review: COMMENTED
Reviewed — comments
The prepared diff supports the stated f16 modifier ordering change, but I did not read every changed file or run checks, so a formal approval is not justified.
What I ran
No commands or tests were run. I read the prepared description, commit list, manifest, and diff; the repository reader was unavailable through the supplied tools.
Results
Findings
Blocking
None verified.
Non-blocking
A complete review still needs inspection of every changed file and relevant callers, especially the translation changes and added execution tests.
Not verified
I read
description.md,commits.md,manifest.json, anddiff.patch. The manifest lists 11 changed repository files, all marked included, but I did not read their full text. No tests, build, hardware behavior, or CI results were independently verified; reported results in the PR description are author supplied.To get this to a good state
Nothing is required. Optional: complete the source review and run the focused checks before seeking a formal decision.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T00:12:43Z.
Review: APPROVED
Reviewed — looks good
The changed code consistently applies f16 output modifiers after conversion to f16 and uses round-mode-aware overflow handling; the draft evidence reports matching RDNA2 measurements and focused test results.
What I ran
Reviewed head
766c72537cdfcdcda96621b61117d044d6f7f625. Read the prepared description, commit summary, manifest, diff, and all nine changed files. No commands or tests were run in this review.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
I did not independently run the reported hardware measurements, ctest suite, or mutation checks. No files were excluded from the prepared review manifest.
To get this to a good state
Nothing is required.