Skip to content

shader: lower pixel alpha-test kills and coverage re-application - #3829

Open
timbot103 wants to merge 1 commit into
R0Y00:mainfrom
timbot103:shader/pixel-alpha-kill-coverage
Open

timbot103 wants to merge 1 commit into
R0Y00:mainfrom
timbot103:shader/pixel-alpha-kill-coverage

Conversation

@timbot103

Copy link
Copy Markdown
Contributor

Lowers wave64 pixel alpha-test v_cmpx_* lane kills without a saved EXEC restore, post-interpolation coverage re-application (s_and_b64 exec, exec, sN in either operand order), and combined one-armed CMPX skips with coverage restore/re-application (items G1, G2, and W1 of #3814).

Part of #3814. Refs #3640, #3641, #3703.

Summary

  • G1 (PixelLaneKills in src/gfx1013_direct_graphics.inl):
    • Recognizes an unclaimed wave64 pixel v_cmpx_* as a lane kill when the program is acyclic, no export precedes it, it dominates all code after it (no crossing branch or then_fallthroughs edge, no earlier s_endpgm), and all subsequent instructions are lane-local (no DPP/DPP8, LDS, quad compare, implicit-LOD sample, image store/atomic 0x08..0x1f, quadmask, s_wqm, lane/mbcnt/store/sendmsg/barrier ops, non-buffer_atomic_add atomics, EXEC/EXECZ reads outside recognized arm anchors, or non-narrowing EXEC writes outside restores from pairs saved strictly after the kill).
    • Lowers to dx.op.discard(!keep) in WQM, exact mode, or non-WQM, updating lane_alive (and covered / graphics_predicate when active) so covered_lane() explicitly ANDs in surviving lanes rather than relying on post-discard IsHelperLane.
    • Proves a following s_cbranch_execz skip (with only lane-local quiet ops between and no branch entering the span) to either a later instruction or a terminal EXP_NULL block (dropping EXP_NULL only when every branch targeting it is such a skip).
  • G2 (s_and_b64 exec, exec, sN & enter_exact):
    • Extracts shared exact-mode transition into enter_exact.
    • Accepts s_and_b64 exec, exec, sN (both operand orders) when sN is proven entry coverage (invariant_entry_masks or tag "0"): leaves WQM in the prologue or in the unique post-dominating branch-free tail (exact_tail, disabled on any backward branch or earlier s_endpgm), and acts as a no-op when already in exact mode or outside WQM.
  • W1 (one-armed CMPX skip + coverage restore/re-application):
    • Combines one-armed CMPX skips cleanly with G1 kills before or after and G2 coverage ANDs.
    • Extends pixel_readonly_arms so a one-armed CMPX skip whose restore is the entry coverage saved immediately before s_wqm_b64 exec, exec lowers its join via coverage_restores -> enter_exact.
  • tests/native_pixel_alpha_kill_check.py + tests/cmake/native_pixel_alpha_kill.cmake:
    • 16 accepted shapes covering G1, G2, W1, and their combinations (DXIL structural checks + 8x2 WARP and hardware GPU readback, each with negative wrong-reference mismatch controls, plus 2 all-lanes-killed draws).
    • 16 fail-closed refusal controls (prior export, DPP/quad-swizzle/EXEC-read/WQM after kill, pre-kill EXEC restore, branch into/around kill, class/clamped compare, unproven/clobbered coverage pair, coverage restore before branch, coverage AND inside uniform arm or after an early s_endpgm).

Verification

Host: Windows 11 (10.0.26200.9974), MSVC C++20 /W4 (zero new warnings), inbox WARP (10.0.26100.9818), NVIDIA GeForce RTX 5080 (32.0.16.2140).

  • Debug (dev) & Release (release):
    • native_pixel_alpha_kill_controls: passed in both configs (16 accepted + 16 refusals).
    • native_pixel_alpha_kill_warp: passed in both configs (WARP readback + wrong-reference mismatch controls + all-killed draws).
    • native_pixel_alpha_kill_hardware: passed in both configs (RTX 5080 readback + wrong-reference mismatch controls + all-killed draws).
    • Regression pass (17/17 in both Debug and Release): native_direct_container, native_pixel_dxil, native_graphics_wqm_{warp,hardware,ir}, native_graphics_vertex_vcc_join_{warp,hardware}, native_pixel_cmpx_{discard_warp,if_else_controls,if_else_warp}, native_pixel_compare_mask_{warp,hardware,container,ir}.
  • tests/bidi_controls_check.py, tests/docs_links_check.py, git diff --check origin/main..HEAD: all clean.

@timbot103
timbot103 force-pushed the shader/pixel-alpha-kill-coverage branch from 6ecff0c to d0a5e3d Compare December 2, 2026 07:26
Part of R0Y00#3814 (items G1, G2 and W1); refs R0Y00#3640, R0Y00#3641, R0Y00#3703.

G1: a wave64 pixel v_cmpx_* outside every recognised EXEC region is an
alpha-test kill when no later EXEC write can return its lanes. A new
pre-pass (PixelLaneKills) accepts it only when everything after it keeps
each lane to itself: no DPP, LDS, quad compare, implicit-LOD sample,
image store/atomic, lane or wave operation, store, quadmask or s_wqm;
EXEC is only narrowed, read by an arm's anchor save, or restored from a
pair saved after the kill. No export may precede it, it must lie on every
path to the code after it, and the program has no loop. It lowers to
dx.op.discard of the failing lanes, with or without WQM, and in exact
mode under the covered predicate. A following EXECZ (only lane-local work
between) is proven: it is taken only by a wave with no surviving lane.
Its target is a later instruction or the EXP_NULL block, which is dropped
when every branch to it is such a skip. Coverage tests AND in the
surviving lanes explicitly.

G2: s_and_b64 exec, exec, sN (either order) with sN the saved entry
coverage now leaves whole-quad mode where it is not the terminal mask,
in the prologue or at the start of the unique post-dominating branch-free
tail (exact_tail), and is a no-op in exact mode or without WQM entry.
The exact-mode transition shared with s_mov_b64 exec, sN moves into
enter_exact.

W1: the plain one-armed CMPX skip followed by the coverage AND already
translated; it now combines with G1 before or after. pixel_readonly_arms
also takes a one-armed CMPX whose restore is the coverage saved before
s_wqm_b64 exec, exec, and lowers that join as the exact transition.

Every new rule accepts or ignores a shape. An ignored CMPX or skip keeps
its existing refusal; a kill that does not dominate the code after it is
refused by name.

tests/native_pixel_alpha_kill_check.py: 16 accepted shapes (DXIL shape
and a WARP/hardware readback of an 8x2 draw, each with wrong references
that must differ) and 16 refusal controls.

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

@lukaslw-lab lukaslw-lab left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed b24a8d8a by reading the diff; not built or run.

Verdict: approve. The discard matches the guest kill wherever lane_local_after accepts the code after it. Every shape it ignores keeps its old refusal. No MUST.

  • SHOULD No fixture pins the one store-like form allowed after a kill. lane_local_after accepts buffer_atomic_add (gfx1013_direct_graphics.inl:1508). Killed lanes skip it only because covered_lane() now ANDs lane_alive (:2287), which the atomic uses as its predicate (:3193). One draw with a kill and then an atomic add, checking the count of surviving pixels only, would keep that from silently regressing.
  • SHOULD No docs update. G1, G2 and W1 change what docs/experiments/mesh86-pixel-execz.md says about CMPX and coverage handling.
  • SHOULD No measured stage count. 566 / 86 / ≈620 is the #3814 shape tally. A ps86 census on f365f125 before and after would give the real gain. This can come in a follow-up.
  • NIT CI runs only _controls: native_pixel_alpha_kill_warp is not in the pr preset filter.
  • NIT #3826, #3828 and this PR all change pixel_readonly_arms. Two of them will need a rebase.
  • Checked, no finding:
    • Killed lanes cannot come back. A restore of a pair saved before the kill is refused (saved_after), and so is an arm anchored before the kill, because the backward anchor search stops at the kill's EXEC write.
    • With implicit-LOD samples, DPP, LDS, quad and lane ops refused, nothing reads a discarded lane. The no-op EXECZ skips and the dropped EXP_NULL block cut only code that runs with EXEC = 0.
    • exact_tail lies after every branch and then_fallthroughs edge, and is disabled by a backward branch or an early s_endpgm.

Tests: _controls compiles 16 shapes and 16 refusals (e.g. a restore that revives killed lanes). _warp/_hardware read back with wrong-reference and all-killed controls. CI runs _controls only (in progress at review time).

@lukaslw-lab

Copy link
Copy Markdown
Contributor

Census follow-up for the SHOULD in my review: on the #3814 capture, this PR does not yet move the G1 stages, because their kill uses a live-mask form that lane_local_after refuses.

Build Pixel stages translated graphics wave or control-flow instruction
f365f125 + #3825 272 728
same + #3826, #3828, #3829 (and the mesh PRs) 272 648

Same capture and method as #3814. 6,436 distinct stages, run through compile_dxil gfx1013-mesh86-ps. Counts only.

In a 120-stage sample of the 648 that remain, 106 still stop at the v_cmpx, and 105 of those have this shape (registers normalized):

s_mov_b64 sK, exec
...                                    ; alpha value, e.g. v_mul_legacy_f32 / v_fract_f32
v_cmpx_lt_f32 sA, vX                   ; kill condition
s_andn2_b64 sK, sK, exec               ; EXEC = lanes to kill; drop them from live sK
s_cbranch_scc0 L                       ; no live lane left
s_and_b64 exec, sC, sK
exp mrtz ... done ; s_endpgm
L: s_mov_b64 exec, sK ; exp null done ; s_endpgm
  • Here the v_cmpx leaves EXEC holding the lanes to kill, not keep. s_andn2_b64 removes them from the live mask sK, scc0 exits when no lane lives, and s_and_b64 exec, sC, sK restores coverage AND live. Per invocation: keep = saved bit AND NOT compare, then discard where the compare holds. The fixtures' keep model (no live mask) would invert this shape.
  • lane_local_after refuses the EXEC read in s_andn2_b64, so these stay refused, which is the safe outcome. Accepting this triple, with sK the save made before the v_cmpx, would cover G1 + G2 for these stages.
  • I measured this on a local stack with shader: lower pixel SAVEEXEC lane-mask expressions (Part of #3814) #3826 and shader: lower one-armed pixel CMPX skips with scalar loads and SALU #3828 merged in (conflicts resolved locally), not on this PR alone. lane_local_after does not depend on those two PRs, so the result should be the same here.

AI-assisted.

@timbot103

Copy link
Copy Markdown
Contributor Author

@lukaslw-lab Thanks for the approval and the exact census breakdown of the live-mask G1+G2 kill form (s_mov_b64 sK, exec -> v_cmpx_* -> s_andn2_b64 sK, sK, exec -> s_cbranch_scc0 L -> s_and_b64 exec, sC, sK) — that is super actionable!

To keep this approved PR clean and ready to land without restarting review, I will follow up in a dedicated PR (staged as Draft under #3514) adding:

  • The live-mask v_cmpx_* + s_andn2_b64 + s_cbranch_scc0 + s_and_b64 exec, sC, sK kill/coverage-restore lowering (with keep = saved & ~cmp and discard where cmp holds)
  • An atomic-after-kill GPU readback draw fixture pinning killed lanes skipping buffer_atomic_add
  • The docs/experiments/mesh86-pixel-execz.md update

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.

2 participants