Skip to content

smem_neg_probe: test the CPU side of the negative-offset probe - #4250

Open
SebSilent wants to merge 1 commit into
ItsNotRexed:mainfrom
SebSilent:test/smem-neg-probe
Open

SebSilent wants to merge 1 commit into
ItsNotRexed:mainfrom
SebSilent:test/smem-neg-probe

Conversation

@SebSilent

@SebSilent SebSilent commented Nov 23, 2026 •

Copy link
Copy Markdown

Claimed in the coordination issue as: claim (new TEST item).

What and why

tools/smem_neg_probe.py (SHADER-336, s_load / s_buffer_load with a negative sOFF plus IMM) had no test file at all. This adds tests/test_smem_neg_probe.py covering the half that needs no GPU:

  • the seed value table's window bounds (byte offsets BASE-0x40 .. BASE+0xBF, None outside);
  • the GFX10 SMEM encoding of the rows llvm-mc rejects (s_buffer_load built by hand as .long words), field by field, including the odd sbase>>1 truncation;
  • the translator's s_buffer_load rule against hand-computed offsets, including sOFF -4 wrapping to an in-table 4 and IMM -4 wrapping past NUM_RECORDS;
  • the kernel skeleton (16 x4 seed stores, one waitcnt per probe row, the value order the readback walk uses);
  • the CLI's --oracle translator mode, with stdout values cross-checked against expected() row by row;
  • the documented 77 exit with a reason line when the hardware oracle is unavailable (verified on this machine, which has no AMD HIP driver).

PR #3356 touches this tool's oracle plumbing; the test file is additive against main and stays independent of it.

Tested

  • python -I tests/test_smem_neg_probe.py: 7 passed.
  • python -m compileall -q tools/smem_neg_probe.py tests/test_smem_neg_probe.py: clean. git diff --check: clean.
  • The 77 path itself was exercised outside the test too (python3 -I tools/smem_neg_probe.py --oracle hw prints the launcher reason and exits 77 here).
  • CPU only, no GPU run, no game files. Linux, Python 3.14.

AI-assisted.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a)

@ray3242

ray3242 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

The CLI test fails under pytest (the runner AGENTS.md documents) and under -v: CliTest.run_tool drops its *argv and calls smp.main(), which has no argv parameter, so argparse reads the test runner's own sys.argv:

$ python -I tests/test_smem_neg_probe.py           -> OK (only because --oracle defaults to translator)
$ python -I tests/test_smem_neg_probe.py -v        -> ERROR test_translator_mode_prints_one_row_per_case: SystemExit: 2
$ python -m pytest -q tests/test_smem_neg_probe.py -> 1 failed, 6 passed

Patching argv in run_tool fixes it without touching the tool:

with mock.patch.object(sys, "argv", [TOOL, *argv]), redirect_stdout(out), redirect_stderr(err):
    status = smp.main()

Also, klepacek's open #3356 (since 2026-11-22) already adds a tests/test_smem_neg_probe.py (152 lines) and gives main(argv=None); the two branches conflict on that file, so it's worth checking with them which one carries the tests.

Linux, Python 3.12.16, no GPU. AI-assisted (Devin).

@SebSilent

Copy link
Copy Markdown
Author

Good catch, thanks. Pushed a fix: main(argv=None) like the other tools, and run_tool passes its list. The -v/pytest leak is gone (python -I tests/test_smem_neg_probe.py -v is green here), and there is now also an in-process check that --oracle hw still returns the documented 77 with a reason line. 8 tests OK. AI-assisted.

@sds-228 sds-228 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review of f554a98: no issues found. The in-process CLI helper now passes argv through, so both translator and skipped-hardware paths are exercised. Targeted suite: 8 tests OK; compileall and diff check clean.

AI-assisted (OpenCode).

@mrbean56

Copy link
Copy Markdown

Tested f554a98c on Linux (Debian 13), Python 3.13.5, CPU only: pass.

python -I tests/test_smem_neg_probe.py -v
Ran 8 tests in 0.037s
OK

Same eight tests as the description (7 + the review-fix one), including the --oracle hw exit-77 path and the translator rows. No GPU, no game data. AI-assisted.

@traeb0n

traeb0n commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Code read and tested at f554a98 on Windows 11 Pro 26300, Python 3.11: looks good.

run result
python -I tests/test_smem_neg_probe.py -v 8 tests OK
python -m pytest -q tests/test_smem_neg_probe.py 8 passed

@ray3242's argv leak is fixed: -v and pytest both pass now.

An independent check of the hand-built words. test_sbuf_raw_encodes_the_gfx10_smem_fields rebuilds each word with the same formula sbuf_raw uses, so it can't catch a field in the wrong place. To cover that, I fed the two .long pairs sbuf_raw produces to LLVM's disassembler (llvm-mc 23.1.6 -disassemble -triple=amdgcn--amdpal -mcpu=gfx1013):

