Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2047,6 +2047,9 @@ if(EXISTS "${INTAKE_PYTHON}")
# The 64-bit vector shifts (#1815).
add_test(NAME startup_gfx2hlsl_b64_shift COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_b64_shift_check.py")
# The high half of an unsigned 32x32 product (#1792).
add_test(NAME startup_gfx2hlsl_mul_hi_u32 COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_mul_hi_u32_check.py")
# SDWA forms of the float-result conversions (#1727).
add_test(NAME startup_gfx2hlsl_sdwa_cvt COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_sdwa_cvt_check.py")
Expand Down
7 changes: 7 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -6073,6 +6073,13 @@ the handler writes back in one statement. VGPR pairs, SGPR pairs and the integer
sources; the low word is always shifted unsigned and only the arithmetic form signs the high word.
Run `ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_b64_shift$' -V`.

**The high half of an unsigned product.** `v_mul_hi_u32` had no lowering - the census form
`v_mul_hi_u32 v1, v0, 0xaaaaaaab` was one of the refused shaders in the loading-screen cache (#870, issue #1792).
These stages have no 64-bit multiply and a plain `*` yields the low half, so the handler calls a preamble helper
built from halfword products. The check builds synthetic VOP3A words, decodes and translates them, and fails
without the helper. `v_mul_hi_i32` and the 24-bit forms are not part of this change.
Run `ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_mul_hi_u32$' -V`.

**The 32-bit scalar complement logic operations.** The scalar table had `s_nand_b64`, `s_nor_b64` and
`s_xnor_b64` and the 32-bit `s_andn2_b32` and `s_orn2_b32`, but not `s_nand_b32`, `s_nor_b32` or `s_xnor_b32`
(#870, issue #1919). They now lower as `~(S0 & S1)`, `~(S0 | S1)` and `~(S0 ^ S1)` on 32 bits, the text
Expand Down
93 changes: 93 additions & 0 deletions tests/startup_gfx2hlsl_mul_hi_u32_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
"""The v_mul_hi_u32 lowering in tools/startup/gfx2hlsl.py (issue #1792).

usage: startup_gfx2hlsl_mul_hi_u32_check.py

`v_mul_hi_u32` (the high 32 bits of an unsigned 32x32 product) had no lowering, so the census form
`v_mul_hi_u32 v1, v0, 0xaaaaaaab` was refused (#870). A plain `*` would be silently wrong: it yields the low half.
The lowering calls a preamble helper built from halfword products, because these stages have no 64-bit multiply.

The instruction bytes are **constructed here** from the field layout the checked-in decoder reads
(`tools/disasm_rdna2.py`, VOP3A branch, opcode 0x16a): prefix `0b110101`, opcode in `w0[25:16]`, the
destination in `w0[7:0]`, src0 in `w1[8:0]`, src1 in `w1[17:9]`, and a literal dword after the pair.
"""
import os
import struct
import sys

here = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(here, '..', 'tools'))
sys.path.insert(0, os.path.join(here, '..', 'tools', 'startup'))
import gfx2hlsl as G # noqa: E402

failures = []


def check(label, condition, detail=''):
print(f"{label}: {'ok' if condition else 'MISMATCH'}")
if not condition:
failures.append(label)
if detail:
print(' ' + detail)


VOP3A_MUL_HI_U32 = 0x16A
VOP3A_MUL_LO_U32 = 0x169
LITERAL = 255
V0, V1, V2 = 0x100, 0x101, 0x102


def encode_vop3a(op, dst, src0, src1, literal=None):
w0 = (0b110101 << 26) | ((op & 0x3FF) << 16) | (dst & 0xFF)
w1 = (src0 & 0x1FF) | ((src1 & 0x1FF) << 9)
code = struct.pack('<II', w0, w1)
return code + struct.pack('<I', literal) if literal is not None else code


class Pipeline:
def __init__(self):
self.resources, self.notes, self.modified = {}, set(), set()


class Stage(G.Stage):
"""A Stage with no pipeline: enough state for a decoded instruction to be translated."""

def __init__(self):
self.lines, self.sym = [], {}
self.used_s, self.used_v, self.uniform_v = set(), set(), set()
self.exec_known = True
self.exports = {}
self.pipe = Pipeline()


def translate(code):
decoded = G.decode(code)
stage = Stage()
for _offset, _size, name, asm in decoded:
stage.one(name, asm)
return [asm for _o, _s, _n, asm in decoded], ' '.join(stage.lines)


asm, emitted = translate(encode_vop3a(VOP3A_MUL_HI_U32, 1, V1, V2))
check('register form decodes', asm == ['v_mul_hi_u32 v1, v1, v2'], repr(asm))
check('register form calls the helper', 'v1 = mul_hi_u32(v1, v2);' in emitted, emitted)

asm, emitted = translate(encode_vop3a(VOP3A_MUL_HI_U32, 1, V0, LITERAL, 0xAAAAAAAB))
check('census form decodes', asm == ['v_mul_hi_u32 v1, v0, 0xaaaaaaab'], repr(asm))
check('census form calls the helper', 'v1 = mul_hi_u32(v0, 0xaaaaaaabu);' in emitted, emitted)

asm, emitted = translate(encode_vop3a(VOP3A_MUL_LO_U32, 1, V1, V2))
check('the low multiply is unchanged', 'v1 = v1 * v2;' in emitted and 'mul_hi_u32' not in emitted, emitted)

pipe = object.__new__(G.Pipeline)
pipe.uds, pipe.resources, pipe.samplers, pipe.uses_sampler_heap = [], {}, {}, False
common = ' '.join(pipe.common())
check('the helper is in the common preamble', 'uint mul_hi_u32(uint a, uint b)' in common, common[:240])
check('the helper uses halfword products', '& 0xffffu' in common.split('mul_hi_u32(uint a')[1].split('}')[0]
and '>> 16' in common.split('mul_hi_u32(uint a')[1].split('}')[0])

if failures:
print('FAIL')
for text in failures:
print(' ' + text)
sys.exit(1)
print('gfx2hlsl mul_hi_u32: all checks passed')
7 changes: 6 additions & 1 deletion tools/startup/gfx2hlsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1594,7 +1594,7 @@ def declarations(self):
# operands are uints; the result is a uint expression
VEC_U = {
'v_add_nc_u32': (2, '{0} + {1}'), 'v_sub_nc_u32': (2, '{0} - {1}'), 'v_subrev_nc_u32': (2, '{1} - {0}'),
'v_mul_lo_u32': (2, '{0} * {1}'), 'v_and_b32': (2, '{0} & {1}'), 'v_or_b32': (2, '{0} | {1}'), 'v_xor_b32': (2, '{0} ^ {1}'),
'v_mul_lo_u32': (2, '{0} * {1}'), 'v_mul_hi_u32': (2, 'mul_hi_u32({0}, {1})'), 'v_and_b32': (2, '{0} & {1}'), 'v_or_b32': (2, '{0} | {1}'), 'v_xor_b32': (2, '{0} ^ {1}'),
'v_not_b32': (1, '~{0}'), 'v_lshlrev_b32': (2, '{1} << ({0} & 31)'), 'v_lshrrev_b32': (2, '{1} >> ({0} & 31)'),
'v_ashrrev_i32': (2, 'uint(int({1}) >> ({0} & 31))'), 'v_min_u32': (2, 'min({0}, {1})'), 'v_max_u32': (2, 'max({0}, {1})'),
'v_min_i32': (2, 'uint(min(int({0}), int({1})))'), 'v_max_i32': (2, 'uint(max(int({0}), int({1})))'),
Expand Down Expand Up @@ -1789,6 +1789,11 @@ def common(self):
'uint2 shr64i(uint lo, uint hi, uint sh) { sh &= 63u; uint sb = sh & 31u, sc = (32u - sb) & 31u;'
' uint cy = sb == 0u ? 0u : (hi << sc); uint h = asuint(asint(hi) >> sb);'
' return uint2(sh < 32u ? ((lo >> sb) | cy) : h, sh < 32u ? h : asuint(asint(hi) >> 31u)); }',
# High 32 bits of an unsigned 32x32 product (issue #1792). These stages have no 64-bit multiply and
# the low half of a * b is not the high half, so it is built from 16-bit halfword products.
'uint mul_hi_u32(uint a, uint b) { uint la = a & 0xffffu, lb = b & 0xffffu;'
' uint middle = (a >> 16u) * lb + ((la * lb) >> 16u);'
' return (a >> 16u) * (b >> 16u) + (middle >> 16u) + (((middle & 0xffffu) + la * (b >> 16u)) >> 16u); }',
'uint ldu(ByteAddressBuffer b, uint a) { uint k = (a & 3) * 8, lo = b.Load(a & ~3u); if (k == 0) return lo; '
'return (lo >> k) | (b.Load((a & ~3u) + 4) << (32 - k)); }',
'cbuffer entry : register(b0) { %s };' % ' '.join('uint ud%d;' % i for i in range(len(self.uds)))]
Expand Down
Loading