shader: lower s_brev_b64, s_bitset0/1_b64 and s_flbit_i32_b64/i64 #96

Closed
lcg007xd-c wants to merge 0 commits from upstream-pr/96 into main
lcg007xd-c commented 2026-11-25 22:31:48 +00:00 (Migrated from github.com)

Imported from uiliannn/VItoriaBr. Originally posted by lcg007xd-c on 2026-11-25T22:31:48Z.


What is broken

The 64-bit forms of scalar bit operations decode, but a program using one fails to translate with s_brev_b64 is not supported (or the matching name). Their 32-bit forms already lower.

SOP1 opcode Instruction
0x0C s_brev_b64
0x1C s_bitset0_b64
0x1E s_bitset1_b64
0x16 s_flbit_i32_b64
0x18 s_flbit_i32_i64

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.2, opcodes 12, 22, 24, 28, 30)

  • s_brev_b64: D.u64 is S0's 64 bits in reverse order.
  • s_bitset0_b64 / s_bitset1_b64: clear or set bit S0[5:0] of the 64-bit destination. The IR already reads the old destination for s_bitset*.
  • s_flbit_i32_b64: the first set bit counted from the top, or −1 if there is none.
  • s_flbit_i32_i64: the number of leading bits equal to the sign bit, or −1 if all of them are. A 32-bit literal source sign-extends, which half already does for _i64.
  • SCC: none of them writes it.

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

  • s_brev_b64: each dword is reversed (DXIL Bfrev), and the two are swapped.
  • s_bitset*_b64: bit 5 of the index picks the dword, and bits 4:0 the bit within it.
  • s_flbit_*: DXIL FirstbitHi of each dword, the high dword first. The signed form first XORs both dwords with the sign.

Tests

  • emit::lower::tests::scalar_64_bit_bit_operations_translate (every host): all five decode, and a program that stores the result translates. Without the lower.rs change it fails: s_brev_b64 [be8c0c00, 7e00020c]: GFX10 code at 0x24: s_brev_b64 is not supported.
  • valu_tests::scalar_64_bit_bit_operations_match_rdna2 (Windows/GPU): four inputs. Each dword is checked for s_brev_b64 and the bitsets, the latter with bit indices 3, 37, 63 and 0. The s_flbit cases include 0, −1-like values and a sign bit alone.
  • 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 reverse_bits/leading_zeros and 64-bit masks on 48 inputs (40 scrambled), including an index of 71 that only bits 5:0 count. I also checked a literal −2 for s_flbit_i32_i64 (sign-extended: 63). 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

