shader: fail closed on stale mesh lane masks and partial VGPRs #3450

Merged
Leyukaka merged 4 commits from upstream-pr/3450 into main 2026-11-29 12:10:41 +00:00
Leyukaka commented 2026-11-29 11:22:37 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-29T11:22:37Z.


Part of #3416.

Wrong translations on main

Three mesh programs are accepted and translated to DXIL that differs from the guest. All were measured with compile_dxil on source-built fixtures, the DXIL read with dxc -dumpbin; nothing was run on a GPU. Fixture registers: v0 = lane, v1 = lane & 1, v2 = lane >> 1; v1 feeds position x. Line numbers are src/gfx1013_direct_mesh.inl at e358cf5f.

1. VGPR written under narrowed EXEC, read after EXEC widens

A VALU write under narrowed EXEC becomes select(lane_active, new, before) only when the register is not already partial (:1162). A second write under a narrower EXEC, or in the other arm of an if/else, stays raw and the register stays in partial_writes. The s_or_b64 / s_and_b64 / s_andn2_b64 handler with dst EXEC recomputes lane_active and never touches partial_writes, so a read after lanes are added takes the raw last value in every lane.

Case Expected (guest) Emitted
v10 written in lanes 0-1, again in lane 0, s_or_b64 exec, exec, s[22:23], v_cmp_eq_u32 vcc, 2, v10 lane 1 position x 5.0 compare reads the raw second value; lane 1 position x 1.0
v10 written in even lanes, in odd lanes after s_andn2_b64 exec, s[22:23], exec, widened, read lane 0 position x 5.0 compare reads the raw else-arm value in every lane; lane 0 position x 0.0

Measured on 1a0120ba; not re-measured as DXIL on e358cf5f, where the first commit with its clear disabled accepts the first case again (mutation below). Control: with s_mov_b64 exec, s[22:23] in place of the s_or_b64, the first case is refused (+0x38: graphics register undefined on an incoming path: 266). Inside an s_cbranch_execz lane-skip block the same sequence is refused at the EXEC write since #3443.

2. Scalar ALU write to EXEC_LO / EXEC_HI outside a lane-skip block

Scalar writes to dst 126/127 are refused only inside an open skip (:868-875). Outside one, three handlers pass them to alu.write:

  • :934-937: s_mulk_i32, s_addk_i32, s_mul_i32, s_add_u32, s_add_i32, s_max_u32, s_cselect_b32;
  • :1052-1053: s_bfe_u32, s_min_u32;
  • :1059-1063: s_pack_ll_b32_b16, s_pack_hh_b32_b16.

The write only stores the register value; after_graphics_write (:33-39) ignores 126/127, so full_exec, lane_active, wave_active, exec_value and partial_writes keep the old mask.

Fixture (wave64): v10 = 3 under full EXEC, v_cmpx_eq_u32 0, v2 narrows to lanes 0-1, v10 = 1 there, the scalar write, v_cmp_eq_u32 vcc, 3, v10, s_mov_b64 exec, -1, v1 = vcc ? v1 : 5.

Scalar write Expected (guest) Emitted
s_max_u32 exec_lo, -1, 0, s_pack_ll_b32_b16 exec_lo, -1, -1 VCC = 1 in lanes 2-31; vertex 2 position x 0.0 compare still ANDed with the old CMPX predicate, VCC = 0 in every lane; vertex 2 position x 5.0
s_max_u32 exec_hi, -1, 0, s_pack_hh_b32_b16 exec_hi, -1, -1 VCC = 1 in lanes 32-63 same stale predicate; those lanes are not among the 3 exported vertices of the fixture

The DXIL of each accepted case is the s_nop control plus three dead scalar instructions. Measured on e358cf5f plus the first commit, which does not touch these handlers.

3. Scalar ALU write to VCC_LO / VCC_HI

The same three handlers accept dst 106/107. The scalar word is stored in register 106, after_graphics_write clears only the VCC word facts, and graphics_lane_mask (src/gfx1013_direct_compute.inl:2716) then returns that word as the per-lane bit.

Fixture: VCC = 0, s_pack_ll_b32_b16 vcc_lo, 5, 0, v1 = vcc ? v1 : 5.

Expected (guest) Emitted
vcc_lo = 5 lanes 0 and 2 keep v1, lane 1 gets 5; vertex 1 position x 5.0 icmp ne i32 <word>, 0 used as the predicate of every lane; vertex 1 position x 1.0

A write to vcc_hi leaves the lane bit in 106 unchanged. Measured on e358cf5f plus the first two commits.

Change

