Skip to content

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

Closed
bartikele wants to merge 5 commits into
Marvisak:mainfrom
bartikele:shader-a16-bias-and-lds-subdword
Closed

bartikele wants to merge 5 commits into
Marvisak:mainfrom
bartikele:shader-a16-bias-and-lds-subdword

Conversation

@bartikele

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown

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 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 mercuryVM left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested 38e9db0 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

Copy link
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown
Contributor Author

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

@Marvisak

Copy link
Copy Markdown
Owner

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

Copy link
Copy Markdown
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants