Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions llvm/include/llvm/IR/VectorTypeUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@

namespace llvm {

/// Identifies if the vector form of the intrinsic that returns a struct has
/// a scalar element at the struct element index \p EleIdx.
LLVM_ABI bool isVectorIntrinsicWithStructReturnScalarAtField(unsigned ID,
int EleIdx);

/// A helper function for converting Scalar types to vector types. If
/// the incoming type is void, we return void. If the EC represents a
/// scalar, we return the scalar type.
Expand All @@ -31,7 +36,11 @@ inline Type *toVectorTy(Type *Scalar, unsigned VF) {
/// Note:
/// - If \p EC is scalar, \p StructTy is returned unchanged
/// - Only unpacked literal struct types are supported
LLVM_ABI Type *toVectorizedStructTy(StructType *StructTy, ElementCount EC);
/// vector types.
/// - If IID (Intrinsic ID) is provided, only fields that are vector types
/// are widened.
LLVM_ABI Type *toVectorizedStructTy(StructType *StructTy, ElementCount EC,
unsigned IID = 0);

/// A helper for converting structs of vector types to structs of scalar types.
/// Note: Only unpacked literal struct types are supported.
Expand All @@ -52,9 +61,9 @@ LLVM_ABI bool canVectorizeStructTy(StructType *StructTy);
/// - If the incoming type is void, we return void
/// - If \p EC is scalar, \p Ty is returned unchanged
/// - Only unpacked literal struct types are supported
inline Type *toVectorizedTy(Type *Ty, ElementCount EC) {
inline Type *toVectorizedTy(Type *Ty, ElementCount EC, unsigned IID = 0) {
if (StructType *StructTy = dyn_cast<StructType>(Ty))
return toVectorizedStructTy(StructTy, EC);
return toVectorizedStructTy(StructTy, EC, IID);
return toVectorTy(Ty, EC);
}

Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/Analysis/VectorUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ bool llvm::isVectorIntrinsicWithStructReturnOverloadAtField(
return TTI->isTargetIntrinsicWithStructReturnOverloadAtField(ID, RetIdx);

switch (ID) {
case Intrinsic::modf:
case Intrinsic::sincos:
case Intrinsic::sincospi:
return false;
case Intrinsic::frexp:
return RetIdx == 0 || RetIdx == 1;
default:
Expand Down
20 changes: 18 additions & 2 deletions llvm/lib/IR/VectorTypeUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,24 @@

#include "llvm/IR/VectorTypeUtils.h"
#include "llvm/ADT/SmallVectorExtras.h"
#include "llvm/IR/Intrinsics.h"

using namespace llvm;

bool llvm::isVectorIntrinsicWithStructReturnScalarAtField(unsigned ID,
int EleIdx) {
switch (ID) {
case Intrinsic::vp_load_ff:
return EleIdx == 1;
default:
return false;
}
}

/// A helper for converting structs of scalar types to structs of vector types.
/// Note: Only unpacked literal struct types are supported.
Type *llvm::toVectorizedStructTy(StructType *StructTy, ElementCount EC) {
Type *llvm::toVectorizedStructTy(StructType *StructTy, ElementCount EC,
unsigned IID) {
if (EC.isScalar())
return StructTy;
assert(isUnpackedStructLiteral(StructTy) &&
Expand All @@ -22,7 +34,11 @@ Type *llvm::toVectorizedStructTy(StructType *StructTy, ElementCount EC) {
"expected all element types to be valid vector element types");
return StructType::get(
StructTy->getContext(),
map_to_vector(StructTy->elements(), [&](Type *ElTy) -> Type * {
map_to_vector(enumerate(StructTy->elements()), [&](auto It) -> Type * {
Type *ElTy = It.value();
if (IID != 0 &&
isVectorIntrinsicWithStructReturnScalarAtField(IID, It.index()))
return ElTy;
return VectorType::get(ElTy, EC);
}));
}
Expand Down
6 changes: 5 additions & 1 deletion llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4139,7 +4139,11 @@ static bool willGenerateVectors(VPlan &Plan, ElementCount VF,
Type *ScalarTy = TypeInfo.inferScalarType(ToCheck);
if (!Visited.insert({ScalarTy}).second)
continue;
Type *WideTy = toVectorizedTy(ScalarTy, VF);
unsigned IID = 0;
if (auto *WI = dyn_cast<VPWidenIntrinsicRecipe>(&R))
WI->getVectorIntrinsicID();
Type *WideTy = toVectorizedTy(ScalarTy, VF, IID);

if (any_of(getContainedTypes(WideTy), WillGenerateTargetVectors))
return true;
}
Expand Down
14 changes: 12 additions & 2 deletions llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1679,7 +1679,16 @@ void VPWidenIntrinsicRecipe::execute(VPTransformState &State) {

SmallVector<Type *, 2> TysForDecl;
// Add return type if intrinsic is overloaded on it.
if (isVectorIntrinsicWithOverloadTypeAtArg(VectorIntrinsicID, -1, State.TTI))
if (ResultTy->isStructTy()) {
auto *StructTy = cast<StructType>(ResultTy);
for (unsigned I = 0, E = StructTy->getNumElements(); I != E; ++I) {
if (isVectorIntrinsicWithStructReturnOverloadAtField(VectorIntrinsicID, I,
State.TTI))
TysForDecl.push_back(
toVectorizedTy(StructTy->getStructElementType(I), State.VF));
}
} else if (isVectorIntrinsicWithOverloadTypeAtArg(VectorIntrinsicID, -1,
State.TTI))
TysForDecl.push_back(VectorType::get(getResultType(), State.VF));
SmallVector<Value *, 4> Args;
for (const auto &I : enumerate(operands())) {
Expand Down Expand Up @@ -1744,7 +1753,8 @@ static InstructionCost getCostForIntrinsics(Intrinsic::ID ID,
}

Type *ScalarRetTy = Ctx.Types.inferScalarType(&R);
Type *RetTy = VF.isVector() ? toVectorizedTy(ScalarRetTy, VF) : ScalarRetTy;
Type *RetTy =
VF.isVector() ? toVectorizedTy(ScalarRetTy, VF, ID) : ScalarRetTy;
SmallVector<Type *> ParamTys;
for (const VPValue *Op : Operands) {
ParamTys.push_back(VF.isVector()
Expand Down
7 changes: 7 additions & 0 deletions llvm/unittests/IR/VectorTypeUtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "llvm/IR/VectorTypeUtils.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/LLVMContext.h"
#include "gtest/gtest.h"

Expand All @@ -24,6 +25,7 @@ TEST(VectorTypeUtilsTest, TestToVectorizedTy) {
Type *FTy = Type::getFloatTy(C);
Type *HomogeneousStructTy = StructType::get(FTy, FTy, FTy);
Type *MixedStructTy = StructType::get(FTy, ITy);
Type *FFLoadRetTy = StructType::get(ITy, ITy);
Type *VoidTy = Type::getVoidTy(C);

for (ElementCount VF :
Expand Down Expand Up @@ -54,6 +56,11 @@ TEST(VectorTypeUtilsTest, TestToVectorizedTy) {
VectorType::get(ITy, VF));

EXPECT_EQ(toVectorizedTy(VoidTy, VF), VoidTy);
Type *WidenFFLoadRetTy =
toVectorizedTy(FFLoadRetTy, VF, Intrinsic::vp_load_ff);
EXPECT_EQ(cast<StructType>(WidenFFLoadRetTy)->getElementType(0),
VectorType::get(ITy, VF));
EXPECT_EQ(cast<StructType>(WidenFFLoadRetTy)->getElementType(1), ITy);
}

ElementCount ScalarVF = ElementCount::getFixed(1);
Expand Down