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

Merged
eleboucher merged 1 commit from upstream-pr/3894 into main 2026-11-30 14:12:01 +00:00
eleboucher commented 2026-11-30 10:47:18 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by eleboucher on 2026-11-30T10:47:18Z.


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.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5563727892 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3894). Originally posted by [eleboucher](https://github.com/eleboucher) on 2026-11-30T10:47:18Z. --- 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.
akimfomin commented 2026-11-30 11:07:18 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by akimfomin on 2026-11-30T11:07:18Z.


@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.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7276567973 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3894#issuecomment-7276567973). Originally posted by [akimfomin](https://github.com/akimfomin) on 2026-11-30T11:07:18Z. --- @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.
eleboucher commented 2026-11-30 11:09:19 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by eleboucher on 2026-11-30T11:09:19Z.


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.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7276615820 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3894#issuecomment-7276615820). Originally posted by [eleboucher](https://github.com/eleboucher) on 2026-11-30T11:09:19Z. --- 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.
akimfomin commented 2026-11-30 11:15:33 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by akimfomin on 2026-11-30T11:15:33Z.


@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.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7276773227 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3894#issuecomment-7276773227). Originally posted by [akimfomin](https://github.com/akimfomin) on 2026-11-30T11:15:33Z. --- @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.
eleboucher commented 2026-11-30 11:23:14 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by eleboucher on 2026-11-30T11:23:14Z.


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.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7276953440 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3894#issuecomment-7276953440). Originally posted by [eleboucher](https://github.com/eleboucher) on 2026-11-30T11:23:14Z. --- 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.
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
trustno1-coder/VIctory-NVIDIA!3894
No description provided.