shader: lower s_absdiff_i32 #94

Open
github-bot wants to merge 0 commits from github-bot/gh-94 into main AGit
Owner

@⁠lcg007xd-c on GitHub · 2026-11-25 22:27 UTC · original
lcg007xd-c:shader-s-absdiff → main · +70 −0 · 2 files · 1 commits · open

What is broken

s_absdiff_i32 (SOP2 0x2C) decodes, but a program using it fails to translate with s_absdiff_i32 is not supported. I found it in the scalar sweep through the CPU harness of #62, the same sweep as #89, #91 and #92.

Semantics

AMD's RDNA2 ISA (§12.1, opcode 44): D.i = S0.i - S1.i; if (D.i < 0) D.i = -D.i, and SCC = (D != 0). The difference is a 32-bit one and wraps. So, for example, |0x80000000 - 0| stays 0x80000000, as the hardware's two's-complement negation leaves it.

The change (emit/lower.rs, in sop)

  • Value: subtract, then negate when the result is negative.
  • SCC: SCC = (D != 0) is what sop's shared SCC path already sets, and the IR already lists absdiff among the SCC writers.

Tests

  • emit::lower::tests::scalar_absolute_difference_translates (every host): the encoding decodes. Two programs translate: one that stores the value, and one that stores SCC through s_cselect_b32. Without the lower.rs change it fails: s_absdiff_i32 [960c0100, 7e00020c]: GFX10 code at 0x24: s_absdiff_i32 is not supported.
  • valu_tests::scalar_absolute_difference_matches_rdna2 (Windows/GPU): the value and SCC for five pairs, covering both orders, a negative operand, equal values (SCC 0), and INT_MIN − 0.
  • CPU run: I ran these on Linux through the CPU harness of #62, which runs the emitted DXIL with LLVM's lli. I compared the value and SCC against Rust's wrapping_sub(..).wrapping_abs() on all 81 pairs of 9 edge values. All match.

Run here (Linux, Rust 1.98.1):

  • cargo test -p victory-shader --lib --locked: 188 passed, 0 failed, 2 ignored.
  • Portable clippy and CARGO_FEATURE_PURE=1 cargo clippy -p victory-shader --all-targets --target x86_64-pc-windows-msvc -- -D warnings: clean.

Not run here: the GPU case, since I have no Windows machine.

🤖 Generated with Claude Code

