shader: run translated compute shaders on the CPU in tests (opt + lli) #62

Open
github-bot wants to merge 4 commits from github-bot/gh-62 into main AGit
Owner

@⁠lcg007xd-c on GitHub · 2026-11-25 21:26 UTC · original
lcg007xd-c:shader-cpu-exec → main · +1068 −1 · 2 files · 4 commits · open

What this adds

Today a lowered instruction's arithmetic is only checked by valu_tests.rs, which needs Windows, a GPU (or WARP) and dxil.dll. On Linux, the tests can only see that a program translates, not what it computes.

The DXIL in a translated container is LLVM bitcode, so LLVM's own tools can run it on the CPU:

  1. opt -data-layout=<host> -mtriple=x86_64-unknown-linux-gnu reads the DXIL bitcode and retargets it (DXIL's pointers are 32 bits).
  2. llvm-link adds a small IR runtime (the RUNTIME string in cpu_tests.rs) that defines the dx.op intrinsics these programs call: handles, cbufferLoadLegacy, rawBufferStore, unary/binary/tertiary float ops, bit scans, dot4AddPacked, f16 conversions, thread ids. An opcode it does not know stops the run and names the opcode.
  3. lli runs it, and the test reads back the UAV dwords the shader stored.

The change

  • emit/cpu_tests.rs (new, #[cfg(test)]): programs have the same shape as valu_tests.rs' build_valu_cs. Four root constants arrive in s0..s3 and are copied to v1..v4, the body leaves its result in v0, and v0 is stored to dword 0. So an expectation written for the CPU also holds for the GPU tests, and the reverse. expect(name, body, &[(inputs, want)]) is all a new case needs.
  • D3D conversion semantics: before the run, each fptosi/fptoui becomes llvm.fpto[su]i.sat. In LLVM IR an out-of-range conversion is poison (x86 gives 0x80000000). In DXIL it has D3D's meaning, which the translation relies on (see the comment at v_cvt_i32_f32): it saturates, and NaN gives 0.
  • Tools: found via VICTORY_LLVM_BIN, then PATH, then the newest /usr/lib/llvm-N/bin (Debian/Ubuntu layout). If they are missing, or on a host other than x86-64 Linux, each test reports VICTORY TEST SKIPPED: … through test_skip, and fails under VICTORY_STRICT_TESTS=1. test_skip is now compiled on every host (it was cfg(windows)).
  • The runtime is a Rust raw string, so the repository policy (xtask admits only listed text extensions) is unchanged.

First cases (all pass on main)

Float add/mul/clamp/neg, integer add/shift/mul_hi/bfe/med3, v_ffbh_u32/v_ffbl_b32/v_ffbh_i32 (including 0 and −1), v_cvt_i32_f32/v_cvt_u32_f32 (saturation and NaN), v_rndne_f32 ties, v_cvt_f16_f32 rounding to nearest even (and overflow to inf), and v_med3_f32 with NaN. The NaN cases are copied from valu_tests.rs' hardware cases. It also covers the packed dot products, including #44's v_dot8_i32_i4 cases and clamped v_dot4_i32_i8 saturating both ways.

It catches wrong lowerings. I changed v_bfe_i32 to extract unsigned, and integer_operations_compute_what_they_should failed: v_bfe_i32 case 0 [abcd1234, 0000001c, 00000004, 00000000]: got 0x0000000a, want 0xfffffffa.

What this cannot check

  • FMad: the runtime computes FMad unfused. A driver may fuse it, so v_fma_f32 (documented as a shortcut to FMad) is not checked here.
  • Lanes: everything runs as one lane, so wave operations and cross-lane exchanges are out of scope.
  • Host behaviour: denormal flushing and a vendor's own f16 rounding are not modelled. Those stay with valu_tests.rs.

Tests run (Linux, Rust 1.98.1, LLVM 23.1.5)

  • cargo test -p victory-shader --lib --locked: 198 passed, 0 failed, 2 ignored (11 of them new).
  • With VICTORY_LLVM_BIN=/nonexistent, the CPU cases print VICTORY TEST SKIPPED: LLVM's opt, llvm-link and lli were not found (set VICTORY_LLVM_BIN); CPU run skipped.
  • Portable clippy (-p victory-core -p victory-pe -p victory-pipeline -p victory-shader -p victory-formats -p victory-game -p xtask --all-targets -- -D warnings): clean.
  • CARGO_FEATURE_PURE=1 cargo clippy -p victory-shader --all-targets --target x86_64-pc-windows-msvc -- -D warnings: clean.
  • Each run spawns three short processes: about 0.8 s for the 10 tests.

Not run here: Windows itself, where these tests skip by design. I also did not check whether the GitHub Ubuntu runner has /usr/lib/llvm-N/bin. If it does, the Linux CI job runs these cases; otherwise they skip there.

🤖 Generated with Claude Code

**@⁠lcg007xd-c** on GitHub · 2026-11-25 21:26 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/62) `lcg007xd-c:shader-cpu-exec` → `main` · +1068 −1 · 2 files · 4 commits · open ## What this adds Today a lowered instruction's arithmetic is only checked by `valu_tests.rs`, which needs Windows, a GPU (or WARP) and `dxil.dll`. On Linux, the tests can only see that a program *translates*, not what it computes. The DXIL in a translated container is LLVM bitcode, so LLVM's own tools can run it on the CPU: 1. `opt -data-layout=<host> -mtriple=x86_64-unknown-linux-gnu` reads the DXIL bitcode and retargets it (DXIL's pointers are 32 bits). 2. `llvm-link` adds a small IR runtime (the `RUNTIME` string in `cpu_tests.rs`) that defines the `dx.op` intrinsics these programs call: handles, `cbufferLoadLegacy`, `rawBufferStore`, `unary`/`binary`/`tertiary` float ops, bit scans, `dot4AddPacked`, f16 conversions, thread ids. An opcode it does not know stops the run and names the opcode. 3. `lli` runs it, and the test reads back the UAV dwords the shader stored. ## The change - **`emit/cpu_tests.rs`** (new, `#[cfg(test)]`): programs have the same shape as `valu_tests.rs`' `build_valu_cs`. Four root constants arrive in `s0..s3` and are copied to `v1..v4`, the body leaves its result in `v0`, and `v0` is stored to dword 0. So an expectation written for the CPU also holds for the GPU tests, and the reverse. `expect(name, body, &[(inputs, want)])` is all a new case needs. - **D3D conversion semantics:** before the run, each `fptosi`/`fptoui` becomes `llvm.fpto[su]i.sat`. In LLVM IR an out-of-range conversion is poison (x86 gives `0x80000000`). In DXIL it has D3D's meaning, which the translation relies on (see the comment at `v_cvt_i32_f32`): it saturates, and NaN gives 0. - **Tools:** found via `VICTORY_LLVM_BIN`, then `PATH`, then the newest `/usr/lib/llvm-N/bin` (Debian/Ubuntu layout). If they are missing, or on a host other than x86-64 Linux, each test reports `VICTORY TEST SKIPPED: …` through `test_skip`, and fails under `VICTORY_STRICT_TESTS=1`. `test_skip` is now compiled on every host (it was `cfg(windows)`). - The runtime is a Rust raw string, so the repository policy (`xtask` admits only listed text extensions) is unchanged. ## First cases (all pass on main) Float add/mul/clamp/neg, integer add/shift/`mul_hi`/`bfe`/`med3`, `v_ffbh_u32`/`v_ffbl_b32`/`v_ffbh_i32` (including 0 and −1), `v_cvt_i32_f32`/`v_cvt_u32_f32` (saturation and NaN), `v_rndne_f32` ties, `v_cvt_f16_f32` rounding to nearest even (and overflow to inf), and `v_med3_f32` with NaN. The NaN cases are copied from `valu_tests.rs`' hardware cases. It also covers the packed dot products, including #44's `v_dot8_i32_i4` cases and clamped `v_dot4_i32_i8` saturating both ways. **It catches wrong lowerings.** I changed `v_bfe_i32` to extract unsigned, and `integer_operations_compute_what_they_should` failed: `v_bfe_i32 case 0 [abcd1234, 0000001c, 00000004, 00000000]: got 0x0000000a, want 0xfffffffa`. ## What this cannot check - **FMad:** the runtime computes FMad unfused. A driver may fuse it, so `v_fma_f32` (documented as a shortcut to FMad) is not checked here. - **Lanes:** everything runs as one lane, so wave operations and cross-lane exchanges are out of scope. - **Host behaviour:** denormal flushing and a vendor's own f16 rounding are not modelled. Those stay with `valu_tests.rs`. ## Tests run (Linux, Rust 1.98.1, LLVM 23.1.5) - `cargo test -p victory-shader --lib --locked`: **198 passed**, 0 failed, 2 ignored (11 of them new). - With `VICTORY_LLVM_BIN=/nonexistent`, the CPU cases print `VICTORY TEST SKIPPED: LLVM's opt, llvm-link and lli were not found (set VICTORY_LLVM_BIN); CPU run skipped`. - Portable clippy (`-p victory-core -p victory-pe -p victory-pipeline -p victory-shader -p victory-formats -p victory-game -p xtask --all-targets -- -D warnings`): clean. - `CARGO_FEATURE_PURE=1 cargo clippy -p victory-shader --all-targets --target x86_64-pc-windows-msvc -- -D warnings`: clean. - Each run spawns three short processes: about 0.8 s for the 10 tests. **Not run here:** Windows itself, where these tests skip by design. I also did not check whether the GitHub Ubuntu runner has `/usr/lib/llvm-N/bin`. If it does, the Linux CI job runs these cases; otherwise they skip there. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The DXIL a translation emits is LLVM bitcode, so LLVM's own tools can
run it: `opt` gives it the host's data layout (DXIL's pointers are 32
bits), `llvm-link` adds a small runtime standing in for the dx.op
intrinsics, and `lli` runs it. emit/cpu_tests.rs does that for programs
of valu_tests.rs' shape (root constants in s0..s3, result stored from
v0), so what a lowered instruction computes can be checked on Linux,
without a GPU or dxil.dll.

Float-to-integer conversions are made saturating before the run: an
out-of-range fptosi is poison in LLVM IR, but in DXIL it has D3D's
meaning (saturate, NaN gives 0), which the translation relies on.

The tools come from VICTORY_LLVM_BIN, PATH or /usr/lib/llvm-N/bin;
without them, or off x86-64 Linux, the tests say they were skipped.
test_skip is now built on every host for that.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The repository policy (xtask) admits only listed text extensions, and
.ll is not one; the runtime is now a raw string next to its tests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
So translations that compute in doubles run on the CPU too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
So the module list's line next to `mod container;` stays free for the
other test modules open PRs add there (#81).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:42:51 +00:00
Author
Owner

@⁠lcg007xd-c on GitHub · 2026-11-25 22:41 UTC · original

Added a third commit: the runtime now also defines MakeDouble/SplitDouble and the f64 Fma/DMax/DMin, so translations that compute in doubles run on the CPU too (used to check #99's f64 arithmetic). Tests: cargo test -p victory-shader --lib --locked 198 passed.

**@⁠lcg007xd-c** on GitHub · 2026-11-25 22:41 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/62#issuecomment-7176669006) Added a third commit: the runtime now also defines `MakeDouble`/`SplitDouble` and the f64 `Fma`/`DMax`/`DMin`, so translations that compute in doubles run on the CPU too (used to check #99's f64 arithmetic). Tests: `cargo test -p victory-shader --lib --locked` 198 passed.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/62/head:github-bot/gh-62'
git switch 'github-bot/gh-62'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-62'
git switch 'github-bot/gh-62'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-62'
git switch 'github-bot/gh-62'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-62'
git switch 'main'
git merge --squash 'github-bot/gh-62'
git switch 'main'
git merge --ff-only 'github-bot/gh-62'
git switch 'main'
git merge 'github-bot/gh-62'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.