Skip to content

tests/semantic: run the structured wave64 emulation in test_loop_ballot_barrier - #4808

Closed
LLLL-zzzz0 wants to merge 2 commits into
ItsNotRexed:mainfrom
LLLL-zzzz0:fix/semantic-structured-wave64-tests
Closed

LLLL-zzzz0 wants to merge 2 commits into
ItsNotRexed:mainfrom
LLLL-zzzz0:fix/semantic-structured-wave64-tests

Conversation

@LLLL-zzzz0

Copy link
Copy Markdown

Claimed in the coordination issue as: #4683 (comment)

What and why

tests/semantic is red on main at 98fa503: test_loop_ballot_barrier.py reports 3 failures and 2 errors.

This PR reads every function (its OpFunctionParameter ids and its entry label) and gives each invocation a call
stack: OpFunctionCall binds the arguments to the callee's parameters and jumps to its entry label,
OpReturnValue writes the result id and returns to the caller, and OpReturn inside a callee returns instead of
ending the invocation. The three control_flow assertions accept either emulation plan, as
tests/test_translate.py:920 already does; which plan is used is not the semantics under test.

The two tests that used to error now execute the emitted SPIR-V and compare every lane against the expected words
computed independently in the module, so the structured plan's output is checked instead of skipped.

Tested

  • python -m unittest tests.semantic.test_loop_ballot_barrier: 15/15 pass (was 3 failures + 2 errors), including
    the two that used to raise on OpFunctionCall.
  • python -m unittest discover -s tests/semantic with LEONIDA_REQUIRE_SPIRV_TOOLS=1: 6245 tests, one failure,
    test_fma_khr.test_spirv_tools_accept_it (Invalid capability 'FMAKHR'). That is the local SPIRV-Tools version
    (v2025.1; the GPU records use v2026.4) and it fails the same way with this patch stashed, so it is not caused by
    this change.
  • Reproduced the failures on a pristine worktree of 98fa503 with nothing on PATH but Windows and Python, and
    on Python 3.12.8 and 3.14, so they are not tool- or interpreter-version dependent.
  • No game files, GPU, shader corpus or runtime involved: only a test module changes. (For reference the machine
    also runs the GPU suite: 201/217 pass on an RTX 4070 SUPER, driver 617.42, and 197/217 on the Ryzen 7 9800X3D
    Radeon iGPU with LEONIDA_PC_TAIL_UBO=1, but nothing here touches the translator or the runtime.)
  • Windows 11 26100, Python 3.12.8 and 3.14.

AI-assisted: yes (DeepSeek Harness).

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a, the
    translator is unchanged)

@shdann

shdann commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

At e6a578aa, the interpreter keeps a function-local variable between two calls to the same helper. Each call must initialize its own local variable. The interpreter returns 2 for the second call. The RTX 4090 and the AMD Radeon iGPU both return 1 for the same module.

The helper initializes a local word to zero, increments it, and returns it. The caller invokes the helper twice and stores the second result. The following script builds the module with the PR test helper. Run it from the checkout root with python -I probe.py. It prints interpreter {0: 2}. To check the GPU output, run vk_compute_test.exe ../calls.spv 1 dump and read word 0.

import sys,re
from pathlib import Path
sys.path.insert(0,str(Path("tests/semantic").resolve()))
import test_loop_ballot_barrier as t
from spirv_asm import assemble
s,_=t._translate_cs("cs_call",["v_mov_b32_e32 v1, 42","buffer_store_dword v1, off, s[0:3], 0","s_endpgm"],threads=1)
s=s.replace("%main = OpFunction","%test_fn = OpTypeFunction %uint\n%main = OpFunction",1)
s=re.sub(r"OpStore (%\w+) %\w+ Aligned 4",r"%first = OpFunctionCall %uint %helper\n%second = OpFunctionCall %uint %helper\nOpStore \1 %second Aligned 4",s)
s+="""
%helper = OpFunction %uint None %test_fn
%start = OpLabel
%slot = OpVariable %_ptr_Function_uint Function %c0
%old = OpLoad %uint %slot
%new = OpIAdd %uint %old %c1
OpStore %slot %new
OpReturnValue %new
OpFunctionEnd
"""
print("interpreter",t.run_compute_workgroup(s,1,1,push_constants=t.VSHARP))
Path("../calls.spv").write_bytes(assemble(s))

