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

[X86] 8-bit vector multiplication should use shift and add method for more constants

Open WalterKruger opened this issue 1 year ago • 11 comments

Vector multiplication by most 8-bit constants is currently implemented by a width extension to 16-bits:

multiplyBy10_clang:
        movdqa  xmm1, xmm0
        punpckhbw       xmm1, xmm1
        movdqa  xmm2, xmmword ptr [rip + .LCPI0_0]
        pmullw  xmm1, xmm2
        movdqa  xmm3, xmmword ptr [rip + .LCPI0_1]
        pand    xmm1, xmm3
        punpcklbw       xmm0, xmm0
        pmullw  xmm0, xmm2
        pand    xmm0, xmm3
        packuswb        xmm0, xmm1
        ret

However, it is often more efficient to instead perform a short sequence of shift-and-adds both in terms of size and dependency length. For example, x * 10 = (x << 3) + (x << 1):

multiplyBy10_shiftAndAdd:
        movdqa  xmm1, xmm0
        paddb   xmm0, xmm0
        psllw   xmm1, 3
        pand    xmm1, xmmword ptr [rip + .LCPI0_0]
        paddb   xmm0, xmm1
        ret

This method is currently implemented, but only for constants that are almost powers of two. Notably, gcc always use this method (although its sequences are often non-optimal).

https://godbolt.org/z/naKxr6z6a

WalterKruger avatar Oct 20 '25 04:10 WalterKruger

@llvm/issue-subscribers-backend-x86

Author: Walter (WalterKruger)

Vector multiplication by most 8-bit constants is currently implemented by a width extension to 16-bits: ```asm multiplyBy10_clang: movdqa xmm1, xmm0 punpckhbw xmm1, xmm1 movdqa xmm2, xmmword ptr [rip + .LCPI0_0] pmullw xmm1, xmm2 movdqa xmm3, xmmword ptr [rip + .LCPI0_1] pand xmm1, xmm3 punpcklbw xmm0, xmm0 pmullw xmm0, xmm2 pand xmm0, xmm3 packuswb xmm0, xmm1 ret ```

However, it is often more efficient to instead perform a short sequence of shift-and-adds both in terms of size and dependency length. For example, x * 10 = (x &lt;&lt; 3) + (x &lt;&lt; 1):

multiplyBy10_shiftAndAdd:
        movdqa  xmm1, xmm0
        paddb   xmm0, xmm0
        psllw   xmm1, 3
        pand    xmm1, xmmword ptr [rip + .LCPI0_0]
        paddb   xmm0, xmm1
        ret

This method is currently implemented, but only for constants that are almost powers of two. Notably, gcc always use this method (although its sequences are often non-optimal).

https://godbolt.org/z/naKxr6z6a

llvmbot avatar Oct 20 '25 04:10 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 Oct 21 '25 07:10 llvmbot

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

Author: Walter (WalterKruger)

Vector multiplication by most 8-bit constants is currently implemented by a width extension to 16-bits: ```asm multiplyBy10_clang: movdqa xmm1, xmm0 punpckhbw xmm1, xmm1 movdqa xmm2, xmmword ptr [rip + .LCPI0_0] pmullw xmm1, xmm2 movdqa xmm3, xmmword ptr [rip + .LCPI0_1] pand xmm1, xmm3 punpcklbw xmm0, xmm0 pmullw xmm0, xmm2 pand xmm0, xmm3 packuswb xmm0, xmm1 ret ```

However, it is often more efficient to instead perform a short sequence of shift-and-adds both in terms of size and dependency length. For example, x * 10 = (x &lt;&lt; 3) + (x &lt;&lt; 1):

multiplyBy10_shiftAndAdd:
        movdqa  xmm1, xmm0
        paddb   xmm0, xmm0
        psllw   xmm1, 3
        pand    xmm1, xmmword ptr [rip + .LCPI0_0]
        paddb   xmm0, xmm1
        ret

This method is currently implemented, but only for constants that are almost powers of two. Notably, gcc always use this method (although its sequences are often non-optimal).

https://godbolt.org/z/naKxr6z6a

llvmbot avatar Oct 21 '25 07:10 llvmbot

We can probably profitably relax this to include splat multiplies of vXi8 types by any splat constant decomposable into a max of TWO pow2 constants.

This involves minor adjustments to X86TargetLowering::decomposeMulByConstant to special case vXi8 vector types along with any additional test cases we need.

RKSimon avatar Oct 21 '25 07:10 RKSimon

Hi! I'd like to work on this issue.

zheguang avatar Oct 21 '25 10:10 zheguang

Hi @zheguang , are you working on this issue? If not I'd like to take a crack at it. Thanks!

grodranlorth avatar Nov 22 '25 15:11 grodranlorth