0x06,0x0c,0x20,0xf4,0xfc,0xff,0x1f,0xfa  ->  s_buffer_load_dword s48, s[12:15], -0x4            (sbuf_imm_m4, soff 0x7d = null)
0x46,0x0c,0x20,0xf4,0xfc,0xff,0x1f,0x28  ->  s_buffer_load_dword s49, s[12:15], s20 offset:-0x4 (sbuf_soff8_imm_m4)

Both decode to exactly the instruction the row names, so the field layout (sdata at 12:6, sbase>>1 at 5:0, op 8, 21-bit IMM, soffset at 31:25, 0x7d as null) is right. One small option, if you want it: assert these two encodings as literals (0xF4200C06, 0xFA1FFFFC and 0xF4200C46, 0x281FFFFC). That would pin them against a later change to the formula.

No game files. AI-assisted (Claude Code).

@bobcatchris bobcatchris left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on a clean checkout of the head (Windows, Python 3.12, CPU only):

  • python -I tests/test_smem_neg_probe.py — 8/8 OK (2.2 s).
  • Ran the tool itself in translator mode: clean row printout, exit 0. The tool delta is the minimal testability change (main(argv=None) + parse_args(argv)); everything else in the diff is the new test file, additive against main and independent of #3356's oracle plumbing as the body says.
  • The suite covers the parts that needed pinning: seed-table window bounds, the hand-built GFX10 SMEM encodings llvm-mc refuses (including the sbase>>1 truncation), the sOFF -4 wrap and the IMM -4 wrap past NUM_RECORDS, the kernel skeleton's value order, and the CLI path cross-checked row by row. The 77-exit hardware-unavailable case matches the documented behaviour of the other probes.

First coverage for this tool at all; the negative-offset probe rules are now regression-protected. Approving.

AI-assisted.

@UlteriorMotive00

Copy link
Copy Markdown
Contributor

One assertion here locks in a rule the translator no longer has:

self.assertEqual(smp.translator_sbuf(0xFFFFFFFC, 8), [4])          # sOFF -4 wraps to an in-table 4

translator_sbuf still truncates sOFF + IMM to 32 bits. docs/design.md (Scalar/buffer memory, SHADER-336) says s_buffer_load adds sOFF and IMM without wrapping at 32 bits: "sOFF 0xfffffffc + IMM 8 ... give 0 (past NUM_RECORDS 0x100) ... the translator has the same rule in both offset forms since #1686". The constant-offset fold in rdna2spirv.py (~line 9590) says the same ("no 32-bit wrap").

I measured it today on native gfx1036 (Radeon iGPU of a Ryzen 7 9800X3D, Windows driver 32.0.21042.62) with python -I tools/smem_neg_probe.py --oracle hw on main aa2988cc:

sbuf_soffm4_imm8       hw 0x0                        translator 0x5eed0004               DIFFERS
sbuf_soffm4            hw 0x0                        translator 0x0                      ok

So on correct hardware the probe exits 1 because of its own stale model. The other two DIFFERS rows (sbuf_imm_m4, sbuf_soff8_imm_m4) are the bit-20 IMM encodings, which the docs call undefined and say the translator doesn't model. They returned 0x49608035 (wave64 0x48081031) and 0xc105804b, the "neither 0 nor the buffer's words" the docs describe. These rows can't be expected to match anything.

I'd suggest the model (and this test) say [None] for (0xFFFFFFFC, 8), with no & 0xFFFFFFFF. The two undefined rows could print their words without counting as a mismatch. Would you like to fold that into this PR, since you're already in the file? If you'd rather not, I'll do it as a follow-up once this and #3356 land, so the three of us don't conflict. AI-assisted (Claude Code).

@SebSilent

Copy link
Copy Markdown
Author

Confirmed, and folded in, thanks. The model really did still wrap: off &= 0xFFFFFFFF after the sum, while the translator has added zext(sOFF & ~3) + imm in 64 bits since #1686 (the constant-offset path in tools/rdna2spirv.py even cites this issue in a comment). sOFF 0xFFFFFFFC + IMM 8 now predicts 0, the value you measured, so on hardware the sbuf_soffm4_imm8 row should print ok instead of the 0x5eed0004 DIFFER.

The two hand-encoded rows went the way you suggest: NOT_MODELLED = {"sbuf_imm_m4", "sbuf_soff8_imm_m4"}, and --oracle hw prints their words with a not modelled verdict and does not count them, since the bit-20 IMM reads are data the translator does not model at all (SHADER-336). A test pins the set against the encoded words, so a row only lands there if its IMM field really has bit 20.

Two extra no-wrap cases in the rule test: translator_sbuf(0xFFFFFFFC, 8) is [None] (the old [4] was the wrap), and translator_sbuf(0x80000000, 0x80000000) is [None] where the masked sum used to fold back to 0.

One thing you may have missed upstream: main was force-pushed while this PR was open (d0835dd9f), so the branch was rebuilt on the new main as a single commit, 895bc8eac. GitHub now says mergeable, but if the rewrite dropped anything from the old base the diff against d0835dd9f is the truth.

