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

[GlobalISel] urem x, (shl pow2, y) is not folded to a mask unless pow2 is 1

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

The urem_pow2_to_mask combine uses isKnownToBeAPowerOfTwo, which for G_SHL only accepts a shift of constant 1. A shift of any other power of two may shift out to zero, so it is not known to be a power of two. For urem that does not matter: division by zero is poison, so x urem (shl 4096, y) can be folded to x & ((shl 4096, y) - 1). SelectionDAG::isKnownToBeAPowerOfTwo takes an OrZero flag for this, and SelectionDAG does the fold.

Reproducer (from llvm/test/CodeGen/AMDGPU/GlobalISel/urem.i32.ll, v_urem_i32_pow2_shl_denom):

define i32 @v_urem_i32_pow2_shl_denom(i32 %x, i32 %y) {
  %shl.y = shl i32 4096, %y
  %r = urem i32 %x, %shl.y
  ret i32 %r
}

llc -global-isel -amdgpu-codegenprepare-disable-idiv-expansion=1 -mtriple=amdgpu6.00-amd-amdpal test.ll -o -

Current GlobalISel output: full 32-bit urem expansion (20 instructions, v_rcp_iflag_f32 + mul_hi + two correction steps).

Desired output (same as SelectionDAG):

	s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
	v_lshl_b32_e32 v1, 0x1000, v1
	v_add_i32_e32 v1, vcc, -1, v1
	v_and_b32_e32 v0, v0, v1
	s_setpc_b64 s[30:31]

Same for vectors and i64: urem.i32.ll v_urem_v2i32_pow2_shl_denom, urem.i64.ll.

Possible fix: add an OrZero parameter to GlobalISel's isKnownToBeAPowerOfTwo (letting G_SHL/G_LSHR recurse without proving non-zero), and pass OrZero=true from urem_pow2_to_mask.

petar-avramovic avatar Oct 28 '26 16: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)

The urem_pow2_to_mask combine uses isKnownToBeAPowerOfTwo, which for G_SHL only accepts a shift of constant 1. A shift of any other power of two may shift out to zero, so it is not known to be a power of two. For urem that does not matter: division by zero is poison, so x urem (shl 4096, y) can be folded to x & ((shl 4096, y) - 1). SelectionDAG::isKnownToBeAPowerOfTwo takes an OrZero flag for this, and SelectionDAG does the fold.

Reproducer (from llvm/test/CodeGen/AMDGPU/GlobalISel/urem.i32.ll, v_urem_i32_pow2_shl_denom):

define i32 @<!-- -->v_urem_i32_pow2_shl_denom(i32 %x, i32 %y) {
  %shl.y = shl i32 4096, %y
  %r = urem i32 %x, %shl.y
  ret i32 %r
}

llc -global-isel -amdgpu-codegenprepare-disable-idiv-expansion=1 -mtriple=amdgpu6.00-amd-amdpal test.ll -o -

Current GlobalISel output: full 32-bit urem expansion (20 instructions, v_rcp_iflag_f32 + mul_hi + two correction steps).

Desired output (same as SelectionDAG):

	s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
	v_lshl_b32_e32 v1, 0x1000, v1
	v_add_i32_e32 v1, vcc, -1, v1
	v_and_b32_e32 v0, v0, v1
	s_setpc_b64 s[30:31]

Same for vectors and i64: urem.i32.ll v_urem_v2i32_pow2_shl_denom, urem.i64.ll.

Possible fix: add an OrZero parameter to GlobalISel's isKnownToBeAPowerOfTwo (letting G_SHL/G_LSHR recurse without proving non-zero), and pass OrZero=true from urem_pow2_to_mask.

@llvm/issue-subscribers-backend-amdgpu

Author: Petar Avramovic (petar-avramovic)

The urem_pow2_to_mask combine uses isKnownToBeAPowerOfTwo, which for G_SHL only accepts a shift of constant 1. A shift of any other power of two may shift out to zero, so it is not known to be a power of two. For urem that does not matter: division by zero is poison, so x urem (shl 4096, y) can be folded to x & ((shl 4096, y) - 1). SelectionDAG::isKnownToBeAPowerOfTwo takes an OrZero flag for this, and SelectionDAG does the fold.

Reproducer (from llvm/test/CodeGen/AMDGPU/GlobalISel/urem.i32.ll, v_urem_i32_pow2_shl_denom):

define i32 @<!-- -->v_urem_i32_pow2_shl_denom(i32 %x, i32 %y) {
  %shl.y = shl i32 4096, %y
  %r = urem i32 %x, %shl.y
  ret i32 %r
}

llc -global-isel -amdgpu-codegenprepare-disable-idiv-expansion=1 -mtriple=amdgpu6.00-amd-amdpal test.ll -o -

Current GlobalISel output: full 32-bit urem expansion (20 instructions, v_rcp_iflag_f32 + mul_hi + two correction steps).

Desired output (same as SelectionDAG):

	s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
	v_lshl_b32_e32 v1, 0x1000, v1
	v_add_i32_e32 v1, vcc, -1, v1
	v_and_b32_e32 v0, v0, v1
	s_setpc_b64 s[30:31]

Same for vectors and i64: urem.i32.ll v_urem_v2i32_pow2_shl_denom, urem.i64.ll.

Possible fix: add an OrZero parameter to GlobalISel's isKnownToBeAPowerOfTwo (letting G_SHL/G_LSHR recurse without proving non-zero), and pass OrZero=true from urem_pow2_to_mask.

Hi, I'd like to work on this, could you please assign it to me? Thank you.

Enfey avatar Oct 29 '26 22:10 Enfey

5. Hi, I'd like to work on this, could you please assign it to me? Thank you.

See point 2, you don't need assignment just state you are working on this

arsenm avatar Oct 29 '26 23:10 arsenm