Skip to content

fix(translator): guard OpEmitVertex by OutputVertices in spirv_gs_to_compute - #3326

Open
evilclanker9000 wants to merge 1 commit into
ItsNotRexed:mainfrom
evilclanker9000:ag/gsc-maxverts-bounds
Open

evilclanker9000 wants to merge 1 commit into
ItsNotRexed:mainfrom
evilclanker9000:ag/gsc-maxverts-bounds

Conversation

@evilclanker9000

Copy link
Copy Markdown
Contributor

Claimed in coordination issue #3232 as EGEN-GSC-MAXVERTS-BOUNDS (finding: #3232 (comment)).

What and why

In tools/spirv_gs_to_compute.py, OpEmitVertex previously lowered inline to unconditional stores into %gsc_rec[(p * max_verts + n) * nout + i] and %gsc_meta[p * (max_verts + 1) + 1 + n] followed by %gsc_n = n + 1, with no check that n < max_verts (OutputVertices).

If a geometry shader emits more than OutputVertices vertices for primitive p, those excess emits silently overwrite primitive p + 1's vertex records and meta count slot (p + 1) * (max_verts + 1) in the shared storage buffers, and leave %gsc_n > max_verts for OpEndPrimitive and OpReturn.

Fix: When OpEmitVertex is present, generate a %gsc_emit helper function taking (%gsc_recbase, %gsc_metabase) that checks %gsc_n < max_verts (OpULessThan) before writing %gsc_rec / %gsc_meta and incrementing %gsc_n. Each OpEmitVertex in %main lowers to a single OpFunctionCall to %gsc_emit. Keeping %gsc_n in [0, max_verts] keeps OpEndPrimitive (meta[metabase + n]) and OpReturn (meta[metabase] = n) within primitive p's allocation as well.

Tested

  • Before fix (against upstream/main f847748c):
    pytest tests/test_spirv_gs_to_compute.py -v
    
    • All 3 new regression tests (test_emit_vertex_is_bounded_by_output_vertices, test_emit_vertex_in_loop_header_stays_valid_spirv, test_zero_output_gs_emit_is_bounded) FAILED; 12 existing tests passed (3 failed, 12 passed in 1.26s).
  • After fix (with SPIRV-Tools + llvm-mc on PATH / LLVM_MC, 0 skipped):
    • pytest tests/test_spirv_gs_to_compute.py -v: 15 passed in 1.58s (every rewritten module validated with spirv-as + spirv-val --target-env vulkan1.2).
    • pytest tests/test_gpu_unsupported_stage.py -v: 6 passed in 0.68s.
    • Cross-target spirv-val check on sample lowered module: vulkan1.2, vulkan1.3, and spv1.4 all passed (OK).
    • python -I tests/test_text_write_line_endings.py: 2 passed (OK).
    • python -m compileall -q make_game.py tools tests and git diff --check: clean (exit 0).
  • Hardware / game data: No GPU required (CPU SPIR-V lowering + spirv-val; synthetic GS inputs only, no game data).

Notes

(Antigravity)

AI-assisted.

…compute

Previously OpEmitVertex lowered directly to unconditional stores into %gsc_rec[(p * max_verts + n) * nout + i] and %gsc_meta[p * (max_verts + 1) + 1 + n] and incremented %gsc_n without checking n < max_verts. Any geometry shader emitting past OutputVertices silently overflowed into primitive p+1's vertex records and meta count slot, and left OpEndPrimitive/OpReturn indexing past max_verts.

Emit a %gsc_emit helper function when OpEmitVertex is present and lower each OpEmitVertex to OpFunctionCall %gsc_emit %gsc_recbase %gsc_metabase. Inside %gsc_emit, skip stores and the %gsc_n increment when %gsc_n >= OutputVertices so %gsc_n stays bounded to [0, max_verts] for OpEndPrimitive and OpReturn too, without splitting structured loop headers or OpPhi blocks at call sites.

Add synthetic regression tests with Vulkan 1.2 spirv-val checks (including emission directly inside a loop header and zero-output GS modules) in tests/test_spirv_gs_to_compute.py.

AI-assisted.
@shdann

shdann commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested f9986a0 on Windows, CPython 3.14.7, CPU only (no GPU, no game files): pass, with one gap: there is no SPIRV-Tools on this machine, so the spirv-val step of the tests did not run.

  • python -m pytest -q tests/test_spirv_gs_to_compute.py (venv with the pinned tests/requirements.txt): 4 passed, 11 skipped (they need spirv-as/spirv-val or llvm-mc).
  • Negative control: with tools/spirv_gs_to_compute.py from the merge base, the three new tests fail.
  • Extra: I executed the rewritten modules with a small evaluator (only the opcodes these modules use) on six hand-built geometry shaders with two outputs and three primitives. For shaders that stay within OutputVertices, the record and meta buffers are identical to the merge-base output. For 4 emits with max 2, an emit after a full strip, and a loop header emitting 4 with max 2, the merge base writes into the next primitive's records and meta words and this head does not, with the count clamped to max_verts.

Someone with SPIRV-Tools should still run the validation half. No defect found. AI-assisted.

@kotorvr

kotorvr commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Tested f9986a0 on Windows 11 (Python 3.13.7, CPU only, no game files) with SPIRV-Tools v2026.4 and llvm-mc available: pass. This adds the spirv-val half that the check above could not run.

  • python -m pytest -q tests/test_spirv_gs_to_compute.py at the head: 15 passed, 0 skipped (every rewritten module goes through spirv-as + spirv-val --target-env vulkan1.2). tests/test_gpu_unsupported_stage.py: 6 passed. python -m compileall -q make_game.py tools tests clean.
  • Same test file with tools/spirv_gs_to_compute.py from the merge base f847748: 4 failed, 11 passed (missing bounded %gsc_emit helper).
  • Nine variants of the PR's 4-emit fixture through rewrite, output validated with spirv-val for vulkan1.2 and vulkan1.3:
    • valid on the head (8): the fixture itself; a module that already declares OpTypeFunction %void %uint %uint; another function before main; another function after main; OutputVertices 1; OutputVertices 4; no OpEmitVertex at all (no helper is generated); an emit after OpEndPrimitive. The same eight are valid on the merge base.
    • invalid on both head and merge base (1): OpEmitVertex only inside a function that main calls is left in the compute module (Opcode EmitVertex requires … Geometry). The base does the same, so this is not from this PR.

Not run: python -m unittest discover -s tests/semantic (stopped before it finished) and anything on a GPU. AI-assisted check (Claude Code).

@memorypressure

Copy link
Copy Markdown

Verified on Linux — the guard already landed on main in a different form: bff06ea (verified present) guards OpEmitVertex on n < OutputVertices inline, adds a 419-line test_spirv_gs_to_compute_bounds.py and the SHADER-617 design row. Against today's main this PR conflicts in both files it touches and contributes no behavior beyond bff06ea (its 15 tests pass at the head, all subsumed by the bounds suite). The base-era CI red is unrelated. Suggest closing; heads-up that #4149 touches the same two files. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

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.

4 participants