llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Redundant subtractions and zeroizing for i16 division on AMDGPU

Open misos1 opened this issue 1 month ago • 7 comments

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
  1. It literally calculates x - (x - x / 3 * 3). These two sub instructions at the end are unnecessary.
  2. v0 was shifted by 17 to the right so v0.h is zero. v_cvt_u32_u16 is not needed (sometimes it puts v_mov_b16 v0.h, 0).
  3. 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

misos1 avatar Oct 06 '26 17:10 misos1

@llvm/issue-subscribers-backend-amdgpu

Author: misos1

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
  1. It literally calculates x - (x - x / 3 * 3). These two sub instructions at the end are unnecessary.
  2. v0 was shifted by 17 to the right so v0.h is zero. v_cvt_u32_u16 is not needed (sometimes it puts v_mov_b16 v0.h, 0).
  3. 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?

Vara715 avatar Oct 06 '26 18:10 Vara715

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.

yoonseoch avatar Oct 08 '26 18:10 yoonseoch

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?

yoonseoch avatar Oct 08 '26 21:10 yoonseoch

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.

yxsamliu avatar Oct 08 '26 21:10 yxsamliu

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

arsenm avatar Oct 08 '26 21:10 arsenm

--offload-device-only makes sense to me.

yoonseoch avatar Oct 08 '26 22:10 yoonseoch