Redundant subtractions and zeroizing for i16 division on AMDGPU
Clang version 24.0.0.
// clang -x hip needs declaration for hipLaunchKernel (3.)
int hipLaunchKernel(void*, int, int, void*, int64_t, void*);
__global__ void kernel(uint32_t *a)
{
int i = __builtin_amdgcn_workitem_id_x();
a[i] = (a[i] & 0xFFFFu) / 3u * 3u;
}
clang++ -x hip -O3 -c -save-temps --offload-arch=gfx900 --offload-arch=gfx1100 a.cpp
- It literally calculates
x - (x - x / 3 * 3). These twosubinstructions at the end are unnecessary. v0was shifted by 17 to the right sov0.his zero.v_cvt_u32_u16is not needed (sometimes it putsv_mov_b16 v0.h, 0).- Why does clang require the declaration for
hipLaunchKernel? Cannot it have it internally declared as some builtin?
gfx900 (v1 = (v1 & 0xFFFFu) / 3u * 3u):
s_mov_b32 s2, 0xaaab
v_mul_u32_u24_sdwa v2, v1, s2 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0 src1_sel:DWORD
v_lshrrev_b32_e32 v2, 17, v2
v_mul_lo_u16_e32 v2, 3, v2 ; the result is already here in v2 (1.)
v_sub_u16_e32 v2, v1, v2
v_sub_u32_sdwa v1, v1, v2 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0 src1_sel:DWORD
gfx1100 (v0 = (v2 & 0xFFFFu) / 3u * 3u):
v_and_b32_e32 v3, 0xffff, v2
v_mul_u32_u24_e32 v0, 0xaaab, v3
v_lshrrev_b32_e32 v0, 17, v0
v_mul_lo_u16 v0.l, v0.l, 3 ; the result is already here in v0 (1.)
v_sub_nc_u16 v0.l, v2.l, v0.l
v_cvt_u32_u16_e32 v0, v0.l ; this zeroes v0.h which is already zero from right shift by 17 (2.)
v_sub_nc_u32_e32 v0, v3, v0
@llvm/issue-subscribers-backend-amdgpu
Author: misos1
// clang -x hip needs declaration for hipLaunchKernel (3.)
int hipLaunchKernel(void*, int, int, void*, int64_t, void*);
__global__ void kernel(uint32_t *a)
{
int i = __builtin_amdgcn_workitem_id_x();
a[i] = (a[i] & 0xFFFFu) / 3u * 3u;
}
clang++ -x hip -O3 -c -save-temps --offload-arch=gfx900 --offload-arch=gfx1100 a.cpp
- It literally calculates
x - (x - x / 3 * 3). These twosubinstructions at the end are unnecessary. v0was shifted by 17 to the right sov0.his zero.v_cvt_u32_u16is not needed (sometimes it putsv_mov_b16 v0.h, 0).- Why does clang require the declaration for
hipLaunchKernel? Cannot it have it internally declared as some builtin?
gfx900 (v1 = (v1 & 0xFFFFu) / 3u * 3u):
s_mov_b32 s2, 0xaaab
v_mul_u32_u24_sdwa v2, v1, s2 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0 src1_sel:DWORD
v_lshrrev_b32_e32 v2, 17, v2
v_mul_lo_u16_e32 v2, 3, v2 ; the result is already here in v2 (1.)
v_sub_u16_e32 v2, v1, v2
v_sub_u32_sdwa v1, v1, v2 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0 src1_sel:DWORD
gfx1100 (v0 = (v2 & 0xFFFFu) / 3u * 3u):
v_and_b32_e32 v3, 0xffff, v2
v_mul_u32_u24_e32 v0, 0xaaab, v3
v_lshrrev_b32_e32 v0, 17, v0
v_mul_lo_u16 v0.l, v0.l, 3 ; the result is already here in v0 (1.)
v_sub_nc_u16 v0.l, v2.l, v0.l
v_cvt_u32_u16_e32 v0, v0.l ; this zeroes v0.h which is already zero from right shift by 17 (2.)
v_sub_nc_u32_e32 v0, v3, v0
hey, can this issue be assigned?
For Item 1 of redundant subtractions:
(x/c)*c --> x - (x%c) by InstCombine
--> x - (trunc(x) % c) by CorrelatedValuePropagation
The subtracted term, (trunc(x) % c) --> trunc(x) - (trunc(x)/c * c) by DAGCombiner, int div is combined into magic div.
--> Remains as x - (trunc(x) - (trunc(x)/c*c)) because x - trunc(x) cannot be folded.
Happens at gfx950, too. Redundant subs are similarly generated for AArch64 and X86. Will look into a fix.
Why does clang require the declaration for hipLaunchKernel? Cannot it have it internally declared as some builtin?
@yxsamliu, do you know if it is intentionally so?
HIP headers own the declaration of the function. Its parameter include dim3 type. If clang defines it, it also need to define the parameter types and be ABI consistent. Theoretically, HIP header may change the declaration of that function, which is a maintenance burden for clang to maintain consistency of that declaration with HIP headers. Normal HIP apps always include hip/hip_runtime.h so this is not an issue. If you only need to do device compilation, you can add --offload-device-only, which will not require the declaration of that function.
7. > Why does clang require the declaration for hipLaunchKernel? Cannot it have it internally declared as some builtin?
[@yxsamliu](https://github.com/yxsamliu), do you know if it is intentionally so?
This is is a super annoying bug that's always been there. Worse, last I checked it crashed if the signature wasn't right
--offload-device-only makes sense to me.