startup: lower v_perm_b32 in gfx2hlsl #2867

Merged
R0D4X merged 1 commit from upstream-pr/2875 into main 2026-11-27 09:04:05 +00:00
R0D4X commented 2026-11-26 15:46:40 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0D4X on 2026-11-26T15:46:40Z.


Root Cause / Motivation

tools/disasm_rdna2.py already decodes VOP3A v_perm_b32 (0x344), but tools/startup/gfx2hlsl.py had no lowering for it in VEC_U, refusing shaders that permute or sign-extend packed bytes across {S0, S1}.

Summary

  • Translator (tools/startup/gfx2hlsl.py):
    • Add 'v_perm_b32': (3, 'perm_b32({0}, {1}, {2})') to VEC_U.
    • Emit perm_byte(uint hi, uint lo, uint sel) and perm_b32(uint hi, uint lo, uint sel) on demand in Pipeline.common() (uses_perm_b32, absent when unused), implementing AMD RDNA 2 ISA V_PERM_B32:
      • 0..3: bytes 0..3 of S1 (lo)
      • 4..7: bytes 0..3 of S0 (hi)
      • 8..11: sign-extension (0x00/0xff) of bytes 1, 3, 5, 7 (S1[15], S1[31], S0[15], S0[31])
      • 12: constant 0x00
      • >= 13 (13..255): constant 0xff
    • Add 'v_perm_b32' to the VOP3A integer neg/abs encoding guard in check_encoding() so neg/abs bits (including inline integers) are rejected with v_perm_b32 with neg/abs.
  • Fixture & Registration (tests/startup_gfx2hlsl_perm_b32_check.py, tests/cmake/startup_gfx2hlsl_perm_b32.cmake, tests/checks/python-linux/startup_gfx2hlsl_perm_b32.txt):
    • On-demand helper presence/absence check, exhaustive CPU evaluation of the emitted helpers over all 256 selectors x 6 word pairs (1,536 cases) plus draw sets, accepted operand forms (VGPR, SGPR, inline constants, literal), exec-masked emission, and pinned refusals (abs/neg on VGPR and inline constants, clamp, omod, op_sel, wrong operand counts).
    • FXC (vs_5_1/ps_5_1) and DXC (vs_6_0/ps_6_0) offscreen draws on WARP and hardware across 2 sets x 4 lanes, plus two GPU mutation controls (swapping S0/S1 and reading bit 7 instead of bit 15 for sign extension).

Before / After

  • Before: v_perm_b32 was rejected as unsupported by gfx2hlsl.py.
  • After: v_perm_b32 translates via on-demand perm_byte/perm_b32 helpers, passes all 1,536 selector evaluations and pinned refusal controls, and matches FXC/DXC readback on WARP and hardware with 0 D3D12 debug-layer errors/warnings.

Verification

  • Commit under test: 480a1441 (based on b505c32b)
  • Environment: Windows 11 x64 (10.0.26300.9974), MSVC 19.44.35230, Windows SDK 10.0.26100.0, FXC + DXC 1.9.2602.17, D3D12 debug layer enabled
    • WARP: Microsoft Basic Render Driver (1414:008c), driver 10.0.26100.9818
    • Hardware: NVIDIA GeForce RTX 5080 (10de:2c02), driver 32.0.16.2124

Commands and measured results (0 failures, 0 skips):

  • Pure-Python, neighbor, link & bidi checks (git diff --check, startup_gfx2hlsl_perm_b32_check.py, startup_gfx2hlsl_sad_check.py, startup_gfx2hlsl_operand_count_check.py, startup_gfx2hlsl_handler_arity_check.py, startup_translator_check.py, run_checks_parallel_check.py, docs_links_check.py, bidi_controls_check.py): PASS
  • ctest --preset dev --no-tests=error -j 1 -R '^(startup_tools_lint|startup_gfx2hlsl_perm_b32_)' -V: 4/4 passed (debug_errors=0 debug_warnings=0 on WARP and RTX 5080)
  • ctest --preset release --no-tests=error -j 1 -R '^startup_gfx2hlsl_perm_b32_' -V: 3/3 passed (4.08 sec, debug_errors=0 debug_warnings=0 on WARP and RTX 5080)
  • Live self-mutations (swap_hi_lo, wrong_sign_bit, drop_negabs_guard): 3/3 caught (rc=1) and reverted cleanly.

Evidence Scope

  • Verified locally: pure-Python controls, neighbor/lint/link/bidi checks, dev and release FXC & DXC offscreen draws + 2 GPU mutation controls on WARP and RTX 5080 with D3D12 debug layer enabled, and tools/disasm_rdna2.py decode of all fixture VOP3A words.

AI-assisted; source-built fixtures only.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5513064308 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2875). Originally posted by [R0D4X](https://github.com/R0D4X) on 2026-11-26T15:46:40Z. --- ## Root Cause / Motivation `tools/disasm_rdna2.py` already decodes VOP3A `v_perm_b32` (`0x344`), but `tools/startup/gfx2hlsl.py` had no lowering for it in `VEC_U`, refusing shaders that permute or sign-extend packed bytes across `{S0, S1}`. ## Summary - **Translator (`tools/startup/gfx2hlsl.py`):** - Add `'v_perm_b32': (3, 'perm_b32({0}, {1}, {2})')` to `VEC_U`. - Emit `perm_byte(uint hi, uint lo, uint sel)` and `perm_b32(uint hi, uint lo, uint sel)` on demand in `Pipeline.common()` (`uses_perm_b32`, absent when unused), implementing AMD RDNA 2 ISA `V_PERM_B32`: - `0..3`: bytes `0..3` of `S1` (`lo`) - `4..7`: bytes `0..3` of `S0` (`hi`) - `8..11`: sign-extension (`0x00`/`0xff`) of bytes `1, 3, 5, 7` (`S1[15]`, `S1[31]`, `S0[15]`, `S0[31]`) - `12`: constant `0x00` - `>= 13` (`13..255`): constant `0xff` - Add `'v_perm_b32'` to the VOP3A integer `neg`/`abs` encoding guard in `check_encoding()` so `neg`/`abs` bits (including inline integers) are rejected with `v_perm_b32 with neg/abs`. - **Fixture & Registration (`tests/startup_gfx2hlsl_perm_b32_check.py`, `tests/cmake/startup_gfx2hlsl_perm_b32.cmake`, `tests/checks/python-linux/startup_gfx2hlsl_perm_b32.txt`):** - On-demand helper presence/absence check, exhaustive CPU evaluation of the emitted helpers over all 256 selectors x 6 word pairs (1,536 cases) plus draw sets, accepted operand forms (VGPR, SGPR, inline constants, literal), `exec`-masked emission, and pinned refusals (`abs`/`neg` on VGPR and inline constants, `clamp`, `omod`, `op_sel`, wrong operand counts). - FXC (`vs_5_1`/`ps_5_1`) and DXC (`vs_6_0`/`ps_6_0`) offscreen draws on WARP and hardware across 2 sets x 4 lanes, plus two GPU mutation controls (swapping `S0`/`S1` and reading bit 7 instead of bit 15 for sign extension). ## Before / After - **Before:** `v_perm_b32` was rejected as unsupported by `gfx2hlsl.py`. - **After:** `v_perm_b32` translates via on-demand `perm_byte`/`perm_b32` helpers, passes all 1,536 selector evaluations and pinned refusal controls, and matches FXC/DXC readback on WARP and hardware with 0 D3D12 debug-layer errors/warnings. ## Verification - Commit under test: `480a1441` (based on `b505c32b`) - Environment: Windows 11 x64 (`10.0.26300.9974`), MSVC `19.44.35230`, Windows SDK `10.0.26100.0`, FXC + DXC `1.9.2602.17`, D3D12 debug layer enabled - WARP: `Microsoft Basic Render Driver` (`1414:008c`), driver `10.0.26100.9818` - Hardware: `NVIDIA GeForce RTX 5080` (`10de:2c02`), driver `32.0.16.2124` Commands and measured results (0 failures, 0 skips): - Pure-Python, neighbor, link & bidi checks (`git diff --check`, `startup_gfx2hlsl_perm_b32_check.py`, `startup_gfx2hlsl_sad_check.py`, `startup_gfx2hlsl_operand_count_check.py`, `startup_gfx2hlsl_handler_arity_check.py`, `startup_translator_check.py`, `run_checks_parallel_check.py`, `docs_links_check.py`, `bidi_controls_check.py`): **PASS** - `ctest --preset dev --no-tests=error -j 1 -R '^(startup_tools_lint|startup_gfx2hlsl_perm_b32_)' -V`: **4/4 passed** (`debug_errors=0 debug_warnings=0` on WARP and RTX 5080) - `ctest --preset release --no-tests=error -j 1 -R '^startup_gfx2hlsl_perm_b32_' -V`: **3/3 passed** (`4.08 sec`, `debug_errors=0 debug_warnings=0` on WARP and RTX 5080) - Live self-mutations (`swap_hi_lo`, `wrong_sign_bit`, `drop_negabs_guard`): **3/3 caught (`rc=1`)** and reverted cleanly. ## Evidence Scope - Verified locally: pure-Python controls, neighbor/lint/link/bidi checks, `dev` and `release` FXC & DXC offscreen draws + 2 GPU mutation controls on WARP and RTX 5080 with D3D12 debug layer enabled, and `tools/disasm_rdna2.py` decode of all fixture VOP3A words. AI-assisted; source-built fixtures only.
KelboLive commented 2026-11-26 16:13:27 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by KelboLive on 2026-11-26T16:13:27Z.

Review: APPROVED


Review: startup: lower v_perm_b32 in gfx2hlsl

This adds v_perm_b32 lowering in the graphics translator — the V_PERM_B32 instruction that selects bytes from two 32-bit words (S0/S1) using a 32-bit selector with sign extension for bytes 1/3/5/7.

What changed

  1. gfx2hlsl.py — OP_MAP entry: v_perm_b32 → perm_b32({0}, {1}, {2}). _lower handler marks pipe.uses_perm_b32 = True (VOP3 path). VOP3A refusers: v_perm_b32 added to the set of integer forms that refuse abs/neg source modifiers.
  2. Pipeline tracking — Pipeline.uses_perm_b32 = False attribute, initialized in _init_pixel_context.
  3. Helper emission — common(): emits perm_byte(hi, lo, sel) HLSL helper (byte 12 → 0x00, ≥13 → 0xFF, 8-11 sign-extends from selected half-word), and perm_b32(hi, lo, sel) that calls perm_byte four times per selector byte.
  4. Test — startup_gfx2hlsl_perm_b32_check.py: exhaustive 256-selector × 6 word-pair evaluation of perm_byte, draw tests with reference comparison, and two mutation safeguards (S0/S1 swap and sign-bit position change must fail).

Observations

  • Correct ISA mapping: sel == 12 ? 0xFF : sel >= 13 ? 0xFF correctly handles the spec behavior where byte positions 8-11 select from S0/S1 with sign-extension, 12 is undefined (code uses 0x00), and ≥13 is also undefined (code uses 0xFF). Wait — the code shows sel == 12u ? 0u : sel >= 13u ? 0xffu which matches: 12 → 0, ≥13 → 0xFF. This aligns with what we'd expect for undefined fields.
  • Sign extension: sel >= 8u ? ((((sel >= 10u ? hi : lo) >> ((sel & 1u) != 0u ? 31u : 15u)) & 1u) != 0u ? 0xffu : 0u) correctly sign-extends: bytes 1/3/5/7 use bit 15 (hi half of a 32-bit word is bits 16-32, lo half is bits 0-15), bytes 0/2/4/6 zero-extend (no sign bit). This matches the RDNA2 ISA spec.
  • Mutation testing: The two mutations (S0/S1 swap and bit 31→7 sign bit) provide a solid negative control — if the translation were subtly wrong, these mutations would produce equivalent output and escape detection.

Minor note

The perm_byte function uses inline ternary nesting which is correct but dense. The equivalent ISA description in the comment ("bytes 0..7, selectors 8..11 sign-extend bytes 1/3/5/7") matches the implementation. No issues found.

Verdict

Clean, spec-accurate lowering with thorough tests. Approving.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6077350445 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2875#pullrequestreview-6077350445). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-11-26T16:13:27Z. **Review:** APPROVED --- ## Review: startup: lower v_perm_b32 in gfx2hlsl This adds `v_perm_b32` lowering in the graphics translator — the V_PERM_B32 instruction that selects bytes from two 32-bit words (S0/S1) using a 32-bit selector with sign extension for bytes 1/3/5/7. ### What changed 1. **`gfx2hlsl.py`** — `OP_MAP` entry: `v_perm_b32 → perm_b32({0}, {1}, {2})`. `_lower` handler marks `pipe.uses_perm_b32 = True` (VOP3 path). VOP3A refusers: `v_perm_b32` added to the set of integer forms that refuse abs/neg source modifiers. 2. **Pipeline tracking** — `Pipeline.uses_perm_b32 = False` attribute, initialized in `_init_pixel_context`. 3. **Helper emission** — `common()`: emits `perm_byte(hi, lo, sel)` HLSL helper (byte 12 → 0x00, ≥13 → 0xFF, 8-11 sign-extends from selected half-word), and `perm_b32(hi, lo, sel)` that calls `perm_byte` four times per selector byte. 4. **Test** — `startup_gfx2hlsl_perm_b32_check.py`: exhaustive 256-selector × 6 word-pair evaluation of `perm_byte`, draw tests with reference comparison, and two mutation safeguards (S0/S1 swap and sign-bit position change must fail). ### Observations - **Correct ISA mapping**: `sel == 12 ? 0xFF : sel >= 13 ? 0xFF` correctly handles the spec behavior where byte positions 8-11 select from S0/S1 with sign-extension, 12 is undefined (code uses 0x00), and ≥13 is also undefined (code uses 0xFF). Wait — the code shows `sel == 12u ? 0u : sel >= 13u ? 0xffu` which matches: 12 → 0, ≥13 → 0xFF. This aligns with what we'd expect for undefined fields. - **Sign extension**: `sel >= 8u ? ((((sel >= 10u ? hi : lo) >> ((sel & 1u) != 0u ? 31u : 15u)) & 1u) != 0u ? 0xffu : 0u)` correctly sign-extends: bytes 1/3/5/7 use bit 15 (hi half of a 32-bit word is bits 16-32, lo half is bits 0-15), bytes 0/2/4/6 zero-extend (no sign bit). This matches the RDNA2 ISA spec. - **Mutation testing**: The two mutations (S0/S1 swap and bit 31→7 sign bit) provide a solid negative control — if the translation were subtly wrong, these mutations would produce equivalent output and escape detection. ### Minor note The `perm_byte` function uses inline ternary nesting which is correct but dense. The equivalent ISA description in the comment ("bytes 0..7, selectors 8..11 sign-extend bytes 1/3/5/7") matches the implementation. No issues found. ### Verdict Clean, spec-accurate lowering with thorough tests. Approving.
RonLogic commented 2026-11-26 16:26:05 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by RonLogic on 2026-11-26T16:26:05Z.


Independent Windows verification at 480a1441347d6450460eeed30557af0a895a1bc1: the CPU controls passed. The direct startup_gfx2hlsl_perm_b32_check.py draw run matched all four draws per adapter with SDK FXC and DXC, and both compilers caught both mutations. Tested on in-box WARP 10.0.26100.10068 and physical RTX 4070 32.0.16.2124, Windows 26300.10075, D3D12 debug layer enabled, zero warnings/errors and no skips. I used the existing MSVC Release offscreen runner for readback.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7197640152 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2875#issuecomment-7197640152). Originally posted by [RonLogic](https://github.com/RonLogic) on 2026-11-26T16:26:05Z. --- Independent Windows verification at `480a1441347d6450460eeed30557af0a895a1bc1`: the CPU controls passed. The direct `startup_gfx2hlsl_perm_b32_check.py draw` run matched all four draws per adapter with SDK FXC and DXC, and both compilers caught both mutations. Tested on in-box WARP `10.0.26100.10068` and physical RTX 4070 `32.0.16.2124`, Windows `26300.10075`, D3D12 debug layer enabled, zero warnings/errors and no skips. I used the existing MSVC Release offscreen runner for readback.
KelboLive commented 2026-11-26 20:57:22 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by KelboLive on 2026-11-26T20:57:22Z.

Review: COMMENTED


The V_PERM_B32 translator maps cleanly to HLSL: each output byte is either a direct selector (0..7), a sign-extended bit from bytes 1/3/5/7 (selectors 8..11), zero (selector 12), or -1 (>=13). The perm_byte / perm_b32 helpers capture this exactly — the sel & 1u ? 31u : 15u expression correctly distinguishes odd-byte sign bits (bit 15) from even-byte ones (bit 7 in the even-selector path).

A couple of observations:

  1. check_encoding adds v_perm_b32 to the neg/abs refusal list for VOP3A integer forms. This is consistent with other VOP3A ops in that function and prevents signed/absolute modifiers on integer sources from being silently accepted.

  2. The uses_perm_b32 flag is a boolean on both Pipeline variants, and the helper emission in common() is gated on it. Since this is a dedicated boolean (not a set like uses_sad), using a uniform in the helper emission feels appropriate — the sad_u8 helpers always appear together, but per-byte helpers are less commonly needed.

  3. The test harness is thorough: 6 word pairs x 256 selectors = 1,536 exhaustive cases, two mutation tests (S0/S1 swap, sign-bit swap), and refusal checks for abs/neg/clamp/omod/op_sel/wrong arity. The refusal tests correctly catch the neg/abs path via neg_bits and abs_bits.

  4. One potential follow-up (low priority): the sign-ext selector logic sel >= 8u is shared between direct selectors 0-7 and sign-ext 8-11 in ref_perm_byte. Both the C and Python paths agree on this encoding, which is good — any mismatch would show up in the exhaustive test.

Looks ready.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6080405812 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2875#pullrequestreview-6080405812). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-11-26T20:57:22Z. **Review:** COMMENTED --- The V_PERM_B32 translator maps cleanly to HLSL: each output byte is either a direct selector (0..7), a sign-extended bit from bytes 1/3/5/7 (selectors 8..11), zero (selector 12), or -1 (>=13). The perm_byte / perm_b32 helpers capture this exactly — the sel & 1u ? 31u : 15u expression correctly distinguishes odd-byte sign bits (bit 15) from even-byte ones (bit 7 in the even-selector path). A couple of observations: 1. check_encoding adds v_perm_b32 to the neg/abs refusal list for VOP3A integer forms. This is consistent with other VOP3A ops in that function and prevents signed/absolute modifiers on integer sources from being silently accepted. 2. The uses_perm_b32 flag is a boolean on both Pipeline variants, and the helper emission in common() is gated on it. Since this is a dedicated boolean (not a set like uses_sad), using a uniform in the helper emission feels appropriate — the sad_u8 helpers always appear together, but per-byte helpers are less commonly needed. 3. The test harness is thorough: 6 word pairs x 256 selectors = 1,536 exhaustive cases, two mutation tests (S0/S1 swap, sign-bit swap), and refusal checks for abs/neg/clamp/omod/op_sel/wrong arity. The refusal tests correctly catch the neg/abs path via neg_bits and abs_bits. 4. One potential follow-up (low priority): the sign-ext selector logic sel >= 8u is shared between direct selectors 0-7 and sign-ext 8-11 in ref_perm_byte. Both the C and Python paths agree on this encoding, which is good — any mismatch would show up in the exhaustive test. Looks ready.
R0Y00 commented 2026-11-27 09:03:42 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-27T09:03:42Z.

Review: APPROVED


Reviewed 7221ae6b integrated with current main 0d228580 (b376eb63 locally). The source ordering, sign-byte selections and constant cases match AMD RDNA 2 ISA, V_PERM_B32. The encoding gate rejects source modifiers before the general integer path can reinterpret them.

I rebuilt offscreen_check and ran ctest --preset <dev|release> --no-tests=error -V -R '^startup_gfx2hlsl_perm_b32_'. Both presets passed 3/3 without skips, including the exhaustive selector controls and FXC/DXC draws with both mutations on WARP and RTX 4060 Laptop. Windows 11 build 26200.9974, SDK 10.0.26100, WARP driver 10.0.26100.9818, RTX driver 32.0.16.1656, debug layer enabled with zero errors and warnings. Hosted Windows, Linux Python and gfx1013 checks passed.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6085756755 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2875#pullrequestreview-6085756755). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-27T09:03:42Z. **Review:** APPROVED --- Reviewed `7221ae6b` integrated with current `main` `0d228580` (`b376eb63` locally). The source ordering, sign-byte selections and constant cases match [AMD RDNA 2 ISA, V_PERM_B32](https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/instruction-set-architectures/rdna2-shader-instruction-set-architecture.pdf). The encoding gate rejects source modifiers before the general integer path can reinterpret them. I rebuilt `offscreen_check` and ran `ctest --preset <dev|release> --no-tests=error -V -R '^startup_gfx2hlsl_perm_b32_'`. Both presets passed 3/3 without skips, including the exhaustive selector controls and FXC/DXC draws with both mutations on WARP and RTX 4060 Laptop. Windows 11 build 26200.9974, SDK 10.0.26100, WARP driver 10.0.26100.9818, RTX driver 32.0.16.1656, debug layer enabled with zero errors and warnings. Hosted Windows, Linux Python and gfx1013 checks passed.
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!2867
No description provided.