All 15 tests in test_loop_ballot_barrier.py pass on this head. At the merge base, the interpreter rejects the function call. Please initialize the callee's Function variables at each call and add a two-call regression test.

AI-assisted.

Fix verified at 7ca617a. The original two-call probe now returns 1 in the interpreter. The same module returns 1 on the RTX 4090 and the Radeon iGPU. All 16 tests in test_loop_ballot_barrier.py pass on Windows 11 with CPython 3.14.7.

@Atomic258

Copy link
Copy Markdown
Contributor

Tested e6a578aa on Windows 11, CPython 3.15.2, CPU only (no GPU, no game files): pass.

Command: python -I -m unittest discover -s tests/semantic -p test_loop_ballot_barrier.py -v

Result: 15/15 tests OK in 1.7 s, including the two tests that previously errored on OpFunctionCall (structured --wave64-emulate outlined helpers). No LEONIDA_W64_EMULATE env needed; the module exercises both emulation plans directly.

Red control: git checkout origin/main -- tests/semantic/test_loop_ballot_barrier.py → same command gives FAILED (failures=3, errors=2), e.g. AssertionError: 'emulated wave64 structured' != 'emulated wave64 rounds' — matches the PR description exactly. Restored the PR file; git status --porcelain clean.

Note: fetched via git fetch ... pull/4808/head + detached checkout of the SHA (local branch pr4808 name was already checked out in another worktree).

AI-assisted (Devin); no game files.

