From eda692a6a23a561915e1a179d94cb6e162664914 Mon Sep 17 00:00:00 2001 From: The Void Date: Wed, 25 Nov 2026 06:05:46 +0100 Subject: [PATCH] startup: lower gfx2hlsl f32 SDWA compares with DWORD selections compare() refused every v_cmp_*_f32_sdwa and v_cmpx_*_f32_sdwa until the f32 predicates were exact on NaN (#1930, #1949). DWORD selections now read the register as binary32 with neg/abs as in the e64 form, so a src0_sel:DWORD src1_sel:DWORD compare emits the e64 text, NaN bit test included. Byte and word selections are refused: the selected field is always +0 or a denormal, so the result depends on the binary32 denormal mode, which gfx2hlsl does not read. pixel_aot refuses the same forms. sext() is integer-only and refused; LLVM 15 does not decode the bit on a float source. clamp, omod, dst_sel and dst_unused stay refused, and class compares are unchanged. LLVM's SIPeepholeSDWA models the sub-dword case as zero-extension read as binary32 bits; the RDNA 2 ISA guide gives only the SDWAB fields. tests/startup_gfx2hlsl_f32_sdwa_check.py: - builds every f32 SDWA form and checks the 720 DWORD/DWORD forms against the e64 text and the ISA reference: 256 NaN/denormal pairs under the IEEE, FXC and DXC models; - refuses all 34,560 byte/word forms with the denormal-mode message; - uses the text before #1930/#1949 as its negative control; - draws 30 DWORD fixtures with default-flag FXC ps_5_1 and DXC ps_6_0. The vopc_sdwa and f32_lg checks no longer assert the blanket refusal. They now check that DWORD selections lower as the e64 form and that byte/word selections are refused with the new message. Refs #1906 Co-Authored-By: Claude Opus 5.5 --- .github/workflows/windows.yml | 3 + CMakeLists.txt | 12 + docs/experiments.md | 36 +- tests/startup_gfx2hlsl_f32_lg_check.py | 22 +- tests/startup_gfx2hlsl_f32_sdwa_check.py | 735 ++++++++++++++++++++++ tests/startup_gfx2hlsl_vopc_sdwa_check.py | 46 +- tools/startup/gfx2hlsl.py | 25 +- 7 files changed, 854 insertions(+), 25 deletions(-) create mode 100644 tests/startup_gfx2hlsl_f32_sdwa_check.py diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index 58c2f00a5e..c7a74b566c 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -100,6 +100,8 @@ jobs: (["tests/startup_gfx2hlsl_f32_lg_check.py", "decode", "libLLVM-15.so.1"], {}), (["tests/startup_gfx2hlsl_f32_nan_check.py", "binary", "llvm-mc-15"], {}), (["tests/startup_gfx2hlsl_f32_nan_check.py", "decode", "libLLVM-15.so.1"], {}), + (["tests/startup_gfx2hlsl_f32_sdwa_check.py", "binary", "llvm-mc-15"], {}), + (["tests/startup_gfx2hlsl_f32_sdwa_check.py", "decode", "libLLVM-15.so.1"], {}), (["tests/startup_inline_sampler_check.py", "binary", "llvm-mc-15"], {}), (["tests/startup_smem_address_check.py", "binary", "llvm-mc-15"], {}), (["tests/compute_entry_layout_check.py", "binary", "llvm-mc-15"], {}), @@ -233,6 +235,7 @@ jobs: tests/startup_gfx2hlsl_sopk_accum_check.py \ tests/startup_gfx2hlsl_f32_lg_check.py \ tests/startup_gfx2hlsl_f32_nan_check.py \ + tests/startup_gfx2hlsl_f32_sdwa_check.py \ tests/startup_gfx2hlsl_root_check.py \ tests/startup_gfx2hlsl_sopk_signext_check.py \ tests/startup_inline_sampler_check.py \ diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d301b2b72..ef674db8d6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2483,6 +2483,10 @@ if(EXISTS "${INTAKE_PYTHON}") # f32 nlt/nle/ngt/nge, o and u hold under FXC's and DXC's rewrites of float compares without -Gis (#1936). add_test(NAME startup_gfx2hlsl_f32_nan COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_f32_nan_check.py") + # f32 SDWA compares (#1906): DWORD selections emit the e64 text and equal the ISA reference over the same 256 pairs + # under the IEEE, FXC and DXC models; every byte/word selection is refused; the text before #1930/#1949 must fail. + add_test(NAME startup_gfx2hlsl_f32_sdwa COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_f32_sdwa_check.py") # A negated source on the vector select flips that source's sign bit (#1709). add_test(NAME startup_gfx2hlsl_cndmask_neg COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_cndmask_neg_check.py") @@ -2683,6 +2687,14 @@ if(EXISTS "${INTAKE_PYTHON}") "$" ${adapter} $<$:${STARTUP_FXC}>) set_tests_properties(startup_gfx2hlsl_f32_nan_${adapter} PROPERTIES TIMEOUT 300 SKIP_RETURN_CODE 77) endforeach() + # Every f32 condition in its DWORD SDWA form decides 16 x 16 pixels with default-flag FXC and DXC, checked pixel + # by pixel (#1906); the text before #1930/#1949 must fail on the compiler that broke it. + foreach(adapter IN ITEMS warp hardware) + add_test(NAME startup_gfx2hlsl_f32_sdwa_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_f32_sdwa_check.py" draw "${DXC_EXECUTABLE}" + "$" ${adapter} $<$:${STARTUP_FXC}>) + set_tests_properties(startup_gfx2hlsl_f32_sdwa_${adapter} PROPERTIES TIMEOUT 300 SKIP_RETURN_CODE 77) + endforeach() add_test(NAME startup_gfx2hlsl_mad_controls COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_mad_check.py") foreach(adapter IN ITEMS warp hardware) diff --git a/docs/experiments.md b/docs/experiments.md index f8a077fc56..5345e0a6d4 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -6502,8 +6502,9 @@ Run `ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_opsel$' -V`. **VOPC SDWA compares (#1906).** `compare()` refused every `v_cmp*_sdwa` and `v_cmpx*_sdwa`. It now lowers the SDWA forms of its 32-bit table. An i32 or u32 source reads its `src0_sel`/`src1_sel` field through `sdwa_source`, sign-extended under `sext()`, and the compare reads that 32-bit value as its type. Binary32 class compares read -a DWORD float from SRC0 and select the integer class mask from SRC1. Ordinary f32 predicates stay refused -in their SDWA form until their NaN behavior is verified (#1920). The +a DWORD float from SRC0 and select the integer class mask from SRC1. Ordinary f32 predicates were refused in +their SDWA form until their NaN behavior was verified (#1920); DWORD selections are now lowered (f32 SDWA entry +below). The destination is `vcc_lo`, the SGPR the decoder prints, or EXEC for `v_cmpx` with the same EXEC masking as before. A sub-dword class value, a missing or repeated select and unsupported SDWA modifiers are refused. `startup_gfx2hlsl_vopc_sdwa` evaluates the emitted class predicate for 42 forms across all ten binary32 classes, @@ -6541,7 +6542,8 @@ fixtures matched); FXC on both backends and DXC on WARP gave the ordered result. `startup_gfx2hlsl_f32_lg` decodes every f32 condition of the table (v_cmp and v_cmpx, e32 and e64 under every neg/abs combination, 510 forms), translates it and evaluates the HLSL over all 256 pairs of +-0, +-min denormal, +-1.0, +-max finite, +-inf, +-quiet, signalling and payload NaN against a reference built from the ISA -definitions on the bit patterns; the old `!=` text fails all 68 lg/nlg forms, and the f32 SDWA forms stay refused. +definitions on the bit patterns; the old `!=` text fails all 68 lg/nlg forms. The f32 SDWA lg/nlg forms with DWORD +selections must emit the e64 text (see the f32 SDWA entry below). The draws `startup_gfx2hlsl_f32_lg_{warp,hardware}` build one of those words from the pixel's x and one from its y, so 16 x 16 pixels cover every pair, and write `v_cmp_lg_f32_e32`, `v_cmp_nlg_f32_e64`, `v_cmp_lg_f32_e64` and `v_cmp_nlg_f32_e32` results or keep a pixel through `v_cmpx_lg/nlg_f32_e32/e64`, compiled with FXC `ps_5_1` and @@ -6574,6 +6576,34 @@ both matched every pixel. Run `ctest --preset dev --no-tests=error -R '^startup_ `tests/startup_gfx2hlsl_f32_nan_check.py binary ` and `decode ` round-trip the fixtures through LLVM 15. +**f32 SDWA compares (#1906).** `compare()` refused every `v_cmp_*_f32_sdwa` and `v_cmpx_*_f32_sdwa` until the f32 +predicates were exact on NaN (#1930, #1949). DWORD selections are now lowered: they read the register as binary32 +with neg/abs as in the e64 form, so a `src0_sel:DWORD src1_sel:DWORD` compare emits the e64 text of the same +modifiers, NaN bit test included. Byte and word selections are refused +(`: f32 SDWA byte/word selection depends on the binary32 denormal mode`), because the selected field is always ++0 or a denormal and its result depends on the denormal mode, which gfx2hlsl does not read. `tools/pixel_aot.py` +refuses the same forms. `sext()` is integer-only (LLVM's `AMDGPUModifierSyntax`) and refused; LLVM 15 does not +decode the bit on a float source. LLVM's `SIPeepholeSDWA` models the sub-dword case as zero-extension read as +binary32 bits; the RDNA 2 ISA guide gives only the SDWAB fields (§13.3.8) and says VOPC SDWA has no OMOD, CLMP, +DST_U or DST_SEL (§6.8). `startup_gfx2hlsl_f32_sdwa` builds and decodes every f32 SDWA form: 15 conditions; v_cmp +into `vcc_lo` and into an SGPR, v_cmpx into EXEC; 7 x 7 selects; 4 x 4 neg/abs. Each of the 720 DWORD/DWORD forms +must emit the e64 text, and its HLSL is evaluated over the 256 source pairs of the lg check under the IEEE, FXC and +DXC models of the nan check (184,320 evaluations) against the ISA reference. Each of the 34,560 forms with a byte or +word select on either source must be refused with that message. The text before #1930/#1949 on the same operands +fails all 128 guarded forms under the model of the compiler that broke it. The draws +`startup_gfx2hlsl_f32_sdwa_{warp,hardware}` write four DWORD/DWORD results of each condition (`(a, b)`, +`(-a, |b|)`, `(-|a|, -b)`, `(|a|, b)`, into `vcc_lo` and SGPRs) and keep pixels through +`v_cmpx__f32_sdwa -v3, |v4| src0_sel:DWORD src1_sel:DWORD`, 30 fixtures compiled with default-flag FXC +`ps_5_1` and DXC `ps_6_0`. Every pixel matched on WARP and the RX 9070 XT with both compilers (denormal sources +flushed where they matter). The text before #1930/#1949 fails each draw on the compiler that broke it (lg/nlg on +both) and matches that compiler's model. The exception is o and u under DXC: their old text keeps each source's +modifiers, and DXC folded `x == x` for `(a, b)` and `(-a, |b|)` in `cmp_o`/`cmp_u` but not for `(-|a|, -b)` or +`(|a|, b)`. In `cmpx_o`/`cmpx_u`, DXC folded the `|b|` side but not the `-a` side. Those matches are recorded, as +are the old text on the other compiler and `/Gis`/`-Gis` runs (every fixture matched). Run +`ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_f32_sdwa' -V`; +`tests/startup_gfx2hlsl_f32_sdwa_check.py binary ` and `decode ` round-trip all 35,280 forms +(lowered and refused) and the fixtures through LLVM 15. Class compares are unchanged. + **Four half channels in a typed buffer load.** `BUF_FMT_16_16_16_16_FLOAT` stores x and y in the first dword and z and w in the second, and the typed-load format table had only the two-channel half-float form, so a load of this format was refused with `buffer format BUF_FMT_16_16_16_16_FLOAT` - 5 of the 29 diff --git a/tests/startup_gfx2hlsl_f32_lg_check.py b/tests/startup_gfx2hlsl_f32_lg_check.py index c642f52e61..574f64a1bb 100644 --- a/tests/startup_gfx2hlsl_f32_lg_check.py +++ b/tests/startup_gfx2hlsl_f32_lg_check.py @@ -25,7 +25,8 @@ neg/abs combination on both sources) is decoded from words built here, translated, and the emitted HLSL is evaluated on the CPU with IEEE binary32 comparisons over every pair of 16 source words: +-0, +-min denormal, +-1.0, +-max finite, +-inf, +-quiet NaN, +-signalling NaN and +-NaN with a payload. The same forms with the old `!=` lowering -restored must disagree with the reference. The f32 SDWA compares stay refused. +restored must disagree with the reference. The f32 SDWA lg/nlg forms with DWORD selections must lower as the e64 +form; tests/startup_gfx2hlsl_f32_sdwa_check.py checks every f32 SDWA form. The draws: a source-built pixel program builds one of those 16 words from the pixel's x and one from its y with integer operations, so 16 x 16 pixels cover every pair: finite unequal, equal (+0 and -0 among them), infinite and @@ -452,8 +453,9 @@ def controls(): wrong.append((asm, hex(a), hex(b), lane)) check('inactive lane: lg/nlg results masked by EXEC in the e32 and e64 forms', not wrong, str(wrong[:3])) - # 5. The f32 SDWA compares stay refused (#1906): lg and nlg, v_cmp and v_cmpx, with and without modifiers. - refused = [] + # 5. The f32 SDWA lg/nlg compares (#1906) with DWORD selections: the same text as the e64 form with the same + # modifiers. Every f32 SDWA form is checked in tests/startup_gfx2hlsl_f32_sdwa_check.py. + differ = [] for op in F32_OPS: cmpx, cond, _ = COMPARES[op] if cond not in ('lg', 'nlg'): @@ -462,12 +464,16 @@ def controls(): name, asm = decode_one(vopc_sdwa(op, source('v', 1, neg=neg, absolute=absolute), source('v', 2), None if cmpx else SGPR_DST)) try: - Probe().one(name, asm) - refused.append((asm, None)) + stage = Probe() + stage.one(name, asm) except G.Unsupported as error: - refused.append((asm, str(error))) - check('f32 SDWA lg/nlg compares stay refused (%d forms)' % len(refused), len(refused) == 16 and all( - error == '%s: f32 SDWA compares are not lowered' % asm.split()[0] for asm, error in refused), str(refused[:3])) + differ.append((asm, str(error))) + continue + _asm, e64 = translate(compare_code(op, 'e64', ((neg, absolute), (False, False)))) + if stage.lines != e64.lines: + differ.append((asm, stage.lines, e64.lines)) + check('f32 SDWA lg/nlg compares with DWORD selections lower as the e64 form (16 forms)', not differ, + str(differ[:3])) # ---- the pixel fixtures ----------------------------------------------------------------------------------------- diff --git a/tests/startup_gfx2hlsl_f32_sdwa_check.py b/tests/startup_gfx2hlsl_f32_sdwa_check.py new file mode 100644 index 0000000000..bcef223b13 --- /dev/null +++ b/tests/startup_gfx2hlsl_f32_sdwa_check.py @@ -0,0 +1,735 @@ +"""f32 VOPC SDWA compares in tools/startup/gfx2hlsl.py: v_cmp_*_f32_sdwa and v_cmpx_*_f32_sdwa (#1906). + + (no arguments) translation controls, pure Python + binary assemble the decoded text of every f32 SDWA form built here and of the pixel + fixtures with llvm-mc and compare with the words built here + decode disassemble the same words with LLVM's C API and compare with + tools/disasm_rdna2.py; LLVM must refuse the sext bit on a float source + draw [fxc] + compile the pixel fixtures with FXC ps_5_1 when given and with DXC ps_6_0, + all without flags, draw 16 x 16 pixels each and compare every pixel with the + ISA reference (needs tests/offscreen_check.cpp built as offscreen_check) + +What is lowered. DWORD selections read the register as binary32 with neg/abs as in the e64 form, so a +src0_sel:DWORD src1_sel:DWORD compare emits the e64 text of the same modifiers, NaN bit test included. Byte and word +selections are refused because the selected field is always +0 or a denormal and its result depends on the +binary32 denormal mode, which gfx2hlsl does not read (tools/pixel_aot.py refuses the same forms). sext() is +integer-only (llvm/docs/AMDGPUModifierSyntax.rst) and refused; LLVM 15 does not decode the bit on a float source. +LLVM's SIPeepholeSDWA models the sub-dword case as zero-extension read as binary32 bits; the RDNA 2 ISA guide gives +only the SDWAB fields (section 13.3.8) and says VOPC SDWA has no OMOD, CLMP, DST_U or DST_SEL (section 6.8). + +Controls: every f32 SDWA form is built from the VOP_SDWA9Be fields and decoded: the 15 conditions of the compare +table; v_cmp into vcc_lo and into an SGPR, v_cmpx into EXEC; every select on each source; every neg/abs combination +on each source. Each DWORD/DWORD form must emit the e64 text and its HLSL is evaluated over the 256 pairs of the 16 +source words of tests/startup_gfx2hlsl_f32_lg_check.py under the three compiler models of +tests/startup_gfx2hlsl_f32_nan_check.py (IEEE; FXC pushing a negation into the opposite relation; DXC folding +reflexive relations and reading less-or-greater as not-equal) against the ISA reference on bit patterns. Every form +with a BYTE or WORD selection on either source must be refused with the denormal-mode message. Negative control: the +text before #1930/#1949 on the same operands must fail under the model of the compiler that broke it. + +The draws build one source word from the pixel's x and one from its y, as the lg check does. Fixture cmp_ +writes four v_cmp__f32_sdwa DWORD/DWORD results as 1.0 or 0.0 under (a, b), (-a, |b|), (-|a|, -b) and +(|a|, b), into vcc_lo and SGPRs; fixture cmpx_ keeps a pixel when v_cmpx__f32_sdwa -v3, |v4| holds. +Each must match the image with denormals kept or the one with them flushed, the same choice for every fixture whose +image depends on it, and the report names it. The text before #1930/#1949 must fail against both images on the +compiler that broke it (lg/nlg on both) and match that compiler's model. The exception is o/u under DXC, which folds +the reflexive compares only for some of the modified operand shapes, so that match is recorded. The old text on the +other compiler and strict compares (/Gis, -Gis) are recorded too. +""" +import functools +import re +import struct +import subprocess +import sys +import tempfile +from pathlib import Path + +HERE = Path(__file__).resolve().parent +sys.path.insert(0, str(HERE)) +import startup_gfx2hlsl_f32_lg_check as L # noqa: E402 +import startup_gfx2hlsl_f32_nan_check as N # noqa: E402 +import startup_gfx2hlsl_vopc_sdwa_check as S # noqa: E402 +from startup_gfx2hlsl_f32_lg_check import ( # noqa: E402 + CLEAR, HEIGHT, ONE, PAIRS, VALUES, WIDTH, G, as_float, assemble, compile_hlsl, flushed, modified, + old_reference, reference) + +SELS = S.SELS +SUB = SELS[:-1] +MODIFIERS = L.MODIFIERS +PLAIN, NEG, ABS, NEG_ABS = (False, False), (True, False), (False, True), (True, True) +SGPR_DST = 6 +F32_OPS = list(L.F32_OPS) +CONDS = sorted({S.COMPARES[op][1] for op in F32_OPS}) +OPCODE = {cond: op for op, (cmpx, cond, typ) in S.COMPARES.items() if typ == 'f32' and not cmpx} +OPCODE_X = {cond: op for op, (cmpx, cond, typ) in S.COMPARES.items() if typ == 'f32' and cmpx} +# Conditions whose text tests the source words for NaN ({2} in the table), and the compiler model under which the +# text before #1930 (lg, nlg) and #1949 (the others) gives a wrong result. +GUARDED = ('lg', 'nlg', 'nlt', 'nle', 'ngt', 'nge', 'o', 'u') +BROKEN_BY = dict(N.BROKEN_BY, lg='ieee', nlg='ieee') +OLD = dict(N.OLD, lg='({0} != {1})', nlg='!({0} != {1})') +MODELS = N.MODELS +DENORMAL_MODE = '%s: f32 SDWA byte/word selection depends on the binary32 denormal mode' + +failures = [] + + +def check(label, ok, detail=''): + print('%s: %s' % (label, 'ok' if ok else 'MISMATCH'), flush=True) + if not ok: + failures.append(label) + if detail: + print(' ' + detail) + + +# ---- the ISA reference, on bit patterns ----------------------------------------------------------------------- +@functools.lru_cache(maxsize=None) +def ref(cond, x, y): + return reference(cond, x, y) + + +def blind(cond, x, y): + """What the text before #1930/#1949 gives under the compiler that broke it.""" + if cond in ('lg', 'nlg'): + return old_reference(cond, x, y) + return N.blind(cond, x, y) + + +def want(cond, mods0, mods1, how='reference'): + model = blind if how == 'old' else ref + return [model(cond, modified(a, *mods0), modified(b, *mods1)) for a, b in PAIRS] + + +# ---- a vectorised evaluator for the emitted HLSL, under the compiler models of the nan check ------------------- +TOKEN = re.compile(r'\s*(0x[0-9a-fA-F]+u?|\d+\.\d+|\d+u?|[A-Za-z_]\w*|<=|>=|==|!=|\|\||&&|[-&!<>?:(),])') +LEVELS = [['||'], ['&&'], ['&'], ['==', '!='], ['<', '>', '<=', '>=']] +OPPOSITE = N.OPPOSITE + + +def parse(text): + """The expression tree of L.Expr, as hashable tuples.""" + tokens = TOKEN.findall(text) + assert ''.join(tokens) == re.sub(r'\s+', '', text), text + pos = [0] + + def peek(): + return tokens[pos[0]] if pos[0] < len(tokens) else None + + def take(): + pos[0] += 1 + return tokens[pos[0] - 1] + + def ternary(): + cond = binary(0) + if peek() != '?': + return cond + take() + yes = ternary() + assert take() == ':' + return ('?', cond, yes, ternary()) + + def binary(level): + if level == len(LEVELS): + return unary() + node = binary(level + 1) + while peek() in LEVELS[level]: + node = (take(), node, binary(level + 1)) + return node + + def unary(): + token = take() + if token in ('-', '!'): + return ('u' + token, unary()) + if token == '(': + node = ternary() + assert take() == ')' + return node + if '.' in token: + return ('const', 'f', float(token)) + if token[0].isdigit(): + return ('const', 'u', int(token.rstrip('u'), 0) & 0xffffffff) + if token in ('true', 'false'): + return ('const', 'b', int(token == 'true')) + if peek() != '(': + return ('var', token) + take() + args = [ternary()] + while peek() == ',': + take() + args.append(ternary()) + assert take() == ')' + return ('call', token, tuple(args)) + + tree = ternary() + assert pos[0] == len(tokens), text + return tree + + +class Vector: + """Evaluates trees over every pair at once: values are (kind, list), kind 'u', 'f' or 'b'. The model rules are + those of startup_gfx2hlsl_f32_nan_check.Model; Python floats compare as IEEE binary32 does on exact values.""" + + def __init__(self, env, model): + self.env, self.model, self.memo = env, model, {} + self.size = len(next(iter(env.values()))) + + def run(self, text): + kind, values = self.eval(parse(text)) + assert kind in ('u', 'b'), text + return values + + def floats(self, node): + return node[0] in OPPOSITE and self.eval(node[1])[0] == 'f' + + def negate(self, node): + if node[0] == 'u!': + return node[1] + if node[0] in ('&&', '||'): + return ('||' if node[0] == '&&' else '&&', self.negate(node[1]), self.negate(node[2])) + if node[0] in OPPOSITE: + return (OPPOSITE[node[0]], node[1], node[2]) + return ('u!', node) + + def eval(self, node): + if node not in self.memo: + self.memo[node] = self.compute(node) + return self.memo[node] + + def compute(self, node): + if self.model == 'fxc' and node[0] == 'u!': + inner = node[1] + if inner[0] in ('&&', '||') or self.floats(inner): + return self.eval(self.negate(inner)) + if self.model == 'dxc': + if self.floats(node) and node[1] == node[2]: + return 'b', [int(node[0] in ('==', '<=', '>='))] * self.size + if (node[0] == '||' and node[1][0] == '<' and node[2][0] == '>' and node[1][1:] == node[2][1:] + and self.floats(node[1])): + return self.eval(('!=',) + node[1][1:]) + kind = node[0] + if kind == 'const': + return node[1], [node[2]] * self.size + if kind == 'var': + return 'u', self.env[node[1]] + if kind == '?': + cond = self.truth(self.eval(node[1])) + (k1, yes), (k2, no) = self.eval(node[2]), self.eval(node[3]) + assert k1 == k2 + return k1, [y if c else n for c, y, n in zip(cond, yes, no)] + if kind == 'call': + (k, v), = [self.eval(a) for a in node[2]] + if node[1] in ('F', 'asfloat'): + assert k == 'u' + return 'f', [as_float(w) for w in v] + assert node[1] == 'abs' and k == 'f', node + return 'f', [abs(f) for f in v] + if kind == 'u-': + k, v = self.eval(node[1]) + assert k == 'f' + return 'f', [-f for f in v] + if kind == 'u!': + return 'b', [int(not t) for t in self.truth(self.eval(node[1]))] + if kind in ('||', '&&'): + a, b = self.truth(self.eval(node[1])), self.truth(self.eval(node[2])) + return 'b', [int(p or q) if kind == '||' else int(p and q) for p, q in zip(a, b)] + (ka, a), (kb, b) = self.eval(node[1]), self.eval(node[2]) + if kind == '&': + assert ka == kb == 'u', node + return 'u', [p & q for p, q in zip(a, b)] + assert ka == kb and ka in ('f', 'u'), node + compare = {'==': lambda p, q: p == q, '!=': lambda p, q: p != q, '<': lambda p, q: p < q, + '>': lambda p, q: p > q, '<=': lambda p, q: p <= q, '>=': lambda p, q: p >= q}[kind] + return 'b', [int(compare(p, q)) for p, q in zip(a, b)] + + @staticmethod + def truth(value): + kind, values = value + assert kind in ('b', 'u'), value + return [v != 0 for v in values] + + +A = [a for a, _b in PAIRS] +B = [b for _a, b in PAIRS] + + +# ---- forms ----------------------------------------------------------------------------------------------------- +def sdwa_code(op, sel0, sel1, mods0, mods1, sdst=None, src=(1, 2)): + return S.vopc_sdwa(op, S.source('v', src[0], neg=mods0[0], absolute=mods0[1]), + S.source('v', src[1], neg=mods1[0], absolute=mods1[1]), sdst, sel0, sel1) + + +def all_forms(): + """(op, destination, sel0, sel1, mods0, mods1) for every f32 SDWA form checked here.""" + for op in F32_OPS: + cmpx = S.COMPARES[op][0] + for destination in (('exec',) if cmpx else ('vcc', 'sgpr')): + for sel0 in SELS: + for sel1 in SELS: + for mods0 in MODIFIERS: + for mods1 in MODIFIERS: + yield op, destination, sel0, sel1, mods0, mods1 + + +def form_code(op, destination, sel0, sel1, mods0, mods1): + return sdwa_code(op, sel0, sel1, mods0, mods1, SGPR_DST if destination == 'sgpr' else None) + + +def translate(code, exec_known=True): + """(asm, stage) or (asm, the refusal).""" + name, asm = S.decode_one(code) + stage = S.Probe(exec_known) + try: + stage.one(name, asm) + except G.Unsupported as error: + return asm, str(error) + return asm, stage + + +class Refused(Exception): + """A form the translator refused where the check needs its text.""" + + +def lines_of(stage): + return ['' % stage] if isinstance(stage, str) else stage.lines + + +def body(stage, target): + if isinstance(stage, str): + raise Refused(stage) + line = stage.lines[0] + assert line.startswith(target + ' = ') and line.endswith(';'), line + return line[len(target) + 3:-1] + + +def operand_texts(mods0, mods1, src=('v1', 'v2')): + """X and Y, the float operand texts of this modifier set, from the DWORD/DWORD lt compare.""" + _asm, stage = translate(sdwa_code(OPCODE['lt'], 'DWORD', 'DWORD', mods0, mods1, + src=(int(src[0][1:]), int(src[1][1:])))) + text = body(stage, 'vcc') + assert text.startswith('((') and text.endswith(') ? 1u : 0u)'), text + x, y = text[2:-len(') ? 1u : 0u)')].split(' < ') + return x, y + + +def old_text(cond, mods0, mods1, src=('v1', 'v2')): + return '((%s) ? 1u : 0u)' % OLD[cond].format(*operand_texts(mods0, mods1, src)) + + +def controls(): + # 1. Why byte and word selections are refused: the field of any word is +0 or a positive denormal. + fields = {S.field(v, sel, False) for v in VALUES for sel in SUB} + check('a BYTE or WORD field fits in 16 bits: as binary32 it is +0 or a positive denormal (%d distinct of the 16 ' + 'words)' % len(fields), all(w <= 0xffff for w in fields) and 0 in fields and any(fields) + and all(as_float(w) < 2.0 ** -126 for w in fields)) + + # 2. The emitted text: DWORD/DWORD is the e64 form with the same modifiers, the NaN test on the registers. + _asm, stage = translate(sdwa_code(OPCODE['lg'], 'DWORD', 'DWORD', NEG, ABS, SGPR_DST)) + expected = 's6 = ((((v1 & 0x7fffffffu) <= 0x7f800000u && (v2 & 0x7fffffffu) <= 0x7f800000u && ' \ + '((-F(v1)) < abs(F(v2)) || (-F(v1)) > abs(F(v2))))) ? 1u : 0u);' + check('v_cmp_lg_f32_sdwa s6, -v1, |v2| DWORD/DWORD: %s' % lines_of(stage), lines_of(stage) == [expected]) + differ = [] + for op in F32_OPS: + cmpx = S.COMPARES[op][0] + for mods in ((m, n) for m in MODIFIERS for n in MODIFIERS): + _asm, sdwa = translate(sdwa_code(op, 'DWORD', 'DWORD', *mods, None if cmpx else SGPR_DST)) + _asm, e64 = translate(L.compare_code(op, 'e64', mods)) + if lines_of(sdwa) != lines_of(e64) or isinstance(sdwa, str) or sdwa.masks != e64.masks: + differ.append((op, mods, lines_of(sdwa), lines_of(e64))) + check('DWORD/DWORD SDWA forms emit the e64 text with the same modifiers (%d forms)' % (len(F32_OPS) * 16), + not differ, str(differ[:2])) + + # 3. Every form: DWORD/DWORD is lowered into its destination and equals the reference under every compiler model; + # every form with a BYTE or WORD selection on either source is refused for the denormal mode. + texts, wrong, count, refused, accepted = {}, [], 0, 0, [] + for form in all_forms(): + op, destination, sel0, sel1, mods0, mods1 = form + cmpx, cond, _ = S.COMPARES[op] + asm, stage = translate(form_code(*form)) + if (sel0, sel1) != ('DWORD', 'DWORD'): + if stage == DENORMAL_MODE % asm.split()[0]: + refused += 1 + else: + accepted.append((asm, lines_of(stage))) + continue + target = {'exec': 'exec', 'vcc': 'vcc', 'sgpr': 's%d' % SGPR_DST}[destination] + if isinstance(stage, str): + wrong.append((asm, stage)) + continue + if (len(stage.lines) != 1 or not stage.lines[0].startswith(target + ' = ') + or (destination == 'sgpr') != (SGPR_DST in stage.masks) or stage.exec_known == cmpx): + wrong.append((asm, stage.lines, 'destination')) + continue + count += 1 + texts.setdefault((cond, mods0, mods1), set()).add(body(stage, target)) + check('%d BYTE/WORD forms (15 conditions; v_cmp into vcc_lo and an SGPR, v_cmpx; a sub-dword select on src0, ' + 'src1 or both; every neg/abs) refused: "%s"' % (refused, DENORMAL_MODE % ''), + not accepted and refused == 15 * 3 * 48 * 16, str(accepted[:3])) + check('%d DWORD/DWORD forms translate, each into its destination' % count, + not wrong and count == 15 * 3 * 16, str(wrong[:3])) + check('v_cmp into vcc_lo or an SGPR and v_cmpx into EXEC emit one text per form (%d texts)' % len(texts), + all(len(t) == 1 for t in texts.values()) and len(texts) == 15 * 16) + texts = {key: next(iter(t)) for key, t in texts.items()} + evaluators = {model: Vector({'v1': A, 'v2': B}, model) for model in MODELS} + wrong, evaluations = [], 0 + for key, text in texts.items(): + expected = want(*key) + for model in MODELS: + evaluations += len(PAIRS) + got = evaluators[model].run(text) + if got != expected: + i = next(i for i, (g, w) in enumerate(zip(got, expected)) if g != w) + wrong.append((key, model, '%08x %08x gives %d, the ISA %d' % (A[i], B[i], got[i], expected[i]))) + check('%d forms (%d texts) x %d pairs x 3 models (%d evaluations) equal the ISA reference' % ( + count, len(texts), len(PAIRS), evaluations), not wrong and len(texts) == 15 * 16, str(wrong[:3])) + + # Negative control: the text before #1930/#1949 on the same operands fails under the compiler that broke it, + # holds under IEEE compares (lg/nlg already fail there), and matches that compiler's model. + caught, missed = 0, [] + for (cond, mods0, mods1) in texts: + if cond not in GUARDED: + continue + old = old_text(cond, mods0, mods1) + expected = want(cond, mods0, mods1) + broken = evaluators[BROKEN_BY[cond]].run(old) + if (broken != expected and broken == want(cond, mods0, mods1, 'old') + and (BROKEN_BY[cond] == 'ieee' or evaluators['ieee'].run(old) == expected)): + caught += 1 + else: + missed.append((cond, mods0, mods1)) + check('negative control: the text before #1930/#1949 fails %d of %d guarded forms under its compiler ' + '(lg/nlg already under IEEE) and matches that compiler\'s model' % (caught, caught + len(missed)), + not missed and caught == 8 * 16, str(missed[:3])) + + # 4. EXEC already reduced: every condition, masked by the lane's EXEC bit, under every model. + wrong = [] + for cond in CONDS: + for op in (OPCODE[cond], OPCODE_X[cond]): + for mods0, mods1 in ((NEG, ABS), (NEG_ABS, PLAIN)): + _asm, stage = translate(sdwa_code(op, 'DWORD', 'DWORD', mods0, mods1), exec_known=False) + if isinstance(stage, str): + wrong.append((cond, op, stage)) + continue + text = stage.lines[0].split(' = ', 1)[1][:-1] + expected = want(cond, mods0, mods1) + for model in MODELS: + for lane in (0, 1): + got = Vector({'v1': A, 'v2': B, 'exec': [lane] * len(A)}, model).run(text) + if got != [lane & w for w in expected]: + wrong.append((cond, op, mods0, model, lane)) + check('inactive lane: every condition masked by EXEC under every model', not wrong, str(wrong[:3])) + + # 5. Scalar and inline-constant sources: DWORD lowered, a sub-dword select refused. + s3 = [0x7fc12345, 0xbf800000, 0x00010000, 0xffff0001] + words = [(s, b) for s in s3 for b in VALUES] + env = {'s3': [s for s, _ in words], 'v2': [b for _, b in words]} + asm, stage = translate(S.vopc_sdwa(OPCODE['nlt'], S.source('s', 3, neg=True), S.source('v', 2, absolute=True), + None, 'DWORD', 'DWORD')) + got = Vector(env, 'fxc').run(body(stage, 'vcc')) + check('SGPR source: %s' % asm, asm == 'v_cmp_nlt_f32_sdwa vcc_lo, -s3, |v2| src0_sel:DWORD src1_sel:DWORD' + and got == [reference('nlt', s ^ 0x80000000, b & 0x7fffffff) for s, b in words], str(lines_of(stage))) + asm, stage = translate(S.vopc_sdwa(OPCODE_X['ge'], S.source('s', 242, neg=True), S.source('v', 2), None, + 'DWORD', 'DWORD')) + got = Vector({'v2': VALUES}, 'ieee').run(body(stage, 'exec')) + check('inline constant neg(1.0): %s' % asm, + asm == 'v_cmpx_ge_f32_sdwa neg(1.0), v2 src0_sel:DWORD src1_sel:DWORD' + and got == [reference('ge', 0xbf800000, b) for b in VALUES], str(lines_of(stage))) + for code in (S.vopc_sdwa(OPCODE['nlt'], S.source('s', 3, neg=True), S.source('v', 2), None, 'WORD_1', 'DWORD'), + S.vopc_sdwa(OPCODE_X['ge'], S.source('s', 242, neg=True), S.source('v', 2), None, 'DWORD', 'BYTE_1')): + asm, stage = translate(code) + check('refused: %s (%s)' % (asm, stage), stage == DENORMAL_MODE % asm.split()[0]) + + # 6. Other refusals, each with its own message. + def expect(label, name, asm, message): + try: + S.Probe().one(name, asm) + got = None + except G.Unsupported as error: + got = str(error) + check('refused: %s (%s)' % (label, got), got == message) + + base = 'v_cmp_lt_f32_sdwa vcc_lo, v1, v2' + expect('sext() on a float source', 'v_cmp_lt_f32_sdwa', + 'v_cmp_lt_f32_sdwa vcc_lo, sext(v1), v2 src0_sel:DWORD src1_sel:DWORD', + 'v_cmp_lt_f32_sdwa: sext() on a float source') + expect('sext() on a float src1 with a word select', 'v_cmpx_nlg_f32_sdwa', + 'v_cmpx_nlg_f32_sdwa v1, -sext(v2) src0_sel:DWORD src1_sel:WORD_1', + 'v_cmpx_nlg_f32_sdwa: sext() on a float source') + for extra in ('clamp', 'mul:2', 'div:2', 'dst_sel:DWORD', 'dst_unused:UNUSED_PAD'): + expect('modifier %s' % extra, 'v_cmp_lt_f32_sdwa', '%s src0_sel:DWORD src1_sel:DWORD %s' % (base, extra), + 'v_cmp_lt_f32_sdwa with SDWA modifier %s' % extra) + expect('missing src1_sel', 'v_cmp_lt_f32_sdwa', base + ' src0_sel:DWORD', + 'v_cmp_lt_f32_sdwa SDWA without src1_sel') + name, asm = S.decode_one(S.vopc_sdwa(OPCODE['lt'], S.source('v', 1, sext=True), S.source('v', 2), None, + 'BYTE_0', 'DWORD')) + check('the decoder refuses the sext bit on a float source: %s' % asm, name == 'vopc_sdwa' + and asm == 'v_cmp_lt_f32_e32 ') + name, asm = S.decode_one(S.vopc_sdwa(0x0f, S.source('v', 1), S.source('v', 2))) + expect('tru (no condition in the table, as in the non-SDWA form)', name, asm, 'v_cmp_tru_f32_sdwa') + name, asm = S.decode_one(S.vopc_sdwa(0x88, S.source('v', 1), S.source('v', 2), None, 'WORD_0', 'BYTE_1')) + expect('class float src0 sub-dword (class compares are unchanged)', name, asm, + 'v_cmp_class_f32_sdwa requires a DWORD float source') + + +# ---- the pixel fixtures --------------------------------------------------------------------------------------- +# Per cmp_ result: (destination SGPR or None for vcc_lo, mods0, mods1), as the e64 results of the nan check. +CMP_RESULTS = [(None, PLAIN, PLAIN), (6, NEG, ABS), (8, NEG_ABS, NEG), (None, ABS, PLAIN)] +CMPX_RESULT = (NEG, ABS) + + +def fixtures(): + """name -> (body words, [(cond, mods0, mods1)] per exported word, or one for v_cmpx).""" + out = {} + for cond in CONDS: + words, results = b'', [] + for index, (sdst, mods0, mods1) in enumerate(CMP_RESULTS): + words += sdwa_code(OPCODE[cond], 'DWORD', 'DWORD', mods0, mods1, sdst, src=(3, 4)) + words += L.cndmask_e64(6 + index, L.VCC_LO if sdst is None else sdst) + results.append((cond, mods0, mods1)) + out['cmp_' + cond] = (words + struct.pack(' 0) + + +def all_codes(): + codes = [form_code(*form) for form in all_forms()] + codes.append(S.vopc_sdwa(OPCODE['nlt'], S.source('s', 3, neg=True), S.source('v', 2, absolute=True), None, + 'DWORD', 'DWORD')) + codes.append(S.vopc_sdwa(OPCODE_X['ge'], S.source('s', 242, neg=True), S.source('v', 2), None, 'DWORD', 'DWORD')) + return codes + + +def check_binary(llvm_mc): + codes = all_codes() + source_text = ''.join(S.decode_one(code)[1] + '\n' for code in codes) + check('llvm-mc assembles %d decoded f32 SDWA forms (lowered and refused) to the same words' % len(codes), + assemble(llvm_mc, source_text) == b''.join(codes)) + for name in FIXTURES: + words = program(name) + text = fixture_text(words) + check('llvm-mc assembles the decoded %s fixture (%d instructions) to the same words' % (name, len(text)), + assemble(llvm_mc, '\n'.join(text) + '\n') == words) + + +def check_decode(library): + from pixel_aot import disassemble + codes = all_codes() + texts = disassemble(b''.join(codes), library) + ours = [S.decode_one(code)[1] for code in codes] + wrong = [(a, b) for a, b in zip(texts, ours) if a != b] + check('LLVM disassembles %d f32 SDWA forms as tools/disasm_rdna2.py does' % len(codes), + len(texts) == len(codes) and not wrong, str(wrong[:3])) + # The sext bit on a float source: tools/disasm_rdna2.py refuses it, and so does LLVM 15. + for index in (0, 1): + sources = [S.source('v', 1), S.source('v', 2)] + sources[index] = S.source('v', index + 1, sext=True) + code = S.vopc_sdwa(OPCODE['lt'], *sources, None, 'BYTE_0', 'BYTE_0') + try: + decoded = disassemble(code, library) + except Exception as error: # noqa: BLE001 - pixel_aot reports an undecodable word this way + decoded = 'refused: %s' % error + check('LLVM refuses sext on float src%d (%s): %s' % (index, code.hex(), decoded), + str(decoded).startswith('refused') and S.decode_one(code)[0] == 'vopc_sdwa') + for name in FIXTURES: + words = program(name) + check('LLVM disassembles the %s fixture as tools/disasm_rdna2.py does' % name, + disassemble(words, library) == fixture_text(words)) + + +def draw(renderer, adapter, vs, ps, expected): + run = subprocess.run([renderer, adapter, '--startup-pixel-words', str(vs), str(ps), str(WIDTH), str(HEIGHT), + str(expected)], capture_output=True, text=True) + if run.returncode == 77: + print(run.stdout + run.stderr) + sys.exit(77) + return run + + +def pixel_lines(run): + return ' / '.join(ln for ln in (run.stdout + run.stderr).splitlines() if 'pixel' in ln) + + +def check_draw(dxc, renderer, adapter, fxc=None): + compilers = ([('fxc', fxc, 'vs_5_1', 'ps_5_1')] if fxc else []) + [('dxc', dxc, 'vs_6_0', 'ps_6_0')] + if not fxc: + print(' no FXC given: the nlt/nle/ngt/nge old-text controls are not drawn') + with tempfile.TemporaryDirectory() as tmp: + directory = Path(tmp) + for kind, compiler, vs_profile, ps_profile in compilers: + vs = compile_hlsl(compiler, kind, L.FULLSCREEN_VS, 'vs_%s' % kind, vs_profile, directory) + modes, header = set(), None + for name, (_words, results) in FIXTURES.items(): + cond = results[0][0] + files = {d: L.words_file(directory, '%s_%s' % (name, d), image(name, denormals=d)) + for d in L.DENORMALS} + ps = compile_hlsl(compiler, kind, pixel_hlsl(name), 'ps_%s_%s' % (name, kind), ps_profile, directory) + matched, report = None, [] + for denormals in L.DENORMALS: + run = draw(renderer, adapter, vs, ps, files[denormals]) + report.append(run.stdout + run.stderr) + if run.returncode == 0 and 'all %d pixels matched' % (WIDTH * HEIGHT) in run.stdout: + matched = denormals + break + header = header or ' '.join(ln for ln in run.stdout.splitlines() + if ln.startswith(('backend=', 'adapter=', 'driver=', 'debug_errors='))) + sensitive = image(name) != image(name, denormals='flushed') + if sensitive or matched is None: + modes.add(matched) + check('%s %s %s: all %d pixels match the ISA reference (%s denormals)' % ( + kind, adapter, name, WIDTH * HEIGHT, matched if sensitive else 'either'), matched is not None, + '\n'.join(report)) + if cond in GUARDED: + bad = compile_hlsl(compiler, kind, pixel_hlsl(name, 'old'), 'ps_%s_old_%s' % (name, kind), + ps_profile, directory) + if BROKEN_BY[cond] in ('ieee', kind): + for denormals in L.DENORMALS: + run = draw(renderer, adapter, vs, bad, files[denormals]) + check('%s %s %s old text fails against the %s image: %s' % ( + kind, adapter, name, denormals, pixel_lines(run)), + run.returncode != 0 and 'pixel mismatch' in run.stderr, run.stdout + run.stderr) + own = L.words_file(directory, name + '_old', image(name, 'old', matched or 'ieee')) + run = draw(renderer, adapter, vs, bad, own) + if cond in ('o', 'u'): + # The old o/u text keeps each source's modifiers, and DXC folds x == x only for some of + # those operand shapes (the nan check draws it on the plain words), so this is recorded. + print(' recorded: %s %s %s old text against the %s model: %s' % ( + kind, adapter, name, BROKEN_BY[cond], pixel_lines(run) or 'all pixels matched')) + else: + check('%s %s %s old text matches the image of the %s model' % ( + kind, adapter, name, BROKEN_BY[cond]), run.returncode == 0, run.stdout + run.stderr) + else: + run = draw(renderer, adapter, vs, bad, files[matched or 'ieee']) + print(' recorded: %s %s %s old text: %s' % ( + kind, adapter, name, pixel_lines(run) or 'all pixels matched')) + # Diagnostic, not checked: the text with IEEE-strict compares, which the startup cache does not use. + strict = N.compile_strict(compiler, kind, pixel_hlsl(name), 'ps_%s_strict_%s' % (name, kind), + ps_profile, directory) + outcome = 'no image matched' + for denormals in L.DENORMALS: + run = draw(renderer, adapter, vs, strict, files[denormals]) + if run.returncode == 0: + outcome = 'all %d pixels matched (%s denormals)' % ( + WIDTH * HEIGHT, denormals if sensitive else 'either') + break + print(' recorded: %s %s %s with %s: %s' % (kind, adapter, name, N.STRICT[kind], outcome)) + print(' ' + str(header)) + check('%s %s: one denormal behaviour across the denormal-sensitive fixtures: %s' % ( + kind, adapter, sorted(map(str, modes))), len(modes) == 1 and None not in modes) + + +def guarded(part): + """Run one part of the check; a refusal where a text is needed is a failure of that part, not a crash.""" + try: + part() + except (Refused, G.Unsupported) as error: + check('%s: every form it needs translates' % part.__name__, False, 'refused: %s' % error) + + +def main(): + if len(sys.argv) == 1: + guarded(controls) + guarded(check_fixtures) + else: + mode, *args = sys.argv[1:] + if mode == 'binary': + check_binary(*args) + elif mode == 'decode': + check_decode(*args) + elif mode == 'draw': + guarded(check_fixtures) + if failures: + print('FAIL: the fixtures do not translate as checked; not drawn') + sys.exit(1) + check_draw(*args) + else: + raise SystemExit(__doc__) + if failures: + print('FAIL') + for text in failures: + print(' ' + text) + sys.exit(1) + print('PASS') + + +if __name__ == '__main__': + main() diff --git a/tests/startup_gfx2hlsl_vopc_sdwa_check.py b/tests/startup_gfx2hlsl_vopc_sdwa_check.py index 0111e9cc60..fa6b49c65f 100644 --- a/tests/startup_gfx2hlsl_vopc_sdwa_check.py +++ b/tests/startup_gfx2hlsl_vopc_sdwa_check.py @@ -23,9 +23,11 @@ Binary32 class compares read a DWORD float in SRC0 and a selected, optionally sign-extended integer mask in SRC1. The emitted predicate is checked against the selected mask for each of the ten classes. Ordinary float predicates -remain refused in their SDWA form pending the NaN predicate work (#1920). +with DWORD selections must lower as their e64 form with the same neg/abs; tests/startup_gfx2hlsl_f32_sdwa_check.py +checks them against the ISA reference. -Refused: a float source with a sub-dword select, an SDWA modifier the decoder does not print for +Refused: a sub-dword or sign-extended class value, a byte or word select on an ordinary float source (its result +depends on the binary32 denormal mode), sext() on a float source, an SDWA modifier the decoder does not print for compares, a missing or repeated select, and a destination that is not vcc_lo, EXEC or an SGPR. The draw: a source-built pixel program multiplies the packed screen position by a constant and keeps a pixel when @@ -269,9 +271,16 @@ def controls(): and all(expr.run({'s3': a, 'v2': b})[0] == int(field(a, 'BYTE_3', True) != field(b, 'WORD_1', False)) for a, b in PAIRS), str((asm, stage.lines))) - # 6. Float compares stay refused in their SDWA form, with every modifier and destination. - accepted, float_forms = [], 0 + # 6. Float compares in their SDWA form (#1906): with DWORD selections every condition, modifier and destination + # emits the text of the e64 form with the same modifiers. Byte and word selections, NaN and the compiler models + # are checked in tests/startup_gfx2hlsl_f32_sdwa_check.py. + differ, float_forms = [], 0 modifiers = [(False, False), (True, False), (False, True), (True, True)] + + def spelled(reg, neg, absolute): + reg = '|%s|' % reg if absolute else reg + return '-' + reg if neg else reg + for op, (cmpx, cond, typ) in sorted(COMPARES.items()): if typ != 'f32' or cond in ('class', 'tru'): continue @@ -281,9 +290,20 @@ def controls(): SGPR_DST if destination == 'sgpr' else None) name, asm = decode_one(code) float_forms += 1 - if refusal(name, asm) != '%s: f32 SDWA compares are not lowered' % name: - accepted.append(asm) - check('%d float SDWA forms refused' % float_forms, not accepted and float_forms == 15 * 16 * 3, str(accepted[:2])) + e64 = name.replace('_sdwa', '_e64') + operands = '%s, %s' % (spelled('v1', neg0, abs0), spelled('v2', neg1, abs1)) + if not cmpx: + operands = '%s, %s' % ('s%d' % SGPR_DST if destination == 'sgpr' else 'vcc_lo', operands) + if refusal(name, asm) is not None: + differ.append((asm, refusal(name, asm))) + continue + _, sdwa_stage = translate(code) + e64_stage = Probe() + e64_stage.one(e64, '%s %s' % (e64, operands)) + if sdwa_stage.lines != e64_stage.lines or sdwa_stage.masks != e64_stage.masks: + differ.append((asm, sdwa_stage.lines, e64_stage.lines)) + check('%d float SDWA forms with DWORD selections lower as the e64 form' % float_forms, + not differ and float_forms == 15 * 16 * 3, str(differ[:2])) # 7. Refusals, each with its own message. def expect(label, name, asm, message): @@ -326,11 +346,21 @@ def expect(label, name, asm, message): None, 'DWORD', 'BYTE_1')) check('class float source preserves abs and neg', 'class32((-abs(F(v1))))' in class_stage.lines[0], str(class_stage.lines)) + # A byte or word select on an ordinary float source stays refused (#1906): the field is +0 or a denormal, so the + # result depends on the binary32 denormal mode. for index in (0, 1): for sel in SELS[:-1]: sels = ('DWORD', sel) if index else (sel, 'DWORD') name, asm = decode_one(vopc_sdwa(0x01, source('v', 1), source('v', 2), None, *sels)) - expect('float src%d_sel:%s' % (index, sel), name, asm, 'v_cmp_lt_f32_sdwa: f32 SDWA compares are not lowered') + expect('float src%d_sel:%s' % (index, sel), name, asm, + 'v_cmp_lt_f32_sdwa: f32 SDWA byte/word selection depends on the binary32 denormal mode') + expect('sext() on a float source', 'v_cmp_lt_f32_sdwa', + 'v_cmp_lt_f32_sdwa vcc_lo, sext(v1), v2 src0_sel:BYTE_0 src1_sel:DWORD', + 'v_cmp_lt_f32_sdwa: sext() on a float source') + for extra in ('clamp', 'mul:2'): + expect('float compare modifier %s' % extra, 'v_cmp_lt_f32_sdwa', + 'v_cmp_lt_f32_sdwa vcc_lo, v1, v2 src0_sel:DWORD src1_sel:DWORD %s' % extra, + 'v_cmp_lt_f32_sdwa with SDWA modifier %s' % extra) base = 'v_cmp_lt_i32_sdwa vcc_lo, v1, v2' for extra in ('clamp', 'dst_sel:DWORD', 'dst_unused:UNUSED_PAD', 'src2_sel:DWORD', 'mul:2'): expect('modifier %s' % extra, 'v_cmp_lt_i32_sdwa', '%s src0_sel:DWORD src1_sel:DWORD %s' % (base, extra), diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index bd078c08b3..c4c601c5f4 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -2095,7 +2095,14 @@ def op_v_cvt_pkrtz_f16_f32(self, ops, mods): # reads its src0_sel/src1_sel field, zero-extended or, under sext(), sign-extended, and the # compare then reads that 32-bit value as its type. A binary32 class compare reads the whole # float in SRC0 and selects an integer field in SRC1 for the mask. - def compare_sdwa_sources(self, base, typ, ops, mods, *, float_src0=False): + # + # An f32 source (#1906) with a DWORD selection reads the register as binary32 with neg/abs as in + # the e64 form. A BYTE or WORD selection is refused: LLVM's SIPeepholeSDWA models it as the field + # zero-extended and read as binary32 bits (the RDNA 2 ISA guide gives SDWAB only its fields), so + # the field is always +0 or a denormal and the result depends on the binary32 denormal mode, which + # this translator does not read (pixel_aot refuses the same forms). sext() is valid for integer + # operands only (LLVM AMDGPUModifierSyntax; LLVM 15 does not decode the bit on a float source). + def compare_sdwa_sources(self, base, typ, ops, mods, *, float_src0=False, float_sources=False): fields = {} for m in mods: key, _, value = m.partition(':') @@ -2113,6 +2120,15 @@ def compare_sdwa_sources(self, base, typ, ops, mods, *, float_src0=False): raise Unsupported('%s requires a DWORD float source' % base) sources.append(self.f(token)) continue + if float_sources: + if 'sext(' in token: + raise Unsupported('%s: sext() on a float source' % base) + if selection != 'DWORD': + raise Unsupported('%s: f32 SDWA byte/word selection depends on the binary32 denormal mode' + % base) + # The whole word: the same text as the e64 form with the same modifiers. + sources.append(self.f(token)) + continue if sext: token = token[len('sext('):-1] word = sdwa_source(self.u(token, low16_only=selection in ('BYTE_0', 'BYTE_1', 'WORD_0')), selection, sext) @@ -2222,10 +2238,6 @@ def compare(self, base, ops, mods=()): # Conditions outside the table or dedicated lowering stay refused in both forms. if cond not in table: raise Unsupported(base) - if sdwa and typ == 'f32': - # The f32 predicates are verified on the plain and e64 forms only (#1920); the SDWA forms - # stay refused until they are verified too. - raise Unsupported('%s: f32 SDWA compares are not lowered' % base) if sdwa: self.check_compare_sdwa_destination(base, writes_exec, ops) if len(ops) == 3: @@ -2233,7 +2245,8 @@ def compare(self, base, ops, mods=()): else: dst, (a, b) = ('exec' if writes_exec else 'vcc'), ops if sdwa: - x, y = self.compare_sdwa_sources(base, typ, [a, b], mods) + # f32 sources: DWORD selections only, read as in the e64 form, so {2} tests the registers. + x, y = self.compare_sdwa_sources(base, typ, [a, b], mods, float_sources=typ == 'f32') elif typ == 'f32': x, y = self.f(a), self.f(b) elif typ == 'i32':