Repository navigation
fix(shader): choose unary f32/f16 special results on the bits, as RDNA does - #537
Conversation
|
Thanks, the NaN, invalid, zero and inf picks match the ISA functional examples, and reading the f32 source as bits (so neg on a NaN keeps its sign) is a nice fix. Two things before it can go in:
|
04d8193 to
dd9431a
Compare
|
Thanks, both done (rebased on current main, last commit amended):
While rerunning the oracle I also ran IEEE=0: everything matches except signaling NaNs, which come back unquieted there (113 cells). The PR keeps the IEEE=1 quieting, as |
|
Thanks, both points are fixed.
One small request, not blocking: the description says the sNaN quieting is the IEEE=1 result and that IEEE=0 leaves 113 cells unquieted. Could that go into TechnicalDebt as a line like the one on #501? Main's unary f32 ops currently pass a signalling NaN through unquieted on RADV, so this is a change in behaviour that the recompiler can't check against the mode, and #508 went the other way (NaN unchanged, IEEE=0). A line makes the split visible until MODE is read. This lands after #323. Since it changes every unary float op PPSA21564 uses, I'll run it through my PPSA21564 check together with #323 and merge both if it's clean. |
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>
dd9431a to
f3b74e2
Compare
|
Rebased on current main: the only conflict was the neighbouring TechnicalDebt line ( |
|
Thanks for the rebase. #323's three commits are patch-identical, the unary commit only changed in context, and the TechnicalDebt line is unchanged next to main's new One merge-order detail: #323's own branch is still at cc69286 (the old base), while this branch carries rebased copies of those commits. If #323 is merged first as it stands, this PR then conflicts in
|
|
Thanks. #323's branch is at 6686bff now, so this branch is #323 plus one commit, and #323, then this one, then #567 merges cleanly at every step. The full ctest passes here with the three merged. These go through my PPSA21564 check before merging ( Gated on PPSA21564 on top of #323, with #567: no visual or FPS change. |
The unary f32/f16 ops pick their special results on the source bits since boykopovar#537. The emitted checks cost about 39 KB of NVIDIA code in PPSA21564's compute program 0x555f41f00. This keeps every result and does less work: - The IR now pins every result the backend's denormal flush could change (rcp, rsq, sqrt, log and exp of an f32 denormal), so the flush in front of FDiv, InverseSqrt, Sqrt, Exp2 and Log2 is dropped. The FragCoord.w reciprocal, the only other FPRecip32 user, flushes in the IR instead. - f32 NaN sources are found with FPIsNan32. Vulkan gives OpFUnordNotEqual a correct result and the module declares SignedZeroInfNanPreserve. - The invalid result and the NaN result are two plain selects in a row, with no LogicalOr and no extra select between the two specials. - negative and nonzero is one compare (bits > 0x807fffff); exp overflow is one signed compare and exp underflow one unsigned compare. - fract's clamp is UMin32. - sin no longer keeps the signed zero in the backend (the IR pins it), and sin/cos reduce with Fract alone: Fract is correctly rounded, so it is already 0 for |x| >= 2^23, and inf and NaN are pinned in the IR.
…esults never use The SPIR-V emitters of FPRecip32, FPRecipSqrt32, FPSqrt, FPExp2 and FPLog2 flushed a denormal input to its signed zero before the host operation. Since #537, unaryFloatSpecials replaces the host result for every input below 2^-126 with the hardware's result for a zero of the same sign: rcp and rsq give a signed infinity, sqrt the signed zero, log -inf and exp 1.0. floatUnary and float16Unary both go through it, so the flushed input could only change a value that is then discarded. v_rcp_iflag_f32 gets the same special values and already went without the flush. The pixel prologue computes PositionW as FPRecip32 of FragCoord.w without those special values, so it now flushes FragCoord.w in the IR first and gives the same result as before.
…esults never use The SPIR-V emitters of FPRecip32, FPRecipSqrt32, FPSqrt, FPExp2 and FPLog2 flushed a denormal input to its signed zero before the host operation. Since #537, unaryFloatSpecials replaces the host result for every input below 2^-126 with the hardware's result for a zero of the same sign: rcp and rsq give a signed infinity, sqrt the signed zero, log -inf and exp 1.0. floatUnary and float16Unary both go through it, so the flushed input could only change a value that is then discarded. v_rcp_iflag_f32 gets the same special values and already went without the flush. The pixel prologue computes PositionW as FPRecip32 of FragCoord.w without those special values, so it now flushes FragCoord.w in the IR first and gives the same result as before. (cherry picked from commit 1dc99ba) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* perf(shader): search a bindless image table once per access EmitImage selects the table slot of a bindless image before it branches over the image's runtime modes, and EmitImageMode then searched the table again in every mode arm. Each search is a binary search of dependent flattened_srt loads, so every bindless sample ran two. The mode arms now take the selection EmitImage already made; the modes copy the base image, so the mapping offset, root and iteration count the search reads are the same. (cherry picked from commit d759a43) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(agc,shader): prime the two host shared mutexes added after 48dcaac Commit 48dcaac introduced PrimedSharedMutex and converted every host std::shared_mutex that existed at the time, because winpthreads initializes a static rwlock on first use and a concurrent first lock returns EINVAL, which std::shared_mutex ignores (docs/dev/TechnicalDebt.md:10). Two host mutexes were added afterward and missed the conversion: - Recompiler.cpp registrationModule() static mutex (added by baf3437) - MultisampleTarget.cpp guardedMutex() static mutex (added by d109f39) Apply the same priming. Recompiler.cpp reuses the PrimedSharedMutex already defined in that translation unit; MultisampleTarget.cpp gets the identical local helper to match the existing inline precedent. Tested: Windows 11 x64, WinLibs GCC 15.2.0 (matches CI). Compiled Recompiler.cpp.obj and all three MultisampleTarget.cpp.obj targets with the project build flags; check_conventions.py passes. (cherry picked from commit 4d37c93) Fork: MultisampleTarget.cpp hunk left to the AGC slice. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * perf(shader): drop the input flush that rcp, rsq, sqrt, exp and log results never use The SPIR-V emitters of FPRecip32, FPRecipSqrt32, FPSqrt, FPExp2 and FPLog2 flushed a denormal input to its signed zero before the host operation. Since #537, unaryFloatSpecials replaces the host result for every input below 2^-126 with the hardware's result for a zero of the same sign: rcp and rsq give a signed infinity, sqrt the signed zero, log -inf and exp 1.0. floatUnary and float16Unary both go through it, so the flushed input could only change a value that is then discarded. v_rcp_iflag_f32 gets the same special values and already went without the flush. The pixel prologue computes PositionW as FPRecip32 of FragCoord.w without those special values, so it now flushes FragCoord.w in the IR first and gives the same result as before. (cherry picked from commit 1dc99ba) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(shader): split a BDA span read's in-place loads from its fallback On an RTX 3060 (Windows, driver 591.86) a dispatch never finishes when the lanes of read_bda_span split between its in-place dword loads and its per-dword byte fallback, which calls read_bda_dword_bytes: Windows resets the GPU and the device is lost. The two were the arms of one selection in the DontInline function. They are now two selections, the loads under the span's condition and the fallback under its negation, joined after both. Loads, lookups, faults and stops are unchanged for every lane; only the SPIR-V control-flow shape changes (SPIR-V specification 2.11, structured control flow). Every stage except mesh shaders is emitted as SPIR-V 1.3, and OpSelect chooses between composites with a scalar condition only from 1.4 on, so the join stays a selection. The same hang and fix were reported in boykopovar#3291 on an RTX 4060 Laptop GPU (Windows, driver 591.86) and an RTX 5060 (Linux, driver 590.48.01). An RTX 4070 Laptop GPU on driver 616.92 does not hang with either shape. The driver-side cause is unknown, so the shape is recorded in docs/dev/debt/shader.md. On main the hang fails nine tests on the RTX 3060, in two ways. agc_driver_{flat,global}_loads{,_x2_x3,_x4} and agc_driver_flat_apertures_{2,5} abort after 64 to 71 s: the lost batch keeps its lease, so teardown waits 60 s for the pinned guest allocation. agc_driver_pixel_interlock fails after about 4 to 7 s with vkDeviceWaitIdle returning VK_ERROR_DEVICE_LOST (-4) and never reaches the pin wait. All nine pass with the change. Fixes boykopovar#3291 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit f1b2e60) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(shader): load a BDA span in place from its own function read_bda_span chooses between the span's in-place dword loads and its per-dword byte fallback, which calls read_bda_dword_bytes. Each shape of that choice so far hangs a group of NVIDIA drivers: a dispatch never finishes and the device is lost. As the two arms of one selection, with the loads emitted in the arm, it hung drivers 591.86 and 590.48.01 when lanes took both arms (boykopovar#3291). As two selections, the loads under the span's condition and the fallback under its negation (boykopovar#4759), it hangs 595.97, 596.36, 610.62, 610.74 and 610.57.04 (boykopovar#4975). The loads now live in load_bda_span, a DontInline function that calls nothing, and read_bda_span is one selection again: one arm calls load_bda_span, the other is the fallback as before. Probe, loads, lookups, faults and stops are unchanged for every lane. On an RTX 4090 (Linux, driver 610.57.04) the two-selection shape hangs agc_driver_scalar_wide_loads and agc_driver_flat_apertures_0..13 in a build with SPIRV-Tools, and agc_driver_bda_span_reads and agc_driver_pixel_interlock in a build without. All pass with this change in both builds. Drivers 591.86, 590.48.01, 595.97, 596.36, 610.62 and 610.74 are not tested; a shape with the loads in their own DontInline function was reported passing on 591.86 in boykopovar#3291. agc_shader_memory now requires that a span read function holds no load and calls its load function once, and that the load function holds the four loads and no call. Refs boykopovar#4975 (cherry picked from commit 8c04efc) Fork: libSceAgc* and docs/dev/debt hunks dropped (AGC slice / file absent). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * perf(shader): convert f16 operands with UnpackHalf2x16 where f16 denormals are kept Since boykopovar#1463, every f16 operand read decodes sign, exponent and mantissa with integer operations, 8 to 12 instructions more per read than UnpackHalf2x16 on NVIDIA. The decode works around an AMD Windows driver that folds the mask of a byte-selected half into UnpackHalf2x16. A new target flag, float16DenormPreserve, is set when the device reports shaderDenormPreserveFloat16 and denormBehaviorIndependence ALL, the same rule as float64DenormPreserve. With it, EmitF16BitsToF32 uses UnpackHalf2x16 and declares DenormPreserve for 16-bit floats, under which SPV_KHR_float_controls keeps unpacked subnormal halves. NVIDIA returns the default NaN for every NaN half, so the NaN bits are rebuilt from the half: its sign at bit 31, its payload at bits 22:13. The DenormPreserve 16 mode is declared once, shared with the native f32 to f16 conversion. Without the flag (lavapipe; RADV reports 32_BIT_ONLY) the integer decode stays. Halves of fields narrower than 16 bits, such as SDWA byte selects, keep the integer decode on every device. The flag is serialized (request version 17) and part of the cache key. New test agc_driver_f16_to_f32_unpack converts all 65,536 halves, with nonzero high bits, through v_cvt_f32_f16, and byte-selected halves through v_add_f16 SDWA, with each decode the device supports, and compares the bits with an exact decode. agc_driver_f16_to_f32_unpack_spirv checks which decode the SPIR-V uses with and without the flag. (cherry picked from commit e4f212b) Fork: request version 21 kept from upstream, on top of version 20 (paClVsOutCntl) from the AGC slice. The Vulkan device half (VulkanDevice.cpp) and the libSceAgcDriver tests are dropped; on Metal float16DenormPreserve stays false, so the integer f16 decode is kept. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * perf(shader): skip the f16 NaN rebuild for operands that are only compared With float16DenormPreserve, EmitF16BitsToF32 rebuilds the bits of a NaN half after UnpackHalf2x16, because NVIDIA returns the default NaN and Intel quiets it. When every use of the converted operand is an f32 comparison or a NaN test, directly or through abs and neg, only whether it is NaN matters, so the conversion returns UnpackHalf2x16's result as is. v_cmp_*_f16 goes from 11.5 to 4 SASS on an RTX 4060. agc_driver_f16_to_f32_unpack adds v_cmp_u_f16 and v_cmp_lt_f16 over all 65,536 halves with each decode the device supports. Its SPIR-V check requires the v_cvt_f32_f16 conversion to rebuild the NaN bits and the compared conversions not to. (cherry picked from commit 352bf30) Fork: libSceAgc* and docs/dev/debt hunks dropped (AGC slice / file absent). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(shader): keep cube image gradients out of the face coordinate bias image_sample_d and image_sample_cd on a cube pass their gradients through the same conversion as the face coordinates, which subtracts 1.0 to move AMD's [1, 2] face range to [0, 1]. A gradient is a difference of coordinates, so the bias turned a one-texel gradient into one of about -1 and the sample read the smallest mip. Gradients now skip the bias. agc_driver_image_sample_cube_derivatives samples every face of a 16x16 cube at five levels through image_sample_d and checks the level and face it read. TechnicalDebt records that the gradient handling follows Mesa and was not measured on hardware. (cherry picked from commit 73eaa1f) Fork: libSceAgc* and docs/dev/debt hunks dropped (AGC slice / file absent). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(shader): declare the GDS buffer coherent GDS is one memory for the whole GPU on AMD hardware: every work-group's ds_read and ds_write with gds see the others' writes, with no cache in between. The recompiler keeps it in a storage buffer that was not decorated Coherent, so on a GPU whose L1 caches storage buffer loads, NVIDIA's from Turing on among them, a plain GDS read could return a value cached before another work-group's GDS write. Atomics were not affected. The gds buffer is now declared Coherent. verifyCoherentGds in agc_shader_memory compiles a ds_read_b32 and a ds_write_b32 on GDS and checks that the gds buffer is Coherent. (cherry picked from commit 0998eb4) Fork: libSceAgc* and docs/dev/debt hunks dropped (AGC slice / file absent). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * perf(shader): reclaim unreachable cached source IR values (cherry picked from commit 57b2dc8) Fork: libSceAgc* and docs/dev/debt hunks dropped (AGC slice / file absent). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(shader): read the ancillary render target index from Layer when the draw exports it (cherry picked from commit 7015c04) Fork: the DrawDecode.cpp line that sets layerExported is in libSceAgcDriver and left to the AGC slice; until it lands layerExported stays false and the ancillary render target index reads as 0, as before. Fork ShaderDiskCache format kept (bumped once at the end of the slice); libSceAgcDriver tests and docs/dev/debt dropped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(shader): serialize the pixel layer export flag from request version 23 main allocates request version 22 to the tessellation patches per group, so a version 22 request has no layer export byte. (cherry picked from commit de5279b) Fork: request version 23 kept to match upstream; version 22 (patchesPerGroup, from AGC commit 25a8372) is not in this tree yet, so the fork writes no version 22 field. libSceAgcDriver test hunk dropped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(shader): bump the shader disk cache format for the sync-8 shader slice The ported recompiler changes add layerExported to the cached pixel info and change generated shader output, so cached entries from format 31 are not reused. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Karan Vora <52519664+karanvora2599@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Mohamed Aziz Hadjkacem <mohamedaziz.hadjkacem21@gmail.com> Co-authored-by: fdfawef324 <287205748+fdfawef324@users.noreply.github.com> Co-authored-by: Dean Galvin <dean.galvin@live.com> Co-authored-by: eljackjoestar-cmd <eljackjoestar@gmail.com> Co-authored-by: TideOPL <obinewman101@gmail.com>
What
The unary float ops
v_ceil,v_floor,v_trunc,v_rndne,v_fract,v_sqrt,v_rsq,v_rcp,v_log,v_exp,v_sin,v_cos(f32 and f16), plusv_rcp_iflag_f32andv_frexp_mant_f32, left their special results to the driver. Vulkan doesn't specify those results (NaN payloads,InverseSqrt/Log2at 0,FDivby 0 or inf), and on local drivers several were wrong:v_frexp_mant_f32), and lavapipe dropped the NaN sign and payload onlog/sin/cos, f32 and f16.log/sqrt/rsqof a negative,fract/sin/cosof inf) were+NaN(0x7fc00000) on lavapipe. The hardware gives-NaN(0xffc00000, f160xfe00).sinat whole and half cycles andcosat quarter cycles returned values like-8.7e-8where the hardware gives+0. The f16 path already had this rule.v_fract_f16(-2^-14)rounded to1.0. The hardware stops at0x3bff, as f32 stops at0x3f7fffff.A shared helper now picks these results on the bits, from the source bits after
neg/abs. Finite results still come from the driver's approximation.float16Trigis merged intofloat16Unary. f16 results go throughapplyF16ResultModifiersfrom #323, so clamp/omod behave as they do there.Behaviour measured on HW (RDNA2, gfx1035, IEEE=1, f32 denormals on)
| 0x400000, f16| 0x200), sign kept, for every op here.0xffc00000/0xfe00.rcp(±0) = ±inf,rcp(±inf) = ±0;rsq(±0) = ±inf,rsq(+inf) = +0;sqrt(±0) = ±0,sqrt(+inf) = +inf;log(±0) = -inf,log(1) = +0,log(+inf) = +inf;exp(±0) = 1,exp(x ≥ 128) = +inf,exp(x < -126) = +0(f32 denormal results are flushed, the same assumption as the existing denormal input flush of these ops);fract(-0) = +0;sin(±0) = ±0.v_rcp,v_rcp_iflag,v_rsq,v_sqrt,v_logandv_expcount as zero with their sign, in every mode (IEEE 0/1, denormals on/off):rcp/rsqgive ±inf,sqrt±0,log-inf (also for a negative denormal),exp1. f16 ops keep denormals.main.sin/coslose accuracy beyond about ±2^20 cycles (the input is converted with limited precision). This is recorded in TechnicalDebt, and the test keeps f32 trig inputs below 256 cycles.Tested
New execution test
agc_driver_vop1_float_unary_testsruns the 27 opcodes (15 f32, 12 f16) on two inputs per lane, 54 results × 32 rows. Rows were picked by class (sign, zero, inf, quiet/signaling NaN, invalid, magnitude bands, zero/inf results), plus fixed rows for sNaN, -inf/-0,fract(-2^-30)and f32 denormals (0x807fffff,0x007fffff). Expected values are what the hardware returns, and a Python model matches all of them. Exact ops and special results are compared bit for bit. Finite results of the approximate ops are compared with tolerances based on the Vulkan precision table: rcp/rsq/sqrt 2^-20 relative, log 2^-21 absolute or 2^-20 relative, exp (4+2|x|) ulp, f32 sin/cos 2^-11 absolute, f16 2^-9 relative or 2^-10 absolute for sin/cos. Denormal results are excluded, and the denormal row is only checked on the six ops that flush (on the others the result depends on the denormal mode).It passes on RADV, local lavapipe and CI's lavapipe (Mesa 25.2.8). The test fails with each of these changes: NaN not quieted or left to the driver (f32, f16), positive invalid NaN,
fractreaching 1 (f32 on lavapipe, f16 on both),sin/coscardinals off (lavapipe; RADV happens to return them exactly), sign ofsin(-0), andv_frexp_mant_f32NaN. The zero/inf pins for rcp/rsq/sqrt/log/exp already come out right on RADV and lavapipe, which is why they're picked on the bits for other drivers. Setting each pin to a wrong value fails the test, so every pin is covered (exceptexp(0), which falls inside the exp tolerance). Without the denormal flush the test fails (sqrt(-denormal)gives0xffc00000, and0x807fffffwith the sign kept but not flushed); RADV and lavapipe already flush theFDivinput ofrcp_iflag, andexpof a denormal rounds to 1 anyway, so those two can't fail locally.agc_driver_float16_result_modifiers_tests(#323) still passes. The full Release ctest passes exceptagc_driver_pixel_helper_lanes,agc_driver_global_load_addtidandagc_driver_vop3_carry_out(wave32 lane 32 on RADV's 64-wide subgroup), which also fail onmainon local RADV.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)