Skip to content

startup: refuse uniform load proofs across EXEC-changing loops - #2768

Merged
MasterDomino merged 3 commits into
R0Y00:mainfrom
RavenCurseBurg:shader/guard-loop-entry-load-proof
Nov 26, 2026
Merged

MasterDomino merged 3 commits into
R0Y00:mainfrom
RavenCurseBurg:shader/guard-loop-entry-load-proof

Conversation

@RavenCurseBurg

@RavenCurseBurg RavenCurseBurg commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2757.

The first iteration of a compute loop can reach a read-only buffer load with the entry EXEC mask, and the next iteration can reach the same instruction with EXEC empty. The second readfirstlane must then see lane zero's previous register contents. The former translation reloaded memory using the new offset.

For compute programs with a back edge and a candidate load, the translator now follows CFG successors from blocks that write EXEC. Loads in those successor blocks are ineligible for the past-entry reload proof. This includes an EXEC write reached by a detour beyond the back-edge instruction. A write that occurs only after loop exit leaves the proof intact. The graphics and compute cache seeds both advance from main's revision 21 to revision 22. The compute seed 22cs invalidates earlier translations of this case.

Verification

Tested commit 0e29f163 on Windows 11. The reported loop and the off-range detour both produced unguarded reloads on the original baseline. Both are refused with the patch. The unchanged-EXEC loop still translates.

python -B tests/startup_gfx2hlsl_readfirstlane_load_recompute_check.py
python -B tests/startup_gfx2hlsl_readfirstlane_uniform_check.py
python -B tests/startup_gfx2hlsl_readfirstlane_recompute_check.py
python -B tests/startup_compute_readfirstlane_check.py
python -B tests/startup_compute_request_offline_check.py
python -B tests/startup_gfx2hlsl_compute_check.py
python -B tests/startup_gfx2hlsl_compute_check.py binary <llvm-mc>
python -B tests/startup_gfx2hlsl_compute_check.py dispatch <dxc> <offscreen_check> <warp|hardware>

The host suites passed, including the independently framed compute key 90041d02ef75b4e9. All checked-in fixture words matched LLVM's gfx1013 assembler (LLVM 15.0.7, clang -cc1as -show-encoding). The dispatch suite passed 27 baselines and detected four controls on each of WARP, Intel UHD 770 and RTX 4060 with DXC 1.9.2609.5.

The new repo-authored gfx1013 fixture takes both arms of a forward branch in separate workgroups. Eight threads per group store the readfirstlane result at individual addresses. The CPU compares all 320 words, including 304 untouched sentinels. Replacing the read with zero fails on each backend at word=0 expected=8b02a7f1 actual=00000000. The regression at the loop head is tested as an explicit translator refusal. The broader question about predication of arbitrary VGPR writes is separate.

Follow-up 0b861347 keeps the graphics and compute cache revisions paired. Running python -B tests/<name>.py passed for startup_shader_cache_rekey_check, startup_shader_cache_key_check, startup_compute_request_offline_check, startup_gfx2hlsl_buffer_store_check, startup_gfx2hlsl_const_copy_join_check, startup_gfx2hlsl_fract_check and startup_packed_position_provenance_check. An independent native C++ run of the runtime hash produced graphics keys 93a74e652d95daf6 and 1ff6b039237188d6 (without and with the input layout).

After #2723 landed at revision 21, 3341d272 merged f1a8db63 and advanced both seeds to 22. All seven cache/translation suites listed above and the compute, readfirstlane-uniform and readfirstlane-load controls passed on this head. The native graphics hash returned 30009a5d8295d679 and dae7d34c534837e5. The independently framed compute key is 5159f6a85d77858e. The joined-load GPU fixture and its zero-read mutant were rerun on all three backends.

AI-assisted with Codex (GPT-6.2 Astra, xhigh).

@MasterDomino

Copy link
Copy Markdown
Collaborator

@RavenCurseBurg, the Linux Python job fails at 0e29f163 on tests/startup_shader_cache_rekey_check.py:

AssertionError: gfx2hlsl.py no longer spells the cache revision as one b'x2w-startup-shader-N' and one b'x2w-startup-shader-Ncs' literal; update revision_module (found [('20', ''), ('21', 'cs')])

The check expects the graphics and compute revisions to share one number. Moving the graphics key to x2w-startup-shader-21 alongside 21cs (in gfx2hlsl.py, d3d12_draw.inl, and the tests and docs that spell it) should clear it. A full review follows.

AI-assisted.

@RavenCurseBurg

Copy link
Copy Markdown
Contributor Author

Pushed 0b861347 to keep the runtime, translator, native fixture seeds and key vectors at 21/21cs. The original re-key test passes with its shared-revision assertion intact. I reran the six adjacent cache/translation tests and independently checked both graphics keys with the C++ runtime hash. The PR description includes the results.

@tedin7 tedin7 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 of head 0b861347 (comment only).

What I checked:

  • gfx2hlsl.py: for compute programs with a back edge and a uniform load, blocks reachable from an EXEC-writing block through the CFG are collected in loads_after_exec_write. Loads there are no longer eligible for the past-entry reload proof. EXEC writes only after loop exit leave the proof intact. This matches the described failure (second readfirstlane after an empty EXEC).
  • Cache revision is 21 / 21cs in the translator, the runtime stub and the fixture seeds. The shared-revision re-key test passes.
  • No sibling table or callsite left out; no other open or merged PR covers this slice.

Commands run (Linux, exact head):

  • python3 -B tests/startup_gfx2hlsl_readfirstlane_load_recompute_check.py rc=0
  • python3 -B tests/startup_gfx2hlsl_compute_check.py rc=0
  • python3 -B tests/startup_shader_cache_rekey_check.py rc=0
  • python3 -B tests/startup_shader_cache_key_check.py rc=0
  • python3 -B tests/startup_compute_readfirstlane_check.py rc=0
  • python3 tests/docs_links_check.py, python3 tests/bidi_controls_check.py, git diff --check origin/main...HEAD: clean
  • Mutation: removing the loads_after_exec_write condition makes startup_gfx2hlsl_readfirstlane_load_recompute_check.py fail (rc=1), so the new test guards the behavior.

Windows results follow in the receipt.

AI-assisted; commands listed were executed locally.

@MasterDomino

Copy link
Copy Markdown
Collaborator

@RavenCurseBurg, thanks for 0b861347. Heads-up: #2723 just merged (f1a8db63) and also took x2w-startup-shader-21 / 21cs. This PR now needs 22 / 22cs so its refusal re-keys entries cached under 21. The semantic review is still running and follows separately.

AI-assisted.

@tedin7

tedin7 commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

Windows Server 2025 (MSVC + D3D12 WARP) at 0b861347:

  • Dev (--preset dev): CTest startup_indirect_draw_indexed, startup_resource_retire_title 8/8 passed. Python checks ok, including startup_gfx2hlsl_compute_check.py, startup_gfx2hlsl_readfirstlane_load_recompute_check.py, startup_shader_cache_rekey_check.py.
  • Release (--preset release), run twice with the same result, 4 of 8 (and 4 of 6) CTest entries fail with SEGFAULT:
    • startup_resource_retire_title_warp_debug0
    • startup_resource_retire_title_off_warp
    • startup_resource_retire_title_hardware_debug0
    • startup_resource_retire_title_off_hardware

The PR's only change to that test is the draw_revision string (...-20 to ...-21), so this may already fail on main in release. I could not confirm that here. Not merging until this is explained. @R0Y00 / MasterDomino: do you know whether startup_resource_retire_title crashes in release on main?

Code review and Linux checks are in the earlier review comment.

AI-assisted; commands listed were executed locally.

@RavenCurseBurg

Copy link
Copy Markdown
Contributor Author

Merged f1a8db63 in 3341d272 and moved both runtime and offline seeds to 22/22cs. The documentation also retains #2723's revision-21 history. The ten local checks, the joined-load readback on WARP/Intel/NVIDIA and its pinned zero-read mismatch all passed on this head. The PR description includes the independently calculated keys.

@tedin7

tedin7 commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

Update on the release crash: it is not caused by this PR.

I ran the same four tests on Windows Server 2025 with this PR's changes reverted (main at f1a8db63, release preset): startup_resource_retire_title_warp_debug0, startup_resource_retire_title_off_warp, startup_resource_retire_title_hardware_debug0 and startup_resource_retire_title_off_hardware crash with SEGFAULT there too. Debug passes. The most recent change touching that test is #2719.

So the PR's own tests are green in debug and release. The release crash is a separate problem on main.

AI-assisted; commands listed were executed locally.

@MasterDomino MasterDomino 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.

Approving 3341d272. Against 0b861347, which I reviewed in full, the PR's own translator diff differs only in the revision, now x2w-startup-shader-22 / 22cs. It merges cleanly and CI is green.

@tedin7, on the Release segfault: it isn't this PR. startup_resource_retire_title segfaults in Release on main too: all 6 variants at f1a8db63, on WARP and an RX 9070 XT. A bisect puts it at #2719: its parent passes 6/6 in Release, and the merge fails 6/6.

  • Cause: a pre-existing ABI heuristic in the shim. For descriptor heap slot 9, small_result treats a live wrapper in the second register as the (return buffer, object) form. Case 9 nests the slot-9 call inside a slot-20 call, so at /O2 rdx still holds the resource wrapper. The shim then writes the handle over the heap object's vtable and returns the heap's address. The RTV lands in an untracked slot, and the heap's next Release jumps into data.
  • Fix: I'll open a PR with a shim fix and a deterministic regression that also fails in Debug.

Why the guard is complete: exec_known is tracked in program order and never comes back after a real EXEC write. So a load placed textually after a writer was already refused; the gap was a writer reaching an earlier load through a back edge, and following CFG successors from every writer block closes it. Through the text harness, each of these now refuses where main accepted:

  • the #2757 repro;
  • s_or_b32 / s_andn2_b32 / s_cselect_b32 into exec_lo, and s_and_b64 exec;
  • v_cmpx (e32 and e64), v_cmp_e64 exec_lo and s_andn2_wrexec_b32;
  • a self-loop closed by s_cbranch_execnz, and an s_cbranch_execz inside the loop;
  • a back edge to offset 0;
  • an outer loop that narrows EXEC after its inner loop exits.

A writer after loop exit and a loop with no writer still translate.

Non-blocking: a divergent if inside a uniform loop (s_and_saveexec … s_or_b32 exec_lo, exec_lo, sN before the back edge) is now refused. main translated it correctly, because EXEC is the entry mask again at the load on every pass. The refusal is conservative and sound; proving the restore could be a follow-up. The bump is justified: wrong translations are now refused, and some still-translating programs drop a dead ulc_N temporary.

Runs:

  • Windows 11 build 26300; AMD Radeon RX 9070 XT (driver 32.0.32029.2010); WARP 10.0.26100.9818; debug layer on, 0 errors.
  • At 0b861347, ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl|^startup_compute_readfirstlane|startup_compute_request_offline|startup_shader_cache|startup_indirect_draw_indexed|startup_resource_retire|startup_packed_position_provenance' gave 321 tests, 319 passed, 0 skipped. The two failures also fail on main on this adapter (#2746): fma_mix_dispatch_hardware and f16_alu_draw_hardware.
  • join-load matches all 320 words on WARP and hardware, and join-load-zero is rejected at word=0 expected=8b02a7f1 actual=00000000.
  • At 3341d272, these all exit 0: the readfirstlane load/uniform/recompute checks, startup_compute_readfirstlane_check.py, startup_compute_request_offline_check.py, startup_gfx2hlsl_compute_check.py (plus binary with llvm-mc 15), startup_shader_cache_rekey_check.py and startup_shader_cache_key_check.py.

Mutants: four are caught by startup_gfx2hlsl_readfirstlane_load_recompute: dropping the guard, removing the transitive walk, seeding only forward successors, and counting only saveexec as a writer. The new refusals also fail against main's translator.

AI-assisted; commands listed were executed locally.

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.

gfx2hlsl: uniform compute load proof (#2607) bypassed at a loop head that narrows EXEC

3 participants