Skip to content

[X86] Ensure that bit reversals of byte vectors are properly lowered on pure GFNI targets #148304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 18, 2025

Conversation

TDecking
Copy link
Contributor

@TDecking TDecking commented Jul 11, 2025

Fixes #148238.

When GFNI is present, custom bit reversal lowerings for scalar integers become active. They work by swapping the bytes in the scalar value and then reversing bits in a vector of bytes. However, the custom bit reversal lowering for a vector of bytes is disabled if GFNI is present in isolation, resulting messed up code.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Jul 11, 2025

@llvm/pr-subscribers-backend-x86

Author: Tobias Decking (TDecking)

Changes

When GFNI is present, custom bit reversal lowerings for scalar integers become active. They work by swapping the bytes in the scalar value and then reversing bits in a vector of bytes. However, the custom bit reversal lowering for a vector of bytes is disabled if GFNI is present in isolation, resulting messed up code.


Full diff: https://github.com/llvm/llvm-project/pull/148304.diff

2 Files Affected:

  • (modified) llvm/lib/Target/X86/X86ISelLowering.cpp (+2-1)
  • (modified) llvm/test/CodeGen/X86/vector-bitreverse.ll (+53-47)
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index 5e35d5630d667..81bdf1d79f881 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -1328,6 +1328,7 @@ X86TargetLowering::X86TargetLowering(const X86TargetMachine &TM,
     setOperationAction(ISD::BITREVERSE, MVT::i16, Custom);
     setOperationAction(ISD::BITREVERSE, MVT::i32, Custom);
     setOperationAction(ISD::BITREVERSE, MVT::i64, Custom);
+    setOperationAction(ISD::BITREVERSE, MVT::v16i8, Custom);
   }
 
   if (!Subtarget.useSoftFloat() && Subtarget.hasSSSE3()) {
@@ -32694,7 +32695,7 @@ static SDValue LowerBITREVERSE(SDValue Op, const X86Subtarget &Subtarget,
   if (Subtarget.hasXOP() && !VT.is512BitVector())
     return LowerBITREVERSE_XOP(Op, DAG);
 
-  assert(Subtarget.hasSSSE3() && "SSSE3 required for BITREVERSE");
+  assert((Subtarget.hasSSSE3() || Subtarget.hasGFNI()) && "SSSE3 or GFNI required for BITREVERSE");
 
   SDValue In = Op.getOperand(0);
   SDLoc DL(Op);
diff --git a/llvm/test/CodeGen/X86/vector-bitreverse.ll b/llvm/test/CodeGen/X86/vector-bitreverse.ll
index 5dcf19013f0b7..4ba2ec393d5d4 100644
--- a/llvm/test/CodeGen/X86/vector-bitreverse.ll
+++ b/llvm/test/CodeGen/X86/vector-bitreverse.ll
@@ -8,6 +8,7 @@
 ; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+avx512vl,+avx512bw | FileCheck %s --check-prefixes=ALL,AVX,AVX512,AVX512BW
 ; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+xop,+avx | FileCheck %s --check-prefixes=ALL,XOP,XOPAVX1
 ; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+xop,+avx2 | FileCheck %s --check-prefixes=ALL,XOP,XOPAVX2
+; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+gfni | FileCheck %s --check-prefixes=ALL,GFNIPURE
 ; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+ssse3,+gfni | FileCheck %s --check-prefixes=ALL,GFNISSE
 ; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+avx,+gfni | FileCheck %s --check-prefixes=ALL,GFNIAVX,GFNIAVX1
 ; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+avx2,+gfni | FileCheck %s --check-prefixes=ALL,GFNIAVX,GFNIAVX2
@@ -60,13 +61,13 @@ define i8 @test_bitreverse_i8(i8 %a) nounwind {
 ; XOP-NEXT:    # kill: def $al killed $al killed $eax
 ; XOP-NEXT:    retq
 ;
-; GFNISSE-LABEL: test_bitreverse_i8:
-; GFNISSE:       # %bb.0:
-; GFNISSE-NEXT:    movd %edi, %xmm0
-; GFNISSE-NEXT:    gf2p8affineqb $0, {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
-; GFNISSE-NEXT:    movd %xmm0, %eax
-; GFNISSE-NEXT:    # kill: def $al killed $al killed $eax
-; GFNISSE-NEXT:    retq
+; GFNIPURE-LABEL: test_bitreverse_i8:
+; GFNIPURE:       # %bb.0:
+; GFNIPURE-NEXT:    movd %edi, %xmm0
+; GFNIPURE-NEXT:    gf2p8affineqb $0, {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
+; GFNIPURE-NEXT:    movd %xmm0, %eax
+; GFNIPURE-NEXT:    # kill: def $al killed $al killed $eax
+; GFNIPURE-NEXT:    retq
 ;
 ; GFNIAVX-LABEL: test_bitreverse_i8:
 ; GFNIAVX:       # %bb.0:
@@ -134,14 +135,14 @@ define i16 @test_bitreverse_i16(i16 %a) nounwind {
 ; XOP-NEXT:    # kill: def $ax killed $ax killed $eax
 ; XOP-NEXT:    retq
 ;
-; GFNISSE-LABEL: test_bitreverse_i16:
-; GFNISSE:       # %bb.0:
-; GFNISSE-NEXT:    movd %edi, %xmm0
-; GFNISSE-NEXT:    gf2p8affineqb $0, {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
-; GFNISSE-NEXT:    movd %xmm0, %eax
-; GFNISSE-NEXT:    rolw $8, %ax
-; GFNISSE-NEXT:    # kill: def $ax killed $ax killed $eax
-; GFNISSE-NEXT:    retq
+; GFNIPURE-LABEL: test_bitreverse_i16:
+; GFNIPURE:       # %bb.0:
+; GFNIPURE-NEXT:    movd %edi, %xmm0
+; GFNIPURE-NEXT:    gf2p8affineqb $0, {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
+; GFNIPURE-NEXT:    movd %xmm0, %eax
+; GFNIPURE-NEXT:    rolw $8, %ax
+; GFNIPURE-NEXT:    # kill: def $ax killed $ax killed $eax
+; GFNIPURE-NEXT:    retq
 ;
 ; GFNIAVX-LABEL: test_bitreverse_i16:
 ; GFNIAVX:       # %bb.0:
@@ -207,13 +208,13 @@ define i32 @test_bitreverse_i32(i32 %a) nounwind {
 ; XOP-NEXT:    vmovd %xmm0, %eax
 ; XOP-NEXT:    retq
 ;
-; GFNISSE-LABEL: test_bitreverse_i32:
-; GFNISSE:       # %bb.0:
-; GFNISSE-NEXT:    movd %edi, %xmm0
-; GFNISSE-NEXT:    gf2p8affineqb $0, {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
-; GFNISSE-NEXT:    movd %xmm0, %eax
-; GFNISSE-NEXT:    bswapl %eax
-; GFNISSE-NEXT:    retq
+; GFNIPURE-LABEL: test_bitreverse_i32:
+; GFNIPURE:       # %bb.0:
+; GFNIPURE-NEXT:    movd %edi, %xmm0
+; GFNIPURE-NEXT:    gf2p8affineqb $0, {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
+; GFNIPURE-NEXT:    movd %xmm0, %eax
+; GFNIPURE-NEXT:    bswapl %eax
+; GFNIPURE-NEXT:    retq
 ;
 ; GFNIAVX-LABEL: test_bitreverse_i32:
 ; GFNIAVX:       # %bb.0:
@@ -282,13 +283,13 @@ define i64 @test_bitreverse_i64(i64 %a) nounwind {
 ; XOP-NEXT:    vmovq %xmm0, %rax
 ; XOP-NEXT:    retq
 ;
-; GFNISSE-LABEL: test_bitreverse_i64:
-; GFNISSE:       # %bb.0:
-; GFNISSE-NEXT:    movq %rdi, %xmm0
-; GFNISSE-NEXT:    gf2p8affineqb $0, {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
-; GFNISSE-NEXT:    movq %xmm0, %rax
-; GFNISSE-NEXT:    bswapq %rax
-; GFNISSE-NEXT:    retq
+; GFNIPURE-LABEL: test_bitreverse_i64:
+; GFNIPURE:       # %bb.0:
+; GFNIPURE-NEXT:    movq %rdi, %xmm0
+; GFNIPURE-NEXT:    gf2p8affineqb $0, {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
+; GFNIPURE-NEXT:    movq %xmm0, %rax
+; GFNIPURE-NEXT:    bswapq %rax
+; GFNIPURE-NEXT:    retq
 ;
 ; GFNIAVX-LABEL: test_bitreverse_i64:
 ; GFNIAVX:       # %bb.0:
@@ -386,10 +387,10 @@ define <16 x i8> @test_bitreverse_v16i8(<16 x i8> %a) nounwind {
 ; XOP-NEXT:    vpperm {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0, %xmm0, %xmm0
 ; XOP-NEXT:    retq
 ;
-; GFNISSE-LABEL: test_bitreverse_v16i8:
-; GFNISSE:       # %bb.0:
-; GFNISSE-NEXT:    gf2p8affineqb $0, {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
-; GFNISSE-NEXT:    retq
+; GFNIPURE-LABEL: test_bitreverse_v16i8:
+; GFNIPURE:       # %bb.0:
+; GFNIPURE-NEXT:    gf2p8affineqb $0, {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
+; GFNIPURE-NEXT:    retq
 ;
 ; GFNIAVX-LABEL: test_bitreverse_v16i8:
 ; GFNIAVX:       # %bb.0:
@@ -873,12 +874,12 @@ define <32 x i8> @test_bitreverse_v32i8(<32 x i8> %a) nounwind {
 ; XOPAVX2-NEXT:    vinserti128 $1, %xmm1, %ymm0, %ymm0
 ; XOPAVX2-NEXT:    retq
 ;
-; GFNISSE-LABEL: test_bitreverse_v32i8:
-; GFNISSE:       # %bb.0:
-; GFNISSE-NEXT:    movdqa {{.*#+}} xmm2 = [1,2,4,8,16,32,64,128,1,2,4,8,16,32,64,128]
-; GFNISSE-NEXT:    gf2p8affineqb $0, %xmm2, %xmm0
-; GFNISSE-NEXT:    gf2p8affineqb $0, %xmm2, %xmm1
-; GFNISSE-NEXT:    retq
+; GFNIPURE-LABEL: test_bitreverse_v32i8:
+; GFNIPURE:       # %bb.0:
+; GFNIPURE-NEXT:    movdqa {{.*#+}} xmm2 = [1,2,4,8,16,32,64,128,1,2,4,8,16,32,64,128]
+; GFNIPURE-NEXT:    gf2p8affineqb $0, %xmm2, %xmm0
+; GFNIPURE-NEXT:    gf2p8affineqb $0, %xmm2, %xmm1
+; GFNIPURE-NEXT:    retq
 ;
 ; GFNIAVX-LABEL: test_bitreverse_v32i8:
 ; GFNIAVX:       # %bb.0:
@@ -1724,14 +1725,14 @@ define <64 x i8> @test_bitreverse_v64i8(<64 x i8> %a) nounwind {
 ; XOPAVX2-NEXT:    vinserti128 $1, %xmm2, %ymm1, %ymm1
 ; XOPAVX2-NEXT:    retq
 ;
-; GFNISSE-LABEL: test_bitreverse_v64i8:
-; GFNISSE:       # %bb.0:
-; GFNISSE-NEXT:    movdqa {{.*#+}} xmm4 = [1,2,4,8,16,32,64,128,1,2,4,8,16,32,64,128]
-; GFNISSE-NEXT:    gf2p8affineqb $0, %xmm4, %xmm0
-; GFNISSE-NEXT:    gf2p8affineqb $0, %xmm4, %xmm1
-; GFNISSE-NEXT:    gf2p8affineqb $0, %xmm4, %xmm2
-; GFNISSE-NEXT:    gf2p8affineqb $0, %xmm4, %xmm3
-; GFNISSE-NEXT:    retq
+; GFNIPURE-LABEL: test_bitreverse_v64i8:
+; GFNIPURE:       # %bb.0:
+; GFNIPURE-NEXT:    movdqa {{.*#+}} xmm4 = [1,2,4,8,16,32,64,128,1,2,4,8,16,32,64,128]
+; GFNIPURE-NEXT:    gf2p8affineqb $0, %xmm4, %xmm0
+; GFNIPURE-NEXT:    gf2p8affineqb $0, %xmm4, %xmm1
+; GFNIPURE-NEXT:    gf2p8affineqb $0, %xmm4, %xmm2
+; GFNIPURE-NEXT:    gf2p8affineqb $0, %xmm4, %xmm3
+; GFNIPURE-NEXT:    retq
 ;
 ; GFNIAVX1-LABEL: test_bitreverse_v64i8:
 ; GFNIAVX1:       # %bb.0:
@@ -2849,6 +2850,11 @@ define <16 x i8> @fold_bitreverse_v16i8() nounwind {
 ; XOP-NEXT:    vmovaps {{.*#+}} xmm0 = [0,255,64,191,32,223,96,159,16,239,80,175,48,207,112,143]
 ; XOP-NEXT:    retq
 ;
+; GFNIPURE-LABEL: fold_bitreverse_v16i8:
+; GFNIPURE:       # %bb.0:
+; GFNIPURE-NEXT:    movaps {{.*#+}} xmm0 = [0,255,64,191,32,223,96,159,16,239,80,175,48,207,112,143]
+; GFNIPURE-NEXT:    retq
+;
 ; GFNISSE-LABEL: fold_bitreverse_v16i8:
 ; GFNISSE:       # %bb.0:
 ; GFNISSE-NEXT:    movaps {{.*#+}} xmm0 = [0,255,64,191,32,223,96,159,16,239,80,175,48,207,112,143]

Copy link

github-actions bot commented Jul 11, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@topperc
Copy link
Collaborator

topperc commented Jul 11, 2025

Please write a more descriptive title including [X86] and put the bug number in the description.

@TDecking TDecking changed the title Fix #148238 [X86] Ensure that bit reversals of byte vectors are properly lowered on pure GFNI targets Jul 11, 2025
@RKSimon RKSimon self-requested a review July 14, 2025 13:59
Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to use the update_llc_test_checks.py script to regenerate the vector-bitreverse.ll file

@@ -8,6 +8,7 @@
; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+avx512vl,+avx512bw | FileCheck %s --check-prefixes=ALL,AVX,AVX512,AVX512BW
; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+xop,+avx | FileCheck %s --check-prefixes=ALL,XOP,XOPAVX1
; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+xop,+avx2 | FileCheck %s --check-prefixes=ALL,XOP,XOPAVX2
; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+gfni | FileCheck %s --check-prefixes=ALL,GFNIPURE
; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+ssse3,+gfni | FileCheck %s --check-prefixes=ALL,GFNISSE
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change this (and the new line) to:

; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+gfni | FileCheck %s --check-prefixes=ALL,GFNISSE,,GFNISSE2
; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+ssse3,+gfni | FileCheck %s --check-prefixes=ALL,GFNISSE,GFNISSSE2

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the help.

Should the second line be turned into GFNISSSE3 instead of GFNISSSE2?

@RKSimon RKSimon requested a review from topperc July 14, 2025 14:02
; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+gfni | FileCheck %s --check-prefixes=ALL,GFNIPURE
; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+ssse3,+gfni | FileCheck %s --check-prefixes=ALL,GFNISSE
; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+gfni | FileCheck %s --check-prefixes=ALL,GFNISSE,GFNISSE2
; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+ssse3,+gfni | FileCheck %s --check-prefixes=ALL,GFNISSE,GFNISSSE2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GFNISSSE2 -> GFNISSSE3

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@TDecking
Copy link
Contributor Author

Sorry if I caused any inconvenience.

@RKSimon RKSimon merged commit 10b0dee into llvm:main Jul 18, 2025
8 of 9 checks passed
Copy link

@TDecking Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@TDecking TDecking deleted the bitrev branch July 18, 2025 19:12
mdfazlay pushed a commit to mdfazlay/llvm-project that referenced this pull request Jul 18, 2025
…on pure GFNI targets (llvm#148304)

Fixes llvm#148238.

When GFNI is present, custom bit reversal lowerings for scalar integers
become active. They work by swapping the bytes in the scalar value and
then reversing bits in a vector of bytes. However, the custom bit
reversal lowering for a vector of bytes is disabled if GFNI is present
in isolation, resulting messed up code.

---------

Co-authored-by: Simon Pilgrim <llvm-dev@redking.me.uk>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bit-reversal codegen broken on x86 systems with gfni but no avx
4 participants