[GlobalISel] urem x, (shl pow2, y) is not folded to a mask unless pow2 is 1
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!
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:
- 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.
- 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.
- 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.
- Run the test suite locally. Remember that the subdirectories under
test/create fine-grained testing targets, so you can e.g. usemake check-clang-astto only run Clang's AST tests. - Create a Git commit.
- Run
git clang-format HEAD~1to format your changes. - 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)
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)
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.
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