Skip to content

Commit 78601b9

Browse files
committed
[LV] Teach the vectorizer to cost and vectorize llvm.sincos intrinsics
This teaches the loop vectorizer that `llvm.sincos` is trivially vectorizable. Additionally, this patch updates the cost model to cost intrinsics that return multiple values correctly. Previously, the cost model only thought intrinsics that return `VectorType` need scalarizing, which meant it cost intrinsics that return multiple vectors (that need scalarizing) way too cheap (giving it the cost of a single function call). The `llvm.sincos` intrinsic also has a custom cost when a vector function library is available, as certain VFs can be expanded (later in code-gen) to a vector function, reducing the cost to a single call (+ the possible loads from the vector function returns values via output pointers).
1 parent cc6fcd3 commit 78601b9

File tree

7 files changed

+256
-28
lines changed

7 files changed

+256
-28
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ class IntrinsicCostAttributes {
126126
// If ScalarizationCost is UINT_MAX, the cost of scalarizing the
127127
// arguments and the return value will be computed based on types.
128128
InstructionCost ScalarizationCost = InstructionCost::getInvalid();
129+
TargetLibraryInfo const *LibInfo = nullptr;
129130

130131
public:
131132
IntrinsicCostAttributes(
@@ -145,7 +146,8 @@ class IntrinsicCostAttributes {
145146
Intrinsic::ID Id, Type *RTy, ArrayRef<const Value *> Args,
146147
ArrayRef<Type *> Tys, FastMathFlags Flags = FastMathFlags(),
147148
const IntrinsicInst *I = nullptr,
148-
InstructionCost ScalarCost = InstructionCost::getInvalid());
149+
InstructionCost ScalarCost = InstructionCost::getInvalid(),
150+
TargetLibraryInfo const *LibInfo = nullptr);
149151

150152
Intrinsic::ID getID() const { return IID; }
151153
const IntrinsicInst *getInst() const { return II; }
@@ -154,6 +156,7 @@ class IntrinsicCostAttributes {
154156
InstructionCost getScalarizationCost() const { return ScalarizationCost; }
155157
const SmallVectorImpl<const Value *> &getArgs() const { return Arguments; }
156158
const SmallVectorImpl<Type *> &getArgTypes() const { return ParamTys; }
159+
const TargetLibraryInfo *getLibInfo() const { return LibInfo; }
157160

158161
bool isTypeBasedOnly() const {
159162
return Arguments.empty();

llvm/include/llvm/CodeGen/BasicTTIImpl.h

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "llvm/ADT/SmallVector.h"
2323
#include "llvm/Analysis/LoopInfo.h"
2424
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
25+
#include "llvm/Analysis/TargetLibraryInfo.h"
2526
#include "llvm/Analysis/TargetTransformInfo.h"
2627
#include "llvm/Analysis/TargetTransformInfoImpl.h"
2728
#include "llvm/Analysis/ValueTracking.h"
@@ -1716,9 +1717,9 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
17161717

17171718
Type *RetTy = ICA.getReturnType();
17181719

1719-
ElementCount RetVF =
1720-
(RetTy->isVectorTy() ? cast<VectorType>(RetTy)->getElementCount()
1721-
: ElementCount::getFixed(1));
1720+
ElementCount RetVF = isVectorizedTy(RetTy) ? getVectorizedTypeVF(RetTy)
1721+
: ElementCount::getFixed(1);
1722+
17221723
const IntrinsicInst *I = ICA.getInst();
17231724
const SmallVectorImpl<const Value *> &Args = ICA.getArgs();
17241725
FastMathFlags FMF = ICA.getFlags();
@@ -1971,6 +1972,49 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
19711972
}
19721973
case Intrinsic::experimental_vector_match:
19731974
return thisT()->getTypeBasedIntrinsicInstrCost(ICA, CostKind);
1975+
case Intrinsic::sincos: {
1976+
// Vector variants of llvm.sincos can be mapped to a vector library call.
1977+
auto const *LibInfo = ICA.getLibInfo();
1978+
if (!LibInfo || !isVectorizedTy(RetTy))
1979+
break;
1980+
1981+
// Find associated libcall.
1982+
VectorType *VectorTy = cast<VectorType>(getContainedTypes(RetTy).front());
1983+
EVT VT = getTLI()->getValueType(DL, VectorTy);
1984+
RTLIB::Libcall LC = RTLIB::getFSINCOS(VT.getVectorElementType());
1985+
const char *LCName = getTLI()->getLibcallName(LC);
1986+
if (!LC || !LCName)
1987+
break;
1988+
1989+
// Search for a corresponding vector variant.
1990+
LLVMContext &Ctx = RetTy->getContext();
1991+
auto VF = getVectorizedTypeVF(RetTy);
1992+
VecDesc const *VD = nullptr;
1993+
for (bool Masked : {false, true}) {
1994+
if ((VD = LibInfo->getVectorMappingInfo(LCName, VF, Masked)))
1995+
break;
1996+
}
1997+
if (!VD)
1998+
break;
1999+
2000+
// Cost the call + mask.
2001+
auto Cost = thisT()->getCallInstrCost(nullptr, RetTy, ICA.getArgTypes(),
2002+
CostKind);
2003+
if (VD->isMasked())
2004+
Cost += thisT()->getShuffleCost(
2005+
TargetTransformInfo::SK_Broadcast,
2006+
VectorType::get(IntegerType::getInt1Ty(Ctx), VF), {}, CostKind, 0,
2007+
nullptr, {});
2008+
2009+
// Lowering to a sincos library call (with output pointers) may require us
2010+
// to emit reloads for the results.
2011+
Cost +=
2012+
thisT()->getMemoryOpCost(
2013+
Instruction::Load, VectorTy,
2014+
thisT()->getDataLayout().getABITypeAlign(VectorTy), 0, CostKind) *
2015+
2;
2016+
return Cost;
2017+
}
19742018
}
19752019

19762020
// Assume that we need to scalarize this intrinsic.)
@@ -1979,10 +2023,13 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
19792023
InstructionCost ScalarizationCost = InstructionCost::getInvalid();
19802024
if (RetVF.isVector() && !RetVF.isScalable()) {
19812025
ScalarizationCost = 0;
1982-
if (!RetTy->isVoidTy())
1983-
ScalarizationCost += getScalarizationOverhead(
1984-
cast<VectorType>(RetTy),
1985-
/*Insert*/ true, /*Extract*/ false, CostKind);
2026+
if (!RetTy->isVoidTy()) {
2027+
for (Type *VectorTy : getContainedTypes(RetTy)) {
2028+
ScalarizationCost += getScalarizationOverhead(
2029+
cast<VectorType>(VectorTy),
2030+
/*Insert*/ true, /*Extract*/ false, CostKind);
2031+
}
2032+
}
19862033
ScalarizationCost +=
19872034
getOperandsScalarizationOverhead(Args, ICA.getArgTypes(), CostKind);
19882035
}
@@ -2637,27 +2684,32 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
26372684
// Else, assume that we need to scalarize this intrinsic. For math builtins
26382685
// this will emit a costly libcall, adding call overhead and spills. Make it
26392686
// very expensive.
2640-
if (auto *RetVTy = dyn_cast<VectorType>(RetTy)) {
2687+
if (isVectorizedTy(RetTy)) {
2688+
ArrayRef<Type *> RetVTys = getContainedTypes(RetTy);
2689+
26412690
// Scalable vectors cannot be scalarized, so return Invalid.
2642-
if (isa<ScalableVectorType>(RetTy) || any_of(Tys, [](const Type *Ty) {
2643-
return isa<ScalableVectorType>(Ty);
2644-
}))
2691+
if (any_of(concat<Type *const>(RetVTys, Tys),
2692+
[](Type *Ty) { return isa<ScalableVectorType>(Ty); }))
26452693
return InstructionCost::getInvalid();
26462694

2647-
InstructionCost ScalarizationCost =
2648-
SkipScalarizationCost
2649-
? ScalarizationCostPassed
2650-
: getScalarizationOverhead(RetVTy, /*Insert*/ true,
2651-
/*Extract*/ false, CostKind);
2695+
InstructionCost ScalarizationCost = ScalarizationCostPassed;
2696+
if (!SkipScalarizationCost) {
2697+
ScalarizationCost = 0;
2698+
for (Type *RetVTy : RetVTys) {
2699+
ScalarizationCost += getScalarizationOverhead(
2700+
cast<VectorType>(RetVTy), /*Insert*/ true,
2701+
/*Extract*/ false, CostKind);
2702+
}
2703+
}
26522704

2653-
unsigned ScalarCalls = cast<FixedVectorType>(RetVTy)->getNumElements();
2705+
unsigned ScalarCalls = getVectorizedTypeVF(RetTy).getFixedValue();
26542706
SmallVector<Type *, 4> ScalarTys;
26552707
for (Type *Ty : Tys) {
26562708
if (Ty->isVectorTy())
26572709
Ty = Ty->getScalarType();
26582710
ScalarTys.push_back(Ty);
26592711
}
2660-
IntrinsicCostAttributes Attrs(IID, RetTy->getScalarType(), ScalarTys, FMF);
2712+
IntrinsicCostAttributes Attrs(IID, toScalarizedTy(RetTy), ScalarTys, FMF);
26612713
InstructionCost ScalarCost =
26622714
thisT()->getIntrinsicInstrCost(Attrs, CostKind);
26632715
for (Type *Ty : Tys) {

llvm/lib/Analysis/TargetTransformInfo.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,12 @@ IntrinsicCostAttributes::IntrinsicCostAttributes(Intrinsic::ID Id, Type *Ty,
101101
ParamTys.push_back(Argument->getType());
102102
}
103103

104-
IntrinsicCostAttributes::IntrinsicCostAttributes(Intrinsic::ID Id, Type *RTy,
105-
ArrayRef<const Value *> Args,
106-
ArrayRef<Type *> Tys,
107-
FastMathFlags Flags,
108-
const IntrinsicInst *I,
109-
InstructionCost ScalarCost)
110-
: II(I), RetTy(RTy), IID(Id), FMF(Flags), ScalarizationCost(ScalarCost) {
104+
IntrinsicCostAttributes::IntrinsicCostAttributes(
105+
Intrinsic::ID Id, Type *RTy, ArrayRef<const Value *> Args,
106+
ArrayRef<Type *> Tys, FastMathFlags Flags, const IntrinsicInst *I,
107+
InstructionCost ScalarCost, TargetLibraryInfo const *LibInfo)
108+
: II(I), RetTy(RTy), IID(Id), FMF(Flags), ScalarizationCost(ScalarCost),
109+
LibInfo(LibInfo) {
111110
ParamTys.insert(ParamTys.begin(), Tys.begin(), Tys.end());
112111
Arguments.insert(Arguments.begin(), Args.begin(), Args.end());
113112
}

llvm/lib/Analysis/VectorUtils.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ bool llvm::isTriviallyVectorizable(Intrinsic::ID ID) {
7272
case Intrinsic::atan2:
7373
case Intrinsic::sin:
7474
case Intrinsic::cos:
75+
case Intrinsic::sincos:
7576
case Intrinsic::tan:
7677
case Intrinsic::sinh:
7778
case Intrinsic::cosh:
@@ -179,6 +180,7 @@ bool llvm::isVectorIntrinsicWithOverloadTypeAtArg(
179180
case Intrinsic::ucmp:
180181
case Intrinsic::scmp:
181182
return OpdIdx == -1 || OpdIdx == 0;
183+
case Intrinsic::sincos:
182184
case Intrinsic::is_fpclass:
183185
case Intrinsic::vp_is_fpclass:
184186
return OpdIdx == 0;

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2881,7 +2881,8 @@ LoopVectorizationCostModel::getVectorIntrinsicCost(CallInst *CI,
28812881
[&](Type *Ty) { return maybeVectorizeType(Ty, VF); });
28822882

28832883
IntrinsicCostAttributes CostAttrs(ID, RetTy, Arguments, ParamTys, FMF,
2884-
dyn_cast<IntrinsicInst>(CI));
2884+
dyn_cast<IntrinsicInst>(CI),
2885+
InstructionCost::getInvalid(), TLI);
28852886
return TTI.getIntrinsicInstrCost(CostAttrs, CostKind);
28862887
}
28872888

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,8 @@ InstructionCost VPWidenIntrinsicRecipe::computeCost(ElementCount VF,
11261126
FastMathFlags FMF = hasFastMathFlags() ? getFastMathFlags() : FastMathFlags();
11271127
IntrinsicCostAttributes CostAttrs(
11281128
VectorIntrinsicID, RetTy, Arguments, ParamTys, FMF,
1129-
dyn_cast_or_null<IntrinsicInst>(getUnderlyingValue()));
1129+
dyn_cast_or_null<IntrinsicInst>(getUnderlyingValue()),
1130+
InstructionCost::getInvalid(), &Ctx.TLI);
11301131
return Ctx.TTI.getIntrinsicInstrCost(CostAttrs, Ctx.CostKind);
11311132
}
11321133

0 commit comments

Comments
 (0)