shader: lower v_sad_u8 and v_sad_u16 #1649
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1656"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by rm-NoobInCoding on 2026-11-24T16:30:55Z.
Change
The integer table in
tools/pixel_aot.pylowered three of the five sums of absolute differences (v_sad_hi_u8,v_sad_u32,v_msad_u8) and refusedv_sad_u8andv_sad_u16. This PR lowers the two remaining forms:v_sad_u8:SAD_U8(S0, S1) + S2, the four byte-lane sum without the shift ofv_sad_hi_u8, reusing the existingsad4_u8helper;v_sad_u16:ABSDIFF(S0[31:16], S1[31:16]) + ABSDIFF(S0[15:0], S1[15:0]) + S2, through a newsad2_u16helper that is emitted only for a program that uses it.Both are unsigned and modulo 2^32, as for the existing forms (AMD RDNA 2 ISA reference, VOP3 opcodes 346 and 348). The
_e32spelling,clamp, float modifiers, a wrong operand count and unwritten registers are refused, as for the rest of the family.v_qsad_pk_u16_u8,v_mqsad_pk_u16_u8andv_mqsad_u32_u8stay refused.The checks that pinned
v_sad_u8andv_sad_u16as refused and uncovered (check_sad_lowering, the 16-bit ternary neighbours intests/pixel_aot_check.py, andrejected_forms,sad_formsand the int16 neighbours intests/form_coverage_check.py) now expect them accepted and covered. The forms that stay refused are still checked as refused.docs/experiments.mdis updated.Coverage: +2 gfx1013 and gfx1030 VOP3 forms (
v_sad_u8,v_sad_u16).Overlap: draft #531 lists
v_sad_u8andv_sad_u16among a wider set of byte and funnel forms. This PR covers only these two SAD forms and does not touch the others #531 lists.Verification
Base
578d1e1c, patch87e48ad7. Windows 11 build 26300,devpreset, DXC 10.0.26100.0, NVIDIA GeForce RTX 4070 (driver 32.0.16.1714) and WARP.pixel_aot_sad_hardwareandpixel_aot_sad_warp: 2/2 passed, no skips. Each now draws three groups: 35 draws from 3 compiles. Thelanesgroup adds 13 draws: 2 rows plus 11 wrong references that must reportpixel_mismatch.v_sad_u8andv_sad_u16.rdna2_tools_check.py: 145 tests OK (18 skipped without LLVM). The shader scanner derives its integer patterns frompixel_aot.INTEGER, sotest_supported_shader_instructionsnow expectsv_sad_u8andv_sad_u16to be supported and keeps their_e32,clampand short-operand spellings refused (second commit; the first CI run failed on this test).pixel_aot_check.pystatic controls,form_coverage_check.py(form coverage checks passed), docs links (16 files, 348 links) and bidi controls: passed.Negative controls.
sad2_u16makespixel_aot_sad_warpfail. Every one of the 13lanescases reportsmismatch, including the two right rows.v_sad_u16throughsad4_u8makescheck_sad_loweringfail on the expected statement.LLVM 15 round trip: not run locally (no LLVM with gfx1013 on this host); it passed in CI (
Assemble and translate gfx1013 fixtures) on78fc5c08. The new lines use the operand forms LLVM 15 already accepts for the sibling SAD instructions: three sources in the VOP3 encoding, at most two scalar registers and literals. The literal accumulator0x3f800010is not an inline constant.Evidence and follow-up
All inputs are synthetic. The rows reuse the byte group's rows, whose halfwords also lie on both sides of 0x8000 (
0xc100,0x40c3), so the unsigned and signed halfword sums of the first row differ (0xffbcand0xff42). Every input and expected result is a normal binary32 or zero, as the draw asserts. The draws show that the translated shader computes the documented operations on desktop GPUs, not that the console does.Checklist
AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T16:41:59Z.
Review: COMMENTED
Peer review of PR #1656 (
shader: lower v_sad_u8 and v_sad_u16) at commit87e48ad7.Findings
No P1/P2 issues found.
v_sad_u8lowers tosad4_u8({0}, {1}, false) + {2}(4 unsigned byte absolute differences summed withsrc2modulo2^{32}, no saturation).v_sad_u16lowers tosad2_u16({0}, {1}) + {2}(2 unsigned 16-bit lane absolute differences summed withsrc2modulo2^{32}, no saturation)._e32,clamp, float modifiers, wrong operand counts, unwritten/out-of-range registers, and >32-bit literals are refused;sad4_u8andsad2_u16helpers are emitted only when referenced.tests/form_coverage_check.pymoves("VOP3", "v_sad_u8")and("VOP3", "v_sad_u16")intosad_formsand updatescheck_int16_ternary_forms;tests/pixel_aot_check.pyadds thelanesgroup and negative controls.Checks Executed
/root/.venvs/x2w-tests/bin/python3 tests/form_coverage_check.py->form coverage checks passed(exit 0)PYTHONPATH=tests:tools /root/.venvs/x2w-tests/bin/python3 -c "import pathlib, tempfile, pixel_aot_check as p; ... p.check_controls(pathlib.Path(d))"->check_controls OK(exit 0, includescheck_sad_lowering)llvm-mc-19 -triple=amdgcn-amd-amdhsa -mcpu=gfx1013 -filetype=obj -o /dev/nullon allSAD_BODIES(bytes,dword,lanes) -> exit 0"sad_u16"intools/pixel_aot.pytosad4_u8({0}, {1}, false) + {2}and re-ranpixel_aot_check.check_sad_lowering()-> failed as expected withAssertionError: ('lanes', ' precise float alu23 = asfloat(sad2_u16(params[0].w, asuint(alu20)) + params[1].y);'). Restored withgit checkout -- .and verified cleangit status --short.x2w_pipe.py win2025 1656 --targets offscreen_check --regex "pixel_aot_sad|pixel_aot_int16_ternary" --py-checks tests/form_coverage_check.py):--preset dev):offscreen_checkbuilt (3.86s),ctest --preset dev -R 'pixel_aot_sad|pixel_aot_int16_ternary'4/4 passed (2.95s:pixel_aot_sad_hardware,pixel_aot_sad_warp,pixel_aot_int16_ternary_hardware,pixel_aot_int16_ternary_warp; hardware tests excluded/WARP on VM)./O2(--preset release):offscreen_checkbuilt (1.53s),ctest --preset release -R 'pixel_aot_sad|pixel_aot_int16_ternary'4/4 passed (2.98s:pixel_aot_sad_warp,pixel_aot_sad_hardware,pixel_aot_int16_ternary_hardware,pixel_aot_int16_ternary_warp).tests/form_coverage_check.py-> passed (16.21s).AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T17:03:53Z.
Review: COMMENTED
Re-review at
head_sha:78fc5c08f91efbead6f0c353a72c79380bb9e862(new commit78fc5c08on top of87e48ad7).Findings
No P1/P2. The new commit only touches
tests/rdna2_tools_check.pyand makes the scanner test match the translator change.v_sad_u8andv_sad_u16as unsupported. It now expectsis_instruction_supportedforv_sad_u8 v1, v2, v3, v4andv_sad_u16 v1, s2, v3, 64, and keeps refusing_e32,clamp, a missing operand andv_sad_u16 ... clamp.is_opcode_recognizedgains both opcodes.test_supported_shader_instructionsagainst this head'stools/and it fails. So this commit fixes a real failing check, not just a tidy-up.Verified
Linux only, x86-64 (no Windows/D3D12 run this time):
PYTHONPATH=tests:tools python -m unittest rdna2_tools_check.TestRdna2Scanner.test_supported_shader_instructions-> OK.python tests/form_coverage_check.py->form coverage checks passed.pixel_aot_check.check_controls(includescheck_sad_lowering) -> OK.llvm-mc-19 -triple=amdgcn-amd-amdhsa -mcpu=gfx1013 -show-encoding:v_sad_u8 v1, v2, v3, v4->01 00 5a d5 02 07 12 04;v_sad_u16 v1, s2, v3, 64->01 00 5c d5 02 06 02 03;v_sad_u16 v1, v2, v3, v4 clampassembles (clamp bit set), and the PR refuses it."sad_u8"entry fromINTEGERintools/pixel_aot.py; the scanner test fails withAssertionError: False is not true : v_sad_u8 v1, v2, v3, v4. Restored;git status --shortclean.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T17:28:18Z.
Review: COMMENTED
Verified at
head_sha:78fc5c08f91efbead6f0c353a72c79380bb9e862(0 P1, 0 P2).Scope & Semantics
tools/pixel_aot.py,tests/pixel_aot_check.py,tests/form_coverage_check.py,tests/rdna2_tools_check.py, anddocs/experiments.md.v_sad_u8(sad4_u8(a, b, false) + c) andv_sad_u16(sad2_u16(a, b) + c) match the AMD RDNA 2 ISA definitions (VOP3 opcodes 346 and 348, unsigned lane absolute differences summed modulo 2^32); VOP3-only guards reject_e32,clamp, float modifiers, and wrong operand counts.Verified
git diff --check origin/main...HEAD-> cleanpython3 tests/docs_links_check.py-> passed (16 Markdown files, 348 links)python3 tests/bidi_controls_check.py-> passed (1276 tracked files)PYTHONPATH=tests:tools python3 -m unittest rdna2_tools_check.TestRdna2Scanner.test_supported_shader_instructions-> OKpixel_aot_check.check_sad_lowering()-> OKllvm-mc-19 -triple=amdgcn-amd-amdhsa -mcpu=gfx1013 -show-encoding:v_sad_u8 v12, s44, v18, s46(0c 00 5a d5 2c 24 ba 00),v_sad_u16 v14, s44, v18, s46(0e 00 5c d5 2c 24 ba 00)--preset dev):offscreen_checkbuilt (10.49s),ctest --preset dev -R 'pixel_aot_sad|pixel_aot_int16_ternary'4/4 passed (3.01s:pixel_aot_sad_hardware,pixel_aot_sad_warp,pixel_aot_int16_ternary_warp,pixel_aot_int16_ternary_hardware)/O2(--preset release):offscreen_checkbuilt (8.17s),ctest --preset release -R 'pixel_aot_sad|pixel_aot_int16_ternary'4/4 passed (3.09s)tests/form_coverage_check.py-> passed (16.76s)sad_u8by<< 16uintools/pixel_aot.py->pixel_aot_check.check_sad_lowering()failed as expected (AssertionErroronlanesalu21)."sad_u16"fromSAD_HELPERSintools/pixel_aot.py->pixel_aot_check.check_sad_lowering()failed as expected (sad2_u16helper count mismatch).AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T17:58:13Z.
The lowering table is exercised; three mutants fail
Ran this at
78fc5c08:Mutation check on the new entries
Three changes to
SAD_LOWERINGintools/pixel_aot.py, each restored afterwards:sad_u16lowered withsad4_u8instead ofsad2_u16RuntimeError: Control sub-check ('fn', 'check_sad_lowering') failedsad_u8drops the accumulator{2}msad_u8's masked form flipped fromtruetofalseSo the three behaviours this patch adds are each pinned by a named sub-check, and the failure message says which one. That is
better than most of the lowering tables in this tool:
check_sad_loweringreports the failing form rather than a genericmismatch.
Two things worth stating in the PR body
rdna2_tools_checkstill skips 18 tests (LLVM_LIBunset), and those are the ones that disassemble against LLVMrather than against hand-written expectations. If the
v_sad_u8/v_sad_u16encodings in this patch were checkedagainst a real
llvm-mc, a line with the version and the printed mnemonic would make that checkable for everyoneelse. Same point as #1661 — not a blocker, just the one claim a reader cannot verify from CI.
sad_u16usessad2_u16whilesad_hi_u8reusessad4_u8. Worth one sentence indocs/experiments.mdsayingwhich helper each form maps to, since the table is the only place that fact lives and it is easy to mis-edit — that is
exactly what my first mutant did, and only the control caught it.
Otherwise
form_coverage_check.pypicks up the new opcodes, the doc entry is in the same patch, and nothing else in the diff lookedloose.
Verdict
Reviewed
78fc5c08. The lowering is correct as far as the fixtures go, and all three forms are guarded by mutation. Noobjections.
Notes