**@⁠lcg007xd-c** on GitHub · 2026-11-25 22:27 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/94) `lcg007xd-c:shader-s-absdiff` → `main` · +70 −0 · 2 files · 1 commits · open ## What is broken `s_absdiff_i32` (SOP2 **0x2C**) decodes, but a program using it fails to translate with `s_absdiff_i32 is not supported`. I found it in the scalar sweep through the CPU harness of #62, the same sweep as #89, #91 and #92. ## Semantics AMD's RDNA2 ISA (§12.1, opcode 44): `D.i = S0.i - S1.i; if (D.i < 0) D.i = -D.i`, and `SCC = (D != 0)`. The difference is a 32-bit one and wraps. So, for example, `|0x80000000 - 0|` stays `0x80000000`, as the hardware's two's-complement negation leaves it. ## The change (`emit/lower.rs`, in `sop`) - **Value:** subtract, then negate when the result is negative. - **SCC:** `SCC = (D != 0)` is what `sop`'s shared SCC path already sets, and the IR already lists `absdiff` among the SCC writers. ## Tests - **`emit::lower::tests::scalar_absolute_difference_translates`** (every host): the encoding decodes. Two programs translate: one that **stores** the value, and one that stores SCC through `s_cselect_b32`. Without the `lower.rs` change it fails: `s_absdiff_i32 [960c0100, 7e00020c]: GFX10 code at 0x24: s_absdiff_i32 is not supported`. - **`valu_tests::scalar_absolute_difference_matches_rdna2`** (Windows/GPU): the value and SCC for five pairs, covering both orders, a negative operand, equal values (SCC 0), and `INT_MIN − 0`. - **CPU run:** I ran these on Linux through the CPU harness of #62, which runs the emitted DXIL with LLVM's `lli`. I compared the value and SCC against Rust's `wrapping_sub(..).wrapping_abs()` on all 81 pairs of 9 edge values. All match. **Run here** (Linux, Rust 1.98.1): - `cargo test -p victory-shader --lib --locked`: 188 passed, 0 failed, 2 ignored. - Portable clippy and `CARGO_FEATURE_PURE=1 cargo clippy -p victory-shader --all-targets --target x86_64-pc-windows-msvc -- -D warnings`: clean. **Not run here:** the GPU case, since I have no Windows machine. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
SOP2 0x2C decoded but failed to translate ("s_absdiff_i32 is not
supported"). AMD's RDNA2 ISA: D.i = S0.i - S1.i, negated if negative
(the 32-bit difference wraps, so |INT_MIN| stays 0x80000000), and
SCC = (D != 0), which the shared SCC path already gives.

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

@⁠lmouz823-lang on GitHub · 2026-11-28 05:31 UTC · original

I can take native Windows GPU validation at exact head 3a7b9d9c9da1e6bd47ba0917779380c4a8b854a3. I will run both existing exact tests and add focused fixed-expectation coverage for wrapped signed-difference boundaries and SCC with incoming flags 0/1. I will verify a temporary negative control and restored-source recompilation on RTX 4060 Laptop and WARP, with an explicitly loaded SDK validator. Exact counts, skipped cases and remaining native AMD/game scope will accompany the small patch. AI-assisted with Codex.

**@⁠lmouz823-lang** on GitHub · 2026-11-28 05:31 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/94#issuecomment-7234718053) I can take native Windows GPU validation at exact head `3a7b9d9c9da1e6bd47ba0917779380c4a8b854a3`. I will run both existing exact tests and add focused fixed-expectation coverage for wrapped signed-difference boundaries and SCC with incoming flags 0/1. I will verify a temporary negative control and restored-source recompilation on RTX 4060 Laptop and WARP, with an explicitly loaded SDK validator. Exact counts, skipped cases and remaining native AMD/game scope will accompany the small patch. AI-assisted with Codex.
Author
Owner

@⁠lmouz823-lang on GitHub · 2026-11-28 05:42 UTC · original

Native Windows GPU validation completed for exact head 3a7b9d9c9da1e6bd47ba0917779380c4a8b854a3. The existing translation and result regressions each execute exactly one test and pass. The original GPU test performs 20 dispatches across RTX 4060 Laptop and WARP.

I added a fixed-expectation test with 12 pairs and incoming SCC both 0 and 1. It covers equal zero/INT_MIN/INT_MAX, both orders of INT_MIN versus INT_MAX, INT_MIN versus 1 and -1, INT_MAX versus -1, and zero versus INT_MIN. Value and outgoing SCC are read separately, giving 96 passing dispatches across the two backends. The table follows AMD's RDNA2 ISA, SOP2 opcode 44, printed page 105. Subtraction wraps at 32 bits before the conditional negation, and the resulting zero/nonzero value replaces SCC.

A temporary negative control changes the sign decision from difference < 0 to operand0 < operand1. The original five-pair GPU test still passes. The new test fails on hardware for INT_MIN versus INT_MAX, returning 0xffffffff where the fixed table expects 1. Its exact selection runs one test and exits 101. I then restored lower.rs byte-for-byte, removed the temporary diagnostics and confirmed actual recompilation before the final three exact selections pass again. Those selections have zero failed or ignored tests and no skip diagnostics.

The explicitly selected SDK dxil.dll 1.8.2502.11 successfully created IDxcValidator. Temporary success markers after loading and after validation plus the container-hash assertion confirmed both new modules and both original modules passed those checks. Those markers are absent from the final patch and runs.

The patch adds one 42-line test function. It also relocates the existing two-line negative-zero doc comment from the absolute-difference test to max_and_min_order_negative_zero_below_positive_zero, which it describes. Removing the new test and reversing that comment move reproduces the head source text exactly. Independent review checked the fixed values, encodings, SCC registers, counts and restoration evidence.

One initial unchanged-head release compilation ended with a rustc access violation (0xc0000005) before any test executed. The failure log was retained. An immediate retry with identical source and configuration compiled successfully in 1m01s, and all subsequent diagnostic, mutation and restored-source builds completed. The cause of that first compiler failure remains undetermined.

Environment was Windows 11 Pro build 26200, i9-14900HX, NVIDIA 610.88 and Rust 1.98.1 MSVC, release. Package Clippy with --all-targets -- -D warnings, asset policy, new-function rustfmt and git diff --check pass. Whole-file rustfmt exits 1 on the original and patched files with the same 17 existing formatting hunks after file/line headers are removed. Native AMD execution, game behavior, other platforms and full shader/workspace suites remain unverified. Coverage is limited to the listed synthetic patterns and SCC/wrapping boundaries.

Reproduction uses an installed SDK validator and the project's Windows build prerequisites.

$env:VICTORY_STRICT_TESTS = '1'
$env:VICTORY_DXIL = 'C:\Program Files (x86)\Windows Kits\10\bin\10.0.26100.0\x64\dxil.dll'
$tests = @(
    'emit::lower::tests::scalar_absolute_difference_translates',
    'emit::valu_tests::scalar_absolute_difference_matches_rdna2',
    'emit::valu_tests::scalar_absolute_difference_wraps_and_replaces_scc'
)
foreach ($test in $tests) {
    cargo test --release --locked -p victory-shader --lib $test -- --exact --nocapture --test-threads=1
}

AI assistance from Codex covered the test, execution, independent review and this report.

Focused test patch and doc-comment relocation
diff --git a/crates/victory-shader/src/emit/valu_tests.rs b/crates/victory-shader/src/emit/valu_tests.rs
index 856264f..102dd01 100644
--- a/crates/victory-shader/src/emit/valu_tests.rs
+++ b/crates/victory-shader/src/emit/valu_tests.rs
@@ -470,8 +470,49 @@ fn v_med3_f32_matches_rdna2_with_nan() {
     });
 }
 
-/// RDNA2 orders -0 below +0 in its maximums and minimums; DXIL's FMax and
-/// FMin compare and select, which can give either zero.
+#[test]
+fn scalar_absolute_difference_wraps_and_replaces_scc() {
+    // SOP2 opcode 44 takes the absolute value after 32-bit subtraction.
+    let pairs = [
+        [0, 0, 0],
+        [0x8000_0000, 0x8000_0000, 0],
+        [0x7FFF_FFFF, 0x7FFF_FFFF, 0],
+        [0x8000_0000, 0x7FFF_FFFF, 1],
+        [0x7FFF_FFFF, 0x8000_0000, 1],
+        [0x8000_0000, 1, 0x7FFF_FFFF],
+        [1, 0x8000_0000, 0x7FFF_FFFF],
+        [0x8000_0000, u32::MAX, 0x7FFF_FFFF],
+        [u32::MAX, 0x8000_0000, 0x7FFF_FFFF],
+        [0x7FFF_FFFF, u32::MAX, 0x8000_0000],
+        [u32::MAX, 0x7FFF_FFFF, 0x8000_0000],
+        [0, 0x8000_0000, 0x8000_0000],
+    ];
+    let cases: Vec<_> = [0, u32::MAX]
+        .into_iter()
+        .flat_map(|seed| pairs.map(|[a, b, _]| [a, b, 0, seed]))
+        .collect();
+    for read in [12, 15] {
+        let shader = build_valu_cs(&[
+            sop2(0x00, 14, 3, 0x81), // incoming SCC from s3 + 1
+            sop2(0x2C, 12, 0, 1),
+            sop2(0x0A, 15, 0x81, 0x80), // materialize SCC without replacing s12
+            vop1(0x001, 0, sgpr(read)),
+        ]);
+        run_cases(&shader, &cases, |backend, i, input, got| {
+            let value = pairs[i % pairs.len()][2];
+            let want = if read == 15 {
+                u32::from(value != 0)
+            } else {
+                value
+            };
+            assert_eq!(
+                got, want,
+                "{backend}: s_absdiff_i32 s{read}, input {input:x?}, expected {want:#010x}"
+            );
+        });
+    }
+}
+
 #[test]
 fn scalar_absolute_difference_matches_rdna2() {
     // s_absdiff_i32 s12, s0, s1; then v0 = s12, or v0 = SCC through
@@ -490,6 +531,8 @@ fn scalar_absolute_difference_matches_rdna2() {
     });
 }
 
