From 90135aaa2224d0d4c65f9b5b701d6972160f9516 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Wed, 25 Nov 2026 00:30:58 +0100 Subject: [PATCH 1/5] feat: lower 32-bit hex literals as 64-bit source of 64-bit vector shifts Part of #1723 --- CMakeLists.txt | 2 + tests/startup_gfx2hlsl_shift64_check.py | 1 - .../startup_gfx2hlsl_shift64_literal_check.py | 42 +++++++++++++++++++ tools/startup/gfx2hlsl.py | 10 +++-- 4 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 tests/startup_gfx2hlsl_shift64_literal_check.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 6abe7740d..ef05cedbd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2423,6 +2423,8 @@ if(EXISTS "${INTAKE_PYTHON}") # gfx2hlsl 64-bit VOP3 shifts: binary round-trip, overlap, guards and WARP/hardware draws (Refs #1723). add_test(NAME startup_gfx2hlsl_shift64_controls COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_shift64_check.py") + add_test(NAME startup_gfx2hlsl_shift64_literal COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_shift64_literal_check.py") foreach(adapter IN ITEMS warp hardware) add_test(NAME startup_gfx2hlsl_shift64_${adapter} COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_shift64_check.py" draw "${DXC_EXECUTABLE}" diff --git a/tests/startup_gfx2hlsl_shift64_check.py b/tests/startup_gfx2hlsl_shift64_check.py index 71a51c295..32c40fd08 100644 --- a/tests/startup_gfx2hlsl_shift64_check.py +++ b/tests/startup_gfx2hlsl_shift64_check.py @@ -313,7 +313,6 @@ def controls(): 'v_lshrrev_b64 v[0:1], v2, v[255:256]', 'v_lshrrev_b64 v[0:1], v2, vcc', 'v_lshrrev_b64 v[0:1], v2, exec', - 'v_lshrrev_b64 v[0:1], v2, 0x41', 'v_lshrrev_b64 v[0:1], v2, 65', 'v_lshrrev_b64 v[0:1], v2, -17', 'v_lshrrev_b64 v[0:1], v2, 1.0', diff --git a/tests/startup_gfx2hlsl_shift64_literal_check.py b/tests/startup_gfx2hlsl_shift64_literal_check.py new file mode 100644 index 000000000..65d7b337d --- /dev/null +++ b/tests/startup_gfx2hlsl_shift64_literal_check.py @@ -0,0 +1,42 @@ +"""32-bit hex literal as the 64-bit source of v_lshlrev_b64, v_lshrrev_b64 and v_ashrrev_i64 (Refs #1723). + +A literal fills the low word. The high word is zero for the unsigned shifts and copies bit 31 for the signed +shift (the same rule the scalar 64-bit operations use). Pure Python; run with no arguments. +""" +import sys +from pathlib import Path + +HERE = Path(__file__).resolve().parent +sys.path.insert(0, str(HERE.parent / 'tools' / 'startup')) +sys.path.insert(0, str(HERE)) +from startup_gfx2hlsl_bit_alu_check import Probe, check, refused # noqa: E402 + +# (operation, helper, literal, expected low word, expected high word) +CASES = ( + ('v_lshlrev_b64', 'shl64', '0x41', '65u', '0u'), + ('v_lshrrev_b64', 'shr64u', '0x80000000', '2147483648u', '0u'), + ('v_lshrrev_b64', 'shr64u', '0xffffffff', '4294967295u', '0u'), + ('v_ashrrev_i64', 'shr64i', '0x7fffffff', '2147483647u', '0u'), + ('v_ashrrev_i64', 'shr64i', '0x80000000', '2147483648u', '4294967295u'), + ('v_ashrrev_i64', 'shr64i', '0xffffffff', '4294967295u', '4294967295u'), +) + + +def lowered(op, literal): + probe = Probe() + probe.one(op, '%s v[0:1], v2, %s' % (op, literal)) + return probe.lines + + +def main(): + for op, helper, literal, low, high in CASES: + want = ['{ uint2 t = %s(%s, %s, v2); v0 = t.x; v1 = t.y; }' % (helper, low, high)] + check('%s with literal %s' % (op, literal), lowered(op, literal) == want) + check('literals wider than 32 bits stay refused', + all(refused('%s v[0:1], v2, 0x100000000' % op) for op in ('v_lshlrev_b64', 'v_ashrrev_i64'))) + check('a literal is not accepted as a destination or in the wrong operand count', + refused('v_lshrrev_b64 0x41, v2, v[4:5]') and refused('v_lshrrev_b64 v[0:1], 0x41')) + + +if __name__ == '__main__': + main() diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 4e6bc97fb..18914dde3 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -1551,8 +1551,9 @@ def op_v_fma_mixlo_f16(self, ops, mods): def op_v_fma_mixhi_f16(self, ops, mods): self.mix(ops, mods, True) - def i64_words(self, tok): - """(low, high) uint expressions of a 64-bit integer source: a VGPR/SGPR pair or an inline int.""" + def i64_words(self, tok, signed=False): + """(low, high) uint expressions of a 64-bit integer source: a VGPR/SGPR pair, an inline int or a + 32-bit hex literal (zero-extended, sign-extended when the operation is signed).""" pair = reg_range(tok) if pair and pair[2] == pair[1] + 1: if pair[0] == 'v' and pair[2] <= 255: @@ -1563,6 +1564,9 @@ def i64_words(self, tok): if re.fullmatch(r'0|-?[1-9]\d*', tok) and -16 <= int(tok) <= 64: value = int(tok) & 0xffffffffffffffff return '%du' % (value & 0xffffffff), '%du' % (value >> 32) + if re.fullmatch(r'0x[0-9a-f]{1,8}', tok): + word = int(tok, 16) + return '%du' % word, ('4294967295u' if signed and word >> 31 else '0u') raise Unsupported('64-bit source %s' % tok) def shift64(self, ops, mods, helper): @@ -1578,7 +1582,7 @@ def shift64(self, ops, mods, helper): or ops[1] in ('vcc', 'exec') or ops[1] in FLOATS): raise Unsupported('64-bit shift amount %s' % ops[1]) amount = self.u(ops[1]) - low, high = self.i64_words(ops[2]) + low, high = self.i64_words(ops[2], helper == 'shr64i') body = '{ uint2 t = %s(%s, %s, %s); v%d = t.x; v%d = t.y; }' % ( helper, low, high, amount, dst[1], dst[2]) if hasattr(self, 'uniform_v'): From df127ec8e0f59deaa98974e4ee67187d15a65fd8 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Wed, 25 Nov 2026 01:57:30 +0100 Subject: [PATCH 2/5] docs: document 32-bit hex literal source of 64-bit vector shifts --- docs/experiments.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/experiments.md b/docs/experiments.md index 015a5a2d1..1896919e9 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -6704,7 +6704,7 @@ The draws (`pixel_aot_cmp_f64_warp` and `_hardware`) run eight predicate groups ctest --preset dev --no-tests=error -R '^pixel_aot_cmp_f64_(warp|hardware)$' -V ``` -The 64-bit `VOP3` shifts `v_lshlrev_b64`, `v_lshrrev_b64` and `v_ashrrev_i64` (issue #928) follow the [AMD RDNA 2 ISA guide](https://gpuopen.com/download/rdna2-shader-instruction-set-architecture.pdf) §12.12 (opcodes 767-769): `D = S1 << S0[5:0]`, `S1 >> S0[5:0]` and `signext(S1) >> S0[5:0]`. The opcodes match the LLVM 15 gfx1013 encodings, and LLVM 15 folds an IR shift by `x & 63` into the bare instruction while keeping `x & 31`. The value uses the two-word representation of the 64-bit comparisons above, so the HLSL needs no 64-bit type: both source words are read into temporaries before the destination pair is written, so the in-place form keeps the original value, and the result is selected for amounts below and above 32, so no 32-bit HLSL shift receives a count of 32 or more. Only `v_ashrrev_i64` fills with the sign bit. The amount is a VGPR, an SGPR, an inline integer from -16 to 64 or a literal; the value is a VGPR pair, an SGPR pair or an inline integer, sign-extended to 64 bits as for the comparisons (assumed, not sourced). At most one source may be an SGPR or a literal (the ISA's one scalar broadcast constant; LLVM 15 refuses the others with `violates constant bus restrictions`). `_e32`, `clamp`, `op_sel` and source modifiers, a destination that is not a pair in `v0`..`v255` or has an outstanding image sample, float amounts and use inside a masked region are refused. The draw (`tests/pixel_aot_check.py draw-shift64`, tests `pixel_aot_shift64_warp` and `pixel_aot_shift64_hardware`) compiles three programs per instruction (register sources; the in-place pair next to the literal amount `0x41`, which wraps to 1; and an SGPR-pair value next to the inline `-1`) and reads the four result words back through `R32G32B32A32_UINT` for three values (one with a negative high word) and the amounts 0, 1, 31, 32, 33, 63, 64, 65, `0xffffffc1` and `0x80000020`, against a Python reference written from the ISA formulas. Three mutated `v_ashrrev_i64` shaders must each mismatch: the amount masked to five bits, the sign fill replaced with zeros, and the result words swapped. The LLVM 15 round trip covers the nine programs; the scanner accepts the same operand shapes and `tests/rdna2_tools_check.py` pins them. The draw pins the translator's model on a desktop adapter; it is not evidence for gfx1013 hardware. +The 64-bit `VOP3` shifts `v_lshlrev_b64`, `v_lshrrev_b64` and `v_ashrrev_i64` (issue #928) follow the [AMD RDNA 2 ISA guide](https://gpuopen.com/download/rdna2-shader-instruction-set-architecture.pdf) §12.12 (opcodes 767-769): `D = S1 << S0[5:0]`, `S1 >> S0[5:0]` and `signext(S1) >> S0[5:0]`. The opcodes match the LLVM 15 gfx1013 encodings, and LLVM 15 folds an IR shift by `x & 63` into the bare instruction while keeping `x & 31`. The value uses the two-word representation of the 64-bit comparisons above, so the HLSL needs no 64-bit type: both source words are read into temporaries before the destination pair is written, so the in-place form keeps the original value, and the result is selected for amounts below and above 32, so no 32-bit HLSL shift receives a count of 32 or more. Only `v_ashrrev_i64` fills with the sign bit. The amount is a VGPR, an SGPR, an inline integer from -16 to 64 or a literal; the value is a VGPR pair, an SGPR pair, an inline integer, sign-extended to 64 bits as for the comparisons (assumed, not sourced), or a 32-bit hex literal (`0x0` to `0xffffffff`, written in hex form). A literal fills the low word; the high word is zero for `v_lshlrev_b64` and `v_lshrrev_b64` and copies bit 31 for `v_ashrrev_i64`, the rule the scalar 64-bit operations use (an assumption, not sourced from the ISA text). A decimal literal outside -16 to 64, a hex literal wider than 32 bits and a literal as destination stay refused. `ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_shift64_literal$' -V` checks six lowered forms (including `0x80000000` and `0xffffffff`, where the signed shift differs) and these refusals; it is pure Python and needs no adapter. At most one source may be an SGPR or a literal (the ISA's one scalar broadcast constant; LLVM 15 refuses the others with `violates constant bus restrictions`). `_e32`, `clamp`, `op_sel` and source modifiers, a destination that is not a pair in `v0`..`v255` or has an outstanding image sample, float amounts and use inside a masked region are refused. The draw (`tests/pixel_aot_check.py draw-shift64`, tests `pixel_aot_shift64_warp` and `pixel_aot_shift64_hardware`) compiles three programs per instruction (register sources; the in-place pair next to the literal amount `0x41`, which wraps to 1; and an SGPR-pair value next to the inline `-1`) and reads the four result words back through `R32G32B32A32_UINT` for three values (one with a negative high word) and the amounts 0, 1, 31, 32, 33, 63, 64, 65, `0xffffffc1` and `0x80000020`, against a Python reference written from the ISA formulas. Three mutated `v_ashrrev_i64` shaders must each mismatch: the amount masked to five bits, the sign fill replaced with zeros, and the result words swapped. The LLVM 15 round trip covers the nine programs; the scanner accepts the same operand shapes and `tests/rdna2_tools_check.py` pins them. The draw pins the translator's model on a desktop adapter; it is not evidence for gfx1013 hardware. The non-packed 16-bit integer `VOP3` ops (issue #669) `v_add_nc_u16`, `v_add_nc_i16`, `v_sub_nc_u16`, `v_sub_nc_i16`, `v_min_u16`, `v_max_u16`, `v_min_i16`, `v_max_i16`, `v_mul_lo_u16`, `v_lshlrev_b16`, `v_lshrrev_b16` and `v_ashrrev_i16` compute on the low 16 bits of two sources and reuse the lane arithmetic of the packed forms: results wrap modulo 2^16, signed min, max and shifts sign-extend the 16-bit value, and shift counts are masked to 4 bits (`& 15`, so a count of 16 shifts by 0). Only the low 16 bits of the result are defined and tracked: the lowering records the destination as `("low16", alu)`, a value whose HLSL variable holds the zero-extended result but whose upper half no accepted reader observes. The destination's previous value is not merged, so a destination may hold anything (a tracked value, an untracked register or another low16 result); a destination with an outstanding image sample is refused. The same applies to the unsuffixed and the `_e64` spelling. The readers that see a low16 value are the 12 ops themselves (a chain of two ops is accepted), the 16-bit integer compares `v_cmp_{eq,ne,lt,le,gt,ge}_{i16,u16}` and `v_mov_b32_sdwa` with `src0_sel:WORD_0`, `BYTE_0` or `BYTE_1` (also with `sext()`), which widens the low half into a fully defined word. Every whole-word or high-half reader is refused: 32-bit integer and float arithmetic, moves, `v_cndmask_b32` data operands, DPP, packed `VOP3P` forms, conversions (including `v_cvt_f32_f16`, which is out of scope for integer low16; binary16 arithmetic results use the distinct `half16` tag, below), dot products, class compares, packs, both export forms and image sample coordinates raise `Expected a numeric register`, `Expected a numeric VGPR in DPP`, `Unsupported pack operand`, `Unsupported export` or `Unsupported texture coordinate`; `v_cvt_f16_f32` into a low16 destination raises `Unsupported conversion destination`; and `v_mov_b32_sdwa` with `WORD_1`, `BYTE_2`, `BYTE_3` or `DWORD` on a low16 source, or with `dst_unused:UNUSED_PRESERVE` into a low16 destination, raises `Reads the undefined high half of a 16-bit result`. A source may be a VGPR in 0 to 255 that holds a tracked value, a tracked SGPR in 0 to 105, an integer inline constant from -16 to 64 or a literal up to `0xffff`. `op_sel`, `clamp`, source modifiers, DPP, SDWA, the f16 ops, float tokens, wider literals, inline integers outside -16 to 64 (llvm-mc would encode `-17` as the literal `0xffef`; the translator refuses it by policy, as for the 16-bit compares), an untracked source, a register out of range and a destination with an outstanding image sample are refused. `tests/form_coverage_check.py` asserts that the 12 unsuffixed forms are counted while the f16 neighbours are not. `tools/scan_unsupported_shaders.py` matches instructions syntactically and cannot see the low16 tags, so it can report a shader as supported that the translator then refuses at a whole-word reader. The binary fixtures (four programs; groups 0 to 2 widen all four results and group 3 its two low-half results with `v_mov_b32_sdwa ... src0_sel:WORD_0`) round-trip through LLVM 15.0.7, and the controls refuse every form above with its message. Reproduce with `ctest --preset dev --no-tests=error -R "^pixel_aot_int16_vop3_"`: groups 0 to 2 preload `v24` to `v27` with a `0xa5c3xxxx` sentinel, run four ops on constant words, widen each result into `v12` to `v15` with SDWA `WORD_0` and export through the bit-exact `R32G32B32A32_UINT` target against a Python reference written from the semantics that contains only the zero-extended low halves; group 3 chains `v_add_nc_u16` into `v_mul_lo_u16` and selects between whole words with `v_cmp_lt_u16` and `v_cmp_lt_i16` on low16 results. Six word sets cover `0x7fff` against `0x8000`, `0xffff + 1`, `0 - 1`, shift counts 15, 16, 17 and 31, a negative operand under `ashrrev`, signed and unsigned min and max that disagree, the overflow `0x1234 * 0x0567`, and sources whose high half differs from the low half. Five mutations of the generated shader must each report a pixel mismatch (a source read from bits 31:16, a shift mask of 31 instead of 15, `v_min_i16` replaced by `v_min_u16`, and a `>> 16u` read of a low16 result by the chained op and by the first compare), and a widening that selects `WORD_1` instead of `WORD_0` must be refused at lowering. The sentinel preloads do not detect a high-half leak, because every accepted reader skips the high half; that half is guarded only by the refusal controls. The binary round trip disassembles through libLLVM; `tools/disasm_rdna2.py` decodes the same encodings. [AMD RDNA 2 ISA reference](https://docs.amd.com/v/u/en-US/rdna2-shader-instruction-set-architecture) From 45a18be54f9a0804ffbfa2b5f20f50e958b35650 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Wed, 25 Nov 2026 23:28:52 +0100 Subject: [PATCH 3/5] fix: sign-extend 32-bit hex literal for s_ashr_i64 source --- tests/startup_gfx2hlsl_shift64_literal_check.py | 12 ++++++++++++ tools/startup/gfx2hlsl.py | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/startup_gfx2hlsl_shift64_literal_check.py b/tests/startup_gfx2hlsl_shift64_literal_check.py index 65d7b337d..8bef0d89b 100644 --- a/tests/startup_gfx2hlsl_shift64_literal_check.py +++ b/tests/startup_gfx2hlsl_shift64_literal_check.py @@ -3,12 +3,14 @@ A literal fills the low word. The high word is zero for the unsigned shifts and copies bit 31 for the signed shift (the same rule the scalar 64-bit operations use). Pure Python; run with no arguments. """ +import struct import sys from pathlib import Path HERE = Path(__file__).resolve().parent sys.path.insert(0, str(HERE.parent / 'tools' / 'startup')) sys.path.insert(0, str(HERE)) +import startup_gfx2hlsl_scalar_m0_check as M # noqa: E402 from startup_gfx2hlsl_bit_alu_check import Probe, check, refused # noqa: E402 # (operation, helper, literal, expected low word, expected high word) @@ -28,7 +30,17 @@ def lowered(op, literal): return probe.lines +def scalar_lowered(opcode): + """s_ s[20:21], 0xffffffff, s9 as encoded (SOP2, source 255 plus a literal dword).""" + stage = M.run_seq(M.encode_sop2(opcode, 20, 255, 9) + struct.pack(' Date: Thu, 26 Nov 2026 06:49:53 +0100 Subject: [PATCH 4/5] fix: sign-extend hex literal source of s_bfe_i64 --- docs/experiments.md | 2 +- tests/startup_gfx2hlsl_shift64_literal_check.py | 8 ++++++-- tools/startup/gfx2hlsl.py | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/docs/experiments.md b/docs/experiments.md index 66746b41a..c6a095c78 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -8261,7 +8261,7 @@ The draws (`pixel_aot_cmp_f64_warp` and `_hardware`) run eight predicate groups ctest --preset dev --no-tests=error -R '^pixel_aot_cmp_f64_(warp|hardware)$' -V ``` -The 64-bit `VOP3` shifts `v_lshlrev_b64`, `v_lshrrev_b64` and `v_ashrrev_i64` (issue #928) follow the [AMD RDNA 2 ISA guide](https://gpuopen.com/download/rdna2-shader-instruction-set-architecture.pdf) §12.12 (opcodes 767-769): `D = S1 << S0[5:0]`, `S1 >> S0[5:0]` and `signext(S1) >> S0[5:0]`. The opcodes match the LLVM 15 gfx1013 encodings, and LLVM 15 folds an IR shift by `x & 63` into the bare instruction while keeping `x & 31`. The value uses the two-word representation of the 64-bit comparisons above, so the HLSL needs no 64-bit type: both source words are read into temporaries before the destination pair is written, so the in-place form keeps the original value, and the result is selected for amounts below and above 32, so no 32-bit HLSL shift receives a count of 32 or more. Only `v_ashrrev_i64` fills with the sign bit. The amount is a VGPR, an SGPR, an inline integer from -16 to 64 or a literal; the value is a VGPR pair, an SGPR pair, an inline integer, sign-extended to 64 bits as for the comparisons (assumed, not sourced), or a 32-bit hex literal (`0x0` to `0xffffffff`, written in hex form). A literal fills the low word; the high word is zero for `v_lshlrev_b64` and `v_lshrrev_b64` and copies bit 31 for `v_ashrrev_i64`, the rule the scalar 64-bit operations use (an assumption, not sourced from the ISA text). A decimal literal outside -16 to 64, a hex literal wider than 32 bits and a literal as destination stay refused. `ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_shift64_literal$' -V` checks six lowered forms (including `0x80000000` and `0xffffffff`, where the signed shift differs) and these refusals; it is pure Python and needs no adapter. At most one source may be an SGPR or a literal (the ISA's one scalar broadcast constant; LLVM 15 refuses the others with `violates constant bus restrictions`). `_e32`, `clamp`, `op_sel` and source modifiers, a destination that is not a pair in `v0`..`v255` or has an outstanding image sample, float amounts and use inside a masked region are refused. The draw (`tests/pixel_aot_check.py draw-shift64`, tests `pixel_aot_shift64_warp` and `pixel_aot_shift64_hardware`) compiles three programs per instruction (register sources; the in-place pair next to the literal amount `0x41`, which wraps to 1; and an SGPR-pair value next to the inline `-1`) and reads the four result words back through `R32G32B32A32_UINT` for three values (one with a negative high word) and the amounts 0, 1, 31, 32, 33, 63, 64, 65, `0xffffffc1` and `0x80000020`, against a Python reference written from the ISA formulas. Three mutated `v_ashrrev_i64` shaders must each mismatch: the amount masked to five bits, the sign fill replaced with zeros, and the result words swapped. The LLVM 15 round trip covers the nine programs; the scanner accepts the same operand shapes and `tests/rdna2_tools_check.py` pins them. The draw pins the translator's model on a desktop adapter; it is not evidence for gfx1013 hardware. +The 64-bit `VOP3` shifts `v_lshlrev_b64`, `v_lshrrev_b64` and `v_ashrrev_i64` (issue #928) follow the [AMD RDNA 2 ISA guide](https://gpuopen.com/download/rdna2-shader-instruction-set-architecture.pdf) §12.12 (opcodes 767-769): `D = S1 << S0[5:0]`, `S1 >> S0[5:0]` and `signext(S1) >> S0[5:0]`. The opcodes match the LLVM 15 gfx1013 encodings, and LLVM 15 folds an IR shift by `x & 63` into the bare instruction while keeping `x & 31`. The value uses the two-word representation of the 64-bit comparisons above, so the HLSL needs no 64-bit type: both source words are read into temporaries before the destination pair is written, so the in-place form keeps the original value, and the result is selected for amounts below and above 32, so no 32-bit HLSL shift receives a count of 32 or more. Only `v_ashrrev_i64` fills with the sign bit. The amount is a VGPR, an SGPR, an inline integer from -16 to 64 or a literal; the value is a VGPR pair, an SGPR pair, an inline integer, sign-extended to 64 bits as for the comparisons (assumed, not sourced), or a 32-bit hex literal (`0x0` to `0xffffffff`, written in hex form). A literal fills the low word; the high word is zero for `v_lshlrev_b64` and `v_lshrrev_b64` and copies bit 31 for `v_ashrrev_i64`. The same hex literal is now also the 64-bit source of the scalar `s_lshl_b64`, `s_lshr_b64`, `s_ashr_i64`, `s_bfe_u64` and `s_bfe_i64`, which refused it before; the high word is zero for the unsigned operations and copies bit 31 for the signed ones (`s_ashr_i64`, `s_bfe_i64`). Only `s_mov_b64` zero-extended a hex literal before this change. The extension rule is an assumption, not sourced from the ISA text. A decimal literal outside -16 to 64, a hex literal wider than 32 bits and a literal as destination stay refused. `ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_shift64_literal$' -V` checks six lowered vector forms and the scalar `s_ashr_i64`, `s_lshr_b64`, `s_bfe_i64` and `s_bfe_u64` (including `0x80000000` and `0xffffffff`, where the signed forms differ) and these refusals; it is pure Python and needs no adapter. At most one source may be an SGPR or a literal (the ISA's one scalar broadcast constant; LLVM 15 refuses the others with `violates constant bus restrictions`). `_e32`, `clamp`, `op_sel` and source modifiers, a destination that is not a pair in `v0`..`v255` or has an outstanding image sample, float amounts and use inside a masked region are refused. The draw (`tests/pixel_aot_check.py draw-shift64`, tests `pixel_aot_shift64_warp` and `pixel_aot_shift64_hardware`) compiles three programs per instruction (register sources; the in-place pair next to the literal amount `0x41`, which wraps to 1; and an SGPR-pair value next to the inline `-1`) and reads the four result words back through `R32G32B32A32_UINT` for three values (one with a negative high word) and the amounts 0, 1, 31, 32, 33, 63, 64, 65, `0xffffffc1` and `0x80000020`, against a Python reference written from the ISA formulas. Three mutated `v_ashrrev_i64` shaders must each mismatch: the amount masked to five bits, the sign fill replaced with zeros, and the result words swapped. The LLVM 15 round trip covers the nine programs; the scanner accepts the same operand shapes and `tests/rdna2_tools_check.py` pins them. The draw pins the translator's model on a desktop adapter; it is not evidence for gfx1013 hardware. The non-packed 16-bit integer `VOP3` ops (issue #669) `v_add_nc_u16`, `v_add_nc_i16`, `v_sub_nc_u16`, `v_sub_nc_i16`, `v_min_u16`, `v_max_u16`, `v_min_i16`, `v_max_i16`, `v_mul_lo_u16`, `v_lshlrev_b16`, `v_lshrrev_b16` and `v_ashrrev_i16` compute on the low 16 bits of two sources and reuse the lane arithmetic of the packed forms: results wrap modulo 2^16, signed min, max and shifts sign-extend the 16-bit value, and shift counts are masked to 4 bits (`& 15`, so a count of 16 shifts by 0). Only the low 16 bits of the result are defined and tracked: the lowering records the destination as `("low16", alu)`, a value whose HLSL variable holds the zero-extended result but whose upper half no accepted reader observes. The destination's previous value is not merged, so a destination may hold anything (a tracked value, an untracked register or another low16 result); a destination with an outstanding image sample is refused. The same applies to the unsuffixed and the `_e64` spelling. The readers that see a low16 value are the 12 ops themselves (a chain of two ops is accepted), the 16-bit integer compares `v_cmp_{eq,ne,lt,le,gt,ge}_{i16,u16}` and `v_mov_b32_sdwa` with `src0_sel:WORD_0`, `BYTE_0` or `BYTE_1` (also with `sext()`), which widens the low half into a fully defined word. Every whole-word or high-half reader is refused: 32-bit integer and float arithmetic, moves, `v_cndmask_b32` data operands, DPP, packed `VOP3P` forms, conversions (including `v_cvt_f32_f16`, which is out of scope for integer low16; binary16 arithmetic results use the distinct `half16` tag, below), dot products, class compares, packs, both export forms and image sample coordinates raise `Expected a numeric register`, `Expected a numeric VGPR in DPP`, `Unsupported pack operand`, `Unsupported export` or `Unsupported texture coordinate`; `v_cvt_f16_f32` into a low16 destination raises `Unsupported conversion destination`; and `v_mov_b32_sdwa` with `WORD_1`, `BYTE_2`, `BYTE_3` or `DWORD` on a low16 source, or with `dst_unused:UNUSED_PRESERVE` into a low16 destination, raises `Reads the undefined high half of a 16-bit result`. A source may be a VGPR in 0 to 255 that holds a tracked value, a tracked SGPR in 0 to 105, an integer inline constant from -16 to 64 or a literal up to `0xffff`. `op_sel`, `clamp`, source modifiers, DPP, SDWA, the f16 ops, float tokens, wider literals, inline integers outside -16 to 64 (llvm-mc would encode `-17` as the literal `0xffef`; the translator refuses it by policy, as for the 16-bit compares), an untracked source, a register out of range and a destination with an outstanding image sample are refused. `tests/form_coverage_check.py` asserts that the 12 unsuffixed forms are counted while the f16 neighbours are not. `tools/scan_unsupported_shaders.py` matches instructions syntactically and cannot see the low16 tags, so it can report a shader as supported that the translator then refuses at a whole-word reader. The binary fixtures (four programs; groups 0 to 2 widen all four results and group 3 its two low-half results with `v_mov_b32_sdwa ... src0_sel:WORD_0`) round-trip through LLVM 15.0.7, and the controls refuse every form above with its message. Reproduce with `ctest --preset dev --no-tests=error -R "^pixel_aot_int16_vop3_"`: groups 0 to 2 preload `v24` to `v27` with a `0xa5c3xxxx` sentinel, run four ops on constant words, widen each result into `v12` to `v15` with SDWA `WORD_0` and export through the bit-exact `R32G32B32A32_UINT` target against a Python reference written from the semantics that contains only the zero-extended low halves; group 3 chains `v_add_nc_u16` into `v_mul_lo_u16` and selects between whole words with `v_cmp_lt_u16` and `v_cmp_lt_i16` on low16 results. Six word sets cover `0x7fff` against `0x8000`, `0xffff + 1`, `0 - 1`, shift counts 15, 16, 17 and 31, a negative operand under `ashrrev`, signed and unsigned min and max that disagree, the overflow `0x1234 * 0x0567`, and sources whose high half differs from the low half. Five mutations of the generated shader must each report a pixel mismatch (a source read from bits 31:16, a shift mask of 31 instead of 15, `v_min_i16` replaced by `v_min_u16`, and a `>> 16u` read of a low16 result by the chained op and by the first compare), and a widening that selects `WORD_1` instead of `WORD_0` must be refused at lowering. The sentinel preloads do not detect a high-half leak, because every accepted reader skips the high half; that half is guarded only by the refusal controls. The binary round trip disassembles through libLLVM; `tools/disasm_rdna2.py` decodes the same encodings. [AMD RDNA 2 ISA reference](https://docs.amd.com/v/u/en-US/rdna2-shader-instruction-set-architecture) diff --git a/tests/startup_gfx2hlsl_shift64_literal_check.py b/tests/startup_gfx2hlsl_shift64_literal_check.py index 8bef0d89b..08c35f4d3 100644 --- a/tests/startup_gfx2hlsl_shift64_literal_check.py +++ b/tests/startup_gfx2hlsl_shift64_literal_check.py @@ -1,7 +1,7 @@ """32-bit hex literal as the 64-bit source of v_lshlrev_b64, v_lshrrev_b64 and v_ashrrev_i64 (Refs #1723). A literal fills the low word. The high word is zero for the unsigned shifts and copies bit 31 for the signed -shift (the same rule the scalar 64-bit operations use). Pure Python; run with no arguments. +shift (the rule of s_mov_b64; the scalar shifts and bitfield extracts take it from this change). Pure Python; run with no arguments. """ import struct import sys @@ -33,7 +33,7 @@ def lowered(op, literal): def scalar_lowered(opcode): """s_ s[20:21], 0xffffffff, s9 as encoded (SOP2, source 255 plus a literal dword).""" stage = M.run_seq(M.encode_sop2(opcode, 20, 255, 9) + struct.pack(' Date: Thu, 26 Nov 2026 06:58:53 +0100 Subject: [PATCH 5/5] fix: keep hex literal refused for v_mad64 addend, align scanner and Linux CI --- .github/workflows/windows.yml | 1 + tests/rdna2_tools_check.py | 4 ++-- tests/startup_gfx2hlsl_shift64_literal_check.py | 2 ++ tools/scan_unsupported_shaders.py | 4 ++-- tools/startup/gfx2hlsl.py | 10 +++++----- 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index cb71ae8c5..78f1a6d88 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -339,6 +339,7 @@ jobs: tests/startup_gfx2hlsl_s_quadmask_check.py \ tests/startup_gfx2hlsl_fma_mix_check.py \ tests/startup_gfx2hlsl_shift64_check.py \ + tests/startup_gfx2hlsl_shift64_literal_check.py \ tests/startup_gfx2hlsl_mad64_check.py \ tests/startup_gfx2hlsl_cmp_class_f16_sdwa_check.py \ tests/startup_gfx2hlsl_mul_hi_i32_check.py \ diff --git a/tests/rdna2_tools_check.py b/tests/rdna2_tools_check.py index 2aaaf9789..32ce96a37 100644 --- a/tests/rdna2_tools_check.py +++ b/tests/rdna2_tools_check.py @@ -6158,10 +6158,10 @@ def test_shift64_scanner_support(self): """Issue #928: the 64-bit VOP3 shifts are supported with at most one SGPR or literal source.""" for line in ("v_lshlrev_b64 v[12:13], v20, v[18:19]", "v_lshrrev_b64 v[2:3], s46, v[4:5]", "v_ashrrev_i64 v[2:3], 0x41, v[4:5]", "v_lshlrev_b64 v[2:3], v1, s[4:5]", - "v_ashrrev_i64 v[2:3], 3, -1", "v_lshlrev_b64_e64 v[2:3], v1, v[4:5]"): + "v_ashrrev_i64 v[2:3], 3, -1", "v_lshlrev_b64 v[2:3], v1, 0x41", "v_lshlrev_b64_e64 v[2:3], v1, v[4:5]"): self.assertTrue(is_instruction_supported(line), line) for line in ("v_lshlrev_b64 v[2:3], s4, s[4:5]", "v_lshlrev_b64 v[2:3], 0x41, s[4:5]", - "v_lshlrev_b64 v[2:3], v1, 0x41", "v_lshlrev_b64 v[2:3], v1, v[4:5] clamp", + "v_lshlrev_b64 v[2:3], s4, 0x41", "v_lshlrev_b64 v[2:3], v1, v[4:5] clamp", "v_lshlrev_b64_e32 v[2:3], v1, v[4:5]", "v_lshlrev_b64 v[2:3], -v1, v[4:5]"): self.assertFalse(is_instruction_supported(line), line) diff --git a/tests/startup_gfx2hlsl_shift64_literal_check.py b/tests/startup_gfx2hlsl_shift64_literal_check.py index 08c35f4d3..c6abbcb27 100644 --- a/tests/startup_gfx2hlsl_shift64_literal_check.py +++ b/tests/startup_gfx2hlsl_shift64_literal_check.py @@ -50,6 +50,8 @@ def main(): check('%s with literal %s' % (op, literal), lowered(op, literal) == want) check('literals wider than 32 bits stay refused', all(refused('%s v[0:1], v2, 0x100000000' % op) for op in ('v_lshlrev_b64', 'v_ashrrev_i64'))) + check('a hex literal stays refused as the addend of v_mad_u64_u32 and v_mad_i64_i32', + all(refused('%s v[0:1], vcc_lo, v2, v3, 0x41' % op) for op in ('v_mad_u64_u32', 'v_mad_i64_i32'))) check('a literal is not accepted as a destination or in the wrong operand count', refused('v_lshrrev_b64 0x41, v2, v[4:5]') and refused('v_lshrrev_b64 v[0:1], 0x41')) diff --git a/tools/scan_unsupported_shaders.py b/tools/scan_unsupported_shaders.py index b81d26544..43ffd8878 100644 --- a/tools/scan_unsupported_shaders.py +++ b/tools/scan_unsupported_shaders.py @@ -310,14 +310,14 @@ def lower_rejection_causes(results: list[dict]) -> dict: rf"\s+{INT16_SOURCE},\s+{INT16_SOURCE},\s+{INT16_SOURCE}" ) # 64-bit VOP3 shifts (issue #928): a VGPR-pair destination, a 32-bit amount (VGPR, SGPR, inline integer or -# literal) and a 64-bit value (VGPR pair, SGPR pair or inline integer), with at most one SGPR or literal source. +# literal) and a 64-bit value (VGPR pair, SGPR pair, inline integer or 32-bit hex literal), with at most one SGPR or literal source. # This matches syntax only; pixel_aot also checks the pair ranges and that the sources are tracked. SHIFT64_AMOUNT = r"(?:v\d+|-?\d+)" SHIFT64_VALUE = r"(?:v\[\d+:\d+\]|-?\d+)" SUPPORTED_INSTRUCTIONS_REGEX.append( r"v_(?:lshlrev_b64|lshrrev_b64|ashrrev_i64)(?:_e64)?\s+v\[\d+:\d+\],\s+" rf"(?:{SHIFT64_AMOUNT},\s+{SHIFT64_VALUE}|(?:s\d+|0x[0-9a-f]{{1,8}}),\s+{SHIFT64_VALUE}" - rf"|{SHIFT64_AMOUNT},\s+s\[\d+:\d+\])" + rf"|{SHIFT64_AMOUNT},\s+s\[\d+:\d+\]|{SHIFT64_AMOUNT},\s+0x[0-9a-f]{{1,8}})" ) for name, (arity, _) in PIXEL_INTEGER.items(): diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index d419658e4..7911e17c4 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -5170,7 +5170,7 @@ def scalar_wide_op(self, name, ops, mods, wide_src, wide_dst, helper, signed=Non r0 = reg_range(ops[1]) if r0 and r0[0] != 's': raise Unsupported('%s source %s' % (name, ops[1])) - low, high = self.i64_words(ops[1], helper == 'shr64i' or signed is True) + low, high = self.i64_words(ops[1], helper == 'shr64i' or signed is True, True) known = [self.scalar_const('s%d' % r0[1]), self.scalar_const('s%d' % r0[2])] if r0 else [ int(low[:-1]), int(high[:-1])] else: @@ -7629,9 +7629,9 @@ def op_v_cubetc_f32(self, ops, mods): def op_v_cubema_f32(self, ops, mods): self.cube('ma', ops, mods) - def i64_words(self, tok, signed=False): + def i64_words(self, tok, signed=False, literal=False): """(low, high) uint expressions of a 64-bit integer source: a VGPR/SGPR pair, an inline int or a - 32-bit hex literal (zero-extended, sign-extended when the operation is signed).""" + 32-bit hex literal when `literal` is set (zero-extended, sign-extended when the operation is signed).""" pair = reg_range(tok) if pair and pair[2] == pair[1] + 1: if pair[0] == 'v' and pair[2] <= 255: @@ -7642,7 +7642,7 @@ def i64_words(self, tok, signed=False): if re.fullmatch(r'0|-?[1-9]\d*', tok) and -16 <= int(tok) <= 64: value = int(tok) & 0xffffffffffffffff return '%du' % (value & 0xffffffff), '%du' % (value >> 32) - if re.fullmatch(r'0x[0-9a-f]{1,8}', tok): + if literal and re.fullmatch(r'0x[0-9a-f]{1,8}', tok): word = int(tok, 16) return '%du' % word, ('4294967295u' if signed and word >> 31 else '0u') raise Unsupported('64-bit source %s' % tok) @@ -7660,7 +7660,7 @@ def shift64(self, ops, mods, helper): or ops[1] in ('vcc', 'exec') or ops[1] in FLOATS): raise Unsupported('64-bit shift amount %s' % ops[1]) amount = self.u(ops[1]) - low, high = self.i64_words(ops[2], helper == 'shr64i') + low, high = self.i64_words(ops[2], helper == 'shr64i', True) body = '{ uint2 t = %s(%s, %s, %s); v%d = t.x; v%d = t.y; }' % ( helper, low, high, amount, dst[1], dst[2]) if hasattr(self, 'uniform_v'):