Skip to content

recomp: omod on f64 and f32/f16 conversions, integer clamp on unsigned/packed adds and dots - #119

Closed
gefydev wants to merge 1 commit into
xcl-dev:mainfrom
gefydev:recomp-clamp-omod
Closed

gefydev wants to merge 1 commit into
xcl-dev:mainfrom
gefydev:recomp-clamp-omod

Conversation

@gefydev

@gefydev gefydev commented Nov 23, 2026

Copy link
Copy Markdown
Member

#105 item 4.

Dropped before this change:

  • omod on the f64 VALU results (v_add_f64, v_mul_f64, v_fma_f64, …);
  • omod and clamp on v_cvt_f32_f64 and v_cvt_f16_f32;
  • integer clamp (saturation) on v_add_nc_u32, v_sub_nc_u32, v_subrev_nc_u32, v_add3_u32, v_pk_add/sub_u16, v_pk_add/sub_i16, v_pk_mad_u16/i16, v_dot2_{i32_i16,u32_u16}, v_dot4_{i32_i8,u32_u8} and v_dot8_{i32_i4,u32_u4}. These wrapped instead.

One point of the audit was wrong: f64 clamp did not narrow to float. DXIL has a double Saturate, and the test confirms clamp keeps double precision on main. So that part is not changed.

Notes:

Test gpu_clamp_and_omod_per_instruction: every changed instruction once, with values that saturate. The non-clamp forms of an add, a packed add and a dot are controls that must still wrap. Without the emit change it fails (first at v_add_f64 omod). With it, it passes on a GTX 1650 under Wine (vkd3d-proton), and so do the other lib tests that run there. Not run on AMD.

…igned and packed adds and dots

The f64 VALU path, v_cvt_f32_f64 and v_cvt_f16_f32 dropped omod (and the two
conversions dropped clamp too). Integer clamp (saturation) was handled only
on v_add_nc_i32 / v_sub_nc_i32; v_add_nc_u32, v_sub_nc_u32, v_subrev_nc_u32,
v_add3_u32, v_pk_{add,sub}_{u16,i16}, v_pk_mad_{u16,i16} and the integer
dot products wrapped instead.

f64 clamp already kept double precision (DXIL has a double saturate), so it
stays. v_cvt_f16_f32 applies the modifiers in f32 before rounding: the same
f16 except where it is denormal.

Test gpu_clamp_and_omod_per_instruction: every changed instruction, with
the non-clamp forms of an add, a packed add and a dot as a control.
ivall added a commit that referenced this pull request Nov 23, 2026
Conflict with fdeacf0 (v_dot4 as SM 6.4 intrinsics) resolved: the intrinsics kept, #119's
clamp forms use them too.
ivall added a commit that referenced this pull request Nov 23, 2026
DXC hit an internal compiler error on (int64_t)dot4add_i8packed(a, b, 0) (the merge of #119
with the SM 6.4 intrinsics); the clamp forms go back to #119's helpers, the plain forms keep
the intrinsics.
@ivall

ivall commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Merged. It conflicted with fdeacf0, which made the plain v_dot4* SM 6.4 intrinsics (dot4add_i8packed / dot4add_u8packed, for FSR4). Your clamped forms keep dot4_u8 / dot4_i8 (c4df2fe): written with the intrinsic, (int64_t)dot4add_i8packed(a, b, 0) gave DXC an internal compiler error. Your test passes on the RX 9070. Checked on Windows 11 with an RX 9070 (RDNA4), merged together with the rest of #110–#129: cargo test --workspace green with the retail samples (shader pack, console DLLs, texture archive), lib tests also with XCL_SPLIT_WAVE64=1; the retail pack still translates 26 698 / 26 698; the FSR4 GPU probe passes; Unicorn Overlord and XDefiant run. Thanks!

@ivall

ivall commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Closing by hand: your rebase (28caa63) landed while I merged the earlier head (34efd81, merge 73c7bbe on main), so GitHub doesn't see it as merged. The content is in. One difference: the rebase writes the clamped 8-bit dots as (int64_t)dot4add_i8packed(a, b, 0) / (uint64_t)dot4add_u8packed(a, b, 0u) - that exact form gave DXC (Windows SDK 10.0.26100) an internal compiler error in gpu_clamp_and_omod_per_instruction, so main keeps your dot4_i8 / dot4_u8 helpers there (c4df2fe).

@ivall ivall closed this Nov 23, 2026
JGonzalezRodriguez pushed a commit to JGonzalezRodriguez/xcl that referenced this pull request Nov 23, 2026
Follow-up to xcl-dev#119 (issue xcl-dev#105 item 4), covering the remaining RDNA2 VALU
integer instructions that support CLAMP=1 saturation and previously
ignored it:

- 16-bit integer (valu16 / valu_pk): v_add_nc_u16, v_sub_nc_u16,
  v_add_nc_i16, v_sub_nc_i16, v_mul_lo_u16, v_mad_u16, v_mad_i16, and
  v_pk_mul_lo_u16 (plus the legacy u16 aliases already matched in valu16)
- 32-bit 24x24 / 16x16 mul/mad: v_mul_u32_u24, v_mul_i32_i24,
  v_mad_u32_u24, v_mad_i32_i24, v_mad_u32_u16, v_mad_i32_i16 (including
  SDWA CLAMP=1 forms via Self::mods)
- 32-bit SAD/MSAD: v_sad_u32, v_sad_u8, v_sad_hi_u8, v_sad_u16, v_msad_u8
- 32-bit carry-out / carry-in: v_add_co_u32, v_sub_co_u32,
  v_subrev_co_u32, v_add_co_ci_u32, v_sub_co_ci_u32, v_subrev_co_ci_u32
  (VOP3 + SDWA; saturates VDST while preserving unclamped carry/borrow in
  SDST/VCC)
- 64-bit integer MAD (valu64): v_mad_u64_u32 and v_mad_i64_i32 (saturates
  64-bit VDST while preserving the unclamped 65-bit SDST bit: unsigned
  carry-out for u64, bit 64 sign bit of the true 65-bit signed sum for
  v_mad_i64_i32)

Compiler / backend fixes in the HLSL prelude:
- sat_u32(uint64_t) uses branchless (uint)r | (0u - min((uint)(r >> 32), 1u))
  instead of select(r > 0xFFFFFFFFu, ...): Windows SDK DXC 1.7-1.9.2602
  lowers the select form on 64-bit products (v_mul_u32_u24 CLAMP=1) into
  forbidden llvm.umul.with.overflow.i32, failing DXIL validation.
- Adds sat_i16(int) using select instead of
  ((uint)clamp(r, -32768, 32767) & 0xFFFFu) across 16-bit signed saturation
  sites: NVIDIA 610.57.4 (Vulkan / vkd3d-proton) drops the & 0xFFFF mask
  after SMin(SMax(r, -32768), 32767), leaking sign bits into the upper half
  of the destination VGPR.

Adds GPU tests (wave64 + wave32) and a CPU-only HLSL codegen test
covering CLAMP=1 saturation, CLAMP=0 wrapping, boundary pairs, SDST/VCC
masks, SDWA CLAMP=1, and confirming v_cvt_pkrtz_f16_f32 ignores VOP3
OMOD/CLAMP.
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.

2 participants