[AMDGPU][GlobalISel] <2 x i64> udiv/urem with 32-bit operands uses the full 64-bit expansion
SelectionDAG's AMDGPUTargetLowering::LowerUDIVREM64 expands a 64-bit udiv/urem as a 32-bit division (and merges the result with a zero high half) when both operands are known to fit in 32 bits. GlobalISel does this for scalar i64 but not for <2 x i64>, where each element gets the full 64-bit expansion.
Reproducer (from llvm/test/CodeGen/AMDGPU/GlobalISel/urem.i64.ll, v_urem_v2i64_24bit):
define <2 x i64> @v_urem_v2i64_24bit(<2 x i64> %num, <2 x i64> %den) {
%num.mask = and <2 x i64> %num, <i64 16777215, i64 16777215>
%den.mask = and <2 x i64> %den, <i64 16777215, i64 16777215>
%result = urem <2 x i64> %num.mask, %den.mask
ret <2 x i64> %result
}
llc -global-isel -amdgpu-codegenprepare-disable-idiv-expansion=1 -mtriple=amdgpu6.00-amd-amdpal -denormal-fp-math-f32=preserve-sign -mattr=+mad-mac-f32-insts test.ll -o -
Current GlobalISel output: 209 instructions (full 64-bit urem expansion per element).
Desired output: 42 instructions, as SelectionDAG, i.e. two 32-bit urem expansions with the high halves set to 0.
Same for udiv: udiv.i64.ll v_udiv_v2i64_24bit.
Possible fix: in AMDGPULegalizerInfo's 64-bit div/rem legalization, check known bits of both operands and use the 32-bit expansion when the high halves are zero, mirroring LowerUDIVREM64.
@llvm/issue-subscribers-backend-amdgpu
Author: Petar Avramovic (petar-avramovic)
Reproducer (from llvm/test/CodeGen/AMDGPU/GlobalISel/urem.i64.ll, v_urem_v2i64_24bit):
define <2 x i64> @<!-- -->v_urem_v2i64_24bit(<2 x i64> %num, <2 x i64> %den) {
%num.mask = and <2 x i64> %num, <i64 16777215, i64 16777215>
%den.mask = and <2 x i64> %den, <i64 16777215, i64 16777215>
%result = urem <2 x i64> %num.mask, %den.mask
ret <2 x i64> %result
}
llc -global-isel -amdgpu-codegenprepare-disable-idiv-expansion=1 -mtriple=amdgpu6.00-amd-amdpal -denormal-fp-math-f32=preserve-sign -mattr=+mad-mac-f32-insts test.ll -o -
Current GlobalISel output: 209 instructions (full 64-bit urem expansion per element).
Desired output: 42 instructions, as SelectionDAG, i.e. two 32-bit urem expansions with the high halves set to 0.
Same for udiv: udiv.i64.ll v_udiv_v2i64_24bit.
Possible fix: in AMDGPULegalizerInfo's 64-bit div/rem legalization, check known bits of both operands and use the 32-bit expansion when the high halves are zero, mirroring LowerUDIVREM64.
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.i64.ll, v_urem_v2i64_24bit):
define <2 x i64> @<!-- -->v_urem_v2i64_24bit(<2 x i64> %num, <2 x i64> %den) {
%num.mask = and <2 x i64> %num, <i64 16777215, i64 16777215>
%den.mask = and <2 x i64> %den, <i64 16777215, i64 16777215>
%result = urem <2 x i64> %num.mask, %den.mask
ret <2 x i64> %result
}
llc -global-isel -amdgpu-codegenprepare-disable-idiv-expansion=1 -mtriple=amdgpu6.00-amd-amdpal -denormal-fp-math-f32=preserve-sign -mattr=+mad-mac-f32-insts test.ll -o -
Current GlobalISel output: 209 instructions (full 64-bit urem expansion per element).
Desired output: 42 instructions, as SelectionDAG, i.e. two 32-bit urem expansions with the high halves set to 0.
Same for udiv: udiv.i64.ll v_udiv_v2i64_24bit.
Possible fix: in AMDGPULegalizerInfo's 64-bit div/rem legalization, check known bits of both operands and use the 32-bit expansion when the high halves are zero, mirroring LowerUDIVREM64.
I like to work on this issue can you please assign it to me ?
5. I like to work on this issue can you please assign it to me ?
please pick one issue at a time, how about this one for start and when your fix is merged pick next one
Hi @kulkarni-aditya-01, are you still working on this? If not, I'd be happy to pick it up.
Seeing as there's been no response in over a week, I'm now working on this issue.
Yes please I think I have to learn more into llvm and then come back again for resolve. Sorry for late reply.