startup: lower combined register+immediate SMEM offsets in gfx2hlsl #1874
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1881"
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 consolelogkyo on 2026-11-24T20:02:46Z.
Problem
In RDNA 2 SMEM instructions (
s_load_dword*ands_buffer_load_dword*), a scalar registerSOFFSET(w1[31:25],s0..s105orm0) and a non-zero 21-bit signed immediateOFFSET(w1[20:0]) can be active at the same time:tools/disasm_rdna2.pyprints the register inops[2]andoffset:0x...inmods(for examples_buffer_load_dwordx4 s[20:23], s[16:19], s8 offset:0x18ors_load_dwordx4 s[24:27], s[10:11], s9 offset:0x10).Previously,
Stage.op_s_buffer_load_dword()andStage.op_s_load_dwordx4()intools/startup/gfx2hlsl.pyignoredmods:offset:0x...on register-addressed SMEM loads. Boths_buffer_load_dword*and opt-in dynamic sampler-heaps_load_dwordx4discardedoffset:0x...frommodsand loaded from+ 0instead of adding the immediate byte offset.+1..2MiB.OFFSET[20:0]is signed two's complement on RDNA 2, whiledisasm_rdna2formatsw1 & 0x1fffffas unsigned hex (0x100000..0x1ffffffor negative byte offsets). Parsing those tokens directly as non-negative literals produced+1..2MiB offsets instead of refusing them.soffsetoperands. Unknown SMEM modifiers and non-SGPR/m0soffsetoperands (vcc_lo,exec_lo,ttmp*, VGPRs, register ranges) were not validated consistently acrosss_buffer_load_dword*and both sampler-heap modes ofs_load_dwordx4.s_buffer_load_dwordx*used a runtime SGPRsoffsetthat lay inside the destination range (dst[1] <= sreg < dst[2]), writing the first destination register overwrotesNbefore subsequent dwords were loaded.What Changed
tools/startup/gfx2hlsl.py:smem_mods(mods)to accept cache-policy flags (glc,dlc) and at most one non-negative 21-bit signedoffset:modifier (0..0xfffff), refusing negative 21-bit immediates (> 0xfffff, an unlowered translator gap refused by name rather than read at+1..2MiB), duplicateoffset:tokens and any unknown modifier.Stage.smem_soffset()to restrict non-literal SMEM offset operands tos0..s105(viaself.S(), preserving descriptor/pointer refusals) orm0acrosss_buffer_load_dword*and both sampler-heap modes ofs_load_dwordx4(without pollutingused_swhen the default sampler-heap shortcut is taken).Stage.op_s_buffer_load_dword()andStage.op_s_load_dwordx4()to addoffset:0x...on register-addresseds_buffer_load_dword*and opt-in sampler-heaps_load_dwordx4loads, refuse negative or out-of-range literal immediates and mismatched destination/base widths, and save an overlapping runtime SGPRsoffsetintoabefore multi-dword destination writes.tests/startup_smem_address_check.py,CMakeLists.txt,.github/workflows/windows.yml,AGENTS.md,docs/experiments.md:binary llvm-mc-15parity check, pure-Pythoncontrols, andwarp/hardwareGPU readback CTests):s_buffer_load_dwordx4withSGPR+offset:0x18 glcat two runtimesoffsetvalues (32and64);s_buffer_load_dwordwithm0+offset:0x14 dlc, constant-SGPR +offset:0x08, and destination-overlapping runtimes_buffer_load_dwordx2 s[20:21], s[16:19], s20 offset:0x1c glc;s_load_dwordx4 s[24:27], s[10:11], s9 offset:0x10compiled with both DXCvs_6_0/ps_6_0and FXCvs_5_1/ps_5_1) selecting entry 1 (s9 = 0), entry 2 (s9 = 16) and fallback entry 2047 (s9 = 2046 * 16);offset:0x18, dropping+ 16on the sampler heap, and reading the clobbereds20word instead ofa) verified to fail readback.Verification
Tested on Windows 11 26H2 (build
26300.9974), MSVC19.44.35230.0, Windows SDK10.0.26100.0(dxc.exe+fxc.exe), D3D12 debug layer enabled (debug_errors=0 debug_warnings=0):10.0.26100.9818— 0 mismatches across all 320-dword buffer readbacks and all DXC/FXC sampler-heap draws; all wrong-reference controls and translation mutations failed as expected.32.0.32029.2010) — 0 mismatches across all 320-dword buffer readbacks and all DXC/FXC sampler-heap draws; all wrong-reference controls and translation mutations failed as expected.AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T20:08:36Z.
Review: COMMENTED
Review of
a9401f53(merge base =origin/main=df40e4bc, so no rebase needed). Comment only, not an approval.Verdict
No P1 or P2 found. The addressing arithmetic matches the encoding the PR describes, and every mutant I tried is caught. Three P3 notes. Clean-room and title-neutral: all instruction bytes in the fixture are built from the bit layout, with no game files or titles.
Addressing
soffset + imm. Inop_s_buffer_load_dwordthe generated load isLoad(<soffset> + <imm> + 4*i)(bytes, no dword scaling), and in the opt-in sampler-heap path ofop_s_load_dwordx4the saved first destination is<soffset> + <imm>.soffset == 125as a bare literal (0x%xof the 21-bit field) and otherwise prints the register plusoffset:0x..., so the two forms cannot appear together. The PR refuses a literal with anoffset:modifier, which covers a hand-written mix.0x100000..0x1fffffare refused in both theoffset:modifier and the literal form, so a negative immediate cannot read at +1..2 MiB. This is deliberately narrower than hardware (negative s_load offsets are legal); the docs say it is refused, not translated.glc,dlcandoffset:, so refusing every other modifier cannot reject something a real capture carries today.sNsits indst[1] <= N < dst[2], the offset is copied toafirst, so later dwords do not read the freshly loaded value.ais the existing scratch variable (declared in the shader prologue and used the same way by two other ops), and nothing runs between the copy and the loads.Mutants (each applied alone to
tools/startup/gfx2hlsl.py, run withtests/startup_smem_address_check.py, then reverted)All 8 fail the check: immediate dropped from
s_buffer_load; limit0xfffffraised to0x1fffff; the overlap copy removed; the+ immremoved from the sampler-heap path; unknown modifiers ignored; a secondoffset:accepted; literal plusoffset:allowed; thes105limit raised to 127.P3
X2W_SAMPLER_HEAPis off, the old code accepted any non-literal offset token and returned a default sampler. The new code evaluates the operand withsmem_soffset(without recording the register use), so a shader whose offset register is nots0..s105/m0, or holds somethingS()cannot evaluate, now stops withUnsupportedwhere it used to translate. That matches the stated intent, but it is a behaviour change on the default path and the description only mentions the opt-in path.old_s_buffer_load_dwordis a copy of the removed implementation, kept for negative controls. It will not follow future changes to the real handler; the mutants above already cover the same failures against the real code.Commands run (Linux, Python 3.14.8, llvm-mc-19)
python -B tests/startup_smem_address_check.py:startup SMEM address controls passed.python -B tests/startup_smem_address_check.py binary llvm-mc-19:SMEM combined-offset encodings match llvm-mc(CI pinsllvm-mc-15).gfx2hlsl, run on this head and onorigin/main: same exit code for each, except the newstartup_smem_address_check.py(absent on main, passes here). That includes the existingstartup_gfx2hlsl_smem_offset_check.py.python -B tests/docs_links_check.py: Self-test passed. Checked 18 Markdown files and 379 links.python -B tests/bidi_controls_check.py: Self-test passed. Checked 1327 tracked source, script and build/CI files: no bidirectional controls.git diff --check origin/main...HEAD: clean.git merge-treewithorigin/main: clean. All mutations reverted withgit checkout -- ..Not run
startup_smem_address_warp,startup_smem_address_hardwareand the FXC compile (no Windows, DXC or GPU here).AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T20:19:13Z.
Ran the check and re-broke all four fixes
At the PR head, device-free:
Four mutations to
tools/startup/gfx2hlsl.py, each reverted, one per fix the description claims:smem_addresssmem_offset(old check)extra = imm or 0→extra = 0)if value > 0xfffff→> 0x1fffff(negative 21-bit accepted)soffsetsave (emit('a = ...')) removedThe right-hand column matters as much as the left: the older
smem_offsetcheck stays green under all four, i.e. these areregressions the repo did not previously catch. That is exactly what "previously ignored
mods" means, and the new fixturedistinguishes each fix separately rather than failing as a block.
One dead assertion in the new fixture
'0X10' or Trueis always truthy, so that line asserts nothing; the real check is the'+ 42)'on the next two lines. Worthdeleting (or making it
assert '0X2A' in st_upper.lines[0]if the intent was to pin the token spelling).Two questions
0x100000..0x1fffffrefusal vs. the reference. The doc row says the negative encodings are refused rather thantranslated as
-1..-524288. Refusing is the conservative choice and I agree with it for now; if the ISA-level answer is"add the sign-extended offset", then this PR turns a wrong result into a named unsupported, which the row should say
explicitly so a future reader knows it is a gap and not a decision.
m0assoffsetwith an immediate. The controls coverm0 + offset:0x14; ism0also checked for thedestination-overlap case (it cannot overlap, so a
passon that path is fine — I just could not tell whether the branch isexercised).
Scope
AGENTS.mdanddocs/experiments.mdgain one line each, both consistent with how the other checks are listed;CMakeLists.txtadds
controls,_warpand_hardwareregistrations withSKIP_RETURN_CODE 77like their neighbours. The workflow changeadds the new file to both the binary-parity matrix and the Linux check list, so it runs on both CI paths.
Verdict
Reviewed. Every fix in the description is pinned by a mutation that also proves the old check missed it. My only concrete
request is the dead
assert '0X10' or True.Notes
Imported from R0Y00/Xbox2Windows. Originally posted by consolelogkyo on 2026-11-24T20:41:35Z.
Thanks for the thorough review and mutation table, @ChazY11! Rebased onto
origin/main(3cc12064) to resolve the list conflicts in.github/workflows/windows.ymlandAGENTS.md, and folded in the cleanups from your and @tedin7's reviews:assert '0X10' or Trueline before theoffset:0X2Acheck (and replaced the copiedold_s_buffer_load_dwordhelper with a live in-process monkeypatch onsmem_mods).OFFSET(0x100000..0x1fffff): clarified indocs/experiments.md(and the PR description) that hardwares_load_*sign-extends the 21-bit immediate, so refusing0x100000..0x1fffffhere is an explicit translator gap (turning a silent+1..2MiB misread into a namedUnsupported) rather than a permanent policy.m0on the destination-overlap check: them0 + offset:0x10loop incontrols()already runs across all fives_buffer_load_dword*widths (1,2,4,8,16), wherereg_range('m0')isNoneso the overlap branch is skipped; pinned that invariant explicitly withassert len(st_m0.lines) == width and all(not line.startswith('a =') for line in st_m0.lines).AI-assisted.