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

[X86] computeKnownBitsForTargetNode - add X86ISD::GF2P8AFFINEQB handling

Open RKSimon opened this issue 8 months ago • 17 comments

X86ISD::GF2P8AFFINEQB nodes are being used for many shift/rotate style expansions, it'd be very useful if we can access the known bits to attempt additional folding afterward (e.g #190502).

This should only be attempted by somebody with access to a CPU with GFNI support to allow them to actually test this properly (although constexpr testing can help now that GFNI intrinsics support it).

Reference: https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#ig_expand=172,3053,2650,3028,3046,849,3516,3630,3054,3053,3054,4389,2239,2239,4602,205,196,4461,3954,1601,2140,417,2873,2851,2571,5840,2571,2927,3639&text=VGF2P8AFFINEQB

(see also GFNIAffine() in llvm-project\clang\lib\AST\ExprConstant.cpp)

RKSimon avatar Apr 05 '26 11:04 RKSimon

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 Apr 05 '26 11:04 llvmbot

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

Author: Simon Pilgrim (RKSimon)

X86ISD::GF2P8AFFINEQB nodes are being used for many shift/rotate style expansions, it'd be very useful if we can access the known bits to attempt additional folding afterward (e.g #190502).

This should only be attempted by somebody with access to a CPU with GFNI support to allow them to actually test this properly (although constexpr testing can help now that GFNI intrinsics support it).

Reference: https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#ig_expand=172,3053,2650,3028,3046,849,3516,3630,3054,3053,3054,4389,2239,2239,4602,205,196,4461,3954,1601,2140,417,2873,2851,2571,5840,2571,2927,3639&text=VGF2P8AFFINEQB

(see also GFNIAffine() in llvm-project\clang\lib\AST\ExprConstant.cpp)

llvmbot avatar Apr 05 '26 11:04 llvmbot

@llvm/issue-subscribers-backend-x86

Author: Simon Pilgrim (RKSimon)

X86ISD::GF2P8AFFINEQB nodes are being used for many shift/rotate style expansions, it'd be very useful if we can access the known bits to attempt additional folding afterward (e.g #190502).

This should only be attempted by somebody with access to a CPU with GFNI support to allow them to actually test this properly (although constexpr testing can help now that GFNI intrinsics support it).

Reference: https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#ig_expand=172,3053,2650,3028,3046,849,3516,3630,3054,3053,3054,4389,2239,2239,4602,205,196,4461,3954,1601,2140,417,2873,2851,2571,5840,2571,2927,3639&text=VGF2P8AFFINEQB

(see also GFNIAffine() in llvm-project\clang\lib\AST\ExprConstant.cpp)

llvmbot avatar Apr 05 '26 11:04 llvmbot

@RKSimon can i work on this ?

Ko496-glitch avatar Apr 05 '26 13:04 Ko496-glitch

@Ko496-glitch since you asked for suggestions, one performant software implementation of a affine is to calculate one bit per iteration for all bytes at once. It seems you can use assignment operators on the KnownBits struct to calculate both ones and zeros simultaneously, it allows for splats by the bit width to generate the needed constants for arbitrary vector sizes without code duplication:

  1. Splat the ith row within each matrix, to all its rows. Maybe using shift + masking + multiplication on the APInt fields?
  2. AND with the vector KnownBits to isolate the bit which are "selected" by that row.
  3. Calculate the byte-wise parity by doing a cumulative xor: x ^= x >> 1; x ^= x >> 2; x ^= x >> 4;. The least bit should contain that byte's parity. The shift can cross the byte boundary without issue.
  4. OR that parity to the 7-i th bit position within the accumulator, which should initially be zero.
  5. After 8 iterations, all accumulator bit should be computed. XOR that with the immediate splatted to all lanes.

WalterKruger avatar Apr 12 '26 13:04 WalterKruger

@WalterKruger thank you so much for this, i will read this more in depth later today

Ko496-glitch avatar Apr 12 '26 14:04 Ko496-glitch

@RKSimon Apologies, but i just checked and my pc does not have GFI support, it is an older version machine so testing wont be possible. You can reassign it but again sorry i should have checked this before asking to work on this

Ko496-glitch avatar Apr 14 '26 02:04 Ko496-glitch

You might want to consider using SDE?

Andarwinux avatar Apr 14 '26 04:04 Andarwinux

Hi @Ko496-glitch are you still working on this? I have a CPU with GFNI support if you would still like to reassign this task

asadium avatar Apr 16 '26 04:04 asadium

Feel free to work,i am trying to figure this on intel SDE but we can always match our findings :)

Ko496-glitch avatar Apr 16 '26 05:04 Ko496-glitch

@asadium hey just wanted to check in if you are active on this ? otherwise i can work on the PR

Ko496-glitch avatar Apr 20 '26 03:04 Ko496-glitch

Yes go ahead, I haven't made substantial progress just yet

asadium avatar Apr 20 '26 03:04 asadium

@RKSimon just wanted to inform i have started working on this, sorry for the delay i was taking care of something

Ko496-glitch avatar Apr 25 '26 16:04 Ko496-glitch

Hi @Ko496-glitch are you still active on this? If not I can make a PR also

asadium avatar Aug 18 '26 02:08 asadium

Hi @Ko496-glitch are you still active on this? If not I can make a PR also

Hi, yes please go ahead

Ko496-glitch avatar Aug 18 '26 20:08 Ko496-glitch

Hey @asadium, are you still working on this?

molaed avatar Oct 28 '26 05:10 molaed

Hi, yes. Feel free to ping me in 2 weeks if I haven't made any updates though : )

asadium avatar Oct 28 '26 10:10 asadium