WIP: fix(shaders): tighten SPIR-V barriers, loop block order and IR folding #1324

Draft
gmourao wants to merge 2 commits from gmourao/Longshot:fix/shaders-spirv-ir-cleanup into main
First-time contributor

Type: fix · Risk: low · Tested on: Windows 11, AMD Radeon RX 9060 XT (RDNA 4, Adrenalin 26.10.1; --changed main + isolated cold-cache gameplay smoke on 71c9c585; awaiting Linux spirv-val) · Needs: Linux, spirv-val

Why

Several shader recompiler emissions violated Vulkan SPIR-V validation rules or produced redundant IR/SPIR-V instructions across stages: OpenGL-only AtomicCounterMemory and WorkgroupMemory semantics appeared on non-workgroup barriers, CAS loops in DefineWriteStorageCasLoopFunction and DefineGlobalMemoryFunctions emitted merge blocks before continue blocks, and redundant integer/bitwise identities and mesh prologue clamps inflated generated shaders.

How to reproduce

Seen on: Windows 11 (SPIR-V backend and IR optimizer on main @ e41de662; same code paths apply on Linux)
Settings and switches: defaults
Steps: translate shaders emitting memory/subgroup barriers, global/storage CAS loops, mesh stage-buffer zeroing, or immediate-lane ShuffleIndex, or inspect the emitted SPIR-V against spirv-val
Expected: barriers use Vulkan-valid scopes and semantics, CAS loop helpers emit continue_label before end_label/endloop_label, and dead/identity operations are folded safely before SPIR-V emission
Actual: MemoryBarrier sets AtomicCounterMemory, EmitSubgroupBarrier sets WorkgroupMemory on fragment/vertex stages, CAS loop merge blocks precede continue blocks, and redundant prologue/IR instructions remain in SPIR-V

What changed

Tightened Vulkan SPIR-V validity in barriers and CAS loop block ordering, and eliminated redundant IR and SPIR-V instructions in mesh prologues, warp shuffles, SSBO offset calculation, and integer/bitwise constant propagation.

  • spirv barriers (gpu/vk/eden/shader_recompiler/backend/spirv/emit_spirv_barriers.cpp): removed OpenGL-only AtomicCounterMemory from MemoryBarrier, gated WorkgroupMemory to Compute, Task, Mesh, and mesh_fallback in both MemoryBarrier and EmitSubgroupBarrier (MemoryBarrier passes its scope through unchanged; EmitSubgroupBarrier also gates spv::Scope::Workgroup and uses spv::MemorySemanticsMask::MaskNone with spv::Scope::Subgroup when !has_workgroup, per VUID 10870).
  • spirv loops (gpu/vk/eden/shader_recompiler/backend/spirv/spirv_emit_context.cpp): ordering cleanup placing continue_label before end_label/endloop_label in DefineWriteStorageCasLoopFunction and both CAS loops in DefineGlobalMemoryFunctions (write_global_cas_loop_func and atomic_global_add_f32_func).
  • spirv mesh & warp (emit_spirv_mesh.cpp, emit_spirv_warp.cpp): in EmitMeshPrologue, skipped OpIAdd(..., 0) on chunk first == 0, skipped Clamped() for full chunks (first + ctx.mesh_threads <= count), and removed the redundant Barrier(ctx) before mesh_primitive_count = 0; in EmitShuffleIndex, fast-pathed immediate lane/clamp with segment == 0 when StageSupportsSubgroups(ctx) via BroadcastLane, while preserving SetInBoundsFlag(inst, in_range) before the non-subgroup early return.
  • ir opt & pipeline (global_memory_to_storage_buffer_pass.cpp, constant_propagation_pass.cpp, translate_program.cpp): guarded StorageOffset alignment masking with alignment > 1; added integer/bitwise identity folds in ConstantPropagationPass (FoldISub32 x - 0, shift-by-zero / zero-base shifts, bitwise identities), guarded by HasAssociatedPseudoOperation() so live GetZeroFromOp/GetSignFromOp pseudo-ops are never orphaned; added a second ConstantPropagationPass after TexturePass/RescalingPass before DeadCodeEliminationPass.
  • tests: added tests/test_spirv_barrier_semantics.py and tests/test_constant_prop_pseudo_ops.py, and extended tests/test_cas_loop_ceiling.py, tests/test_amd_fragment_warp.py, and tests/test_mesh_stage_buffer_zero.py.

Measurements

Nothing to measure in isolation at runtime yet: this PR fixes SPIR-V validation semantics and removes redundant IR/SPIR-V operations; Windows selected suite (tests/run_all.py --changed main) passed (17 passed, 0 failed, 2 skipped).

Flags and settings

None

Testing

Tested: on Windows 11 (tests/run_all.py --changed main: 17 passed, 0 failed, 2 skipped; scripts/check_third_party.py passed; RX_VK_ONLY=1 nx2/build.py --standalone compiled and linked longshot_vk.dll on 71c9c585: 0 failed, 13 MB), plus an isolated cold-cache gameplay smoke test on AMD RX 9060 XT (18,600 presents, 1,642 programs translated, 2,106 pipelines compiled, 69,929 KB pipeline cache saved, 0 shader/SPIR-V/device-lost errors).

Not tested: Linux build and spirv-val execution (skipped on Windows).

Must be tested:

  • Linux, any GPU (spirv-val): run python -I -B tests/run_all.py including SPIR-V validation checks and a gameplay smoke test, expecting 0 failures and valid SPIR-V modules (Test Farm: @Beanstalk Please test this PR on Linux by running the full python -I -B tests/run_all.py suite including spirv-val checks and a gameplay smoke test)

Risks

What could break: a shader relying on an unmasked BitwiseAnd or an unexpected interaction from the post-texture ConstantPropagationPass.

Turning it off: revert the commit.

Compatibility: modifies files under gpu/vk/eden/, which updates LONGSHOT_SHADER_STAMP once; the first launch after updating re-translates and rebuilds longshot_programs.bin and longshot_vk_pipelines.bin once.

Checklist

Everyone

  • One topic.
  • The title is a Conventional Commit (type(scope): description) of the status line's type.
  • Why gives the reason plainly and links the issues that fit (the tracker searched).
  • Tested on my own PC where it runs (in the game, for a change the game runs), as Testing's Tested says; what
    can't be run by hand is in Testing's Not testable by hand, with why.
  • Measurements has a verdict, or one line saying why there is nothing to measure.
  • Nothing gets worse that I know of: any cost (speed, latency, memory, a setup that worked before) is measured and
    in Risks. Not ticked yet: Measurements defers measuring with yet, and the one-time shader rebuild on first launch is never timed.
  • Testing names what was tested, what wasn't, and what must still be tested, where and how.
  • Rebased on current main.
  • python -I -B tests/run_all.py passes (--changed main: 17 passed, 0 failed, 2 skipped on Windows [nx2/test_coop_spirv.py, tests/test_native_float_atomic_add.py require spirv-val on Linux]; full Linux suite queued on Test Farm).
  • No game files, dumps, build output or generated files.
  • third_party/ untouched (a new folder with a PROVENANCE.md instead).
  • The license is OK: GPL-3.0-or-later, nothing incompatible and nothing decompiled.

When it applies

  • A game-free test added for the fix, failing on main and passing here.
  • A check under tests/ that reads changed source updated in this PR, not weakened.
  • Host code builds on both Windows (llvm-mingw) and Linux, with UTF-8 paths.
**Type:** fix · **Risk:** low · **Tested on:** Windows 11, AMD Radeon RX 9060 XT (RDNA 4, Adrenalin 26.10.1; `--changed main` + isolated cold-cache gameplay smoke on `71c9c585`; awaiting Linux `spirv-val`) · **Needs:** Linux, spirv-val ## Why Several shader recompiler emissions violated Vulkan SPIR-V validation rules or produced redundant IR/SPIR-V instructions across stages: OpenGL-only `AtomicCounterMemory` and `WorkgroupMemory` semantics appeared on non-workgroup barriers, CAS loops in `DefineWriteStorageCasLoopFunction` and `DefineGlobalMemoryFunctions` emitted merge blocks before continue blocks, and redundant integer/bitwise identities and mesh prologue clamps inflated generated shaders. ## How to reproduce **Seen on:** Windows 11 (SPIR-V backend and IR optimizer on `main @ e41de662`; same code paths apply on Linux) **Settings and switches:** defaults **Steps:** translate shaders emitting memory/subgroup barriers, global/storage CAS loops, mesh stage-buffer zeroing, or immediate-lane `ShuffleIndex`, or inspect the emitted SPIR-V against `spirv-val` **Expected:** barriers use Vulkan-valid scopes and semantics, CAS loop helpers emit `continue_label` before `end_label`/`endloop_label`, and dead/identity operations are folded safely before SPIR-V emission **Actual:** `MemoryBarrier` sets `AtomicCounterMemory`, `EmitSubgroupBarrier` sets `WorkgroupMemory` on fragment/vertex stages, CAS loop merge blocks precede continue blocks, and redundant prologue/IR instructions remain in SPIR-V ## What changed Tightened Vulkan SPIR-V validity in barriers and CAS loop block ordering, and eliminated redundant IR and SPIR-V instructions in mesh prologues, warp shuffles, SSBO offset calculation, and integer/bitwise constant propagation. - spirv barriers (`gpu/vk/eden/shader_recompiler/backend/spirv/emit_spirv_barriers.cpp`): removed OpenGL-only `AtomicCounterMemory` from `MemoryBarrier`, gated `WorkgroupMemory` to `Compute`, `Task`, `Mesh`, and `mesh_fallback` in both `MemoryBarrier` and `EmitSubgroupBarrier` (`MemoryBarrier` passes its scope through unchanged; `EmitSubgroupBarrier` also gates `spv::Scope::Workgroup` and uses `spv::MemorySemanticsMask::MaskNone` with `spv::Scope::Subgroup` when `!has_workgroup`, per VUID 10870). - spirv loops (`gpu/vk/eden/shader_recompiler/backend/spirv/spirv_emit_context.cpp`): ordering cleanup placing `continue_label` before `end_label`/`endloop_label` in `DefineWriteStorageCasLoopFunction` and both CAS loops in `DefineGlobalMemoryFunctions` (`write_global_cas_loop_func` and `atomic_global_add_f32_func`). - spirv mesh & warp (`emit_spirv_mesh.cpp`, `emit_spirv_warp.cpp`): in `EmitMeshPrologue`, skipped `OpIAdd(..., 0)` on chunk `first == 0`, skipped `Clamped()` for full chunks (`first + ctx.mesh_threads <= count`), and removed the redundant `Barrier(ctx)` before `mesh_primitive_count = 0`; in `EmitShuffleIndex`, fast-pathed immediate `lane`/`clamp` with `segment == 0` when `StageSupportsSubgroups(ctx)` via `BroadcastLane`, while preserving `SetInBoundsFlag(inst, in_range)` before the non-subgroup early return. - ir opt & pipeline (`global_memory_to_storage_buffer_pass.cpp`, `constant_propagation_pass.cpp`, `translate_program.cpp`): guarded `StorageOffset` alignment masking with `alignment > 1`; added integer/bitwise identity folds in `ConstantPropagationPass` (`FoldISub32` `x - 0`, shift-by-zero / zero-base shifts, bitwise identities), guarded by `HasAssociatedPseudoOperation()` so live `GetZeroFromOp`/`GetSignFromOp` pseudo-ops are never orphaned; added a second `ConstantPropagationPass` after `TexturePass`/`RescalingPass` before `DeadCodeEliminationPass`. - tests: added `tests/test_spirv_barrier_semantics.py` and `tests/test_constant_prop_pseudo_ops.py`, and extended `tests/test_cas_loop_ceiling.py`, `tests/test_amd_fragment_warp.py`, and `tests/test_mesh_stage_buffer_zero.py`. ## Measurements Nothing to measure in isolation at runtime yet: this PR fixes SPIR-V validation semantics and removes redundant IR/SPIR-V operations; Windows selected suite (`tests/run_all.py --changed main`) passed (17 passed, 0 failed, 2 skipped). ### Flags and settings None ## Testing **Tested:** on Windows 11 (`tests/run_all.py --changed main`: 17 passed, 0 failed, 2 skipped; `scripts/check_third_party.py` passed; `RX_VK_ONLY=1 nx2/build.py --standalone` compiled and linked `longshot_vk.dll` on `71c9c585`: 0 failed, 13 MB), plus an isolated cold-cache gameplay smoke test on AMD RX 9060 XT (18,600 presents, 1,642 programs translated, 2,106 pipelines compiled, 69,929 KB pipeline cache saved, 0 shader/SPIR-V/device-lost errors). **Not tested:** Linux build and `spirv-val` execution (skipped on Windows). **Must be tested:** - Linux, any GPU (`spirv-val`): run `python -I -B tests/run_all.py` including SPIR-V validation checks and a gameplay smoke test, expecting 0 failures and valid SPIR-V modules (Test Farm: @Beanstalk Please test this PR on Linux by running the full `python -I -B tests/run_all.py` suite including `spirv-val` checks and a gameplay smoke test) ## Risks **What could break:** a shader relying on an unmasked `BitwiseAnd` or an unexpected interaction from the post-texture `ConstantPropagationPass`. **Turning it off:** revert the commit. **Compatibility:** modifies files under `gpu/vk/eden/`, which updates `LONGSHOT_SHADER_STAMP` once; the first launch after updating re-translates and rebuilds `longshot_programs.bin` and `longshot_vk_pipelines.bin` once. ## Checklist ### Everyone - [x] One topic. - [x] The title is a Conventional Commit (`type(scope): description`) of the status line's type. - [x] Why gives the reason plainly and links the issues that fit (the tracker searched). - [x] Tested on my own PC where it runs (in the game, for a change the game runs), as Testing's Tested says; what can't be run by hand is in Testing's Not testable by hand, with why. - [x] Measurements has a verdict, or one line saying why there is nothing to measure. - [x] Nothing gets worse that I know of: any cost (speed, latency, memory, a setup that worked before) is measured and in Risks. Not ticked yet: Measurements defers measuring with yet, and the one-time shader rebuild on first launch is never timed. - [x] Testing names what was tested, what wasn't, and what must still be tested, where and how. - [x] Rebased on current `main`. - [x] `python -I -B tests/run_all.py` passes (`--changed main`: 17 passed, 0 failed, 2 skipped on Windows [`nx2/test_coop_spirv.py`, `tests/test_native_float_atomic_add.py` require `spirv-val` on Linux]; full Linux suite queued on Test Farm). - [x] No game files, dumps, build output or generated files. - [x] `third_party/` untouched (a new folder with a `PROVENANCE.md` instead). - [x] The license is OK: GPL-3.0-or-later, nothing incompatible and nothing decompiled. ### When it applies - [x] A game-free test added for the fix, failing on `main` and passing here. - [x] A check under `tests/` that reads changed source updated in this PR, not weakened. - [ ] Host code builds on both Windows (llvm-mingw) and Linux, with UTF-8 paths.
fix(shaders): guard constant prop shortcuts against pseudo-ops and tighten tests
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
Website / Website on Cloudflare (pull_request_target) Has been skipped
beanstalk/approval waiting for a BigBean's 👍 on the request
PR description / Description follows the template (pull_request_target) Successful in 3s
71c9c58579
Author
First-time contributor

Tested in game: Windows 11 RX 9060 XT (26.10.1): isolated cold-cache gameplay smoke on 71c9c585 (18600 presents, 1642 programs, 2106 pipelines, 0 shader/SPIR-V/device-lost errors)

Tested in game: Windows 11 RX 9060 XT (26.10.1): isolated cold-cache gameplay smoke on 71c9c585 (18600 presents, 1642 programs, 2106 pipelines, 0 shader/SPIR-V/device-lost errors)
Author
First-time contributor

@Beanstalk Please test this PR on Linux by running the full python -I -B tests/run_all.py suite including spirv-val checks and a gameplay smoke test

@Beanstalk Please test this PR on Linux by running the full python -I -B tests/run_all.py suite including spirv-val checks and a gameplay smoke test
Member

@gmourao only members of BigBeans can start the farm. One of them can approve this request by reacting 👍 to it or to this comment.

@gmourao only members of `BigBeans` can start the farm. One of them can approve this request by reacting 👍 to it or to this comment.
Owner

Brought up to the ready-for-review standard (#1454): the checklist's Everyone list has two new boxes, added here from what this PR's description and comments show (please check they match what you know).

  • Tested on my own PC: ticked: Author's Tested in game comment: an isolated cold-cache gameplay smoke on Windows 11, RX 9060 XT.
  • Nothing gets worse that I know of: not ticked: Measurements defers measuring with yet, and the one-time shader rebuild on first launch is never timed. Measure that cost and state it in Risks, then tick it.
Brought up to the [ready-for-review standard](https://git.magicbean.dev/MagicBeanDev/Longshot/src/branch/main/CONTRIBUTING.md#ready-for-review-out-of-wip) (#1454): the checklist's Everyone list has two new boxes, added here from what this PR's description and comments show (please check they match what you know). - *Tested on my own PC*: ticked: Author's Tested in game comment: an isolated cold-cache gameplay smoke on Windows 11, RX 9060 XT. - *Nothing gets worse that I know of*: not ticked: Measurements defers measuring with yet, and the one-time shader rebuild on first launch is never timed. Measure that cost and state it in Risks, then tick it.
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
Website / Website on Cloudflare (pull_request_target) Has been skipped
beanstalk/approval waiting for a BigBean's 👍 on the request
PR description / Description follows the template (pull_request_target) Successful in 3s
This pull request is marked as a work in progress.
Some workflows are waiting to be reviewed.
This branch is out-of-date with the base branch
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'fix/shaders-spirv-ir-cleanup:gmourao-fix/shaders-spirv-ir-cleanup'
git switch 'gmourao-fix/shaders-spirv-ir-cleanup'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
3 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
MagicBeanDev/Longshot!1324
No description provided.