LLLL-zzzz0 added a commit to LLLL-zzzz0/Leonida that referenced this pull request Nov 23, 2026
shdann (PR ItsNotRexed#4808): the interpreter kept a helper's Function-storage variable between two calls to
the same helper, so a helper that starts from 0, increments its local and returns it answered 1 and
then 2, where the GPU answers 1 and 1. A Function-storage variable belongs to one call, not to the
invocation.

The parser records each function's Function-storage variables (fn_locals), and OpFunctionCall resets
exactly the callee's to their initial values before it binds the parameters and jumps; the main
function's locals still start once per invocation.

Regression test: FunctionCallTests.test_callee_function_variables_are_initialized_on_every_call calls
the same helper twice through the translator's output; it fails with 2 != 1 at the previous commit and
passes now. shdann's probe prints interpreter {0: 1}, vk_compute_test calls.spv 1 dump prints
out[0] = 1 (the GPU's answer) on an RTX 4070 SUPER with driver 617.42, and test_loop_ballot_barrier
is 16/16.

Full discover -s tests/semantic (LEONIDA_REQUIRE_SPIRV_TOOLS=1): 6246 tests, one failure,
test_fma_khr.test_spirv_tools_accept_it, which fails the same way with this change stashed (the local
SPIRV-Tools v2025.1 has no FMAKHR capability; the GPU records use v2026.4).

AI-assisted (DeepSeek Harness); verified on Windows 11, Python 3.12.8.
@LLLL-zzzz0

Copy link
Copy Markdown
Author

Fixed in 7ca617a. A Function-storage variable now starts a new call instead of living for the whole
invocation:

  • the parser records each function's Function-storage variables (fn_locals);
  • OpFunctionCall resets exactly the callee's to their initial values before it binds the parameters
    and jumps. The main function's locals still start once per invocation.

FunctionCallTests.test_callee_function_variables_are_initialized_on_every_call splices the same
two-call helper into the translator's output: at e6a578aa it fails with 2 != 1, at 7ca617a3 it
passes.

Checked on this machine:

  • your probe: interpreter {0: 1};
  • vk_compute_test calls.spv 1 dump on an RTX 4070 SUPER (driver 617.42, Windows 11):
    out[0..7] = 1 4008636142 ..., so the interpreter and the GPU agree on this module;
  • test_loop_ballot_barrier: 16/16;
  • discover -s tests/semantic with LEONIDA_REQUIRE_SPIRV_TOOLS=1: 6246 tests, only
    test_fma_khr.test_spirv_tools_accept_it fails, and it fails the same way with this change stashed
    (the local SPIRV-Tools v2025.1 has no FMAKHR capability; the GPU records use v2026.4).

AI-assisted.

@KkelebekK

Copy link
Copy Markdown

Tested 7ca617a on AMD Radeon 780M (RADV PHOENIX), Mesa 26.2.6, Linux (CachyOS, kernel 7.3.4): pass
Tested 7ca617a on NVIDIA GeForce RTX 4070 Laptop GPU, driver 620.70.06, Linux (CachyOS, kernel 7.3.4): pass

Test programs built from the same commit (7ca617a, based on 98fa503).

python -I -m unittest discover -s tests/semantic -p test_loop_ballot_barrier.py:

  • 7ca617a: Ran 16 tests, OK
  • main af70fc7: Ran 15 tests, FAILED (failures=3, errors=2), all in the emulate paths (Unsupported SPIR-V op OpFunctionCall in the interpreter, and 'emulated wave64 structured' != 'emulated wave64 rounds'), as the description says

python -I scripts/run_gpu_tests.py --build runtime/build/linux (221 shaders: 221 translated, 221 pass spirv-val; no FAIL in any run):

  • Radeon 780M (subgroup 32..64): 202/217 passed, 15 skipped (the "run by d3d12_*" ones)
  • RTX 4070 Laptop (subgroup 32..32), plain: 170/217 passed, 47 skipped (those 15 plus the wave64 tests, as documented)
  • RTX 4070 Laptop, LEONIDA_W64_EMULATE=1 for build and check: 201/217 passed, 16 skipped (the 15 plus ps_w64x_readlane: no 64-wide fragment subgroup)

The PR only changes a Python test, so the GPU runs show nothing else moved; they do not exercise the change itself.

Tools: llvm-mc 23.1.4, SPIRV-Tools v2026.4, CMake 4.5.0, GCC 16.2.1, Python 3.14, Vulkan loader 1.4.363, Vulkan-Headers 363 (-DLEONIDA_VULKAN_HEADERS_FETCH=ON).

@tupacfan04

Copy link
Copy Markdown
Contributor

APPROVE. The interpreter change is narrowly scoped to OpFunctionCall/OpReturnValue/OpReturn, and it fixes a real red suite:

  • head 7ca617a3: tests/semantic/test_loop_ballot_barrier.py 16/16 OK; compileall clean; merge clean vs origin/main (ec34b54f).
  • negative control: the same file on origin/main still fails 3 tests + 2 errors, so the new test and the parser support are load-bearing.
  • The docs/expectation updates are correct: structured emulation legitimately reports emulated wave64 structured, and accepting both values at test_loop_ballot_barrier.py:1081,1401,1474 is the right assertion.

I checked the new call frame handling: parameters bind to fn_params[fid], Function locals are reinitialized per call, nested calls push/pop (return block, return ip, result), and OpReturn/OpReturnValue restore the caller. That is enough for the outlined helpers and does not change the existing single-function path.

No blocking issues.

AI-assisted review, no game files, no GPU.

@AwkGrep256

Copy link
Copy Markdown
Contributor

Tested 7ca617a on Linux (CachyOS, kernel 7.3.5, Python 3.14.10). python -m unittest tests.semantic.test_loop_ballot_barrier gives OK (15 tests); on main (ec34b54f) it gives 3 failures and 2 errors, as the PR says. The change only touches the test module. AI-assisted (Claude Code).

…ot_barrier

4d9ef4e (ItsNotRexed#884) added the structured --wave64-emulate plan: rdna2spirv now reports
"emulated wave64 structured" and outlines lane-mask setup into helper functions that main calls
with OpFunctionCall. This module still expected "emulated wave64 rounds" and its multi-invocation
interpreter parsed only %main and raised on OpFunctionCall, so the semantic suite was red on main
(3 failures + 2 errors, independent of Python version and without any external tool on PATH).

Parse every function and its OpFunctionParameter ids and give each invocation a call stack:
OpFunctionCall binds the arguments to the callee's parameters and jumps to its entry label,
OpReturnValue writes the result id and returns to the caller, and OpReturn inside a callee returns
instead of ending the invocation. The three control_flow assertions accept either emulation plan,
as tests/test_translate.py already does; which plan is used is not the semantics under test.

The two tests that used to error now execute the emitted SPIR-V and compare every lane with the
independently computed expected words, so the structured plan's output is checked rather than
skipped. test_loop_ballot_barrier: 15/15. Full discover: 6245 tests, only test_fma_khr fails,
and it fails the same way with this patch stashed: the local SPIRV-Tools v2025.1 predates the
FMAKHR capability (the GPU records use v2026.4).

AI-assisted (DeepSeek Harness); verified on Windows 11, Python 3.12 and 3.14.
shdann (PR ItsNotRexed#4808): the interpreter kept a helper's Function-storage variable between two calls to
the same helper, so a helper that starts from 0, increments its local and returns it answered 1 and
then 2, where the GPU answers 1 and 1. A Function-storage variable belongs to one call, not to the
invocation.

The parser records each function's Function-storage variables (fn_locals), and OpFunctionCall resets
exactly the callee's to their initial values before it binds the parameters and jumps; the main
function's locals still start once per invocation.

Regression test: FunctionCallTests.test_callee_function_variables_are_initialized_on_every_call calls
the same helper twice through the translator's output; it fails with 2 != 1 at the previous commit and
passes now. shdann's probe prints interpreter {0: 1}, vk_compute_test calls.spv 1 dump prints
out[0] = 1 (the GPU's answer) on an RTX 4070 SUPER with driver 617.42, and test_loop_ballot_barrier
is 16/16.

Full discover -s tests/semantic (LEONIDA_REQUIRE_SPIRV_TOOLS=1): 6246 tests, one failure,
test_fma_khr.test_spirv_tools_accept_it, which fails the same way with this change stashed (the local
SPIRV-Tools v2025.1 has no FMAKHR capability; the GPU records use v2026.4).

AI-assisted (DeepSeek Harness); verified on Windows 11, Python 3.12.8.
@LLLL-zzzz0
LLLL-zzzz0 force-pushed the fix/semantic-structured-wave64-tests branch from 7ca617a to cc54082 Compare November 23, 2026 06:20
@LLLL-zzzz0

Copy link
Copy Markdown
Author

Rebased onto d974c88a: test_loop_ballot_barrier is 16/16 here, and the same file still gives 3 failures + 2 errors on d974c88a itself, so the seven merge commits since 98fa503 did not change the red.

Overlap, which I should have checked before opening this. #3995 (Vonsek, opened ~25 h earlier) fixes the same five cases, and #3953 (closed) did it with call frames as well. I searched the coordination issue's tables but not the open pull requests that touch this file, so this is a third implementation. Sorry for the extra triage.

How the two open ones differ:

  • tests: test_loop_ballot_barrier follows #2089's structured wave64 emulation (main red since 1cc7497b) #3995 inlines the single-block helpers at parse time (OpFunctionCall → inline body, OpReturnValue → OpCopyObject) and adds _translate_cs(structured=False), so the --dispatch-loop round path is checked explicitly next to the structured plan.
  • Here the interpreter gets a real call frame: several blocks per callee, OpFunctionCall binds the parameters and pushes (return block, return ip, result), OpReturnValue writes the result back, and the callee's Function-storage variables are initialized on every call (from shdann's review above, with a two-call regression test). The three control_flow assertions accept either plan.

One consequence matters if #3995 lands unchanged: inlining a helper that declares a Function variable shares it across calls inside the caller, which is the same 2-vs-1 mismatch shdann found here, so that path would need the per-call initialization anyway.

Your call on which one to take:

No game data. AI-assisted (DeepSeek Harness).

@SweatierKey

Copy link
Copy Markdown
Contributor

Independent CPU + SPIRV-Tools verification on Linux (Python 3.13.5, spirv-as/spirv-val with LEONIDA_REQUIRE_SPIRV_TOOLS=1, head cc54082d, base main 3faac557):

  • Before (main 3faac557): python3 -I tests/semantic/test_loop_ballot_barrier.py fails (15 tests: 3 failures, 2 errors).
  • After (cc54082d):
    • python3 -I -m py_compile tests/semantic/test_loop_ballot_barrier.py — clean.
    • python3 -I tests/semantic/test_loop_ballot_barrier.py -v — 16/16 OK (1.343s), including test_callee_function_variables_are_initialized_on_every_call.
    • git diff --check origin/main...HEAD — clean.
  • Diff review: cleanly scoped OpFunctionCall/OpReturnValue/OpReturn support in the multi-invocation interpreter, properly re-initializing callee Function-storage locals per call and verifying it with a dedicated regression test.

Looks good to merge.

@abilliontokens

Copy link
Copy Markdown
Contributor

Tested cc54082d on AMD Radeon RX 9060 XT (RDNA4, AMDVLK 32.0.32029.2010, subgroup 32..64, Windows 11): pass (CPU-only change).

  • python -I -m unittest discover -s tests/semantic -p "test_loop_ballot_barrier.py": 16 tests OK (was 3 failures + 2 errors on main 98fa503).
  • python -I -m compileall -q tests tools: clean.
  • No GPU test needed: the change fixes the structured --wave64-emulate interpreter plan's per-call local initialization; the test executes emitted SPIR-V against expected words on CPU.

@Roberto0953

Copy link
Copy Markdown
Contributor

Tested cc54082d merged into main 3faac557 (clean merge) on Windows 11, Python 3.13, spirv-as/spirv-val on PATH, CPU only: pass.

python -m unittest discover -s tests/semantic -p test_loop_ballot_barrier.py:

Tree Result
main 3faac557 Ran 15 tests: FAILED (failures=3, errors=2)
main + this PR Ran 16 tests: OK

Context from the same machine: the whole semantic suite on main (Ran 6245 tests) fails only in this file, so this file is what keeps main red here.

Since three open PRs fix the same file, I ran all three the same way, in case it helps choose one:

PR Result merged into main Skips or expected failures added by the diff
#3995 Ran 15 tests: OK none
#4808 Ran 16 tests: OK none
#5024 Ran 16 tests: OK none

Each PR changes only tests/semantic/test_loop_ballot_barrier.py, so I did not rerun the rest of the suite. I compared results, not the designs.

AI-assisted (Claude Code); run on this machine, no game files.

@memorypressure

Copy link
Copy Markdown

Verified against main 3faac55: head cc54082 merges cleanly and test_loop_ballot_barrier.py passes 16/16, covering the structured wave64 emulation and per-call Function-variable init; the neighbouring wave64-emulate, ballot-votes and barrier-control suites stay green. (test_wave64_subgroup_ops failing under python -I is the known import-support wrinkle, identical on main.) Heads-up: long-open #4152 reworks the same file substantially (+161/-48 versus your +113/-14) — the two should be compared before either merges. No CI checks are reported on the branch. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

@Atomic258

Copy link
Copy Markdown
Contributor

Tested cc54082 on Windows Server 2022, CPython 3.13.12: pass

  • python -I -m unittest discover -s tests/semantic -p "test_loop_ballot_barrier.py" -> OK
  • Notably: the main-branch version of this suite fails on this box (named 'emulated wave64 structured' / OpFunctionCall issues) - the reworked cases here fix that breakage.

git diff --check clean; python -m compileall -q exit 0. No game files, no GPU. AI-assisted (Devin).

@LLLL-zzzz0

Copy link
Copy Markdown
Author

Closing this in favour of #3995 (merged at 11:36Z today, which fixes the five red cases on main). #5024 is the remaining candidate for this file.

The part of this PR that is still missing on main after #3995 is documented in #5024 instead: a helper that declares a Function-storage variable raises Unsupported SPIR-V op OpVariable in the merged inliner (shdann's two-call module), and the inliner does not reserve ids the caller already uses (shdann's other probe, %saved 1 vs %call1_x 2). shdann's per-call requirement from this PR is not covered by the merged code; I left the ~20-line shape for it there.

Nothing here needs to be taken from this branch. No game data. AI-assisted (DeepSeek Harness).

@LLLL-zzzz0 LLLL-zzzz0 closed this Nov 23, 2026
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.

10 participants