From 583178d4034e7c0eb6150d1003774f8e08340ae1 Mon Sep 17 00:00:00 2001 From: The Void Date: Tue, 24 Nov 2026 22:59:51 +0100 Subject: [PATCH] startup: keep gfx2hlsl f32 n-forms, o and u exact under FXC and DXC The startup cache compiles gfx2hlsl pairs with FXC and no flags, and neither compiler keeps float compares IEEE-strict unless asked. FXC compiled !(a < b) to ge (and likewise for <=, >, >=), so v_cmp nlt, nle, ngt and nge gave 0 on every NaN pair; DXC folded a != a and a == a, so u was constant 0 and o constant 1. WARP and an RX 9070 XT agreed, and /Gis or -Gis restored the ISA result. The n-forms now read !(ORDERED && a OP b), and o and u read ORDERED and !ORDERED, with ORDERED the bit test of both source words that lg uses since #1920. neq is unchanged. Compiler flags are unchanged. tests/startup_gfx2hlsl_f32_nan_check.py evaluates every f32 form under IEEE compares and under models of both rewrites, and draws every lowered condition but lg/nlg with FXC and DXC; the pre-fix text fails on the compiler that rewrote it and matches that rewrite's image. Strict-flag runs are recorded as a diagnostic. Fixes #1936 Co-Authored-By: Claude Opus 5.5 --- .github/workflows/windows.yml | 3 + CMakeLists.txt | 11 + docs/experiments.md | 21 ++ tests/startup_gfx2hlsl_f32_lg_check.py | 14 +- tests/startup_gfx2hlsl_f32_nan_check.py | 464 ++++++++++++++++++++++++ tools/startup/gfx2hlsl.py | 10 +- 6 files changed, 513 insertions(+), 10 deletions(-) create mode 100644 tests/startup_gfx2hlsl_f32_nan_check.py diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index b7d2ec863..4d5ae7789 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -90,6 +90,8 @@ jobs: (["tests/startup_gfx2hlsl_sop2_complement_check.py", "decode", "libLLVM-15.so.1"], {}), (["tests/startup_gfx2hlsl_f32_lg_check.py", "binary", "llvm-mc-15"], {}), (["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_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"], {}), @@ -206,6 +208,7 @@ jobs: tests/startup_gfx2hlsl_vopc_sdwa_check.py \ tests/startup_gfx2hlsl_sop2_complement_check.py \ tests/startup_gfx2hlsl_f32_lg_check.py \ + tests/startup_gfx2hlsl_f32_nan_check.py \ tests/startup_inline_sampler_check.py \ tests/startup_buffer_address_check.py \ tests/startup_smem_address_check.py \ diff --git a/CMakeLists.txt b/CMakeLists.txt index 683290def..951115f23 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2075,6 +2075,9 @@ if(EXISTS "${INTAKE_PYTHON}") # +-0, denormals, +-1, +-max, +-inf and NaNs against the ISA reference; the old != lowering must fail. add_test(NAME startup_gfx2hlsl_f32_lg COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_f32_lg_check.py") + # 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") # 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") @@ -2199,6 +2202,14 @@ if(EXISTS "${INTAKE_PYTHON}") "$" ${adapter} $<$:${STARTUP_FXC}>) set_tests_properties(startup_gfx2hlsl_f32_lg_${adapter} PROPERTIES TIMEOUT 300 SKIP_RETURN_CODE 77) endforeach() + # Every lowered f32 condition but lg/nlg decides 16 x 16 pixels with FXC and DXC, checked pixel by pixel (#1936); + # the text before #1936 must fail on the compiler that rewrote it and match that rewrite's image. + foreach(adapter IN ITEMS warp hardware) + add_test(NAME startup_gfx2hlsl_f32_nan_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_f32_nan_check.py" draw "${DXC_EXECUTABLE}" + "$" ${adapter} $<$:${STARTUP_FXC}>) + set_tests_properties(startup_gfx2hlsl_f32_nan_${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 f3bb18513..bff52d76a 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -6077,6 +6077,27 @@ and matches its own image. Run `ctest --preset dev --no-tests=error -R '^startup `tests/startup_gfx2hlsl_f32_lg_check.py binary ` and `decode ` round-trip the forms and the fixtures through LLVM 15. +**f32 nlt, nle, ngt, nge, o and u under compilers without `-Gis` (#1936).** The startup cache compiles every +`gfx2hlsl` pair with FXC and no flags, and neither compiler keeps float compares IEEE-strict unless asked. FXC +compiled `!(a < b)` to `ge a, b` (and likewise for `<=`, `>`, `>=`), so nlt, nle, ngt and nge gave 0 on every NaN +pair where the ISA gives 1; DXC folded `a != a` and `a == a`, so u was constant 0 and o constant 1. WARP and an +RX 9070 XT (driver 32.0.31044.16) agreed in every case, and `/Gis` or `-Gis` restored the ISA result, so the +bytecode decides it. The n-forms now read `!(ORDERED && a OP b)` and o and u read ORDERED and `!ORDERED`, with +ORDERED the bit test of both source words that lg uses; neq is unchanged, as both compilers keep `!(a == b)` true +on NaN. `startup_gfx2hlsl_f32_nan` evaluates the HLSL of all 510 f32 forms over the 256 source pairs under IEEE +compares and under models of the two rewrites (a negation pushed into the opposite relation; reflexive float +relations folded, and less-or-greater read as unordered not-equal, #1920); each equals the ISA reference, and the +text before #1936 fails all 204 forms of the six conditions under the model of the compiler that broke it. The +draws `startup_gfx2hlsl_f32_nan_{warp,hardware}` write four results of each of the 13 conditions other than lg +and nlg (e32, and e64 with `(-a, |b|)`, `(-|a|, -b)`, `(|a|, b)`) and keep pixels through `v_cmpx_*_e64` for the +six, compiled with 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 #1936 fails each draw on the compiler that +rewrote it (156 NaN pixels against the flushed image) and matches that rewrite's image; on the other compiler it +is recorded, and it matched. Also recorded, not checked: with `/Gis` and `-Gis` the old and the new text of the six +both matched every pixel. Run `ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_f32_nan' -V`; +`tests/startup_gfx2hlsl_f32_nan_check.py binary ` and `decode ` round-trip the fixtures through +LLVM 15. + **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 795d5d8c6..c642f52e6 100644 --- a/tests/startup_gfx2hlsl_f32_lg_check.py +++ b/tests/startup_gfx2hlsl_f32_lg_check.py @@ -346,11 +346,11 @@ def first_mismatch(expr, cond, mods, model=reference): return None -# The lowering of every other condition before #1920, for v_cmp_*_f32_e32 vcc_lo, v1, v2. +# The lowering of every other condition before #1920, for v_cmp_*_f32_e32 vcc_lo, v1, v2. nlt, nle, ngt, nge, o and u +# changed with #1936 and are checked in tests/startup_gfx2hlsl_f32_nan_check.py. UNCHANGED = {'f': 'false', 'lt': 'F(v1) < F(v2)', 'eq': 'F(v1) == F(v2)', 'le': 'F(v1) <= F(v2)', - 'gt': 'F(v1) > F(v2)', 'ge': 'F(v1) >= F(v2)', 'o': '(F(v1) == F(v1) && F(v2) == F(v2))', - 'u': '(F(v1) != F(v1) || F(v2) != F(v2))', 'nge': '!(F(v1) >= F(v2))', 'ngt': '!(F(v1) > F(v2))', - 'nle': '!(F(v1) <= F(v2))', 'neq': '!(F(v1) == F(v2))', 'nlt': '!(F(v1) < F(v2))'} + 'gt': 'F(v1) > F(v2)', 'ge': 'F(v1) >= F(v2)', 'neq': '!(F(v1) == F(v2))'} +NAN_FORMS = ('nlt', 'nle', 'ngt', 'nge', 'o', 'u') def controls(): @@ -375,7 +375,7 @@ def controls(): (old_reference(cond, a, b) != reference(cond, a, b)) == (is_nan(a) or is_nan(b)) for cond in ('lg', 'nlg') for a, b in PAIRS)) check('every f32 condition of the table is checked (%d opcodes)' % len(F32_OPS), len(F32_OPS) == 30 and sorted( - {COMPARES[op][1] for op in F32_OPS}) == sorted(set(UNCHANGED) | {'lg', 'nlg'})) + {COMPARES[op][1] for op in F32_OPS}) == sorted(set(UNCHANGED) | {'lg', 'nlg'} | set(NAN_FORMS))) # 2. The new lowering's text; every other condition keeps its text. asm, stage = translate(compare_code(0x05, 'e32')) @@ -391,12 +391,12 @@ def controls(): changed = [] for op in F32_OPS: cmpx, cond, _ = COMPARES[op] - if cmpx or cond in ('lg', 'nlg'): + if cmpx or cond in ('lg', 'nlg') + NAN_FORMS: continue asm, stage = translate(compare_code(op, 'e32')) if stage.lines != ['vcc = ((%s) ? 1u : 0u);' % UNCHANGED[cond]]: changed.append((asm, stage.lines)) - check('the 13 other conditions keep their lowering', not changed, str(changed)) + check('the %d other conditions keep their lowering' % len(UNCHANGED), not changed, str(changed)) # 3. Every form of every condition equals the reference on every pair; the old lg/nlg lowering does not. forms = evaluations = 0 diff --git a/tests/startup_gfx2hlsl_f32_nan_check.py b/tests/startup_gfx2hlsl_f32_nan_check.py new file mode 100644 index 000000000..3394ee151 --- /dev/null +++ b/tests/startup_gfx2hlsl_f32_nan_check.py @@ -0,0 +1,464 @@ +"""f32 VOPC nlt, nle, ngt, nge, o and u in tools/startup/gfx2hlsl.py under FXC and DXC without -Gis (#1936). + + (no arguments) translation controls, pure Python + binary assemble the decoded text 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 + draw [fxc] + compile the pixel fixtures with FXC ps_5_1 when given and with DXC ps_6_0, + draw 16 x 16 pixels each and compare every pixel with the ISA reference + (needs tests/offscreen_check.cpp built as offscreen_check) + +The startup cache compiles gfx2hlsl pairs with FXC and no flags. Neither FXC nor DXC keeps float compares +IEEE-strict unless asked (/Gis, -Gis), and both rewrite the text these conditions had before #1936: + + FXC !(a < b) becomes a >= b (and so on for <=, >, >=), false when a source is NaN: nlt, nle, ngt and nge + gave 0 on every NaN pair where the ISA gives 1. + DXC a != a and a == a fold to false and true: u gave 0 and o gave 1 on every pair. + +The lowering is now `!(ORDERED && a OP b)` for the n-forms and ORDERED and !ORDERED for o and u, where ORDERED +tests both source words for NaN on their bits, as lg and nlg do since #1920. Any rewrite of the float relation is +then behind an integer NaN test. + +Controls: every f32 condition of the compare table, in every form (v_cmp and v_cmpx; e32, and e64 under every +neg/abs combination), is translated and its HLSL is evaluated over all 256 pairs of the 16 source words of +tests/startup_gfx2hlsl_f32_lg_check.py under three models of the compiler: IEEE binary32 compares; the FXC +rewrite, a negation pushed through && and || into the opposite relation; and the DXC folds, a relation between +two identical float operands folded to its value on equal operands and a < b || a > b read as a != b (#1920). +Each must equal the ISA reference. The text before #1936, rebuilt from the same operands, must fail under the +model of the compiler that broke it and hold under IEEE compares. + +The draws: as in tests/startup_gfx2hlsl_f32_lg_check.py, a source-built pixel program builds one of the 16 words +from x and one from y, so the 16 x 16 pixels cover every pair. Fixture `cmp_` writes four results of one +condition as 1.0 or 0.0: v_cmp__f32_e32, and the e64 form with (-a, |b|), (-|a|, -b) and (|a|, b). +Fixture `cmpx_` keeps a pixel when v_cmpx__f32_e64 (-a, |b|) holds. All 13 lowered conditions +other than lg and nlg are drawn; every fixture must match the ISA image with denormals kept or flushed, the same +choice across the fixtures of one compiler and adapter whose image depends on it (f, o and u do not). The text +before #1936 must fail its draw against both images on the compiler that broke it and must match the image of that +compiler's rewrite. As a diagnostic, not checked, the old and the new text of the six are also drawn with /Gis and +-Gis, which the startup cache does not use. +""" +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 +from startup_gfx2hlsl_f32_lg_check import ( # noqa: E402 + COMPARES, F32_OPS, PAIRS, VALUES, CLEAR, ONE, WIDTH, HEIGHT, Expr, G, assemble, compile_hlsl, flushed, + is_nan, modified, reference) + +NAN_FORMS = ('nlt', 'nle', 'ngt', 'nge', 'o', 'u') +BROKEN_BY = {'nlt': 'fxc', 'nle': 'fxc', 'ngt': 'fxc', 'nge': 'fxc', 'o': 'dxc', 'u': 'dxc'} +OPPOSITE = {'<': '>=', '>=': '<', '>': '<=', '<=': '>', '==': '!=', '!=': '=='} +# The text before #1936, from the float operand texts X and Y. +OLD = {'nlt': '!({0} < {1})', 'nle': '!({0} <= {1})', 'ngt': '!({0} > {1})', 'nge': '!({0} >= {1})', + 'o': '({0} == {0} && {1} == {1})', 'u': '({0} != {0} || {1} != {1})'} +ORDERED = '(v1 & 0x7fffffffu) <= 0x7f800000u && (v2 & 0x7fffffffu) <= 0x7f800000u' +OPCODE = {cond: op for op, (cmpx, cond, typ) in COMPARES.items() if typ == 'f32' and not cmpx} +OPCODE_X = {cond: op for op, (cmpx, cond, typ) in COMPARES.items() if typ == 'f32' and cmpx} + +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 compiler models --------------------------------------------------------------------------------------- +class Model(Expr): + """Expr under a compiler model: 'ieee', 'fxc' (negation pushed into the opposite relation) or 'dxc' + (reflexive float relations folded, less-or-greater read as unordered not-equal).""" + + def __init__(self, text, model): + super().__init__(text) + self.model = model + + def floats(self, env, node): + return node[0] in OPPOSITE and self.eval(env, node[1])[0] == 'f' + + def negate(self, env, node): + if node[0] == 'u!': + return node[1] + if node[0] in ('&&', '||'): + return ('||' if node[0] == '&&' else '&&', self.negate(env, node[1]), self.negate(env, node[2])) + if node[0] in OPPOSITE: + return (OPPOSITE[node[0]], node[1], node[2]) + return ('u!', node) + + def eval(self, env, node): + if self.model == 'fxc' and node[0] == 'u!': + inner = node[1] + if inner[0] in ('&&', '||') or self.floats(env, inner): + return self.eval(env, self.negate(env, inner)) + if self.model == 'dxc': + if self.floats(env, node) and node[1] == node[2]: + return 'b', int(node[0] in ('==', '<=', '>=')) + if (node[0] == '||' and node[1][0] == '<' and node[2][0] == '>' and node[1][1:] == node[2][1:] + and self.floats(env, node[1])): + return self.eval(env, ('!=',) + node[1][1:]) + return super().eval(env, node) + + +MODELS = ('ieee', 'fxc', 'dxc') + + +def operands(form, mods, cmpx): + """The float operand texts X and Y of this form, read from the lt compare of the same form.""" + op = OPCODE_X['lt'] if cmpx else OPCODE['lt'] + _asm, stage = L.translate(L.compare_code(op, form, mods)) + text = stage.lines[0].split(' = ', 1)[1] + assert text.startswith('((') and text.endswith(') ? 1u : 0u);'), text + x, y = text[2:-len(') ? 1u : 0u);')].split(' < ') + return x, y + + +def first_mismatch(expr, cond, mods): + (neg0, abs0), (neg1, abs1) = mods + for a, b in PAIRS: + want = reference(cond, modified(a, neg0, abs0), modified(b, neg1, abs1)) + got = expr.run({'v1': a, 'v2': b}) + if got != want: + return '%08x %08x gives %d, the ISA %d' % (a, b, got, want) + return None + + +def controls(): + # 1. The emitted text. + want = {'nlt': '!(%s && F(v1) < F(v2))', 'nle': '!(%s && F(v1) <= F(v2))', 'ngt': '!(%s && F(v1) > F(v2))', + 'nge': '!(%s && F(v1) >= F(v2))', 'o': '(%s)', 'u': '!(%s)'} + wrong = [] + for cond in NAN_FORMS: + _asm, stage = L.translate(L.compare_code(OPCODE[cond], 'e32')) + line = 'vcc = ((%s) ? 1u : 0u);' % (want[cond] % ORDERED) + if stage.lines != [line]: + wrong.append((cond, stage.lines, line)) + check('v_cmp_{nlt,nle,ngt,nge,o,u}_f32_e32 test both source words on their bits', not wrong, str(wrong)) + _asm, stage = L.translate(L.compare_code(OPCODE_X['nge'], 'e64', ((True, True), (False, True)))) + check('v_cmpx_nge_f32_e64 -|a|, |b| tests the words under the modifiers: %s' % stage.lines, stage.lines == [ + 'exec = ((!(%s && (-abs(F(v1))) >= abs(F(v2)))) ? 1u : 0u);' % ORDERED]) + + # 2. The models: on the text before #1936 they reproduce what was drawn. + x, y = 'F(v1)', 'F(v2)' + seen = {} + for cond in NAN_FORMS: + for model in MODELS: + expr = Model(OLD[cond].format(x, y), model) + seen[(cond, model)] = first_mismatch(expr, cond, ((False, False), (False, False))) is None + check('models: the old text holds under IEEE compares and fails exactly under the compiler that broke it', + all(seen[(c, m)] == (m == 'ieee' or m != BROKEN_BY[c]) for c in NAN_FORMS for m in MODELS), + str(sorted(k for k, v in seen.items() if v != (k[1] == 'ieee' or k[1] != BROKEN_BY[k[0]])))) + lg = Model('(F(v1) < F(v2) || F(v1) > F(v2))', 'dxc') + check('models: dxc reads the #1920 less-or-greater as unordered not-equal', + lg.run({'v1': 0x7fc00000, 'v2': 0}) == 1 and Model('(F(v1) < F(v2) || F(v1) > F(v2))', 'ieee').run( + {'v1': 0x7fc00000, 'v2': 0}) == 0) + + # 3. Every form of every condition under every model; the old text of the six fails under its compiler. + forms, wrong, caught, missed = 0, [], 0, [] + for op, form, mods, _destination in L.all_forms(): + cmpx, cond, _ = COMPARES[op] + asm, stage = L.translate(L.compare_code(op, form, mods)) + text = stage.lines[0].split(' = ', 1)[1][:-1] + forms += 1 + for model in MODELS: + miss = first_mismatch(Model(text, model), cond, mods) + if miss: + wrong.append((asm, model, miss)) + if cond in NAN_FORMS: + old = '((%s) ? 1u : 0u)' % OLD[cond].format(*operands(form, mods, cmpx)) + if (first_mismatch(Model(old, 'ieee'), cond, mods) is None + and first_mismatch(Model(old, BROKEN_BY[cond]), cond, mods)): + caught += 1 + else: + missed.append(asm) + check('%d f32 forms x %d pairs equal the ISA reference under IEEE, FXC and DXC compares' % (forms, len(PAIRS)), + not wrong and forms == len(F32_OPS) * 17, str(wrong[:3])) + check('the text before #1936 fails %d of %d nlt/nle/ngt/nge/o/u forms under its compiler and holds under IEEE' % ( + caught, caught + len(missed)), caught == 6 * 2 * 17 and not missed, str(missed[:3])) + + # 4. EXEC already reduced: the result is masked by the lane's EXEC bit. + wrong = [] + for cond in NAN_FORMS: + for op in (OPCODE[cond], OPCODE_X[cond]): + mods = ((True, False), (False, True)) + asm, stage = L.translate(L.compare_code(op, 'e64', mods), exec_known=False) + text = stage.lines[0].split(' = ', 1)[1][:-1] + for model in MODELS: + expr = Model(text, model) + for a, b in PAIRS: + for lane in (0, 1): + want = lane & reference(cond, modified(a, True, False), modified(b, False, True)) + if expr.run({'v1': a, 'v2': b, 'exec': lane}) != want: + wrong.append((asm, model, hex(a), hex(b), lane)) + check('inactive lane: the six results are masked by EXEC under every model', not wrong, str(wrong[:3])) + + +# ---- the pixel fixtures ----------------------------------------------------------------------------------------- +DRAWN = [c for c in ('f', 'lt', 'eq', 'le', 'gt', 'ge', 'neq') + NAN_FORMS] +CMP_MODS = [((False, False), (False, False)), ((True, False), (False, True)), ((True, True), (True, False)), + ((False, True), (False, False))] +SDST = 6 + + +def fixtures(): + """name -> (body words, [(condition, src0 (neg, abs), src1 (neg, abs))] per exported word, or one for v_cmpx).""" + out = {} + for cond in DRAWN: + words, results = b'', [] + for index, mods in enumerate(CMP_MODS): + (n0, a0), (n1, a1) = mods + if index == 0: + words += L.vopc_e32(OPCODE[cond], ('v', 3), 4) + L.cndmask_e64(6, L.VCC_LO) + else: + words += L.vop3_compare(OPCODE[cond], SDST, 3, 4, (n0, n1), (a0, a1)) + L.cndmask_e64(6 + index, SDST) + results.append((cond, mods[0], mods[1])) + words += struct.pack('', 'nge': '>='}.get(cond) + if rel and body.startswith('!(ORD && ') and (' %s ' % rel) in body and body.count(' %s ' % rel) == 1: + xy = body[len('!(ORD && '):-1].split(' %s ' % rel) + if len(xy) == 2 and not any(r in body for r in (' < ', ' <= ', ' > ', ' >= ') if r != ' %s ' % rel): + body = OLD[cond].format(*xy) + break + else: + if body in ('ORD', '!ORD'): + body = OLD['o' if body == 'ORD' else 'u'].format('F(v3)', 'F(v4)') + line = line[:len(line) - len(stripped)] + '%s = ((%s) ? 1u : 0u);' % (target, body) + out.append(line) + return '\n'.join(out) + '\n' + + +def check_fixtures(): + for name, (_words, results) in FIXTURES.items(): + conds = {c for c, _m0, _m1 in results} + text, old = pixel_hlsl(name), pixel_hlsl(name, old=True) + guarded = text.count('(v3 & 0x7fffffffu)') + if conds & set(NAN_FORMS): + check('%s: %d compares test the source words; the old text has none' % (name, guarded), + guarded == len(results) and old.count('(v3 & 0x7fffffffu)') == 0 and old != text) + else: + check('%s: no bit test, the old text is the same' % name, guarded == 0 and old == text) + for denormals in L.DENORMALS: + good, bad = image(name, denormals=denormals), image(name, blind, denormals) + differing = sum(good[i:i + 4] != bad[i:i + 4] for i in range(0, len(good), 4)) + want = 156 if conds & set(NAN_FORMS) else 0 + check('%s (%s denormals): the compiler-rewritten old text changes %d of %d pixels' % ( + name, denormals, differing, WIDTH * HEIGHT), differing == want) + # The evaluator on the compare lines of each stage reads the same decisions as the images, under every model. + for name, (_words, results) in FIXTURES.items(): + for old in (False, True): + lines = [ln.strip() for ln in pixel_hlsl(name, old).splitlines() + if ln.strip().startswith(('vcc = ', 's6 = ', 'exec = ')) and ('F(v3)' in ln or '(v3 & ' in ln)] + ok = len(lines) == len(results) or (results[0][0] == 'f' and not lines) + for line, (cond, m0, m1) in zip(lines, results): + for model in MODELS: + if old and model != 'ieee': + continue + expr = Model(line.split(' = ', 1)[1][:-1], model) + ok = ok and all(expr.run({'v3': a, 'v4': b}) == reference(cond, modified(a, *m0), modified(b, *m1)) + for a, b in PAIRS) + check('%s%s: evaluated compares equal the ISA reference' % (name, ' (old, IEEE)' if old else ''), ok, + str(lines)) + + +def fixture_text(words): + return [L.llvm_text(asm) for _o, _s, _n, asm in G.decode(words)] + + +def check_binary(llvm_mc): + 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 + 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 + + +STRICT = {'fxc': '/Gis', 'dxc': '-Gis'} + + +def compile_strict(compiler, kind, source, out, profile, directory): + """compile_hlsl with IEEE-strict compares, for the recorded comparison only.""" + path = directory / ('%s.hlsl' % out) + path.write_text(source, encoding='ascii') + target = directory / ('%s.cso' % out) + if kind == 'fxc': + args = [compiler, '/nologo', STRICT[kind], '/T', profile, '/E', 'main', '/Fo', str(target), str(path)] + else: + args = [compiler, STRICT[kind], '-T', profile, '-E', 'main', '-Fo', str(target), str(path)] + subprocess.run(args, check=True, capture_output=True, text=True) + return target + + +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 negative 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(): + conds = {c for c, _m0, _m1 in results} + 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='))) + # f, o and u do not read denormals: both images are the same and say nothing about the mode. + 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 not conds & set(NAN_FORMS): + continue + bad = compile_hlsl(compiler, kind, pixel_hlsl(name, old=True), 'ps_%s_old_%s' % (name, kind), + ps_profile, directory) + if BROKEN_BY[min(conds)] == kind: + for denormals in L.DENORMALS: + run = draw(renderer, adapter, vs, bad, files[denormals]) + lines = [ln for ln in run.stderr.splitlines() if 'pixel' in ln] + check('%s %s %s old text fails against the %s image: %s' % ( + kind, adapter, name, denormals, ' / '.join(lines)), + run.returncode != 0 and lines and lines[0].startswith('pixel mismatch'), + run.stdout + run.stderr) + own = L.words_file(directory, name + '_blind', image(name, blind, matched or 'ieee')) + run = draw(renderer, adapter, vs, bad, own) + check('%s %s %s old text matches the image of the %s rewrite' % (kind, adapter, name, kind), + 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, ' / '.join( + ln for ln in (run.stdout + run.stderr).splitlines() if 'pixel' in ln))) + # Diagnostic, not checked: the old and the new text with IEEE-strict compares (/Gis, -Gis), which + # the startup cache does not use. + for label, old in (('old', True), ('new', False)): + strict = compile_strict(compiler, kind, pixel_hlsl(name, old=old), + 'ps_%s_%s_strict_%s' % (name, label, 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 %s text with %s: %s' % ( + kind, adapter, name, label, 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 main(): + if len(sys.argv) == 1: + controls() + check_fixtures() + else: + mode, *args = sys.argv[1:] + if mode == 'binary': + check_binary(*args) + elif mode == 'decode': + check_decode(*args) + elif mode == 'draw': + check_fixtures() + 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/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 8695b6c1f..601db1cd1 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -1435,11 +1435,15 @@ def compare(self, base, ops, mods=()): # HLSL != is unordered, so lg reads as less or greater, behind {2}: both source words not NaN, # tested on their bits. DXC marks float compares fast unless -Gis, and a driver may then fold # less-or-greater into an unordered not-equal. + # FXC without /Gis compiles !(a < b) to a >= b, false on NaN, and DXC without -Gis folds x != x + # and x == x to constants (#1936). So nlt, nle, ngt and nge negate an ordered relation behind {2}, + # and o and u read {2} alone; the startup cache compiles with FXC and no flags. table = {'lt': '{0} < {1}', 'eq': '{0} == {1}', 'le': '{0} <= {1}', 'gt': '{0} > {1}', 'lg': '({2} && ({0} < {1} || {0} > {1}))', 'ne': '{0} != {1}', - 'ge': '{0} >= {1}', 'neq': '!({0} == {1})', 'nlt': '!({0} < {1})', 'nle': '!({0} <= {1})', 'ngt': '!({0} > {1})', - 'nge': '!({0} >= {1})', 'nlg': '!({2} && ({0} < {1} || {0} > {1}))', 'o': '({0} == {0} && {1} == {1})', - 'u': '({0} != {0} || {1} != {1})', 't': 'true', 'f': 'false'} + 'ge': '{0} >= {1}', 'neq': '!({0} == {1})', 'nlt': '!({2} && {0} < {1})', + 'nle': '!({2} && {0} <= {1})', 'ngt': '!({2} && {0} > {1})', 'nge': '!({2} && {0} >= {1})', + 'nlg': '!({2} && ({0} < {1} || {0} > {1}))', 'o': '({2})', 'u': '!({2})', + 't': 'true', 'f': 'false'} # Conditions outside the table, v_cmp_class_* among them, stay refused in both forms. if cond not in table: raise Unsupported(base)