[X86] Make bfloat a legal type wherever the equivalent i16 type is #241456
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-powerpc
@llvm/pr-subscribers-backend-x86
Author: Oscar Smith (oscardssmith)
<details>
<summary>Changes</summary>
As pointed out in https://github.com/llvm/llvm-project/pull/240632#discussion_r4534305598, bf16 vectors (unlike f16 vectors) were considered illegal even when the vector registers could support storing them. Now we register v8bf16 with SSE2, v16bf16 with AVX and v32bf16 with AVX512F, matching f16 vecs, and pick the register class with VLX like the other vector types.
The result is much better codegen, especially for code that is just doing calling convention or data movement.
---
Patch is 194.71 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/241456.diff
11 Files Affected:
````````````diff --git a/llvm/lib/CodeGen/SelectionDAG/LegalizeFloatTypes.cpp b/llvm/lib/CodeGen/SelectionDAG/LegalizeFloatTypes.cpp
index fe74297e9c5c1..f237dada9bddf 100644
--- a/llvm/lib/CodeGen/SelectionDAG/LegalizeFloatTypes.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeFloatTypes.cpp
@@ -2670,10 +2670,29 @@ SDValue DAGTypeLegalizer::SoftPromoteHalfRes_ConstantFP(SDNode *N) {
}
SDValue DAGTypeLegalizer::SoftPromoteHalfRes_EXTRACT_VECTOR_ELT(SDNode *N) {
- SDValue NewOp = BitConvertVectorToIntegerVector(N->getOperand(0));
+ SDValue Vec = N->getOperand(0);
+ SDValue Idx = N->getOperand(1);
+
+ // Bitcasting a split vector to a widened integer vector would cycle, as the
+ // widening is legalized by extracting these same elements.
+ EVT VecVT = Vec.getValueType();
+ auto *IdxC = dyn_cast<ConstantSDNode>(Idx);
+ if (IdxC && VecVT.isFixedLengthVector() &&
+ getTypeAction(VecVT) == TargetLowering::TypeSplitVector &&
+ getTypeAction(VecVT.changeVectorElementTypeToInteger()) ==
+ TargetLowering::TypeWidenVector) {
+ SDValue Lo, Hi;
+ GetSplitVector(Vec, Lo, Hi);
+ uint64_t IdxVal = IdxC->getZExtValue();
+ uint64_t LoElts = Lo.getValueType().getVectorNumElements();
+ Vec = IdxVal < LoElts ? Lo : Hi;
+ if (IdxVal >= LoElts)
+ Idx = DAG.getConstant(IdxVal - LoElts, SDLoc(N), Idx.getValueType());
+ }
+
+ SDValue NewOp = BitConvertVectorToIntegerVector(Vec);
return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N),
- NewOp.getValueType().getVectorElementType(), NewOp,
- N->getOperand(1));
+ NewOp.getValueType().getVectorElementType(), NewOp, Idx);
}
SDValue DAGTypeLegalizer::SoftPromoteHalfRes_FCOPYSIGN(SDNode *N) {
diff --git a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
index 4e56e25e9364d..718bd2f416bab 100644
--- a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
@@ -398,7 +398,20 @@ SDValue DAGTypeLegalizer::ScalarizeVecRes_PartialReduction(SDNode *N) {
// Make sure we're not trying an extending partial reduction in this path.
assert(InVT.getScalarType() == ResVT && "Extending operands unsupported");
- SDValue Mul = DAG.getNode(IsFP ? ISD::FMUL : ISD::MUL, DL, InVT, L, R);
+ // FMUL does not fold a splat of 1.0 already legalized to an integer splat.
+ auto IsLegalizedOneSplatFP = [&](SDValue V) {
+ SDValue BC = peekThroughBitcasts(V);
+ if (BC.getScalarValueSizeInBits() != ResVT.getSizeInBits())
+ return false;
+ ConstantSDNode *C = isConstOrConstSplat(BC, /*AllowUndefs=*/false,
+ /*AllowTruncation=*/true);
+ return C && C->getAPIntValue().trunc(ResVT.getSizeInBits()) ==
+ APFloat::getOne(ResVT.getFltSemantics()).bitcastToAPInt();
+ };
+
+ SDValue Mul = L;
+ if (!IsFP || !IsLegalizedOneSplatFP(R))
+ Mul = DAG.getNode(IsFP ? ISD::FMUL : ISD::MUL, DL, InVT, L, R);
SDValue In = DAG.getNode(IsFP ? ISD::VECREDUCE_FADD : ISD::VECREDUCE_ADD, DL,
ResVT, Mul);
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index 4d081b7ecbf43..ea4e9f38c619a 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -2490,17 +2490,15 @@ X86TargetLowering::X86TargetLowering(const X86TargetMachine &TM,
}
}
- // Keep v8bf16 legal with AVX even without native bf16 instructions so that
- // f32-to-bf16 conversions can be expanded in the vector domain.
- if (!Subtarget.useSoftFloat() && Subtarget.hasAVX()) {
- addRegisterClass(MVT::v8bf16, Subtarget.hasAVX512() ? &X86::VR128XRegClass
- : &X86::VR128RegClass);
- if (Subtarget.hasAVXNECONVERT() || Subtarget.hasBWI()) {
- addRegisterClass(MVT::v16bf16, Subtarget.hasAVX512()
- ? &X86::VR256XRegClass
- : &X86::VR256RegClass);
+ // bf16 vectors are legal wherever the equivalent i16 vector is.
+ if (!Subtarget.useSoftFloat() && Subtarget.hasSSE2()) {
+ addRegisterClass(MVT::v8bf16, Subtarget.hasVLX() ? &X86::VR128XRegClass
+ : &X86::VR128RegClass);
+ if (Subtarget.hasAVX())
+ addRegisterClass(MVT::v16bf16, Subtarget.hasVLX() ? &X86::VR256XRegClass
+ : &X86::VR256RegClass);
+ if (Subtarget.hasAVXNECONVERT() || Subtarget.hasBWI())
addLegalFPImmediate(APFloat::getZero(APFloat::BFloat()));
- }
// We set the type action of bf16 to TypeSoftPromoteHalf, but we don't
// provide the method to promote BUILD_VECTOR. Set the operation action
// Custom to do the customization later.
@@ -2527,8 +2525,7 @@ X86TargetLowering::X86TargetLowering(const X86TargetMachine &TM,
setOperationAction(ISD::FP_ROUND, MVT::v8bf16, Custom);
}
- if (!Subtarget.useSoftFloat() && Subtarget.hasBWI() &&
- Subtarget.useAVX512Regs()) {
+ if (!Subtarget.useSoftFloat() && Subtarget.useAVX512Regs()) {
addRegisterClass(MVT::v32bf16, &X86::VR512RegClass);
setF16Action(MVT::v32bf16, Expand);
setOperationAction(ISD::BUILD_VECTOR, MVT::v32bf16, Custom);
@@ -2948,10 +2945,11 @@ X86TargetLowering::getPreferredVectorAction(MVT VT) const {
!Subtarget.hasBWI())
return TypeSplitVector;
- // Since v8f16 is legal, widen anything over v4f16.
+ // Since v8f16 and v8bf16 are legal, widen anything over v4f16 and v4bf16.
if (!VT.isScalableVector() && VT.getVectorNumElements() != 1 &&
- VT.getVectorNumElements() <= 4 && !Subtarget.hasF16C() &&
- VT.getVectorElementType() == MVT::f16)
+ VT.getVectorNumElements() <= 4 &&
+ ((VT.getVectorElementType() == MVT::f16 && !Subtarget.hasF16C()) ||
+ (VT.getVectorElementType() == MVT::bf16 && !Subtarget.hasAVX())))
return TypeSplitVector;
if (!VT.isScalableVector() && VT.getVectorNumElements() != 1 &&
@@ -9491,7 +9489,7 @@ X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
if (VT.getVectorElementType() == MVT::i1 && Subtarget.hasAVX512())
return LowerBUILD_VECTORvXi1(Op, dl, DAG, Subtarget);
- if (VT.getVectorElementType() == MVT::bf16 && Subtarget.hasAVX())
+ if (VT.getVectorElementType() == MVT::bf16)
return LowerBUILD_VECTORvXbf16(Op, DAG, Subtarget);
if (SDValue VectorCst = materializeVectorConstant(Op, dl, DAG, Subtarget))
@@ -22926,8 +22924,9 @@ SDValue X86TargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
Subtarget.hasAVXNECONVERT()))
return Op;
// Without native conversions, round in vector registers instead of
- // unrolling to libcalls.
- if (!IsStrict && SVT.getScalarType() == MVT::f32)
+ // unrolling to libcalls. Vectors need the variable blends of SSE4.1.
+ if (!IsStrict && SVT.getScalarType() == MVT::f32 &&
+ (!VT.isVector() || Subtarget.hasSSE41()))
return expandFP_ROUND(Op.getNode(), DAG);
return SDValue();
}
diff --git a/llvm/lib/Target/X86/X86InstrSSE.td b/llvm/lib/Target/X86/X86InstrSSE.td
index 3edf9680bfd6e..a5b831bd39ba4 100644
--- a/llvm/lib/Target/X86/X86InstrSSE.td
+++ b/llvm/lib/Target/X86/X86InstrSSE.td
@@ -649,12 +649,20 @@ let Predicates = [UseSSE1] in {
let Predicates = [UseSSE2] in {
def : Pat<(alignedloadv8f16 addr:$src),
(MOVAPSrm addr:$src)>;
+ def : Pat<(alignedloadv8bf16 addr:$src),
+ (MOVAPSrm addr:$src)>;
def : Pat<(loadv8f16 addr:$src),
(MOVUPSrm addr:$src)>;
+ def : Pat<(loadv8bf16 addr:$src),
+ (MOVUPSrm addr:$src)>;
def : Pat<(alignedstore128 (v8f16 VR128:$src), addr:$dst),
(MOVAPSmr addr:$dst, VR128:$src)>;
+ def : Pat<(alignedstore128 (v8bf16 VR128:$src), addr:$dst),
+ (MOVAPSmr addr:$dst, VR128:$src)>;
def : Pat<(store (v8f16 VR128:$src), addr:$dst),
(MOVUPSmr addr:$dst, VR128:$src)>;
+ def : Pat<(store (v8bf16 VR128:$src), addr:$dst),
+ (MOVUPSmr addr:$dst, VR128:$src)>;
}
//===----------------------------------------------------------------------===//
diff --git a/llvm/test/CodeGen/X86/atomic-load-store.ll b/llvm/test/CodeGen/X86/atomic-load-store.ll
index 8b13c3e7e666a..b12499294c279 100644
--- a/llvm/test/CodeGen/X86/atomic-load-store.ll
+++ b/llvm/test/CodeGen/X86/atomic-load-store.ll
@@ -880,39 +880,26 @@ define void @store_atomic_vec2_bfloat(ptr %x, <2 x bfloat> %v) nounwind {
; CHECK-SSE2-O0-LABEL: store_atomic_vec2_bfloat:
; CHECK-SSE2-O0: # %bb.0:
; CHECK-SSE2-O0-NEXT: movaps %xmm0, %xmm1
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm1
-; CHECK-SSE2-O0-NEXT: pextrw $1, %xmm0, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm0
+; CHECK-SSE2-O0-NEXT: movaps %xmm1, %xmm0
; CHECK-SSE2-O0-NEXT: pslld $16, %xmm0
+; CHECK-SSE2-O0-NEXT: psrld $16, %xmm1
+; CHECK-SSE2-O0-NEXT: pslld $16, %xmm1
; CHECK-SSE2-O0-NEXT: movd %xmm1, %eax
; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE2-O0-NEXT: movw %ax, -{{[0-9]+}}(%rsp)
-; CHECK-SSE2-O0-NEXT: movd %xmm0, %eax
-; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE2-O0-NEXT: movw %ax, -{{[0-9]+}}(%rsp)
-; CHECK-SSE2-O0-NEXT: movl -{{[0-9]+}}(%rsp), %eax
-; CHECK-SSE2-O0-NEXT: movl %eax, (%rdi)
+; CHECK-SSE2-O0-NEXT: psrld $16, %xmm0
+; CHECK-SSE2-O0-NEXT: pinsrw $1, %eax, %xmm0
+; CHECK-SSE2-O0-NEXT: movd %xmm0, (%rdi)
; CHECK-SSE2-O0-NEXT: retq
;
; CHECK-SSE4-O0-LABEL: store_atomic_vec2_bfloat:
; CHECK-SSE4-O0: # %bb.0:
-; CHECK-SSE4-O0-NEXT: movaps %xmm0, %xmm1
; CHECK-SSE4-O0-NEXT: xorps %xmm2, %xmm2
-; CHECK-SSE4-O0-NEXT: movaps %xmm1, %xmm0
-; CHECK-SSE4-O0-NEXT: pblendw {{.*#+}} xmm0 = xmm2[0],xmm0[1],xmm2[2],xmm0[3],xmm2[4],xmm0[5],xmm2[6],xmm0[7]
-; CHECK-SSE4-O0-NEXT: pslld $16, %xmm1
-; CHECK-SSE4-O0-NEXT: movd %xmm1, %eax
-; CHECK-SSE4-O0-NEXT: shrl $16, %eax
-; CHECK-SSE4-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE4-O0-NEXT: movw %ax, -{{[0-9]+}}(%rsp)
-; CHECK-SSE4-O0-NEXT: movd %xmm0, %eax
-; CHECK-SSE4-O0-NEXT: shrl $16, %eax
-; CHECK-SSE4-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE4-O0-NEXT: movw %ax, -{{[0-9]+}}(%rsp)
-; CHECK-SSE4-O0-NEXT: movl -{{[0-9]+}}(%rsp), %eax
-; CHECK-SSE4-O0-NEXT: movl %eax, (%rdi)
+; CHECK-SSE4-O0-NEXT: movaps %xmm0, %xmm1
+; CHECK-SSE4-O0-NEXT: pblendw {{.*#+}} xmm1 = xmm2[0],xmm1[1],xmm2[2],xmm1[3],xmm2[4],xmm1[5],xmm2[6],xmm1[7]
+; CHECK-SSE4-O0-NEXT: pslld $16, %xmm0
+; CHECK-SSE4-O0-NEXT: psrld $16, %xmm0
+; CHECK-SSE4-O0-NEXT: pblendw {{.*#+}} xmm0 = xmm0[0],xmm1[1],xmm0[2,3,4,5,6,7]
+; CHECK-SSE4-O0-NEXT: movd %xmm0, (%rdi)
; CHECK-SSE4-O0-NEXT: retq
;
; CHECK-AVX-O0-LABEL: store_atomic_vec2_bfloat:
@@ -984,70 +971,51 @@ define void @store_atomic_vec4_bfloat(ptr %x, <4 x bfloat> %v) nounwind {
;
; CHECK-SSE2-O0-LABEL: store_atomic_vec4_bfloat:
; CHECK-SSE2-O0: # %bb.0:
-; CHECK-SSE2-O0-NEXT: movaps %xmm0, %xmm2
-; CHECK-SSE2-O0-NEXT: movaps %xmm2, %xmm3
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm3
-; CHECK-SSE2-O0-NEXT: pextrw $3, %xmm2, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm0
+; CHECK-SSE2-O0-NEXT: movaps %xmm0, %xmm1
+; CHECK-SSE2-O0-NEXT: movaps %xmm1, %xmm0
; CHECK-SSE2-O0-NEXT: pslld $16, %xmm0
-; CHECK-SSE2-O0-NEXT: pextrw $2, %xmm2, %eax
+; CHECK-SSE2-O0-NEXT: movaps %xmm1, %xmm2
+; CHECK-SSE2-O0-NEXT: shufps {{.*#+}} xmm2 = xmm2[1,1,1,1]
+; CHECK-SSE2-O0-NEXT: pslld $16, %xmm2
+; CHECK-SSE2-O0-NEXT: pextrw $3, %xmm1, %eax
+; CHECK-SSE2-O0-NEXT: movd %eax, %xmm3
+; CHECK-SSE2-O0-NEXT: pslld $16, %xmm3
+; CHECK-SSE2-O0-NEXT: pextrw $1, %xmm1, %eax
; CHECK-SSE2-O0-NEXT: movd %eax, %xmm1
; CHECK-SSE2-O0-NEXT: pslld $16, %xmm1
-; CHECK-SSE2-O0-NEXT: pextrw $1, %xmm2, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm2
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm2
; CHECK-SSE2-O0-NEXT: movd %xmm3, %eax
; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE2-O0-NEXT: movw %ax, -{{[0-9]+}}(%rsp)
-; CHECK-SSE2-O0-NEXT: movd %xmm2, %eax
-; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE2-O0-NEXT: movw %ax, -{{[0-9]+}}(%rsp)
-; CHECK-SSE2-O0-NEXT: movd %xmm1, %eax
-; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE2-O0-NEXT: movw %ax, -{{[0-9]+}}(%rsp)
-; CHECK-SSE2-O0-NEXT: movd %xmm0, %eax
-; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE2-O0-NEXT: movw %ax, -{{[0-9]+}}(%rsp)
-; CHECK-SSE2-O0-NEXT: movq -{{[0-9]+}}(%rsp), %rax
-; CHECK-SSE2-O0-NEXT: movq %rax, (%rdi)
+; CHECK-SSE2-O0-NEXT: movd %xmm2, %ecx
+; CHECK-SSE2-O0-NEXT: shrl $16, %ecx
+; CHECK-SSE2-O0-NEXT: movd %xmm1, %edx
+; CHECK-SSE2-O0-NEXT: shrl $16, %edx
+; CHECK-SSE2-O0-NEXT: psrld $16, %xmm0
+; CHECK-SSE2-O0-NEXT: pinsrw $1, %edx, %xmm0
+; CHECK-SSE2-O0-NEXT: pinsrw $2, %ecx, %xmm0
+; CHECK-SSE2-O0-NEXT: pinsrw $3, %eax, %xmm0
+; CHECK-SSE2-O0-NEXT: movq %xmm0, (%rdi)
; CHECK-SSE2-O0-NEXT: retq
;
; CHECK-SSE4-O0-LABEL: store_atomic_vec4_bfloat:
; CHECK-SSE4-O0: # %bb.0:
; CHECK-SSE4-O0-NEXT: movaps %xmm0, %xmm1
; CHECK-SSE4-O0-NEXT: xorps %xmm0, %xmm0
-; CHECK-SSE4-O0-NEXT: movaps %xmm1, %xmm2
-; CHECK-SSE4-O0-NEXT: pblendw {{.*#+}} xmm2 = xmm0[0],xmm2[1],xmm0[2],xmm2[3],xmm0[4],xmm2[5],xmm0[6],xmm2[7]
; CHECK-SSE4-O0-NEXT: movaps %xmm1, %xmm3
-; CHECK-SSE4-O0-NEXT: pslld $16, %xmm3
-; CHECK-SSE4-O0-NEXT: pextrw $3, %xmm1, %eax
-; CHECK-SSE4-O0-NEXT: movd %eax, %xmm0
+; CHECK-SSE4-O0-NEXT: pblendw {{.*#+}} xmm3 = xmm0[0],xmm3[1],xmm0[2],xmm3[3],xmm0[4],xmm3[5],xmm0[6],xmm3[7]
+; CHECK-SSE4-O0-NEXT: movaps %xmm1, %xmm0
; CHECK-SSE4-O0-NEXT: pslld $16, %xmm0
-; CHECK-SSE4-O0-NEXT: pextrw $2, %xmm1, %eax
+; CHECK-SSE4-O0-NEXT: movshdup {{.*#+}} xmm2 = xmm1[1,1,3,3]
+; CHECK-SSE4-O0-NEXT: pslld $16, %xmm2
+; CHECK-SSE4-O0-NEXT: pextrw $3, %xmm1, %eax
; CHECK-SSE4-O0-NEXT: movd %eax, %xmm1
; CHECK-SSE4-O0-NEXT: pslld $16, %xmm1
-; CHECK-SSE4-O0-NEXT: movd %xmm3, %eax
-; CHECK-SSE4-O0-NEXT: shrl $16, %eax
-; CHECK-SSE4-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE4-O0-NEXT: movw %ax, -{{[0-9]+}}(%rsp)
-; CHECK-SSE4-O0-NEXT: movd %xmm2, %eax
-; CHECK-SSE4-O0-NEXT: shrl $16, %eax
-; CHECK-SSE4-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE4-O0-NEXT: movw %ax, -{{[0-9]+}}(%rsp)
-; CHECK-SSE4-O0-NEXT: movd %xmm1, %eax
-; CHECK-SSE4-O0-NEXT: shrl $16, %eax
-; CHECK-SSE4-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE4-O0-NEXT: movw %ax, -{{[0-9]+}}(%rsp)
-; CHECK-SSE4-O0-NEXT: movd %xmm0, %eax
-; CHECK-SSE4-O0-NEXT: shrl $16, %eax
-; CHECK-SSE4-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE4-O0-NEXT: movw %ax, -{{[0-9]+}}(%rsp)
-; CHECK-SSE4-O0-NEXT: movq -{{[0-9]+}}(%rsp), %rax
-; CHECK-SSE4-O0-NEXT: movq %rax, (%rdi)
+; CHECK-SSE4-O0-NEXT: psrld $16, %xmm0
+; CHECK-SSE4-O0-NEXT: pblendw {{.*#+}} xmm0 = xmm0[0],xmm3[1],xmm0[2,3,4,5,6,7]
+; CHECK-SSE4-O0-NEXT: pmovzxwd {{.*#+}} xmm2 = xmm2[0],zero,xmm2[1],zero,xmm2[2],zero,xmm2[3],zero
+; CHECK-SSE4-O0-NEXT: pblendw {{.*#+}} xmm0 = xmm0[0,1],xmm2[2],xmm0[3,4,5,6,7]
+; CHECK-SSE4-O0-NEXT: movsldup {{.*#+}} xmm1 = xmm1[0,0,2,2]
+; CHECK-SSE4-O0-NEXT: pblendw {{.*#+}} xmm0 = xmm0[0,1,2],xmm1[3],xmm0[4,5,6,7]
+; CHECK-SSE4-O0-NEXT: movq %xmm0, (%rdi)
; CHECK-SSE4-O0-NEXT: retq
;
; CHECK-AVX-O0-LABEL: store_atomic_vec4_bfloat:
@@ -2555,276 +2523,31 @@ define void @store_atomic_vec16_bfloat_unaligned(ptr %x, <16 x bfloat> %v) nounw
; CHECK-AVX-O3-NEXT: addq $40, %rsp
; CHECK-AVX-O3-NEXT: retq
;
-; CHECK-SSE2-O0-LABEL: store_atomic_vec16_bfloat_unaligned:
-; CHECK-SSE2-O0: # %bb.0:
-; CHECK-SSE2-O0-NEXT: subq $40, %rsp
-; CHECK-SSE2-O0-NEXT: movaps %xmm1, %xmm6
-; CHECK-SSE2-O0-NEXT: movaps %xmm0, %xmm14
-; CHECK-SSE2-O0-NEXT: movq %rdi, %rsi
-; CHECK-SSE2-O0-NEXT: xorps %xmm11, %xmm11
-; CHECK-SSE2-O0-NEXT: movaps %xmm11, %xmm3
-; CHECK-SSE2-O0-NEXT: punpckhwd {{.*#+}} xmm3 = xmm3[4],xmm6[4],xmm3[5],xmm6[5],xmm3[6],xmm6[6],xmm3[7],xmm6[7]
-; CHECK-SSE2-O0-NEXT: punpckhwd {{.*#+}} xmm11 = xmm11[4],xmm14[4],xmm11[5],xmm14[5],xmm11[6],xmm14[6],xmm11[7],xmm14[7]
-; CHECK-SSE2-O0-NEXT: movaps %xmm6, %xmm7
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm7
-; CHECK-SSE2-O0-NEXT: movaps %xmm14, %xmm15
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm15
-; CHECK-SSE2-O0-NEXT: pextrw $7, %xmm6, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm0
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm0
-; CHECK-SSE2-O0-NEXT: pextrw $6, %xmm6, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm1
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm1
-; CHECK-SSE2-O0-NEXT: pextrw $5, %xmm6, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm2
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm2
-; CHECK-SSE2-O0-NEXT: pextrw $3, %xmm6, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm4
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm4
-; CHECK-SSE2-O0-NEXT: pextrw $2, %xmm6, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm5
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm5
-; CHECK-SSE2-O0-NEXT: pextrw $1, %xmm6, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm6
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm6
-; CHECK-SSE2-O0-NEXT: pextrw $7, %xmm14, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm8
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm8
-; CHECK-SSE2-O0-NEXT: pextrw $6, %xmm14, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm9
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm9
-; CHECK-SSE2-O0-NEXT: pextrw $5, %xmm14, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm10
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm10
-; CHECK-SSE2-O0-NEXT: pextrw $3, %xmm14, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm12
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm12
-; CHECK-SSE2-O0-NEXT: pextrw $2, %xmm14, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm13
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm13
-; CHECK-SSE2-O0-NEXT: pextrw $1, %xmm14, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm14
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm14
-; CHECK-SSE2-O0-NEXT: movd %xmm15, %eax
-; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE2-O0-NEXT: movw %ax, (%rsp)
-; CHECK-SSE2-O0-NEXT: movd %xmm14, %eax
-; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE2-O0-NEXT: movw %ax, {{[0-9]+}}(%rsp)
-; CHECK-SSE2-O0-NEXT: movd %xmm13, %eax
-; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE2-O0-NEXT: movw %ax, {{[0-9]+}}(%rsp)
-; CHECK-SSE2-O0-NEXT: movd %xmm12, %eax
-; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE2-O0-NEXT: movw %ax, {{[0-9]+}}(%rsp)
-; CHECK-SSE2-O0-NEXT: movd %xmm11, %eax
-; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE2-O0-NEXT: movw %ax, {{[0-9]+}}(%rsp)
-; CHECK-SSE2-O0-NEXT: movd %xmm10, %eax
-; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE2-O0-NEXT: movw %ax, {{[0-9]+}}(%rsp)
-; CHECK-SSE2-O0-NEXT: movd %xmm9, %eax
-; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE2-O0-NEXT: movw %ax, {{[0-9]+}}(%rsp)
-; CHECK-SSE2-O0-NEXT: movd %xmm8, %eax
-; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE2-O0-NEXT: movw %ax, {{[0-9]+}}(%rsp)
-; CHECK-SSE2-O0-NEXT: movd %xmm7, %eax
-; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; ...
[truncated]
</details>
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-selectiondag
Author: Oscar Smith (oscardssmith)
<details>
<summary>Changes</summary>
As pointed out in https://github.com/llvm/llvm-project/pull/240632#discussion_r4534305598, bf16 vectors (unlike f16 vectors) were considered illegal even when the vector registers could support storing them. Now we register v8bf16 with SSE2, v16bf16 with AVX and v32bf16 with AVX512F, matching f16 vecs, and pick the register class with VLX like the other vector types.
The result is much better codegen, especially for code that is just doing calling convention or data movement.
---
Patch is 194.71 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/241456.diff
11 Files Affected:
````````````diff --git a/llvm/lib/CodeGen/SelectionDAG/LegalizeFloatTypes.cpp b/llvm/lib/CodeGen/SelectionDAG/LegalizeFloatTypes.cpp
index fe74297e9c5c1..f237dada9bddf 100644
--- a/llvm/lib/CodeGen/SelectionDAG/LegalizeFloatTypes.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeFloatTypes.cpp
@@ -2670,10 +2670,29 @@ SDValue DAGTypeLegalizer::SoftPromoteHalfRes_ConstantFP(SDNode *N) {
}
SDValue DAGTypeLegalizer::SoftPromoteHalfRes_EXTRACT_VECTOR_ELT(SDNode *N) {
- SDValue NewOp = BitConvertVectorToIntegerVector(N->getOperand(0));
+ SDValue Vec = N->getOperand(0);
+ SDValue Idx = N->getOperand(1);
+
+ // Bitcasting a split vector to a widened integer vector would cycle, as the
+ // widening is legalized by extracting these same elements.
+ EVT VecVT = Vec.getValueType();
+ auto *IdxC = dyn_cast<ConstantSDNode>(Idx);
+ if (IdxC && VecVT.isFixedLengthVector() &&
+ getTypeAction(VecVT) == TargetLowering::TypeSplitVector &&
+ getTypeAction(VecVT.changeVectorElementTypeToInteger()) ==
+ TargetLowering::TypeWidenVector) {
+ SDValue Lo, Hi;
+ GetSplitVector(Vec, Lo, Hi);
+ uint64_t IdxVal = IdxC->getZExtValue();
+ uint64_t LoElts = Lo.getValueType().getVectorNumElements();
+ Vec = IdxVal < LoElts ? Lo : Hi;
+ if (IdxVal >= LoElts)
+ Idx = DAG.getConstant(IdxVal - LoElts, SDLoc(N), Idx.getValueType());
+ }
+
+ SDValue NewOp = BitConvertVectorToIntegerVector(Vec);
return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N),
- NewOp.getValueType().getVectorElementType(), NewOp,
- N->getOperand(1));
+ NewOp.getValueType().getVectorElementType(), NewOp, Idx);
}
SDValue DAGTypeLegalizer::SoftPromoteHalfRes_FCOPYSIGN(SDNode *N) {
diff --git a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
index 4e56e25e9364d..718bd2f416bab 100644
--- a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
@@ -398,7 +398,20 @@ SDValue DAGTypeLegalizer::ScalarizeVecRes_PartialReduction(SDNode *N) {
// Make sure we're not trying an extending partial reduction in this path.
assert(InVT.getScalarType() == ResVT && "Extending operands unsupported");
- SDValue Mul = DAG.getNode(IsFP ? ISD::FMUL : ISD::MUL, DL, InVT, L, R);
+ // FMUL does not fold a splat of 1.0 already legalized to an integer splat.
+ auto IsLegalizedOneSplatFP = [&](SDValue V) {
+ SDValue BC = peekThroughBitcasts(V);
+ if (BC.getScalarValueSizeInBits() != ResVT.getSizeInBits())
+ return false;
+ ConstantSDNode *C = isConstOrConstSplat(BC, /*AllowUndefs=*/false,
+ /*AllowTruncation=*/true);
+ return C && C->getAPIntValue().trunc(ResVT.getSizeInBits()) ==
+ APFloat::getOne(ResVT.getFltSemantics()).bitcastToAPInt();
+ };
+
+ SDValue Mul = L;
+ if (!IsFP || !IsLegalizedOneSplatFP(R))
+ Mul = DAG.getNode(IsFP ? ISD::FMUL : ISD::MUL, DL, InVT, L, R);
SDValue In = DAG.getNode(IsFP ? ISD::VECREDUCE_FADD : ISD::VECREDUCE_ADD, DL,
ResVT, Mul);
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index 4d081b7ecbf43..ea4e9f38c619a 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -2490,17 +2490,15 @@ X86TargetLowering::X86TargetLowering(const X86TargetMachine &TM,
}
}
- // Keep v8bf16 legal with AVX even without native bf16 instructions so that
- // f32-to-bf16 conversions can be expanded in the vector domain.
- if (!Subtarget.useSoftFloat() && Subtarget.hasAVX()) {
- addRegisterClass(MVT::v8bf16, Subtarget.hasAVX512() ? &X86::VR128XRegClass
- : &X86::VR128RegClass);
- if (Subtarget.hasAVXNECONVERT() || Subtarget.hasBWI()) {
- addRegisterClass(MVT::v16bf16, Subtarget.hasAVX512()
- ? &X86::VR256XRegClass
- : &X86::VR256RegClass);
+ // bf16 vectors are legal wherever the equivalent i16 vector is.
+ if (!Subtarget.useSoftFloat() && Subtarget.hasSSE2()) {
+ addRegisterClass(MVT::v8bf16, Subtarget.hasVLX() ? &X86::VR128XRegClass
+ : &X86::VR128RegClass);
+ if (Subtarget.hasAVX())
+ addRegisterClass(MVT::v16bf16, Subtarget.hasVLX() ? &X86::VR256XRegClass
+ : &X86::VR256RegClass);
+ if (Subtarget.hasAVXNECONVERT() || Subtarget.hasBWI())
addLegalFPImmediate(APFloat::getZero(APFloat::BFloat()));
- }
// We set the type action of bf16 to TypeSoftPromoteHalf, but we don't
// provide the method to promote BUILD_VECTOR. Set the operation action
// Custom to do the customization later.
@@ -2527,8 +2525,7 @@ X86TargetLowering::X86TargetLowering(const X86TargetMachine &TM,
setOperationAction(ISD::FP_ROUND, MVT::v8bf16, Custom);
}
- if (!Subtarget.useSoftFloat() && Subtarget.hasBWI() &&
- Subtarget.useAVX512Regs()) {
+ if (!Subtarget.useSoftFloat() && Subtarget.useAVX512Regs()) {
addRegisterClass(MVT::v32bf16, &X86::VR512RegClass);
setF16Action(MVT::v32bf16, Expand);
setOperationAction(ISD::BUILD_VECTOR, MVT::v32bf16, Custom);
@@ -2948,10 +2945,11 @@ X86TargetLowering::getPreferredVectorAction(MVT VT) const {
!Subtarget.hasBWI())
return TypeSplitVector;
- // Since v8f16 is legal, widen anything over v4f16.
+ // Since v8f16 and v8bf16 are legal, widen anything over v4f16 and v4bf16.
if (!VT.isScalableVector() && VT.getVectorNumElements() != 1 &&
- VT.getVectorNumElements() <= 4 && !Subtarget.hasF16C() &&
- VT.getVectorElementType() == MVT::f16)
+ VT.getVectorNumElements() <= 4 &&
+ ((VT.getVectorElementType() == MVT::f16 && !Subtarget.hasF16C()) ||
+ (VT.getVectorElementType() == MVT::bf16 && !Subtarget.hasAVX())))
return TypeSplitVector;
if (!VT.isScalableVector() && VT.getVectorNumElements() != 1 &&
@@ -9491,7 +9489,7 @@ X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
if (VT.getVectorElementType() == MVT::i1 && Subtarget.hasAVX512())
return LowerBUILD_VECTORvXi1(Op, dl, DAG, Subtarget);
- if (VT.getVectorElementType() == MVT::bf16 && Subtarget.hasAVX())
+ if (VT.getVectorElementType() == MVT::bf16)
return LowerBUILD_VECTORvXbf16(Op, DAG, Subtarget);
if (SDValue VectorCst = materializeVectorConstant(Op, dl, DAG, Subtarget))
@@ -22926,8 +22924,9 @@ SDValue X86TargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
Subtarget.hasAVXNECONVERT()))
return Op;
// Without native conversions, round in vector registers instead of
- // unrolling to libcalls.
- if (!IsStrict && SVT.getScalarType() == MVT::f32)
+ // unrolling to libcalls. Vectors need the variable blends of SSE4.1.
+ if (!IsStrict && SVT.getScalarType() == MVT::f32 &&
+ (!VT.isVector() || Subtarget.hasSSE41()))
return expandFP_ROUND(Op.getNode(), DAG);
return SDValue();
}
diff --git a/llvm/lib/Target/X86/X86InstrSSE.td b/llvm/lib/Target/X86/X86InstrSSE.td
index 3edf9680bfd6e..a5b831bd39ba4 100644
--- a/llvm/lib/Target/X86/X86InstrSSE.td
+++ b/llvm/lib/Target/X86/X86InstrSSE.td
@@ -649,12 +649,20 @@ let Predicates = [UseSSE1] in {
let Predicates = [UseSSE2] in {
def : Pat<(alignedloadv8f16 addr:$src),
(MOVAPSrm addr:$src)>;
+ def : Pat<(alignedloadv8bf16 addr:$src),
+ (MOVAPSrm addr:$src)>;
def : Pat<(loadv8f16 addr:$src),
(MOVUPSrm addr:$src)>;
+ def : Pat<(loadv8bf16 addr:$src),
+ (MOVUPSrm addr:$src)>;
def : Pat<(alignedstore128 (v8f16 VR128:$src), addr:$dst),
(MOVAPSmr addr:$dst, VR128:$src)>;
+ def : Pat<(alignedstore128 (v8bf16 VR128:$src), addr:$dst),
+ (MOVAPSmr addr:$dst, VR128:$src)>;
def : Pat<(store (v8f16 VR128:$src), addr:$dst),
(MOVUPSmr addr:$dst, VR128:$src)>;
+ def : Pat<(store (v8bf16 VR128:$src), addr:$dst),
+ (MOVUPSmr addr:$dst, VR128:$src)>;
}
//===----------------------------------------------------------------------===//
diff --git a/llvm/test/CodeGen/X86/atomic-load-store.ll b/llvm/test/CodeGen/X86/atomic-load-store.ll
index 8b13c3e7e666a..b12499294c279 100644
--- a/llvm/test/CodeGen/X86/atomic-load-store.ll
+++ b/llvm/test/CodeGen/X86/atomic-load-store.ll
@@ -880,39 +880,26 @@ define void @store_atomic_vec2_bfloat(ptr %x, <2 x bfloat> %v) nounwind {
; CHECK-SSE2-O0-LABEL: store_atomic_vec2_bfloat:
; CHECK-SSE2-O0: # %bb.0:
; CHECK-SSE2-O0-NEXT: movaps %xmm0, %xmm1
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm1
-; CHECK-SSE2-O0-NEXT: pextrw $1, %xmm0, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm0
+; CHECK-SSE2-O0-NEXT: movaps %xmm1, %xmm0
; CHECK-SSE2-O0-NEXT: pslld $16, %xmm0
+; CHECK-SSE2-O0-NEXT: psrld $16, %xmm1
+; CHECK-SSE2-O0-NEXT: pslld $16, %xmm1
; CHECK-SSE2-O0-NEXT: movd %xmm1, %eax
; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE2-O0-NEXT: movw %ax, -{{[0-9]+}}(%rsp)
-; CHECK-SSE2-O0-NEXT: movd %xmm0, %eax
-; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE2-O0-NEXT: movw %ax, -{{[0-9]+}}(%rsp)
-; CHECK-SSE2-O0-NEXT: movl -{{[0-9]+}}(%rsp), %eax
-; CHECK-SSE2-O0-NEXT: movl %eax, (%rdi)
+; CHECK-SSE2-O0-NEXT: psrld $16, %xmm0
+; CHECK-SSE2-O0-NEXT: pinsrw $1, %eax, %xmm0
+; CHECK-SSE2-O0-NEXT: movd %xmm0, (%rdi)
; CHECK-SSE2-O0-NEXT: retq
;
; CHECK-SSE4-O0-LABEL: store_atomic_vec2_bfloat:
; CHECK-SSE4-O0: # %bb.0:
-; CHECK-SSE4-O0-NEXT: movaps %xmm0, %xmm1
; CHECK-SSE4-O0-NEXT: xorps %xmm2, %xmm2
-; CHECK-SSE4-O0-NEXT: movaps %xmm1, %xmm0
-; CHECK-SSE4-O0-NEXT: pblendw {{.*#+}} xmm0 = xmm2[0],xmm0[1],xmm2[2],xmm0[3],xmm2[4],xmm0[5],xmm2[6],xmm0[7]
-; CHECK-SSE4-O0-NEXT: pslld $16, %xmm1
-; CHECK-SSE4-O0-NEXT: movd %xmm1, %eax
-; CHECK-SSE4-O0-NEXT: shrl $16, %eax
-; CHECK-SSE4-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE4-O0-NEXT: movw %ax, -{{[0-9]+}}(%rsp)
-; CHECK-SSE4-O0-NEXT: movd %xmm0, %eax
-; CHECK-SSE4-O0-NEXT: shrl $16, %eax
-; CHECK-SSE4-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE4-O0-NEXT: movw %ax, -{{[0-9]+}}(%rsp)
-; CHECK-SSE4-O0-NEXT: movl -{{[0-9]+}}(%rsp), %eax
-; CHECK-SSE4-O0-NEXT: movl %eax, (%rdi)
+; CHECK-SSE4-O0-NEXT: movaps %xmm0, %xmm1
+; CHECK-SSE4-O0-NEXT: pblendw {{.*#+}} xmm1 = xmm2[0],xmm1[1],xmm2[2],xmm1[3],xmm2[4],xmm1[5],xmm2[6],xmm1[7]
+; CHECK-SSE4-O0-NEXT: pslld $16, %xmm0
+; CHECK-SSE4-O0-NEXT: psrld $16, %xmm0
+; CHECK-SSE4-O0-NEXT: pblendw {{.*#+}} xmm0 = xmm0[0],xmm1[1],xmm0[2,3,4,5,6,7]
+; CHECK-SSE4-O0-NEXT: movd %xmm0, (%rdi)
; CHECK-SSE4-O0-NEXT: retq
;
; CHECK-AVX-O0-LABEL: store_atomic_vec2_bfloat:
@@ -984,70 +971,51 @@ define void @store_atomic_vec4_bfloat(ptr %x, <4 x bfloat> %v) nounwind {
;
; CHECK-SSE2-O0-LABEL: store_atomic_vec4_bfloat:
; CHECK-SSE2-O0: # %bb.0:
-; CHECK-SSE2-O0-NEXT: movaps %xmm0, %xmm2
-; CHECK-SSE2-O0-NEXT: movaps %xmm2, %xmm3
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm3
-; CHECK-SSE2-O0-NEXT: pextrw $3, %xmm2, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm0
+; CHECK-SSE2-O0-NEXT: movaps %xmm0, %xmm1
+; CHECK-SSE2-O0-NEXT: movaps %xmm1, %xmm0
; CHECK-SSE2-O0-NEXT: pslld $16, %xmm0
-; CHECK-SSE2-O0-NEXT: pextrw $2, %xmm2, %eax
+; CHECK-SSE2-O0-NEXT: movaps %xmm1, %xmm2
+; CHECK-SSE2-O0-NEXT: shufps {{.*#+}} xmm2 = xmm2[1,1,1,1]
+; CHECK-SSE2-O0-NEXT: pslld $16, %xmm2
+; CHECK-SSE2-O0-NEXT: pextrw $3, %xmm1, %eax
+; CHECK-SSE2-O0-NEXT: movd %eax, %xmm3
+; CHECK-SSE2-O0-NEXT: pslld $16, %xmm3
+; CHECK-SSE2-O0-NEXT: pextrw $1, %xmm1, %eax
; CHECK-SSE2-O0-NEXT: movd %eax, %xmm1
; CHECK-SSE2-O0-NEXT: pslld $16, %xmm1
-; CHECK-SSE2-O0-NEXT: pextrw $1, %xmm2, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm2
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm2
; CHECK-SSE2-O0-NEXT: movd %xmm3, %eax
; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE2-O0-NEXT: movw %ax, -{{[0-9]+}}(%rsp)
-; CHECK-SSE2-O0-NEXT: movd %xmm2, %eax
-; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE2-O0-NEXT: movw %ax, -{{[0-9]+}}(%rsp)
-; CHECK-SSE2-O0-NEXT: movd %xmm1, %eax
-; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE2-O0-NEXT: movw %ax, -{{[0-9]+}}(%rsp)
-; CHECK-SSE2-O0-NEXT: movd %xmm0, %eax
-; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE2-O0-NEXT: movw %ax, -{{[0-9]+}}(%rsp)
-; CHECK-SSE2-O0-NEXT: movq -{{[0-9]+}}(%rsp), %rax
-; CHECK-SSE2-O0-NEXT: movq %rax, (%rdi)
+; CHECK-SSE2-O0-NEXT: movd %xmm2, %ecx
+; CHECK-SSE2-O0-NEXT: shrl $16, %ecx
+; CHECK-SSE2-O0-NEXT: movd %xmm1, %edx
+; CHECK-SSE2-O0-NEXT: shrl $16, %edx
+; CHECK-SSE2-O0-NEXT: psrld $16, %xmm0
+; CHECK-SSE2-O0-NEXT: pinsrw $1, %edx, %xmm0
+; CHECK-SSE2-O0-NEXT: pinsrw $2, %ecx, %xmm0
+; CHECK-SSE2-O0-NEXT: pinsrw $3, %eax, %xmm0
+; CHECK-SSE2-O0-NEXT: movq %xmm0, (%rdi)
; CHECK-SSE2-O0-NEXT: retq
;
; CHECK-SSE4-O0-LABEL: store_atomic_vec4_bfloat:
; CHECK-SSE4-O0: # %bb.0:
; CHECK-SSE4-O0-NEXT: movaps %xmm0, %xmm1
; CHECK-SSE4-O0-NEXT: xorps %xmm0, %xmm0
-; CHECK-SSE4-O0-NEXT: movaps %xmm1, %xmm2
-; CHECK-SSE4-O0-NEXT: pblendw {{.*#+}} xmm2 = xmm0[0],xmm2[1],xmm0[2],xmm2[3],xmm0[4],xmm2[5],xmm0[6],xmm2[7]
; CHECK-SSE4-O0-NEXT: movaps %xmm1, %xmm3
-; CHECK-SSE4-O0-NEXT: pslld $16, %xmm3
-; CHECK-SSE4-O0-NEXT: pextrw $3, %xmm1, %eax
-; CHECK-SSE4-O0-NEXT: movd %eax, %xmm0
+; CHECK-SSE4-O0-NEXT: pblendw {{.*#+}} xmm3 = xmm0[0],xmm3[1],xmm0[2],xmm3[3],xmm0[4],xmm3[5],xmm0[6],xmm3[7]
+; CHECK-SSE4-O0-NEXT: movaps %xmm1, %xmm0
; CHECK-SSE4-O0-NEXT: pslld $16, %xmm0
-; CHECK-SSE4-O0-NEXT: pextrw $2, %xmm1, %eax
+; CHECK-SSE4-O0-NEXT: movshdup {{.*#+}} xmm2 = xmm1[1,1,3,3]
+; CHECK-SSE4-O0-NEXT: pslld $16, %xmm2
+; CHECK-SSE4-O0-NEXT: pextrw $3, %xmm1, %eax
; CHECK-SSE4-O0-NEXT: movd %eax, %xmm1
; CHECK-SSE4-O0-NEXT: pslld $16, %xmm1
-; CHECK-SSE4-O0-NEXT: movd %xmm3, %eax
-; CHECK-SSE4-O0-NEXT: shrl $16, %eax
-; CHECK-SSE4-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE4-O0-NEXT: movw %ax, -{{[0-9]+}}(%rsp)
-; CHECK-SSE4-O0-NEXT: movd %xmm2, %eax
-; CHECK-SSE4-O0-NEXT: shrl $16, %eax
-; CHECK-SSE4-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE4-O0-NEXT: movw %ax, -{{[0-9]+}}(%rsp)
-; CHECK-SSE4-O0-NEXT: movd %xmm1, %eax
-; CHECK-SSE4-O0-NEXT: shrl $16, %eax
-; CHECK-SSE4-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE4-O0-NEXT: movw %ax, -{{[0-9]+}}(%rsp)
-; CHECK-SSE4-O0-NEXT: movd %xmm0, %eax
-; CHECK-SSE4-O0-NEXT: shrl $16, %eax
-; CHECK-SSE4-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE4-O0-NEXT: movw %ax, -{{[0-9]+}}(%rsp)
-; CHECK-SSE4-O0-NEXT: movq -{{[0-9]+}}(%rsp), %rax
-; CHECK-SSE4-O0-NEXT: movq %rax, (%rdi)
+; CHECK-SSE4-O0-NEXT: psrld $16, %xmm0
+; CHECK-SSE4-O0-NEXT: pblendw {{.*#+}} xmm0 = xmm0[0],xmm3[1],xmm0[2,3,4,5,6,7]
+; CHECK-SSE4-O0-NEXT: pmovzxwd {{.*#+}} xmm2 = xmm2[0],zero,xmm2[1],zero,xmm2[2],zero,xmm2[3],zero
+; CHECK-SSE4-O0-NEXT: pblendw {{.*#+}} xmm0 = xmm0[0,1],xmm2[2],xmm0[3,4,5,6,7]
+; CHECK-SSE4-O0-NEXT: movsldup {{.*#+}} xmm1 = xmm1[0,0,2,2]
+; CHECK-SSE4-O0-NEXT: pblendw {{.*#+}} xmm0 = xmm0[0,1,2],xmm1[3],xmm0[4,5,6,7]
+; CHECK-SSE4-O0-NEXT: movq %xmm0, (%rdi)
; CHECK-SSE4-O0-NEXT: retq
;
; CHECK-AVX-O0-LABEL: store_atomic_vec4_bfloat:
@@ -2555,276 +2523,31 @@ define void @store_atomic_vec16_bfloat_unaligned(ptr %x, <16 x bfloat> %v) nounw
; CHECK-AVX-O3-NEXT: addq $40, %rsp
; CHECK-AVX-O3-NEXT: retq
;
-; CHECK-SSE2-O0-LABEL: store_atomic_vec16_bfloat_unaligned:
-; CHECK-SSE2-O0: # %bb.0:
-; CHECK-SSE2-O0-NEXT: subq $40, %rsp
-; CHECK-SSE2-O0-NEXT: movaps %xmm1, %xmm6
-; CHECK-SSE2-O0-NEXT: movaps %xmm0, %xmm14
-; CHECK-SSE2-O0-NEXT: movq %rdi, %rsi
-; CHECK-SSE2-O0-NEXT: xorps %xmm11, %xmm11
-; CHECK-SSE2-O0-NEXT: movaps %xmm11, %xmm3
-; CHECK-SSE2-O0-NEXT: punpckhwd {{.*#+}} xmm3 = xmm3[4],xmm6[4],xmm3[5],xmm6[5],xmm3[6],xmm6[6],xmm3[7],xmm6[7]
-; CHECK-SSE2-O0-NEXT: punpckhwd {{.*#+}} xmm11 = xmm11[4],xmm14[4],xmm11[5],xmm14[5],xmm11[6],xmm14[6],xmm11[7],xmm14[7]
-; CHECK-SSE2-O0-NEXT: movaps %xmm6, %xmm7
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm7
-; CHECK-SSE2-O0-NEXT: movaps %xmm14, %xmm15
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm15
-; CHECK-SSE2-O0-NEXT: pextrw $7, %xmm6, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm0
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm0
-; CHECK-SSE2-O0-NEXT: pextrw $6, %xmm6, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm1
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm1
-; CHECK-SSE2-O0-NEXT: pextrw $5, %xmm6, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm2
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm2
-; CHECK-SSE2-O0-NEXT: pextrw $3, %xmm6, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm4
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm4
-; CHECK-SSE2-O0-NEXT: pextrw $2, %xmm6, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm5
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm5
-; CHECK-SSE2-O0-NEXT: pextrw $1, %xmm6, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm6
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm6
-; CHECK-SSE2-O0-NEXT: pextrw $7, %xmm14, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm8
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm8
-; CHECK-SSE2-O0-NEXT: pextrw $6, %xmm14, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm9
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm9
-; CHECK-SSE2-O0-NEXT: pextrw $5, %xmm14, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm10
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm10
-; CHECK-SSE2-O0-NEXT: pextrw $3, %xmm14, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm12
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm12
-; CHECK-SSE2-O0-NEXT: pextrw $2, %xmm14, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm13
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm13
-; CHECK-SSE2-O0-NEXT: pextrw $1, %xmm14, %eax
-; CHECK-SSE2-O0-NEXT: movd %eax, %xmm14
-; CHECK-SSE2-O0-NEXT: pslld $16, %xmm14
-; CHECK-SSE2-O0-NEXT: movd %xmm15, %eax
-; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE2-O0-NEXT: movw %ax, (%rsp)
-; CHECK-SSE2-O0-NEXT: movd %xmm14, %eax
-; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE2-O0-NEXT: movw %ax, {{[0-9]+}}(%rsp)
-; CHECK-SSE2-O0-NEXT: movd %xmm13, %eax
-; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE2-O0-NEXT: movw %ax, {{[0-9]+}}(%rsp)
-; CHECK-SSE2-O0-NEXT: movd %xmm12, %eax
-; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE2-O0-NEXT: movw %ax, {{[0-9]+}}(%rsp)
-; CHECK-SSE2-O0-NEXT: movd %xmm11, %eax
-; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE2-O0-NEXT: movw %ax, {{[0-9]+}}(%rsp)
-; CHECK-SSE2-O0-NEXT: movd %xmm10, %eax
-; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE2-O0-NEXT: movw %ax, {{[0-9]+}}(%rsp)
-; CHECK-SSE2-O0-NEXT: movd %xmm9, %eax
-; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE2-O0-NEXT: movw %ax, {{[0-9]+}}(%rsp)
-; CHECK-SSE2-O0-NEXT: movd %xmm8, %eax
-; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; CHECK-SSE2-O0-NEXT: movw %ax, {{[0-9]+}}(%rsp)
-; CHECK-SSE2-O0-NEXT: movd %xmm7, %eax
-; CHECK-SSE2-O0-NEXT: shrl $16, %eax
-; CHECK-SSE2-O0-NEXT: # kill: def $ax killed $ax killed $eax
-; ...
[truncated]
</details>
<!--PREMERGE ADVISOR COMMENT: Windows-->
:window: Windows x64 Test Results
149668 tests passed
2318 tests skipped
:white_check_mark: The build succeeded and all tests passed.
The rule up here is hasAVX...
But down here the rule is hasSSE41, won't that break for SSE4.1 with no AVX? Can this check be rewritten in terms of legality? (Or is this check just wholly redundant, if this is going to return is-legal or default expand, I would hope it wouldn't be marked custom)
Are the legalize actions actually the same? I don't expect to need this much code if half is already legalizing the same way
Thanks for the review @araenm! So the actual problem with the initial version of this PR was that I was trying to work around the fact that scalar bf16 also has weird handling pre AVX. Fixing that makes this diff a lot more sensible (at least imo). It now only touches x86 codegen (loongarch, SelctionDag and PPC tags can be removed), and the diff is much more mechanical rather than inventing new mechanisms. As is, this PR is an overall bf16 improvement (with minor regression in masked_compress load/store, and I have a followup 34 loc change to X86ISelLowering that fixes these regressions (further 700 line reduction in bfloat codegen)
This basically does the same thing as https://github.com/llvm/llvm-project/pull/214487, and the concerns on that pr still sound. For the vector types, it would be another large change. We lack enough coverage tests for BF16 vector. It's better to split into a seperate patch.
It's better to split into a separate patch.
@phoebewang What is better split into a separate patch? bf16 coverage?
@phoebewang Are you saying that the part to split is scalar bf16 being legal and vector bf16 being legal?
We lack enough coverage tests for BF16 vector.
Is the right answer to add a bunch of coverage? With this PR, that coverage should be reasonably cheap. The status quo of people not testing bf16 because it gets bad codegen and we can't improve the codegen for it because we don't have sufficient tests seems sub-optimal.
@phoebewang Are you saying that the part to split is scalar bf16 being legal and vector bf16 being legal?
Yes.
> We lack enough coverage tests for BF16 vector.
> Is the right answer to add a bunch of coverage? With this PR, that coverage should be reasonably cheap. The status quo of people not testing bf16 because it gets bad codegen and we can't improve the codegen for it because we don't have sufficient tests seems sub-optimal.
We didn't do that because bf16 vector will be scalarized anyway due to the libcall, but 3fdac7de5496 has improved it on AVX. We can extend it to SSE2 to improve the codegen. Otherwise, it's not that useful to make the vector type legal.
even if libcalls scalarize, plenty of vector operations (extracts, inserts, permutes etc) didn't do math and thus don't need libcalls.
Furthermore, we know this architecture works because it's what we already do for f16. As far as I can see, there's no reason why bf16 vecs should be illegal on targets where f16 vecs are legal. almost all of the targets with legal f16 vecs don't have native f16 math.
Can you remove this FIXME and add
; RUN: llc < %s -mtriple=x86_64-- -mattr=+avx2 | FileCheck %s --check-prefixes=AVX
; RUN: llc < %s -mtriple=x86_64-- -mattr=+avx512f | FileCheck %s --check-prefixes=AVX
; RUN: llc < %s -mtriple=x86_64-- -mattr=+avx512vl | FileCheck %s --check-prefixes=AVX
; RUN: llc < %s -mtriple=x86_64-- -mattr=+avxneconvert | FileCheck %s --check-prefixes=AVX
; RUN: llc < %s -mtriple=x86_64-- -mattr=+avx512bw | FileCheck %s --check-prefixes=AVX
above? If so, I will drop https://github.com/llvm/llvm-project/pull/240805 since this PR makes it redundant.
Done.
We didn't do that because bf16 vector will be scalarized anyway due to the libcall
@phoebewang I think this is a good example of why making the type legal is a good idea even if arithmetic gets scalarized. Reducing bf16 complexity by making it take the same paths as f16 fixes bugs because it gets handled in a fashion that requires fewer workarounds to make things work.
even if libcalls scalarize, plenty of vector operations (extracts, inserts, permutes etc) didn't do math and thus don't need libcalls.
> Furthermore, we know this architecture works because it's what we already do for f16. As far as I can see, there's no reason why bf16 vecs should be illegal on targets where f16 vecs are legal. almost all of the targets with legal f16 vecs don't have native f16 math.
I made the f16 vectors legal, and it took about a dozen follow-up patches to fix the problems it exposed. With enough test coverage, most of those would have been caught earlier. That's why I think coverage matters here.
There is one difference, though. When we made vector f16 legal, F16C already provided vector f16 conversions on the popular targets, so I treated pre-F16C targets as niche and didn't worry about their performance. bf16 is different: the conversion features (AVX512BF16 and AVX-NE-CONVERT) are still missing on many targets today. So we need to pay attention to code quality on those targets too, not just correctness.
It's true. I just don't think it represents common scenarios.
Are there specific tests that you want me to add, or should I just look for every f16 test and add a corresponding bf16 one?
Are there specific tests that you want me to add, or should I just look for every f16 test and add a corresponding bf16 one?
Ideally, each line should have its coverage in the test. However, many of them are added mechanically and we don't want to introduce inefficient verbose tests. So, learn the current f16 test and adding corresponding bf16 ones would be good.
arsenm
oscardssmith
Fixes https://github.com/llvm/llvm-project/pull/240805.
As pointed out in https://github.com/llvm/llvm-project/pull/240632#discussion_r4534305598,
bf16(unlikef16) was considered illegal on many sub-architectures.bf16was soft-promoted as a scalar, andbf16vectors were only legal with AVX (v8bf16) or withAVX-NE-CONVERT/AVX512BW(v16bf16,v32bf16), even though the vector registers can always hold them.Register scalar
bf16inFR16/FR16XwithSSE2, likef16, with its operations promoted tof32. Registerv8bf16withSSE2,v16bf16withAVXandv32bf16withAVX512F, matching thei16vectors, and pick the register class withVLXlike the other vector types.Without
AVX,bf16vectors of up to 4 elements are split rather than widened tov8bf16, likef16vectors withoutF16C, since their operations are scalarized anyway.Vector rounding to
bf16is only expanded in the vector domain when thef32source vector is legal.