Shaders: scalar and dword buffer addresses ignore their two low bits, as the hardware does (misaligned access under Aftermath) #3894

Manually merged
github merged 1 commit from github-bot/gh-3894 into gh-pr/3894/base 2026-11-30 14:14:38 +00:00 AGit
Owner

@⁠eleboucher on GitHub · 2026-11-30 10:47 UTC · original
eleboucher:smem-align → main · +83 −1 · 4 files · 1 commits · merged 2026-11-30 14:12 UTC

Scalar memory reads (S_LOAD_DWORD*, S_BUFFER_LOAD_DWORD*) ignore the two low address bits on the console. Our DXIL passed them through to rawBufferLoad (declared 4-byte aligned), so an offset the title computes or loads that is not a multiple of 4 became a misaligned access on the host.

AMD RDNA2 ISA reference:

  • Section 7 (scalar memory): "The two LSBs of V#.base and of the final address are ignored to force Dword alignment." Section 7.5 adds that OFFSET itself has no alignment restriction.
  • Section 8.1.7: "For Dword or larger reads or writes of non-formatted ops (such as BUFFER_LOAD_DWORD), the two LSBs of the byte-address are ignored, thus forcing Dword alignment." (in the default SH_MEM_CONFIG.alignment_mode DWORD)

What changes (crates/victory-shader/src/emit/lower.rs):

  • An align4 helper (& !3, folded away for constants already aligned).
  • The final SMEM address is masked: s_load by address (memory_smem) and s_buffer_load (after offset + soffset). The page number comes from the high bits, so it is unchanged.
  • Non-formatted dword/x2/x3/x4 MUBUF loads and stores mask their final byte address. For a stride-1 raw buffer the index and offset are summed first, since the hardware masks the whole sum. Typed, formatted, byte, short and atomic paths are untouched. This part sits behind VICTORY_VMEM_ALIGN4 (default on, =0 restores the old lowering), in the shader cache key, in case a title runs with the UNALIGNED mode.
  • A test, scalar_load_clears_the_two_low_address_bits, covers an s_load_dword at offset 4k+2.

How it showed up: with NVIDIA Aftermath's shader error reporting on, main (41adb551) lost the device in every run, always "Misaligned Address Error" in the same compute program (title 89d08e561897d4e5, dxil:8519). There, byte offsets loaded by s_buffer_load are reused as SGPR offsets of later s_buffer_loads. Without error reporting, NVIDIA silently ignores the misalignment; per D3D12 it is undefined.

Tested on an RTX 4070 Ti SUPER (driver 621.24), native 1440p with ray tracing, under Aftermath shader error reporting:

  • Before: 8 of 8 runs faulted at that instruction.
  • With this change: the misaligned fault is gone. Error reporting now stops at the next, unrelated report (a "Shader Instruction Error" in a pixel shader).
  • cargo test -p victory-shader: 296 passed. cargo xwin check/clippy with and without victory-gpu/diagnostics: no new warnings in the touched files.

What it does not do: it is not the cause of the device losses main has with ray tracing on and error reporting off. Those still happen here with this change (2 of 2 runs, lost at 120-240 s). It makes the translation exact and removes undefined behaviour, so that the remaining fault reports point at real problems.

**@⁠eleboucher** on GitHub · 2026-11-30 10:47 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3894) `eleboucher:smem-align` → `main` · +83 −1 · 4 files · 1 commits · merged 2026-11-30 14:12 UTC Scalar memory reads (`S_LOAD_DWORD*`, `S_BUFFER_LOAD_DWORD*`) ignore the two low address bits on the console. Our DXIL passed them through to `rawBufferLoad` (declared 4-byte aligned), so an offset the title computes or loads that is not a multiple of 4 became a misaligned access on the host. AMD RDNA2 ISA reference: - Section 7 (scalar memory): "The two LSBs of V#.base and of the final address are ignored to force Dword alignment." Section 7.5 adds that OFFSET itself has no alignment restriction. - Section 8.1.7: "For Dword or larger reads or writes of non-formatted ops (such as BUFFER_LOAD_DWORD), the two LSBs of the byte-address are ignored, thus forcing Dword alignment." (in the default `SH_MEM_CONFIG.alignment_mode` DWORD) What changes (`crates/victory-shader/src/emit/lower.rs`): - An `align4` helper (`& !3`, folded away for constants already aligned). - The final SMEM address is masked: `s_load` by address (`memory_smem`) and `s_buffer_load` (after offset + soffset). The page number comes from the high bits, so it is unchanged. - Non-formatted dword/x2/x3/x4 MUBUF loads and stores mask their final byte address. For a stride-1 raw buffer the index and offset are summed first, since the hardware masks the whole sum. Typed, formatted, byte, short and atomic paths are untouched. This part sits behind `VICTORY_VMEM_ALIGN4` (default on, `=0` restores the old lowering), in the shader cache key, in case a title runs with the UNALIGNED mode. - A test, `scalar_load_clears_the_two_low_address_bits`, covers an `s_load_dword` at offset 4k+2. How it showed up: with NVIDIA Aftermath's shader error reporting on, `main` (41adb551) lost the device in every run, always "Misaligned Address Error" in the same compute program (title `89d08e561897d4e5`, `dxil:8519`). There, byte offsets loaded by `s_buffer_load` are reused as SGPR offsets of later `s_buffer_load`s. Without error reporting, NVIDIA silently ignores the misalignment; per D3D12 it is undefined. Tested on an RTX 4070 Ti SUPER (driver 621.24), native 1440p with ray tracing, under Aftermath shader error reporting: - Before: 8 of 8 runs faulted at that instruction. - With this change: the misaligned fault is gone. Error reporting now stops at the next, unrelated report (a "Shader Instruction Error" in a pixel shader). - `cargo test -p victory-shader`: 296 passed. `cargo xwin check`/`clippy` with and without `victory-gpu/diagnostics`: no new warnings in the touched files. What it does not do: it is not the cause of the device losses `main` has with ray tracing on and error reporting off. Those still happen here with this change (2 of 2 runs, lost at 120-240 s). It makes the translation exact and removes undefined behaviour, so that the remaining fault reports point at real problems.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-30 10:51:52 +00:00
Author
Owner

