fix(shader): apply the f32 output modifier and denormal flush to v_cvt_f32_f64 #5159
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5159"
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 nikowolf123 on 2026-10-13T13:12:30Z.
What
v_cvt_f32_f64writes an f32 result, but the recompiler treated that result like an f64 result in two places.float64Operationchecked the VOP3mul:2,mul:4anddiv:2against the f16/f64 denormal field and then cleared it. In the titles' float mode (IEEE=0,FLOAT_MODE0xc0, which is also how shaders without a known mode are translated) the modifier was silently dropped. With f16/f64 denormals flushed (for example 0x00) the translation threw. The hardware applies the modifier in both modes. Example:v_cvt_f32_f64_e64 v22, v[4:5] mul:2of 1.0 gives 0x40000000 on gfx1036 in the titles' mode, andmaingave 0x3f800000.f32DenormalFlushForhad no case forv_cvt_f32_f64, so a result below 2^-126 was whatever the host returned. In the f32 flush modes, 2^-130 must give +0. On lavapipemaingave 0x00080000.Sources:
tools/hw-oracle. The data is in gist SleglJan/30456e5687240f0b639a78e5477f2f91 (revision 1d26d816),audit.json, F64Rounding column 12 (v_cvt_f32_f64_e64 v22, v[4:5] mul:2). In all 28 finite rows the modifier is applied in the titles' mode (IEEE=0, f32 denormals flushed, f16/f64 kept). It is also applied with f16/f64 denormals flushed. It is ignored with IEEE=1 or with f32 denormals kept. The ±inf and NaN rows are the same in every mode. I checked all 28 cells against the model this change implements (round, flush, then raise the exponent by one) and found no mismatch.FP_DENORMbits [5:4] are the single-precision mode and bits [7:6] the double/half-precision mode. Mode 0 flushes input and output denormals, mode 1 flushes output only, and mode 2 flushes input only.V_CVT_F32_F64, "D.f = (float)S0.d", and denormals are supported.llvm/lib/Target/AMDGPU/VOP1Instructions.td:281-282definesV_CVT_F32_F64withVOP_F32_F64, which has an f32 destination (SIInstrInfo.td:3137).SIInstrInfo.td:2903(HasOMod = !if(IsVOP3P, 0, DstVT.isFP)) gives it an omod operand.SIFoldOperands.cpp:2491-2509:isOModtreats aV_MUL_F32by 2, 4 or 0.5 as an output modifier only when f32 output denormals arePreserveSign.SIFoldOperands.cpp:2625-2687:tryFoldOModfolds that multiply into the instruction that defines its operand when that instruction has an unset omod.SIFoldOperands.cpp:3181-3185and3236-3239: it does not fold with IEEE mode on.fptruncfollowed by a multiply by 2.0 compiles tov_cvt_f32_f64 ... mul:2under the f32 flush mode, whatever the f64 mode.llvm/lib/Target/AMDGPU/SIISelLowering.cpp:16312-16324:isCanonicalizedlistsISD::FP_ROUNDunder "These will flush denorms if required."llvm/test/CodeGen/AMDGPU/fcanonicalize-elimination.ll:237-251(test_fold_canonicalize_fpround_value_f32_f64, RUN lines 1-4 include-denormal-fp-math-f32=preserve-sign) expects nov_mul/v_maxcanonicalize afterv_cvt_f32_f64_e32.src/amd/compiler/aco_optimizer.cpp:3905-3980(apply_output_mul, called forv_mul_f32at 3995-4001) takes the denormal mode from the result type:denorm32for an f32 result (3913). It folds only underfp_denorm_flushand when signed zeros need not be preserved (3954-3956), and only into a parent with output modifiers (3960).aco_opcodes.py:1040defines("v_cvt_f32_f64", dst(F32), ...).F32hasmods=True(aco_opcodes.py:260), whichaco_opcodes_cpp.py:81turns intooutput_modifiers.aco_optimizer.cpp:1658-1717(gather_canonicalized) marks every float VALU result, this one included, as canonicalized. Lines 2198-2204 then turn a canonicalize of such a value into a plain move, so withdenorm32at flush ACO relies on the conversion having flushed its result.What changed:
Float64Instructions.cpp,float64Operation: the f32 result (onlyConvertF32F64has one) is written with its modifiers.writeOperandthen applies the existing f32 result rule:applyF32ResultModifiers, withoutputModifierAppliesreadingFLOAT_MODEbits 5:4 and IEEE. The f16/f64 refusal stays only where the f32 rule ignores the modifier but the f16/f64 rule would apply it. That is f32 output denormals kept with f16/f64 flushed at IEEE=0, for example 0x30. Nobody has measured that mode, so translating it still throws. U32 and U64 results are unchanged.VectorInstructions.cpp,f32DenormalFlushFor:VCvtF32F64joins the group that flushes inputs and result (flush = 3u). The source is read withreadF64, so only the result flush takes effect. As for every instruction in that group, f32 denormal modes 1 and 2 now throw, because they are not measured. Before, mode 1 was translated without the output flush the ISA describes for it, and mode 2, which keeps output denormals, needed no flush and was translated.docs/dev/debt/shader.md: the f32 denormals entry and the output modifier entry now say which parts are measured, which come from the sources, and which are not measured. The unmeasured parts are scaling before rounding near the f32 denormal and overflow boundaries,mul:4anddiv:2, mode 0x30, and tininess detection just below 2^-126.F64Rounding.cpp: column 12 (cvt f32, themul:2instruction above) runs with no float mode, that is, in the titles' mode. Its 28 finite cells held the value without the modifier, which is what the hardware returns with IEEE=1 or with f32 denormals kept. They now hold the measured titles'-mode value fromaudit.json, which is the old value times 2. Row: old → new:Rows 3, 5, 6 and 8 (±inf, NaN) do not change, as measured.
New test
VCvtF32F64ResultModes.cpp(agc_driver_vcvt_f32_f64_result_modes): 32 f64 sources over 64 lanes, run in wave32 and wave64. Each source is converted plain and withmul:2,mul:4anddiv:2.What each row rests on:
mul:2of finite values, ±inf and NaN matches the #981 measurement. 1.0, -2.5, -2^-60 and 0x4153959f_c5f99476 are rows of the measured kernel.mul:4anddiv:2of normal values are exact multiplications by 4 and 0.5 (section 6.2.2).div:2give the same result whether scaling comes before or after rounding. So none of these rows depends on the unmeasured order.No title is known to use this encoding.
Tested
Linux (WSL2 Ubuntu), GCC, Release, configured like CI (
-DBUILD_TESTING=ON -DANYPS5_ENABLE_SPIRV_TOOLS=ON). Tests ran on lavapipe (llvmpipe, LLVM 21.1.8) withVK_ICD_FILENAMES=<lvp_icd>.json ANYPS5_REQUIRE_VULKAN=1 GALLIUM_OVERRIDE_CPU_CAPS=avx.agc_driver_vcvt_f32_f64_result_modes: "wave32 no float mode lane 0 v_cvt_f32_f64 mul:2 is 0x3f800000, expected 0x40000000"agc_driver_f64_rounding: "lane 0 cvt f32 is 0xa1800000, expected 0xa2000000"VCvtF32F64removed fromf32DenormalFlushFor: "lane 24 v_cvt_f32_f64 is 0x00080000, expected 0x00000000"mul:2ctest -j4 -R 'agc_shader|agc_driver|shader|recompiler': 384 tests, none fail, 36 skip on lavapipe. Unmodifiedmainin the same tree runs 383 tests with none failing and the same 36 skipped. The only per-test difference is the new test.ctest -j4: 1050 tests, none fail, 43 skip (the 36 above and 7 video out tests). In the run on unmodifiedmain(1049 tests),guest_video_out_wqhd_detectionfailed andguest_video_out_output_modeandguest_video_out_resolution_statuswere skipped. All three pass when rerun onmain's recompiler and with this change, so there is no per-test regression.tools/check_conventions.py --base origin/main(main's checker) andgit diff --check: clean.This was not measured on AMD hardware for this PR. Only the #981 data above was. A gfx103x reviewer can check every row of the new test with the oracle, from the repository root:
What each mode should print:
Expectedrows.Expectedin all four outputs.Kept.These extra rows settle the unmeasured items in the debt entries:
Run
edges.txtwith the same loop. The rows are, in order:mul:2of 1.5 * 2^-127 andmul:4of 1.5 * 2^-128 (scaling before or after the flush)div:2div:2of a value that rounds to infinityNative Windows 11, WinLibs GCC 15.2.0, NVIDIA GeForce RTX 5080 (driver 616.92), on this branch rebased onto
main0f2683cb7(d176ede7e):agc_driver_vcvt_f32_f64_result_modesandagc_driver_f64_roundingpass 3 of 3 runs, andctest -R 'agc_shader|agc_driver|shader|recompiler'passes 383 of 383.Rebased onto
mainfdefd7660. The only conflict wasdocs/dev/debt/shader.md, wheremainadded the patch-constants entry above the output-modifier entry this PR edits; both are kept. The code change is unchanged. WSL2 Ubuntu 26.04, lavapipe (Mesa 26.0.8), CI configuration:agc_driver_vcvt_f32_f64_result_modesandagc_driver_f64_roundingpass, andctest -R 'agc_shader|agc_driver|shader|recompiler'passes 390 of 390.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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-13T16:02:47Z.
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.
main)docs/dev/debt/shader.mdL90-91docs/dev/debt/shader.mdL90-91docs/dev/debt/shader.mdL90-91docs/dev/debt/shader.mdL90-91docs/dev/debt/shader.mdL90-91Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:31:32Z.
Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.
Pull request closed