shader: lower s_bfe_u64, s_bfe_i64 and s_bfm_b64 #97

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

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

What is broken

The 64-bit scalar bitfield instructions decode, but a program using one fails to translate with s_bfe_u64 is not supported (or the matching name). Their 32-bit forms (s_bfe_u32, s_bfe_i32, s_bfm_b32) already lower.

SOP2 opcode Instruction
0x29 s_bfe_u64
0x2A s_bfe_i64
0x25 s_bfm_b64

They are part of the scalar gap the sweep through the CPU harness of #62 turned up (see #89).

Semantics (AMD's RDNA2 ISA, §12.1, opcodes 37, 41, 42)

  • s_bfe_u64: D.u64 = (S0.u64 >> S1[5:0]) & ((1 << S1[22:16]) - 1) and SCC = (D != 0). s_bfe_i64 sign-extends the field from its top bit.
  • s_bfm_b64: D.u64 = ((1 << S0[5:0]) - 1) << S1[5:0]. It writes no SCC.
  • Widths of 64 or more (the field is 7 bits) take every bit left after the shift. That is how this file's bfe helper already treats a width of 32 or more for s_bfe_u32.

The change (emit/lower.rs, in sop's 64-bit section)

  • 64-bit integers: the source pair is joined into an i64, and the extract (or mask) is computed in i64 (INT64 flag), mirroring the 32-bit bfe helper: a full-width mask, then a sign extension skipped for widths 0 and ≥ 64.
  • Result: split back into the two dwords. SCC is set for the extracts.

Tests

  • emit::lower::tests::scalar_64_bit_bitfields_translate (every host): all three decode, and a program that stores the result translates. Without the lower.rs change it fails: s_bfe_u64 [948c0200, 7e00020c]: GFX10 code at 0x24: s_bfe_u64 is not supported.
  • valu_tests::scalar_64_bit_bitfields_match_rdna2 (Windows/GPU): both dwords of each result for four cases: a field across the dword boundary, a sign-set top nibble, width 64, and width 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 both dwords against a Rust u64/i64 reference on 38 cases. They include widths 1, 32, 63+, 127 and junk in S1's other bits, plus SCC for both extracts. 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. I could not check widths above 64 on RDNA2 hardware, so they follow the 32-bit lowering's rule.

🤖 Generated with Claude Code

**@⁠lcg007xd-c** on GitHub · 2026-11-25 22:34 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/97) `lcg007xd-c:shader-s-bfe64` → `main` · +154 −0 · 2 files · 1 commits · open ## What is broken The 64-bit scalar bitfield instructions decode, but a program using one fails to translate with `s_bfe_u64 is not supported` (or the matching name). Their 32-bit forms (`s_bfe_u32`, `s_bfe_i32`, `s_bfm_b32`) already lower. | SOP2 opcode | Instruction | |---|---| | 0x29 | `s_bfe_u64` | | 0x2A | `s_bfe_i64` | | 0x25 | `s_bfm_b64` | They are part of the scalar gap the sweep through the CPU harness of #62 turned up (see #89). ## Semantics (AMD's RDNA2 ISA, §12.1, opcodes 37, 41, 42) - **`s_bfe_u64`:** `D.u64 = (S0.u64 >> S1[5:0]) & ((1 << S1[22:16]) - 1)` and `SCC = (D != 0)`. `s_bfe_i64` sign-extends the field from its top bit. - **`s_bfm_b64`:** `D.u64 = ((1 << S0[5:0]) - 1) << S1[5:0]`. It writes no SCC. - **Widths of 64 or more** (the field is 7 bits) take every bit left after the shift. That is how this file's `bfe` helper already treats a width of 32 or more for `s_bfe_u32`. ## The change (`emit/lower.rs`, in `sop`'s 64-bit section) - **64-bit integers:** the source pair is joined into an i64, and the extract (or mask) is computed in i64 (`INT64` flag), mirroring the 32-bit `bfe` helper: a full-width mask, then a sign extension skipped for widths 0 and ≥ 64. - **Result:** split back into the two dwords. SCC is set for the extracts. ## Tests - **`emit::lower::tests::scalar_64_bit_bitfields_translate`** (every host): all three decode, and a program that **stores** the result translates. Without the `lower.rs` change it fails: `s_bfe_u64 [948c0200, 7e00020c]: GFX10 code at 0x24: s_bfe_u64 is not supported`. - **`valu_tests::scalar_64_bit_bitfields_match_rdna2`** (Windows/GPU): both dwords of each result for four cases: a field across the dword boundary, a sign-set top nibble, width 64, and width 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 both dwords against a Rust `u64`/`i64` reference on 38 cases. They include widths 1, 32, 63+, 127 and junk in S1's other bits, plus SCC for both extracts. 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. I could not check widths above 64 on RDNA2 hardware, so they follow the 32-bit lowering's rule. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
SOP2 0x29, 0x2A and 0x25 decoded but failed to translate ("s_bfe_u64
is not supported"): the 64-bit forms of s_bfe_u32/s_bfe_i32/s_bfm_b32.
AMD's RDNA2 ISA: s_bfe extracts a field of width S1[22:16] at offset
S1[5:0] (sign-extended for _i64; a width of 64 or more takes every
remaining bit, as the 32-bit lowering does for 32), SCC = (D != 0);
s_bfm_b64 builds ((1 << S0[5:0]) - 1) << S1[5:0] with no SCC. Computed
in 64-bit integers.

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

@⁠lmouz823-lang on GitHub · 2026-11-28 04:40 UTC · original

I can take Windows GPU and SCC validation at exact head 7fceac4d9604c1bca5148e4b9e14b9315e715081. I will execute the existing translation/result tests and add fixed-expectation coverage for BFE updating SCC from the complete 64-bit result and BFM preserving incoming SCC 0/1. New cases will use widths 0–63, including a high-dword-only nonzero result. I will report the existing width-64 case as consistency with the current PR expectations, with its native semantic uncertainty retained. A temporary low-dword-only SCC check will provide a negative control, followed by restoration and fresh runs on RTX 4060 Laptop and WARP with the SDK validator. AI-assisted with Codex.

**@⁠lmouz823-lang** on GitHub · 2026-11-28 04:40 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/97#issuecomment-7234184873) I can take Windows GPU and SCC validation at exact head `7fceac4d9604c1bca5148e4b9e14b9315e715081`. I will execute the existing translation/result tests and add fixed-expectation coverage for BFE updating SCC from the complete 64-bit result and BFM preserving incoming SCC 0/1. New cases will use widths 0–63, including a high-dword-only nonzero result. I will report the existing width-64 case as consistency with the current PR expectations, with its native semantic uncertainty retained. A temporary low-dword-only SCC check will provide a negative control, followed by restoration and fresh runs on RTX 4060 Laptop and WARP with the SDK validator. AI-assisted with Codex.
Author
Owner

@⁠lmouz823-lang on GitHub · 2026-11-28 04:52 UTC · original

Native Windows validation completed for exact head 7fceac4d9604c1bca5148e4b9e14b9315e715081 on RTX 4060 Laptop and WARP. The existing translation and GPU tests each execute exactly one test and pass. The existing GPU test includes width 64, so its success records agreement with this PR's chosen expectations. Native RDNA2 behavior at width 64 or above remains unverified.

I added one fixed-table GPU regression restricted to width 0–63 and offset + width <= 64. It separately reads the low dword, high dword and SCC for all three operations, with incoming SCC both 0 and 1. Six input vectors per operation × two incoming flags × three readbacks × two backends gives 216 passing dispatches. It checks BFE setting SCC from the full 64-bit result, signed extraction, and BFM preserving SCC for zero and nonzero masks. One BFE case produces low=0, high=1, which specifically checks the high dword contributes to SCC. Expectations follow AMD's RDNA2 ISA, SOP2 opcodes 37, 41 and 42, printed page 105.

A negative control temporarily changes this branch's nonzero(self, rl, rh) to nonzero(self, rl, rl). The original result-only GPU test still passes. The new test fails on hardware for source 0x0000000100000000, offset 0, width 63 and incoming SCC 0, reporting SCC 0 where the fixed table expects 1. After restoring the implementation and removing diagnostics, a fresh compilation and final runs of both GPU tests and the translation test each pass. There are no skips or ignored tests. The additional patch contains one 94-line synthetic test function.

The explicit SDK dxil.dll 1.8.2502.11 created IDxcValidator. Temporary success markers after DXIL validation and the container-hash assertion confirmed all nine new modules and all six original modules passed both checks. Those markers were removed before the final runs. Independent review checked the fixed values, encodings, bounds, SCC producer/readback, counts and final source restoration.

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 (195 files), git diff --check and rustfmt on the added function pass. Native AMD execution, game behavior and the full workspace suite remain unverified.

Reproduction uses an installed SDK validator and the 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_64_bit_bitfields_translate',
    'emit::valu_tests::scalar_64_bit_bitfields_match_rdna2',
    'emit::valu_tests::scalar_64_bit_bitfields_write_or_preserve_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.

94-line synthetic test patch 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 1ad12e7..5830af5 100644
--- a/crates/victory-shader/src/emit/valu_tests.rs
+++ b/crates/victory-shader/src/emit/valu_tests.rs
@@ -337,6 +337,100 @@ fn scalar_64_bit_bitfields_match_rdna2() {
     }
 }
 
+#[test]
+fn scalar_64_bit_bitfields_write_or_preserve_scc() {
+    // RDNA2 ISA SOP2 opcodes 37, 41 and 42 (printed page 105).
+    // New cases use width <= 63 and offset + width <= 64. Each fixed row
+    // is [low dword, high dword, SCC with incoming 0, SCC with incoming 1].
+    type Inputs = [[u32; 3]; 6];
+    type Expected = [[u32; 4]; 6];
+    let extracts: Inputs = [
+        [u32::MAX, u32::MAX, 0],
+        [1, 0, 1 << 16],
+        [2, 0, 1 << 16],
+        [0, 1, 63 << 16],
+        [0x89AB_CDEF, 0x0123_4567, 28 | 8 << 16],
+        [0, 0x8000_0000, 60 | 4 << 16],
+    ];
+    let masks: Inputs = [
+        [0, 7, 0],
+        [1, 63, 0],
+        [32, 0, 0],
+        [33, 31, 0],
+        [63, 1, 0],
+        [31, 33, 0],
+    ];
+    let checks: [(u8, &str, Inputs, Expected); 3] = [
+        (
+            0x29,
+            "s_bfe_u64",
+            extracts,
+            [
+                [0, 0, 0, 0],
+                [1, 0, 1, 1],
+                [0, 0, 0, 0],
+                [0, 1, 1, 1],
+                [0x78, 0, 1, 1],
+                [8, 0, 1, 1],
+            ],
+        ),
+        (
+            0x2A,
+            "s_bfe_i64",
+            extracts,
+            [
+                [0, 0, 0, 0],
+                [u32::MAX, u32::MAX, 1, 1],
+                [0, 0, 0, 0],
+                [0, 1, 1, 1],
+                [0x78, 0, 1, 1],
+                [0xFFFF_FFF8, u32::MAX, 1, 1],
+            ],
+        ),
+        (
+            0x25,
+            "s_bfm_b64",
+            masks,
+            [
+                [0, 0, 0, 1],
+                [0, 0x8000_0000, 0, 1],
+                [u32::MAX, 0, 0, 1],
+                [0x8000_0000, u32::MAX, 0, 1],
+                [0xFFFF_FFFE, u32::MAX, 0, 1],
+                [0, 0xFFFF_FFFE, 0, 1],
+            ],
+        ),
+    ];
+    for (op, name, inputs, expected) in checks {
+        let cases: Vec<_> = [0, u32::MAX]
+            .into_iter()
+            .flat_map(|seed| inputs.map(|[a, b, c]| [a, b, c, seed]))
+            .collect();
+        let src1 = if op == 0x25 { 1 } else { 2 };
+        for (read, label) in [(12, "low"), (13, "high"), (15, "SCC")] {
+            let shader = build_valu_cs(&[
+                sop2(0x00, 14, 3, 0x81), // s3 + 1 seeds SCC with carry 0 or 1
+                sop2(op, 12, 0, src1),
+                sop2(0x0A, 15, 0x81, 0x80), // keep both result dwords intact
+                vop1(0x001, 0, sgpr(read)),
+            ]);
+            run_cases(&shader, &cases, |backend, i, input, got| {
+                let prior = usize::from(input[3] == u32::MAX);
+                let column = match read {
+                    12 => 0,
+                    13 => 1,
+                    _ => 2 + prior,
+                };
+                let want = expected[i % inputs.len()][column];
+                assert_eq!(
+                    got, want,
+                    "{backend}: {name} {label}, prior SCC {prior}, input {input:x?}"
+                );
+            });
+        }
+    }
+}
+
 #[test]
 fn packed_add_selects_halves_of_inline_constants() {
     // Add the inline constant 1/(2*pi) to v1 (zero) to read back its two halves.
**@⁠lmouz823-lang** on GitHub · 2026-11-28 04:52 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/97#issuecomment-7234312520) Native Windows validation completed for exact head `7fceac4d9604c1bca5148e4b9e14b9315e715081` on RTX 4060 Laptop and WARP. The existing translation and GPU tests each execute exactly one test and pass. The existing GPU test includes width 64, so its success records agreement with this PR's chosen expectations. Native RDNA2 behavior at width 64 or above remains unverified. I added one fixed-table GPU regression restricted to **width 0–63 and offset + width <= 64**. It separately reads the low dword, high dword and SCC for all three operations, with incoming SCC both 0 and 1. Six input vectors per operation × two incoming flags × three readbacks × two backends gives **216 passing dispatches**. It checks BFE setting SCC from the full 64-bit result, signed extraction, and BFM preserving SCC for zero and nonzero masks. One BFE case produces low=0, high=1, which specifically checks the high dword contributes to SCC. Expectations follow [AMD's RDNA2 ISA, SOP2 opcodes 37, 41 and 42, printed page 105](https://docs.amd.com/v/u/en-US/rdna2-shader-instruction-set-architecture). A negative control temporarily changes this branch's `nonzero(self, rl, rh)` to `nonzero(self, rl, rl)`. The original result-only GPU test still passes. The new test fails on hardware for source `0x0000000100000000`, offset 0, width 63 and incoming SCC 0, reporting SCC 0 where the fixed table expects 1. After restoring the implementation and removing diagnostics, a fresh compilation and final runs of both GPU tests and the translation test each pass. There are no skips or ignored tests. The additional patch contains one 94-line synthetic test function. The explicit SDK `dxil.dll` 1.8.2502.11 created `IDxcValidator`. Temporary success markers after DXIL validation and the container-hash assertion confirmed all nine new modules and all six original modules passed both checks. Those markers were removed before the final runs. Independent review checked the fixed values, encodings, bounds, SCC producer/readback, counts and final source restoration. 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 (195 files), `git diff --check` and rustfmt on the added function pass. Native AMD execution, game behavior and the full workspace suite remain unverified. Reproduction uses an installed SDK validator and the 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_64_bit_bitfields_translate', 'emit::valu_tests::scalar_64_bit_bitfields_match_rdna2', 'emit::valu_tests::scalar_64_bit_bitfields_write_or_preserve_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>94-line synthetic test patch 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 1ad12e7..5830af5 100644 --- a/crates/victory-shader/src/emit/valu_tests.rs +++ b/crates/victory-shader/src/emit/valu_tests.rs @@ -337,6 +337,100 @@ fn scalar_64_bit_bitfields_match_rdna2() { } } +#[test] +fn scalar_64_bit_bitfields_write_or_preserve_scc() { + // RDNA2 ISA SOP2 opcodes 37, 41 and 42 (printed page 105). + // New cases use width <= 63 and offset + width <= 64. Each fixed row + // is [low dword, high dword, SCC with incoming 0, SCC with incoming 1]. + type Inputs = [[u32; 3]; 6]; + type Expected = [[u32; 4]; 6]; + let extracts: Inputs = [ + [u32::MAX, u32::MAX, 0], + [1, 0, 1 << 16], + [2, 0, 1 << 16], + [0, 1, 63 << 16], + [0x89AB_CDEF, 0x0123_4567, 28 | 8 << 16], + [0, 0x8000_0000, 60 | 4 << 16], + ]; + let masks: Inputs = [ + [0, 7, 0], + [1, 63, 0], + [32, 0, 0], + [33, 31, 0], + [63, 1, 0], + [31, 33, 0], + ]; + let checks: [(u8, &str, Inputs, Expected); 3] = [ + ( + 0x29, + "s_bfe_u64", + extracts, + [ + [0, 0, 0, 0], + [1, 0, 1, 1], + [0, 0, 0, 0], + [0, 1, 1, 1], + [0x78, 0, 1, 1], + [8, 0, 1, 1], + ], + ), + ( + 0x2A, + "s_bfe_i64", + extracts, + [ + [0, 0, 0, 0], + [u32::MAX, u32::MAX, 1, 1], + [0, 0, 0, 0], + [0, 1, 1, 1], + [0x78, 0, 1, 1], + [0xFFFF_FFF8, u32::MAX, 1, 1], + ], + ), + ( + 0x25, + "s_bfm_b64", + masks, + [ + [0, 0, 0, 1], + [0, 0x8000_0000, 0, 1], + [u32::MAX, 0, 0, 1], + [0x8000_0000, u32::MAX, 0, 1], + [0xFFFF_FFFE, u32::MAX, 0, 1], + [0, 0xFFFF_FFFE, 0, 1], + ], + ), + ]; + for (op, name, inputs, expected) in checks { + let cases: Vec<_> = [0, u32::MAX] + .into_iter() + .flat_map(|seed| inputs.map(|[a, b, c]| [a, b, c, seed])) + .collect(); + let src1 = if op == 0x25 { 1 } else { 2 }; + for (read, label) in [(12, "low"), (13, "high"), (15, "SCC")] { + let shader = build_valu_cs(&[ + sop2(0x00, 14, 3, 0x81), // s3 + 1 seeds SCC with carry 0 or 1 + sop2(op, 12, 0, src1), + sop2(0x0A, 15, 0x81, 0x80), // keep both result dwords intact + vop1(0x001, 0, sgpr(read)), + ]); + run_cases(&shader, &cases, |backend, i, input, got| { + let prior = usize::from(input[3] == u32::MAX); + let column = match read { + 12 => 0, + 13 => 1, + _ => 2 + prior, + }; + let want = expected[i % inputs.len()][column]; + assert_eq!( + got, want, + "{backend}: {name} {label}, prior SCC {prior}, input {input:x?}" + ); + }); + } + } +} + #[test] fn packed_add_selects_halves_of_inline_constants() { // Add the inline constant 1/(2*pi) to v1 (zero) to read back its two halves. ``` </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/97/head:github-bot/gh-97'
git switch 'github-bot/gh-97'

Merge

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