Skip to content

Commit

Permalink
[NFC][llvm] Make the contructors of ElementCount private.
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D86120
  • Loading branch information
Francesco Petrogalli committed Aug 19, 2020
1 parent d29d1e2 commit 264afb9
Show file tree
Hide file tree
Showing 25 changed files with 107 additions and 81 deletions.
5 changes: 3 additions & 2 deletions llvm/include/llvm/Analysis/VectorUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ struct VFShape {
// Retrieve the VFShape that can be used to map a (scalar) function to itself,
// with VF = 1.
static VFShape getScalarShape(const CallInst &CI) {
return VFShape::get(CI, /*EC*/ {1, false}, /*HasGlobalPredicate*/ false);
return VFShape::get(CI, ElementCount::getFixed(1),
/*HasGlobalPredicate*/ false);
}

// Retrieve the basic vectorization shape of the function, where all
Expand Down Expand Up @@ -305,7 +306,7 @@ typedef unsigned ID;
inline Type *ToVectorTy(Type *Scalar, unsigned VF, bool isScalable = false) {
if (Scalar->isVoidTy() || VF == 1)
return Scalar;
return VectorType::get(Scalar, {VF, isScalable});
return VectorType::get(Scalar, ElementCount::get(VF, isScalable));
}

/// Identify if the intrinsic is trivially vectorizable.
Expand Down
5 changes: 3 additions & 2 deletions llvm/include/llvm/IR/DerivedTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,8 @@ class VectorType : public Type {

static VectorType *get(Type *ElementType, unsigned NumElements,
bool Scalable) {
return VectorType::get(ElementType, {NumElements, Scalable});
return VectorType::get(ElementType,
ElementCount::get(NumElements, Scalable));
}

static VectorType *get(Type *ElementType, const VectorType *Other) {
Expand Down Expand Up @@ -640,7 +641,7 @@ class ScalableVectorType : public VectorType {
};

inline ElementCount VectorType::getElementCount() const {
return ElementCount(ElementQuantity, isa<ScalableVectorType>(this));
return ElementCount::get(ElementQuantity, isa<ScalableVectorType>(this));
}

/// Class to represent pointers.
Expand Down
7 changes: 4 additions & 3 deletions llvm/include/llvm/IR/Intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ namespace Intrinsic {
unsigned Pointer_AddressSpace;
unsigned Struct_NumElements;
unsigned Argument_Info;
ElementCount Vector_Width;
// There is no default constructor in `ElementCount`, so we need
// to explicitly initialize this field with a value.
ElementCount Vector_Width = ElementCount::getFixed(0);
};

enum ArgKind {
Expand Down Expand Up @@ -190,8 +192,7 @@ namespace Intrinsic {
static IITDescriptor getVector(unsigned Width, bool IsScalable) {
IITDescriptor Result;
Result.Kind = Vector;
Result.Vector_Width.Min = Width;
Result.Vector_Width.Scalable = IsScalable;
Result.Vector_Width = ElementCount::get(Width, IsScalable);
return Result;
}
};
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/Support/MachineValueType.h
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ namespace llvm {
}

ElementCount getVectorElementCount() const {
return { getVectorNumElements(), isScalableVector() };
return ElementCount::get(getVectorNumElements(), isScalableVector());
}

/// Given a vector type, return the minimum number of elements it contains.
Expand Down
33 changes: 25 additions & 8 deletions llvm/include/llvm/Support/TypeSize.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,23 @@ namespace llvm {
template <typename T> struct DenseMapInfo;

class ElementCount {
private:
/// Prevent code from using initializer-list contructors like
/// ElementCount EC = {<unsigned>, <bool>}. The static `get*`
/// methods below are preferred, as users should always make a
/// conscious choice on the type of `ElementCount` they are
/// requesting.
ElementCount(unsigned Min, bool Scalable) : Min(Min), Scalable(Scalable) {}

public:
/// No default constructor. Users should use one of the `get*`
/// static methods below, as they should always make a conscious
/// choice on the type of `ElementCount` they are requesting.
ElementCount() = delete;
unsigned Min; // Minimum number of vector elements.
bool Scalable; // If true, NumElements is a multiple of 'Min' determined
// at runtime rather than compile time.

ElementCount() = default;

ElementCount(unsigned Min, bool Scalable)
: Min(Min), Scalable(Scalable) {}

ElementCount operator*(unsigned RHS) {
return { Min * RHS, Scalable };
}
Expand All @@ -54,7 +61,13 @@ class ElementCount {
bool operator!=(unsigned RHS) const { return !(*this == RHS); }

ElementCount NextPowerOf2() const {
return ElementCount(llvm::NextPowerOf2(Min), Scalable);
return {(unsigned)llvm::NextPowerOf2(Min), Scalable};
}

static ElementCount getFixed(unsigned Min) { return {Min, false}; }
static ElementCount getScalable(unsigned Min) { return {Min, true}; }
static ElementCount get(unsigned Min, bool Scalable) {
return {Min, Scalable};
}
};

Expand Down Expand Up @@ -279,8 +292,12 @@ inline TypeSize alignTo(TypeSize Size, uint64_t Align) {
}

template <> struct DenseMapInfo<ElementCount> {
static inline ElementCount getEmptyKey() { return {~0U, true}; }
static inline ElementCount getTombstoneKey() { return {~0U - 1, false}; }
static inline ElementCount getEmptyKey() {
return ElementCount::getScalable(~0U);
}
static inline ElementCount getTombstoneKey() {
return ElementCount::getFixed(~0U - 1);
}
static unsigned getHashValue(const ElementCount& EltCnt) {
if (EltCnt.Scalable)
return (EltCnt.Min * 37U) - 1U;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/VFABIDemangling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ ElementCount getECFromSignature(FunctionType *Signature) {
if (auto *VTy = dyn_cast<VectorType>(Ty))
return VTy->getElementCount();

return ElementCount(/*Min=*/1, /*Scalable=*/false);
return ElementCount::getFixed(/*Min=*/1);
}
} // namespace

Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/AsmParser/LLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7395,7 +7395,7 @@ int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
// All vector parameters should have the same vector width.
ElementCount GEPWidth = BaseType->isVectorTy()
? cast<VectorType>(BaseType)->getElementCount()
: ElementCount(0, false);
: ElementCount::getFixed(0);

while (EatIfPresent(lltok::comma)) {
if (Lex.getKind() == lltok::MetadataVar) {
Expand All @@ -7408,7 +7408,7 @@ int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {

if (auto *ValVTy = dyn_cast<VectorType>(Val->getType())) {
ElementCount ValNumEl = ValVTy->getElementCount();
if (GEPWidth != ElementCount(0, false) && GEPWidth != ValNumEl)
if (GEPWidth != ElementCount::getFixed(0) && GEPWidth != ValNumEl)
return Error(EltLoc,
"getelementptr vector index has a wrong number of elements");
GEPWidth = ValNumEl;
Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -729,15 +729,15 @@ static void getCopyToPartsVector(SelectionDAG &DAG, const SDLoc &DL,
assert(IntermediateVT.isScalableVector() == ValueVT.isScalableVector() &&
"Mixing scalable and fixed vectors when copying in parts");

ElementCount DestEltCnt;
Optional<ElementCount> DestEltCnt;

if (IntermediateVT.isVector())
DestEltCnt = IntermediateVT.getVectorElementCount() * NumIntermediates;
else
DestEltCnt = ElementCount(NumIntermediates, false);
DestEltCnt = ElementCount::getFixed(NumIntermediates);

EVT BuiltVectorTy = EVT::getVectorVT(
*DAG.getContext(), IntermediateVT.getScalarType(), DestEltCnt);
*DAG.getContext(), IntermediateVT.getScalarType(), DestEltCnt.getValue());
if (ValueVT != BuiltVectorTy) {
if (SDValue Widened = widenVectorToPartType(DAG, Val, DL, BuiltVectorTy))
Val = Widened;
Expand Down Expand Up @@ -3746,7 +3746,7 @@ void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
bool IsVectorGEP = I.getType()->isVectorTy();
ElementCount VectorElementCount =
IsVectorGEP ? cast<VectorType>(I.getType())->getElementCount()
: ElementCount(0, false);
: ElementCount::getFixed(0);

if (IsVectorGEP && !N.getValueType().isVector()) {
LLVMContext &Context = *DAG.getContext();
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/TargetLoweringBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ TargetLoweringBase::getTypeConversion(LLVMContext &Context, EVT VT) const {
if (NumElts == 1)
return LegalizeKind(TypeScalarizeVector, EltVT);

if (VT.getVectorElementCount() == ElementCount(1, true))
if (VT.getVectorElementCount() == ElementCount::getScalable(1))
report_fatal_error("Cannot legalize this vector");

// Try to widen vector elements until the element type is a power of two and
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/CodeGen/ValueTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ EVT EVT::getExtendedVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements,

EVT EVT::getExtendedVectorVT(LLVMContext &Context, EVT VT, ElementCount EC) {
EVT ResultVT;
ResultVT.LLVMTy =
VectorType::get(VT.getTypeForEVT(Context), {EC.Min, EC.Scalable});
ResultVT.LLVMTy = VectorType::get(VT.getTypeForEVT(Context), EC);
assert(ResultVT.isExtended() && "Type is not extended!");
return ResultVT;
}
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/IR/ConstantFold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,8 @@ Constant *llvm::ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2,
ArrayRef<int> Mask) {
auto *V1VTy = cast<VectorType>(V1->getType());
unsigned MaskNumElts = Mask.size();
ElementCount MaskEltCount = {MaskNumElts, isa<ScalableVectorType>(V1VTy)};
auto MaskEltCount =
ElementCount::get(MaskNumElts, isa<ScalableVectorType>(V1VTy));
Type *EltTy = V1VTy->getElementType();

// Undefined shuffle mask -> undefined value.
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/IR/Constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2245,7 +2245,7 @@ Constant *ConstantExpr::getGetElementPtr(Type *Ty, Constant *C,
unsigned AS = C->getType()->getPointerAddressSpace();
Type *ReqTy = DestTy->getPointerTo(AS);

ElementCount EltCount = {0, false};
auto EltCount = ElementCount::getFixed(0);
if (VectorType *VecTy = dyn_cast<VectorType>(C->getType()))
EltCount = VecTy->getElementCount();
else
Expand Down Expand Up @@ -2938,7 +2938,7 @@ Constant *ConstantDataVector::getSplat(unsigned NumElts, Constant *V) {
return getFP(V->getType(), Elts);
}
}
return ConstantVector::getSplat({NumElts, false}, V);
return ConstantVector::getSplat(ElementCount::getFixed(NumElts), V);
}


Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/IR/IRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ Value *IRBuilderBase::CreateStripInvariantGroup(Value *Ptr) {

Value *IRBuilderBase::CreateVectorSplat(unsigned NumElts, Value *V,
const Twine &Name) {
ElementCount EC(NumElts, false);
auto EC = ElementCount::getFixed(NumElts);
return CreateVectorSplat(EC, V, Name);
}

Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/IR/Instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3330,9 +3330,9 @@ CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
// scalar types means that checking that vector lengths match also checks that
// scalars are not being converted to vectors or vectors to scalars).
ElementCount SrcEC = SrcIsVec ? cast<VectorType>(SrcTy)->getElementCount()
: ElementCount(0, false);
: ElementCount::getFixed(0);
ElementCount DstEC = DstIsVec ? cast<VectorType>(DstTy)->getElementCount()
: ElementCount(0, false);
: ElementCount::getFixed(0);

// Switch on the opcode provided
switch (op) {
Expand Down Expand Up @@ -3390,9 +3390,9 @@ CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
if (SrcIsVec && DstIsVec)
return SrcEC == DstEC;
if (SrcIsVec)
return SrcEC == ElementCount(1, false);
return SrcEC == ElementCount::getFixed(1);
if (DstIsVec)
return DstEC == ElementCount(1, false);
return DstEC == ElementCount::getFixed(1);

return true;
}
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/IR/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ FixedVectorType *FixedVectorType::get(Type *ElementType, unsigned NumElts) {
"be an integer, floating point, or "
"pointer type.");

ElementCount EC(NumElts, false);
auto EC = ElementCount::getFixed(NumElts);

LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
VectorType *&Entry = ElementType->getContext()
Expand All @@ -641,7 +641,7 @@ ScalableVectorType *ScalableVectorType::get(Type *ElementType,
"be an integer, floating point, or "
"pointer type.");

ElementCount EC(MinNumElts, true);
auto EC = ElementCount::getScalable(MinNumElts);

LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
VectorType *&Entry = ElementType->getContext()
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11985,7 +11985,8 @@ static SDValue LowerSVEIntrinsicEXT(SDNode *N, SelectionDAG &DAG) {

unsigned ElemSize = VT.getVectorElementType().getSizeInBits() / 8;
unsigned ByteSize = VT.getSizeInBits().getKnownMinSize() / 8;
EVT ByteVT = EVT::getVectorVT(Ctx, MVT::i8, { ByteSize, true });
EVT ByteVT =
EVT::getVectorVT(Ctx, MVT::i8, ElementCount::getScalable(ByteSize));

// Convert everything to the domain of EXT (i.e bytes).
SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, ByteVT, N->getOperand(1));
Expand Down
17 changes: 9 additions & 8 deletions llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1804,10 +1804,10 @@ void InnerLoopVectorizer::createVectorIntOrFpInductionPHI(
// FIXME: If the step is non-constant, we create the vector splat with
// IRBuilder. IRBuilder can constant-fold the multiply, but it doesn't
// handle a constant vector splat.
Value *SplatVF =
isa<Constant>(Mul)
? ConstantVector::getSplat({VF, false}, cast<Constant>(Mul))
: Builder.CreateVectorSplat(VF, Mul);
Value *SplatVF = isa<Constant>(Mul)
? ConstantVector::getSplat(ElementCount::getFixed(VF),
cast<Constant>(Mul))
: Builder.CreateVectorSplat(VF, Mul);
Builder.restoreIP(CurrIP);

// We may need to add the step a number of times, depending on the unroll
Expand Down Expand Up @@ -3399,7 +3399,8 @@ unsigned LoopVectorizationCostModel::getVectorCallCost(CallInst *CI,
// If we can't emit a vector call for this function, then the currently found
// cost is the cost we need to return.
NeedToScalarize = true;
VFShape Shape = VFShape::get(*CI, {VF, false}, false /*HasGlobalPred*/);
VFShape Shape =
VFShape::get(*CI, ElementCount::getFixed(VF), false /*HasGlobalPred*/);
Function *VecFunc = VFDatabase(*CI).getVectorizedFunction(Shape);

if (!TLI || CI->isNoBuiltin() || !VecFunc)
Expand Down Expand Up @@ -3860,7 +3861,7 @@ void InnerLoopVectorizer::fixReduction(PHINode *Phi) {
// incoming scalar reduction.
VectorStart = ReductionStartValue;
} else {
Identity = ConstantVector::getSplat({VF, false}, Iden);
Identity = ConstantVector::getSplat(ElementCount::getFixed(VF), Iden);

// This vector is the Identity vector where the first element is the
// incoming scalar reduction.
Expand Down Expand Up @@ -4541,8 +4542,8 @@ void InnerLoopVectorizer::widenCallInstruction(CallInst &I, VPUser &ArgOperands,
assert(VectorF && "Can't retrieve vector intrinsic.");
} else {
// Use vector version of the function call.
const VFShape Shape =
VFShape::get(*CI, {VF, false} /*EC*/, false /*HasGlobalPred*/);
const VFShape Shape = VFShape::get(*CI, ElementCount::getFixed(VF),
false /*HasGlobalPred*/);
#ifndef NDEBUG
assert(VFDatabase(*CI).getVectorizedFunction(Shape) != nullptr &&
"Can't create vector function.");
Expand Down
15 changes: 8 additions & 7 deletions llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3029,7 +3029,7 @@ void BoUpSLP::buildTree_rec(ArrayRef<Value *> VL, unsigned Depth,
Intrinsic::ID ID = getVectorIntrinsicIDForCall(CI, TLI);

VFShape Shape = VFShape::get(
*CI, {static_cast<unsigned int>(VL.size()), false /*Scalable*/},
*CI, ElementCount::getFixed(static_cast<unsigned int>(VL.size())),
false /*HasGlobalPred*/);
Function *VecFunc = VFDatabase(*CI).getVectorizedFunction(Shape);

Expand Down Expand Up @@ -3264,9 +3264,9 @@ getVectorCallCosts(CallInst *CI, VectorType *VecTy, TargetTransformInfo *TTI,
int IntrinsicCost =
TTI->getIntrinsicInstrCost(CostAttrs, TTI::TCK_RecipThroughput);

auto Shape =
VFShape::get(*CI, {static_cast<unsigned>(VecTy->getNumElements()), false},
false /*HasGlobalPred*/);
auto Shape = VFShape::get(*CI, ElementCount::getFixed(static_cast<unsigned>(
VecTy->getNumElements())),
false /*HasGlobalPred*/);
Function *VecFunc = VFDatabase(*CI).getVectorizedFunction(Shape);
int LibCost = IntrinsicCost;
if (!CI->isNoBuiltin() && VecFunc) {
Expand Down Expand Up @@ -4553,9 +4553,10 @@ Value *BoUpSLP::vectorizeTree(TreeEntry *E) {

Function *CF;
if (!UseIntrinsic) {
VFShape Shape = VFShape::get(
*CI, {static_cast<unsigned>(VecTy->getNumElements()), false},
false /*HasGlobalPred*/);
VFShape Shape =
VFShape::get(*CI, ElementCount::getFixed(static_cast<unsigned>(
VecTy->getNumElements())),
false /*HasGlobalPred*/);
CF = VFDatabase(*CI).getVectorizedFunction(Shape);
} else {
Type *Tys[] = {FixedVectorType::get(CI->getType(), E->Scalars.size())};
Expand Down
5 changes: 3 additions & 2 deletions llvm/unittests/Analysis/VectorUtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ TEST_F(BasicTest, isSplat) {
Value *SplatC = IRB.CreateVectorSplat(5, ScalarC);
EXPECT_TRUE(isSplatValue(SplatC));

Value *SplatC_SVE = IRB.CreateVectorSplat(ElementCount(5, true), ScalarC);
Value *SplatC_SVE =
IRB.CreateVectorSplat(ElementCount::getScalable(5), ScalarC);
EXPECT_TRUE(isSplatValue(SplatC_SVE));

// FIXME: Constant splat analysis does not allow undef elements.
Expand Down Expand Up @@ -502,7 +503,7 @@ class VFShapeAPITest : public testing::Test {
SmallVector<VFParameter, 8> &ExpectedParams = Expected.Parameters;

void buildShape(unsigned VF, bool IsScalable, bool HasGlobalPred) {
Shape = VFShape::get(*CI, {VF, IsScalable}, HasGlobalPred);
Shape = VFShape::get(*CI, ElementCount::get(VF, IsScalable), HasGlobalPred);
}

bool validParams(ArrayRef<VFParameter> Parameters) {
Expand Down
Loading

0 comments on commit 264afb9

Please sign in to comment.