Repository navigation
rdna2spirv: decode only the components a typed load writes; fconst is an OpConstant (SHADER-FORMAT-DECODE-ONE-COMPONENT) - #2439
Conversation
… an OpConstant (SHADER-FORMAT-DECODE-ONE-COMPONENT) A buffer_load_format_* with a run-time FORMAT called the four per-component decode functions %fmt_dec_c0..3 at every load, whatever it wrote. One function %fmt_dec(format, element, component) replaces them, and a load calls it once per register it writes, with the component that register's DST_SEL picks: _x makes one call, not four. The function computes the four components' channel width and offset as before and the parameter selects one, so the values are the same. fconst (and tof of a uint constant) emitted a new OpBitcast of the uint constant at every use; they now give one cached OpConstant %float per bit pattern, written so spirv-as reads back the same 32 bits (float_literal: shortest decimal for a finite value, the SPIRV-Tools hex-float spelling for an infinity or a NaN). The test interpreters read that spelling. Tests: test_buffer_format_decode_one.py with hand-written fixtures (calls per load, values for 16 codes, swizzles, out of bounds, _x/_xy against _xyzw for all 128 codes) and test_fconst_constant.py (exact bits of +-0, denormals, infinities and NaN payloads). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
First compute pipeline creation on RTX 4090. I compared
Each row uses synthetic, wave64-emulated compute programs with a runtime buffer descriptor. The eight sites read distinct offsets 256 bytes apart. All components returned by each load feed a per-lane integer checksum. I checked the emitted decoder call counts: four per site on the base and one through four on the PR, according to width. Method. For each site count and width I varied a live integer literal across 12 matched program pairs, shuffling pair order and running each revision first six times. Each module ran in a fresh process. I timed only Correctness. All 192 timed dispatches matched the CPU reference for their 64 output words and preserved every other word in the 4 KiB buffer. The timed case used RGBA8_UINT with a Z/X/W/Y swizzle. I then reused one pair per shape with an RGBA32_UINT descriptor selecting W/1/Z/X. All 16 of those dispatches matched under Khronos validation, with zero validation errors. All 192 timed modules passed Vulkan 1.2 SPIR-V validation. These measurements cover first pipeline creation for small synthetic modules on this driver. The size and load-width mix of a game module could change its result. Measurement claim |
|
Reviewed head Passing at head (Windows, python3 -I, per file): the two new suites Discrimination run: copied the base One trap worth recording for future reviewers: pointing What I did not check here: the GPU side (first-pipeline-creation timings, shader-size deltas on the RTX 4090 are @ecox982745's, reproduced below), and the corpus-wide 22,528-typed-load comparison is the author's. What I verified is the interpreter-side contract: values identical, shared decode function shape pinned (and its size bound Merge state at read time: No findings on the change. AI-assisted (Hermes agent); the runs above are reproducible on any CPython box without a GPU. |
|
Heads-up: stdlib-tests-linux is red at this head — Failing job: https://github.com/ubiquitarianism/leonida/actions/runs/45962173838/job/140385835209 — Reproduced locally at this head (22a8be1), pure Python, no GPU: Root cause is exactly the fconst change. For the and at this head: So the disassembly no longer contains an The emitted form looks semantically fine: the literals here (2.0, 4.0, 0.5) are exact f32 and the which keeps the bit-exactness assertion (the id names the raw bits) without pinning the literal's textual form. Everything before/after that section of Environment for the local runs: CPython 3.13.14 (the repo's activated venv), Windows host, repo clone at head via |
|
Independently verified head
No regressions observed on Blackwell or AMD Radeon. AI-assisted. |
|
Reviewed at
AI-assisted (Claude Code, Claude Opus 5.5 and Claude Fable 5.2) |
…me bits fconst now emits one cached OpConstant %float per value (%fc<bits>) instead of an OpConstant %uint bitcast at each use, so the v_cvt_f32_f64 omod check looks for %fc<scale bits> = OpConstant %float and checks that its literal gives back exactly the scale's 32 bits. The rest of the data flow is pinned as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Fixed in The 4 failures were the
Test change ( Local runs (Windows 11, CPython 3.13,
AI-assisted (Claude Code, Claude Opus 5.5). |
|
Merging
AI-assisted review (Antigravity, Gemini). |
…en.fconsts, since ProjectLeonida#2439) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YBndacH5qds9tvU9hk8wyT
Claimed in the coordination issue as: SHADER-FORMAT-DECODE-ONE-COMPONENT
What and why
Two code-size changes in
tools/rdna2spirv.py. Neither changes a value.A typed load decodes only the components it writes. A
buffer_load_format_*that takes its format from the V# went through four shared functions,%fmt_dec_c0…%fmt_dec_c3, and every load called all four, even_x. There is now one function,%fmt_dec(format, element, component). A load calls it once per register it writes, with the component that register's DST_SEL field picks (DST_SEL - 4). For DST_SEL 0, 1 or a reserved code,kwraps past 3 and the call's value is thrown away, as before. So_xmakes 1 call,_xy2 and_xyzw4. The function body is the old per-component code: it computes the channel width and bit offset for each of the four components, as before, and the component parameter picks one with three selects. The rest of the decode is shared and unchanged. It is the same value by construction. The DST_SEL chain at each site is nowsel - 4 < 4 ? bounded(decode(sel - 4)) : sel == 1 ? one : 0, the same function ofselas the old five-way chain. MTBUF (tbuffer_load_format_*, a fixed format) and the stores are not touched. A store already makes one encode call per site.fconstgives anOpConstant %float.fconstused to emit a newOpBitcast %floatof the uint constant at every use, and so didtofof a uint constant (float literal operands). Both now return one cachedOpConstant %floatper bit pattern:%f0and%f1for 0 and 1,%fc<bits>for the others.touof such a constant gives the uint constant back, as it did through the bitcast.float_literalwrites the literal so that it assembles to the same 32 bits:0x1.8p+128, payload as stored).Checked on 24 tricky patterns, NaN payloads included, with SPIRV-Tools 2026.4
spirv-asandtools/spirv_asm.py. The three test interpreters (spirv_mini_interp,spirv_wave_interp,tests/spirv_interp.py) now read that hex-float spelling. Before, onlyspirv_asmdid. Two tests that inspected the old bitcast form now follow the constant (test_alu_sincos's sabotage control,test_image_store_dmask's analyzer), and so doestest_bitcast_round_trip's bareGen.New tests:
tests/semantic/test_buffer_format_decode_one.pywith the hand-written fixturesfixtures/typed_load_runtime_{x,xy,xyzw}.asm. The V# comes from the push constants, so the format and DST_SEL are run-time values. The test counts theOpFunctionCalls to%fmt_decper load (1, 2 and 4). It checks the values against the reference decode for 15 formats and a code outside the table, with identity, swizzled, constant and reserved selectors, and out of bounds. It also checks that_xand_xymatch the first registers of_xyzwfor all 128 codes with random selectors.tests/semantic/test_fconst_constant.py: the literal assembles back to the same bits (+-0, the smallest and largest denormals, the largest finite values, +-inf, quiet and signalling NaNs with payloads), one constant per value with no instruction emitted, and a translated module (v_sin_f32,v_cvt_norm_i16_f16) whose constants keep their bits and still give the expected words.The
docs/design.mdrow "Run-time format decode cost" described the four functions. It now describes the one function and how it was checked. No rule changes.Tested
Translator suite,
python -I scripts/run_semantic_tests.py tests/semantic: 502 files, 6,385 tests, 0 failed (plainmain: 500 files, 6,374 tests, 0 failed). The standalonetests/test_*.pythat translate or interpret SPIR-V pass too: 67 underpython -I, and 1,406 pytest tests withtests/requirements.txt.test_rcp_iflag_modsandtest_translate's gather LOD-bounds test now expect the float constant.Equivalence with
main: 22,528 typed loads through the mini interpreter, all bit-identical, translated bymainand by this branch. The loads covered_x,_xy,_xyz,_xyzw,d16_x,d16_xy,d16_xyzandd16_xyzw, all 128 format codes, random DST_SEL, byte alignments 0-3,num_recordsfrom 0 to in bounds, and random words plus NaN, infinity and denormal patterns. That script is local only.tools/buffer_format_fuzz.py --self-test: ok before and after. Its other modes need an AMD hardware oracle, and the translator side of its model istest_buffer_formats_all.py, which passes.GPU tests (
scripts/run_gpu_tests.py, llvm-mc 23.1.6):cs_buf_fmt8,cs_fmt_1010102,cs_fmt_d16_xyz,cs_fmt_int_narrow,cs_fmt_sint32,cs_fmt_xy_xyz,cs_fmt_all(336/336 words) andcs_tbuffer_store;d3d12_buffer_uav_test(cs_buf_typed, typed buffer UAVs) passes: 21,394 values, error 0;mainwith the same build on this GPU:cs_imgatomic_ext(6 of 64 sint words) andcs_texture_uav(a format without formatless storage access).Corpus slice (
--limit 300 --wave64-emulate): 300 translated and 300 passspirv-val, onmainand on this branch. The modules go from 78,480,168 to 74,512,388 bytes (−5.1%).Module size, 3 shaders retranslated locally with the make_game flags (
--wave64-emulate). Every module passesspirv-valbefore and after:%fmt_deccalls…CS_ScreenProbes_ActivateProbes…CS_RaytracingResolve_…The claim estimated −28% for ActivateProbes with single-component loads. The real gain is smaller. In a minimal program, a
_xload costs 200 instructions at its site onmainand 189 here (_xyzw: 236 → 216). The calls and the select chain were only a small part of it. The rest is the address, the bounds check, the five dword loads, the window shift and the DST_SEL kind test, and this PR does not change any of that. On top of that, each module drops three of the four decode bodies. 3,281 sites × 11 instructions is about the 36,900 instructions that ActivateProbes loses. I did not measurevkCreateComputePipelinestime: a retail test run was using the GPU.GPUs tested: NVIDIA GeForce RTX 4070 Laptop 8 GB, driver 621.40, Windows 11.
AI-assisted: yes (Claude Code, Claude Opus 5.5)
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (n/a: no behaviour change)🤖 Generated with Claude Code