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

[X86] Missed Optimization: Vector 8-bit `rotl(x, 1)` should be lowered as `(x + x) - (x < 0)`

Open WalterKruger opened this issue 6 months ago • 13 comments

Due to a lack of support, most 8-bit shifts are implemented using a 16-bit shift + AND:

rotl1_src:
        movdqa  xmm1, xmm0
        paddb   xmm1, xmm0
        psrlw   xmm0, 7
        pand    xmm0, xmmword ptr [rip + .LCPI2_0]
        por     xmm0, xmm1
        ret

The OR and right shift can be replaced with a subtraction by a less-than-zero mask, which acts like a conditional disjoint add by 1. This shortens the dependency chain and avoids the shift, which has worse throughput on some architectures.

rotl1_tgt:
        pxor    xmm1, xmm1
        pcmpgtb xmm1, xmm0
        paddb   xmm0, xmm0
        psubb   xmm0, xmm1
        ret

https://godbolt.org/z/199KoWhs8

WalterKruger avatar May 16 '26 04:05 WalterKruger

hi i would like to take this issue

Ippo47 avatar May 16 '26 08:05 Ippo47

@llvm/issue-subscribers-backend-x86

Author: Walter (WalterKruger)

Due to a lack of support, most 8-bit shifts are implemented using a 16-bit shift + AND:
rotl1_src:
        movdqa  xmm1, xmm0
        paddb   xmm1, xmm0
        psrlw   xmm0, 7
        pand    xmm0, xmmword ptr [rip + .LCPI2_0]
        por     xmm0, xmm1
        ret

The OR and right shift can be replaced with a subtraction by a less-than-zero mask, which acts like a conditional disjoint add by 1. This shortens the dependency chain and avoids the shift, which has worse throughput on some architectures.

rotl1_tgt:
        pxor    xmm1, xmm1
        pcmpgtb xmm1, xmm0
        paddb   xmm0, xmm0
        psubb   xmm0, xmm1
        ret

https://godbolt.org/z/199KoWhs8

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: Walter (WalterKruger)

Due to a lack of support, most 8-bit shifts are implemented using a 16-bit shift + AND:
rotl1_src:
        movdqa  xmm1, xmm0
        paddb   xmm1, xmm0
        psrlw   xmm0, 7
        pand    xmm0, xmmword ptr [rip + .LCPI2_0]
        por     xmm0, xmm1
        ret

The OR and right shift can be replaced with a subtraction by a less-than-zero mask, which acts like a conditional disjoint add by 1. This shortens the dependency chain and avoids the shift, which has worse throughput on some architectures.

rotl1_tgt:
        pxor    xmm1, xmm1
        pcmpgtb xmm1, xmm0
        paddb   xmm0, xmm0
        psubb   xmm0, xmm1
        ret

https://godbolt.org/z/199KoWhs8

Easy enough to implement in LowerRotate

@Ippo47 please begin by creating a alive2 link to demonstrate you understand the task

RKSimon avatar May 16 '26 13:05 RKSimon

Hi, if no one is working on this I would like to contribute

Alive2 link: https://alive2.llvm.org/ce/z/teP3my

ArihantChawla avatar May 21 '26 11:05 ArihantChawla

@Ippo47 is still working on this - please give them time to respond, and maybe find another ticket that doesn't already have an assignee?

RKSimon avatar May 21 '26 11:05 RKSimon

@RKSimon @WalterKruger sorry for late reply, here is my Alive2 link https://alive2.llvm.org/ce/z/FrSxNt

Ippo47 avatar May 22 '26 08:05 Ippo47

@lppo47 any luck? You might want to take a look at #198980 for reference

RKSimon avatar May 25 '26 14:05 RKSimon

@lppo47 any luck? You might want to take a look at #198980 for reference

sure, thanks for this

Ippo47 avatar May 26 '26 12:05 Ippo47

can i take this issue ??

AnkitDubeycs25 avatar Jun 02 '26 08:06 AnkitDubeycs25

@Ippo47 is still working on it!! @AnkitDubeycs25

minicusprime avatar Jun 02 '26 08:06 minicusprime

@RKSimon Issue was inactive for over a month with no development, so I took it. Please review.

VachanVY avatar Jun 28 '26 06:06 VachanVY