From fd22219e451188c526aa6775a0e2585fa3905328 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 28 Nov 2026 21:38:05 +0000 Subject: [PATCH 1/5] shader: lower pixel compare masks composed with VCC A whole-quad pixel tail can AND, OR, or ANDN2 a two-SGPR compare with VCC, keep that pair across a 64-bit move, and refuse a half overwrite or an SCC read whose wave-wide result is not uniform. Co-authored-by: SimSalabimse --- CMakeLists.txt | 3 + docs/experiments.md | 2 + docs/experiments/pixel-compare-mask-vcc.md | 27 ++ src/gfx1013_direct_graphics.inl | 115 ++++++++- tests/cmake/native_pixel_compare_mask.cmake | 19 ++ tests/native_direct_container_check.py | 3 + tests/native_pixel_compare_mask_check.py | 258 ++++++++++++++++++++ tests/startup_indirect_draw_indexed.cpp | 17 +- 8 files changed, 439 insertions(+), 5 deletions(-) create mode 100644 docs/experiments/pixel-compare-mask-vcc.md create mode 100644 tests/cmake/native_pixel_compare_mask.cmake create mode 100644 tests/native_pixel_compare_mask_check.py diff --git a/CMakeLists.txt b/CMakeLists.txt index e3425e05f6..469fddcf7c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1970,6 +1970,9 @@ if(TARGET compile_dxil AND EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${ add_test(NAME native_fixed_mesh57_mask_${backend} COMMAND startup_indirect_draw_indexed ${backend} mesh) set_tests_properties(native_fixed_mesh57_mask_${backend} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77 ENVIRONMENT "X2W_TEST_NATIVE_MESH57=1;X2W_TEST_NATIVE_MESH57_MRT=1;X2W_TEST_NATIVE_MESH57_MASK=1;X2W_NATIVE_SHADERS=${DXC_BIN_DIR}") + add_test(NAME native_fixed_mesh57_compare_vcc_${backend} COMMAND startup_indirect_draw_indexed ${backend} mesh) + set_tests_properties(native_fixed_mesh57_compare_vcc_${backend} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77 + ENVIRONMENT "X2W_TEST_NATIVE_MESH57=1;X2W_TEST_NATIVE_MESH57_COMPARE_VCC=1;X2W_NATIVE_SHADERS=${DXC_BIN_DIR}") endforeach() add_executable(native_store_replay tests/native_store_replay.cpp tests/desktop_gpu.cpp) target_include_directories(native_store_replay PRIVATE src tests) diff --git a/docs/experiments.md b/docs/experiments.md index e7fa723759..17d6f5451e 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -17986,6 +17986,8 @@ design-B keep cases (a live slot stays reboundable after final `Release()` and c See [fixed mesh pipelines through slot 57](experiments/native-fixed-mesh57.md) for the source-built indirect-draw fixtures. +A pixel tail can AND, OR, or ANDN2 a two-SGPR compare mask with VCC after whole-quad entry. The source words and the readback are in [pixel compare masks with VCC](experiments/pixel-compare-mask-vcc.md). + ## Binary16 normalized integer conversions (`v_cvt_norm_i16_f16`, `v_cvt_norm_u16_f16`) across VOP1/VOP3/DPP16/DPP8/SDWA in pixel_aot [The pixel shader translator](../tools/pixel_aot.py) adds 10 exact binary16 normalized integer conversion forms across all 5 encodings (counted in both `docs/coverage/gfx1013.tsv` and `docs/coverage/rdna2.tsv`): diff --git a/docs/experiments/pixel-compare-mask-vcc.md b/docs/experiments/pixel-compare-mask-vcc.md new file mode 100644 index 0000000000..6929b389b2 --- /dev/null +++ b/docs/experiments/pixel-compare-mask-vcc.md @@ -0,0 +1,27 @@ +# Pixel compare masks composed with VCC + +A gfx1013 pixel program can save its entry coverage, enter whole-quad mode, and +later combine a named two-SGPR compare with VCC. `s_and_b64`, `s_or_b64` and +`s_andn2_b64` (`S0 & ~S1`) each produce a new pair. `s_mov_b64` keeps that +pair's lane bit. A later `s_mov_b32` of either half refuses with +`compare mask half overwritten`. A later SCC read is lowered only when both +operands were uniform, so the bit is the same on every lane. A varying combine +refuses with `SCC read without an established wave-wide nonzero result`. + +The fixture is source words in `tests/native_pixel_compare_mask_check.py`. +The named mask is `y > 1` while the quad is still whole. VCC is `x > 2` after +exact coverage returns, so a helper lane keeps a mask bit and contributes +nothing to VCC. A diagonal draw leaves pixel `(4, 0)` covered and `(4, 1)` a +helper. The vertical quad read of the mask is 1 at `(4, 0)`. Pixels the +triangle does not cover stay at the clear value, -1. + +The fixed-mesh check is separate. It links the reviewed mesh with this kind of +tail, then creates a color pipeline from the captured root and attachment +state. Its readback is the slot 57 target, not the 8x2 fixture above. + +```powershell +ctest --preset dev --no-tests=error -R "native_pixel_compare_mask|native_fixed_mesh57_compare_vcc" -V +``` + +WARP and hardware are separate tests. A hardware result of 77 is a capability +skip. The container and IR tests do not draw. diff --git a/src/gfx1013_direct_graphics.inl b/src/gfx1013_direct_graphics.inl index 2bf090b7b7..32ec2537c0 100644 --- a/src/gfx1013_direct_graphics.inl +++ b/src/gfx1013_direct_graphics.inl @@ -1661,6 +1661,15 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv = return alu.ir.value("xor i1 " + helper + ", true"); }; GraphicsFullWave full_wave{alu, stage.wave32 ? 32u : 64u}; + // A two-SGPR compare and the combines made from it. The bit is the lane's + // view of that pair. A 32-bit write to either half drops the pair. + struct CompareMaskBit { + string bit; + bool uniform = false; + }; + std::map compare_masks; + std::set broken_compare_masks; + bool compare_scc_unknown = false; auto instruction = [&](const Inst &i, const State &state) { const auto pc = alu.graphics_pc = graphics.pc = i.pc; if (waterfalls.restores.count(pc)) return; @@ -1981,7 +1990,14 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv = if (i.dstfirst+(stage.wave32 ? 1u : 2u) && it->first= 6 && i.name.substr(0, 6) == "v_cmp_" && + i.name.find("class") == std::string_view::npos) || + i.name == "v_cndmask_b32"); + if (exact_after_wqm && !exact_mask_select && i.enc != Encoding::Smem && i.enc != Encoding::Sopp && i.enc != Encoding::Sop1 && i.enc != Encoding::Sop2 && i.enc != Encoding::Sopk && i.enc != Encoding::Export && !(i.name == "v_mov_b32" && i.dst >= 256 && !i.dpp_control && !i.subword) && @@ -2002,6 +2018,8 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv = i.name != "v_nop" && i.name != "s_nop" && i.name != "s_clause" && i.name != "s_setprio" && i.name != "s_inst_prefetch" && i.name != "s_waitcnt") scalar_entry = false; + if (compare_scc_unknown && (i.name == "s_cselect_b32" || i.name == "s_cselect_b64")) + refuse(pc, "SCC read without an established wave-wide nonzero result"); if (i.dpp_control) { if (i.mirror_mask()) refuse(pc, i.mirror_mask() == 7 ? "DPP half-row mirror requires a proven eight-lane pixel layout" @@ -2100,6 +2118,10 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv = alu.write(i.dst,{}); if (!stage.wave32) alu.write(i.dst+1,{}); alu.write(1024+pc,bit); + if (i.dst <= 104 && !(i.dst & 1)) { + compare_masks[i.dst] = {bit, alu.ir.dependencies[bit].none()}; + broken_compare_masks.erase(i.dst); + } } if (i.dst == 106 && alu.graphics_predicate.empty() && scalar_operand(i.src[0]) && scalar_operand(i.src[1]) && alu.ir.dependencies[bit].none()) uniform_vcc_values.insert(bit); @@ -2112,7 +2134,13 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv = a = alu.subword(a, i.src_select[0], i.src_sext[0]); b = alu.subword(b, i.src_select[1], i.src_sext[1]); } - auto mask = alu.graphics_lane_mask(i,state,i.src[2],"unproven graphics CNDMASK source"); + string mask; + if (i.src[2] <= 104 && broken_compare_masks.count(i.src[2])) + refuse(pc, "compare mask half overwritten"); + else if (auto found = compare_masks.find(i.src[2]); found != compare_masks.end()) + mask = found->second.bit; + else + mask = alu.graphics_lane_mask(i,state,i.src[2],"unproven graphics CNDMASK source"); alu.vector_write(i.dst, alu.place_subword(i, alu.select(alu.cmp("ne",mask,"0"), b, a))); return; } @@ -2127,6 +2155,87 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv = alu.write(i.dst,value); return; } + auto compare_mask_bit = [&](unsigned src) -> std::optional { + if (src > 104 || (src & 1)) + return std::nullopt; + if (broken_compare_masks.count(src)) + refuse(pc, "compare mask half overwritten"); + if (auto found = compare_masks.find(src); found != compare_masks.end()) + return found->second; + const auto &low = state[src]; + const bool tagged = low.type == Symbol::Type::lane_mask || low.type == Symbol::Type::uniform_mask; + if (tagged && !low.offset && (stage.wave32 || state[src + 1] == Symbol{low.type, low.value, 1})) { + auto bit = alu.read(1024 + low.value); + CompareMaskBit saved{bit, alu.ir.dependencies[bit].none()}; + compare_masks[src] = saved; + return saved; + } + return std::nullopt; + }; + auto vcc_bit = [&]() -> std::optional { + auto found = alu.graphics_registers.find(106); + if (found == alu.graphics_registers.end() || found->second.empty()) + return std::nullopt; + return CompareMaskBit{found->second, alu.ir.dependencies[found->second].none()}; + }; + if (pixel && i.name == "s_mov_b64" && i.dst <= 104 && !(i.dst & 1) && i.src[0] <= 104) { + const bool known = broken_compare_masks.count(i.src[0]) || compare_masks.count(i.src[0]) || + state[i.src[0]].type == Symbol::Type::lane_mask || + state[i.src[0]].type == Symbol::Type::uniform_mask; + if (known) { + if (broken_compare_masks.count(i.src[0])) { + broken_compare_masks.insert(i.dst); + compare_masks.erase(i.dst); + } else if (auto bit = compare_mask_bit(i.src[0])) { + compare_masks[i.dst] = *bit; + broken_compare_masks.erase(i.dst); + } + alu.write(i.dst, {}); + alu.write(i.dst + 1, {}); + return; + } + } + if (pixel && i.name == "s_mov_b32" && i.dst <= 105) { + const unsigned base = i.dst & ~1u; + if (compare_masks.count(base) || broken_compare_masks.count(base) || + state[base].type == Symbol::Type::lane_mask || state[base].type == Symbol::Type::uniform_mask) + broken_compare_masks.insert(base); + } + if (pixel && (whole_quad || exact_after_wqm) && + (i.name == "s_and_b64" || i.name == "s_or_b64" || i.name == "s_andn2_b64") && + i.dst <= 104 && !(i.dst & 1) && i.src[0] != 126 && i.src[1] != 126) { + auto resolve = [&](unsigned src) -> std::optional { + return src == 106 ? vcc_bit() : compare_mask_bit(src); + }; + auto left = resolve(i.src[0]); + auto right = resolve(i.src[1]); + if (left && right) { + // s_andn2_b64 is S0 & ~S1. The operands are not interchangeable. + string bit = i.name == "s_or_b64" ? alu.ir.binary("or", left->bit, right->bit) : + i.name == "s_and_b64" ? alu.ir.binary("and", left->bit, right->bit) : + alu.ir.binary("and", left->bit, alu.ir.binary("xor", right->bit, "1")); + const bool uniform = left->uniform && right->uniform && alu.ir.dependencies[bit].none(); + compare_masks[i.dst] = {bit, uniform}; + broken_compare_masks.erase(i.dst); + alu.write(i.dst, {}); + alu.write(i.dst + 1, {}); + alu.write(1024 + pc, bit); + if (uniform) { + alu.write(128, bit); + compare_scc_unknown = false; + } else { + alu.write(128, {}); + compare_scc_unknown = true; + } + return; + } + auto tracked = [&](unsigned src) { + return src == 106 || (src <= 104 && !(src & 1) && + (compare_masks.count(src) || broken_compare_masks.count(src))); + }; + if (tracked(i.src[0]) && tracked(i.src[1])) + refuse(pc, "compare mask is not a 64-bit lane mask"); + } if (whole_quad && !exact_after_wqm && alu.graphics_predicate.empty() && i.name == "s_and_b64" && i.dst <= 104 && !(i.dst & 1) && ((i.src[0] == 106 && i.src[1] == 126) || (i.src[1] == 106 && i.src[0] == 126))) { @@ -2306,6 +2415,8 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv = alu.ir.body << " br label %graphicsEnd\n"; } else if (block.successors.size() == 2) { const auto branch = regions.lane_branches.find(last.pc); + if (branch == regions.lane_branches.end() && last.op < 6 && compare_scc_unknown) + refuse(last.pc, "SCC read without an established wave-wide nonzero result"); auto condition = branch != regions.lane_branches.end() ? alu.cmp(branch->second.second ? "ne" : "eq", alu.read(branch->second.first), "0") : alu.cmp((last.op & 1) ? "ne" : "eq", alu.read(last.op >= 6 ? 106 : 128), "0"); diff --git a/tests/cmake/native_pixel_compare_mask.cmake b/tests/cmake/native_pixel_compare_mask.cmake new file mode 100644 index 0000000000..b07b56c81d --- /dev/null +++ b/tests/cmake/native_pixel_compare_mask.cmake @@ -0,0 +1,19 @@ +# Pixel compare mask composed with VCC after whole-quad entry. +# native_pixel_compare_mask_ir checks the DXIL text. The container entry validates it through DXC. +# The draw entries read back the four combine results on WARP and on a physical GPU. +if(TARGET compile_dxil AND TARGET offscreen_check AND EXISTS "${INTAKE_PYTHON}") + foreach(backend IN ITEMS warp hardware) + add_test(NAME native_pixel_compare_mask_${backend} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/native_pixel_compare_mask_check.py" + "$" "$" "${DXC_BIN_DIR}" ${backend}) + set_tests_properties(native_pixel_compare_mask_${backend} PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60) + endforeach() + add_test(NAME native_pixel_compare_mask_container COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/native_pixel_compare_mask_check.py" container + "$" "${DXC_BIN_DIR}") +endif() +if(TARGET native_pixel_ir_dump AND EXISTS "${INTAKE_PYTHON}") + add_test(NAME native_pixel_compare_mask_ir COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/native_pixel_compare_mask_check.py" ir + "$") +endif() diff --git a/tests/native_direct_container_check.py b/tests/native_direct_container_check.py index 5cb817ed10..7265b69c3b 100644 --- a/tests/native_direct_container_check.py +++ b/tests/native_direct_container_check.py @@ -1361,6 +1361,9 @@ def install(raw, container): assert f"frontend_cache_hit={warm}" in result.stdout check("patch-range", "gfx1013-container-ps", patched_pixel(3), "record outside") check("pixel", "gfx1013-direct-constant-ps", words(0x7e0002f2, 0xf800180f, 0, end)) + from native_pixel_compare_mask_check import container_cases + for label, blob, reason in container_cases(): + check("pixel-" + label, "gfx1013-container-ps", blob, reason) if __name__ == "__main__": diff --git a/tests/native_pixel_compare_mask_check.py b/tests/native_pixel_compare_mask_check.py new file mode 100644 index 0000000000..8059ad70da --- /dev/null +++ b/tests/native_pixel_compare_mask_check.py @@ -0,0 +1,258 @@ +"""Lower a pixel compare mask composed with VCC in a whole-quad tail. + +The program is hand-built from gfx1013 source words. A diagonal draw covers +x + y < 5.5, so the quad at x = 4..5 has one covered lane, (4, 0), and three +helper lanes. The named mask is y > 1, taken under whole-quad execution, and +VCC is x > 2 after exact coverage is restored. AND, OR and both ANDN2 orders +write different covered pixels. The vertical quad read of the mask is 1 at +(4, 0) only because the helper at (4, 1) contributed a bit. +""" +import struct +import subprocess +import sys +from pathlib import Path +from tempfile import TemporaryDirectory + +from native_direct_container_check import patched_pixel, signature +from native_scalar_lds_check import sop1, vmove + + +HALF = "compare mask half overwritten" +SCC = "SCC read without an established wave-wide nonzero result" +CLEAR = -1.0 + + +def cmp_gt(dst, src0, src1): + return [0xD4000000 | (4 << 16) | dst, src0 | (src1 << 9)] + + +def cndmask(dst, false, true, mask): + return [0xD5010000 | dst, false | (true << 9) | (mask << 18)] + + +def scalar(op, dst, src0, src1): + return 0x80000000 | (op << 23) | (dst << 16) | (src1 << 8) | src0 + + +def container(code): + return patched_pixel(len(code), code, input_address=3 << 8, + input_signature=((b"ISG1", signature(b"SV_Position", 1)), + (b"OSG1", signature(b"SV_Target", 64))), + export=(0xF800180F, 0x03020100)) + + +def tail(op, src0, src1, *, after=()): + # s[4:5] is y > 1 under WQM. v5 is that bit read from the other row. + # VCC is x > 2 after the saved coverage returns. s[8:9] is the copy. + # v2 and v3 are not pixel-entry registers. Define them before exact + # coverage returns so the later select has an incoming value. + code = [sop1(4, 20, 126), sop1(10, 126, 126), *cmp_gt(4, 257, 242), + *cndmask(4, 128, 242, 4), vmove(5, 250), 0xFF044E04, + vmove(2, 128), vmove(3, 128), + sop1(4, 126, 20), *cmp_gt(106, 256, 244), + scalar(op, 6, src0, src1), sop1(4, 8, 6), *after, + *cndmask(0, 128, 242, 8), vmove(1, 261), *cndmask(2, 128, 242, 106), vmove(3, 242)] + return container(code) + + +def scc_program(*, varying): + # Both compares run before coverage returns, so a constant result stays + # uniform. A position compare does not: its bit differs across the wave. + # The OR is after the restore. Only the uniform OR may be read back through SCC. + code = [sop1(4, 20, 126), sop1(10, 126, 126)] + code += cmp_gt(4, 257, 242) if varying else cmp_gt(4, 242, 128) + code += cmp_gt(106, 256, 244) if varying else cmp_gt(106, 128, 242) + code += [vmove(0, 128), vmove(1, 128), vmove(2, 128), vmove(3, 242), + sop1(4, 126, 20), scalar(17, 6, 4, 106), scalar(10, 0, 242, 128), vmove(0, 0)] + return container(code) + + +def half_overwrite(): + return tail(15, 4, 106, after=[sop1(3, 9, 128)]) + + +CASES = { + "and": (15, 4, 106), + "and-swap": (15, 106, 4), + "or": (17, 4, 106), + "or-swap": (17, 106, 4), + "andn2-mask-vcc": (21, 4, 106), + "andn2-vcc-mask": (21, 106, 4), +} + + +def covered(x, y): + return (x + 0.5) + (y + 0.5) < 5.5 + + +def mask_bit(x, y): + return (y + 0.5) > 1.0 + + +def vcc_bit(x, y): + return (x + 0.5) > 2.0 + + +def combine(name, mask, vcc): + if name.startswith("andn2"): + left, right = (vcc, mask) if name.endswith("vcc-mask") else (mask, vcc) + return left and not right + if name.startswith("and"): + return mask and vcc + return mask or vcc + + +def image(name): + pixels = [] + for y in range(2): + for x in range(8): + if not covered(x, y): + pixels.append((CLEAR, CLEAR, CLEAR, CLEAR)) + continue + bit = combine(name, mask_bit(x, y), vcc_bit(x, y)) + helper = mask_bit(x, y ^ 1) + pixels.append((float(bit), float(helper), float(vcc_bit(x, y)), 1.0)) + return pixels + + +def scc_image(): + return [(1.0, 0.0, 0.0, 1.0) if covered(x, y) else (CLEAR,) * 4 + for y in range(2) for x in range(8)] + + +def assert_references_differ(): + pictures = {name: tuple(image(name)) for name in CASES} + names = list(pictures) + for i, left in enumerate(names): + for right in names[i + 1:]: + if left.split("-")[0] == right.split("-")[0] and not left.startswith("andn2"): + assert pictures[left] == pictures[right], (left, right) + else: + assert pictures[left] != pictures[right], (left, right) + own = mask_bit(4, 0) + assert own is False and image("and")[4][1] == 1.0 + assert image("and")[4][0] == 0.0 and image("or")[4][0] == 1.0 + assert image("andn2-mask-vcc")[4] != image("andn2-vcc-mask")[4] + assert all(pixel == (CLEAR,) * 4 for pixel in image("or")[5:8]) + + +def run(cmd): + result = subprocess.run(cmd, capture_output=True, text=True) + if result.returncode == 77: + sys.exit(77) + return result + + +def draw(compile_guest, raster, backend, vs, blob, pixels, work): + compiled = compile_guest(blob) + assert compiled.returncode == 0, compiled.stderr + expected = work / "expected.bin" + expected.write_bytes(b"".join(struct.pack("<4f", *pixel) for pixel in pixels)) + result = run([raster, backend, "--startup-pixel-words", vs, work / "ps.cso", "8", "2", expected]) + return result + + +def compile_main(cli, sdk): + with TemporaryDirectory() as tmp: + work = Path(tmp) + def compile_guest(blob): + source = work / "guest" + source.write_bytes(blob) + return subprocess.run([cli, sdk, work / "cache", "gfx1013-container-ps", source, work / "ps.cso"], + capture_output=True, text=True) + kept = work / "and.cso" + for name, (op, src0, src1) in CASES.items(): + result = compile_guest(tail(op, src0, src1)) + assert result.returncode == 0, (name, result.stderr) + if name == "and": + kept.write_bytes((work / "ps.cso").read_bytes()) + print(f"validated {name}") + uniform = compile_guest(scc_program(varying=False)) + assert uniform.returncode == 0, uniform.stderr + print("validated uniform SCC select") + for label, blob, reason in ( + ("half overwrite", half_overwrite(), HALF), + ("varying SCC", scc_program(varying=True), SCC)): + refused = compile_guest(blob) + assert refused.returncode and reason in refused.stderr, (label, refused.stderr) + print(f"refused {label}") + dump = subprocess.run([Path(sdk) / "dxc.exe", "-dumpbin", kept], + capture_output=True, text=True) + assert dump.returncode == 0 and "@dx.op.isHelperLane.i1" in dump.stdout, dump.stdout + print("direct container: DXIL validated, helper-lane coverage is in the module") + + +def ir_main(dump): + with TemporaryDirectory() as tmp: + work = Path(tmp) + def lower(blob): + path = work / "guest" + path.write_bytes(blob) + return subprocess.run([dump, path], capture_output=True, text=True) + and_ir = lower(tail(15, 4, 106)) + assert and_ir.returncode == 0, and_ir.stderr + text = and_ir.stdout + assert "dx.op.quadOp.i32" in text and "i8 1" in text, text + assert "dx.op.isHelperLane" in text and " and i32 " in text, text + or_ir = lower(tail(17, 4, 106)) + assert or_ir.returncode == 0 and " or i32 " in or_ir.stdout, or_ir.stderr + nand = lower(tail(21, 106, 4)) + assert nand.returncode == 0 and " xor i32 " in nand.stdout, nand.stderr + for label, blob, reason in ( + ("half overwrite", half_overwrite(), HALF), + ("varying SCC", scc_program(varying=True), SCC)): + refused = lower(blob) + assert refused.returncode and reason in refused.stderr, (label, refused.stderr) + print(f"ir refused {label}") + uniform = lower(scc_program(varying=False)) + assert uniform.returncode == 0, uniform.stderr + print("ir: and, or, andn2, quad read and exact coverage") + + +def draw_main(cli, raster, sdk, backend): + with TemporaryDirectory() as tmp: + work = Path(tmp) + vs = work / "vs.cso" + source = work / "vs.hlsl" + source.write_text( + "float4 main(uint i:SV_VertexID):SV_Position {" + "return float4(i==0?-0.25:(i==1?0.5:-5),i==0?-1.5:(i==1?1.5:0),0,1);}") + subprocess.run([Path(sdk) / "dxc.exe", "-T", "vs_6_6", "-Fo", vs, source], check=True) + def compile_guest(blob): + guest = work / "guest" + guest.write_bytes(blob) + return subprocess.run([cli, sdk, work / "cache", "gfx1013-container-ps", guest, work / "ps.cso"], + capture_output=True, text=True) + for name in ("and", "or", "andn2-mask-vcc", "andn2-vcc-mask"): + op, src0, src1 = CASES[name] + result = draw(compile_guest, raster, backend, vs, tail(op, src0, src1), image(name), work) + assert result.returncode == 0, (name, result.stdout, result.stderr) + print(f"{backend} {name}: 16 pixels matched") + swapped = draw(compile_guest, raster, backend, vs, tail(*CASES["andn2-vcc-mask"]), + image("andn2-mask-vcc"), work) + assert swapped.returncode == 1 and "pixels differ" in swapped.stderr, swapped.stderr + print(f"{backend} andn2 operand order: pixels differ") + uniform = draw(compile_guest, raster, backend, vs, scc_program(varying=False), scc_image(), work) + assert uniform.returncode == 0, (uniform.stdout, uniform.stderr) + print(f"{backend} uniform SCC select: 16 pixels matched") + + +def container_cases(): + """Blobs for the direct-container regression. Empty reason means the compile must succeed.""" + cases = [(name, tail(op, src0, src1), "") for name, (op, src0, src1) in CASES.items()] + cases.append(("compare-scc-uniform", scc_program(varying=False), "")) + cases.append(("compare-half-overwrite", half_overwrite(), HALF)) + cases.append(("compare-scc-varying", scc_program(varying=True), SCC)) + return cases + + +if __name__ == "__main__": + assert_references_differ() + if len(sys.argv) == 1: + print("compare-mask references differ for and, or and both andn2 orders") + elif sys.argv[1] == "ir": + ir_main(sys.argv[2]) + elif sys.argv[1] == "container": + compile_main(sys.argv[2], sys.argv[3]) + else: + draw_main(*sys.argv[1:]) diff --git a/tests/startup_indirect_draw_indexed.cpp b/tests/startup_indirect_draw_indexed.cpp index 835026a24a..3c724e55e8 100644 --- a/tests/startup_indirect_draw_indexed.cpp +++ b/tests/startup_indirect_draw_indexed.cpp @@ -2062,7 +2062,8 @@ static int native_fixed_mesh_fixture(ID3D12Device* device, ID3D12Device* guest, const bool heap = pixel_heap || GetEnvironmentVariableA("X2W_TEST_NATIVE_MESH57_UINT_HEAP",nullptr,0) != 0; const bool buffers = heap || GetEnvironmentVariableA("X2W_TEST_NATIVE_MESH57_BUFFERS", nullptr, 0) != 0; const bool constants = buffers || GetEnvironmentVariableA("X2W_TEST_NATIVE_MESH57_CONSTANT", nullptr, 0) != 0; - const bool varying = constants || GetEnvironmentVariableA("X2W_TEST_NATIVE_MESH57_VARYING", nullptr, 0) != 0; + const bool compare_vcc = GetEnvironmentVariableA("X2W_TEST_NATIVE_MESH57_COMPARE_VCC", nullptr, 0) != 0; + const bool varying = compare_vcc || constants || GetEnvironmentVariableA("X2W_TEST_NATIVE_MESH57_VARYING", nullptr, 0) != 0; // Windows recycles process IDs. Retain failed runs for inspection without reading an old archive. const auto prefix = "mesh57-native-" + std::to_string(GetCurrentProcessId()) + "-"; std::filesystem::path cache; @@ -2114,6 +2115,13 @@ static int native_fixed_mesh_fixture(ID3D12Device* device, ID3D12Device* guest, ms.insert(ms.end()-1, {constants ? 0x7e0c020cu : 0x7e0c0280u, 0xf80002c3, heap ? 0x0000060au : 0x00000604u}); ps = {0xc8023002, 0xc8063102, 0xf800180f, 0x00000100, 0xbf810000}; } + if (compare_vcc) { + // Whole-quad tail: a uniform true compare mask AND VCC from SV_Position.x > 0, + // copied as a pair, then selected. The covered pixel is white with green left clear. + ps = {0xbe94047e, 0xbefe0a7e, 0xd4040004, 0x000100f2, 0x7e020280, 0x7e0402f2, 0x7e0602f2, + 0xbefe0414, 0xd404006a, 0x00010100, 0x87866a04, 0xbe880406, 0xd5010000, 0x0021e480, + 0xf800180f, 0x03020100, 0xbf810000}; + } if (buffers) { // s_load_dwordx4 table0+2848 and table1+32/+96; s_buffer_load_dword at +4 of each SRV. // Read root3+4 through its synthesized byte descriptor. Combine their disjoint bits with root4 in s12. @@ -2228,7 +2236,7 @@ static int native_fixed_mesh_fixture(ID3D12Device* device, ID3D12Device* guest, const auto put = [&](const void* data, size_t n) { out.write(static_cast(data), n); }; put(&profile, sizeof(profile)); if (varying) { - const UINT position_x = waterfall ? 1u<<8 : 0; + const UINT position_x = (compare_vcc || waterfall) ? 1u<<8 : 0; const x2w_startup::Mesh57PixelEntry entry{position_x,position_x,64}; put(&entry,sizeof(entry)); if (constants) { @@ -2271,6 +2279,8 @@ static int native_fixed_mesh_fixture(ID3D12Device* device, ID3D12Device* guest, return pso; }; const auto pso = create(); + if (compare_vcc) + std::printf("native fixed mesh compare mask: reviewed stage pair linked and created a color PSO\n"); fields[15] = 1; // Same stages and root, unreviewed fixed pipeline state. const auto mismatched = create(); { @@ -2348,8 +2358,9 @@ static int native_fixed_mesh_fixture(ID3D12Device* device, ID3D12Device* guest, ComPtr signature; check(guest->CreateCommandSignature(&desc, nullptr, IID_PPV_ARGS(&signature)), "mesh signature"); const auto work = writer_pipeline(device, writer, "write_one_mesh_group"); + const char *reviewed = compare_vcc ? "native fixed mesh compare mask" : "native fixed mesh triangle"; const bool ok = run(device, guest, writer, work.Get(), root.Get(), pso.Get(), signature.Get(), DrawKind::mesh, false, - Refusal::none, "native fixed mesh triangle", nullptr, false, nullptr, false, false, 0, false, false, + Refusal::none, reviewed, nullptr, false, nullptr, false, false, 0, false, false, nullptr, nullptr, false, 1, &profile) && run(device, guest, writer, work.Get(), root.Get(), mismatched.Get(), signature.Get(), DrawKind::mesh, false, Refusal::none, "unreviewed fixed mesh state", nullptr, false, nullptr, false, false, 0, false, From a081635b83c17715f64a74ffac3d2be141ea294d Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 28 Nov 2026 21:49:42 +0000 Subject: [PATCH 2/5] shader: keep compare-mask tracking in the pixel whole-quad tail A vertex class-mask high-half overwrite must still refuse as an unproven CNDMASK source. The pair tracker was recording every graphics compare. Co-authored-by: SimSalabimse --- docs/experiments/pixel-compare-mask-vcc.md | 4 +++- src/gfx1013_direct_graphics.inl | 20 +++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/docs/experiments/pixel-compare-mask-vcc.md b/docs/experiments/pixel-compare-mask-vcc.md index 6929b389b2..e4e4a19176 100644 --- a/docs/experiments/pixel-compare-mask-vcc.md +++ b/docs/experiments/pixel-compare-mask-vcc.md @@ -1,7 +1,9 @@ # Pixel compare masks composed with VCC A gfx1013 pixel program can save its entry coverage, enter whole-quad mode, and -later combine a named two-SGPR compare with VCC. `s_and_b64`, `s_or_b64` and +later combine a named two-SGPR compare with VCC. The mask tracker is that +whole-quad tail only. A vertex class-mask half overwrite still refuses as an +unproven CNDMASK source. `s_and_b64`, `s_or_b64` and `s_andn2_b64` (`S0 & ~S1`) each produce a new pair. `s_mov_b64` keeps that pair's lane bit. A later `s_mov_b32` of either half refuses with `compare mask half overwritten`. A later SCC read is lowered only when both diff --git a/src/gfx1013_direct_graphics.inl b/src/gfx1013_direct_graphics.inl index 32ec2537c0..a338b734b5 100644 --- a/src/gfx1013_direct_graphics.inl +++ b/src/gfx1013_direct_graphics.inl @@ -1670,6 +1670,11 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv = std::map compare_masks; std::set broken_compare_masks; bool compare_scc_unknown = false; + // Vertex class masks still use the symbolic pair. Recording them here would + // keep a CNDMASK source after a 32-bit half overwrite. + auto in_compare_tail = [&] { + return pixel && (whole_quad || exact_after_wqm); + }; auto instruction = [&](const Inst &i, const State &state) { const auto pc = alu.graphics_pc = graphics.pc = i.pc; if (waterfalls.restores.count(pc)) return; @@ -2118,7 +2123,7 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv = alu.write(i.dst,{}); if (!stage.wave32) alu.write(i.dst+1,{}); alu.write(1024+pc,bit); - if (i.dst <= 104 && !(i.dst & 1)) { + if (in_compare_tail() && i.dst <= 104 && !(i.dst & 1)) { compare_masks[i.dst] = {bit, alu.ir.dependencies[bit].none()}; broken_compare_masks.erase(i.dst); } @@ -2135,10 +2140,11 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv = b = alu.subword(b, i.src_select[1], i.src_sext[1]); } string mask; - if (i.src[2] <= 104 && broken_compare_masks.count(i.src[2])) + const auto tracked = in_compare_tail() ? compare_masks.find(i.src[2]) : compare_masks.end(); + if (in_compare_tail() && i.src[2] <= 104 && broken_compare_masks.count(i.src[2])) refuse(pc, "compare mask half overwritten"); - else if (auto found = compare_masks.find(i.src[2]); found != compare_masks.end()) - mask = found->second.bit; + else if (tracked != compare_masks.end()) + mask = tracked->second.bit; else mask = alu.graphics_lane_mask(i,state,i.src[2],"unproven graphics CNDMASK source"); alu.vector_write(i.dst, alu.place_subword(i, alu.select(alu.cmp("ne",mask,"0"), b, a))); @@ -2178,7 +2184,7 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv = return std::nullopt; return CompareMaskBit{found->second, alu.ir.dependencies[found->second].none()}; }; - if (pixel && i.name == "s_mov_b64" && i.dst <= 104 && !(i.dst & 1) && i.src[0] <= 104) { + if (in_compare_tail() && i.name == "s_mov_b64" && i.dst <= 104 && !(i.dst & 1) && i.src[0] <= 104) { const bool known = broken_compare_masks.count(i.src[0]) || compare_masks.count(i.src[0]) || state[i.src[0]].type == Symbol::Type::lane_mask || state[i.src[0]].type == Symbol::Type::uniform_mask; @@ -2195,13 +2201,13 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv = return; } } - if (pixel && i.name == "s_mov_b32" && i.dst <= 105) { + if (in_compare_tail() && i.name == "s_mov_b32" && i.dst <= 105) { const unsigned base = i.dst & ~1u; if (compare_masks.count(base) || broken_compare_masks.count(base) || state[base].type == Symbol::Type::lane_mask || state[base].type == Symbol::Type::uniform_mask) broken_compare_masks.insert(base); } - if (pixel && (whole_quad || exact_after_wqm) && + if (in_compare_tail() && (i.name == "s_and_b64" || i.name == "s_or_b64" || i.name == "s_andn2_b64") && i.dst <= 104 && !(i.dst & 1) && i.src[0] != 126 && i.src[1] != 126) { auto resolve = [&](unsigned src) -> std::optional { From 53a8f2ab5fe80af837958bd9caee4700e7e9ce8a Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sun, 29 Nov 2026 12:37:19 +0000 Subject: [PATCH 3/5] shader: drop compare-mask pairs a later scalar write replaces A literal or numeric s_mov_b64, s_movk_i32, scalar arithmetic, a scalar load, or a copy of a pair that is no longer a mask left the old compare bit for v_cndmask. s_cselect_b64 into that pair keeps the new full mask. Co-authored-by: SimSalabimse --- docs/experiments/pixel-compare-mask-vcc.md | 6 +- src/gfx1013_direct_graphics.inl | 38 +++++- tests/native_pixel_compare_mask_check.py | 128 +++++++++++++++++++-- 3 files changed, 160 insertions(+), 12 deletions(-) diff --git a/docs/experiments/pixel-compare-mask-vcc.md b/docs/experiments/pixel-compare-mask-vcc.md index e4e4a19176..85b122ae64 100644 --- a/docs/experiments/pixel-compare-mask-vcc.md +++ b/docs/experiments/pixel-compare-mask-vcc.md @@ -6,7 +6,11 @@ whole-quad tail only. A vertex class-mask half overwrite still refuses as an unproven CNDMASK source. `s_and_b64`, `s_or_b64` and `s_andn2_b64` (`S0 & ~S1`) each produce a new pair. `s_mov_b64` keeps that pair's lane bit. A later `s_mov_b32` of either half refuses with -`compare mask half overwritten`. A later SCC read is lowered only when both +`compare mask half overwritten`. Any other scalar write over that pair, +including a literal or numeric `s_mov_b64`, `s_movk_i32`, scalar arithmetic, +a scalar load, or a copy whose source is no longer a 64-bit mask, drops the +tracked bit. A later select then refuses with `unproven graphics CNDMASK source`. +`s_cselect_b64` into the same pair keeps the new full mask instead. A later SCC read is lowered only when both operands were uniform, so the bit is the same on every lane. A varying combine refuses with `SCC read without an established wave-wide nonzero result`. diff --git a/src/gfx1013_direct_graphics.inl b/src/gfx1013_direct_graphics.inl index a338b734b5..cdc437887a 100644 --- a/src/gfx1013_direct_graphics.inl +++ b/src/gfx1013_direct_graphics.inl @@ -1677,6 +1677,36 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv = }; auto instruction = [&](const Inst &i, const State &state) { const auto pc = alu.graphics_pc = graphics.pc = i.pc; + // A scalar destination the tracker does not itself rewrite drops any + // pair it overlaps. Otherwise v_cndmask keeps a compare bit the guest + // register no longer holds. + struct UnclaimedMaskRelease { + bool claimed = false; + const Inst* inst = nullptr; + bool pixel = false; + const bool* whole_quad = nullptr; + const bool* exact_after_wqm = nullptr; + std::map* masks = nullptr; + std::set* broken = nullptr; + ~UnclaimedMaskRelease() { + if (claimed || inst == nullptr || !pixel || (!*whole_quad && !*exact_after_wqm)) + return; + const unsigned width = GraphicsMaskRegions::scalar_width(*inst); + if (!width || inst->dst > 105) + return; + for (unsigned reg = inst->dst; reg < inst->dst + width && reg <= 105; ++reg) { + const unsigned base = reg & ~1u; + masks->erase(base); + broken->erase(base); + } + } + } mask_release; + mask_release.inst = &i; + mask_release.pixel = pixel; + mask_release.whole_quad = &whole_quad; + mask_release.exact_after_wqm = &exact_after_wqm; + mask_release.masks = &compare_masks; + mask_release.broken = &broken_compare_masks; if (waterfalls.restores.count(pc)) return; if (auto dead=waterfalls.poisoned.find(pc);dead!=waterfalls.poisoned.end()) for (unsigned reg:dead->second) alu.write(reg,{}); @@ -2126,6 +2156,7 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv = if (in_compare_tail() && i.dst <= 104 && !(i.dst & 1)) { compare_masks[i.dst] = {bit, alu.ir.dependencies[bit].none()}; broken_compare_masks.erase(i.dst); + mask_release.claimed = true; } } if (i.dst == 106 && alu.graphics_predicate.empty() && scalar_operand(i.src[0]) && scalar_operand(i.src[1]) && @@ -2192,9 +2223,11 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv = if (broken_compare_masks.count(i.src[0])) { broken_compare_masks.insert(i.dst); compare_masks.erase(i.dst); + mask_release.claimed = true; } else if (auto bit = compare_mask_bit(i.src[0])) { compare_masks[i.dst] = *bit; broken_compare_masks.erase(i.dst); + mask_release.claimed = true; } alu.write(i.dst, {}); alu.write(i.dst + 1, {}); @@ -2204,8 +2237,10 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv = if (in_compare_tail() && i.name == "s_mov_b32" && i.dst <= 105) { const unsigned base = i.dst & ~1u; if (compare_masks.count(base) || broken_compare_masks.count(base) || - state[base].type == Symbol::Type::lane_mask || state[base].type == Symbol::Type::uniform_mask) + state[base].type == Symbol::Type::lane_mask || state[base].type == Symbol::Type::uniform_mask) { broken_compare_masks.insert(base); + mask_release.claimed = true; + } } if (in_compare_tail() && (i.name == "s_and_b64" || i.name == "s_or_b64" || i.name == "s_andn2_b64") && @@ -2223,6 +2258,7 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv = const bool uniform = left->uniform && right->uniform && alu.ir.dependencies[bit].none(); compare_masks[i.dst] = {bit, uniform}; broken_compare_masks.erase(i.dst); + mask_release.claimed = true; alu.write(i.dst, {}); alu.write(i.dst + 1, {}); alu.write(1024 + pc, bit); diff --git a/tests/native_pixel_compare_mask_check.py b/tests/native_pixel_compare_mask_check.py index 8059ad70da..bbb02d86ae 100644 --- a/tests/native_pixel_compare_mask_check.py +++ b/tests/native_pixel_compare_mask_check.py @@ -7,6 +7,7 @@ write different covered pixels. The vertical quad read of the mask is 1 at (4, 0) only because the helper at (4, 1) contributed a bit. """ +import re import struct import subprocess import sys @@ -19,6 +20,8 @@ HALF = "compare mask half overwritten" SCC = "SCC read without an established wave-wide nonzero result" +UNPROVEN = "unproven graphics CNDMASK source" +NOT_PAIR = "compare mask is not a 64-bit lane mask" CLEAR = -1.0 @@ -71,6 +74,88 @@ def half_overwrite(): return tail(15, 4, 106, after=[sop1(3, 9, 128)]) +def with_exports(code): + return container(code + [vmove(1, 128), vmove(2, 128), vmove(3, 242)]) + + +def whole_quad_compare(): + # Saved exact coverage, then whole-quad mode, then y > 1 in s[4:5]. + return [sop1(4, 20, 126), sop1(10, 126, 126), *cmp_gt(4, 257, 242)] + + +def literal_pair_zero(): + # s_mov_b64 of literal 0 skips the tagged-source copy and must not keep the old bit. + code = whole_quad_compare() + [sop1(4, 4, 128), *cndmask(0, 128, 242, 4)] + return with_exports(code) + + +def literal_copy_zero(): + # Same hole on the fixture tail: the copy into s[8:9] is then replaced by 0. + return tail(15, 4, 106, after=[sop1(4, 8, 128)]) + + +def numeric_pair_mov(): + # s[10:11] is an untagged numeric pair, not a compare. + code = whole_quad_compare() + [sop1(3, 10, 128), sop1(3, 11, 128), sop1(4, 4, 10), + *cndmask(0, 128, 242, 4)] + return with_exports(code) + + +def movk_high(): + # s_movk_i32 s5, 0 overwrites the high half. SOPK is not s_mov_b32. + code = whole_quad_compare() + [0xB0000000 | (5 << 16), *cndmask(0, 128, 242, 4)] + return with_exports(code) + + +def add_high(): + # s_add_u32 s5, 0, 1 writes the high half from constants. It is not a mask combine. + code = whole_quad_compare() + [scalar(0, 5, 128, 129), *cndmask(0, 128, 242, 4)] + return with_exports(code) + + +def smem_low(): + # s_mov_b32 s1, 0 keeps the table high half constant, then s_load_dword s4, s0, 0. + code = whole_quad_compare() + [sop1(3, 1, 128), 0xF4000100, 0xFA000000, *cndmask(0, 128, 242, 4), + vmove(1, 128), vmove(2, 128), vmove(3, 242)] + return patched_pixel(len(code), code, table=True, input_address=3 << 8, + input_signature=((b"ISG1", signature(b"SV_Position", 1)), + (b"OSG1", signature(b"SV_Target", 64))), + export=(0xF800180F, 0x03020100)) + + +def mismatched_copy(): + # s5 is no longer the high half of s4, so the copy must not keep s[8:9]'s old bit. + movk = 0xB0000000 | (5 << 16) + code = whole_quad_compare() + [*cmp_gt(8, 256, 244), movk, sop1(4, 8, 4), + *cndmask(0, 128, 242, 8)] + return with_exports(code) + + +def broken_copy(): + # A 32-bit clobber stays broken across s_mov_b64 onto the select source. + return tail(15, 4, 106, after=[sop1(3, 5, 128), sop1(4, 8, 4)]) + + +def broken_combine(): + # The combine itself must see the broken source, not the bit recorded before the clobber. + code = whole_quad_compare() + [*cmp_gt(106, 256, 244), sop1(3, 5, 128), + scalar(15, 6, 4, 106), *cndmask(0, 128, 242, 6)] + return with_exports(code) + + +def missing_vcc_operand(): + # Both operands are tracked, but VCC has no bit yet. + code = whole_quad_compare() + [scalar(15, 6, 4, 106), *cndmask(0, 128, 242, 6)] + return with_exports(code) + + +def cselect_replaces_pair(): + # s_cmp_eq_u32 1, 1 then s_cselect_b64 s[4:5], -1, 0 replaces the compare with a full mask. + cmp = 0xBF000000 | (6 << 16) | (129 << 8) | 129 + code = whole_quad_compare() + [cmp, scalar(11, 4, 193, 128), *cndmask(0, 128, 242, 4)] + return with_exports(code) + + CASES = { "and": (15, 4, 106), "and-swap": (15, 106, 4), @@ -170,12 +255,17 @@ def compile_guest(blob): uniform = compile_guest(scc_program(varying=False)) assert uniform.returncode == 0, uniform.stderr print("validated uniform SCC select") - for label, blob, reason in ( - ("half overwrite", half_overwrite(), HALF), - ("varying SCC", scc_program(varying=True), SCC)): - refused = compile_guest(blob) - assert refused.returncode and reason in refused.stderr, (label, refused.stderr) - print(f"refused {label}") + done = set(CASES) | {"compare-scc-uniform"} + for label, blob, reason in container_cases(): + if label in done: + continue + result = compile_guest(blob) + if reason: + assert result.returncode and reason in result.stderr, (label, result.stderr) + print(f"refused {label}") + else: + assert result.returncode == 0, (label, result.stderr) + print(f"validated {label}") dump = subprocess.run([Path(sdk) / "dxc.exe", "-dumpbin", kept], capture_output=True, text=True) assert dump.returncode == 0 and "@dx.op.isHelperLane.i1" in dump.stdout, dump.stdout @@ -198,15 +288,22 @@ def lower(blob): assert or_ir.returncode == 0 and " or i32 " in or_ir.stdout, or_ir.stderr nand = lower(tail(21, 106, 4)) assert nand.returncode == 0 and " xor i32 " in nand.stdout, nand.stderr - for label, blob, reason in ( - ("half overwrite", half_overwrite(), HALF), - ("varying SCC", scc_program(varying=True), SCC)): + for label, blob, reason in container_cases(): + if not reason: + continue refused = lower(blob) assert refused.returncode and reason in refused.stderr, (label, refused.stderr) print(f"ir refused {label}") uniform = lower(scc_program(varying=False)) assert uniform.returncode == 0, uniform.stderr - print("ir: and, or, andn2, quad read and exact coverage") + replaced = lower(cselect_replaces_pair()) + assert replaced.returncode == 0, replaced.stderr + # The select condition is the new full mask. A stale compare bit still + # compiles, so the icmp has to read this select and not the earlier compare. + assert re.search( + r"%(\w+) = select i1 %\w+, i32 4294967295, i32 0\n\s*%\w+ = icmp ne i32 %\1, 0", + replaced.stdout), replaced.stdout + print("ir: and, or, andn2, quad read, exact coverage and scalar mask replace") def draw_main(cli, raster, sdk, backend): @@ -243,6 +340,17 @@ def container_cases(): cases.append(("compare-scc-uniform", scc_program(varying=False), "")) cases.append(("compare-half-overwrite", half_overwrite(), HALF)) cases.append(("compare-scc-varying", scc_program(varying=True), SCC)) + cases.append(("compare-literal-zero", literal_pair_zero(), UNPROVEN)) + cases.append(("compare-literal-copy-zero", literal_copy_zero(), UNPROVEN)) + cases.append(("compare-numeric-mov", numeric_pair_mov(), UNPROVEN)) + cases.append(("compare-movk-high", movk_high(), UNPROVEN)) + cases.append(("compare-add-high", add_high(), UNPROVEN)) + cases.append(("compare-smem-low", smem_low(), UNPROVEN)) + cases.append(("compare-mismatched-copy", mismatched_copy(), UNPROVEN)) + cases.append(("compare-broken-copy", broken_copy(), HALF)) + cases.append(("compare-broken-combine", broken_combine(), HALF)) + cases.append(("compare-missing-vcc", missing_vcc_operand(), NOT_PAIR)) + cases.append(("compare-cselect-mask", cselect_replaces_pair(), "")) return cases From 61df3c5f34b8d4405587f113060a0eafdfe51e19 Mon Sep 17 00:00:00 2001 From: Leyukaka <45659323+Leyukaka@users.noreply.github.com> Date: Sun, 29 Nov 2026 16:34:06 +0100 Subject: [PATCH 4/5] graphics: keep a wave32 compare mask when its high half is written --- src/gfx1013_direct_graphics.inl | 3 ++- tests/native_pixel_compare_mask_check.py | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/gfx1013_direct_graphics.inl b/src/gfx1013_direct_graphics.inl index cdc437887a..4878cd50e6 100644 --- a/src/gfx1013_direct_graphics.inl +++ b/src/gfx1013_direct_graphics.inl @@ -2234,7 +2234,8 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv = return; } } - if (in_compare_tail() && i.name == "s_mov_b32" && i.dst <= 105) { + // A wave32 mask is one SGPR, so the odd register is not its high half. + if (in_compare_tail() && !stage.wave32 && i.name == "s_mov_b32" && i.dst <= 105) { const unsigned base = i.dst & ~1u; if (compare_masks.count(base) || broken_compare_masks.count(base) || state[base].type == Symbol::Type::lane_mask || state[base].type == Symbol::Type::uniform_mask) { diff --git a/tests/native_pixel_compare_mask_check.py b/tests/native_pixel_compare_mask_check.py index bbb02d86ae..b95ab1ad12 100644 --- a/tests/native_pixel_compare_mask_check.py +++ b/tests/native_pixel_compare_mask_check.py @@ -37,11 +37,11 @@ def scalar(op, dst, src0, src1): return 0x80000000 | (op << 23) | (dst << 16) | (src1 << 8) | src0 -def container(code): +def container(code, wave32=False): return patched_pixel(len(code), code, input_address=3 << 8, input_signature=((b"ISG1", signature(b"SV_Position", 1)), (b"OSG1", signature(b"SV_Target", 64))), - export=(0xF800180F, 0x03020100)) + export=(0xF800180F, 0x03020100), wave32=wave32) def tail(op, src0, src1, *, after=()): @@ -74,8 +74,8 @@ def half_overwrite(): return tail(15, 4, 106, after=[sop1(3, 9, 128)]) -def with_exports(code): - return container(code + [vmove(1, 128), vmove(2, 128), vmove(3, 242)]) +def with_exports(code, wave32=False): + return container(code + [vmove(1, 128), vmove(2, 128), vmove(3, 242)], wave32) def whole_quad_compare(): @@ -149,6 +149,14 @@ def missing_vcc_operand(): return with_exports(code) +def wave32_high_write(): + # Wave32: s_mov_b32 s20, exec_lo; s_wqm_b32 exec_lo, exec_lo; the compare + # writes s4 only. s_mov_b32 s5, 0 leaves that mask, so the select lowers. + code = [sop1(3, 20, 126), sop1(9, 126, 126), *cmp_gt(4, 257, 242), sop1(3, 5, 128), + *cndmask(0, 128, 242, 4)] + return with_exports(code, wave32=True) + + def cselect_replaces_pair(): # s_cmp_eq_u32 1, 1 then s_cselect_b64 s[4:5], -1, 0 replaces the compare with a full mask. cmp = 0xBF000000 | (6 << 16) | (129 << 8) | 129 @@ -351,6 +359,7 @@ def container_cases(): cases.append(("compare-broken-combine", broken_combine(), HALF)) cases.append(("compare-missing-vcc", missing_vcc_operand(), NOT_PAIR)) cases.append(("compare-cselect-mask", cselect_replaces_pair(), "")) + cases.append(("compare-wave32-high-write", wave32_high_write(), "")) return cases From 2211dac07b58740d1beecf8cfe5975aabb126f44 Mon Sep 17 00:00:00 2001 From: Leyukaka <45659323+Leyukaka@users.noreply.github.com> Date: Sun, 29 Nov 2026 16:35:07 +0100 Subject: [PATCH 5/5] graphics: drop tracked compare masks at block entry --- src/gfx1013_direct_graphics.inl | 9 ++++++++ tests/native_pixel_compare_mask_check.py | 27 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/gfx1013_direct_graphics.inl b/src/gfx1013_direct_graphics.inl index 4878cd50e6..4caecec35a 100644 --- a/src/gfx1013_direct_graphics.inl +++ b/src/gfx1013_direct_graphics.inl @@ -1670,6 +1670,9 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv = std::map compare_masks; std::set broken_compare_masks; bool compare_scc_unknown = false; + // First pc of each block. An entry holds only in the block that recorded + // it: a join or a sibling block reads the pair through the per-edge state. + std::set block_entries; // Vertex class masks still use the symbolic pair. Recording them here would // keep a CNDMASK source after a 32-bit half overwrite. auto in_compare_tail = [&] { @@ -1677,6 +1680,10 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv = }; auto instruction = [&](const Inst &i, const State &state) { const auto pc = alu.graphics_pc = graphics.pc = i.pc; + if (block_entries.count(pc)) { + compare_masks.clear(); + broken_compare_masks.clear(); + } // A scalar destination the tracker does not itself rewrite drops any // pair it overlaps. Otherwise v_cndmask keeps a compare bit the guest // register no longer holds. @@ -2411,6 +2418,8 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv = } }; auto blocks = graphics_blocks(program, regions); + for (const auto &entry : blocks) + block_entries.insert(entry.first); std::map> edges; std::vector ready; if (!blocks.at(0).predecessors) diff --git a/tests/native_pixel_compare_mask_check.py b/tests/native_pixel_compare_mask_check.py index b95ab1ad12..0ae6b7256c 100644 --- a/tests/native_pixel_compare_mask_check.py +++ b/tests/native_pixel_compare_mask_check.py @@ -149,6 +149,31 @@ def missing_vcc_operand(): return with_exports(code) +SCC_TRUE = 0xBF000000 | (6 << 16) | (129 << 8) | 129 # s_cmp_eq_u32 1, 1 + + +def branch(op, offset): + return 0xBF800000 | (op << 16) | offset + + +def join_after_clear(): + # SCC is 1, so the guest always runs the then arm and v0 is 0 on every lane. + # The else arm copies the compare. The join must not select on it. + code = whole_quad_compare() + [SCC_TRUE, branch(4, 2), # s_cbranch_scc0 else + sop1(4, 4, 128), branch(2, 1), # then: s_mov_b64 s[4:5], 0 + sop1(4, 4, 4), # else: s_mov_b64 s[4:5], s[4:5] + *cndmask(0, 128, 242, 4)] # join + return with_exports(code) + + +def join_after_redefine(): + # The then arm redefines s[4:5]. A join select on that bit would read a + # value that does not dominate it. + code = whole_quad_compare() + [SCC_TRUE, branch(4, 2), *cmp_gt(4, 256, 244), + *cndmask(0, 128, 242, 4)] + return with_exports(code) + + def wave32_high_write(): # Wave32: s_mov_b32 s20, exec_lo; s_wqm_b32 exec_lo, exec_lo; the compare # writes s4 only. s_mov_b32 s5, 0 leaves that mask, so the select lowers. @@ -360,6 +385,8 @@ def container_cases(): cases.append(("compare-missing-vcc", missing_vcc_operand(), NOT_PAIR)) cases.append(("compare-cselect-mask", cselect_replaces_pair(), "")) cases.append(("compare-wave32-high-write", wave32_high_write(), "")) + cases.append(("compare-join-after-clear", join_after_clear(), UNPROVEN)) + cases.append(("compare-join-after-redefine", join_after_redefine(), UNPROVEN)) return cases