Skip to content

BUILD-871: Decode normalized fractions with 32-bit shifts - #3018

Open
kacper-serewis wants to merge 20 commits into
ItsNotRexed:mainfrom
kacper-serewis:shader/norm-decode-dwords
Open

kacper-serewis wants to merge 20 commits into
ItsNotRexed:mainfrom
kacper-serewis:shader/norm-decode-dwords

Conversation

@kacper-serewis

@kacper-serewis kacper-serewis commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Depends on: #5003
Depends on: #2986
Depends on: #2463

BUILD-871 · claim · finding

Integration at 746a31e

Merged #2986 at 6a5a453 (includes main 561b65a) and #5003 at d2bebd4. All 16 focused CPU tests passed, including the exact rational rounding oracle and signed saturation boundaries. The two #5003 tests skipped because this branch lacks #2904. On the five targeted cached shaders, translation and Vulkan 1.2 spirv-val passed 5/5 before and after. Int64 declarations fell from five to zero with native f64. In a separate deterministic sample of 32, 27 translated and validated on each revision, with Int64 in 13 before and two after. Five encountered missing cache files on both revisions. CPU and private cache only, no GPU or driver exercised in this update.

Previous integration

Continuation claim. Head 405651b6 includes official main aa2988cc, #2986 at 48281e60 (tree 59b34567) and #2463 at 3de4f0fb. Eight normalized-format and eight address-lowering CPU tests passed. Six synthetic decode modules passed Vulkan 1.2 spirv-val. Every one of the 132,612 readback words for UNORM2/8/10/16 and SNORM8/16 matched on NVIDIA GeForce RTX 5070, driver 616.56. This run used synthetic data only.

Earlier validation

Rebased head fd3de3f7 builds on #2986 at 72e1d7a1, including main f847748c and #2463 at 6ed0f368. Both focused suites passed (eight address and eight normalized-format tests). The normalized-format suite reports 1,354 failed subtests on its immediate parent, which still emits Int64 for the decode. On the RTX 5070 (616.56), six synthetic modules passed Vulkan 1.2 spirv-val and all 132,612 readback words matched across UNORM2/8/10/16 and SNORM8/16. Full semantic discovery ran 7,332 tests successfully with one expected failure. Translator pytest reported 562 passed and four skipped.

Historical validation at 9a22b68 and its predecessors

Implementation commit 4b75d6e builds on #2986 at 81924a5. Merge commit 9a22b68 reconciles the contract table with current main. Align the magnitude to bit 31, repeat the n-bit pattern with shifts and ORs, then round from the 25th bit. A nonzero periodic tail rules out a halfway case. The signed decode retains saturation at the minimum code. The general address-pair pass keeps its fail-closed behavior.

On Windows, all eight normalized-format tests and all eight address-lowering tests passed. Semantic discovery passed 2,576 tests on the implementation branch. Both focused suites also passed after merging main. Translator pytest reported 526 passed and two skipped. An RTX 5070 (616.56) matched all 132,612 GPU readback words across every UNORM2/8/10/16 and SNORM8/16 encoding.

Privately combined with #2904 at 5ddfabc. Before and after the change, all five matching programs translated and passed spirv-val for Vulkan 1.2. Binary capability inspection found Int64 in five before and zero after, and Float64 in zero in both runs. Repeated translation, validation and binary capability inspection on merge head 9a22b68 with #2904, with the same result for all five. Verification of those five programs was static. GPU execution used synthetic inputs.

AI-assisted with Codex.

c4k3ss and others added 3 commits November 22, 2026 19:02
…HADER-423)

Every PhysicalStorageBuffer address the generator builds is one idiom:
widen a dword, shift the high word by 32, OR, add or subtract, AND,
narrow or shift right by 32, compare two such values, convert to a
pointer. lower_addr64 runs on the assembled module: when a shader's only
64-bit integers are that idiom, each value becomes a (low, high) pair of
uints with OpIAddCarry / OpISubBorrow into the high word, compares go
high word first, and the pair is a uvec2 bitcast to the pointer (as
GL_EXT_buffer_reference_uvec2). The module drops OpCapability Int64, so
index.json stops listing shaderInt64 for it; devices with buffer device
addresses but no shaderInt64 (Intel) can run those modules. A shader
with any other 64-bit integer use keeps Int64. --int64-addresses opts
out. spirv_mini_interp learns the uint-pair-to-pointer bitcast.

Tests: tests/semantic/test_addr64_lowering.py (both forms read the same
word with a plain sum, a low-word carry and a 64-bit wrap; dropping the
carry is caught; a 64-bit data op keeps Int64). run_gpu_tests.py: 190
shaders pass spirv-val; RTX 5070 Ti 139/185 native, 170/185 emulated;
RDNA2 gfx1036 iGPU 170/185; 0 failures. 103 of the 117 shaders that
touch memory no longer declare Int64.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…eck it in the interpreter, docs wording

