Skip to content

Tests: generate RDNA2 programs for compile-time measurements (#1269) - #1763

Open
BetterJdue wants to merge 2 commits into
xcl-dev:mainfrom
BetterJdue:compile-bench-gen
Open

BetterJdue wants to merge 2 commits into
xcl-dev:mainfrom
BetterJdue:compile-bench-gen

Conversation

@BetterJdue

Copy link
Copy Markdown

A generator of deterministic RDNA2 programs for the synthetic compile-time benchmark of #1269: no game data, so it runs anywhere, CI included. Test-only: one commit in crates/xcl/tests/compile_bench/, nothing in crates/xcl/src. Nothing is timed yet; the DXC measurement is the next PR, stacked on this one.

Part of #1269

What is in it

  • crates/xcl/tests/compile_bench/gen.rs: program(&Knobs) assembles a program with rdna2::encode from a seed and knobs: the number of operations, divergent ifs (v_cmp → s_and_saveexec → s_cbranch_execz) and how deep they nest, uniform loops (SGPR counter, backward s_cbranch_scc1) and how deep they nest, wave32 or wave64, the VGPR and SGPR pools, and the share of LDS and memory accesses. Knobs that would give more operations than asked for are refused.
  • crates/xcl/tests/compile_bench/main.rs: 7 tests, CPU only. They check in the disassembly, not in the generator: the operation count, how deep the ifs and loops nest, where every branch lands, the EXEC instructions of each wave size, the LDS and memory shares and the VGPR pool; 24 knob combinations translate completely with structured control flow on and off.

Tested

  • AMD Radeon RX 6700 (RDNA2), driver 32.0.21045.29003, Ryzen 5 3600, Windows 11 Pro 26300, Rust 1.100.0 MSVC, debug.
  • cargo test -p xcl --test compile_bench: 7 passed, 0 failed, 0 ignored.
  • cargo test --workspace --no-fail-fast -- --test-threads=4 on 5ae62c5: 755 passed, 3 failed, 41 ignored (20 binaries); no SKIPPED tests. The failures are placed_title_textures_share_vram_and_preserve_pixels ("3D 64x64x64 4 mips: subresource 2 mismatch") and two tests that then fail on the lock it poisons; the first fails alone on main 930e8b4 too (4 of 4 runs). The same failure (same message, RDNA2, same driver) is reported fixed by D3D12.X: map and unmap 3D texture mips separately #1028 on an RX 6600, RX 6750 XT and RX 6900 XT; D3D12.X: map and unmap 3D texture mips separately #1028 was not run here.
  • Negative control: 10 mutations of the generator (seed ignored, loops not nested, loop and execz branch offsets off by one, wrong EXEC width, if depth ignored, VGPR pool too large, LDS share ignored, operations miscounted, the operation-count check removed); each makes at least one test fail.

Regression run

  • Not needed: test-only change, nothing in crates/xcl/src.

Not tested

  • scripts/smoke.ps1 or any retail title (no game data here; the change does not touch the runtime).
  • NVIDIA, wave32-only GPUs; release build.

AI use

  • Claude Code with Claude Opus 5.5: wrote the generator, the tests and this description, and ran the tests on the machine above; reviewed by an independent Claude Code agent before posting.

🤖 Generated with Claude Code

…#1269)

The synthetic compile-time benchmark of xcl-dev#1269 needs console programs that
anyone can build, CI included, without game data. This adds the generator
it will measure: deterministic RDNA2 programs, assembled with
rdna2::encode, from a seed and knobs for the number of operations,
divergent ifs (v_cmp, s_and_saveexec, s_cbranch_execz) and how deep they
nest, uniform loops (SGPR counter, backward s_cbranch_scc1) and how deep
they nest, wave32 or wave64, the VGPR and SGPR pools, and the share of LDS
and memory accesses. Knobs that would give more operations than asked for
(fewer than two per loop level) are refused. Nothing is timed yet; that
comes next, with DXC.

The tests count what each knob does in the disassembly of the program,
not in the generator: the operations, how deep the ifs and loops nest,
that every s_cbranch_execz lands on its EXEC restore and every loop
branch on the instruction after its counter is set, the EXEC instructions
of each wave size, the LDS and memory shares, the highest VGPR. Every
knob combination the tests use translates completely, structured and as
the guarded block chain, with as many instructions as the disassembly.

Negative control: ten mutations of the generator (seed ignored, loops
not nested, loop and execz branch offsets off by one, wave32 using the
64-bit EXEC instructions, if depth ignored, VGPR pool one too large, LDS
share ignored, operations miscounted, the operation-count check removed)
each make at least one test fail.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ivall ivall added the goal:world-load Bears on the retail title getting past world loading label Nov 24, 2026
@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, ~23 ignored. (Intermittent single-test flake under parallel scheduling; clean on single-threaded rerun — same flake seen on unmodified main.)

Thanks!

Large synthetic loop bodies can exceed the signed SOPP displacement. The narrowing cast silently wraps the back edge and produces a different program. Check the displacement before encoding it; cover rejection and a large representable loop.
@anes66

anes66 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Large loop bodies silently wrap their SOPP back edge on exact head 5ae62c52c218f2ee7ba36e18a83769b5d5ae13a9. nested_loop narrows the negative dword displacement with back as i16, so benchmark knobs can produce a different control-flow graph instead of rejecting an unencodable loop.

Patch: 0f257d1, directly on that head. It uses i16::try_from before encoding, following the generator's existing panic-on-invalid-knobs contract. Two test files only; no runtime changes or competing PR.

Tested

  • Windows 10 Pro 19045, Ryzen 7 9800X3D, Rust 1.100.0 MSVC, debug. CPU-only, no DXC or GPU execution.
  • The new rejection test uses ops=100000, one loop, no divergent regions/LDS/memory operations. Before the check: 0 passed / 1 failed, because the generator did not reject the out-of-range branch. After: it rejects with loop body exceeds SOPP branch range.
  • A second test disassembles a representable 10000-operation program, verifies the backward branch lands immediately after its counter initialization, and checks operation count and loop shape.
  • cargo test --locked -p xcl --test compile_bench -- --nocapture --test-threads=1: 9 passed / 0 failed / 0 ignored, no skips. This also runs the existing 24 knob combinations through both structured and unstructured CPU translation.

Not tested
The full workspace/GPU suite, release, retail inputs, or integration onto newer main. Oversized loops are rejected rather than expanded into far jumps; the encoded loop range remains the ISA's signed 16-bit dword range.

AI use
Codex (GPT-6; exact variant unavailable) found the narrowing issue, wrote the check and regression tests, ran CPU validation and prepared this report.

CPU integration follow-up

The generator at 0f257d19e5b3950ebf00b61090ab78d25fc45ea4, including the accepted branch-range fix, was also checked in an integration tree based on main 06c9f55 with #1906, #1860, the proposed #2076 rebase, #1972, #2322 and #2124. Test-only addition 0f733a4 is on that integration tree, not directly on this PR's head.

Windows 10 Pro 19045, Ryzen 7 9800X3D, Rust 1.100.0 MSVC, debug, SDK DXC/DXIL 1.9.2602.17:

  • 64 generated programs / 288 validated direct-DXIL variants: eight seeds, four 300-operation profiles (straight ALU, divergent branches, nested loops, mixed LDS/global memory), wave32/native wave64/split wave64, and plain/flat-compact/nested-compact emission. Three isolated children each validate 96 variants.
  • SDK disassembly verifies the requested forms. A representative wave32 case has no xcl_blocks table and 0 switches in plain form, the table and 1 switch flat, and the table and 8 switches nested. The parent requires a larger switch count for nested dispatch, so merely ignoring the mode is insufficient.
  • cargo test --locked -p xcl --test compile_bench -j 2 -- --nocapture --test-threads=1: 10 passed / 0 failed / 0 ignored / 0 runtime skips, including the original nine generator tests. The two should_panic rejection tests pass as intended.
  • Negative control: asking the nested child for flat dispatch still validates its 96 containers but fails the parent with switch counts [0, 1, 1]. Restored before the final passing run and commit.

No GPU capability query, pipeline build or shader execution; these generated memory accesses were never run. Not tested: output parity, timings, release, full workspace suite, a title or this added matrix on the standalone old PR head. This checks compiler/validator coverage, not compilation-speed improvement.

@raylitebull

Copy link
Copy Markdown
Member

\CI\ workflow approved for \5ae62c5\ (\crates/xcl/tests/compile_bench/{gen.rs,main.rs}). Note @anes66's finding above on
ested_loop\ (\�ack as i16\ silently wrapping if \ops\ is large enough to exceed SOPP's signed 16-bit dword range); using \i16::try_from\ + obtaining an independent NVIDIA check on \5ae62c5\ (plus \Build and test\ green) will make this ready.

@BetterJdue

Copy link
Copy Markdown
Author

Thanks @anes66: your commit is pulled into this PR unchanged, so the head is now 0f257d1 (a fast-forward on 5ae62c5, author kept).

Tested on 0f257d1: AMD Radeon RX 6700 (RDNA2), driver 32.0.21045.29003; Ryzen 5 3600; 32 GB RAM; Windows 11 Pro 26300; Rust 1.100.0 MSVC; DXC 1.9.2602.17; debug.

  • cargo test --workspace --no-fail-fast -- --test-threads=4: 757 passed, 3 failed, 41 ignored, no SKIPPED. The 3 failures are the RDNA2 texture test (placed_title_textures_share_vram_and_preserve_pixels, "subresource 2 mismatch", also failing alone on main b9b6ea6) and the two tests that fail on the lock it poisons; none is in this PR's files.
  • cargo test -p xcl --test compile_bench: 9 passed, 0 failed.
  • With back as i16 put back, oversized_loop_branches_are_refused fails; with the fix it passes.

An independent run on NVIDIA on the new head would still help: DavidM1911's RTX 3060 Ti report above was on 5ae62c5 merged with main.

AI use: Claude Code (Claude Opus 5.5) ran these tests and wrote this comment.

AI-assisted verification using synthetic inputs.

@upipibi

upipibi commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

The independent NVIDIA run asked for above, on the new head 0f257d1: RTX 3070 (Ampere, driver 616.56), Windows 11, debug.

  • cargo test -p xcl --test compile_bench: 9 passed, 0 failed (oversized_loop_branches_are_refused included).
  • cargo test -p xcl --lib -- --test-threads=4: 706 passed, 1 failed, 21 ignored. The failure is hle::xaudio2::tests::xma2_source_voice_decodes_and_manages_lifetime (0 samples played where 28679 were expected, xaudio2.rs:1611). That test waits a fixed time for audio playback, and the machine was under heavy parallel load during this run. This PR doesn't touch XAudio2. Tests: the XAudio2 voice tests wait 30 s for playback and skip a slow audio device (#1487) #1600 gives those voice tests a longer wait.

Not run: the whole workspace.

AI-assisted (Claude Code). Synthetic tests only.

@vakaobr

vakaobr commented Nov 25, 2026

Copy link
Copy Markdown

@BetterJdue one knob I needed for running the programs rather than only compiling them: race_free. Suggesting it here so the harness keeps using this generator instead of a fork of it.

Why. The programs let lanes race: a global_store_dword to lane * 4 + o1 and a later global_load_dword of lane * 4 + o2 by another lane, s_load_dword of addresses a vector store just wrote, and ds_read_b32 of a lane's LDS dword before anything wrote it. On the console a wave runs those in program order; translated to one GPU thread per lane, D3D12 gives no such order. With data in memory, 59 of 1000 seeds gave different results on two runs of the HLSL path on D3DMetal, and 48 more differed between the HLSL path and #1426's direct path for the same reason. With the knob on: 1000 tested, 0 differences, 0 unstable (details on #1269).

What it does (off by default):

  • global and scalar loads read only bytes 0..1024 (lane * 4 + offset ≤ 63 × 4 + 192 × 4 = 1020),
  • every global store writes the lane's own dword at byte 1024 + lane * 4,
  • the program starts with ds_write_b32 v1, v0, so each lane's LDS dword is set before it is read.

With the knob off the programs are byte for byte the same as 5ae62c5: the store still draws its data register before its offset, as the array literal did.

@@ pub struct Knobs {
     pub lds_pct: u32,
     pub mem_pct: u32,
+    /// programs whose result does not depend on the order lanes see each other's memory
+    /// accesses, for running and comparing them: global loads and scalar loads read only bytes
+    /// 0..1024, every global store writes the lane's own dword at byte 1024 + lane * 4, and the
+    /// lane's LDS dword is written before anything reads it
+    pub race_free: bool,
 }
@@ impl Default for Knobs {
-        Knobs { seed: 1, ops: 200, if_depth: 2, divergent_pct: 10, loops: 0, loop_depth: 1, loop_trips: 4, wave64: true, vgprs: 16, sgprs: 8, lds_pct: 5, mem_pct: 10 }
+        Knobs { seed: 1, ops: 200, if_depth: 2, divergent_pct: 10, loops: 0, loop_depth: 1, loop_trips: 4, wave64: true, vgprs: 16, sgprs: 8, lds_pct: 5, mem_pct: 10, race_free: false }
@@ fn op(&mut self) -> Vec<u8> {
-                0 => inst("GLOBAL_LOAD_DWORD", "ENC_FLAT_GLBL", &[("VDST", self.vgpr()), ("ADDR", 1), ("SADDR", 2), ("OFFSET", 4 * self.rng.below(256))], None),
-                1 => inst("GLOBAL_STORE_DWORD", "ENC_FLAT_GLBL", &[("ADDR", 1), ("DATA", self.vgpr()), ("SADDR", 2), ("OFFSET", 4 * self.rng.below(256))], None),
+                // race-free: lane * 4 + offset stays below byte 1024 (63 * 4 + 192 * 4 = 1020)
+                0 => inst("GLOBAL_LOAD_DWORD", "ENC_FLAT_GLBL", &[("VDST", self.vgpr()), ("ADDR", 1), ("SADDR", 2), ("OFFSET", 4 * self.rng.below(if self.k.race_free { 193 } else { 256 }))], None),
+                1 => {
+                    // the data register is drawn before the offset, as before, so programs without
+                    // race_free stay the same
+                    let data = self.vgpr();
+                    let offset = if self.k.race_free { 1024 } else { 4 * self.rng.below(256) };
+                    inst("GLOBAL_STORE_DWORD", "ENC_FLAT_GLBL", &[("ADDR", 1), ("DATA", data), ("SADDR", 2), ("OFFSET", offset)], None)
+                }
@@ pub fn program(k: &Knobs) -> Vec<u8> {
     let mut v = inst("V_LSHLREV_B32", "ENC_VOP2", &[("VDST", 1), ("SRC0", 130), ("VSRC1", 0)], None); // v1 = lane * 4
+    if k.race_free {
+        v.extend(inst("DS_WRITE_B32", "ENC_DS", &[("ADDR", 1), ("DATA0", 0)], None)); // the lane's LDS dword = lane id
+    }

It's commit 5fa8e36 on vakaobr/xcl tests/diff-harness, on top of your 5ae62c5 (kept as is; that branch is on main c4d37be). Take it into #1763 if you agree, or I'll stack it as its own PR after #1763; either is fine with me.

Tested: cargo test -p xcl --test compile_bench with the knob in: 7 passed, 0 failed, on the Mac (M5 Pro, CrossOver 26.3 / D3DMetal, Rust 1.100.0 x86_64-pc-windows-gnu, debug). Not tested: Windows.

AI use: Claude Code (Claude Opus 5.5) wrote the knob and this comment.

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

Labels

goal:world-load Bears on the retail title getting past world loading

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants