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

[AMDGPU][GlobalISel] No known bits for llvm.amdgcn.mul.u24 / mul.i24

Open petar-avramovic opened this issue 1 month ago • 5 comments

SelectionDAG computes known bits for MUL_U24 / MUL_I24 (AMDGPUTargetLowering::computeKnownBitsForTargetNode). GlobalISel's SITargetLowering::computeKnownBitsForTargetInstr has no case for the amdgcn_mul_u24 / amdgcn_mul_i24 intrinsics, so combines that depend on known bits (e.g. narrowing a 64-bit shl of a zext) do not fire after a 24-bit multiply.

Reproducer (from llvm/test/CodeGen/AMDGPU/GlobalISel/shl-ext-reduce.ll, mulu24_shl64):

define amdgpu_kernel void @mulu24_shl64(ptr addrspace(1) nocapture %arg) {
bb:
  %tmp = tail call i32 @llvm.amdgcn.workitem.id.x()
  %tmp1 = and i32 %tmp, 6
  %mulconv = mul nuw nsw i32 %tmp1, 7
  %tmp2 = zext i32 %mulconv to i64
  %tmp3 = getelementptr inbounds i32, ptr addrspace(1) %arg, i64 %tmp2
  store i32 0, ptr addrspace(1) %tmp3, align 4
  ret void
}

declare i32 @llvm.amdgcn.workitem.id.x()

llc -global-isel -mtriple=amdgpu9.00-amd-amdpal test.ll -o -

MIR after amdgpu-regbank-combiner:

    %17:vgpr(i32) = G_INTRINSIC intrinsic(@llvm.amdgcn.mul.u24), %16(i32), %29(i32)
    %21:vgpr(i64) = nsw G_SHL %19, %31(i32)
    %22:vgpr(p1) = nusw inbounds G_PTR_ADD %32, %21(i64)

Current GlobalISel output:

	s_load_dwordx2 s[0:1], s[4:5], 0x0
	v_and_b32_e32 v0, 6, v0
	v_mul_u32_u24_e32 v0, 7, v0
	v_mov_b32_e32 v1, 0
	v_lshlrev_b64 v[2:3], 2, v[0:1]
	s_waitcnt lgkmcnt(0)
	v_mov_b32_e32 v4, s0
	v_mov_b32_e32 v5, s1
	v_add_co_u32_e32 v2, vcc, v4, v2
	v_addc_co_u32_e32 v3, vcc, v5, v3, vcc
	global_store_dword v[2:3], v1, off
	s_endpgm

Desired output (same as SelectionDAG):

	s_load_dwordx2 s[0:1], s[4:5], 0x0
	v_and_b32_e32 v0, 6, v0
	v_mul_u32_u24_e32 v0, 7, v0
	v_lshlrev_b32_e32 v0, 2, v0
	v_mov_b32_e32 v1, 0
	s_waitcnt lgkmcnt(0)
	global_store_dword v0, v1, s[0:1]
	s_endpgm

Possible fix: add amdgcn_mul_u24 / amdgcn_mul_i24 to SITargetLowering::computeKnownBitsForTargetInstr: known bits of both operands, truncated to 24 bits and zero/sign extended, then KnownBits::mul.

petar-avramovic avatar Oct 28 '26 17:10 petar-avramovic

Hi!

This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:

  1. Check that no other contributor is working on this issue. If someone is assigned to the issue or claimed to be working on it, ping the person. After one week without a response, the assignee may be changed.
  2. Leave a comment indicating that you are working on the issue, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
  3. Fix the issue locally. Be aware that LLVM's AI policy forbids the use of AI tools to fix good first issues since it squanders a learning opportunity. Using LLMs to search and learn is permitted, since the goal is learning.
  4. Run the test suite locally. Remember that the subdirectories under test/ create fine-grained testing targets, so you can e.g. use make check-clang-ast to only run Clang's AST tests.
  5. Create a Git commit.
  6. Run git clang-format HEAD~1 to format your changes.
  7. Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.

If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.

@llvm/issue-subscribers-good-first-issue

Author: Petar Avramovic (petar-avramovic)

SelectionDAG computes known bits for MUL_U24 / MUL_I24 (AMDGPUTargetLowering::computeKnownBitsForTargetNode). GlobalISel's SITargetLowering::computeKnownBitsForTargetInstr has no case for the amdgcn_mul_u24 / amdgcn_mul_i24 intrinsics, so combines that depend on known bits (e.g. narrowing a 64-bit shl of a zext) do not fire after a 24-bit multiply.

Reproducer (from llvm/test/CodeGen/AMDGPU/GlobalISel/shl-ext-reduce.ll, mulu24_shl64):

define amdgpu_kernel void @<!-- -->mulu24_shl64(ptr addrspace(1) nocapture %arg) {
bb:
  %tmp = tail call i32 @<!-- -->llvm.amdgcn.workitem.id.x()
  %tmp1 = and i32 %tmp, 6
  %mulconv = mul nuw nsw i32 %tmp1, 7
  %tmp2 = zext i32 %mulconv to i64
  %tmp3 = getelementptr inbounds i32, ptr addrspace(1) %arg, i64 %tmp2
  store i32 0, ptr addrspace(1) %tmp3, align 4
  ret void
}

declare i32 @llvm.amdgcn.workitem.id.x()

llc -global-isel -mtriple=amdgpu9.00-amd-amdpal test.ll -o -

MIR after amdgpu-regbank-combiner:

    %17:vgpr(i32) = G_INTRINSIC intrinsic(@<!-- -->llvm.amdgcn.mul.u24), %16(i32), %29(i32)
    %21:vgpr(i64) = nsw G_SHL %19, %31(i32)
    %22:vgpr(p1) = nusw inbounds G_PTR_ADD %32, %21(i64)

Current GlobalISel output:

	s_load_dwordx2 s[0:1], s[4:5], 0x0
	v_and_b32_e32 v0, 6, v0
	v_mul_u32_u24_e32 v0, 7, v0
	v_mov_b32_e32 v1, 0
	v_lshlrev_b64 v[2:3], 2, v[0:1]
	s_waitcnt lgkmcnt(0)
	v_mov_b32_e32 v4, s0
	v_mov_b32_e32 v5, s1
	v_add_co_u32_e32 v2, vcc, v4, v2
	v_addc_co_u32_e32 v3, vcc, v5, v3, vcc
	global_store_dword v[2:3], v1, off
	s_endpgm

Desired output (same as SelectionDAG):

	s_load_dwordx2 s[0:1], s[4:5], 0x0
	v_and_b32_e32 v0, 6, v0
	v_mul_u32_u24_e32 v0, 7, v0
	v_lshlrev_b32_e32 v0, 2, v0
	v_mov_b32_e32 v1, 0
	s_waitcnt lgkmcnt(0)
	global_store_dword v0, v1, s[0:1]
	s_endpgm

Possible fix: add amdgcn_mul_u24 / amdgcn_mul_i24 to SITargetLowering::computeKnownBitsForTargetInstr: known bits of both operands, truncated to 24 bits and zero/sign extended, then KnownBits::mul.

@llvm/issue-subscribers-backend-amdgpu

Author: Petar Avramovic (petar-avramovic)

SelectionDAG computes known bits for MUL_U24 / MUL_I24 (AMDGPUTargetLowering::computeKnownBitsForTargetNode). GlobalISel's SITargetLowering::computeKnownBitsForTargetInstr has no case for the amdgcn_mul_u24 / amdgcn_mul_i24 intrinsics, so combines that depend on known bits (e.g. narrowing a 64-bit shl of a zext) do not fire after a 24-bit multiply.

Reproducer (from llvm/test/CodeGen/AMDGPU/GlobalISel/shl-ext-reduce.ll, mulu24_shl64):

define amdgpu_kernel void @<!-- -->mulu24_shl64(ptr addrspace(1) nocapture %arg) {
bb:
  %tmp = tail call i32 @<!-- -->llvm.amdgcn.workitem.id.x()
  %tmp1 = and i32 %tmp, 6
  %mulconv = mul nuw nsw i32 %tmp1, 7
  %tmp2 = zext i32 %mulconv to i64
  %tmp3 = getelementptr inbounds i32, ptr addrspace(1) %arg, i64 %tmp2
  store i32 0, ptr addrspace(1) %tmp3, align 4
  ret void
}

declare i32 @llvm.amdgcn.workitem.id.x()

llc -global-isel -mtriple=amdgpu9.00-amd-amdpal test.ll -o -

MIR after amdgpu-regbank-combiner:

    %17:vgpr(i32) = G_INTRINSIC intrinsic(@<!-- -->llvm.amdgcn.mul.u24), %16(i32), %29(i32)
    %21:vgpr(i64) = nsw G_SHL %19, %31(i32)
    %22:vgpr(p1) = nusw inbounds G_PTR_ADD %32, %21(i64)

Current GlobalISel output:

	s_load_dwordx2 s[0:1], s[4:5], 0x0
	v_and_b32_e32 v0, 6, v0
	v_mul_u32_u24_e32 v0, 7, v0
	v_mov_b32_e32 v1, 0
	v_lshlrev_b64 v[2:3], 2, v[0:1]
	s_waitcnt lgkmcnt(0)
	v_mov_b32_e32 v4, s0
	v_mov_b32_e32 v5, s1
	v_add_co_u32_e32 v2, vcc, v4, v2
	v_addc_co_u32_e32 v3, vcc, v5, v3, vcc
	global_store_dword v[2:3], v1, off
	s_endpgm

Desired output (same as SelectionDAG):

	s_load_dwordx2 s[0:1], s[4:5], 0x0
	v_and_b32_e32 v0, 6, v0
	v_mul_u32_u24_e32 v0, 7, v0
	v_lshlrev_b32_e32 v0, 2, v0
	v_mov_b32_e32 v1, 0
	s_waitcnt lgkmcnt(0)
	global_store_dword v0, v1, s[0:1]
	s_endpgm

Possible fix: add amdgcn_mul_u24 / amdgcn_mul_i24 to SITargetLowering::computeKnownBitsForTargetInstr: known bits of both operands, truncated to 24 bits and zero/sign extended, then KnownBits::mul.

Hi, i'd like to do this, could you please assign this issue to me ?

TelGome avatar Oct 29 '26 08:10 TelGome

I like to work on this issue can you please assign to me ?

kulkarni-aditya-01 avatar Oct 29 '26 09:10 kulkarni-aditya-01