<!-- greyfinch-sync:uiliannn/VItoriaBr:issue:5500308579 --> Imported from [uiliannn/VItoriaBr](https://github.com/uiliannn/VitoriaBr2.0/pull/96). Originally posted by [lcg007xd-c](https://github.com/lcg007xd-c) on 2026-11-25T22:31:48Z. --- ## What is broken The 64-bit forms of scalar bit operations decode, but a program using one fails to translate with `s_brev_b64 is not supported` (or the matching name). Their 32-bit forms already lower. | SOP1 opcode | Instruction | |---|---| | 0x0C | `s_brev_b64` | | 0x1C | `s_bitset0_b64` | | 0x1E | `s_bitset1_b64` | | 0x16 | `s_flbit_i32_b64` | | 0x18 | `s_flbit_i32_i64` | 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.2, opcodes 12, 22, 24, 28, 30) - **`s_brev_b64`:** `D.u64` is S0's 64 bits in reverse order. - **`s_bitset0_b64` / `s_bitset1_b64`:** clear or set bit `S0[5:0]` of the 64-bit destination. The IR already reads the old destination for `s_bitset*`. - **`s_flbit_i32_b64`:** the first set bit counted from the top, or −1 if there is none. - **`s_flbit_i32_i64`:** the number of leading bits equal to the sign bit, or −1 if all of them are. A 32-bit literal source sign-extends, which `half` already does for `_i64`. - **SCC:** none of them writes it. ## The change (`emit/lower.rs`, in `sop`'s 64-bit section) - **`s_brev_b64`:** each dword is reversed (DXIL `Bfrev`), and the two are swapped. - **`s_bitset*_b64`:** bit 5 of the index picks the dword, and bits 4:0 the bit within it. - **`s_flbit_*`:** DXIL `FirstbitHi` of each dword, the high dword first. The signed form first XORs both dwords with the sign. ## Tests - **`emit::lower::tests::scalar_64_bit_bit_operations_translate`** (every host): all five decode, and a program that **stores** the result translates. Without the `lower.rs` change it fails: `s_brev_b64 [be8c0c00, 7e00020c]: GFX10 code at 0x24: s_brev_b64 is not supported`. - **`valu_tests::scalar_64_bit_bit_operations_match_rdna2`** (Windows/GPU): four inputs. Each dword is checked for `s_brev_b64` and the bitsets, the latter with bit indices 3, 37, 63 and 0. The `s_flbit` cases include 0, −1-like values and a sign bit alone. - **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 `reverse_bits`/`leading_zeros` and 64-bit masks on 48 inputs (40 scrambled), including an index of 71 that only bits 5:0 count. I also checked a literal `−2` for `s_flbit_i32_i64` (sign-extended: 63). 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)
lmouz823-lang commented 2026-11-28 05:12:14 +00:00 (Migrated from github.com)

Imported from uiliannn/VItoriaBr. Originally posted by lmouz823-lang on 2026-11-28T05:12:14Z.


I can take native Windows GPU validation at exact head ab2a558ccb42305edc93006362080be8c0aa8281. I will run the existing translation and result tests, add focused fixed-expectation coverage for 64-bit boundary indices and preservation of incoming SCC, and use a temporary regression to verify the added coverage. Final restored-source runs will use RTX 4060 Laptop and WARP with the SDK validator. I will report exact counts, any skipped cases and the remaining native AMD/game scope. AI-assisted with Codex.

<!-- greyfinch-sync:uiliannn/VItoriaBr:comment:7234522303 --> Imported from [uiliannn/VItoriaBr](https://github.com/uiliannn/VItoriaBr/pull/96#issuecomment-7234522303). Originally posted by [lmouz823-lang](https://github.com/lmouz823-lang) on 2026-11-28T05:12:14Z. --- I can take native Windows GPU validation at exact head `ab2a558ccb42305edc93006362080be8c0aa8281`. I will run the existing translation and result tests, add focused fixed-expectation coverage for 64-bit boundary indices and preservation of incoming SCC, and use a temporary regression to verify the added coverage. Final restored-source runs will use RTX 4060 Laptop and WARP with the SDK validator. I will report exact counts, any skipped cases and the remaining native AMD/game scope. AI-assisted with Codex.
lmouz823-lang commented 2026-11-28 05:22:53 +00:00 (Migrated from github.com)

Imported from uiliannn/VItoriaBr. Originally posted by lmouz823-lang on 2026-11-28T05:22:53Z.


Native Windows GPU validation completed for exact head ab2a558ccb42305edc93006362080be8c0aa8281. The existing translation and GPU regressions each execute exactly one test and pass. The original GPU case runs 64 dispatches across RTX 4060 Laptop and WARP.

I added one fixed-expectation regression covering all five instructions' SCC preservation with incoming SCC 0 and 1. Six vectors cover zero, all ones, high-only data, the sign bit and mixed dwords, with bitset indices 0, 31, 32, 63, 64 and 71. Result dwords and SCC are read separately. A separate 32-bit literal -2 checks sign extension for s_flbit_i32_i64, with result 63 and both incoming flags retained. Expectations follow AMD's RDNA2 ISA, SOP1 opcodes 12, 22, 24, 28 and 30, printed pages 112 and 115–116.

The main matrix uses 13 shaders × 12 inputs × two backends, plus two literal shaders × two inputs × two backends, giving 320 passing dispatches. The added patch is 125 test-only lines. Independent review checked the fixed result tables, encodings, seed/readback registers and counts.

A temporary negative control replaces the bitset dword selector (index & 32) != 0 with index >= 32. The original GPU regression still passes because its indices are below 64. The new regression fails on hardware at index 64, observing high dword 0x7ffffffe instead of 0x7fffffff for s_bitset0_b64. That exact test exits 101. I then restored the implementation byte-for-byte, removed temporary diagnostics and confirmed a fresh 1m09s compilation before the final 320-dispatch pass. Both original exact tests also pass again. 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 15 new modules and all eight original modules passed those checks. The markers are absent from the final patch and runs.

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), new-function rustfmt and git diff --check pass. Whole-file rustfmt exits 1 on both the original and patched source, with the same 17 existing formatting hunks after removing file/line headers. Native AMD execution, the full shader/workspace suites and game behavior remain unverified. This validation covers the listed synthetic source patterns and SCC/index 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_64_bit_bit_operations_translate',
    'emit::valu_tests::scalar_64_bit_bit_operations_match_rdna2',
    'emit::valu_tests::scalar_64_bit_operations_preserve_scc_and_wrap_indices'
)
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.

125-line synthetic GPU 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 77eb189..e4b395c 100644
--- a/crates/victory-shader/src/emit/valu_tests.rs
+++ b/crates/victory-shader/src/emit/valu_tests.rs
@@ -1668,6 +1668,131 @@ fn whole_wave_f64_sum_and_cvt_f32_f64() {
     }
 }
 
+#[test]
+fn scalar_64_bit_operations_preserve_scc_and_wrap_indices() {
+    // RDNA2 SOP1 opcodes 12, 22, 24, 28 and 30 preserve SCC.
+    // Fixed columns are the result's low/high dwords; FLBIT writes only low.
+    let inputs = [
+        [0, 0, 0],
+        [u32::MAX, u32::MAX, 31],
+        [0, 1, 32],
+        [0, 0x8000_0000, 63],
+        [0x8000_0000, 0x7FFF_FFFF, 64],
+        [0x0123_4567, 0xFEDC_BA98, 71],
+    ];
+    let checks = [
+        (
+            0x0C,
+            "s_brev_b64",
+            [
+                [0, 0],
+                [u32::MAX, u32::MAX],
+                [0x8000_0000, 0],
+                [1, 0],
+                [0xFFFF_FFFE, 1],
+                [0x195D_3B7F, 0xE6A2_C480],
+            ],
+        ),
+        (
+            0x1C,
+            "s_bitset0_b64",
+            [
+                [0, 0],
+                [0x7FFF_FFFF, u32::MAX],
+                [0, 0],
+                [0, 0],
+                [0x8000_0000, 0x7FFF_FFFF],
+                [0x0123_4567, 0xFEDC_BA98],
+            ],
+        ),
+        (
+            0x1E,
+            "s_bitset1_b64",
+            [
+                [1, 0],
+                [u32::MAX, u32::MAX],
+                [0, 1],
+                [0, 0x8000_0000],
+                [0x8000_0001, 0x7FFF_FFFF],
+                [0x0123_45E7, 0xFEDC_BA98],
+            ],
+        ),
+        (
+            0x16,
+            "s_flbit_i32_b64",
+            [[u32::MAX, 0], [0, 0], [31, 0], [0, 0], [1, 0], [0, 0]],
+        ),
+        (
+            0x18,
+            "s_flbit_i32_i64",
+            [
+                [u32::MAX, 0],
+                [u32::MAX, 0],
+                [31, 0],
+                [1, 0],
+                [1, 0],
+                [7, 0],
+            ],
+        ),
+    ];
+    let cases: Vec<_> = [0, u32::MAX]
+        .into_iter()
+        .flat_map(|seed| inputs.map(|[a, b, bit]| [a, b, bit, seed]))
+        .collect();
+    for (op, name, expected) in checks {
+        let bitset = op == 0x1C || op == 0x1E;
+        let reads: &[u8] = if op == 0x16 || op == 0x18 {
+            &[12, 15]
+        } else {
+            &[12, 13, 15]
+        };
+        for &read in reads {
+            let mut body = vec![sop2(0x00, 14, 3, 0x81)]; // SCC from s3 + 1
+            if bitset {
+                body.push(0xBE8C_0400); // copy s[0:1] to destination s[12:13]
+            }
+            body.push(0xBE80_0000 | 12 << 16 | op << 8 | if bitset { 2 } else { 0 });
+            body.push(sop2(0x0A, 15, 0x81, 0x80)); // materialize SCC, retain s12/13
+            body.push(vop1(0x001, 0, sgpr(read)));
+            let shader = build_valu_cs(&body);
+            run_cases(&shader, &cases, |backend, i, input, got| {
+                let prior = u32::from(input[3] == u32::MAX);
+                let want = if read == 15 {
+                    prior
+                } else {
+                    expected[i % inputs.len()][usize::from(read == 13)]
+                };
+                assert_eq!(
+                    got, want,
+                    "{backend}: {name}, s{read}, prior SCC {prior}, input {input:x?}"
+                );
+            });
+        }
+    }
+    // A 32-bit literal -2 is sign-extended to 64 bits, leaving 63 leading ones.
+    for read in [12, 15] {
+        let shader = build_valu_cs(&[
+            sop2(0x00, 14, 3, 0x81),
+            0xBE8C_18FF,
+            0xFFFF_FFFE,
+            sop2(0x0A, 15, 0x81, 0x80),
+            vop1(0x001, 0, sgpr(read)),
+        ]);
+        run_cases(
+            &shader,
+            &[[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 { 63 };
+                assert_eq!(
+                    got, want,
+                    "{backend}: literal -2, s{read}, prior SCC {prior}"
+                );
+            },
+        );
+    }
+}
+
 #[test]
 fn scalar_64_bit_bit_operations_match_rdna2() {
     // op s[12:13], s[0:1] (s_bitset*: s[12:13] = s[0:1] first, bit s2);
<!-- greyfinch-sync:uiliannn/VItoriaBr:comment:7234633271 --> Imported from [uiliannn/VItoriaBr](https://github.com/uiliannn/VItoriaBr/pull/96#issuecomment-7234633271). Originally posted by [lmouz823-lang](https://github.com/lmouz823-lang) on 2026-11-28T05:22:53Z. --- Native Windows GPU validation completed for exact head `ab2a558ccb42305edc93006362080be8c0aa8281`. The existing translation and GPU regressions each execute exactly one test and pass. The original GPU case runs 64 dispatches across RTX 4060 Laptop and WARP. I added one fixed-expectation regression covering all five instructions' SCC preservation with incoming SCC 0 and 1. Six vectors cover zero, all ones, high-only data, the sign bit and mixed dwords, with bitset indices 0, 31, 32, 63, 64 and 71. Result dwords and SCC are read separately. A separate 32-bit literal `-2` checks sign extension for `s_flbit_i32_i64`, with result 63 and both incoming flags retained. Expectations follow [AMD's RDNA2 ISA, SOP1 opcodes 12, 22, 24, 28 and 30, printed pages 112 and 115–116](https://docs.amd.com/v/u/en-US/rdna2-shader-instruction-set-architecture). The main matrix uses 13 shaders × 12 inputs × two backends, plus two literal shaders × two inputs × two backends, giving **320 passing dispatches**. The added patch is 125 test-only lines. Independent review checked the fixed result tables, encodings, seed/readback registers and counts. A temporary negative control replaces the bitset dword selector `(index & 32) != 0` with `index >= 32`. The original GPU regression still passes because its indices are below 64. The new regression fails on hardware at index 64, observing high dword `0x7ffffffe` instead of `0x7fffffff` for `s_bitset0_b64`. That exact test exits 101. I then restored the implementation byte-for-byte, removed temporary diagnostics and confirmed a fresh 1m09s compilation before the final 320-dispatch pass. Both original exact tests also pass again. 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 15 new modules and all eight original modules passed those checks. The markers are absent from the final patch and runs. 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), new-function rustfmt and `git diff --check` pass. Whole-file rustfmt exits 1 on both the original and patched source, with the same 17 existing formatting hunks after removing file/line headers. Native AMD execution, the full shader/workspace suites and game behavior remain unverified. This validation covers the listed synthetic source patterns and SCC/index 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_64_bit_bit_operations_translate', 'emit::valu_tests::scalar_64_bit_bit_operations_match_rdna2', 'emit::valu_tests::scalar_64_bit_operations_preserve_scc_and_wrap_indices' ) 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>125-line synthetic GPU 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 77eb189..e4b395c 100644 --- a/crates/victory-shader/src/emit/valu_tests.rs +++ b/crates/victory-shader/src/emit/valu_tests.rs @@ -1668,6 +1668,131 @@ fn whole_wave_f64_sum_and_cvt_f32_f64() { } } +#[test] +fn scalar_64_bit_operations_preserve_scc_and_wrap_indices() { + // RDNA2 SOP1 opcodes 12, 22, 24, 28 and 30 preserve SCC. + // Fixed columns are the result's low/high dwords; FLBIT writes only low. + let inputs = [ + [0, 0, 0], + [u32::MAX, u32::MAX, 31], + [0, 1, 32], + [0, 0x8000_0000, 63], + [0x8000_0000, 0x7FFF_FFFF, 64], + [0x0123_4567, 0xFEDC_BA98, 71], + ]; + let checks = [ + ( + 0x0C, + "s_brev_b64", + [ + [0, 0], + [u32::MAX, u32::MAX], + [0x8000_0000, 0], + [1, 0], + [0xFFFF_FFFE, 1], + [0x195D_3B7F, 0xE6A2_C480], + ], + ), + ( + 0x1C, + "s_bitset0_b64", + [ + [0, 0], + [0x7FFF_FFFF, u32::MAX], + [0, 0], + [0, 0], + [0x8000_0000, 0x7FFF_FFFF], + [0x0123_4567, 0xFEDC_BA98], + ], + ), + ( + 0x1E, + "s_bitset1_b64", + [ + [1, 0], + [u32::MAX, u32::MAX], + [0, 1], + [0, 0x8000_0000], + [0x8000_0001, 0x7FFF_FFFF], + [0x0123_45E7, 0xFEDC_BA98], + ], + ), + ( + 0x16, + "s_flbit_i32_b64", + [[u32::MAX, 0], [0, 0], [31, 0], [0, 0], [1, 0], [0, 0]], + ), + ( + 0x18, + "s_flbit_i32_i64", + [ + [u32::MAX, 0], + [u32::MAX, 0], + [31, 0], + [1, 0], + [1, 0], + [7, 0], + ], + ), + ]; + let cases: Vec<_> = [0, u32::MAX] + .into_iter() + .flat_map(|seed| inputs.map(|[a, b, bit]| [a, b, bit, seed])) + .collect(); + for (op, name, expected) in checks { + let bitset = op == 0x1C || op == 0x1E; + let reads: &[u8] = if op == 0x16 || op == 0x18 { + &[12, 15] + } else { + &[12, 13, 15] + }; + for &read in reads { + let mut body = vec![sop2(0x00, 14, 3, 0x81)]; // SCC from s3 + 1 + if bitset { + body.push(0xBE8C_0400); // copy s[0:1] to destination s[12:13] + } + body.push(0xBE80_0000 | 12 << 16 | op << 8 | if bitset { 2 } else { 0 }); + body.push(sop2(0x0A, 15, 0x81, 0x80)); // materialize SCC, retain s12/13 + body.push(vop1(0x001, 0, sgpr(read))); + let shader = build_valu_cs(&body); + run_cases(&shader, &cases, |backend, i, input, got| { + let prior = u32::from(input[3] == u32::MAX); + let want = if read == 15 { + prior + } else { + expected[i % inputs.len()][usize::from(read == 13)] + }; + assert_eq!( + got, want, + "{backend}: {name}, s{read}, prior SCC {prior}, input {input:x?}" + ); + }); + } + } + // A 32-bit literal -2 is sign-extended to 64 bits, leaving 63 leading ones. + for read in [12, 15] { + let shader = build_valu_cs(&[ + sop2(0x00, 14, 3, 0x81), + 0xBE8C_18FF, + 0xFFFF_FFFE, + sop2(0x0A, 15, 0x81, 0x80), + vop1(0x001, 0, sgpr(read)), + ]); + run_cases( + &shader, + &[[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 { 63 }; + assert_eq!( + got, want, + "{backend}: literal -2, s{read}, prior SCC {prior}" + ); + }, + ); + } +} + #[test] fn scalar_64_bit_bit_operations_match_rdna2() { // op s[12:13], s[0:1] (s_bitset*: s[12:13] = s[0:1] first, bit s2); ``` </details>

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
uiliannn/VItoriaBr!96
No description provided.