Repository navigation
Translator: pack d16 image sample/gather results with integer f32->f16 RTE - #489
Conversation
|
Measured the packed 16-bit words from both A synthetic 8×8 RGBA32_FLOAT texture held 64 distinct binary32 values. For nine binary16 rounding boundaries I uploaded the midpoint and its two immediately adjacent binary32 values, with both signs. Cases included zero/min-subnormal, max-subnormal/min-normal, odd and even normal ties, a power-of-two transition, and the 65520 overflow threshold. The other ten inputs were signed zeros, minimum subnormals, minimum normals, maximum finite halves and infinities. Channel permutations and the gather footprint exercised both packed words and both half positions. The pixel shaders extracted each half as an unsigned 16-bit integer for readback, preserving the word even for negative zero and infinity.
The explicit RTZ controls matched a separate toward-zero oracle on every word. For example, input bits |
|
Tested ca611ae on two GPUs, Linux 7.3.5 (CachyOS): pass.
Ran |
|
Tested
|
|
Thanks @Vonsek, @esteban-dcp and @dcastell1 for the GPU tests and the 16,384-word binary16 RTE measurement! Rebased onto current main after #240 merged (code and fixture identical to ca611ae). |
b291690 to
2efd471
Compare
|
Tested ca611ae on RTX 5070 (Blackwell, subgroup 32..32), NVIDIA 616.56, Windows 11 Pro 26200: pass.
Tools: llvm-mc 21.1.8 (WSL), SPIRV-Tools v2026.4. |
|
Tested 2efd471 on two AMD GPUs with RADV (Mesa 26.3.1, Linux): pass.
CPU side
Three notes from reading it
AI-assisted (Claude Code, Claude Fable 5.2). |
|
Replying to @aslan2142. @aslan2142, I checked the rest of the stock texture on For a fixture-specific texel with your 9/255 input, the shader already expands the packed half back to f32 before export. The exact RGBA32F readback word to assert for that channel is AI-assisted. |
|
@tylerstraub you're right, thanks for checking. The stock texture's codes (0, 32, …, 224, 128, 255) all convert the same under both roundings, so it takes a texel of its own. I get the same two words for 9/255: One update to my test comment: the AI-assisted (Claude Code, Claude Fable 5.2). |
Work around an amdvlk64 pipeline-creation crash (0xC0000005 on RX 9070 XT) triggered when GLSL.std.450 PackHalf2x16 on an OpImageSampleExplicitLod d16 result feeds a structured loop (OpLoopMerge inside OpSelectionMerge) using OpGroupNonUniformAny. Replace PackHalf2x16 at the two d16 image_gather4*/image_sample* sites in tools/rdna2spirv.py with a branchless pure-integer IEEE-754 binary16 RTE packer (Gen.f32_to_f16_int / Gen.pack_f16_pair_int). Leave all other PackHalf2x16 call sites unchanged. Add unit tests covering edge cases and a 58,368-value sweep across all 256 exponents (tests/test_d16_pack.py), SPIR-V translation/validation tests for d16 image_sample and image_gather4 (tests/test_translate.py), and a synthetic GPU sample shader (runtime/test/shaders/ps_texture_sample_d16.asm).
2efd471 to
dbb3319
Compare
|
Thanks @kacper-serewis, @aslan2142 and @tylerstraub for the GPU tests and the review notes! Rebased onto c432cdc (now that #551 is on main, tools/rdna2spirv.py is down to the two d16 call-site changes) and corrected the 0x3804 half-float comment. |
|
Replying to @eIBart0. There’s a landing-order dependency with #575 after both rebases. At When combining the heads, please update all three callers together (or retain a forwarding method), then run the d16 pack tests and translate both a sample and a gather fixture. Otherwise those paths would call a missing Python method even if Git accepts the merge. I verified the two diffs; this is a source-level finding. #489 changes · #575 changes AI-assisted. |
|
Tested I measured the 9/255 rounding case discussed above on this rebased head. The stock ps_texture_sample_d16 draw passed with center RGBA (0.501953, 0.501953, 0.501953, 1.0); both shaders translated and passed spirv-val. For a second draw I reused those same SPIR-V modules and changed only the synthetic RGBA8 texture initialization in a local copy of the harness: every red texel held code 9. All 4,096 red framebuffer words were exactly 0x3d10a000 (half 0x2885, nearest-even). Zero were 0x3d108000 (half 0x2884, truncation). This measures translated Vulkan image_sample d16 for one distinguishing input. The gather path, native RDNA2 sampler rounding and the Windows AMD pipeline-creation issue were outside this run. AI-assisted verification. |
- rdna2spirv: restore f32_to_f16_int / pack_f16_pair_int, which #489's call sites need - spirv_mini_interp: OpFDiv by zero is undefined, so refuse it again in the shared float branch - test_run_gpu_tests: take the wave32 lane-dump list from check.py rather than restating it - vulkan_replay: pass the ResourceBinding to round_trip in the clear path, as its new signature expects Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An OpULessThan and OpSelect give the same shift (the same bits on 30,648 inputs). With no GLSL.std.450 left, f16_bits can narrow the d16 memory results, which ItsNotRexed#489 keeps free of extended instructions.
…t goes main had two integer round-to-nearest-even narrowers: ItsNotRexed#489's f32_to_f16_int for the d16 image results and f16_bits for everything else. They gave the same bits on 30,648 inputs, so pack_f16_pair_int now uses f16_bits and there is one path to keep right.
Summary
Works around an
amdvlk64pipeline-creation crash (0xC0000005on AMD RX 9070 XT) triggered whenGLSL.std.450PackHalf2x16on anOpImageSampleExplicitLodd16result feeds a structured loop (OpLoopMergeinsideOpSelectionMerge) usingOpGroupNonUniformAny.Replaces
PackHalf2x16only at the twod16image_gather4*/image_sample*packing sites intools/rdna2spirv.pywith a branchless, pure-integer IEEE-754 binary16 round-to-nearest-even (RTE) helper (Gen.f32_to_f16_int/Gen.pack_f16_pair_int). All otherPackHalf2x16sites remain untouched.Rounding
The integer helper implements IEEE-754 binary16 RTE (preserving
±0.0, preserving f16 subnormals, overflowing|x| >= 65520to±Inf, and canonicalizing NaNs to sign-preserving quiet NaNs). RTE is assumed for the sampler'sd16conversion, in line with D3Df32→f16conversion rules — this is inferred, not yet measured on hardware.Previously,
PackHalf2x16lowered tov_cvt_pkrtz_f16_f32(RTZ) in both AMDVLK/LLPC (SPIRVReader.cpp:10609+ArithBuilder.cpp:106) and Mesa ACO (aco_select_nir_alu.cpp:594). Compared to that prior lowering, there is a known difference of≤1 ULPon values that are not exact in f16, and finite overflow produces±Infinstead of±65504. Measuring actual RDNA2d16sampler rounding on hardware is left as a follow-up.Changes
tools/rdna2spirv.py: Add branchless pure-integerGen.f32_to_f16_intandGen.pack_f16_pair_int; usepack_f16_pair_intonly in thed16image_gather4*andimage_sample*packing paths.tests/test_d16_pack.py: Pure-stdlibunittestsuite checking exact edge cases (±0, subnormal/normal tie-to-even boundaries, max finite65504.0,±65520.0 → ±Inf,±Inf, quiet/signaling NaNs), a 58,368-float sweep across both signs and all 256 exponents againststruct.pack('<e', ...), and emitted SPIR-V op checks.tests/test_translate.py: Addtest_image_sample_and_gather_d16_avoid_packhalf2x16verifyingd16image_sampleandimage_gather4emit noPackHalf2x16and passspirv-val.runtime/test/shaders/ps_texture_sample_d16.asm&scripts/run_gpu_tests.py: Synthetic GPU test sampling the 2D gradient texture withd16, unpacking all 4 f16 halves viav_cvt_f32_f16_e32, and exporting tomrt0viavk_texture_test.Verification
python3 -m compileall -q make_game.py tools— OKtests/test_*.py(includingtests/test_d16_pack.py) — OK (tests.test_d16_pack+tests.test_shader_checks: 18 tests OK)LLVM_MC=/usr/lib/llvm-22/bin/llvm-mc python3 -m pytest tests -q --ignore=tests/semantic— 1334 passed, 6 skipped, 1 xfailed, 1018 subtests passed (verified on18333ffbefore the 1-lineDRAWN_WITHrebase)python3 -m unittest discover -s tests/semantic -v— 440 tests OKd16runtime/test/shaders/*.asm.spvasmdiff vsorigin/main— 60/60 comparable shaders byte-identical (1 skipped without synthetic DXBC);ps_texture_sample_d16.asmtranslates, passesspirv-val, and emits zeroPackHalf2x16.ps_texture_sample_d16) on hardware: PASS on AMD Radeon RX 9070 XT (Windows 11,amdvlk64;center(32,32)=(0.501953, 0.501953, 0.501953, 1.0), exact binary16 RTE0x3804/0x3c00for UNORM8128/255and1.0).amdvlk64RX 9070 XT pipeline-creation check: Across the 26,503-shader corpus, all 600 shaders withd16image_sample*/image_gather4*(507 MS,45 VS,32 CS,16 PS,0 GS) translate and passspirv-val(600/600); on AMD Radeon RX 9070 XT (amdvlk64,AMD_VK_USE_PIPELINE_CACHE=0,--jobs 1), the previously crashing compute shader (5,009 RDNA2 instructions, 1.42 MB SPIR-V; 3/30xC0000005onmainand on a 160-line synthetic repro) now compiles cleanly in 1.81 s (rc=0), completing 1,243/1,243 CS in[0, 1243)without crashes.