All four commits touch src/gfx1013_direct_mesh.inl and tests/native_direct_mesh_check.py only (+13 / +48).

  • 6c268218: in the EXEC-destination s_or_b64 / s_and_b64 / s_andn2_b64 handler, unless the op can only narrow, every register in partial_writes is written undefined and the set is cleared, as s_mov_b64 exec, -1 already does. A later read refuses. Narrowing-only forms keep the set: s_and_b64 with EXEC as either source, s_andn2_b64 with EXEC as src0, s_or_b64 exec, exec, exec.
  • 21ccd56e: the three handlers of case 2 refuse dst 126/127 (mesh scalar write to EXEC).
  • 8ee5b5c6: the same handlers refuse dst 106/107 (mesh scalar write to VCC); adds the s_min_u32 EXEC case.
  • a87c6f7e: test only, pins the s_bfe_u32 / s_min_u32 VCC refusal.

The dedicated EXEC and VCC paths (v_cmp_*, v_cmpx_*, s_cselect_b64, the B64 mask ops, s_mov_b64) are unchanged.

Tests and mutations

native_direct_mesh cases, each refusal asserting a nonzero exit, the diagnostic with its offset, and no output file:

  • nested-or-widen, else-arm-widen: the two rows of case 1, +0x38: graphics register undefined on an incoming path: 266.
  • merged-or-widen: v10 first written under full EXEC, then the nested sequence; accepted, asserts select i1.
  • scalar-exec-{pack-ll-lo,pack-hh-hi,max-lo,max-hi,min-lo}: +0x2c: mesh scalar write to EXEC. scalar-exec-nop: same program with s_nop, accepted.
  • scalar-vcc-{pack-ll-lo,max-hi,min-lo}: +0x20: mesh scalar write to VCC.

Results: ctest -R '^(native_direct_mesh|native_compile_dxil_refusals.*)$' 2/2 on 6c268218. On the head a87c6f7e, ctest -R '^(native_direct_mesh|native_compile_dxil_refusals.*|native_fixed_mesh57.*_warp)$' 12/12; with the s_bfe_u32 / s_min_u32 VCC guard removed, native_direct_mesh fails at scalar-vcc-min-lo.

Mutations, each rebuilt and reverted:

Commit Mutation Result
6c268218 clear never runs nested-or-widen fails (accepted)
6c268218 also exempt s_or_b64 with EXEC as src0 and any src1 nested-or-widen fails
6c268218 clear only inside a lane-skip block (before the rebase, on 1a0120ba) nested-or-widen fails
6c268218 drop the s_andn2_b64 exemption (on 1a0120ba) survives
21ccd56e remove the EXEC refusal in the arithmetic handler scalar-exec-max-lo fails
21ccd56e dst==127 changed to dst==128 there scalar-exec-max-hi fails
21ccd56e remove the EXEC refusal in the PACK handler scalar-exec-pack-ll-lo fails
8ee5b5c6 remove the EXEC refusal in the s_bfe_u32 / s_min_u32 handler scalar-exec-min-lo fails
8ee5b5c6 remove the VCC refusal in the arithmetic handler, or reduce it to dst==106 scalar-vcc-max-hi fails
8ee5b5c6 remove the VCC refusal in the PACK handler scalar-vcc-pack-ll-lo fails

Correct on main, now refused

  • Scalar writes of 0 to VCC, of -1 to vcc_lo in wave32, and vcc_hi writes in wave32 followed only by a v_cndmask_b32: main lowered these correctly, 8ee5b5c6 refuses them. No test depends on them.
  • A register still in partial_writes when EXEC may widen is refused on a later read, including a single narrowed write over an undefined value. A single narrowed write over a defined value already becomes a select and leaves the set (merged-or-widen).
  • An s_and_b64 of two non-EXEC masks that happens to narrow is treated as widening.

No EXEC form that main translated correctly was found to be refused by 21ccd56e: none of the three handlers updated the lane state for dst 126/127.

Not covered

  • The s_and_b64 and s_andn2_b64 exemptions have no test; removing them only makes more programs refuse.
  • The cost on real mesh programs is not measurable today: the cached mesh requests have no entry layout sidecar, so all of them refuse at mesh request has no entry layout before this code runs.
  • Case 3 with vcc_hi and case 2 on exec_hi have no measured vertex difference in these fixtures; they are refused on the same grounds.

A select-based alternative for case 1 (merging the second write as select(lane_active, new, before) too) was evaluated and not shipped: it still reads the raw value in the widened lanes when the earlier value had been cleared by s_mov_b64 exec, -1 (lane 1 position x 1.0, guest 5.0), a case this change refuses.

