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 @@ -1702,6 +1702,9 @@ if(EXISTS "${INTAKE_PYTHON}")
# lowering of the same selections.
add_test(NAME startup_gfx2hlsl_sdwa COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_sdwa_check.py")
# The high half of an unsigned 32x32 product (#1781).
add_test(NAME startup_gfx2hlsl_mul_hi COMMAND "${INTAKE_PYTHON}" -B

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Add the multiply-high check to PR CI

startup_gfx2hlsl_mul_hi is registered for the full CTest suite, but the PR test preset’s include filter in CMakePresets.json excludes it, and the python-linux run_checks_parallel.py list in .github/workflows/windows.yml omits this standalone script. As a result, no PR job runs this regression check; it is only selected by the full dev CTest path on non-PR runs. Add it to the platform-independent workflow and its documented command list so lowering regressions are caught before merge. The repository identifies that list as the platform-independent Python suite.

AGENTS.md reference: AGENTS.md:L98-L101

Useful? React with 👍 / 👎.

"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_mul_hi_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 @@ -5777,6 +5777,13 @@ uint-result table, so every `v_cvt_f32_*_sdwa` was refused until it was pointed
restored path keeps the source selection, the `dst_sel`/`dst_unused` write and the `U(...)` wrapper, and a
conversion's output modifier inside an SDWA form is still refused rather than dropped.

**The high half of an unsigned product.** `v_mul_hi_u32` had no lowering, so the form refused - one of the
remaining refused shaders in the first main-game loading-screen cache (#870, issue #1792). It now lowers to
a halfword-product helper carried in the shader preamble: the helper computes the high 32 bits by carrying
the low halfword product into the two cross products, because HLSL has no 64-bit multiply in these stages
and the low half of `a * b` is not the high half. The register and literal forms both lower; `v_mul_lo_u32`
is unchanged. Run `ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_mul_hi$' -V`.

## Pixel color without a texture sample

For an unsampled pixel program, a matched P1/P2 interpolation in the entry mask can supply attr0.x/y/z/w from a four-component TEXCOORD0. The profile binds entry v0/v1 to the barycentric pair and the observed m0 source to the interpolation base. Each completed P2 produces a distinct value, including when the destination overwrites v0. The test vertex shader varies all four channels across a full-screen triangle. CPU references check all four output words at 1,024 pixel centers; an incorrect channel at an interior pixel must fail. Run `ctest --preset dev --no-tests=error -R '^pixel_aot_attribute_'`.
Expand Down
219 changes: 219 additions & 0 deletions tests/startup_gfx2hlsl_mul_hi_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
"""`v_mul_hi_u32` in tools/startup/gfx2hlsl.py (issue #1792).

usage: startup_gfx2hlsl_mul_hi_check.py

The high half of an unsigned 32x32 product had no lowering, so `v_mul_hi_u32` was refused - one of the
remaining refused shaders in the main-game loading-screen cache (#870). `tools/pixel_aot.py` already had the
halfword-product helper; this port lowers the VOP3A form to it.

The instruction bytes are **constructed here** from the field layout the checked-in decoder reads
(`tools/disasm_rdna2.py`, VOP3A branch): prefix `0b110101`, opcode `0x16a` in `w0[25:16]`, vdst in `w0[7:0]`,
src0 in `w1[8:0]`, src1 in `w1[17:9]`, and source 255 followed by a literal dword.
"""
import os
import re
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
V0, V1, V2 = 0x100, 0x101, 0x102
LITERAL_SOURCE = 255


def encode_vop3a(op, vdst, srcs, literal=None):
"""Encode a native VOP3A instruction; a source of 255 is followed by its literal dword."""
w0 = (0b110101 << 26) | ((op & 0x3FF) << 16) | (vdst & 0xFF)
w1 = 0
for i, src in enumerate(srcs):
w1 |= (src & 0x1FF) << (9 * i)
code = struct.pack('<II', w0, w1)
if literal is not None:
code += struct.pack('<I', literal)
return code


class Pipeline:
"""Just enough pipeline state for one scalarized multiply."""

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)


# 1. The encoder produces the form the decoder reads; a layout change shows up here first.
asm, emitted = translate(encode_vop3a(VOP3A_MUL_HI_U32, 1, [V1, V2]))
check('encodes and decodes the register form', asm == ['v_mul_hi_u32 v1, v1, v2'], repr(asm))

# 2. The lowering calls the halfword-product helper rather than a plain multiply (the low half of the product
# is not the high half, so a `*` here would be silently wrong).
check('the register form uses the helper', emitted == 'v1 = mul_hi_u32(v1, v2);', emitted)

# 3. The census form carries a literal operand, which reaches the helper with the same suffix the other
# uint operations use.
asm, emitted = translate(encode_vop3a(VOP3A_MUL_HI_U32, 1, [V0, LITERAL_SOURCE],
literal=0xAAAAAAAB))
check('encodes and decodes the literal form', asm == ['v_mul_hi_u32 v1, v0, 0xaaaaaaab'], repr(asm))
check('the literal form uses the helper', emitted == 'v1 = mul_hi_u32(v0, 0xaaaaaaabu);', emitted)

# 4. The helper is in the preamble the generated shader is built with; without it the lowering would not
# compile even though its text looks right.
pipe = object.__new__(G.Pipeline)
pipe.uds, pipe.resources, pipe.samplers, pipe.uses_sampler_heap = [], {}, {}, False
common = ' '.join(pipe.common())
check('the helper is emitted in the common preamble', 'uint mul_hi_u32(uint a, uint b)' in common, common[:200])
check('the helper uses halfword products, not a 64-bit multiply',
'a & 0xffffu' in common and 'b >> 16' in common, common[:200])

# 5. A neighbouring operation is untouched: the low-half multiply still lowers to a plain product.
VOP3A_MUL_LO_U32 = 0x169
asm, emitted = translate(encode_vop3a(VOP3A_MUL_LO_U32, 1, [V1, V2]))
check('the low multiply is unchanged', emitted == 'v1 = v1 * v2;', emitted)

# ---- value-level check of the emitted helper --------------------------------------------------------------
# The reference is the exact product, so a wrong carry in the helper must fail even though the emitted text is
# unchanged. The evaluator covers the integer operators this helper uses, with C precedence.
TOKEN = re.compile(r'\s*(0x[0-9a-fA-F]+u?|\d+u?|[A-Za-z_]\w*|>>|<<|[&|^()*+-])')


def tokens(source):
out, position = [], 0
while position < len(source):
match = TOKEN.match(source, position)
if not match:
raise ValueError('cannot tokenise %r' % source[position:position + 20])
out.append(match.group(1))
position = match.end()
return out


class Expr:
"""A recursive-descent evaluator for the 32-bit integer HLSL the mul_hi helper emits."""

LEVELS = [('|',), ('^',), ('&',), ('>>', '<<'), ('+', '-'), ('*',)]

def __init__(self, source, variables):
self.items, self.at, self.vars = tokens(source), 0, variables

def peek(self):
return self.items[self.at] if self.at < len(self.items) else ''

def take(self):
item = self.peek()
self.at += 1
return item

def value(self):
return self.binary(0)

def binary(self, level):
if level == len(self.LEVELS):
return self.unary()
left = self.binary(level + 1)
while self.peek() in self.LEVELS[level]:
operator = self.take()
right = self.binary(level + 1)
if operator == '*':
left = (left * right) & 0xFFFFFFFF
elif operator == '+':
left = (left + right) & 0xFFFFFFFF
elif operator == '-':
left = (left - right) & 0xFFFFFFFF
elif operator == '&':
left &= right
elif operator == '^':
left ^= right
elif operator == '|':
left |= right
elif operator == '>>':
left >>= right
elif operator == '<<':
left = (left << right) & 0xFFFFFFFF
return left

def unary(self):
item = self.take()
if item == '(':
value = self.value()
if self.take() != ')':
raise ValueError('missing )')
return value
if re.fullmatch(r'0x[0-9a-fA-F]+u?', item):
return int(item.rstrip('u'), 16)
if re.fullmatch(r'\d+u?', item):
return int(item.rstrip('u'))
return self.vars[item]


def evaluate_mul_hi(helper, a, b):
"""Run the emitted helper body for one pair, so what is checked is the value it returns."""
body = helper[helper.index('{') + 1:helper.rindex('}')]
variables = {'a': a, 'b': b}
for statement in (piece.strip() for piece in body.split(';')):
if not statement:
continue
if statement.startswith('uint '):
for part in statement[4:].split(','):
name, expr = (piece.strip() for piece in part.split('=', 1))
variables[name] = Expr(expr, variables).value() & 0xFFFFFFFF
elif statement.startswith('return '):
return Expr(statement[7:], variables).value() & 0xFFFFFFFF
raise ValueError('the helper returned nothing for %#x %#x' % (a, b))


_pipe = object.__new__(G.Pipeline)
_pipe.uds, _pipe.resources, _pipe.samplers, _pipe.uses_sampler_heap = [], {}, {}, False
mul_hi_text = [entry for entry in _pipe.common() if 'uint mul_hi_u32(' in entry][0]
edges = [0, 1, 2, 0xFFFF, 0x10000, 0x10001, 0x7FFFFFFF, 0x80000000,
0x80000001, 0xFFFEFFFF, 0xFFFF0000, 0xFFFFFFFF, 0x12345678]
pairs = [(a, b) for a in edges for b in edges]
state = 0x2545F491
for _ in range(20000):
state = (state * 1103515245 + 12345) & 0xFFFFFFFF
first = state
state = (state * 1103515245 + 12345) & 0xFFFFFFFF
pairs.append((first, state))
mismatches = [(a, b, (a * b) >> 32, evaluate_mul_hi(mul_hi_text, a, b)) for a, b in pairs
if evaluate_mul_hi(mul_hi_text, a, b) != ((a * b) >> 32)]
check('the emitted helper agrees with the exact product on %d pairs' % len(pairs), not mismatches,
repr(mismatches[:4]))

if failures:
print('FAIL')
for text in failures:
print(' ' + text)
sys.exit(1)
print('PASS')
6 changes: 5 additions & 1 deletion tools/startup/gfx2hlsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,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 @@ -1270,6 +1270,10 @@ def common(self):
out = ['#define F(x) asfloat(x)', '#define U(x) asuint(x)',
'float mul_legacy(float a, float b) { return (a == 0.0 || b == 0.0) ? 0.0 : a * b; }',
'float med3(float a, float b, float c) { return max(min(a, b), min(max(a, b), c)); }',
# High half of an unsigned 32x32 product, by halfword products so it fits 32-bit HLSL.
'uint mul_hi_u32(uint a, uint b) { uint la = a & 0xffffu, lb = b & 0xffffu;'
' uint middle = (a >> 16) * lb + ((la * lb) >> 16);'
' return (a >> 16) * (b >> 16) + (middle >> 16) + (((middle & 0xffffu) + la * (b >> 16)) >> 16); }',
'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