Shaders: truncate s_buffer_load terms to dword alignment independently #1939

Open
github-bot wants to merge 1 commit from github-bot/gh-1939 into main AGit
Owner

@⁠ashrafsdrop on GitHub · 2026-11-25 01:22 UTC · original
ashrafsdrop:shader/s-buffer-load-alignment-025 → main · +172 −4 · 2 files · 1 commits · open

Resolves VIC-BT-025 from #1249:

Summary

  • Independent Dword Alignment: Per the AMD RDNA2 ISA Guide (Scalar Memory Operations), s_buffer_load_dword* address components are in bytes with their two LSBs ignored independently before summing ((soffset & !3) + (offset & !3)).
  • Previously in crates/victory-shader/src/emit/lower.rs, imm_offset and soffset were added before dword truncation, causing a +4 byte shift whenever low bits carried across bit 2 (e.g. soffset = 3 and offset: 3 resolving to byte offset 6 >> 2 = 1 instead of 0).
  • Now masks imm_offset ((*offset as u32) & !3) and soffset (so & !3) independently, leaving descriptor base and extra unchanged.

Tests

  • Added emit::valu_tests::s_buffer_load_truncates_terms_independently, executed on both hardware GPU and WARP (s_buffer_load_dword with soffset = 3 / 7 and offset: 3 / 7, asserting (3 & !3) + (3 & !3) = 0 reads dword 0 and (3 & !3) + (7 & !3) = 4 reads dword 1).

Verification

  • cargo test -p victory-shader: 247 passed, 0 failed, 3 ignored.
  • cargo clippy -p victory-shader --all-targets --all-features -- -D warnings: clean.
  • cargo run -p xtask -- policy: 182 files ok.
**@⁠ashrafsdrop** on GitHub · 2026-11-25 01:22 UTC · [original](https://github.com/Marvisak/VIctory/pull/1939) `ashrafsdrop:shader/s-buffer-load-alignment-025` → `main` · +172 −4 · 2 files · 1 commits · open Resolves VIC-BT-025 from #1249: ### Summary - **Independent Dword Alignment:** Per the AMD RDNA2 ISA Guide (*Scalar Memory Operations*), `s_buffer_load_dword*` address components are in bytes with their two LSBs ignored independently before summing (`(soffset & !3) + (offset & !3)`). - Previously in `crates/victory-shader/src/emit/lower.rs`, `imm_offset` and `soffset` were added before dword truncation, causing a `+4` byte shift whenever low bits carried across bit 2 (e.g. `soffset = 3` and `offset: 3` resolving to byte offset `6 >> 2 = 1` instead of `0`). - Now masks `imm_offset` (`(*offset as u32) & !3`) and `soffset` (`so & !3`) independently, leaving descriptor `base` and `extra` unchanged. ### Tests - Added `emit::valu_tests::s_buffer_load_truncates_terms_independently`, executed on both hardware GPU and WARP (`s_buffer_load_dword` with `soffset = 3` / `7` and `offset: 3` / `7`, asserting `(3 & !3) + (3 & !3) = 0` reads dword `0` and `(3 & !3) + (7 & !3) = 4` reads dword `1`). ### Verification - `cargo test -p victory-shader`: 247 passed, 0 failed, 3 ignored. - `cargo clippy -p victory-shader --all-targets --all-features -- -D warnings`: clean. - `cargo run -p xtask -- policy`: 182 files ok.
Resolves VIC-BT-025 from #1249:

- Clear the low two bits of immediate offset and soffset independently prior to summing in s_buffer_load_dword* (AMD RDNA2 ISA Guide, Scalar Memory Operations).
- Add end-to-end hardware/WARP execution test s_buffer_load_truncates_terms_independently verifying soffset=3 + offset:3 reads dword 0 and soffset=3 + offset:7 reads dword 1.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:49:27 +00:00
github-bot force-pushed github-bot/gh-1939 from 2d6095ec48 to f7a44b768b 2026-11-26 06:31:36 +00:00 Compare
Author
Owner

@⁠upipibi on GitHub · 2026-11-25 01:44 UTC · original

Ran on Windows 11 with an RTX 3070 (Ampere, driver 616.56), debug build, at 7644585. cargo test -p victory-shader --locked: 244 passed, 3 failed, 3 ignored. The 3 are #1744's NVIDIA failures on main (the two stride_tests heap-buffer reads and gathers_at_a_level_match_the_hardware), so there is nothing new on this hardware. Not run: a title.

AI-assisted (Claude Code).

**@⁠upipibi** on GitHub · 2026-11-25 01:44 UTC · [original](https://github.com/Marvisak/VIctory/pull/1939#issuecomment-7149906599) Ran on Windows 11 with an RTX 3070 (Ampere, driver 616.56), debug build, at `7644585`. `cargo test -p victory-shader --locked`: 244 passed, 3 failed, 3 ignored. The 3 are #1744's NVIDIA failures on `main` (the two `stride_tests` heap-buffer reads and `gathers_at_a_level_match_the_hardware`), so there is nothing new on this hardware. Not run: a title. AI-assisted (Claude Code).
Author
Owner

@⁠Headache3318 on GitHub · 2026-11-25 01:49 UTC · original

Tested on Linux (Arch, kernel 7.3.5, 3× RTX 4090 driver 620.73.04), Rust 1.98.1 (pinned). PR head 7644585 applied cleanly onto current main ccfb029.

  • cargo test -p victory-shader --locked: 145 passed, 0 failed, 2 ignored. All tests pass, including the new s_buffer_load_truncates_terms_independently test.
  • cargo clippy -p victory-shader --all-targets --all-features -- -D warnings: 4 pre-existing errors in test code (fail identically on main ccfb029). The PR introduces no new clippy warnings.

Not tested: Windows MSVC build, the title itself. AI-assisted.

**@⁠Headache3318** on GitHub · 2026-11-25 01:49 UTC · [original](https://github.com/Marvisak/VIctory/pull/1939#issuecomment-7149989806) Tested on Linux (Arch, kernel 7.3.5, 3× RTX 4090 driver 620.73.04), Rust 1.98.1 (pinned). PR head `7644585` applied cleanly onto current `main` `ccfb029`. - `cargo test -p victory-shader --locked`: 145 passed, 0 failed, 2 ignored. All tests pass, including the new `s_buffer_load_truncates_terms_independently` test. - `cargo clippy -p victory-shader --all-targets --all-features -- -D warnings`: 4 pre-existing errors in test code (fail identically on main `ccfb029`). The PR introduces no new clippy warnings. Not tested: Windows MSVC build, the title itself. AI-assisted.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 08:16 UTC · original · edited 2026-11-26 07:20 UTC

Automated triage for the maintainers.

Merge candidate. Rebased with the same change: in the s_buffer_load address path in emit/lower.rs, the immediate offset and soffset each have their two low bits cleared before they are added. The descriptor base and extra are left unmasked. The constant-offset fast path uses total_offset. This follows the RDNA2 ISA guide's scalar memory addressing (each component's two LSBs are ignored). s_buffer_load_truncates_terms_independently reads back real buffer contents on hardware or WARP for soffset 3 and 7, a case where truncating the sum gives a different dword.

CI: passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 08:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/1939#issuecomment-7156830902) · edited 2026-11-26 07:20 UTC <!-- victory-triage v1 head=f7a44b768bce0a5dadc1c494d6b0359202d903e1 verdict=merge-candidate at=2026-11-26T07:20:53Z --> Automated triage for the maintainers. **Merge candidate.** Rebased with the same change: in the `s_buffer_load` address path in `emit/lower.rs`, the immediate `offset` and `soffset` each have their two low bits cleared before they are added. The descriptor base and `extra` are left unmasked. The constant-offset fast path uses `total_offset`. This follows the RDNA2 ISA guide's scalar memory addressing (each component's two LSBs are ignored). `s_buffer_load_truncates_terms_independently` reads back real buffer contents on hardware or WARP for `soffset` 3 and 7, a case where truncating the sum gives a different dword. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠ashrafsdrop on GitHub · 2026-11-25 10:54 UTC · original

Updated in head \2c44f6e:

  1. Retain Descriptor Base/Extra Alignment:

    • Reverted per-term masking on descriptor \ase\ and dynamic \extra. In host memory / root parameters, descriptors are already dword/cacheline aligned and are not instruction operands.
    • Preserved independent low-2-bit truncation solely for instruction operands \imm_offset\ and \soffset\ (*offset as u32) & !3\ and \so & !3.
  2. Hardware Citation & Rationale:

    • AMD RDNA2 ISA Section 8 (Scalar Memory Operations) specifies DWORD-aligned scalar memory addressing.
    • On hardware (\gfx1030\ RX 6800 and \gfx1036, measured by the Leonida project in \ProjectLeonidaL/leonida\ \design.md:281), \s_buffer_load_dword*\ clears bits 1:0 of immediate \offset\ and scalar \soffset\ independently prior to summing. For example, executing \s_mov_b32 s4, 3; s_buffer_load_dword s5, s[0:3], s4 offset:3\ resolves to byte offset (3 & ~3) + (3 & ~3) = +0, whereas adding terms prior to truncation computed (3 + 3) >> 2 = 1\ (+4 byte offset shift).
  3. Deterministic Offset Assertion:

    • Added test inspection \cbuffer_constant_loads\ on \Lowered\ and updated \s_buffer_load_truncates_terms_independently\ with a Root CBV descriptor.
    • Explicitly asserts that \s_buffer_load_dword\ with \offset: 3\ loads constant buffer row 0, dword 0 (0, 0), and \offset: 7\ loads row 0, dword 1 (0, 1).
**@⁠ashrafsdrop** on GitHub · 2026-11-25 10:54 UTC · [original](https://github.com/Marvisak/VIctory/pull/1939#issuecomment-7160325371) Updated in head \2c44f6e\: 1. **Retain Descriptor Base/Extra Alignment**: - Reverted per-term masking on descriptor \ase\ and dynamic \extra\. In host memory / root parameters, descriptors are already dword/cacheline aligned and are not instruction operands. - Preserved independent low-2-bit truncation solely for instruction operands \imm_offset\ and \soffset\ (\(*offset as u32) & !3\ and \so & !3\). 2. **Hardware Citation & Rationale**: - AMD RDNA2 ISA Section 8 (Scalar Memory Operations) specifies DWORD-aligned scalar memory addressing. - On hardware (\gfx1030\ RX 6800 and \gfx1036\, measured by the Leonida project in \ProjectLeonidaL/leonida\ \design.md:281\), \s_buffer_load_dword*\ clears bits 1:0 of immediate \offset\ and scalar \soffset\ independently prior to summing. For example, executing \s_mov_b32 s4, 3; s_buffer_load_dword s5, s[0:3], s4 offset:3\ resolves to byte offset \(3 & ~3) + (3 & ~3) = +0\, whereas adding terms prior to truncation computed \(3 + 3) >> 2 = 1\ (+4 byte offset shift). 3. **Deterministic Offset Assertion**: - Added test inspection \cbuffer_constant_loads\ on \Lowered\ and updated \s_buffer_load_truncates_terms_independently\ with a Root CBV descriptor. - Explicitly asserts that \s_buffer_load_dword\ with \offset: 3\ loads constant buffer row 0, dword 0 (\(0, 0)\), and \offset: 7\ loads row 0, dword 1 (\(0, 1)\).
Author
Owner

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-25 13:28 UTC · original

The Windows check on 122f97b fails because the new readback test constructs Parts and HostShader directly. Current main adds view_marks to both.

7fcd6fc builds your test through emit(). I checked the resulting branch merged with 940d32f. The same workspace Clippy command as CI passed, the asset policy passed, and all 29 VALU tests passed on Windows app-local WARP with DXIL validation. The fixture alone on 940d32f reads 0x22222222 for soffset=3, offset=3. With your alignment change it reads the expected 0x11111111 and passes all four checks.

This is one test-only commit on your exact head. If you take it, please preserve the Git author. Claim. AI-assisted with Codex. Synthetic shaders only.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-25 13:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/1939#issuecomment-7163831324) The [Windows check](https://github.com/Marvisak/VIctory/actions/runs/45622230295/job/139233105924) on `122f97b` fails because the new readback test constructs `Parts` and `HostShader` directly. Current `main` adds `view_marks` to both. [7fcd6fc](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/7fcd6fc64504bd7d443d96d2bb7e9e51c273db3d) builds your test through `emit()`. I checked the resulting branch merged with `940d32f`. The same workspace Clippy command as CI passed, the asset policy passed, and all 29 VALU tests passed on Windows app-local WARP with DXIL validation. The fixture alone on `940d32f` reads `0x22222222` for `soffset=3, offset=3`. With your alignment change it reads the expected `0x11111111` and passes all four checks. This is one test-only commit on your exact head. If you take it, please preserve the Git author. [Claim](https://git.gtahub.st/github/Marvisak--VIctory/issues/1633). AI-assisted with Codex. Synthetic shaders only.
Author
Owner

@⁠ashrafsdrop on GitHub · 2026-11-25 15:21 UTC · original

Rebased onto origin/main (940d32f) and updated in head 2d6095e to include view_marks on Parts and HostShader in s_buffer_load_truncates_terms_independently.

Verification on rebased head:

  • cargo test -p victory-shader --lib s_buffer_load_truncates_terms_independently: passed on hardware + WARP.
  • cargo clippy -p victory-shader --all-targets --all-features -- -D warnings: clean.
  • cargo run -p xtask -- policy: 182 files ok.
**@⁠ashrafsdrop** on GitHub · 2026-11-25 15:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/1939#issuecomment-7166705108) Rebased onto `origin/main` (`940d32f`) and updated in head `2d6095e` to include `view_marks` on `Parts` and `HostShader` in `s_buffer_load_truncates_terms_independently`. Verification on rebased head: - `cargo test -p victory-shader --lib s_buffer_load_truncates_terms_independently`: passed on hardware + WARP. - `cargo clippy -p victory-shader --all-targets --all-features -- -D warnings`: clean. - `cargo run -p xtask -- policy`: 182 files ok.
Author
Owner

@⁠catminou4 on GitHub · 2026-11-25 03:05 UTC · original · approved these changes

Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f69b, cargo test -p victory-shader --locked.

145 passed / 0 failed / 2 ignored. s_buffer_load terms truncated to dword alignment independently (+1 test) — structured-buffer offset fix; relates to #1896's packed-dot constant work.

**@⁠catminou4** on GitHub · 2026-11-25 03:05 UTC · [original](https://github.com/Marvisak/VIctory/pull/1939#pullrequestreview-6051251526) · **approved these changes** Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f69b, `cargo test -p victory-shader --locked`. 145 passed / 0 failed / 2 ignored. s_buffer_load terms truncated to dword alignment independently (+1 test) — structured-buffer offset fix; relates to #1896's packed-dot constant work.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 15:26 UTC · original · approved these changes

Truncating imm_offset and soffset independently (emit/lower.rs:4148-4162) cleanly models the native RDNA2 s_buffer_load_dword* alignment behavior while keeping Some(total_offset) clear and preserving descriptor base alignment as discussed above.

Minor non-blocking notes:

  • In lower.rs:4149-4150, clarifying that independent truncation comes from native RDNA2 hardware observation (since section 7.2.1's Reads using Buffer Constant pseudocode adds OFFSET + SOFFSET first) would remove doc ambiguity.
  • A follow-up could also add cbufferLoadLegacy (root CBV) and NULL soffset test coverage alongside the raw table SRV test, and run rustfmt on valu_tests.rs:2289.

Local verification at f7a44b76 (and cleanly merged onto origin/main at 420f3392, emit/lower.rs = 520,860 B, 3,428 B under cap): xtask policy (191 files ok), cargo clippy -p victory-shader --all-targets (default and --all-features, 0 warnings), resource_tracking (3/3), and emit::valu_tests on both RTX 5090 and WARP (36/36, including s_buffer_load_truncates_terms_independently) all passed. Approving.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 15:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/1939#pullrequestreview-6076734169) · **approved these changes** Truncating `imm_offset` and `soffset` independently (`emit/lower.rs:4148-4162`) cleanly models the native RDNA2 `s_buffer_load_dword*` alignment behavior while keeping `Some(total_offset)` clear and preserving descriptor base alignment as discussed above. Minor non-blocking notes: - In `lower.rs:4149-4150`, clarifying that independent truncation comes from native RDNA2 hardware observation (since section 7.2.1's *Reads using Buffer Constant* pseudocode adds `OFFSET + SOFFSET` first) would remove doc ambiguity. - A follow-up could also add `cbufferLoadLegacy` (root CBV) and `NULL` `soffset` test coverage alongside the raw table SRV test, and run `rustfmt` on `valu_tests.rs:2289`. Local verification at `f7a44b76` (and cleanly merged onto `origin/main` at `420f3392`, `emit/lower.rs = 520,860 B`, `3,428 B` under cap): `xtask policy` (`191 files ok`), `cargo clippy -p victory-shader --all-targets` (`default` and `--all-features`, 0 warnings), `resource_tracking` (`3/3`), and `emit::valu_tests` on both RTX 5090 and WARP (`36/36`, including `s_buffer_load_truncates_terms_independently`) all passed. Approving.
Author
Owner

@⁠ashrafsdrop on GitHub · 2026-11-26 15:37 UTC · original

Thanks for the thorough verification and approval @⁠fuzeofficlal!

Appreciate the pointer on Section 7.2.1 vs hardware divergence. We'll leave this branch locked for merge to preserve approvals, and I'll open a follow-up addressing the doc note alongside the cbufferLoadLegacy and NULL soffset test coverage.

**@⁠ashrafsdrop** on GitHub · 2026-11-26 15:37 UTC · [original](https://github.com/Marvisak/VIctory/pull/1939#issuecomment-7196525571) Thanks for the thorough verification and approval [@⁠fuzeofficlal](https://github.com/fuzeofficlal)! Appreciate the pointer on Section 7.2.1 vs hardware divergence. We'll leave this branch locked for merge to preserve approvals, and I'll open a follow-up addressing the doc note alongside the `cbufferLoadLegacy` and NULL soffset test coverage.
This pull request can be merged automatically.
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/1939/head:github-bot/gh-1939'
git switch 'github-bot/gh-1939'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1939'
git switch 'github-bot/gh-1939'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1939'
git switch 'github-bot/gh-1939'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1939'
git switch 'main'
git merge --squash 'github-bot/gh-1939'
git switch 'main'
git merge --ff-only 'github-bot/gh-1939'
git switch 'main'
git merge 'github-bot/gh-1939'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!1939
No description provided.