Skip to content

Translator: runs of lane-local VALU instructions under one if (active) - #3549

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
DaniSBR1:pr-active-regions
Nov 23, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
DaniSBR1:pr-active-regions

Conversation

@DaniSBR1

Copy link
Copy Markdown
Contributor

New SHADER item, claimed in #3381 (claim). Depends on: none.

What and why

The translator keeps EXEC as a per-invocation flag, and dst() writes every VGPR as OpSelect(active, new, old). In straight-line code active is always true and the driver folds the selects away. After the first EXEC write it can't, so every VALU write keeps both the old and the new value live and costs a v_cndmask. On an RX 5700 XT (RADV), a pixel shader with 8 divergent if/else regions (s_and_saveexec / s_andn2 on EXEC, 100 v_fma/v_mul/v_add/v_min/v_max a side) ran 32% slower than the same program written as native SPIR-V with real if/else.

Each invocation is one lane, so skipping a lane-local instruction in an inactive lane is the same as keeping the old value of everything it writes, which is what the selects do. In a shader that may write EXEC, a run of two or more lane-local VALU instructions now goes inside one OpSelectionMerge / OpBranchConditional %active and writes its VGPRs directly. The driver then predicates the run with its hardware EXEC.

  • Lane-local (Gen.lane_local) leaves out:
    • cross-lane reads (DPP, v_readlane, v_writelane, v_permlane, v_mbcnt);
    • lane-mask, SCC and EXEC writes (v_cmp*);
    • carry and other scalar results that are written in every lane (_co_, v_div_scale, v_mad_u64_u32);
    • v_interp, and lane masks passing through a VGPR (lane_masks.py mask_writes).
  • Safety net (Gen.check_lane_local): inside a run, an instruction that emits anything but stores to %vgpr (a vote, a barrier, an image op, a store to VCC/SCC/an SGPR, a kill, ...) stops the translation with an AssertionError. It can't lose its EXEC behaviour silently.
  • Scope: a shader with no EXEC write translates exactly as before (Gen.may_write_exec). --wave64-emulate is unchanged: its rounds split blocks at instruction boundaries, and Translator: elide dead SCC votes and fold safe lane-local execz regions under --wave64-emulate (#884) #1905 covers that mode. A single lane-local instruction keeps its select (MIN_ACTIVE_RUN = 2), because wrapping it measured no gain.
  • docs/design.md: one sentence in the EXEC row.

Results

RX 5700 XT (RDNA1), RADV Mesa 26.0.8, main f847748:

main this branch
Bench, 8 divergent if/else (native SPIR-V: 0.647 ms, 56 VGPRs, 18 waves) 0.851 ms, 80 VGPRs, 12 waves/SIMD, 2881 instructions 0.693 ms, 64 VGPRs, 16 waves/SIMD, 2145 instructions; bit-identical image
run_gpu_tests.py 173/199, 25 skipped, 1 failed 174/199, 25 skipped, 0 failed
same, LEONIDA_BARY_FALLBACK=1 LEONIDA_MESH_COMPUTE=1 LEONIDA_F16_INTERP_FALLBACK=1 183/199, 1 failed 184/199, 0 failed
build.py 224/224 translated and valid 224/224 (105 with runs)
Fuzzers: valu_fuzz f16/mem, lds_fuzz, crosslane_fuzz (wave32, and wave64 with --w64-native), 2,100 programs, fixed seeds logs identical to main line for line; every f16/mem/lds program has runs
unittest discover -s tests/semantic 7326 OK
pytest (on ec8ec4c, before the last rebase) 9361 passed, 90 skipped 9365 passed, 90 skipped

The one test that changes is cs_signed_zero_nan, from FAIL to PASS. Word 5 (0 × inf) is the Mesa NIR issue in #166's known failures. On RADV here it shows only through the per-write OpSelect(active, ...): the same program without its v_cmpx passes. This change avoids that pattern; it doesn't fix Mesa (llvmpipe still loses it with a literal 0).

New test: tests/semantic/test_active_runs.py runs a wave32 and a wave64 if/else over EXEC on every lane with spirv_wave_interp:

  • inactive lanes keep their values;
  • the result equals the per-write-select translation;
  • a shader without EXEC writes translates unchanged;
  • an instruction wrongly judged lane-local stops the translation.

Breaking the run's condition (all lanes enter) makes the value tests fail.

Not done: corpus counts. I don't have a work folder, so the translated and spirv-val counts over the 26,503 shaders before and after still need a run by someone who does.

AI-assisted (Claude Code): code, tests and this description. The tests ran on my own RX 5700 XT.

Tested 2645144 (on main f847748) on AMD Radeon RX 5700 XT (RDNA1, Navi 10), RADV Mesa 26.0.8-1ubuntu0.3, Vulkan 1.4.335, Ubuntu 26.04.1 (kernel 7.0.0-42), LLVM 21: pass

🤖 Generated with Claude Code

@TreXito

TreXito commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 26451441 (base f847748c) on NVIDIA GeForce RTX 3060 Ti (Ampere), driver 580.173.02 (Vulkan 1.4.312), Linux (Ubuntu 26.04 container on a TrueNAS 26.0 host, Python 3.14.4): pass, as a correctness check. I didn't measure speed.

  • tests/semantic/test_active_runs.py: 4 passed (4 subtests) on this head. With tools/ from f847748c it gives 6 failed, 2 passed: three find no MIN_ACTIVE_RUN, one no Gen.lane_local, and two count 0 runs where they expect 4.
  • scripts/run_gpu_tests.py, whole suite: 152/199 passed, 46 skipped, 1 failed, identical per test to main f847748c on this card. The failure is ps_sdwa_f16_const, the NVIDIA f16 denormal flush on main (rdna2spirv: widen f16 denormal operands without UnpackHalf2x16 (NVIDIA reads them as 0) #3220 / rdna2spirv: declare DenormPreserve 16 so a driver keeps f16 denormals (SHADER-526) #3040), not this PR.
  • Since this changes how every VALU write after an EXEC change is emitted, the relevant fixtures are the ones with real EXEC divergence. These all pass: cs_exec64, cs_exec_const, cs_mask_vgpr_exec, cs_barrier_divergent, cs_w32_mask_imm, cs_wqm_w32, cs_dpp_mask, ps_scc_mask32, ps_uniform_branch, ps_bitmask_perm, ps_wqm.
  • ctest on the Linux build (CMake + Ninja, GCC): 41/41 passed.
  • Not covered: the 64-wide fixtures skip on this card (subgroup 32 only), and none of the GPU fixtures has your 8-region if/else benchmark shape.

AI-assisted (Claude Code). Synthetic test shaders only, no game files.

@sds-228

sds-228 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 26451441 on NVIDIA RTX 3080 Laptop (Ampere), driver 621.24, Vulkan 1.4.357, Windows 11:

  • Full run_gpu_tests.py against a runtime built from current main: 153/199 passed, 46 skipped, 0 failed.
  • Translation: 204/204 shaders translated, 204/204 pass spirv-val.
  • The 199-case total is this branch's baseline; the newer integer sample_l fixture that makes other branches report 200 is not in its history, so no case is missing.

llvm-mc 21.1.8, SPIRV-Tools via WSL forwarders, Python 3.13. Synthetic fixtures only, no game data. AI-assisted (OpenCode).

@kotorvr

kotorvr commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 2645144 on NVIDIA GeForce RTX 4090 (Ada, subgroup 32..32), 621.14, Windows 11 (26200): pass. An NVIDIA result next to the PR's RX 5700 XT / RADV one.

Test programs: MSVC 19.44 RelWithDebInfo build of main f847748, the branch's base (the PR changes nothing under runtime/). llvm-mc 21.1.8, Vulkan SDK 1.4.363.0, Python 3.13.7.

main f847748 2645144
python -I scripts/run_gpu_tests.py 153/199 passed, 46 skipped, 0 failed 153/199 passed, 46 skipped, 0 failed
the same with LEONIDA_W64_EMULATE=1 184/199 passed, 15 skipped 184/199 passed, 15 skipped
python -m unittest discover -s tests/semantic 7326 tests OK (1 expected failure), 797 s
python -I tools/rdna2spirv.py <work>/shaders/dxbc/disasm --limit 200 --no-asm (my own work folder; counts only) 200 translated, 200 pass spirv-val 200 translated, 200 pass spirv-val
vk_pipeline_check <that slice> cache.bin --jobs 8 171 ms + 29 ps modules and pipelines ok the same: 200 of 200, 0 failed, 0 skipped

In the slice, 194 of the 218 .spv files differ from main's and the folder shrinks by 5.9% (60.56 MB to 56.99 MB), so the runs reach real shaders. All of them still compile to a pipeline on this driver. I am not quoting compile times: main's modules in that slice were already in the driver's own shader cache from an earlier run, so the two timings aren't comparable. Run-time speed was not measured here.

On the branch: tests/semantic/test_active_runs.py 4 OK, test_saveexec* 8 OK, test_value_semantics.py 34 OK; python -I -m compileall -q make_game.py tools tests exit 0.

Not run: the full corpus (26,503 shaders). No game data quoted. AI-assisted check (Claude Code).

@MKmajster

MKmajster commented Nov 22, 2026 •

Copy link
Copy Markdown

Tested 2645144 on NVIDIA GeForce RTX 3070 (Ampere, 32-wide subgroups), driver 617.42, Windows 11 24H2 (26100). No change in any result against main f847748, although the branch changes 86 of the 207 modules the GPU suite translates.

(Edited: the first version called this the first non-AMD run; TreXito, sds-228 and kotorvr had posted NVIDIA results a few minutes earlier. What this adds is the fuzzer comparison and the count of changed modules.)

Same runtime build (main f847748) for both, with only tools/ from the branch:

main this branch
run_gpu_tests.py 153/199, 46 skipped (wave64), 0 failed 153/199, 46 skipped, 0 failed; all 199 result lines identical (paths and heap bases aside)
translated .spv differing from main 86 of 207
valu_fuzz --family f16 --wave 32 --seed 7 --programs 150 150/150 match 150/150 match
valu_fuzz --family mem --wave 32 --seed 7 --programs 150 150/150 match 150/150 match
lds_fuzz --wave 32 --seed 7 --programs 150 (native) 150/150 match 150/150 match
lds_fuzz --emulate --seed 7 --programs 150 300/300 match 300/300 match
crosslane_fuzz --wave both --seed 7 --programs 150 w32 native 49 match / 5 mismatch / 21 hyp-only; w64 emulate 50 / 3 / 22 the same counts

cs_signed_zero_nan already passes on this NVIDIA driver on main, words 0x80000000 0x80000000 0x80008000 0x8000 0x1 0x1 0x1 0x7c00, so the RADV-only effect in the description doesn't show here either way. The crosslane mismatches are on main too, in the same numbers, so they predate this branch. Only the counts were compared there, not each program.

Not measured: performance on NVIDIA (the bench in the description isn't in the tree), and native wave64 (this GPU is 32-wide only).

No game files. AI-assisted (Claude).

With EXEC as a per-invocation flag, dst() writes every VGPR as OpSelect(active, new, old). Outside straight-line
code the driver can't prove the lane active, so both values stay live and each write costs a v_cndmask: a pixel
shader with 8 divergent if/else regions of fma/mul/add/min/max runs 32% slower than the same program written
natively (RX 5700 XT, RADV).

In a shader that may write EXEC, a run of two or more lane-local VALU instructions now goes inside one
OpSelectionMerge / OpBranchConditional %active and writes its VGPRs directly. Each invocation is one lane, so
skipping the run in an inactive lane keeps the old value of everything it writes, as the selects did; the driver
then predicates it with its own EXEC. Lane-local leaves out cross-lane reads (DPP, readlane, writelane, permlane,
mbcnt), lane-mask, SCC and EXEC writes (v_cmp*), carry and other scalar results written in every lane (_co_,
div_scale, mad_u64), v_interp, and masks passing through a VGPR. Inside a run, an instruction that emits anything
but VGPR stores (a vote, a barrier, an image op, a store to VCC/SCC/an SGPR, ...) fails the translation instead of
losing its EXEC behaviour. Shaders that never write EXEC translate as before, and --wave64-emulate is unchanged.

RX 5700 XT, that bench: 0.851 -> 0.693 ms (native 0.647), VGPRs 80 -> 64, 12 -> 16 waves, same image.
tests/semantic/test_active_runs.py runs a wave32 and a wave64 if/else on every lane.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Zombins2

Copy link
Copy Markdown

AI-assisted. No game files used.

AMD Windows-driver correctness report for 2645144 against parent f847748. Ryzen 5 7600X iGPU (RDNA2/gfx1036, AMD 26.10.1, subgroup 32..64), Windows 11 build 26200. Test programs built from the parent with MSVC 19.50 / Ninja Release; llvm-mc 23.1.6.

run_gpu_tests.py, whole suite, native and LEONIDA_W64_EMULATE=1:

mode PR: pass / fail / skip parent: pass / fail / skip
native 178 / 0 / 21 177 / 1 / 21
emulated 177 / 1 / 21 178 / 0 / 21

The only initial status differences were:

  • Parent/native cs_smem_sload_neg: failed with a device-line-only detail. Targeted reruns passed 5/5 on each revision, native.
  • PR/emulated cs_imgatomic3d: failed with "vk_store_test printed no uint words". Targeted reruns passed 10/10 per revision per mode (40/40 total, native plus emulated).

Byte comparisons found 86 native and 52 emulated .spv files changed among 218 common module files per mode. These are compiled-file counts, including files for skipped fixtures, rather than counts of executed passing tests. cs_imgatomic3d is changed in both modes.

The initial cs_imgatomic3d failure did not recur in those targeted runs, but its cause remains unresolved. The message and executable differ from the device-line symptom reported in TEST-388, so these results do not establish a shared cause or exclude a PR-dependent intermittent issue. Apart from the two initial failures above, all recorded statuses matched. No fresh all-green full-suite rerun or performance measurement is claimed.

@DaniSBR1

Copy link
Copy Markdown
Contributor Author

Rebased onto aa2988c. The block loop of translate() now sits inside #1905's fold loop. The runs apply where the loop translates a block's instructions. #1905's folded regions are built only under --wave64-emulate, where runs are off. On the RX 5700 XT the GPU suite matches main per test, except cs_signed_zero_nan, which passes: 185/210 and 195/210 with the RDNA1 fallbacks, 0 failed. 234/234 test shaders translate and validate.

Thanks to everyone who ran it, and to @kotorvr for the 200-shader corpus slice: 200/200 translated, validated and compiled to pipelines. That covers the corpus count I couldn't do.

@ItsNotRexed
ItsNotRexed merged commit a1e5ddb into ItsNotRexed:main Nov 23, 2026
3 of 10 checks passed
ItsNotRexed added a commit that referenced this pull request Nov 23, 2026
…ted VGPR pointers; test_ds_swizzle_b32 follows #4177's numeric offsets

The active-run check (#3549) accepted only stores through an inline %vgpr OpAccessChain, so with the register
pointers hoisted into %entry (#4440) every VGPR write in a run failed the assertion. #4402's test still expected
numeric ds_swizzle offsets to be refused; since #4177 (measured on gfx1036 for all 16,384 offsets) only
0xC000..0xFFFF is.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
gregoirener pushed a commit to gregoirener/Leonida-latest-backup-Monday-23-nov-Japanese-time- that referenced this pull request Nov 23, 2026
…index and permlane16 checks see the wave32-half shuffle and ItsNotRexed#3549's active runs

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
crastinator added a commit to crastinator/Leonida that referenced this pull request Nov 23, 2026
…class test

main's active-run check (ItsNotRexed#3549) rejects every OpFunctionCall inside an
if (active) run, so ps_mandelbrot failed on the v_sqrt_f32 helper call.
Each f32 helper body now passes check_lane_local when it is built and is
recorded in lane_local_fns; calls to those are allowed in a run, any
other call is still rejected.

test_undefined_spirv_is_its_own_class used v_cvt_pk_u8_f32 of NaN, which
ItsNotRexed#1129 made defined, and with the helpers no oracle op translates to
undefined SPIR-V any more. The test now hands check() an interpreter
that refuses the input.
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.

7 participants