@⁠akimfomin on GitHub · 2026-11-30 11:07 UTC · original

@⁠eleboucher Independent source-only check on the exact 844200e9 lowering carried to the pinned Lab branch. I added a generated s_buffer_load_dword case with SGPR offsets 2, 3, 4, 7 and 11 plus immediate 1. The requested dwords at (SGPR + 1) >> 2 all matched on Windows WARP in both the normal CBV and raw-twin paths. The 3 + 1 and 7 + 1 cases would select a different dword if the components were aligned before adding. Your scalar test and all 11 Lab shader-cache tests also passed. This isolates the scalar semantics; the VMEM mode and the separate device-loss path still have their own validation. I can share the generated Rust fixture upstream.

**@⁠akimfomin** on GitHub · 2026-11-30 11:07 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3894#issuecomment-7276567973) @⁠eleboucher Independent source-only check on the exact `844200e9` lowering carried to the pinned Lab branch. I added a generated `s_buffer_load_dword` case with SGPR offsets 2, 3, 4, 7 and 11 plus immediate 1. The requested dwords at `(SGPR + 1) >> 2` all matched on Windows WARP in both the normal CBV and raw-twin paths. The `3 + 1` and `7 + 1` cases would select a different dword if the components were aligned before adding. Your scalar test and all 11 Lab shader-cache tests also passed. This isolates the scalar semantics; the VMEM mode and the separate device-loss path still have their own validation. I can share the generated Rust fixture upstream.
Author
Owner

@⁠eleboucher on GitHub · 2026-11-30 11:09 UTC · original

Thanks for the independent check, and especially the 3 + 1 / 7 + 1 carry cases: that is exactly where masking before the add would differ. Yes please, a generated fixture upstream would be welcome; if it fits as a test in crates/victory-shader/src/emit/lower/tests.rs next to scalar_load_clears_the_two_low_address_bits, a suggestion or a small follow-up PR both work for me.

**@⁠eleboucher** on GitHub · 2026-11-30 11:09 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3894#issuecomment-7276615820) Thanks for the independent check, and especially the `3 + 1` / `7 + 1` carry cases: that is exactly where masking before the add would differ. Yes please, a generated fixture upstream would be welcome; if it fits as a test in `crates/victory-shader/src/emit/lower/tests.rs` next to `scalar_load_clears_the_two_low_address_bits`, a suggestion or a small follow-up PR both work for me.
Author
Owner

@⁠akimfomin on GitHub · 2026-11-30 11:15 UTC · original

@⁠eleboucher Here is the small generated address-carry fixture and recipe for reuse. The patch applies cleanly to the exact #3894 head. The ten dispatches ran on the Lab backport with this fix. The fixture covers both the CBV and raw-twin routes and needs no title files.

**@⁠akimfomin** on GitHub · 2026-11-30 11:15 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3894#issuecomment-7276773227) @⁠eleboucher Here is the small [generated address-carry fixture and recipe](https://github.com/akimfomin/VIctory-NVIDIA-Lab/pull/22) for reuse. The patch applies cleanly to the exact #3894 head. The ten dispatches ran on the Lab backport with this fix. The fixture covers both the CBV and raw-twin routes and needs no title files.
Author
Owner

@⁠eleboucher on GitHub · 2026-11-30 11:23 UTC · original

Thanks, the fixture looks like the right regression test for this. Since it is your work, it would be cleanest as your own follow-up PR on top of #3894 (or straight onto main once this lands), so the authorship stays yours. I'm happy to review and run it on my 4070 Ti SUPER. The red check here is unwind_copy is never used in victory-loader, which is on main already (this diff touches only victory-shader and shader_cache.rs); #2048 clears it.

**@⁠eleboucher** on GitHub · 2026-11-30 11:23 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3894#issuecomment-7276953440) Thanks, the fixture looks like the right regression test for this. Since it is your work, it would be cleanest as your own follow-up PR on top of #3894 (or straight onto main once this lands), so the authorship stays yours. I'm happy to review and run it on my 4070 Ti SUPER. The red `check` here is `unwind_copy is never used` in `victory-loader`, which is on main already (this diff touches only `victory-shader` and `shader_cache.rs`); #2048 clears it.
github-bot changed target branch from main to gh-pr/3894/base 2026-11-30 14:14:29 +00:00
github manually merged commit 844200e93b into gh-pr/3894/base 2026-11-30 14:14:31 +00:00
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/trustno1-coder--VIctory-NVIDIA!3894
No description provided.