shader: lower v_lerp_u8 #86

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

@⁠lcg007xd-c on GitHub · 2026-11-25 22:14 UTC · original
lcg007xd-c:shader-lerp-u8 → main · +85 −0 · 2 files · 1 commits · open

What is broken

v_lerp_u8 (VOP3 0x14D) decodes, but a program using it fails to translate with v_lerp_u8 is not supported. I found it by translating a one-instruction program for every VOP3 opcode the decoder names. It averages packed unsigned bytes, the usual way to blend two RGBA8 values.

Semantics

AMD's RDNA2 ISA (§12.12, opcode 333) defines it as an "unsigned 8-bit pixel average on packed unsigned bytes": D.u[8i+7:8i] = (S0.u[8i+7:8i] + S1.u[8i+7:8i] + S2.u[8i]) >> 1. Bit 0 of each byte of S2 is the rounding bit: set rounds a .5 up, clear truncates it.

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

Per byte: extract both bytes, add them plus the rounding bit, shift right by one, and place the result back. No byte can carry into the next: the most is (255 + 255 + 1) >> 1 = 255.

Tests

  • emit::lower::tests::byte_average_translates (every host): the encoding decodes to the name, and a program that stores the result translates, with src2 a VGPR or an SGPR. Without the lower.rs change it fails: v_lerp_u8 [d54d0000, 040e0501]: GFX10 code at 0x24: v_lerp_u8 is not supported.
  • valu_tests::v_lerp_u8_averages_bytes_rounding_by_s2 (Windows/GPU): exact averages, .5 truncated and rounded up, 0xFF+0xFF with S2 bytes 0xFE/0xFF (only bit 0 counts), and mixed bytes.
  • 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 a Rust reference on 203 inputs (200 scrambled triples plus edges). 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:14 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/86) `lcg007xd-c:shader-lerp-u8` → `main` · +85 −0 · 2 files · 1 commits · open ## What is broken `v_lerp_u8` (VOP3 **0x14D**) decodes, but a program using it fails to translate with `v_lerp_u8 is not supported`. I found it by translating a one-instruction program for every VOP3 opcode the decoder names. It averages packed unsigned bytes, the usual way to blend two RGBA8 values. ## Semantics AMD's RDNA2 ISA (§12.12, opcode 333) defines it as an "unsigned 8-bit pixel average on packed unsigned bytes": `D.u[8i+7:8i] = (S0.u[8i+7:8i] + S1.u[8i+7:8i] + S2.u[8i]) >> 1`. Bit 0 of each byte of S2 is the rounding bit: set rounds a .5 up, clear truncates it. ## The change (`emit/lower.rs`, in `integer`) Per byte: extract both bytes, add them plus the rounding bit, shift right by one, and place the result back. No byte can carry into the next: the most is `(255 + 255 + 1) >> 1 = 255`. ## Tests - **`emit::lower::tests::byte_average_translates`** (every host): the encoding decodes to the name, and a program that **stores** the result translates, with src2 a VGPR or an SGPR. Without the `lower.rs` change it fails: `v_lerp_u8 [d54d0000, 040e0501]: GFX10 code at 0x24: v_lerp_u8 is not supported`. - **`valu_tests::v_lerp_u8_averages_bytes_rounding_by_s2`** (Windows/GPU): exact averages, .5 truncated and rounded up, `0xFF+0xFF` with S2 bytes `0xFE`/`0xFF` (only bit 0 counts), and mixed bytes. - **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 a Rust reference on 203 inputs (200 scrambled triples plus edges). 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)
VOP3 0x14D decoded but failed to translate ("v_lerp_u8 is not
supported"). AMD's RDNA2 ISA: each byte of the result is the average of
S0's and S1's bytes, rounded up when bit 0 of S2's byte is set:
(S0.u8 + S1.u8 + S2.u[8i]) >> 1.

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

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

I can take native Windows GPU validation at exact head e1302d7f3e61b6a689b92847211db01dfb8617dd. I will run the existing translation and GPU tests, audit missing source-form or byte-boundary coverage, and provide a focused fixed-expectation regression where justified. Results will include actual RTX 4060 Laptop and WARP execution, SDK validation evidence, a targeted negative control, freshly rebuilt restored-source runs, and explicit skipped/native AMD/game scope. AI-assisted with Codex.

**@⁠lmouz823-lang** on GitHub · 2026-11-28 09:05 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/86#issuecomment-7237009354) I can take native Windows GPU validation at exact head `e1302d7f3e61b6a689b92847211db01dfb8617dd`. I will run the existing translation and GPU tests, audit missing source-form or byte-boundary coverage, and provide a focused fixed-expectation regression where justified. Results will include actual RTX 4060 Laptop and WARP execution, SDK validation evidence, a targeted negative control, freshly rebuilt restored-source runs, and explicit skipped/native AMD/game scope. AI-assisted with Codex.
Author
Owner

@⁠lmouz823-lang on GitHub · 2026-11-28 09:19 UTC · original

Windows validation and rounding-source regression

Windows validation for PR #86 at head e1302d7f3e61b6a689b92847211db01dfb8617dd is complete. The base is 2b33e081ed7831f49610b747a508fb56d3cb3aa9. The original tests and a small additional rounding-source test pass through native Windows execution.

Environment and actual execution

  • Windows 11 build 26200, Rust 1.98.1 MSVC, release profile and locked dependencies.
  • NVIDIA RTX 4060 Laptop GPU, driver 610.88, and Microsoft Basic Render Driver / WARP both executed the selected GPU tests.
  • VICTORY_STRICT_TESTS=1, with VICTORY_DXIL explicitly naming the SDK 10.0.26100.0 x64 dxil.dll, version 1.8.2502.11. Its SHA256 was 77e039c905030a641e53658a008b74e90635a5ea9b6b79eabd0f2003bdfca59a.
  • Temporary diagnostic markers in the existing VALU validator branch recorded SDK load, successful validation and matching container hash for all 5 added-test modules and the 1 original-test module. These markers were removed before the final source was rebuilt and retested.
Exact selection Final result Checked dispatches across both adapters
emit::lower::tests::byte_average_translates 1 passed, 0 failed, 0 ignored CPU translation
emit::valu_tests::v_lerp_u8_averages_bytes_rounding_by_s2 1 passed, 0 failed, 0 ignored 10
emit::valu_tests::v_lerp_u8_reads_scalar_inline_and_literal_rounding_bits 1 passed, 0 failed, 0 ignored 40

Each final invocation selected exactly one test, and neither GPU run reported a runtime skip. Counts follow the checked test/helper loops, with both adapters confirmed in the raw logs.

Additional coverage

The attached 48-line test exercises S2 from s3, inline 0, inline 1, inline -1, and a real literal 0x01000100. S0 and S1 remain VGPR inputs. The SGPR value deliberately differs from v3, so selecting the original vector operand accidentally changes the expected result.

The fixed tables check each byte's own rounding bit on odd sums, mixed channel values and maximum byte sums. For example, averaging all-zero with all-one bytes gives 7F7F7F7F for inline 0, 7F7F7F80 for inline 1, 80808080 for inline -1, and 807F807F for literal 01000100. The four input rows under each of five source forms produce 5 × 4 × 2 = 40 checked dispatches from 5 modules. The expected words follow RDNA2 VOP3 opcode 333's per-byte unsigned average. AMD RDNA2 ISA

Negative control and restored-source verification

I temporarily replaced only the literal-S2 value with zero in the v_lerp_u8 lowering arm. The original VGPR-S2 GPU test still passed on both adapters. The added test failed on NVIDIA at its first literal case, input [0, FFFFFFFF, FFFFFFFF, 01000100], with actual 7F7F7F7F, expected 807F807F, and Cargo exit 101. This failure stopped the remaining literal hardware cases and the literal WARP negative run. The earlier SGPR and inline source loops completed before that deliberate failure.

I restored lower.rs byte-for-byte, removed diagnostic logging, cleaned only this shader package's release outputs in the isolated build target, and confirmed a real 1m58s compilation of the final source. All three final exact selections above passed after that rebuild.

Release Clippy with --all-targets -- -D warnings, the 195-file asset policy check, new-function rustfmt, git diff --check, forward application against a separate index of this exact head, and reverse application checks passed. Whole-file rustfmt exits 1 on the pinned original and final source. Both have the same 16 pre-existing formatting hunks and identical ordered changed lines, 83 additions and 30 removals.

Skipped and unverified scope

The selected final tests had zero ignored or skipped cases. Each final exact invocation filtered out the other 244 library tests. Native AMD execution, actual game/visual acceptance, other platforms and the full shader/workspace suites remain unverified. Other S0/S1 source classes, modifiers, destination/source alias combinations and wave64 were outside this targeted test.

An additional LLVM assembler cross-check was attempted with the installed official LLVM 23.1.6 Windows portable tools. That llvm-mc could not target amdgcn-amd-amdhsa, so this check produced no verified assembly encodings. Its failure is excluded from the GPU acceptance counts.

Only crates/victory-shader/src/emit/valu_tests.rs differs from the pinned head, with 48 additions. Final source SHA256 is 48fa2985abb69124b0ca8120222289f0a578a4bde4412c85629b654b2043a95a. Patch SHA256 is a431e41e53dea44299017046fbb78b99d8081f217968888f6d0fc8cef9137f59.

Reproduction

Set VICTORY_STRICT_TESTS=1 and VICTORY_DXIL to an installed SDK x64 dxil.dll. Apply the attached patch and run from the repository root.

cargo test --release --locked -p victory-shader --lib emit::lower::tests::byte_average_translates -- --exact --nocapture --test-threads=1
cargo test --release --locked -p victory-shader --lib emit::valu_tests::v_lerp_u8_averages_bytes_rounding_by_s2 -- --exact --nocapture --test-threads=1
cargo test --release --locked -p victory-shader --lib emit::valu_tests::v_lerp_u8_reads_scalar_inline_and_literal_rounding_bits -- --exact --nocapture --test-threads=1

AI assistance from Codex covered the audit, test patch, execution and this report. The fixed expected words were also independently checked.

Complete rounding-source regression patch against this head
diff --git a/crates/victory-shader/src/emit/valu_tests.rs b/crates/victory-shader/src/emit/valu_tests.rs
index 60d7dc5..85635b4 100644
--- a/crates/victory-shader/src/emit/valu_tests.rs
+++ b/crates/victory-shader/src/emit/valu_tests.rs
@@ -1360,6 +1360,54 @@ fn v_lerp_u8_averages_bytes_rounding_by_s2() {
     });
 }
 
+#[test]
+fn v_lerp_u8_reads_scalar_inline_and_literal_rounding_bits() {
+    let inputs = [
+        [0, u32::MAX, u32::MAX, 0x0100_0100],
+        [0x0302_0100, 0x0203_0405, 0, 0x0001_0001],
+        [0xFFFE_FDFC, 0xFF01_0203, u32::MAX, 0xFEFF_FEFF],
+        [u32::MAX, u32::MAX, 0, u32::MAX],
+    ];
+    for (source, literal, expected) in [
+        (
+            sgpr(3),
+            None,
+            [0x807F_807F, 0x0203_0203, 0xFF80_7F80, u32::MAX],
+        ),
+        (
+            0x80, // inline 0
+            None,
+            [0x7F7F_7F7F, 0x0202_0202, 0xFF7F_7F7F, u32::MAX],
+        ),
+        (
+            0x81, // inline 1 only rounds byte 0 up
+            None,
+            [0x7F7F_7F80, 0x0202_0203, 0xFF7F_7F80, u32::MAX],
+        ),
+        (
+            0xC1, // inline -1 rounds every byte up
+            None,
+            [0x8080_8080, 0x0303_0303, 0xFF80_8080, u32::MAX],
+        ),
+        (
+            0xFF,
+            Some(0x0100_0100),
+            [0x807F_807F, 0x0302_0302, 0xFF7F_807F, u32::MAX],
+        ),
+    ] {
+        let [w0, w1] = vop3(0x14D, 0, vgpr(1), vgpr(2), source);
+        let mut body = vec![w0, w1];
+        body.extend(literal);
+        let shader = build_valu_cs(&body);
+        run_cases(&shader, &inputs, |backend, i, input, got| {
+            assert_eq!(
+                got, expected[i],
+                "{backend}: S2 {source:#x}, literal {literal:x?}, input {input:x?}"
+            );
+        });
+    }
+}
+
 #[test]
 fn v_cmp_class_f16_matches_rdna2() {
     // v_cmp_class_f16_e64 s6, v1, v2 (VOP3 opcode 0x08f); v_cndmask_b32_e64 v0, 0, 1, s6
**@⁠lmouz823-lang** on GitHub · 2026-11-28 09:19 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/86#issuecomment-7237165848) ## Windows validation and rounding-source regression Windows validation for PR #86 at head `e1302d7f3e61b6a689b92847211db01dfb8617dd` is complete. The base is `2b33e081ed7831f49610b747a508fb56d3cb3aa9`. The original tests and a small additional rounding-source test pass through native Windows execution. ### Environment and actual execution - Windows 11 build 26200, Rust 1.98.1 MSVC, release profile and locked dependencies. - NVIDIA RTX 4060 Laptop GPU, driver 610.88, and Microsoft Basic Render Driver / WARP both executed the selected GPU tests. - `VICTORY_STRICT_TESTS=1`, with `VICTORY_DXIL` explicitly naming the SDK 10.0.26100.0 x64 `dxil.dll`, version 1.8.2502.11. Its SHA256 was `77e039c905030a641e53658a008b74e90635a5ea9b6b79eabd0f2003bdfca59a`. - Temporary diagnostic markers in the existing VALU validator branch recorded SDK load, successful validation and matching container hash for all 5 added-test modules and the 1 original-test module. These markers were removed before the final source was rebuilt and retested. | Exact selection | Final result | Checked dispatches across both adapters | | --- | --- | --- | | `emit::lower::tests::byte_average_translates` | 1 passed, 0 failed, 0 ignored | CPU translation | | `emit::valu_tests::v_lerp_u8_averages_bytes_rounding_by_s2` | 1 passed, 0 failed, 0 ignored | 10 | | `emit::valu_tests::v_lerp_u8_reads_scalar_inline_and_literal_rounding_bits` | 1 passed, 0 failed, 0 ignored | 40 | Each final invocation selected exactly one test, and neither GPU run reported a runtime skip. Counts follow the checked test/helper loops, with both adapters confirmed in the raw logs. ### Additional coverage The attached 48-line test exercises S2 from `s3`, inline `0`, inline `1`, inline `-1`, and a real literal `0x01000100`. S0 and S1 remain VGPR inputs. The SGPR value deliberately differs from `v3`, so selecting the original vector operand accidentally changes the expected result. The fixed tables check each byte's own rounding bit on odd sums, mixed channel values and maximum byte sums. For example, averaging all-zero with all-one bytes gives `7F7F7F7F` for inline 0, `7F7F7F80` for inline 1, `80808080` for inline -1, and `807F807F` for literal `01000100`. The four input rows under each of five source forms produce `5 × 4 × 2 = 40` checked dispatches from 5 modules. The expected words follow RDNA2 VOP3 opcode 333's per-byte unsigned average. [AMD RDNA2 ISA](https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/instruction-set-architectures/rdna2-shader-instruction-set-architecture.pdf) ### Negative control and restored-source verification I temporarily replaced only the literal-S2 value with zero in the `v_lerp_u8` lowering arm. The original VGPR-S2 GPU test still passed on both adapters. The added test failed on NVIDIA at its first literal case, input `[0, FFFFFFFF, FFFFFFFF, 01000100]`, with actual `7F7F7F7F`, expected `807F807F`, and Cargo exit 101. This failure stopped the remaining literal hardware cases and the literal WARP negative run. The earlier SGPR and inline source loops completed before that deliberate failure. I restored `lower.rs` byte-for-byte, removed diagnostic logging, cleaned only this shader package's release outputs in the isolated build target, and confirmed a real 1m58s compilation of the final source. All three final exact selections above passed after that rebuild. Release Clippy with `--all-targets -- -D warnings`, the 195-file asset policy check, new-function rustfmt, `git diff --check`, forward application against a separate index of this exact head, and reverse application checks passed. Whole-file rustfmt exits 1 on the pinned original and final source. Both have the same 16 pre-existing formatting hunks and identical ordered changed lines, 83 additions and 30 removals. ### Skipped and unverified scope The selected final tests had zero ignored or skipped cases. Each final exact invocation filtered out the other 244 library tests. Native AMD execution, actual game/visual acceptance, other platforms and the full shader/workspace suites remain unverified. Other S0/S1 source classes, modifiers, destination/source alias combinations and wave64 were outside this targeted test. An additional LLVM assembler cross-check was attempted with the installed official LLVM 23.1.6 Windows portable tools. That `llvm-mc` could not target `amdgcn-amd-amdhsa`, so this check produced no verified assembly encodings. Its failure is excluded from the GPU acceptance counts. Only `crates/victory-shader/src/emit/valu_tests.rs` differs from the pinned head, with 48 additions. Final source SHA256 is `48fa2985abb69124b0ca8120222289f0a578a4bde4412c85629b654b2043a95a`. Patch SHA256 is `a431e41e53dea44299017046fbb78b99d8081f217968888f6d0fc8cef9137f59`. ### Reproduction Set `VICTORY_STRICT_TESTS=1` and `VICTORY_DXIL` to an installed SDK x64 `dxil.dll`. Apply the attached patch and run from the repository root. ```powershell cargo test --release --locked -p victory-shader --lib emit::lower::tests::byte_average_translates -- --exact --nocapture --test-threads=1 cargo test --release --locked -p victory-shader --lib emit::valu_tests::v_lerp_u8_averages_bytes_rounding_by_s2 -- --exact --nocapture --test-threads=1 cargo test --release --locked -p victory-shader --lib emit::valu_tests::v_lerp_u8_reads_scalar_inline_and_literal_rounding_bits -- --exact --nocapture --test-threads=1 ``` AI assistance from Codex covered the audit, test patch, execution and this report. The fixed expected words were also independently checked. <details> <summary>Complete rounding-source regression patch against this head</summary> ```diff diff --git a/crates/victory-shader/src/emit/valu_tests.rs b/crates/victory-shader/src/emit/valu_tests.rs index 60d7dc5..85635b4 100644 --- a/crates/victory-shader/src/emit/valu_tests.rs +++ b/crates/victory-shader/src/emit/valu_tests.rs @@ -1360,6 +1360,54 @@ fn v_lerp_u8_averages_bytes_rounding_by_s2() { }); } +#[test] +fn v_lerp_u8_reads_scalar_inline_and_literal_rounding_bits() { + let inputs = [ + [0, u32::MAX, u32::MAX, 0x0100_0100], + [0x0302_0100, 0x0203_0405, 0, 0x0001_0001], + [0xFFFE_FDFC, 0xFF01_0203, u32::MAX, 0xFEFF_FEFF], + [u32::MAX, u32::MAX, 0, u32::MAX], + ]; + for (source, literal, expected) in [ + ( + sgpr(3), + None, + [0x807F_807F, 0x0203_0203, 0xFF80_7F80, u32::MAX], + ), + ( + 0x80, // inline 0 + None, + [0x7F7F_7F7F, 0x0202_0202, 0xFF7F_7F7F, u32::MAX], + ), + ( + 0x81, // inline 1 only rounds byte 0 up + None, + [0x7F7F_7F80, 0x0202_0203, 0xFF7F_7F80, u32::MAX], + ), + ( + 0xC1, // inline -1 rounds every byte up + None, + [0x8080_8080, 0x0303_0303, 0xFF80_8080, u32::MAX], + ), + ( + 0xFF, + Some(0x0100_0100), + [0x807F_807F, 0x0302_0302, 0xFF7F_807F, u32::MAX], + ), + ] { + let [w0, w1] = vop3(0x14D, 0, vgpr(1), vgpr(2), source); + let mut body = vec![w0, w1]; + body.extend(literal); + let shader = build_valu_cs(&body); + run_cases(&shader, &inputs, |backend, i, input, got| { + assert_eq!( + got, expected[i], + "{backend}: S2 {source:#x}, literal {literal:x?}, input {input:x?}" + ); + }); + } +} + #[test] fn v_cmp_class_f16_matches_rdna2() { // v_cmp_class_f16_e64 s6, v1, v2 (VOP3 opcode 0x08f); v_cndmask_b32_e64 v0, 0, 1, s6 ``` </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/86/head:github-bot/gh-86'
git switch 'github-bot/gh-86'

Merge

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