Skip to content

shader: how should pixel_aot represent exec-masked conditional regions and branches? #338

Closed

Description

@rasptum

Question

tools/pixel_aot.py lowers straight-line programs only. lower() rejects s_branch, every s_cbranch_*, v_cmpx_* and s_*_saveexec_b64, and its execution-mask state is limited to "entry" and "quad". Conditional execution is common in precompiled pixel programs, and I found no issue or PR that claims it.

Which representation should the translator use, and what is the smallest first step you would accept? I would like to take this on, starting with the stage below.

Evidence so far

Measured with LLVM 15.0.7 on source-written inputs (main at 6ee2406):

  • A per-pixel if that LLVM cannot flatten compiles for gfx1013 to v_cmp_*, s_and_saveexec_b64 s[a:b], vcc, s_cbranch_execz L, the body, then s_or_b64 exec, exec, s[a:b] at L. The branch only skips a body whose mask is empty for the whole wave.
  • if/else adds s_xor_b64 and s_andn2_saveexec_b64 on the saved pair. A condition on scalar operands becomes s_cbranch_vccnz with no masking. A loop ends in a backward s_cbranch_execnz. A kill ends in a null export.
  • disassemble() prints a branch target as an unsigned 16-bit word count from the next instruction (s_cbranch_execz 4, backward 65531), and the assembler accepts the same text. Instructions are 4 or 8 bytes, so resolving a target needs the byte offsets that disassemble(offsets=True) already returns.
  • DXC 1.7.2308 compiles an implicit-derivative Sample inside a per-pixel if or loop without a diagnostic, so the translator must enforce that rule itself.

Inference, not measured: instruction semantics come from the AMD RDNA 2 ISA reference, and the shapes are LLVM's. Another compiler may emit different ones.

Option Covers Main cost
A. Per-lane predication: exec is a tracked per-pixel boolean, a masked region merges at its join, a skip branch over it is ignored masked if, else, nesting, later discard a region stack and register snapshots, no uniform branches or loops by itself
B. CFG reconstruction and structuring also uniform branches and loops a rewrite of the single pass
C. State machine switch in a loop any shape every register a mutable variable, derivatives inside the loop

B and C still need A's model, because a wave-level branch on a per-lane mask has no per-pixel meaning unless it is a pure skip. I propose A first, with uniform forward branches and natural loops as later stages, not promised here.

Stage one, one PR: one forward, non-nested region opened by s_and_saveexec_b64 on a tracked comparison and closed by s_or_b64 exec, exec from the same pair, with an optional s_cbranch_execz directly after the save whose target is the restore. Registers written in the body become mask ? inner : outer selects at the join, so the HLSL has no control flow. Rejected in stage one: loads, samples, exports and exec changes inside the region, else forms, nesting, all other branches, v_cmpx, b32 forms, and reads after the join of a register first defined inside or of a scalar written inside.

Completion condition

The research task is complete when the representation and the stage-one boundary are chosen. Stage one is complete when these source-built programs pass on WARP and hardware, every pixel compared bitwise with a CPU reference that uses exact binary32 arithmetic on the existing 8x8 ramp:

  1. A masked if that some pixels take and others do not in one draw.
  2. The same program with the skip branch, which must lower to identical HLSL.
  3. An if/else pair written as two complementary regions.
  4. One shader with two constant sets: mask empty for every pixel, then full.
  5. A value written before the region, overwritten inside and read after the join.

Draw controls: a reference with the opposite polarity and one that ignores the mask must each report pixel_mismatch. must_reject controls: backward branch, target inside the region, target past the restore, target not on an instruction boundary, restore without save, restore from another pair, saved pair overwritten, missing restore before the export, a constant load whose wait sits only inside the skippable region, and a load or sample inside the region. The programs join check_binary, and the coverage record gains s_and_saveexec_b64 and s_cbranch_execz.

Shared interfaces: the input of lower() (offset and text pairs for programs with branches), its mask, pending and scc state, and the ACCEPTED recorder in the test. tests/form_coverage_check.py would move s_and_saveexec_b64 from rejected to covered, and the existing rejection programs stay rejected. Existing handlers are not edited, so #232, #303, #304, #306 and #320 meet this change only in adjacent lines. #257 also clears SCC on exec writes. #176 and #271 define the wait state the join rule reads, so I would rebase on whichever lands first. Samples inside a region wait for #283 and #320.