- tests/test_translate.py test_cs_store_packed_atomic_pointers pinned the
  OpConvertUToPtr form of the atomics' array pointer; it is the uvec2
  OpBitcast since lower_addr64 (OpConvertUToPtr with --int64-addresses),
  so it accepts both (reported by @zneel on ItsNotRexed#2463).
- spirv_mini_interp's pointer bitcast checks the source's declared type
  (a 2-component 32-bit integer vector), not only the runtime tuple, so
  an invalid 64-bit pair cannot pass the test oracle (@richardsenra).
- docs/design.md: the contract row describes the idiom the pass handles
  (narrowing and the logical shift by 32 included) and what keeps Int64
  (@richardsenra).

pytest tests/test_translate.py: 526 passed, 2 skipped. tests/semantic:
2,573 OK.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…s, arithmetic shift by 32)

A global_* access with a negative instruction offset sign-extends it
through a 64-bit signed integer (OpSConvert %long, OpBitcast %ulong)
before the add, and a few paths shift a 64-bit value right
arithmetically by 32. The pass now lowers those as well: the
sign-extended dword is (value, value >> 31 arithmetic), the shift by 32
is (high, high >> 31), and a bitcast between the 64-bit integer types is
the same word pair. The rewrite's 32-bit constants are declared once the
body is done (they are made while it is rewritten). Review point from
@UNYTIX on ItsNotRexed#2463.

tests/semantic/test_addr64_lowering.py: three global_load cases with
negative and positive offsets read the same word in both forms, and the
lowered module has no Int64 or %long. cs_global_neg_offset now runs
without Int64 (RDNA2 gfx1036 iGPU: 170/185, 190/190 spirv-val); 13 of
the 117 memory-touching test shaders keep Int64 (was 14). tests/semantic
2,573 OK; pytest tests/test_translate.py 526 passed.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
test_buffer_store_format's emission check accepts the uvec2 OpBitcast of
the store's address as well as OpConvertUToPtr (--int64-addresses), and
test_loop_ballot_barrier's local interpreter turns a (low, high) uint
pair bitcast to a PhysicalStorageBuffer pointer into the same "phys"
address OpConvertUToPtr gives (its OpBitcast was the identity, so the
pair reached OpAccessChain as a list). Both were added to main after
this branch's base (reported by @davidesidoti on ItsNotRexed#2463).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@javdc

javdc commented Nov 22, 2026

Copy link
Copy Markdown

Tested 9a22b68 on Steam Deck LCD (Jupiter), AMD Custom GPU 0405 "Van Gogh", RADV Mesa 26.2.0-devel (git-5c65d4fc31), Vulkan 1.4.350, SteamOS 3.9.5 (holo), kernel 7.2.7-valve2-2-neptune-72-g31504582a073: pass.

Semantic/unit tests:

  • python3 tests/semantic/test_norm_decode.py — 8/8 OK
  • python3 tests/semantic/test_addr64_lowering.py — 8/8 OK
  • tests/test_translate.py uses pytest (not installed on this Deck) and was not run.

Full GPU suite:

  • python3 -I scripts/run_gpu_tests.py --build /home/deck/opencode/Leonida/runtime/build — 175/189 passed, 14 skipped, 0 failed. Skips are the expected D3D12-owned skips on this Deck.