@grodranlorth sure, go for it! :) (I've been side-tracked quite a bit this month. Hope to get back to LLVM next month or so.)

zheguang avatar Nov 24 '25 00:11 zheguang

Hello. After some investigation, I want to make sure I understand the task so I do the right thing. I understand that this is an optimization for x86 microarchitectures from roughly 2015 to 2018 -- is that correct?

LLVM already has the optimization to use shift+add/sub instead of pmull for vXi8 splat multiplies (prior PR) implemented which I see in the asm of my benchmark (though I understand it doesn't support negative constants of the form $x = 2^a \pm 2^b$)). At that time, we decided to enable this optimization only for microarchitectures that have the the TuningFastImmVectorShift tuning flag, i.e. those since about 2015. Additionally, LLVM has an optimization to prefer GFNI for these multiplies (roughly microarchitectures since 2018), which is even faster than shift+add/sub according to my benchmark.

Thus I want to clarify, what is the ask in this PR?

  • Enabling the shift+add/sub optimization regardless of TuningFastImmVectorShift?
  • Adding more constants (negative, at least) to the existing shift+add/sub optimization?
  • Something else?

cc: @WalterKruger @RKSimon

grodranlorth avatar Dec 01 '25 23:12 grodranlorth

The assembly snippet is for a baseline x86-64-v1 target but it seems like it is beneficial for all targets, at least without avx512, due to how long the chain is (splat => mul16 => pand => packuswb), even if both halves can be calculated at once with a wider vector.

though I understand it doesn't support negative constants of the form $x = 2^a ± 2^b$

I have only seen shift+add/sub be emitted for constants the are either exact powers of 2 or one off (i.e. $x = 2^n ± 1$). For example, the "negative" constant 7 is implemented as (x << 3) - x.

Additionally, LLVM has an optimization to prefer GFNI for these multiplies... which is even faster than shift+add/sub according to my benchmark.

vgf2p8affineqb is useful only as a more efficient way to implement bitshifts, as prior targets have emulate them using a 16-bit shift + AND to mask bits that cross a 8-bit lane.

WalterKruger avatar Dec 02 '25 01:12 WalterKruger

Most AVX512 target benefits from dedicated 16 => 8-bit truncation, but this is only might be competitive if the vector can be width extended to a larger size.

multiplyBy10:
        vpmovzxbw       ymm0, xmm0
        vpmullw ymm0, ymm0, ymmword ptr [rip + .LCPI0_0]
        vpmovwb xmm0, ymm0
        vzeroupper
        ret

Vs shift+add (without GFNI, which would save on the vpand):

multiplyBy10_shiftAndAdd:
        vpaddb   xmm1, xmm0, xmm0
        vpsllw   xmm0 xmm0, 3
        vpand    xmm0, xmm0, xmmword ptr [rip + .LCPI0_0]
        vpaddb   xmm0, xmm0, xmm1
        ret

@grodranlorth

WalterKruger avatar Dec 02 '25 01:12 WalterKruger

Thank you, Walter.

I have only seen shift+add/sub be emitted for constants the are either exact powers of 2 or one off (i.e. $x = 2^n \pm 1$). For example, the "negative" constant 7 is implemented as (x << 3) - x.

I understand that LLVM currently also supports constants of the form $2^a \pm 2^b$ (see llvm/lib/Target/X86/X86ISelLowering.cpp:49998 the last branch on Subtarget.fastImmVectorShift(), that prior PR above). When the target has the TuningFastImmVectorShift feature, it emits shifts+add/sub for such constants. For instance, if we add -march=alderlake to your example:

multiplyBy10:
        vpmovzxbw       ymm0, xmm0
        vpaddw  ymm1, ymm0, ymm0
        vpsllw  ymm0, ymm0, 3
        vpaddw  ymm0, ymm0, ymm1
        vpand   ymm0, ymm0, ymmword ptr [rip + .LCPI0_0]
        vextracti128    xmm1, ymm0, 1
        vpackuswb       xmm0, xmm0, xmm1
        vzeroupper
...
multiplyBy60:
        vpmovzxbw       ymm0, xmm0
        vpsllw  ymm1, ymm0, 2
        vpsllw  ymm0, ymm0, 6
        vpsubw  ymm0, ymm0, ymm1
        vpand   ymm0, ymm0, ymmword ptr [rip + .LCPI2_0]
        vextracti128    xmm1, ymm0, 1
        vpackuswb       xmm0, xmm0, xmm1
        vzeroupper

But it does not avoid the width extension.

The assembly snippet is for a baseline x86-64-v1 target but it seems like it is beneficial for all targets, at least without avx512, due to how long the chain is (splat => mul16 => pand => packuswb), even if both halves can be calculated at once with a wider vector.

I see, it could be that avoiding the width extension makes this optimization beneficial to other generic targets and not only those with TuningFastImmVectorShift. If so, that benefit is independent of GFNI support.

TuningFastImmVectorShift should avoid the width extension as well.

I will investigate extending X86TargetLowering::decomposeMulByConstant for these constants and thus avoid the width extension and use shift+add/sub for the generic targets.

@WalterKruger

grodranlorth avatar Dec 06 '25 16:12 grodranlorth