Base: e358cf5f. git merge-tree --write-tree against main 20bd1553 is clean.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5550216263 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3450). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-11-29T11:22:37Z. --- Part of #3416. ## Wrong translations on main Three mesh programs are accepted and translated to DXIL that differs from the guest. All were measured with `compile_dxil` on source-built fixtures, the DXIL read with `dxc -dumpbin`; nothing was run on a GPU. Fixture registers: v0 = lane, v1 = lane & 1, v2 = lane >> 1; v1 feeds position x. Line numbers are `src/gfx1013_direct_mesh.inl` at `e358cf5f`. ### 1. VGPR written under narrowed EXEC, read after EXEC widens A VALU write under narrowed EXEC becomes `select(lane_active, new, before)` only when the register is not already partial (:1162). A second write under a narrower EXEC, or in the other arm of an if/else, stays raw and the register stays in `partial_writes`. The `s_or_b64` / `s_and_b64` / `s_andn2_b64` handler with dst EXEC recomputes `lane_active` and never touches `partial_writes`, so a read after lanes are added takes the raw last value in every lane. | Case | Expected (guest) | Emitted | |---|---|---| | v10 written in lanes 0-1, again in lane 0, `s_or_b64 exec, exec, s[22:23]`, `v_cmp_eq_u32 vcc, 2, v10` | lane 1 position x 5.0 | compare reads the raw second value; lane 1 position x 1.0 | | v10 written in even lanes, in odd lanes after `s_andn2_b64 exec, s[22:23], exec`, widened, read | lane 0 position x 5.0 | compare reads the raw else-arm value in every lane; lane 0 position x 0.0 | Measured on `1a0120ba`; not re-measured as DXIL on `e358cf5f`, where the first commit with its clear disabled accepts the first case again (mutation below). Control: with `s_mov_b64 exec, s[22:23]` in place of the `s_or_b64`, the first case is refused (`+0x38: graphics register undefined on an incoming path: 266`). Inside an `s_cbranch_execz` lane-skip block the same sequence is refused at the EXEC write since #3443. ### 2. Scalar ALU write to EXEC_LO / EXEC_HI outside a lane-skip block Scalar writes to dst 126/127 are refused only inside an open skip (:868-875). Outside one, three handlers pass them to `alu.write`: - :934-937: `s_mulk_i32`, `s_addk_i32`, `s_mul_i32`, `s_add_u32`, `s_add_i32`, `s_max_u32`, `s_cselect_b32`; - :1052-1053: `s_bfe_u32`, `s_min_u32`; - :1059-1063: `s_pack_ll_b32_b16`, `s_pack_hh_b32_b16`. The write only stores the register value; `after_graphics_write` (:33-39) ignores 126/127, so `full_exec`, `lane_active`, `wave_active`, `exec_value` and `partial_writes` keep the old mask. Fixture (wave64): v10 = 3 under full EXEC, `v_cmpx_eq_u32 0, v2` narrows to lanes 0-1, v10 = 1 there, the scalar write, `v_cmp_eq_u32 vcc, 3, v10`, `s_mov_b64 exec, -1`, `v1 = vcc ? v1 : 5`. | Scalar write | Expected (guest) | Emitted | |---|---|---| | `s_max_u32 exec_lo, -1, 0`, `s_pack_ll_b32_b16 exec_lo, -1, -1` | VCC = 1 in lanes 2-31; vertex 2 position x 0.0 | compare still ANDed with the old CMPX predicate, VCC = 0 in every lane; vertex 2 position x 5.0 | | `s_max_u32 exec_hi, -1, 0`, `s_pack_hh_b32_b16 exec_hi, -1, -1` | VCC = 1 in lanes 32-63 | same stale predicate; those lanes are not among the 3 exported vertices of the fixture | The DXIL of each accepted case is the `s_nop` control plus three dead scalar instructions. Measured on `e358cf5f` plus the first commit, which does not touch these handlers. ### 3. Scalar ALU write to VCC_LO / VCC_HI The same three handlers accept dst 106/107. The scalar word is stored in register 106, `after_graphics_write` clears only the VCC word facts, and `graphics_lane_mask` (`src/gfx1013_direct_compute.inl:2716`) then returns that word as the per-lane bit. Fixture: VCC = 0, `s_pack_ll_b32_b16 vcc_lo, 5, 0`, `v1 = vcc ? v1 : 5`. | | Expected (guest) | Emitted | |---|---|---| | vcc_lo = 5 | lanes 0 and 2 keep v1, lane 1 gets 5; vertex 1 position x 5.0 | `icmp ne i32 <word>, 0` used as the predicate of every lane; vertex 1 position x 1.0 | A write to vcc_hi leaves the lane bit in 106 unchanged. Measured on `e358cf5f` plus the first two commits. ## Change All four commits touch `src/gfx1013_direct_mesh.inl` and `tests/native_direct_mesh_check.py` only (+13 / +48). - `6c268218`: in the EXEC-destination `s_or_b64` / `s_and_b64` / `s_andn2_b64` handler, unless the op can only narrow, every register in `partial_writes` is written undefined and the set is cleared, as `s_mov_b64 exec, -1` already does. A later read refuses. Narrowing-only forms keep the set: `s_and_b64` with EXEC as either source, `s_andn2_b64` with EXEC as src0, `s_or_b64 exec, exec, exec`. - `21ccd56e`: the three handlers of case 2 refuse dst 126/127 (`mesh scalar write to EXEC`). - `8ee5b5c6`: the same handlers refuse dst 106/107 (`mesh scalar write to VCC`); adds the `s_min_u32` EXEC case. - `a87c6f7e`: test only, pins the `s_bfe_u32` / `s_min_u32` VCC refusal. The dedicated EXEC and VCC paths (`v_cmp_*`, `v_cmpx_*`, `s_cselect_b64`, the B64 mask ops, `s_mov_b64`) are unchanged. ## Tests and mutations `native_direct_mesh` cases, each refusal asserting a nonzero exit, the diagnostic with its offset, and no output file: - `nested-or-widen`, `else-arm-widen`: the two rows of case 1, `+0x38: graphics register undefined on an incoming path: 266`. - `merged-or-widen`: v10 first written under full EXEC, then the nested sequence; accepted, asserts `select i1`. - `scalar-exec-{pack-ll-lo,pack-hh-hi,max-lo,max-hi,min-lo}`: `+0x2c: mesh scalar write to EXEC`. `scalar-exec-nop`: same program with `s_nop`, accepted. - `scalar-vcc-{pack-ll-lo,max-hi,min-lo}`: `+0x20: mesh scalar write to VCC`. Results: `ctest -R '^(native_direct_mesh|native_compile_dxil_refusals.*)$'` 2/2 on `6c268218`. On the head `a87c6f7e`, `ctest -R '^(native_direct_mesh|native_compile_dxil_refusals.*|native_fixed_mesh57.*_warp)$'` 12/12; with the `s_bfe_u32` / `s_min_u32` VCC guard removed, `native_direct_mesh` fails at `scalar-vcc-min-lo`. Mutations, each rebuilt and reverted: | Commit | Mutation | Result | |---|---|---| | `6c268218` | clear never runs | `nested-or-widen` fails (accepted) | | `6c268218` | also exempt `s_or_b64` with EXEC as src0 and any src1 | `nested-or-widen` fails | | `6c268218` | clear only inside a lane-skip block (before the rebase, on `1a0120ba`) | `nested-or-widen` fails | | `6c268218` | drop the `s_andn2_b64` exemption (on `1a0120ba`) | survives | | `21ccd56e` | remove the EXEC refusal in the arithmetic handler | `scalar-exec-max-lo` fails | | `21ccd56e` | `dst==127` changed to `dst==128` there | `scalar-exec-max-hi` fails | | `21ccd56e` | remove the EXEC refusal in the PACK handler | `scalar-exec-pack-ll-lo` fails | | `8ee5b5c6` | remove the EXEC refusal in the `s_bfe_u32` / `s_min_u32` handler | `scalar-exec-min-lo` fails | | `8ee5b5c6` | remove the VCC refusal in the arithmetic handler, or reduce it to `dst==106` | `scalar-vcc-max-hi` fails | | `8ee5b5c6` | remove the VCC refusal in the PACK handler | `scalar-vcc-pack-ll-lo` fails | ## Correct on main, now refused - Scalar writes of 0 to VCC, of -1 to vcc_lo in wave32, and vcc_hi writes in wave32 followed only by a `v_cndmask_b32`: main lowered these correctly, `8ee5b5c6` refuses them. No test depends on them. - A register still in `partial_writes` when EXEC may widen is refused on a later read, including a single narrowed write over an undefined value. A single narrowed write over a defined value already becomes a select and leaves the set (`merged-or-widen`). - An `s_and_b64` of two non-EXEC masks that happens to narrow is treated as widening. No EXEC form that main translated correctly was found to be refused by `21ccd56e`: none of the three handlers updated the lane state for dst 126/127. ## Not covered - The `s_and_b64` and `s_andn2_b64` exemptions have no test; removing them only makes more programs refuse. - The cost on real mesh programs is not measurable today: the cached mesh requests have no entry layout sidecar, so all of them refuse at `mesh request has no entry layout` before this code runs. - Case 3 with vcc_hi and case 2 on exec_hi have no measured vertex difference in these fixtures; they are refused on the same grounds. A select-based alternative for case 1 (merging the second write as `select(lane_active, new, before)` too) was evaluated and not shipped: it still reads the raw value in the widened lanes when the earlier value had been cleared by `s_mov_b64 exec, -1` (lane 1 position x 1.0, guest 5.0), a case this change refuses. Base: `e358cf5f`. `git merge-tree --write-tree` against main `20bd1553` is clean.
Sign in to join this conversation.
No reviewers
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!3450
No description provided.