+/// RDNA2 orders -0 below +0 in its maximums and minimums; DXIL's FMax and
+/// FMin compare and select, which can give either zero.
 #[test]
 fn max_and_min_order_negative_zero_below_positive_zero() {
     let (pz, nz) = (0_u32, 0x8000_0000_u32);
**@⁠lmouz823-lang** on GitHub · 2026-11-28 05:42 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/94#issuecomment-7234832847) Native Windows GPU validation completed for exact head `3a7b9d9c9da1e6bd47ba0917779380c4a8b854a3`. The existing translation and result regressions each execute exactly one test and pass. The original GPU test performs 20 dispatches across RTX 4060 Laptop and WARP. I added a fixed-expectation test with 12 pairs and incoming SCC both 0 and 1. It covers equal zero/INT_MIN/INT_MAX, both orders of INT_MIN versus INT_MAX, INT_MIN versus 1 and -1, INT_MAX versus -1, and zero versus INT_MIN. Value and outgoing SCC are read separately, giving **96 passing dispatches** across the two backends. The table follows [AMD's RDNA2 ISA, SOP2 opcode 44, printed page 105](https://docs.amd.com/v/u/en-US/rdna2-shader-instruction-set-architecture). Subtraction wraps at 32 bits before the conditional negation, and the resulting zero/nonzero value replaces SCC. A temporary negative control changes the sign decision from `difference < 0` to `operand0 < operand1`. The original five-pair GPU test still passes. The new test fails on hardware for INT_MIN versus INT_MAX, returning `0xffffffff` where the fixed table expects `1`. Its exact selection runs one test and exits 101. I then restored `lower.rs` byte-for-byte, removed the temporary diagnostics and confirmed actual recompilation before the final three exact selections pass again. Those selections have zero failed or ignored tests and no skip diagnostics. The explicitly selected SDK `dxil.dll` 1.8.2502.11 successfully created `IDxcValidator`. Temporary success markers after loading and after validation plus the container-hash assertion confirmed both new modules and both original modules passed those checks. Those markers are absent from the final patch and runs. The patch adds one 42-line test function. It also relocates the existing two-line negative-zero doc comment from the absolute-difference test to `max_and_min_order_negative_zero_below_positive_zero`, which it describes. Removing the new test and reversing that comment move reproduces the head source text exactly. Independent review checked the fixed values, encodings, SCC registers, counts and restoration evidence. One initial unchanged-head release compilation ended with a `rustc` access violation (`0xc0000005`) before any test executed. The failure log was retained. An immediate retry with identical source and configuration compiled successfully in 1m01s, and all subsequent diagnostic, mutation and restored-source builds completed. The cause of that first compiler failure remains undetermined. Environment was Windows 11 Pro build 26200, i9-14900HX, NVIDIA 610.88 and Rust 1.98.1 MSVC, release. Package Clippy with `--all-targets -- -D warnings`, asset policy, new-function rustfmt and `git diff --check` pass. Whole-file rustfmt exits 1 on the original and patched files with the same 17 existing formatting hunks after file/line headers are removed. Native AMD execution, game behavior, other platforms and full shader/workspace suites remain unverified. Coverage is limited to the listed synthetic patterns and SCC/wrapping boundaries. Reproduction uses an installed SDK validator and the project's Windows build prerequisites. ```powershell $env:VICTORY_STRICT_TESTS = '1' $env:VICTORY_DXIL = 'C:\Program Files (x86)\Windows Kits\10\bin\10.0.26100.0\x64\dxil.dll' $tests = @( 'emit::lower::tests::scalar_absolute_difference_translates', 'emit::valu_tests::scalar_absolute_difference_matches_rdna2', 'emit::valu_tests::scalar_absolute_difference_wraps_and_replaces_scc' ) foreach ($test in $tests) { cargo test --release --locked -p victory-shader --lib $test -- --exact --nocapture --test-threads=1 } ``` AI assistance from Codex covered the test, execution, independent review and this report. <details> <summary>Focused test patch and doc-comment relocation</summary> ```diff diff --git a/crates/victory-shader/src/emit/valu_tests.rs b/crates/victory-shader/src/emit/valu_tests.rs index 856264f..102dd01 100644 --- a/crates/victory-shader/src/emit/valu_tests.rs +++ b/crates/victory-shader/src/emit/valu_tests.rs @@ -470,8 +470,49 @@ fn v_med3_f32_matches_rdna2_with_nan() { }); } -/// RDNA2 orders -0 below +0 in its maximums and minimums; DXIL's FMax and -/// FMin compare and select, which can give either zero. +#[test] +fn scalar_absolute_difference_wraps_and_replaces_scc() { + // SOP2 opcode 44 takes the absolute value after 32-bit subtraction. + let pairs = [ + [0, 0, 0], + [0x8000_0000, 0x8000_0000, 0], + [0x7FFF_FFFF, 0x7FFF_FFFF, 0], + [0x8000_0000, 0x7FFF_FFFF, 1], + [0x7FFF_FFFF, 0x8000_0000, 1], + [0x8000_0000, 1, 0x7FFF_FFFF], + [1, 0x8000_0000, 0x7FFF_FFFF], + [0x8000_0000, u32::MAX, 0x7FFF_FFFF], + [u32::MAX, 0x8000_0000, 0x7FFF_FFFF], + [0x7FFF_FFFF, u32::MAX, 0x8000_0000], + [u32::MAX, 0x7FFF_FFFF, 0x8000_0000], + [0, 0x8000_0000, 0x8000_0000], + ]; + let cases: Vec<_> = [0, u32::MAX] + .into_iter() + .flat_map(|seed| pairs.map(|[a, b, _]| [a, b, 0, seed])) + .collect(); + for read in [12, 15] { + let shader = build_valu_cs(&[ + sop2(0x00, 14, 3, 0x81), // incoming SCC from s3 + 1 + sop2(0x2C, 12, 0, 1), + sop2(0x0A, 15, 0x81, 0x80), // materialize SCC without replacing s12 + vop1(0x001, 0, sgpr(read)), + ]); + run_cases(&shader, &cases, |backend, i, input, got| { + let value = pairs[i % pairs.len()][2]; + let want = if read == 15 { + u32::from(value != 0) + } else { + value + }; + assert_eq!( + got, want, + "{backend}: s_absdiff_i32 s{read}, input {input:x?}, expected {want:#010x}" + ); + }); + } +} + #[test] fn scalar_absolute_difference_matches_rdna2() { // s_absdiff_i32 s12, s0, s1; then v0 = s12, or v0 = SCC through @@ -490,6 +531,8 @@ fn scalar_absolute_difference_matches_rdna2() { }); } +/// RDNA2 orders -0 below +0 in its maximums and minimums; DXIL's FMax and +/// FMin compare and select, which can give either zero. #[test] fn max_and_min_order_negative_zero_below_positive_zero() { let (pz, nz) = (0_u32, 0x8000_0000_u32); ``` </details>
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/94/head:github-bot/gh-94'
git switch 'github-bot/gh-94'

Merge

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