shader: scalar instructions inside pixel_aot masked regions — proposed first stage #980

Open
opened 2026-11-23 20:02:30 +00:00 by MasterDomino · 6 comments
MasterDomino commented 2026-11-23 20:02:30 +00:00 (Migrated from github.com)
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by MasterDomino on 2026-11-23T20:02:30Z.


Question

How should tools/pixel_aot.py treat scalar instructions inside a masked region? What is the smallest first stage you would accept?

This builds on #957 and #975/#976. With #976's body set, most real regions that remain blocked contain only scalar instructions besides admitted VALU work. Scalar instructions ignore EXEC, but s_cbranch_execz jumps over the whole region when the wave's mask is empty. Their effects therefore depend on a wave-level condition ("any lane active"), which the per-lane model can't compute. Today every scalar instruction is refused in a region with Instruction cannot run in a masked region.

Evidence so far

Measured on one local input (the 12,101 PS records from #932; aggregates only), decoded with tools/disasm_rdna2.py. I counted the regions #957 accepts structurally (save + f32/i32/u32 _e32 v_cmpx + restore of the same pair, no further branch) whose body is fully admitted by #976 apart from scalar instructions.

  • Totals. 604 regions in 532 records. 596 of them have an s_cbranch_execz skip; only 8 don't (those contain s_nop and s_mov_b32).

  • Scalar instructions in the 596, by occurrence:

    Instruction Count
    s_waitcnt 1,075
    s_buffer_load_dword 660
    s_load_dwordx4 452
    s_add_i32 183
    s_mul_i32 143
    s_mov_b32 98
    s_cmp_gt_i32 97
    s_cselect_b32 97
    s_movk_i32 16
    s_buffer_load_dwordx4 10
  • Where the SGPRs they write are first read after the restore (linear order, before being rewritten):

    First read Regions
    not read after the region 371
    inside a later EXEC-narrowed span opened by a different compare 81
    in unconditional code (mostly as s_buffer_load descriptors, or as a v_cmp source) 144

    The read detection is a syntactic scan of the decoded text (destinations of scalar instructions excluded), and the decoder stops at the first s_endpgm, so treat these as approximate.

Semantics I'm relying on. The AMD RDNA 2 ISA guide §3.1 (p. 12 of 283) says EXEC "affects vector-ALU, vector-memory, LDS, GDS, and export instructions. It does not affect scalar (ALU or memory) execution or branches." It also says EXECZ "can be used as a condition for branches to skip code when EXEC is zero."

Inference, not measured. If the region is entered, the scalar results are the ones computed inside it. If the whole wave skips it, the registers keep their earlier values. A per-lane translation can't tell those cases apart, except where the results aren't observed after the region.

Proposed first stage

Admit side-effect-free scalar instructions in the region body, executed unconditionally as the vector body already is (the skip stays elided). Every SGPR and SCC they write becomes region-conditional: readable inside the same region, refused after the restore until rewritten outside any region. This mirrors the existing rule for VGPRs defined only on the masked path.

  • Candidates.
    • s_waitcnt and s_nop, which have no architectural effect here;
    • s_mov_b32, s_movk_i32, s_add_i32, s_mul_i32, s_cmp_*_i32 and s_cselect_b32, through their existing lowerings;
    • s_load_dwordx4 and s_buffer_load_dword/x4 on descriptors the translator already tracks, with the existing pending-load rules.
  • SCC. It becomes region-conditional too. That also makes an s_mov_b64 close leave SCC unknown when the body wrote it.
  • Saved pair. It still may not be written or overlapped by a pending load.

On this input that admits the 371 regions whose scalar results are dead after the restore. The other 225 stay refused.

I would implement this, but not before #976 has merged and you have answered the questions below.

Questions for you

  1. Unconditional scalar loads. Executing these loads unconditionally means issuing constant loads the console would skip when the whole wave is inactive. Is that acceptable for tracked descriptors, given that the translator already bounds-checks every binding? Or should loads wait for a later stage?
  2. The region-conditional rule. Is refusing every post-region read of a region-written SGPR or SCC the right line? The alternative would model "any lane active" with a wave intrinsic, but D3D wave composition doesn't match the console's, so I would keep that out.
  3. One stage or two? Should s_waitcnt/s_nop (no state) land first on their own, before the ALU and load forms?

Completion condition (first stage)

  • Translation controls:
    • each admitted form in a region under both openers;
    • a post-region read of a region-written SGPR or SCC rejects, and an unconditional rewrite makes it readable again;
    • the saved pair is still protected;
    • a scalar load waited inside the region is accepted;
    • a pending load crossing the restore rejects.
  • Binary. LLVM 15 gfx1013 round trips.
  • Draws.
    • mixed, full and empty masks, where in-region scalar results feed the region's VGPR writes;
    • for each control, a reference that applies the region to every lane must mismatch.

Required environment

Translation and the LLVM 15 round trip run on Linux. The draws need Windows (WARP in CI). No physical GPU and no game files.

  • The task can be reproduced with public or source-built inputs.
  • I did not include game files, dumps, distribution links or credentials.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6567724028 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/987). Originally posted by [MasterDomino](https://github.com/MasterDomino) on 2026-11-23T20:02:30Z. --- ### Question How should `tools/pixel_aot.py` treat scalar instructions inside a masked region? What is the smallest first stage you would accept? This builds on #957 and #975/#976. With #976's body set, most real regions that remain blocked contain only scalar instructions besides admitted VALU work. Scalar instructions ignore EXEC, but `s_cbranch_execz` jumps over the whole region when the wave's mask is empty. Their effects therefore depend on a wave-level condition ("any lane active"), which the per-lane model can't compute. Today every scalar instruction is refused in a region with `Instruction cannot run in a masked region`. ### Evidence so far **Measured on one local input** (the 12,101 PS records from #932; aggregates only), decoded with `tools/disasm_rdna2.py`. I counted the regions #957 accepts structurally (save + f32/i32/u32 `_e32` `v_cmpx` + restore of the same pair, no further branch) whose body is fully admitted by #976 apart from scalar instructions. - **Totals.** 604 regions in 532 records. 596 of them have an `s_cbranch_execz` skip; only 8 don't (those contain `s_nop` and `s_mov_b32`). - **Scalar instructions in the 596**, by occurrence: | Instruction | Count | |---|---| | `s_waitcnt` | 1,075 | | `s_buffer_load_dword` | 660 | | `s_load_dwordx4` | 452 | | `s_add_i32` | 183 | | `s_mul_i32` | 143 | | `s_mov_b32` | 98 | | `s_cmp_gt_i32` | 97 | | `s_cselect_b32` | 97 | | `s_movk_i32` | 16 | | `s_buffer_load_dwordx4` | 10 | - **Where the SGPRs they write are first read** after the restore (linear order, before being rewritten): | First read | Regions | |---|---| | not read after the region | 371 | | inside a later EXEC-narrowed span opened by a different compare | 81 | | in unconditional code (mostly as `s_buffer_load` descriptors, or as a `v_cmp` source) | 144 | The read detection is a syntactic scan of the decoded text (destinations of scalar instructions excluded), and the decoder stops at the first `s_endpgm`, so treat these as approximate. **Semantics I'm relying on.** The [AMD RDNA 2 ISA guide](https://gpuopen.com/download/rdna2-shader-instruction-set-architecture.pdf) §3.1 (p. 12 of 283) says EXEC "affects vector-ALU, vector-memory, LDS, GDS, and export instructions. It does not affect scalar (ALU or memory) execution or branches." It also says EXECZ "can be used as a condition for branches to skip code when EXEC is zero." **Inference, not measured.** If the region is entered, the scalar results are the ones computed inside it. If the whole wave skips it, the registers keep their earlier values. A per-lane translation can't tell those cases apart, except where the results aren't observed after the region. ### Proposed first stage Admit side-effect-free scalar instructions in the region body, executed unconditionally as the vector body already is (the skip stays elided). Every SGPR and SCC they write becomes *region-conditional*: readable inside the same region, refused after the restore until rewritten outside any region. This mirrors the existing rule for VGPRs defined only on the masked path. - **Candidates.** - `s_waitcnt` and `s_nop`, which have no architectural effect here; - `s_mov_b32`, `s_movk_i32`, `s_add_i32`, `s_mul_i32`, `s_cmp_*_i32` and `s_cselect_b32`, through their existing lowerings; - `s_load_dwordx4` and `s_buffer_load_dword`/`x4` on descriptors the translator already tracks, with the existing pending-load rules. - **SCC.** It becomes region-conditional too. That also makes an `s_mov_b64` close leave SCC unknown when the body wrote it. - **Saved pair.** It still may not be written or overlapped by a pending load. On this input that admits the 371 regions whose scalar results are dead after the restore. The other 225 stay refused. I would implement this, but not before #976 has merged and you have answered the questions below. ### Questions for you 1. **Unconditional scalar loads.** Executing these loads unconditionally means issuing constant loads the console would skip when the whole wave is inactive. Is that acceptable for tracked descriptors, given that the translator already bounds-checks every binding? Or should loads wait for a later stage? 2. **The region-conditional rule.** Is refusing every post-region read of a region-written SGPR or SCC the right line? The alternative would model "any lane active" with a wave intrinsic, but D3D wave composition doesn't match the console's, so I would keep that out. 3. **One stage or two?** Should `s_waitcnt`/`s_nop` (no state) land first on their own, before the ALU and load forms? ### Completion condition (first stage) - **Translation controls:** - each admitted form in a region under both openers; - a post-region read of a region-written SGPR or SCC rejects, and an unconditional rewrite makes it readable again; - the saved pair is still protected; - a scalar load waited inside the region is accepted; - a pending load crossing the restore rejects. - **Binary.** LLVM 15 gfx1013 round trips. - **Draws.** - mixed, full and empty masks, where in-region scalar results feed the region's VGPR writes; - for each control, a reference that applies the region to every lane must mismatch. ### Required environment Translation and the LLVM 15 round trip run on Linux. The draws need Windows (WARP in CI). No physical GPU and no game files. - [x] The task can be reproduced with public or source-built inputs. - [x] I did not include game files, dumps, distribution links or credentials. AI-assisted.
R0Y00 commented 2026-11-24 06:49:37 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T20:16:32Z.


Start with s_nop and waits whose counters are already satisfied in the tracked state. A wait that would resolve any pending work, or whose counter state is unknown, stays refused. Keep the current save pair, restore target, branch and EXEC restrictions. Exercise both openers and show that a pending-load wait still rejects.

Then a separate patch can admit already modeled, side-effect-free scalar ALU using proven inputs, with writes to ordinary SGPRs and SCC readable inside the region only. A read after restore stays refused until an unconditional rewrite defines it. Include a post-restore SCC consumer and a sentinel for a preexisting SGPR, along with mixed/full/empty mask readback and the unmasked control. Keep scalar loads and their waits outside both slices. A descriptor that can be bound does not establish that a load skipped by an empty wave can be executed unconditionally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7112836962 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/987#issuecomment-7112836962). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T20:16:32Z. --- Start with s_nop and waits whose counters are already satisfied in the tracked state. A wait that would resolve any pending work, or whose counter state is unknown, stays refused. Keep the current save pair, restore target, branch and EXEC restrictions. Exercise both openers and show that a pending-load wait still rejects. Then a separate patch can admit already modeled, side-effect-free scalar ALU using proven inputs, with writes to ordinary SGPRs and SCC readable inside the region only. A read after restore stays refused until an unconditional rewrite defines it. Include a post-restore SCC consumer and a sentinel for a preexisting SGPR, along with mixed/full/empty mask readback and the unmasked control. Keep scalar loads and their waits outside both slices. A descriptor that can be bound does not establish that a load skipped by an empty wave can be executed unconditionally.
MasterDomino commented 2026-11-24 06:49:41 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by MasterDomino on 2026-11-23T21:16:41Z.


Taking stage 1 as approved above: s_nop and waits whose counters are already satisfied in the tracked state, both openers covered, and a pending-load wait shown to still reject. Stage 2 (scalar ALU) will follow as a separate PR after this one.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7114244129 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/987#issuecomment-7114244129). Originally posted by [MasterDomino](https://github.com/MasterDomino) on 2026-11-23T21:16:41Z. --- Taking stage 1 as approved above: `s_nop` and waits whose counters are already satisfied in the tracked state, both openers covered, and a pending-load wait shown to still reject. Stage 2 (scalar ALU) will follow as a separate PR after this one. AI-assisted.
MasterDomino commented 2026-11-24 06:49:44 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by MasterDomino on 2026-11-23T21:52:53Z.


Stage 1 is up as #1023.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7115041701 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/987#issuecomment-7115041701). Originally posted by [MasterDomino](https://github.com/MasterDomino) on 2026-11-23T21:52:53Z. --- Stage 1 is up as #1023. AI-assisted.
R0Y00 commented 2026-11-24 06:49:57 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T00:41:44Z.


The two agreed slices are on main. #1023 admitted s_nop and already-satisfied waits, and #1064 (f7c9f579) added the scoped scalar ALU. The latter passed LLVM 15 and all six Windows masked-region checks across RTX 4060 Laptop and WARP. The in-region SGPR/SCC results, post-restore refusals, unconditional rewrite, saved pair and mixed/full/empty masks are covered. A wait that would resolve pending work and a scalar load inside the region still reject. This stays open for those effects if a separate source-built case establishes their execution and visibility rules.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7118335938 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/987#issuecomment-7118335938). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T00:41:44Z. --- The two agreed slices are on main. #1023 admitted s_nop and already-satisfied waits, and #1064 (f7c9f579) added the scoped scalar ALU. The latter passed LLVM 15 and all six Windows masked-region checks across RTX 4060 Laptop and WARP. The in-region SGPR/SCC results, post-restore refusals, unconditional rewrite, saved pair and mixed/full/empty masks are covered. A wait that would resolve pending work and a scalar load inside the region still reject. This stays open for those effects if a separate source-built case establishes their execution and visibility rules.
tedin7 commented 2026-11-24 06:50:02 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T01:30:08Z.


Claiming Stage 3 in tools/pixel_aot.py: s_waitcnt that resolves outstanding vm/lgkm work and scalar loads (s_load_dword*, s_buffer_load_dword*) inside masked regions (s_and_saveexec_b64 / v_cmpx_*), with a draw readback and independent CPU reference. Environment: Linux with LLVM 19 locally; I rely on the Ubuntu LLVM 15 job and the Windows WARP jobs for the rest. AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7119160968 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/987#issuecomment-7119160968). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T01:30:08Z. --- Claiming Stage 3 in tools/pixel_aot.py: s_waitcnt that resolves outstanding vm/lgkm work and scalar loads (s_load_dword*, s_buffer_load_dword*) inside masked regions (s_and_saveexec_b64 / v_cmpx_*), with a draw readback and independent CPU reference. Environment: Linux with LLVM 19 locally; I rely on the Ubuntu LLVM 15 job and the Windows WARP jobs for the rest. AI-assisted.
R0Y00 commented 2026-11-24 06:50:05 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T01:53:17Z.


Please take the wait slice first. Use a source-built case where the wait must make a particular pending result available, and include full, mixed and empty masks. Check what remains pending after restore, and make a dropped or misplaced wait fail. Keep scalar loads separate until there is an independent reference for execution and result visibility.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7119547387 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/987#issuecomment-7119547387). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T01:53:17Z. --- Please take the wait slice first. Use a source-built case where the wait must make a particular pending result available, and include full, mixed and empty masks. Check what remains pending after restore, and make a dropped or misplaced wait fail. Keep scalar loads separate until there is an independent reference for execution and result visibility.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows#980
No description provided.