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

[AMDGPU][GlobalISel] <2 x i64> udiv/urem with 32-bit operands uses the full 64-bit expansion

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

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.

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

@llvm/issue-subscribers-backend-amdgpu

Author: Petar Avramovic (petar-avramovic)

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 &lt;2 x i64&gt; @<!-- -->v_urem_v2i64_24bit(&lt;2 x i64&gt; %num, &lt;2 x i64&gt; %den) {
  %num.mask = and &lt;2 x i64&gt; %num, &lt;i64 16777215, i64 16777215&gt;
  %den.mask = and &lt;2 x i64&gt; %den, &lt;i64 16777215, i64 16777215&gt;
  %result = urem &lt;2 x i64&gt; %num.mask, %den.mask
  ret &lt;2 x i64&gt; %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:

  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'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 &lt;2 x i64&gt; @<!-- -->v_urem_v2i64_24bit(&lt;2 x i64&gt; %num, &lt;2 x i64&gt; %den) {
  %num.mask = and &lt;2 x i64&gt; %num, &lt;i64 16777215, i64 16777215&gt;
  %den.mask = and &lt;2 x i64&gt; %den, &lt;i64 16777215, i64 16777215&gt;
  %result = urem &lt;2 x i64&gt; %num.mask, %den.mask
  ret &lt;2 x i64&gt; %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 ?

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

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

petar-avramovic avatar Oct 29 '26 10:10 petar-avramovic

Hi @kulkarni-aditya-01, are you still working on this? If not, I'd be happy to pick it up.

Enfey avatar Nov 11 '26 02:11 Enfey

Seeing as there's been no response in over a week, I'm now working on this issue.

Enfey avatar Nov 16 '26 18:11 Enfey

Yes please I think I have to learn more into llvm and then come back again for resolve. Sorry for late reply.

kulkarni-aditya-01 avatar Nov 17 '26 10:11 kulkarni-aditya-01