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
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6449,6 +6449,11 @@ if(EXISTS "${INTAKE_PYTHON}")
"$<TARGET_FILE:offscreen_check>" ${adapter})
set_tests_properties(pixel_aot_sad_${adapter} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77
ENVIRONMENT "FORM_COVERAGE_RECORD=${CMAKE_BINARY_DIR}/form_coverage/pixel_aot_sad_${adapter}.txt")
add_test(NAME pixel_aot_sdwa_f16_compare_${adapter} COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/pixel_aot_check.py" draw-sdwa-f16-compare "${DXC_EXECUTABLE}"
"$<TARGET_FILE:offscreen_check>" ${adapter})
set_tests_properties(pixel_aot_sdwa_f16_compare_${adapter} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77
ENVIRONMENT "FORM_COVERAGE_RECORD=${CMAKE_BINARY_DIR}/form_coverage/pixel_aot_sdwa_f16_compare_${adapter}.txt")
add_test(NAME pixel_aot_rndne_${adapter} COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/pixel_rndne_check.py" "${DXC_EXECUTABLE}"
"$<TARGET_FILE:offscreen_check>" ${adapter})
Expand Down
2 changes: 2 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -5814,6 +5814,8 @@ Scalar ALU support also covers the 64-bit SOP1/SOP2 family (`s_cmov_b64`, `s_not

The SDWA forms of the 16 `v_cmp_*_f32` predicates are lowered when both `src0_sel` and `src1_sel` are `DWORD`. With whole-dword selections they compare the operands exactly as the e32/e64 forms do; the translator emits identical HLSL for both, and a control asserts it. A byte or word selection would compare a zero-extended field as binary32 and is refused. The predicate draws are repeated with the SDWA compares for all four groups and the same five input pairs, including NaN on either side. A reference that inverts each group's unordered result must report a mismatch.

The binary16 SDWA comparisons `v_cmp_{f,lt,eq,le,gt,lg,ge,o,u,nge,nlg,ngt,nle,neq,nlt,tru}_f16_sdwa` and `v_cmp_class_f16_sdwa` lower with word selections. `WORD_1` selects the high half of a source; `WORD_0` and `DWORD` leave the low half, which is what the e32 form reads, so a comparison with both selections `DWORD` and no modifier emits the same HLSL as its e32 spelling, which a control asserts. `-`, `|x|` and `-|x|` apply after the selection, and the comparison is then the existing binary16 one: the ordered relations test `num16` on the selected, modified encodings and compare the widened values, and the class test reads `class16` of the selected, modified half against the mask. Sources are registers; byte selections, `sext()`, inline constants and literals (LLVM 15 prints them as `neg(2.0)` and similar), a missing selection, `exec` as the destination, a class mask with a selection other than `DWORD` or with a modifier, and the `v_cmpx` SDWA forms are refused. One program exports four words through the `R32G32B32A32_UINT` readback target: three words hold the 16 predicates as bits under (src0, src1, src0_sel, src1_sel) = (A, B, `WORD_1`, `WORD_0`), (-B, |A|, `WORD_1`, `DWORD`) and (-|A|, B, `DWORD`, `WORD_1`), and the fourth holds four class tests of C (`WORD_1`, -C `WORD_0`, |C| `WORD_1`, -|C| `DWORD`) against the mask D. Each of A, B and C carries binary16 values in both halves, so a selection that reads the wrong half changes the answer. Ten input sets cover ordered pairs both ways, equal values, signed zeros, infinities, denormals, quiet and signaling NaNs in either operand, and adjacent values; a static check asserts that every predicate bit other than f and tru, and every class bit, takes both values over the sets. For each wrong model (selections ignored, modifiers ignored) the first set it changes is drawn against that model's words and must report `pixel_mismatch`. That is 12 draws from one compile. They run as `pixel_aot_sdwa_f16_compare_hardware` and `pixel_aot_sdwa_f16_compare_warp` (`tests/pixel_aot_check.py draw-sdwa-f16-compare`), with their own coverage record listing all 17 forms, and passed on the NVIDIA GeForce RTX 4070 (driver 32.0.16.1714) and on WARP, Windows 11 (build 26300). As a check of the fixture, ignoring `WORD_1` in the lowering makes the draw mismatch on WARP. `tools/scan_unsupported_shaders.py` accepts the same forms. The LLVM 15 round trip was not run on that host. They show that the translated shader computes the stated comparisons on desktop GPUs, not that the console does.

Binary32 floating-point vector comparisons (`v_cmp_{f,lt,eq,le,gt,lg,ge,o,u,nge,nlg,ngt,nle,neq,nlt,tru}_f32` in e32, e64, and whole-dword SDWA forms) evaluate the 16 IEEE-754 relations against scalar and vector operands, including subnormals, signed zeroes, infinities, and quiet/signaling NaNs. The e32 (VOPC) encoding requires destination `vcc` or `vcc_lo`; SGPR destinations (`s52`, `s[52:53]`) are accepted on suffix-less and explicit `_e64` (VOP3) encodings. Source modifiers (`neg`, `abs`) are accepted on e64 operands (`-s44`, `|v18|`, `-|s44|`), while `clamp` and `op_sel` remain rejected on comparisons. Rejection controls verify that untracked source registers, SGPR destinations on explicit e32, and sub-dword SDWA selections (`src0_sel:WORD_0`, `src1_sel:BYTE_2`) raise `ValueError`. The synthetic fixture `f32_compares()` verifies all 16 predicates across e32, e64, and whole-dword SDWA, validating them against an exact reference oracle (`f32_compares_reference()`). Mutating predicates or modifiers produces a mismatch.

The original [#958](https://github.com/R0Y00/Xbox2Windows/issues/958) report at `0f0c3eb` recorded a strict comparison mismatch on an RX 9070 XT. Predicate bit 18 compares binary32 subnormal `0x00000001` with `-0.0` and should be clear. The hardware readback set it while WARP and the separate denormal-preservation control passed. The strict expectation and predicate mutation controls are unchanged. Reproduce with `ctest --preset dev --no-tests=error -R '^pixel_aot_comparisons_(warp|hardware)$' -V`. See the reduced desktop DXIL measurements below and the additional adapter results in #958.
Expand Down
191 changes: 190 additions & 1 deletion tests/pixel_aot_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -16291,6 +16291,7 @@ def _check_controls_inline(directory):
"check_sat_pack_u8_lowering",
"check_pack_b32_lowering",
"check_sad_lowering",
"check_sdwa_f16_compare_lowering",
"check_scalar_controls",
"check_three_way_templates",
"check_sdwa_unary_lowering",
Expand Down Expand Up @@ -16885,6 +16886,8 @@ def check_binary(directory, assembler, objcopy, library):
fixtures.append((uncompressed_export(pack_b32_group()), translate_int))
fixtures.append((uncompressed_export(pack_b32_inline_group()), translate_int))
fixtures.extend((uncompressed_export(sad_group(group)), translate_int) for group in SAD_BODIES)
# The binary16 SDWA comparisons and class tests, assembled, disassembled and translated like their source text.
fixtures.append((uncompressed_export(sdwa_f16_compares()), translate_int))
fixtures.extend((compressed_export(mask), translate_masked) for mask in (0x3, 0xC, 0xF))
fixtures.extend((uncompressed_export([], mask), translate_masked) for mask in (0x1, 0x8, 0x5, 0xA))
for restore in (MASKED_OR_RESTORE, MASKED_MOV_RESTORE):
Expand Down Expand Up @@ -20884,6 +20887,191 @@ def cmp_pred(pred: str, a: float, b: float) -> bool:
return (w0, w1, w2, w3)


# Binary16 SDWA comparisons and class tests (v_cmp_*_f16_sdwa, v_cmp_class_f16_sdwa). A (s44 -> v18), B (s45 -> v16),
# C (s46 -> v20) and the class mask D (s47 -> v21) carry binary16 values in both halves, so a selection that reads the
# wrong half changes the answer. Words 0..2 hold the 16 predicates under one (dst, src0, src1, src0_sel, src1_sel)
# each; word 3 holds four class tests as (source, src0_sel).
SDWA_F16_COMPARES = (
("vcc_lo", "v18", "v16", "WORD_1", "WORD_0"),
("s52", "-v16", "|v18|", "WORD_1", "DWORD"),
("vcc_lo", "-|v18|", "v16", "DWORD", "WORD_1"),
)
SDWA_F16_CLASSES = (("v20", "WORD_1"), ("-v20", "WORD_0"), ("|v20|", "WORD_1"), ("-|v20|", "DWORD"))
SDWA_F16_SETS = ( # (A, B, C, D), each half as high:low
(0x3c004000, 0x40003c00, 0x7e003c00, 0x3ff), # 1:2 against 2:1; qNaN:1 against every class
(0x7e003c00, 0x3c007e00, 0x80000000, 0x060), # qNaN:1 against 1:qNaN; -0:+0 against the zero classes
(0x80000000, 0x00008000, 0x7c00fc00, 0x204), # -0:+0 against +0:-0; +inf:-inf against the infinities
(0x7c00fc00, 0xfc007c00, 0x00018001, 0x090), # +inf:-inf against -inf:+inf; denormals against their classes
(0xc400c400, 0xc4004400, 0x7c013800, 0x101), # -4:-4 against -4:4; sNaN:0.5 against sNaN and +normal
(0x00018001, 0x80010001, 0xbc003c00, 0x108), # denormal pairs; -1:1 against the normal classes
(0x7c013800, 0x38007c01, 0x3c00bc00, 0x002), # sNaN against 0.5; 1:-1 against qNaN only
(0x31183119, 0x31193118, 0xfe007e00, 0x3fc), # adjacent values; -qNaN:qNaN against the non-NaN classes
(0x44003c00, 0xc4003c00, 0x3c007c00, 0x300), # 4:1 against -4:1, so every word compares greater
(0x3c007e00, 0x7e003c00, 0x3c007c00, 0x200), # 1:qNaN against qNaN:1, unordered for words 1 and 2
)


def sdwa_f16_compares() -> list[str]:
body = ["v_mov_b32_e32 v12, 0", "v_mov_b32_e32 v13, 0", "v_mov_b32_e32 v14, 0", "v_mov_b32_e32 v15, 0",
"v_mov_b32_e32 v16, s45", "v_mov_b32_e32 v18, s44", "v_mov_b32_e32 v20, s46", "v_mov_b32_e32 v21, s47"]

def emit_flag(acc, bit, cond, insn):
body.append(insn)
body.append(f"v_cndmask_b32_e64 v17, 0, 1, {cond}")
if bit:
body.append(f"v_lshlrev_b32_e32 v17, {bit}, v17")
body.append(f"v_or_b32_e32 {acc}, {acc}, v17")

for word, (dst, src0, src1, sel0, sel1) in enumerate(SDWA_F16_COMPARES):
for bit, pred in enumerate(PREDICATES):
emit_flag(f"v{12 + word}", bit, dst,
f"v_cmp_{pred}_f16_sdwa {dst}, {src0}, {src1} src0_sel:{sel0} src1_sel:{sel1}")
for bit, (source, sel) in enumerate(SDWA_F16_CLASSES):
dst = "s52" if bit % 2 else "vcc_lo"
emit_flag("v15", bit, dst, f"v_cmp_class_f16_sdwa {dst}, {source}, v21 src0_sel:{sel} src1_sel:DWORD")
return body


def sdwa_f16_operand(token, words, sel, *, selections=True, modifiers=True):
"""The binary16 bits an SDWA source names: its register half, then |x| and -."""
neg = token.startswith("-")
name = token.lstrip("-")
use_abs = name.startswith("|")
word = words[{"v18": 0, "v16": 1, "v20": 2}[name.strip("|")]]
bits = (word >> 16) & 0xffff if selections and sel == "WORD_1" else word & 0xffff
if modifiers and use_abs:
bits &= 0x7fff
if modifiers and neg:
bits ^= 0x8000
return bits


def sdwa_f16_reference(words, **model):
"""The four words sdwa_f16_compares() exports. The keyword arguments describe wrong models."""
def half(bits):
return struct.unpack("<e", struct.pack("<H", bits))[0]

def half_class(h):
mag, neg = h & 0x7fff, h >= 0x8000
if mag > 0x7c00:
return 0x2 if h & 0x0200 else 0x1
if mag == 0x7c00:
return 0x4 if neg else 0x200
if mag >= 0x0400:
return 0x8 if neg else 0x100
if mag:
return 0x10 if neg else 0x80
return 0x20 if neg else 0x40

def holds(pred, a, b):
ordered = not math.isnan(a) and not math.isnan(b)
return {"f": False, "lt": ordered and a < b, "eq": ordered and a == b, "le": ordered and a <= b,
"gt": ordered and a > b, "lg": ordered and a != b, "ge": ordered and a >= b, "o": ordered,
"u": not ordered, "nge": not (ordered and a >= b), "nlg": not (ordered and a != b),
"ngt": not (ordered and a > b), "nle": not (ordered and a <= b), "neq": not (ordered and a == b),
"nlt": not (ordered and a < b), "tru": True}[pred]

out = []
for _, src0, src1, sel0, sel1 in SDWA_F16_COMPARES:
a = half(sdwa_f16_operand(src0, words, sel0, **model))
b = half(sdwa_f16_operand(src1, words, sel1, **model))
out.append(sum(1 << bit for bit, pred in enumerate(PREDICATES) if holds(pred, a, b)))
out.append(sum(1 << bit for bit, (source, sel) in enumerate(SDWA_F16_CLASSES)
if half_class(sdwa_f16_operand(source, words, sel, **model)) & words[3]))
return tuple(out)


SDWA_F16_WRONG_MODELS = (("selections ignored", {"selections": False}), ("modifiers ignored", {"modifiers": False}))


def check_sdwa_f16_compare_draws(directory, compiler, renderer, backend):
"""Draw the 16 binary16 SDWA predicates and the SDWA class test on SDWA_F16_SETS, and for each wrong model the
first set it changes against that model's words, which must report a mismatch. On a desktop GPU the draws
establish that the translated shader computes the stated comparisons, not what the console does."""
source = directory / "sdwa-f16-compare.hlsl"
shader = directory / "sdwa-f16-compare.cso"
source.write_text(translate_int(uncompressed_export(sdwa_f16_compares())), encoding="ascii")
compile_shader(compiler, source, shader)
cases = []
for index, words in enumerate(SDWA_F16_SETS):
constants = bytearray(256)
struct.pack_into("<4I", constants, 12, *words)
expected = tuple(as_float(w) for w in sdwa_f16_reference(words))
cases.append((write_case(directory / f"sdwa-f16-cmp-{index}.case", constants, expected, integer_rt=True),
"pass"))
for label, model in SDWA_F16_WRONG_MODELS:
words = next(w for w in SDWA_F16_SETS if sdwa_f16_reference(w, **model) != sdwa_f16_reference(w))
constants = bytearray(256)
struct.pack_into("<4I", constants, 12, *words)
wrong = tuple(as_float(w) for w in sdwa_f16_reference(words, **model))
cases.append((write_case(directory / f"sdwa-f16-cmp-{label.replace(' ', '-')}.case", constants, wrong,
integer_rt=True), "mismatch"))
run_pixel_batch(renderer, backend, shader, cases)
return 0


def check_sdwa_f16_compare_lowering():
"""Static checks for the binary16 SDWA comparisons: the 17 forms, the inputs and the refusals."""
forms = {line.split()[0] for line in sdwa_f16_compares() if "_sdwa" in line}
assert forms == {f"v_cmp_{pred}_f16_sdwa" for pred in PREDICATES} | {"v_cmp_class_f16_sdwa"}, forms
translate_int(uncompressed_export(sdwa_f16_compares()))
# Every predicate bit other than f and tru, and every class bit, takes both values over the sets, and every
# wrong model changes at least one set.
results = [sdwa_f16_reference(words) for words in SDWA_F16_SETS]
for word in range(3):
for bit, pred in enumerate(PREDICATES):
seen = {(result[word] >> bit) & 1 for result in results}
assert seen == ({0} if pred == "f" else {1} if pred == "tru" else {0, 1}), (word, pred, seen)
for bit in range(len(SDWA_F16_CLASSES)):
assert {(result[3] >> bit) & 1 for result in results} == {0, 1}, bit
for label, model in SDWA_F16_WRONG_MODELS:
assert any(sdwa_f16_reference(w, **model) != sdwa_f16_reference(w) for w in SDWA_F16_SETS), label
# With DWORD selections and no modifier the SDWA comparison is the e32 one.
setup = ["v_mov_b32_e32 v16, s45", "v_mov_b32_e32 v18, s44"]
tail = ["v_cndmask_b32_e64 v12, 0, 1, vcc_lo"]
plain = translate_int(uncompressed_export([*setup, "v_cmp_lt_f16_e32 vcc_lo, v18, v16", *tail]))
sdwa = translate_int(uncompressed_export(
[*setup, "v_cmp_lt_f16_sdwa vcc_lo, v18, v16 src0_sel:DWORD src1_sel:DWORD", *tail]))
assert plain == sdwa
# WORD_1 reads the high half, for the value and for the ordered test, and only for the source it names. v18 (A)
# is alu21 and v16 (B) is alu20. These text checks guard each selection without a GPU; the draws check the values.
def comparison(instruction):
lines = translate_int(uncompressed_export([*setup, instruction, *tail])).splitlines()
found = [line for line in lines if line.startswith(" bool cmp")]
assert len(found) == 1, found
return found[0]

high_a, low_a = "(asuint(alu21) >> 16u)", "asuint(alu21)"
high_b, low_b = "(asuint(alu20) >> 16u)", "asuint(alu20)"
line = comparison("v_cmp_lt_f16_sdwa vcc_lo, v18, v16 src0_sel:WORD_1 src1_sel:WORD_0")
assert f"num16(({high_a} & 0xffffu))" in line and f"widen16({high_a})" in line, line
assert f"num16(({low_b} & 0xffffu))" in line and f"widen16({low_b})" in line and high_b not in line, line
line = comparison("v_cmp_lt_f16_sdwa vcc_lo, v18, v16 src0_sel:WORD_0 src1_sel:WORD_1")
assert f"num16(({low_a} & 0xffffu))" in line and f"widen16({low_a})" in line and high_a not in line, line
assert f"num16(({high_b} & 0xffffu))" in line and f"widen16({high_b})" in line, line
# The class test reads the selected half of its value, with the modifier applied after the selection; the mask
# register is read whole.
line = comparison("v_cmp_class_f16_sdwa vcc_lo, -v18, v16 src0_sel:WORD_1 src1_sel:DWORD")
assert line == f" bool cmp22 = (class16((({high_a} & 0xffffu) ^ 0x8000u)) & {low_b}) != 0u;", line
line = comparison("v_cmp_class_f16_sdwa vcc_lo, v18, v16 src0_sel:WORD_0 src1_sel:DWORD")
assert line == f" bool cmp22 = (class16(({low_a} & 0xffffu)) & {low_b}) != 0u;", line
good = "v_cmp_lt_f16_sdwa vcc_lo, v18, v16 src0_sel:WORD_1 src1_sel:WORD_0"
for bad in (
"v_cmp_lt_f16_sdwa vcc_lo, v18, v16 src0_sel:BYTE_0 src1_sel:WORD_0",
"v_cmp_lt_f16_sdwa vcc_lo, sext(v18), v16 src0_sel:WORD_1 src1_sel:WORD_0",
"v_cmp_lt_f16_sdwa vcc_lo, 1.0, v16 src0_sel:WORD_1 src1_sel:WORD_0",
"v_cmp_lt_f16_sdwa vcc_lo, 0x3c00, v16 src0_sel:WORD_1 src1_sel:WORD_0",
"v_cmp_lt_f16_sdwa vcc_lo, v18, v16 src0_sel:WORD_1",
"v_cmp_lt_f16_sdwa exec_lo, v18, v16 src0_sel:WORD_1 src1_sel:WORD_0",
"v_cmp_lt_f16_sdwa vcc_lo, v18, v106 src0_sel:WORD_1 src1_sel:WORD_0",
"v_cmp_class_f16_sdwa vcc_lo, v18, v16 src0_sel:WORD_1 src1_sel:WORD_1",
"v_cmp_class_f16_sdwa vcc_lo, v18, -v16 src0_sel:WORD_1 src1_sel:DWORD",
"v_cmpx_lt_f16_sdwa v18, v16 src0_sel:WORD_1 src1_sel:WORD_0",
):
translate_int(uncompressed_export([*setup, good, *tail]))
must_reject(translate_int, uncompressed_export([*setup, bad, *tail]))


def check_f16_compare_draws(directory, compiler, renderer, backend):
source = directory / "f16-compare.hlsl"
shader = directory / "f16-compare.cso"
Expand Down Expand Up @@ -22139,7 +22327,7 @@ def main():
"draw-e64-integer", "draw-bfm", "draw-brev", "draw-align",
"draw-sdwa-integer", "draw-plain-bitscan", "draw-vop3-ternary-int", "draw-swap",
"draw-mac-legacy", "draw-carry-out", "draw-carry-in", "draw-mad64", "draw-cvt-f64-int", "draw-cvt-f64-f32", "draw-cvt-int-f64", "draw-pack-int16", "draw-pack-b32",
"draw-sad",
"draw-sad", "draw-sdwa-f16-compare",
"draw-int16-vop3", "draw-int16-ternary", "draw-compressed-export", "draw-uncompressed-export",
"draw-uncompressed-integer-export",
"draw-title-loads", "draw-ds-swizzle", "draw-exec-regions", "draw-cmpx-regions", "draw-cmpx-e64", "draw-scc-skip",
Expand Down Expand Up @@ -22172,6 +22360,7 @@ def main():
"draw-pack-int16": check_pack_int16_draws,
"draw-pack-b32": check_pack_b32_draw,
"draw-sad": check_sad_draws,
"draw-sdwa-f16-compare": check_sdwa_f16_compare_draws,
"draw-int16-vop3": check_int16_vop3_draw,
"draw-int16-ternary": check_int16_ternary_draw,
"draw-f16-arith": check_f16_arith_draw,
Expand Down
Loading
Loading