perf(shader): round v_cvt_pknorm and v_cvt_pk_u8 in f32 #2410
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2410"
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 karanvora2599 on 2026-10-09T18:29:08Z.
What
v_cvt_pknorm_i16_f32,v_cvt_pknorm_u16_f32andv_cvt_pk_u8_f32round f32 values to integers to nearest even. The norm conversions did that with 64-bit integer arithmetic andv_cvt_pk_u8_f32with a hand-built fraction test; both are now computed with f32 operations that give the same bits. On an RTX 4060 the norm conversions drop from 52-53.5 to 28.5-29.5 instructions andv_cvt_pk_u8_f32from 16.5 to 8.5.Background
v_cvt_pknorm_u16_f32 d, a, bpacksround(clamp(a, 0, 1) * 65535)and the same ofbinto two 16-bit halves; thei16form usesclamp(x, -1, 1) * 32767. Rounding is to nearest even on the exact product, NaN gives 0, f32 denormal inputs are flushed.v_cvt_pk_u8_f32 d, x, i, sroundsxto nearest even, clamped to 0..255 (NaN gives 0), and inserts it as bytei & 3ofs.Before
normF32(Translation/src/FloatInstructions.cpp, #596) formed the exact productsignificand * scaleas a 64-bit integer (IMul64), shifted it right by149 - exponent(a variable 64-bit shift), recovered the sticky bit with a second variable 64-bit shift and a 64-bit compare, and rounded with the parity of the truncated result. On NVIDIA each 64-bit multiply, variable shift and compare is several instructions.vCvtPkU8F32(Translation/src/ConvertInstructions.cpp) truncated with the saturating conversion, converted the integer back to f32, subtracted it to get the fraction, and compared the fraction with 0.5 and the integer's parity to decide the rounding.After
|x| < 1:p, the f32 nearest to|x| * scale, and its exact errore, from correctly rounded subtractions only.scale + 1(65536 or 32768) is a power of two, soA = |x| * (scale + 1)is exact and the exact product isA - |x|. SinceA >= |x|,p = A - |x|ande = (A - p) - |x|is Fast2Sum:eis exactlyA - |x| - p.efromfma(|x|, scale, -p). Vulkan gives GLSLFmaon f32 only the precision ofOpFMulfollowed byOpFAdd, and lavapipe in Mesa 25.2.8 (CI's) splits it, soewas 0 there (see the review).pis the f32 nearest to the exact product, so no half-integer lies strictly between them;roundEven(p)is therefore the right answer unlesspis itself exactlyk + 0.5. Forp < 2^23(p <= 65535here) everyk + 0.5is exactly representable in f32, so that case is recognised asp - floor(p) == 0.5.p = k + 0.5the exact product isp + e:e > 0rounds up tok + 1,e < 0down tok, ande == 0is a true tie thatroundEvenresolves.eonly matters whenp >= 0.5; then|x| >= 0.5 / 65535, so the exact product is a multiple of at least2^-40andeis either 0 or a normal number: a driver that flushes denormals can't change it.|x| >= 1and the sign keep their previous selects. The lanes that select a constant multiply 0 instead of|x|, so the conversion to integer only ever sees values in range.v_cvt_pk_u8_f32: NaN andx <= 0give 0,x >= 255gives 255, anything else isroundEven(x)converted to an integer. The source is read without the f32 denormal flush, which can't change a value rounded to an integer. The byte insertion is unchanged.All the operations used (
FPMul32,FPSub32,FPFloor32,FPRoundEven32, the compares) lower to single native SPIR-V instructions, and the multiply and subtractions carryNoContraction.Where it matters
v_cvt_pknorm_*packs 16-bit normalized values (for example for 16-bit unorm/snorm outputs and packed normals),v_cvt_pk_u8_f32builds RGBA8 words in compute shaders. Only an Ada GPU (sm_89) was measured.Tested
Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs r7), Release,
ANYPS5_ENABLE_SPIRV_TOOLS=ON, the branch merged withmainat1c75f4d2. GPU tests on an NVIDIA GeForce RTX 4060 Laptop GPU (driver 591.86, subgroup 32), an Intel UHD Graphics (Raptor Lake, driver 101.6790), and lavapipe from Mesa 25.2.8 (the version CI installs) and Mesa 26.2.4, both the Windows builds, withGALLIUM_OVERRIDE_CPU_CAPS=avx.Correctness
New
agc_driver_f32_pack_rounding: 563,232 inputs through all three instructions in one dispatch (FLOAT_MODE0xc0):k, the f32 nearest to(k + 0.5) / 65535and to(k + 0.5) / 32767, and the two floats on each side of it, which is where the product rounds to or next to a half-integer;k + 0.5fork < 256(forv_cvt_pk_u8_f32);Expected values come from the exact product in double (24 × 16 significant bits, so exact) rounded to nearest even on the host, with the saturation, sign and NaN rules of the bit-level version. It passes on all four devices, and so does the previous bit-level implementation of
main, which checks the expectations against the code that was measured on hardware.Negative checks, each fails the new test on the RTX 4060 and on lavapipe:
|x| * scale - p, what a splitfmagives):f32 pack rounding: v_cvt_pknorm_u16_f32 of 0x37c000c0 is 0x00020002, expected 0x00010001. Thefmaversion of this PR failed the same way on lavapipe 25.2.8;roundEven(p)):f32 pack rounding: v_cvt_pknorm_u16_f32 of 0x37c000c0 is 0x00020002, expected 0x00010001(the product rounds to exactly 1.5 but the exact value is below it);v_cvt_pk_u8_f32rounding halves up (floor(x + 0.5)):f32 pack rounding: v_cvt_pk_u8_f32 of 0x3f000000 is 0x00000001, expected 0x00000000.agc_driver_vop3_pack_ldexp(v_cvt_pknorm_*rows computed on the bits as measured on RDNA2) andagc_driver_vop3_legacy_source_modifiers(v_cvt_pk_u8_f32with every source modifier) pass on all four devices.Every shader test (
agc_driver_*,agc_shader_*,agc_spirv_*,vector_*,agc_masked_select,agc_validate_program,agc_denormal*,agc_float16_*; 320 tests):agc_driver_flat_loads*,agc_driver_global_loads*,agc_driver_flat_apertures_2,agc_driver_flat_apertures_4(then0xc0000409) andagc_driver_pixel_interlock. None of them runs a pack conversion.agc_driver_image_plane_address_on_line_implicit_lod(pixel 20,0 component 0 is 170, expected 10). It fails the same way withmain's recompiler on lavapipe 25.2.8 and 26.2.4.Instructions per op (RTX 4060, not committed)
Method: a wave32 compute program with a dependent chain of 16
v_xor_b32+<op>steps (the xor keeps the compiler from folding the chain), sources other than the chain in registers, recompiled withFLOAT_MODE0xc0 and created withAPS5_PIPELINE_STATS=1; "Binary Size" / 16 bytes per instruction, compared with the same chain ofv_add_nc_u32steps.main(1c75f4d2)v_cvt_pknorm_u16_f32v_cvt_pknorm_i16_f32v_cvt_pk_u8_f32(byte index in a register)All other opcodes in the sweep compile to the same size. The exact subtractions cost 1.5 and 1 instructions more than the earlier
fmaversion (27 and 28.5). Throughptxas(CUDA 12.4), the same sequence is 2 instructions longer than thefmaone on sm_61, sm_75, sm_86 and sm_89 alike:FMULand threeFADDinstead ofFMULandFFMA.The measurement programs aren't part of this PR; I can attach them if useful.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-09T18:29:18Z.
Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T08:19:17Z.
Review: CHANGES_REQUESTED
Ran this on the CI lavapipe (Mesa 25.2.8, the one in the "Install lavapipe" step) and the new test fails there, while it passes on RADV and on a current lavapipe:
Translation/src/FloatInstructions.cpp:310: the tie correction relies one = fma(|x|, scale, -p)being the exact error of the product.FPFma32is emitted as GLSLFma(SpirvAluEmitterMath.cpp:1174), and for f32 the Vulkan spec (spirvenv, precision table) only guarantees the precision ofOpFMulfollowed byOpFAdd, so a driver may split it. Thene == 0for every input and a product that rounds up tok + 0.5is resolved byroundEveninstead of by the sign of the real error.NoContractiondoes not prevent splitting an explicitFma. Same issue as #446.|x|andscale, asEmitFPFma64does for f64), or keep the 64-bit integer path. The test you added already covers it; please also run it with the CI lavapipe, since the PR CI doesn't build.v_cvt_pk_u8_f32(ConvertInstructions.cpp:214-222) uses only exact ops and passes on both.The instruction counts (RTX 4060) could not be checked here; they stay as your measurement.
Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-11T09:59:07Z.
Thanks for running it on CI's lavapipe. I reproduced it with the Windows build of Mesa 25.2.8 (
GALLIUM_OVERRIDE_CPU_CAPS=avx): the samev_cvt_pknorm_u16_f32 of 0x37c000c0 is 0x00020002, expected 0x00010001. In that version llvmpipe emits every NIRffmaasllvm.fmuladd(lp_bld_nir_soa.c), which LLVM splits into a multiply and an add when FMA is off. Mesa 26.x does that only forffma_weak, which is why my lavapipe passed.Fixed in
17267336withoutFma.scale + 1(65536 or 32768) is a power of two, soA = |x| * (scale + 1)is exact and the exact product isA - |x|. SinceA >= |x|,p = A - |x|ande = (A - p) - |x|is Fast2Sum:eis the exact error ofp, from correctly rounded subtractions only.agc_driver_f32_pack_rounding,agc_driver_vop3_pack_ldexpandagc_driver_vop3_legacy_source_modifierspass on lavapipe 25.2.8 and 26.2.4, the RTX 4060 and an Intel UHD.|x| * scale - p, which is what the splitfmacomputed) makes the new test fail on the RTX 4060 and on lavapipe 26.2.4 with the same line, so it catches this on a driver that fuses too.agc_driver_image_plane_address_on_line_implicit_lodon lavapipe, which fails the same way withmain's recompiler.fmaversion on the RTX 4060. Against currentmain,v_cvt_pknorm_u16_f32goes from 52 to 28.5 andv_cvt_pknorm_i16_f32from 53.5 to 29.5.I also merged current
main(1c75f4d2) and updated the description.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T10:16:05Z.
Review: APPROVED
Re-checked on the current head:
agc_driver_f32_pack_rounding(563,232 inputs) passes on RADV and on CI's lavapipe (Mesa 25.2.8), as doagc_driver_vop3_pack_ldexpandagc_driver_vop3_legacy_source_modifiers. Mutating the tie correction and thev_cvt_pk_u8_f32rounding (floor(x + 0.5)) both fail the new test. The Fast2Sum error term no longer depends onFma. Merging.