victory-shader: lower a16 biased image samples and sub-dword LDS access #996

Closed
bartikele wants to merge 5 commits from upstream-pr/996 into main
bartikele commented 2026-11-24 14:47:20 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by bartikele on 2026-11-24T14:47:20Z.


Adds DXIL lowering in \ ictory-shader\ for:

  • \image_sample_b\ with \16: keeps offset, bias, and comparison reference unpacked (32-bit dwords) while unpacking coordinates from \whole\ onward as 16-bit pairs.
  • Sub-dword LDS access (\ds_read_{u8,i8,u16,i16}, \ds_write_{b8,b16}): extracts/sign-extends bytes and halves on read, and performs race-free \tomicrmw and\ + \tomicrmw or\ on the containing 32-bit LDS dword on write. Includes unit test \lds_byte_and_half_read_write_lower.

Verified with \cargo clippy -p victory-shader --all-targets --locked -- -D warnings, \cargo test -p victory-shader --locked\ (122 passed), \cargo xtask policy, and \ ictory translate --validate.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6584457918 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/996). Originally posted by [bartikele](https://github.com/bartikele) on 2026-11-24T14:47:20Z. --- Adds DXIL lowering in \ ictory-shader\ for: - \image_sample_b\ with \16\: keeps offset, bias, and comparison reference unpacked (32-bit dwords) while unpacking coordinates from \whole\ onward as 16-bit pairs. - Sub-dword LDS access (\ds_read_{u8,i8,u16,i16}\, \ds_write_{b8,b16}\): extracts/sign-extends bytes and halves on read, and performs race-free \tomicrmw and\ + \tomicrmw or\ on the containing 32-bit LDS dword on write. Includes unit test \lds_byte_and_half_read_write_lower\. Verified with \cargo clippy -p victory-shader --all-targets --locked -- -D warnings\, \cargo test -p victory-shader --locked\ (122 passed), \cargo xtask policy\, and \ ictory translate --validate\.
Zipettte commented 2026-11-24 15:04:15 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Zipettte on 2026-11-24T14:54:50Z.


Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at c188c02: cargo test -p victory-shader passes, 122 passed / 0 failed, including lds_byte_and_half_read_write_lower. Workspace baseline on main (ffe310e) was green on the same machine (342 passed).

Tested with AI assistance (Claude).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7135153225 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/996#issuecomment-7135153225). Originally posted by [Zipettte](https://github.com/Zipettte) on 2026-11-24T14:54:50Z. --- Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at c188c02: `cargo test -p victory-shader` passes, 122 passed / 0 failed, including `lds_byte_and_half_read_write_lower`. Workspace baseline on main (ffe310e) was green on the same machine (342 passed). _Tested with AI assistance (Claude)._
catminou4 commented 2026-11-24 16:04:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T15:41:45Z.

Review: COMMENTED


Two features in one PR (a16 biased image samples + LDS sub-dword), both lowering-only tested on macOS (98 passed / 0 failed / 1 ignored after merge with ffe310e). The a16 packing fix looks careful — offset/bias/reference stay 32-bit, packed pairs start at whole, and the biased-sample refusal is lifted with the component math adjusted accordingly.

The LDS sub-dword writes concern me though, and it's the same pattern I flagged on #571: ds_write_b8/b16[_d16_hi] lowers to atomic And(clear) + atomic Or(value) on the containing dword. Two waves of the same workgroup writing the same byte (or one writing while another does a full ds_write_b32) can interleave between the And and the Or and produce an OR-mixture or a clobbered dword — LDS is per-workgroup but waves in a workgroup run concurrently, and nothing here serializes the pair. Hardware ds_write_b8 has byte-granular semantics the emulation doesn't quite reproduce. Before this lands I'd want either (a) a host-execution test on the Windows lane with two waves racing the same dword, or (b) a documented assumption that console shaders never do this (with the reason), or (c) the read-modify-write done in one guarded sequence if the ISA allows.

Also a triage note: the LDS sub-dword lowering overlaps the #632/#116/#101/#571 cluster — whichever lands first should subsume the others to avoid four divergent implementations of the same instructions.

<!-- greyfinch-sync:Marvisak/VIctory:review:6042461994 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/996#pullrequestreview-6042461994). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T15:41:45Z. **Review:** COMMENTED --- Two features in one PR (a16 biased image samples + LDS sub-dword), both lowering-only tested on macOS (98 passed / 0 failed / 1 ignored after merge with `ffe310e`). The a16 packing fix looks careful — offset/bias/reference stay 32-bit, packed pairs start at `whole`, and the biased-sample refusal is lifted with the component math adjusted accordingly. The LDS sub-dword writes concern me though, and it's the same pattern I flagged on #571: `ds_write_b8/b16[_d16_hi]` lowers to `atomic And(clear)` + `atomic Or(value)` on the containing dword. Two waves of the same workgroup writing the same byte (or one writing while another does a full `ds_write_b32`) can interleave between the And and the Or and produce an OR-mixture or a clobbered dword — LDS is per-workgroup but waves in a workgroup run concurrently, and nothing here serializes the pair. Hardware `ds_write_b8` has byte-granular semantics the emulation doesn't quite reproduce. Before this lands I'd want either (a) a host-execution test on the Windows lane with two waves racing the same dword, or (b) a documented assumption that console shaders never do this (with the reason), or (c) the read-modify-write done in one guarded sequence if the ISA allows. Also a triage note: the LDS sub-dword lowering overlaps the #632/#116/#101/#571 cluster — whichever lands first should subsume the others to avoid four divergent implementations of the same instructions.
mercuryVM commented 2026-11-24 17:02:57 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T16:21:31Z.

Review: CHANGES_REQUESTED


Tested 38e9db08 on Windows 11, RTX 5070, MSVC: cargo test -p victory-shader -- --test-threads=1 passes (122 passed, 1 ignored). I read the lowering by hand and found two bugs:

  1. Low-half d16 LDS reads overwrite the high half (crates/victory-shader/src/emit/lower.rs:2201-2212).

    • ds_read_{u8,i8,u16}_d16 writes only bits 15:0 of vdst and keeps bits 31:16, the mirror of the _hi forms.
    • The block merges with the old register only when hi. For d16 && !hi it writes the bare 16-bit result, so the upper half becomes zero.
    • Fix: read the old value, as the hi arm does, and write (old & 0xFFFF0000) | (r & 0xFFFF).
    • The new test only checks that these forms lower, so it does not catch this.
  2. The a16 bias is decoded as an f32 (lower.rs:6045-6046, with the new comment at :5712).

    • Under A16 the bias does sit in its own dword, but that dword holds an f16 in its low half and nothing in the upper half. LLVM's AMDGPU legalizer documents this: "Bias is of type half but occupies full 32-bit".
    • self.as_float(bias) reinterprets the whole dword as an f32, which gives a near-zero denormal or junk instead of the bias.
    • Fix: when *a16, mask the dword to its low 16 bits and pass it through legacyF16ToF32, the same as to_float does for coordinates.
    • The offset and the comparison reference remain true 32-bit dwords, as the comment says.

Smaller points:

  • A ds_read_u16/ds_write_b16 at a byte address with addr & 3 == 3 straddles two dwords. Here it reads or writes only the low byte of the field. RDNA requires 2-byte alignment for these, so this is probably fine, but a short note would help.
  • whole uses starts_with("image_sample_b"), while first uses name == "image_sample_b". Only the exact name reaches op 61 today, so this is harmless, but the two should agree.
  • v_cvt_f32_f64_lowers_with_double_flag looks unrelated to this PR's title.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6043142303 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/996#pullrequestreview-6043142303). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T16:21:31Z. **Review:** CHANGES_REQUESTED --- Tested 38e9db08 on Windows 11, RTX 5070, MSVC: `cargo test -p victory-shader -- --test-threads=1` passes (122 passed, 1 ignored). I read the lowering by hand and found two bugs: 1. **Low-half d16 LDS reads overwrite the high half** (`crates/victory-shader/src/emit/lower.rs:2201-2212`). - `ds_read_{u8,i8,u16}_d16` writes only bits 15:0 of vdst and keeps bits 31:16, the mirror of the `_hi` forms. - The block merges with the old register only when `hi`. For `d16 && !hi` it writes the bare 16-bit result, so the upper half becomes zero. - Fix: read the old value, as the `hi` arm does, and write `(old & 0xFFFF0000) | (r & 0xFFFF)`. - The new test only checks that these forms lower, so it does not catch this. 2. **The a16 bias is decoded as an f32** (`lower.rs:6045-6046`, with the new comment at `:5712`). - Under A16 the bias does sit in its own dword, but that dword holds an f16 in its low half and nothing in the upper half. LLVM's AMDGPU legalizer documents this: "Bias is of type half but occupies full 32-bit". - `self.as_float(bias)` reinterprets the whole dword as an f32, which gives a near-zero denormal or junk instead of the bias. - Fix: when `*a16`, mask the dword to its low 16 bits and pass it through `legacyF16ToF32`, the same as `to_float` does for coordinates. - The offset and the comparison reference remain true 32-bit dwords, as the comment says. Smaller points: - A `ds_read_u16`/`ds_write_b16` at a byte address with `addr & 3 == 3` straddles two dwords. Here it reads or writes only the low byte of the field. RDNA requires 2-byte alignment for these, so this is probably fine, but a short note would help. - `whole` uses `starts_with("image_sample_b")`, while `first` uses `name == "image_sample_b"`. Only the exact name reaches op 61 today, so this is harmless, but the two should agree. - `v_cvt_f32_f64_lowers_with_double_flag` looks unrelated to this PR's title. AI-assisted (Claude Code).
bartikele commented 2026-11-24 18:05:39 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by bartikele on 2026-11-24T17:18:41Z.


Thanks @mercuryVM and @catminou4 for the thorough review! Updated shader-a16-bias-and-lds-subdword with the following fixes:

  1. Low-half ds_read_{u8,i8,u16}_d16: now preserves bits 31:16 of vdst via self.low_half(inst, reg, r), matching the _hi path.
  2. a16 image_sample_b bias: masks the bias VGPR to its low 16 bits (& 0xFFFF) and decodes it via legacyF16ToF32 (to_float) when *a16 is set, and indexes bias at usize::from(offset_word).
  3. Consistency: first now uses name.starts_with("image_sample_b") to match whole.

Regarding @catminou4's note on ds_write_b8/b16: DXIL groupshared uint[] has no byte-granular store intrinsic, so atomicAnd + atomicOr is the closest lowering available today.

Re-verified with cargo clippy -p victory-shader --all-targets --locked -- -D warnings, cargo test -p victory-shader --locked, and cargo xtask policy.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7139002779 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/996#issuecomment-7139002779). Originally posted by [bartikele](https://github.com/bartikele) on 2026-11-24T17:18:41Z. --- Thanks @mercuryVM and @catminou4 for the thorough review! Updated `shader-a16-bias-and-lds-subdword` with the following fixes: 1. **Low-half `ds_read_{u8,i8,u16}_d16`**: now preserves bits `31:16` of `vdst` via `self.low_half(inst, reg, r)`, matching the `_hi` path. 2. **`a16` `image_sample_b` bias**: masks the bias VGPR to its low 16 bits (`& 0xFFFF`) and decodes it via `legacyF16ToF32` (`to_float`) when `*a16` is set, and indexes `bias` at `usize::from(offset_word)`. 3. **Consistency**: `first` now uses `name.starts_with("image_sample_b")` to match `whole`. Regarding `@catminou4`'s note on `ds_write_b8/b16`: DXIL `groupshared uint[]` has no byte-granular store intrinsic, so `atomicAnd` + `atomicOr` is the closest lowering available today. Re-verified with `cargo clippy -p victory-shader --all-targets --locked -- -D warnings`, `cargo test -p victory-shader --locked`, and `cargo xtask policy`.
bartikele commented 2026-11-24 21:03:36 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by bartikele on 2026-11-24T20:40:38Z.


Updated \ds_write_{b8,b16}{,_d16_hi}\ in \

<!-- greyfinch-sync:Marvisak/VIctory:comment:7143907489 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/996#issuecomment-7143907489). Originally posted by [bartikele](https://github.com/bartikele) on 2026-11-24T20:40:38Z. --- Updated \ds_write_{b8,b16}{,_d16_hi}\ in \
upstream-sync closed this pull request 2026-11-25 01:05:11 +00:00
Marvisak commented 2026-11-25 01:07:55 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:29:51Z.


Automated triage for the maintainers. Closing because this change is already in main, or main has since replaced the code it patches.

Both halves are in main: a16 biased samples (image()) and sub-dword LDS accesses (ds()).

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148569882 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/996#issuecomment-7148569882). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:29:51Z. --- Automated triage for the maintainers. Closing because this change is already in `main`, or `main` has since replaced the code it patches. Both halves are in main: a16 biased samples (image()) and sub-dword LDS accesses (ds()). If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.
bartikele commented 2026-11-25 14:07:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by bartikele on 2026-11-25T13:21:44Z.


Rebased onto current main (940d32f), keeping main's image_bvh and 16 sample bias/offset handling while preserving the sub-dword LDS read/write lowering (ds_write_{b8,b16}{,d16_hi} via atomic cmpxchg loop and ds_read{u8,i8,u16,i16}{,_d16,_d16_hi}).

Verified on 940d32f:

  • cargo clippy -p victory-shader --all-targets --locked -- -D warnings: clean
  • cargo test -p victory-shader --locked lds_byte_and_half_read_write_lower: passed
  • cargo test -p victory-shader --locked lds_bytes_and_halves_match_the_hardware: passed
  • cargo xtask policy: 182 files ok
<!-- greyfinch-sync:Marvisak/VIctory:comment:7163665849 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/996#issuecomment-7163665849). Originally posted by [bartikele](https://github.com/bartikele) on 2026-11-25T13:21:44Z. --- Rebased onto current main (940d32f), keeping main's image_bvh and 16 sample bias/offset handling while preserving the sub-dword LDS read/write lowering (ds_write_{b8,b16}{,_d16_hi} via atomic cmpxchg loop and ds_read_{u8,i8,u16,i16}{,_d16,_d16_hi}). Verified on 940d32f: - cargo clippy -p victory-shader --all-targets --locked -- -D warnings: clean - cargo test -p victory-shader --locked lds_byte_and_half_read_write_lower: passed - cargo test -p victory-shader --locked lds_bytes_and_halves_match_the_hardware: passed - cargo xtask policy: 182 files ok

Pull request closed

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
Marvisak/VIctory!996
No description provided.