shader: lower s_sext_i32_i8 and s_sext_i32_i16 #91

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

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

What is broken

s_sext_i32_i8 (SOP1 0x19) and s_sext_i32_i16 (SOP1 0x1A) decode, but a program using either fails to translate with s_sext_i32_i8 is not supported (or the i16 name). They sign-extend a byte or a short held in an SGPR, which compilers emit for int8/int16 loads and for packed constants.

How I found them: I translated every SOP1/SOP2 opcode the decoder names through the CPU harness of #62, with the result in s12. The earlier coverage run wrote s10, which overlaps the UAV descriptor, so it could not see scalar gaps. The same sweep found the s_pack_* gap (#89).

Semantics

AMD's RDNA2 ISA (§12.2, opcodes 25 and 26): D.i = signext(S0.i[7:0]) and D.i = signext(S0.i[15:0]). They write no SCC, and the IR's writes_scc already leaves them out.

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

Each is a shift left by 24 (or 16) followed by an arithmetic shift right by the same amount.

Tests

  • emit::lower::tests::scalar_sign_extensions_translate (every host): both decode, and a program that moves the result to v0 and stores it translates. Without the lower.rs change it fails: s_sext_i32_i8 [be8c1901, 7e00020c]: GFX10 code at 0x24: s_sext_i32_i8 is not supported.
  • valu_tests::scalar_sign_extensions_match_rdna2 (Windows/GPU): three hand-computed cases each, covering negative and positive bytes and halves with junk above them.
  • CPU run: I ran these on Linux through the CPU harness of #62, which runs the emitted DXIL with LLVM's lli. I compared against Rust's as i8/as i16 casts on 80 scrambled inputs, plus a 32-bit literal source. 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:24 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/91) `lcg007xd-c:shader-s-sext` → `main` · +64 −0 · 2 files · 1 commits · open ## What is broken `s_sext_i32_i8` (SOP1 **0x19**) and `s_sext_i32_i16` (SOP1 **0x1A**) decode, but a program using either fails to translate with `s_sext_i32_i8 is not supported` (or the `i16` name). They sign-extend a byte or a short held in an SGPR, which compilers emit for `int8`/`int16` loads and for packed constants. **How I found them:** I translated every SOP1/SOP2 opcode the decoder names through the CPU harness of #62, with the result in `s12`. The earlier coverage run wrote `s10`, which overlaps the UAV descriptor, so it could not see scalar gaps. The same sweep found the `s_pack_*` gap (#89). ## Semantics AMD's RDNA2 ISA (§12.2, opcodes 25 and 26): `D.i = signext(S0.i[7:0])` and `D.i = signext(S0.i[15:0])`. They write no SCC, and the IR's `writes_scc` already leaves them out. ## The change (`emit/lower.rs`, in `sop`) Each is a shift left by 24 (or 16) followed by an arithmetic shift right by the same amount. ## Tests - **`emit::lower::tests::scalar_sign_extensions_translate`** (every host): both decode, and a program that moves the result to `v0` and **stores** it translates. Without the `lower.rs` change it fails: `s_sext_i32_i8 [be8c1901, 7e00020c]: GFX10 code at 0x24: s_sext_i32_i8 is not supported`. - **`valu_tests::scalar_sign_extensions_match_rdna2`** (Windows/GPU): three hand-computed cases each, covering negative and positive bytes and halves with junk above them. - **CPU run:** I ran these on Linux through the CPU harness of #62, which runs the emitted DXIL with LLVM's `lli`. I compared against Rust's `as i8`/`as i16` casts on 80 scrambled inputs, plus a 32-bit literal source. 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)
SOP1 0x19 and 0x1A decoded but failed to translate ("s_sext_i32_i8 is
not supported"). AMD's RDNA2 ISA: D.i = signext(S0[7:0]) and
signext(S0[15:0]); they write no SCC.

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:42 +00:00
Author
Owner

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

I can take native Windows GPU validation at exact head 91424850be390994344e55cff8d55f209e95bc5d. I will run both existing exact tests, add focused fixed-expectation byte/halfword boundaries and SCC-preservation coverage, and verify a targeted negative control followed by restored-source recompilation. Final runs will use RTX 4060 Laptop and WARP with the SDK validator. Counts, skipped cases and remaining native AMD/game limits will accompany the small patch. AI-assisted with Codex.

**@⁠lmouz823-lang** on GitHub · 2026-11-28 08:05 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/91#issuecomment-7236357083) I can take native Windows GPU validation at exact head `91424850be390994344e55cff8d55f209e95bc5d`. I will run both existing exact tests, add focused fixed-expectation byte/halfword boundaries and SCC-preservation coverage, and verify a targeted negative control followed by restored-source recompilation. Final runs will use RTX 4060 Laptop and WARP with the SDK validator. Counts, skipped cases and remaining native AMD/game limits will accompany the small patch. AI-assisted with Codex.
Author
Owner

@⁠lmouz823-lang on GitHub · 2026-11-28 08:18 UTC · original

Native Windows GPU validation completed for exact head 91424850be390994344e55cff8d55f209e95bc5d. The original translation and GPU regressions each execute exactly one test and pass. The original GPU test runs 12 dispatches across RTX 4060 Laptop and WARP.

I added one 61-line fixed-expectation regression. Seven inputs cover byte boundaries 0/0x7f/0x80/0xff and halfword boundaries 0/0x7fff/0x8000/0xffff with upper-bit junk. Each input runs with incoming SCC 0 and 1 supplied dynamically through a root constant. The value and outgoing SCC are read separately for both instructions. A real 0xbeef7f80 literal additionally checks byte result 0xffffff80 and halfword result 0x00007f80, with both incoming flags preserved. Expectations follow AMD's RDNA2 ISA, the integer arithmetic summary and SOP1 opcodes 25/26.

Four main shaders times 14 inputs times two backends give 112 passing dispatches. Four literal shaders times two inputs times two backends add 16, for 128 passing dispatches. The test checks the result and SCC independently through s12 and s15, with the carry seed written to s14.

A temporary IR mutation wrongly lists the sext stem among SCC writers. The original value-only GPU test still passes. The new test fails on hardware at input 0xabcd007f with incoming SCC 0, reporting outgoing SCC 1 against the expected 0. Its exact selection executes one test and exits 101. After restoring the original IR bytes and removing diagnostics, I cleaned only the isolated shader package release target and confirmed an actual 1m34s compilation before the final new GPU, original GPU and translation exact selections each pass. All three final 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 markers after load and after validation plus the container-hash assertion confirmed all eight new modules and both original modules passed those checks. The markers were removed for the final runs. Independent review checked the fixed values, encodings, registers, counts and source restoration.

One preliminary diagnostic-setup assertion incorrectly expected a single validator-load site. This head has separate VALU and wave helper sites, and the assertion stopped before the new function was written. A subsequent test attempt selected zero tests and was rejected by the exact-test runner. Both preparation and zero-selection records are excluded from acceptance. The corrected preparation targets the VALU helper, checks its exit code and verifies the function is present before dependent tests. The preliminary log is retained separately.

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

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_sign_extensions_translate',
    'emit::valu_tests::scalar_sign_extensions_match_rdna2',
    'emit::valu_tests::scalar_sign_extensions_preserve_scc_at_boundaries'
)
foreach ($test in $tests) {
    cargo test --release --locked -p victory-shader --lib $test -- --exact --nocapture --test-threads=1
}

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

Fixed-expectation regression against this PR head
diff --git a/crates/victory-shader/src/emit/valu_tests.rs b/crates/victory-shader/src/emit/valu_tests.rs
index c074e5d..1567769 100644
--- a/crates/victory-shader/src/emit/valu_tests.rs
+++ b/crates/victory-shader/src/emit/valu_tests.rs
@@ -589,6 +589,67 @@ fn v_cmp_lg_is_false_for_nan() {
     }
 }
 
+#[test]
+fn scalar_sign_extensions_preserve_scc_at_boundaries() {
+    // Input, sign-extended byte, sign-extended halfword, with upper-bit junk.
+    let table = [
+        [0xFFFF_0000, 0, 0],
+        [0xABCD_007F, 0x7F, 0x7F],
+        [0x1234_0080, 0xFFFF_FF80, 0x80],
+        [0x9876_00FF, u32::MAX, 0xFF],
+        [0xFFFF_7FFF, u32::MAX, 0x7FFF],
+        [0xAAAA_8000, 0, 0xFFFF_8000],
+        [0x1234_FFFF, u32::MAX, u32::MAX],
+    ];
+    let cases: Vec<_> = [0, u32::MAX]
+        .into_iter()
+        .flat_map(|seed| table.map(|[value, _, _]| [value, 0, 0, seed]))
+        .collect();
+    for (op, name, column, literal_want) in [
+        (0x19, "s_sext_i32_i8", 1, 0xFFFF_FF80),
+        (0x1A, "s_sext_i32_i16", 2, 0x7F80),
+    ] {
+        for read in [12, 15] {
+            let shader = build_valu_cs(&[
+                sop2(0x00, 14, 3, 0x81), // incoming SCC from s3 + 1
+                0xBE8C_0000 | op << 8,
+                sop2(0x0A, 15, 0x81, 0x80), // preserve result in s12
+                vop1(0x001, 0, sgpr(read)),
+            ]);
+            run_cases(&shader, &cases, |backend, i, input, got| {
+                let prior = u32::from(input[3] == u32::MAX);
+                let want = if read == 15 {
+                    prior
+                } else {
+                    table[i % table.len()][column]
+                };
+                assert_eq!(got, want, "{backend}: {name}, s{read}, input {input:x?}");
+            });
+
+            // A real literal gives a negative byte and a positive halfword.
+            let literal = build_valu_cs(&[
+                sop2(0x00, 14, 3, 0x81),
+                0xBE8C_0000 | op << 8 | 0xFF,
+                0xBEEF_7F80,
+                sop2(0x0A, 15, 0x81, 0x80),
+                vop1(0x001, 0, sgpr(read)),
+            ]);
+            run_cases(
+                &literal,
+                &[[0, 0, 0, 0], [0, 0, 0, u32::MAX]],
+                |backend, _, input, got| {
+                    let prior = u32::from(input[3] == u32::MAX);
+                    let want = if read == 15 { prior } else { literal_want };
+                    assert_eq!(
+                        got, want,
+                        "{backend}: literal {name}, s{read}, prior SCC {prior}"
+                    );
+                },
+            );
+        }
+    }
+}
+
 #[test]
 fn scalar_sign_extensions_match_rdna2() {
     // s_sext_i32_i8 / s_sext_i32_i16 s12, s0; v_mov_b32 v0, s12.
**@⁠lmouz823-lang** on GitHub · 2026-11-28 08:18 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/91#issuecomment-7236498669) Native Windows GPU validation completed for exact head `91424850be390994344e55cff8d55f209e95bc5d`. The original translation and GPU regressions each execute exactly one test and pass. The original GPU test runs 12 dispatches across RTX 4060 Laptop and WARP. I added one 61-line fixed-expectation regression. Seven inputs cover byte boundaries 0/0x7f/0x80/0xff and halfword boundaries 0/0x7fff/0x8000/0xffff with upper-bit junk. Each input runs with incoming SCC 0 and 1 supplied dynamically through a root constant. The value and outgoing SCC are read separately for both instructions. A real `0xbeef7f80` literal additionally checks byte result `0xffffff80` and halfword result `0x00007f80`, with both incoming flags preserved. Expectations follow [AMD's RDNA2 ISA, the integer arithmetic summary and SOP1 opcodes 25/26](https://docs.amd.com/v/u/en-US/rdna2-shader-instruction-set-architecture). Four main shaders times 14 inputs times two backends give 112 passing dispatches. Four literal shaders times two inputs times two backends add 16, for **128 passing dispatches**. The test checks the result and SCC independently through s12 and s15, with the carry seed written to s14. A temporary IR mutation wrongly lists the `sext` stem among SCC writers. The original value-only GPU test still passes. The new test fails on hardware at input `0xabcd007f` with incoming SCC 0, reporting outgoing SCC 1 against the expected 0. Its exact selection executes one test and exits 101. After restoring the original IR bytes and removing diagnostics, I cleaned only the isolated shader package release target and confirmed an actual 1m34s compilation before the final new GPU, original GPU and translation exact selections each pass. All three final 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 markers after load and after validation plus the container-hash assertion confirmed all eight new modules and both original modules passed those checks. The markers were removed for the final runs. Independent review checked the fixed values, encodings, registers, counts and source restoration. One preliminary diagnostic-setup assertion incorrectly expected a single validator-load site. This head has separate VALU and wave helper sites, and the assertion stopped before the new function was written. A subsequent test attempt selected zero tests and was rejected by the exact-test runner. Both preparation and zero-selection records are excluded from acceptance. The corrected preparation targets the VALU helper, checks its exit code and verifies the function is present before dependent tests. The preliminary log is retained separately. Environment was Windows 11 Pro build 26200, RTX 4060 Laptop, NVIDIA 610.88 and Rust 1.98.1 MSVC, release. Package Clippy with `--all-targets -- -D warnings`, asset policy (195 files), new-function rustfmt and `git diff --check` pass. Whole-file rustfmt exits 1 on both original and final source with the same 17 existing formatting hunks after file/line headers are removed. Native AMD execution, game behavior, other platforms and the full shader/workspace suites remain unverified. Coverage is limited to the listed synthetic patterns and flag states. 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_sign_extensions_translate', 'emit::valu_tests::scalar_sign_extensions_match_rdna2', 'emit::valu_tests::scalar_sign_extensions_preserve_scc_at_boundaries' ) foreach ($test in $tests) { cargo test --release --locked -p victory-shader --lib $test -- --exact --nocapture --test-threads=1 } ``` AI assistance from Codex covered the synthetic regression, execution, independent review and this report. <details> <summary>Fixed-expectation regression against this PR head</summary> ```diff diff --git a/crates/victory-shader/src/emit/valu_tests.rs b/crates/victory-shader/src/emit/valu_tests.rs index c074e5d..1567769 100644 --- a/crates/victory-shader/src/emit/valu_tests.rs +++ b/crates/victory-shader/src/emit/valu_tests.rs @@ -589,6 +589,67 @@ fn v_cmp_lg_is_false_for_nan() { } } +#[test] +fn scalar_sign_extensions_preserve_scc_at_boundaries() { + // Input, sign-extended byte, sign-extended halfword, with upper-bit junk. + let table = [ + [0xFFFF_0000, 0, 0], + [0xABCD_007F, 0x7F, 0x7F], + [0x1234_0080, 0xFFFF_FF80, 0x80], + [0x9876_00FF, u32::MAX, 0xFF], + [0xFFFF_7FFF, u32::MAX, 0x7FFF], + [0xAAAA_8000, 0, 0xFFFF_8000], + [0x1234_FFFF, u32::MAX, u32::MAX], + ]; + let cases: Vec<_> = [0, u32::MAX] + .into_iter() + .flat_map(|seed| table.map(|[value, _, _]| [value, 0, 0, seed])) + .collect(); + for (op, name, column, literal_want) in [ + (0x19, "s_sext_i32_i8", 1, 0xFFFF_FF80), + (0x1A, "s_sext_i32_i16", 2, 0x7F80), + ] { + for read in [12, 15] { + let shader = build_valu_cs(&[ + sop2(0x00, 14, 3, 0x81), // incoming SCC from s3 + 1 + 0xBE8C_0000 | op << 8, + sop2(0x0A, 15, 0x81, 0x80), // preserve result in s12 + vop1(0x001, 0, sgpr(read)), + ]); + run_cases(&shader, &cases, |backend, i, input, got| { + let prior = u32::from(input[3] == u32::MAX); + let want = if read == 15 { + prior + } else { + table[i % table.len()][column] + }; + assert_eq!(got, want, "{backend}: {name}, s{read}, input {input:x?}"); + }); + + // A real literal gives a negative byte and a positive halfword. + let literal = build_valu_cs(&[ + sop2(0x00, 14, 3, 0x81), + 0xBE8C_0000 | op << 8 | 0xFF, + 0xBEEF_7F80, + sop2(0x0A, 15, 0x81, 0x80), + vop1(0x001, 0, sgpr(read)), + ]); + run_cases( + &literal, + &[[0, 0, 0, 0], [0, 0, 0, u32::MAX]], + |backend, _, input, got| { + let prior = u32::from(input[3] == u32::MAX); + let want = if read == 15 { prior } else { literal_want }; + assert_eq!( + got, want, + "{backend}: literal {name}, s{read}, prior SCC {prior}" + ); + }, + ); + } + } +} + #[test] fn scalar_sign_extensions_match_rdna2() { // s_sext_i32_i8 / s_sext_i32_i16 s12, s0; v_mov_b32 v0, s12. ``` </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/91/head:github-bot/gh-91'
git switch 'github-bot/gh-91'

Merge

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