Requested: your choice among A, B and C, the stage-one boundary (should the else form be included?), select merges or if blocks in the HLSL, and whether lower() may take offset pairs.

Required environment

Windows x64 with DXC, LLVM 15 for the round trip, no game files. I can run Windows 10 in-box WARP 10.0.19041, WARP 1.0.22 and an RTX 3060.

AI-assisted (Claude Code).

Activity

  1. R0Y00 commented on Nov 21, 2026

    @R0Y00
    Owner

    Please proceed with option A. Start with a single forward masked-if from a known entry mask: a supported comparison, s_and_saveexec_b64, an optional immediately adjacent s_cbranch_execz targeting the matching s_or_b64 restore, then pure per-lane register operations and that restore. Multiple independently closed regions may follow one another. Complementary regions make a good test. Leave true else sequences, nesting, loops and general wave branches for separate designs.

    Merge the inner and original values with a per-lane select. Eliding the whole-wave empty-mask skip is valid here only because speculative evaluation is safe for every input. Reject memory, sampling/derivatives, exports, discard, waits or pending-state changes, additional EXEC writes, and observable wave-state side effects. Protect the saved register pair, reject post-merge reads of values undefined on any incoming path, and model SCC from save/restore or reject its use.

    Keep the straight-line lowering API. Branch-bearing input can supply offset/text pairs: compute the signed displacement from the next instruction in ISA units and require the exact restore boundary. Please cover mixed, empty and full masks, preservation of outer values, and equivalent results with and without the skip. Include reversed-polarity and ignored-mask controls and a rejected wrong target. Round-trip with LLVM 15 and exercise the same draw on WARP and hardware, recording each result separately.

  2. MarlomMedeiros commented on Nov 22, 2026

    @MarlomMedeiros
    Contributor

    Option A is implemented in #651.

    Against the requested shape:

    • Single forward masked-if from a known entry mask: s_and_saveexec_b64 requires a tracked condition pair and a full entry mask, s_or_b64 exec, exec, s[..] closes it, and an optional s_cbranch_execz is only accepted immediately after the save when its signed 16-bit displacement lands exactly on the matching restore index.
    • Multiple independently closed regions work, and the complementary-region case is a fixture.
    • Inner and outer values merge through per-lane precise float selects; registers first written inside are marked conditional and post-merge reads of path-undefined values reject until an unconditional write.
    • Inside a region only pure per-lane vector ops pass: the gate plus a per-instruction state snapshot reject memory, sampling, derivatives, interpolation, exports, waits, pending-state changes, EXEC-affecting and cross-lane work. The saved pair carries an exec_saved marker that rejects reuse, and SCC is dropped across save/restore so its consumers reject cleanly.
    • The straight-line API is unchanged for string lists; branch-bearing input supplies (offset, text) pairs.
    • Coverage includes mixed/full/empty masks (thresholds 0.5, -1, 10), outer-value preservation, identical results with and without the skip, reversed-polarity and mask-free mutants that must mismatch, and rejected wrong targets (wrong pair, later same-pair restore, backward, mid-region, out of range).
    • LLVM 15 round-trip covers a label-based s_cbranch_execz program assembled with llvm-mc and disassembled on gfx1013.
    • Draws ran on WARP and on hardware (RTX 5070, driver 32.0.16.2135, Windows 11 26300.10075, debug layer clean); both are recorded separately in docs/experiments.md.

    AI-assisted: yes. All reported checks ran locally on the adapter and driver listed.

  3. R0Y00 commented on Nov 23, 2026

    @R0Y00
    Owner

    Stage one is implemented in #651. The translator uses per-lane selects, requires the matching restore for the optional skip, and keeps unsupported regions on the rejection path. The LLVM 15 round-trip covered the actual labelled branch.

    I also reran the mixed, empty and full masks for the ordinary, skip and complementary variants with an R32G32B32A32_UINT readback target. All nine draws per backend matched all 64 pixels bit-for-bit on WARP 10.0.26100.9818 and RTX 4060 Laptop driver 32.0.16.1656, Windows build 26200.9974, debug layer enabled. The original reversed-predicate and mask-free controls both mismatched. This completes the stage-one scope.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions