Shaders: 16-bit image derivatives pack two to a dword; cd_g16 and gather4h names #69

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

@⁠lcg007xd-c on GitHub · 2026-11-25 21:34 UTC · original
lcg007xd-c:fix/mimg-g16-decode → main · +100 −1 · 4 files · 2 commits · open

What

image_addr_dwords counted the derivatives of a _g16 form (16-bit gradients) as whole dwords. For example, image_sample_d_g16 on a 2D image was decoded as reading six address VGPRs, but the instruction has four (llvm-mc prints v[0:3]). The same error hit every d_g16 variant in 2D, 3D, cube and the arrays. _g16 now packs the derivatives in pairs, the way a16 already did. Nothing else changes.

The MIMG table also gets the names LLVM uses for 0xE8–0xEF (image_sample_cd_g16 … image_sample_c_cd_cl_o_g16) and 0x61 (image_gather4h). None of them lowers yet. The lowering matches exact names, so they are refused as unsupported, and a test pins that.

Today the lowering refuses d_g16 too, so the wrong count showed up only in the decoded Fields and the SP3 text, plus the extra VGPRs the IR thought those instructions read. It matters as soon as someone lowers _g16.

How it was checked

  • Full sweep against LLVM. I decoded every image_sample* / image_gather4* opcode in the table, in each of the dims that can be sampled (1D, 2D, 3D, cube, 1D array, 2D array), and compared the address size with llvm-mc -triple=amdgcn -mcpu=gfx1030 --disassemble. Before: 32 disagreements, all of them _g16. After: none. The MSAA dims differ for every sample form, because LLVM prints those oddly; you can't sample an MSAA image, so I left them out.
  • New test. g16_derivatives_pack_in_pairs pins 14 of those words to LLVM's exact text, including the new names. It fails without the fields.rs change.
  • New test. g16_and_gather4h_forms_are_refused checks that the lowering returns Unsupported for each of the new names and for d_g16.
  • CI commands. The portable CI commands and Windows-target clippy (--target x86_64-pc-windows-msvc) pass locally.

Not tested: Windows runtime, GPU, and the game. Nothing here changes generated DXIL.

Note: the Windows check job fails on current main for an unrelated reason (5668b8b, missing Layout::bytes_from), which #64 fixes.

🤖 Generated with Claude Code

**@⁠lcg007xd-c** on GitHub · 2026-11-25 21:34 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/69) `lcg007xd-c:fix/mimg-g16-decode` → `main` · +100 −1 · 4 files · 2 commits · open ## What `image_addr_dwords` counted the derivatives of a `_g16` form (16-bit gradients) as whole dwords. For example, `image_sample_d_g16` on a 2D image was decoded as reading six address VGPRs, but the instruction has four (`llvm-mc` prints `v[0:3]`). The same error hit every `d_g16` variant in 2D, 3D, cube and the arrays. `_g16` now packs the derivatives in pairs, the way `a16` already did. Nothing else changes. The MIMG table also gets the names LLVM uses for 0xE8–0xEF (`image_sample_cd_g16` … `image_sample_c_cd_cl_o_g16`) and 0x61 (`image_gather4h`). None of them lowers yet. The lowering matches exact names, so they are refused as unsupported, and a test pins that. Today the lowering refuses `d_g16` too, so the wrong count showed up only in the decoded `Fields` and the SP3 text, plus the extra VGPRs the IR thought those instructions read. It matters as soon as someone lowers `_g16`. ## How it was checked - **Full sweep against LLVM.** I decoded every `image_sample*` / `image_gather4*` opcode in the table, in each of the dims that can be sampled (1D, 2D, 3D, cube, 1D array, 2D array), and compared the address size with `llvm-mc -triple=amdgcn -mcpu=gfx1030 --disassemble`. Before: 32 disagreements, all of them `_g16`. After: none. The MSAA dims differ for every sample form, because LLVM prints those oddly; you can't sample an MSAA image, so I left them out. - **New test.** `g16_derivatives_pack_in_pairs` pins 14 of those words to LLVM's exact text, including the new names. It fails without the `fields.rs` change. - **New test.** `g16_and_gather4h_forms_are_refused` checks that the lowering returns `Unsupported` for each of the new names and for `d_g16`. - **CI commands.** The portable CI commands and Windows-target clippy (`--target x86_64-pc-windows-msvc`) pass locally. Not tested: Windows runtime, GPU, and the game. Nothing here changes generated DXIL. Note: the Windows `check` job fails on current main for an unrelated reason (5668b8b, missing `Layout::bytes_from`), which #64 fixes. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
image_addr_dwords counted a _g16 form's derivatives as whole dwords, so
image_sample_d_g16 on a 2D image read six address VGPRs where the
instruction has four (llvm-mc: v[0:3]), and every d_g16 variant in 2D,
3D, cube and 2D arrays was two (or three) too long. `_g16` now packs the
derivatives in pairs, the way `a16` already did. Against llvm-mc's
disassembly of every image_sample/gather4 opcode in the 1D to 2D-array
dims, the counts now all agree (before: 32 disagreements, all _g16).

The table also gets the names LLVM gives MIMG 0xE8-0xEF
(image_sample_cd_g16 to image_sample_c_cd_cl_o_g16) and 0x61
(image_gather4h). None of these lowers yet; the lowering matches exact
names, and a test checks they are refused as unsupported.

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

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

I can take native Windows metadata validation at exact head 14a9828e02e61bae3b460a5eb939faec283ea3a8. This follows the image-name cases recorded in #87. I will run the existing g16 text and unsupported-lowering regressions, then check focused address/register boundaries and supported dimension/flag controls through decoded fields or IR. I will report exact counts, negative controls, restored-source verification and omitted LLVM/GPU/game coverage with a minimal justified patch. AI-assisted with Codex.

**@⁠lmouz823-lang** on GitHub · 2026-11-28 06:11 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/69#issuecomment-7235142989) I can take native Windows metadata validation at exact head `14a9828e02e61bae3b460a5eb939faec283ea3a8`. This follows the image-name cases recorded in #87. I will run the existing g16 text and unsupported-lowering regressions, then check focused address/register boundaries and supported dimension/flag controls through decoded fields or IR. I will report exact counts, negative controls, restored-source verification and omitted LLVM/GPU/game coverage with a minimal justified patch. AI-assisted with Codex.
Author
Owner

@⁠lmouz823-lang on GitHub · 2026-11-28 06:21 UTC · original

Native Windows metadata and IR validation completed for exact head 14a9828e02e61bae3b460a5eb939faec283ea3a8. The existing SP3 test executes one exact test and passes, checking its 14 fixed text examples. The existing emit::lower::image::tests::g16_and_gather4h_forms_are_refused also executes one exact test and passes.

I added one synthetic regression, 48 test-only added lines, through the normal GFX10 decode, Instruction::fields and ir::build path. It checks the contiguous address range in Fields::Image and exactly which high VGPRs the IR reads. Address registers start at v240 while data starts at v16, keeping those ranges separate.

Six non-MSAA dimensions use fixed G16 dword counts, 3/4/7/5/4/5 with full-width coordinates and 3/3/6/4/3/4 with a16 coordinates. Two ordinary image_sample_d controls use 2D and 3D counts 6/9 or a16 counts 3/6. Both a16 states and wave32/wave64 run for every row, giving 32 synthetic IR programs per invocation. Each program checks v240 through v255 for the precise read boundary. The encodings are hand-written from the existing MIMG encoder and opcode table.

A temporary negative control drops image address reads in the IR by adding .take(0) to its address iterator. Both original PR exact tests still pass, and the original seven IR tests also pass. The new exact test fails at the first G16 case because v240 is missing from its reads, with exit 101. After restoring that production line, I cleaned only the isolated shader-package target, confirmed a fresh 11.98-second compilation, and reran the final checks.

  • The complete GFX10 group passes 13/13.
  • The IR group passes 8/8, including the added regression.
  • The original SP3, original Unsupported and new IR exact selections each execute one test and pass.
  • All selected tests have zero ignored tests and no runtime skip diagnostics.

As a separate read-only cross-check, the nine newly named forms match the names stored in #87's committed fixture at b7104dbd13a005ff4a3942aef795d0fb7c85e15f. That fixture's SHA256 is cef83c381ac1edd37c7850143dff989563fe69d11196b7df19a9d3d5e83f2f96. This comparison uses the stored names and this head's MIMG opcode table. Local LLVM assembly or regeneration was outside the validation.

Two preliminary run errors were excluded from acceptance. An incorrectly qualified Unsupported filter selected zero tests, which the runner rejected. The corrected full path above then executed one passing test. The first draft's ordinary control used opcode 0x028 (image_sample_c) and failed its expected derivative count. Correcting it to 0x022 (image_sample_d) compiled and passed before the formal negative control. Those preliminary logs are retained separately.

Environment was Windows 11, Rust 1.98.1 MSVC, the default test profile and an isolated target. Package Clippy with --all-targets -- -D warnings, new-function rustfmt, git diff --check and asset policy (196 files) pass. Whole-file rustfmt exits 1 on both the head and final IR source, with the same 18 existing formatting hunks after file/line headers are removed. Removing the added test reproduces the original source after newline normalization.

Scope is decoding, operand metadata and SSA register reads. The original Unsupported test continues to require these G16/gather4h forms to be refused by lowering. GPU/DXIL execution, sampling correctness, actual game behavior, NSA-address variants, other platforms and the remaining shader/workspace suites remain unverified.

Reproduce after applying the attached patch.

cargo test --locked -p victory-shader --lib gfx10::sp3::tests::g16_derivatives_pack_in_pairs -- --exact --nocapture --test-threads=1
cargo test --locked -p victory-shader --lib emit::lower::image::tests::g16_and_gather4h_forms_are_refused -- --exact --nocapture --test-threads=1
cargo test --locked -p victory-shader --lib ir::tests::g16_address_ranges_reach_ir_without_extra_registers -- --exact --nocapture --test-threads=1
cargo test --locked -p victory-shader --lib gfx10:: -- --nocapture --test-threads=1
cargo test --locked -p victory-shader --lib ir::tests:: -- --nocapture --test-threads=1

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

Focused IR register-range regression
diff --git a/crates/victory-shader/src/ir.rs b/crates/victory-shader/src/ir.rs
index 0b8fd9c..3711615 100644
--- a/crates/victory-shader/src/ir.rs
+++ b/crates/victory-shader/src/ir.rs
@@ -1553,6 +1553,54 @@ mod tests {
         build(&code, wave64, 256).unwrap()
     }
 
+    #[test]
+    fn g16_address_ranges_reach_ir_without_extra_registers() {
+        // G16 has packed derivatives; a16 also packs the coordinate halfwords.
+        // The final two rows are ordinary 2D/3D derivative controls.
+        for (opcode, dim, counts) in [
+            (0x0E8, 0, [3, 3]),
+            (0x0E8, 1, [4, 3]),
+            (0x0E8, 2, [7, 6]),
+            (0x0E8, 3, [5, 4]),
+            (0x0E8, 4, [4, 3]),
+            (0x0E8, 5, [5, 4]),
+            (0x022, 1, [6, 3]),
+            (0x022, 2, [9, 6]),
+        ] {
+            for a16 in [false, true] {
+                let count = counts[usize::from(a16)];
+                for wave64 in [false, true] {
+                    let program = build_words(
+                        &[
+                            0xF000_0000 | (opcode & 0x7F) << 18 | 0xF00 | dim << 3 | opcode >> 7,
+                            0x0040_10F0 | u32::from(a16) << 30,
+                            S_ENDPGM,
+                        ],
+                        wave64,
+                    );
+                    let image = &program.blocks[0].insts[0];
+                    let Fields::Image { addr, .. } = &image.fields else {
+                        panic!("unexpected fields: {:?}", image.fields);
+                    };
+                    assert_eq!(
+                        addr,
+                        &[Operand::Vector {
+                            reg: 240,
+                            dwords: count
+                        }]
+                    );
+                    for reg in 240..=u8::MAX {
+                        assert_eq!(
+                            image.read(Reg::V(reg)).is_some(),
+                            reg < 240 + count,
+                            "opcode {opcode:#x}, dim {dim}, a16 {a16}, wave64 {wave64}, v{reg}"
+                        );
+                    }
+                }
+            }
+        }
+    }
+
     fn input(p: &Program, reg: Reg) -> Value {
         p.inputs.iter().find(|i| i.0 == reg).expect("input").1
     }
**@⁠lmouz823-lang** on GitHub · 2026-11-28 06:21 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/69#issuecomment-7235247267) Native Windows metadata and IR validation completed for exact head `14a9828e02e61bae3b460a5eb939faec283ea3a8`. The existing SP3 test executes one exact test and passes, checking its 14 fixed text examples. The existing `emit::lower::image::tests::g16_and_gather4h_forms_are_refused` also executes one exact test and passes. I added one synthetic regression, **48 test-only added lines**, through the normal GFX10 decode, `Instruction::fields` and `ir::build` path. It checks the contiguous address range in `Fields::Image` and exactly which high VGPRs the IR reads. Address registers start at v240 while data starts at v16, keeping those ranges separate. Six non-MSAA dimensions use fixed G16 dword counts, **3/4/7/5/4/5** with full-width coordinates and **3/3/6/4/3/4** with a16 coordinates. Two ordinary `image_sample_d` controls use 2D and 3D counts **6/9** or a16 counts **3/6**. Both a16 states and wave32/wave64 run for every row, giving **32 synthetic IR programs per invocation**. Each program checks v240 through v255 for the precise read boundary. The encodings are hand-written from the existing MIMG encoder and opcode table. A temporary negative control drops image address reads in the IR by adding `.take(0)` to its address iterator. Both original PR exact tests still pass, and the original seven IR tests also pass. The new exact test fails at the first G16 case because v240 is missing from its reads, with exit 101. After restoring that production line, I cleaned only the isolated shader-package target, confirmed a fresh **11.98-second compilation**, and reran the final checks. - The complete GFX10 group passes **13/13**. - The IR group passes **8/8**, including the added regression. - The original SP3, original Unsupported and new IR exact selections each execute one test and pass. - All selected tests have zero ignored tests and no runtime skip diagnostics. As a separate read-only cross-check, the nine newly named forms match the names stored in #87's committed fixture at `b7104dbd13a005ff4a3942aef795d0fb7c85e15f`. That fixture's SHA256 is `cef83c381ac1edd37c7850143dff989563fe69d11196b7df19a9d3d5e83f2f96`. This comparison uses the stored names and this head's MIMG opcode table. Local LLVM assembly or regeneration was outside the validation. Two preliminary run errors were excluded from acceptance. An incorrectly qualified Unsupported filter selected zero tests, which the runner rejected. The corrected full path above then executed one passing test. The first draft's ordinary control used opcode `0x028` (`image_sample_c`) and failed its expected derivative count. Correcting it to `0x022` (`image_sample_d`) compiled and passed before the formal negative control. Those preliminary logs are retained separately. Environment was Windows 11, Rust 1.98.1 MSVC, the default test profile and an isolated target. Package Clippy with `--all-targets -- -D warnings`, new-function rustfmt, `git diff --check` and asset policy (**196 files**) pass. Whole-file rustfmt exits 1 on both the head and final IR source, with the same 18 existing formatting hunks after file/line headers are removed. Removing the added test reproduces the original source after newline normalization. Scope is decoding, operand metadata and SSA register reads. The original Unsupported test continues to require these G16/gather4h forms to be refused by lowering. GPU/DXIL execution, sampling correctness, actual game behavior, NSA-address variants, other platforms and the remaining shader/workspace suites remain unverified. Reproduce after applying the attached patch. ```powershell cargo test --locked -p victory-shader --lib gfx10::sp3::tests::g16_derivatives_pack_in_pairs -- --exact --nocapture --test-threads=1 cargo test --locked -p victory-shader --lib emit::lower::image::tests::g16_and_gather4h_forms_are_refused -- --exact --nocapture --test-threads=1 cargo test --locked -p victory-shader --lib ir::tests::g16_address_ranges_reach_ir_without_extra_registers -- --exact --nocapture --test-threads=1 cargo test --locked -p victory-shader --lib gfx10:: -- --nocapture --test-threads=1 cargo test --locked -p victory-shader --lib ir::tests:: -- --nocapture --test-threads=1 ``` AI assistance from Codex covered the synthetic regression, execution and this report. <details> <summary>Focused IR register-range regression</summary> ```diff diff --git a/crates/victory-shader/src/ir.rs b/crates/victory-shader/src/ir.rs index 0b8fd9c..3711615 100644 --- a/crates/victory-shader/src/ir.rs +++ b/crates/victory-shader/src/ir.rs @@ -1553,6 +1553,54 @@ mod tests { build(&code, wave64, 256).unwrap() } + #[test] + fn g16_address_ranges_reach_ir_without_extra_registers() { + // G16 has packed derivatives; a16 also packs the coordinate halfwords. + // The final two rows are ordinary 2D/3D derivative controls. + for (opcode, dim, counts) in [ + (0x0E8, 0, [3, 3]), + (0x0E8, 1, [4, 3]), + (0x0E8, 2, [7, 6]), + (0x0E8, 3, [5, 4]), + (0x0E8, 4, [4, 3]), + (0x0E8, 5, [5, 4]), + (0x022, 1, [6, 3]), + (0x022, 2, [9, 6]), + ] { + for a16 in [false, true] { + let count = counts[usize::from(a16)]; + for wave64 in [false, true] { + let program = build_words( + &[ + 0xF000_0000 | (opcode & 0x7F) << 18 | 0xF00 | dim << 3 | opcode >> 7, + 0x0040_10F0 | u32::from(a16) << 30, + S_ENDPGM, + ], + wave64, + ); + let image = &program.blocks[0].insts[0]; + let Fields::Image { addr, .. } = &image.fields else { + panic!("unexpected fields: {:?}", image.fields); + }; + assert_eq!( + addr, + &[Operand::Vector { + reg: 240, + dwords: count + }] + ); + for reg in 240..=u8::MAX { + assert_eq!( + image.read(Reg::V(reg)).is_some(), + reg < 240 + count, + "opcode {opcode:#x}, dim {dim}, a16 {a16}, wave64 {wave64}, v{reg}" + ); + } + } + } + } + } + fn input(p: &Program, reg: Reg) -> Value { p.inputs.iter().find(|i| i.0 == reg).expect("input").1 } ``` </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/69/head:github-bot/gh-69'
git switch 'github-bot/gh-69'

Merge

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