Synthetic checks for the seed value table, the hand-encoded GFX10 SMEM words,
the kernel skeleton, the translator-mode output and the 77 exit contract. The
s_buffer_load model follows the translator's rule since ItsNotRexed#1686: sOFF and IMM are
added without wrapping at 32 bits, so sOFF 0xFFFFFFFC + IMM 8 predicts 0, the
value measured on gfx1036 (SHADER-336). The two hand-encoded rows carry an IMM
with bit 20 set, whose hardware reads the translator does not model at all, so
--oracle hw reports them without counting a mismatch.
@SebSilent

Copy link
Copy Markdown
Author

Rebased onto current main (98fa503f8) and merged the two test files.

main grew tests/test_smem_neg_probe.py from #3756 while this PR was open and my branch added the same path, so the conflict was both-added. Instead of picking a side I kept the upstream file as the shape (SmemNegProbeTest + OracleLifecycleTest) and folded my cases into it. 14 tests now: the union of what both sides covered, plus two new ones for the bit-20 rows.

Two assertions in the upstream file had to change, because the tool's model changed under them:

  • translator_sbuf(0xFFFFFFFC, 8) was [4], now [None]
  • expected()["sbuf_soffm4_imm8"] was [SEED + 4], now [None]

Both were pinning the 32-bit wrap that rdna2spirv dropped in #1686. tools/rdna2spirv.py:10240 says it out loud now: the constant path keeps zext(sOFF & ~3) + imm as a Python int with no 32-bit wrap, and the run-time path forms the same sum in %ulong. So s20 = -4 with offset:8 lands at 0x100000004, past NUM_RECORDS, and the dword reads 0. The old [4] was describing the pre-#1686 translator, which is the bug this PR's tool half is about. Everything else in the upstream file passes against the new model unchanged, including the four oracle lifecycle tests.

Mutation pass over the merged file, each mutant killed:

mutation killed by
put the 32-bit wrap back in translator_sbuf test_translator_sbuf_bounds, test_expected_and_kernel_lines
NOT_MODELLED emptied out test_not_modelled_rows_are_the_bit20_immediates
hw mode silently drops the not-modelled rows test_hw_reports_the_not_modelled_rows_without_matching_them
hw mode scores the not-modelled rows against the model same test
main() ignores the argv it is handed all 5 hw lifecycle tests
sbuf_raw drops the sbase >> 1 half test_sbuf_raw_encoding
seed window widened by one byte test_seed_bounds

The two hw-mode rows are new: before this pass nothing checked that the bit-20 rows are still printed, or that they do not count as a mismatch.

python -I -B tests/test_smem_neg_probe.py: 14 tests OK. compileall clean on the tool, tools/hw_oracle/runner.py and the test file.

AI-assisted.

@SebSilent

Copy link
Copy Markdown
Author

Both folded in already at 7e3771a. translator_sbuf keeps the sum wide (no & 0xFFFFFFFF on the add) and bound-checks it, so (0xFFFFFFFC, 8) gives [None]; the test asserts that and cites rdna2spirv's no-32-bit-wrap rule. The two bit-20 IMM rows sit in a NOT_MODELLED set: --oracle hw prints their words as 'not modelled' and skips the mismatch count, so the probe no longer exits 1 on its own stale model. 14 tests OK. Thanks for the hardware measurement, it lines up.

@kapitche

Copy link
Copy Markdown
Contributor

Tested 7e3771a merged with main 98fa503 on Linux (WSL2) Python 3.12.16 and native Windows 11 Python 3.13.1: pass

  • python -I tests/test_smem_neg_probe.py: 14 tests OK on Linux and 14 OK on Windows (clean worktree at the PR head, which already contains main); pytest tests/test_smem_neg_probe.py on Linux: 14 passed. python -I tools/smem_neg_probe.py (translator mode) exits 0 and prints all 10 rows; the new --oracle hw subprocess test returns 77 on both machines (no AMD oracle).
  • Negative control: with tools/smem_neg_probe.py restored from main, 2 tests fail and 2 error (test_translator_sbuf_bounds, test_expected_and_kernel_lines, and errors in test_not_modelled_rows_are_the_bit20_immediates and test_hw_reports_the_not_modelled_rows_without_matching_them, which need NOT_MODELLED); with the PR file all pass. Restored afterwards.
  • Read tools/rdna2spirv.py around 10215-10260: the s_buffer_load IMM is kept as an unsigned 32-bit value (imm &= 0xFFFFFFFF, then & ~3) and the sum with sOFF & ~3 is formed without a 32-bit wrap, which is what the updated translator_sbuf now models, so (sOFF=0xFFFFFFFC, IMM=8) is out of range ([None]) as the new test asserts.
  • Not verified here: the hardware behaviour of the two bit-20 IMM rows (no AMD device on this machine). One comment-only inaccuracy, no effect on results: in tests/test_smem_neg_probe.py the line translator_sbuf(None, -4) # 0x1ffffc, past the table -- with the IMM kept as a 32-bit value the offset is 0xfffffffc, still past the table.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants