startup: lower v_perm_b32 in gfx2hlsl #2867
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2875"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by R0D4X on 2026-11-26T15:46:40Z.
Root Cause / Motivation
tools/disasm_rdna2.pyalready decodes VOP3Av_perm_b32(0x344), buttools/startup/gfx2hlsl.pyhad no lowering for it inVEC_U, refusing shaders that permute or sign-extend packed bytes across{S0, S1}.Summary
tools/startup/gfx2hlsl.py):'v_perm_b32': (3, 'perm_b32({0}, {1}, {2})')toVEC_U.perm_byte(uint hi, uint lo, uint sel)andperm_b32(uint hi, uint lo, uint sel)on demand inPipeline.common()(uses_perm_b32, absent when unused), implementing AMD RDNA 2 ISAV_PERM_B32:0..3: bytes0..3ofS1(lo)4..7: bytes0..3ofS0(hi)8..11: sign-extension (0x00/0xff) of bytes1, 3, 5, 7(S1[15],S1[31],S0[15],S0[31])12: constant0x00>= 13(13..255): constant0xff'v_perm_b32'to the VOP3A integerneg/absencoding guard incheck_encoding()soneg/absbits (including inline integers) are rejected withv_perm_b32 with neg/abs.tests/startup_gfx2hlsl_perm_b32_check.py,tests/cmake/startup_gfx2hlsl_perm_b32.cmake,tests/checks/python-linux/startup_gfx2hlsl_perm_b32.txt):exec-masked emission, and pinned refusals (abs/negon VGPR and inline constants,clamp,omod,op_sel, wrong operand counts).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 (swappingS0/S1and reading bit 7 instead of bit 15 for sign extension).Before / After
v_perm_b32was rejected as unsupported bygfx2hlsl.py.v_perm_b32translates via on-demandperm_byte/perm_b32helpers, 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
480a1441(based onb505c32b)10.0.26300.9974), MSVC19.44.35230, Windows SDK10.0.26100.0, FXC + DXC1.9.2602.17, D3D12 debug layer enabledMicrosoft Basic Render Driver(1414:008c), driver10.0.26100.9818NVIDIA GeForce RTX 5080(10de:2c02), driver32.0.16.2124Commands and measured results (0 failures, 0 skips):
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): PASSctest --preset dev --no-tests=error -j 1 -R '^(startup_tools_lint|startup_gfx2hlsl_perm_b32_)' -V: 4/4 passed (debug_errors=0 debug_warnings=0on 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=0on WARP and RTX 5080)swap_hi_lo,wrong_sign_bit,drop_negabs_guard): 3/3 caught (rc=1) and reverted cleanly.Evidence Scope
devandreleaseFXC & DXC offscreen draws + 2 GPU mutation controls on WARP and RTX 5080 with D3D12 debug layer enabled, andtools/disasm_rdna2.pydecode of all fixture VOP3A words.AI-assisted; source-built fixtures only.
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_b32lowering 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
gfx2hlsl.py—OP_MAPentry:v_perm_b32 → perm_b32({0}, {1}, {2})._lowerhandler markspipe.uses_perm_b32 = True(VOP3 path). VOP3A refusers:v_perm_b32added to the set of integer forms that refuse abs/neg source modifiers.Pipeline.uses_perm_b32 = Falseattribute, initialized in_init_pixel_context.common(): emitsperm_byte(hi, lo, sel)HLSL helper (byte 12 → 0x00, ≥13 → 0xFF, 8-11 sign-extends from selected half-word), andperm_b32(hi, lo, sel)that callsperm_bytefour times per selector byte.startup_gfx2hlsl_perm_b32_check.py: exhaustive 256-selector × 6 word-pair evaluation ofperm_byte, draw tests with reference comparison, and two mutation safeguards (S0/S1 swap and sign-bit position change must fail).Observations
sel == 12 ? 0xFF : sel >= 13 ? 0xFFcorrectly 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 showssel == 12u ? 0u : sel >= 13u ? 0xffuwhich matches: 12 → 0, ≥13 → 0xFF. This aligns with what we'd expect for undefined fields.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.Minor note
The
perm_bytefunction 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.
Imported from R0Y00/Xbox2Windows. Originally posted by RonLogic on 2026-11-26T16:26:05Z.
Independent Windows verification at
480a1441347d6450460eeed30557af0a895a1bc1: the CPU controls passed. The directstartup_gfx2hlsl_perm_b32_check.py drawrun matched all four draws per adapter with SDK FXC and DXC, and both compilers caught both mutations. Tested on in-box WARP10.0.26100.10068and physical RTX 407032.0.16.2124, Windows26300.10075, D3D12 debug layer enabled, zero warnings/errors and no skips. I used the existing MSVC Release offscreen runner for readback.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:
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.
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.
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.
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.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-27T09:03:42Z.
Review: APPROVED
Reviewed
7221ae6bintegrated with currentmain0d228580(b376eb63locally). 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_checkand ranctest --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.