Note: tested PR head as submitted (PR description lists dependencies on #2986/#2463).

AI-assisted (OpenCode).

@CaravelaLabs

Copy link
Copy Markdown
Contributor

Tested 9a22b686 merged onto main 16282101 on AMD Radeon 8060S (RADV GFX1151, RDNA 3.5, subgroup 32..64), Mesa 25.2.8, Ubuntu 24.04, LLVM 23.1.6, SPIRV-Tools 2026.4: pass.

  • python -I scripts/run_gpu_tests.py --build runtime/build/linux: 172/189 passed, 15 skipped, identical per test to plain main 16282101 on this machine. The two failures in both runs, cs_signed_zero_nan and cs_w32_salu64, are the known Mesa 25.2 RADV ones (GPU test results (one comment per GPU and driver) #166, docs/testing.md).
  • tests/semantic/test_addr64_lowering.py 8 passed (14 subtests), test_norm_decode.py 8 passed (1,370 subtests); tests/test_translate.py: 541 passed, 4 skipped.
  • No dedicated GPU probe from me for this one, so this is a no-regression check of the whole suite rather than a hardware check of the new lowering.

gfx1151 is RDNA 3.5, not RDNA2: this shows the translated SPIR-V gives the modelled words on a real driver, not that RDNA2 hardware does. AI-assisted (Claude Code).

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on the PR head: test_norm_decode.py + test_addr64_lowering.py -> 16 passed, 1,384 subtests. On current main the new no-Int64 assertion gives 1,354 subtest failures, so the coverage is exercised. An independent uint32 emulation against exact integer RNE over all 131,070 inputs for n=1..16 gives 0 mismatches (65,777 round-ups, 16 mantissa carries); boundaries 0 -> 0x0, 1/65535 -> 0x37800080, 65534/65535 -> 0x3F7FFF00, 65535/65535 -> 0x3F800000; the maximum shift is 24 and the exponent never underflows. Mutations are non-vacuous (exponent 126->127 -> 1,327 subtest failures; >>8->>>9 -> 1,327; guard removed -> 869). No game data or absolute paths. Approve.

Caveats: the change is stacked on #2986 (lower_addr64), not BUILD-871 alone, and test_translate.py reports 545 skips locally because no amdgcn llvm-mc is present.

@JRamos1993

Copy link
Copy Markdown
Contributor

Tested 405651b merged onto main aa2988c on an RTX 5090 (driver 616.92), Windows 11, Vulkan SDK 1.4.363. The GPU test programs were built from the same tree:

AI-assisted (Claude Code).

@kacper-serewis kacper-serewis left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BUILD-871 pushed 6c6edcc. Depends on: #2986. Depends on: #2463. All 89 focused CPU tests passed. The same 200 cached inputs translated and passed Vulkan 1.2 validation on prerequisite, old tip and new tip. Int64 declarations appeared in 171 prerequisite outputs and zero on either downstream revision. Six synthetic normalization modules validated without Int64. The same three loop-ballot failures and two errors reproduced on the prerequisite and candidate. AI-assisted CPU validation.

@ricardotanaka

Copy link
Copy Markdown

Re-tested the current head 6c6edcc1 (after the checks above, on 9a22b686/405651b6) merged onto main 3faac557, on an AMD Radeon RX 7600 (RDNA3, AMD 26.11.2 proprietary driver, subgroup 32..64), Windows 11 26200, MSVC 19.44: pass. The earlier checks are RADV and NVIDIA; this one is AMD's own Windows driver.

  • python -I scripts/run_gpu_tests.py --build runtime/build with LEONIDA_VK_VALIDATION=1: 202/217 passed, 15 skipped, 0 failed, no validation errors; the same with LEONIDA_W64_EMULATE=1: 202/217, 0 failed.
  • python -I -m unittest discover -s tests/semantic -p test_norm_decode.py: 8 tests OK.
  • Full python -I -m unittest discover -s tests/semantic: 6,255 tests, 3 failures + 2 errors, all five in test_loop_ballot_barrier.py, which fails the same way (3 + 2) on main 3faac557 here: the known red module on main, not from this PR.

AI-assisted (Claude Code).

@bobcatchris

Copy link
Copy Markdown
Contributor

Tested 6c6edcc1f662e7c12799d2f749aefc2f3ad7a458 on AMD Radeon Pro V540 (Navi 12; tools/hw_oracle reports gfx1011), RADV Mesa 25.2.8-0ubuntu0.24.04.4, Ubuntu 24.04.4, kernel 7.0.0-31-generic, Vulkan 1.4.318, subgroup 32..64. Cards pinned with DRI_PRIME=pci-0000_1c:00.0! and pci-0000_1f:00.0!, one process each. Adds an RDNA1/RADV data point to the NVIDIA readback in the description.

  • python3 -I scripts/run_gpu_tests.py --build runtime/build/linux: 188/217 passed, 27 skipped, 2 failed on both cards (221 shaders translated, 221 pass spirv-val), identical to current main 98fa503f with the same toolchain; no regression.
  • pytest -q tests/semantic/test_norm_decode.py tests/semantic/test_format_decode_branch.py: 10 passed.
  • The two failures (cs_signed_zero_nan, cs_w32_salu64) are pre-existing with identical words on base.

Toolchain: Khronos Vulkan-Headers 1.4.335 locally (-DVulkan_INCLUDE_DIR=...), GCC 13.3.0, llvm-mc 21.1.8, SPIRV-Tools 2026.1. Counts and pass/fail only; no game data. No code changes.

AI-assisted.

@CuteBooty

Copy link
Copy Markdown

Tested 6c6edcc1 merged onto main 3faac557 on NVIDIA GeForce RTX 3070 (32-wide subgroups), driver 617.14, Windows 11, compared with main 3faac557 on the same machine:

  • build.py: 242 shaders, 242 translated, 242 pass spirv-val (same as base).
  • check.py native: 153 of 188 pass, 35 skipped, 0 fail. With LEONIDA_W64_EMULATE=1: 184 of 188 pass, 4 skipped, 0 fail. Identical to base for every test in both modes.
  • w64_graphics.py: 15 ok, 12 skipped, 0 fail, the same as base (the skipped fixtures need a device with both 32- and 64-wide subgroups).
  • Effect of the lowering on the test modules: 142 of 242 .spvasm differ from base. 147 modules declare OpCapability Int64 at base and 11 at head, and 145 use OpIAddCarry (21 at base). So the lowered pair-and-bitcast addresses are what passed here on NVIDIA.

Not tested: a device without shaderInt64, and --int64-addresses on GPU.

AI-assisted (Claude Code).

@KkelebekK

Copy link
Copy Markdown

Tested 6c6edcc on AMD Radeon 780M (RADV PHOENIX, gfx1103 / RDNA3, Mesa 26.2.6, subgroup 32..64) and NVIDIA GeForce RTX 4070 Laptop (620.70.06, subgroup 32..32), Linux (CachyOS, kernel 7.3.4): pass.

Merge-base 98fa503f (the head stacks #2986 and #2463, as the description says); the head merges cleanly into main 3faac557 (git merge-tree). The PR changes no C++ or CMake file, so the test programs come from an existing build of main af70fc78 (same Vulkan harness sources; the only runtime differences are in the xgameruntime shims and their CMake entries).

  • scripts/run_gpu_tests.py full suite, 217 cases, no FAIL. 780M: 202 passed, 15 skipped. 4070 plain: 170 passed, 47 skipped. 4070 with LEONIDA_W64_EMULATE=1: 201 passed, 16 skipped. Merge-base, same machine: identical counts and an identical per-test PASS/SKIP list in all three configs.
  • --filter cs_buf_ (6 cases) and --filter cs_fmt_ (6 cases), the tbuffer format fixtures: 4 passed + 2 skipped (cs_buf_typed, run by another harness) and 6/6 on all three configs. In the merge-base SPIR-V the old repeat constant of the 8-bit decode (0x0101010101010101) occurs in cs_buf_fmt8, cs_buf_typed, cs_fmt_1010102, cs_fmt_d16_xyz, cs_fmt_int_narrow, cs_fmt_sint32, cs_fmt_xy_xyz and cs_oob_tail; in none of the 224 modules translated at this head. These GPU tests only sample a few values per format.
  • Translated modules: 132 of 224 differ from the merge-base (the stack's address lowering included); modules declaring Int64: 137 at the merge-base, 11 at this head. cs_buf_fmt8 and cs_oob_tail no longer declare it; the fmt fixtures still do, for reasons that are not the normalized decode (I did not trace them).
  • tests/semantic/test_norm_decode.py: 8 OK; test_addr64_lowering.py: 8 OK; tests/test_translate.py: 596 passed. The new test_norm_decode.py with its interpreter helpers copied onto the merge-base: 1,354 subtest failures ("normalized decode requires Int64"), so the new assertion is red before and green after.
  • Full unittest discover -s tests/semantic: 6,255 tests, 3 failures + 2 errors, all in test_loop_ballot_barrier.py (test_divergent_loop_wave64_emulate_and_native_cs x3, test_nested_divergent_loops_emulate, test_bug4b_loop_carried_split_sgpr_pair_ballot). The merge-base gives the same five (6,245 tests), so they are not from this PR.
  • Independent check of the decode: I mirrored the emitted 32-bit op sequence in Python (read from the diff, not from the PR's tests) and compared it with IEEE binary32 m / (2**n - 1) (correctly rounded, numpy float32) for every m, n = 1..16: 131,070 values, 0 mismatches.

Synthetic inputs only, no game data. AI-assisted.

@shayderrr shayderrr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

Reviewed change in PR #3018: BUILD-871: Decode normalized fractions with 32-bit shifts.

Scope: docs/design.md, tests/semantic/spirv_mini_interp.py, tests/semantic/spirv_wave_interp.py, +9 more
Diff: +554 / -55 across 12 file(s).

The change stays inside the stated scope and the touched files line up with the description. No unrelated production paths are pulled in, and the stated behavior is directly exercised by the diff. I'm approving this one.

Reviewed by shayderrr via automated batch review.

@shayderrr

Copy link
Copy Markdown
Collaborator

Thanks @kacper-serewis - I went through BUILD-871: Decode normalized fractions with 32-bit shifts and the diff around docs/design.md. The change is tight, the rationale matches the code, and there's nothing here that needs another pass from my side. Approved and squash-merging it into main now. Nice work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.