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

[X86] Add i512 ADD/SUB expansion on AVX512 targets

Open RKSimon opened this issue 11 months ago • 16 comments

Based off https://www.numberworld.org/y-cruncher/internals/addition.html we can implement i512 ISD::ADD/SUB handling inside X86TargetLowering::ReplaceNodeResults for subtargets that support useAVX512Regs - similar to what we've done for AND/OR/XOR.

The AVX512F variant seems to give a better base implementation than AVX512DQ, we can leave other combines to decide whether to move scalar ops (ADD/SHL) back to mask register ops (KADD/KSHIFT etc.).

We will need to use mayFoldIntoVector to check that its worthwhile to perform this as transferring integer data to the FPU unit can be expensive.

A later patch would be to add support for the carry-in/out handling (ISD::UADDO_CARRY etc.) as well for completely general multi-precision arithmetic - but basic i512 addition/subtraction should be enough for this ticket.

RKSimon avatar Dec 30 '25 15:12 RKSimon

@llvm/issue-subscribers-backend-x86

Author: Simon Pilgrim (RKSimon)

Based off https://www.numberworld.org/y-cruncher/internals/addition.html we can implement i512 ISD::ADD/SUB handling inside X86TargetLowering::ReplaceNodeResults for subtargets that support useAVX512Regs - similar to what we've done for AND/OR/XOR.

The AVX512F variant seems to give a better base implementation than AVX512DQ, we can leave other combines to decide whether to move scalar ops (ADD/SHL) back to mask register ops (KADD/KSHIFT etc.).

We will need to use mayFoldIntoVector to check that its worthwhile to perform this as transferring integer data to the FPU unit can be expensive.

A later patch would be to add support for the carry-in/out handling (ISD::UADDO_CARRY etc.) as well for completely general multi-precision arithmetic - but basic i512 addition/subtraction should be enough for this ticket.

llvmbot avatar Dec 30 '25 15:12 llvmbot

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.
  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.

llvmbot avatar Dec 30 '25 15:12 llvmbot

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

Author: Simon Pilgrim (RKSimon)

Based off https://www.numberworld.org/y-cruncher/internals/addition.html we can implement i512 ISD::ADD/SUB handling inside X86TargetLowering::ReplaceNodeResults for subtargets that support useAVX512Regs - similar to what we've done for AND/OR/XOR.

The AVX512F variant seems to give a better base implementation than AVX512DQ, we can leave other combines to decide whether to move scalar ops (ADD/SHL) back to mask register ops (KADD/KSHIFT etc.).

We will need to use mayFoldIntoVector to check that its worthwhile to perform this as transferring integer data to the FPU unit can be expensive.

A later patch would be to add support for the carry-in/out handling (ISD::UADDO_CARRY etc.) as well for completely general multi-precision arithmetic - but basic i512 addition/subtraction should be enough for this ticket.

llvmbot avatar Dec 30 '25 15:12 llvmbot

Hi @RKSimon :) Could you assign this issue to me, please :) ?

Islam-Imad avatar Dec 30 '25 15:12 Islam-Imad

Hi @RKSimon Can you assign this issue to me,please :) ?

Arka-009 avatar Dec 30 '25 15:12 Arka-009

@Islam-Imad @Arka-009 have you either got an avx512 capable machine to test this please?

RKSimon avatar Dec 30 '25 16:12 RKSimon

I'm on an AMD Ryzen 5 laptop. It supports AVX/AVX2 :[

Islam-Imad avatar Dec 30 '25 16:12 Islam-Imad

is it doable to utilize a given tool to solve this problem ?

Islam-Imad avatar Dec 30 '25 18:12 Islam-Imad

I'm not sure what you mean?

RKSimon avatar Dec 30 '25 20:12 RKSimon

An emulator for example that support avx512 to test the correctness on it instead of my machine. e.g Intel SDE i don't have any experience with these tools to be honest.

Islam-Imad avatar Dec 30 '25 20:12 Islam-Imad

We might have enough constexpr handling support for avx512 intrinsics to do basic testing of the original code, you can do that on Godbolt without avx512 support.

RKSimon avatar Dec 30 '25 21:12 RKSimon

I'll start working on it and will reach out if I have any questions. Please assign this to me.

Islam-Imad avatar Dec 30 '25 21:12 Islam-Imad

Using SDE is simple too, you can download it here and use like sde64 -skx -- YourApplication

phoebewang avatar Dec 31 '25 06:12 phoebewang

Hi, I am interested in working on this issue. I am new to LLVM. Could you please guide me on which files or functions I should look at first?

ayush-singh-4286 avatar Dec 31 '25 15:12 ayush-singh-4286

Hi, I am interested in working on this issue. I am new to LLVM. Could you please guide me on which files or functions I should look at first?

This issue is assigned to me already and i am still working on it. you can find other issues here https://github.com/llvm/llvm-project/issues?q=is%3Aissue%20state%3Aopen%20label%3A%22good%20first%20issue%22%20no%3Aassignee

Islam-Imad avatar Dec 31 '25 16:12 Islam-Imad

Using SDE is simple too, you can download it here and use like sde64 -skx -- YourApplication

I have already used this tool to test the correctness of my code. If you have time to see the problem of store vectorization in the opened PR that would be great :) @phoebewang

Islam-Imad avatar Jan 09 '26 19:01 Islam-Imad