Skip to content

Commit 349b678

Browse files
committed
Fixups
1 parent 16afa09 commit 349b678

File tree

9 files changed

+149
-135
lines changed

9 files changed

+149
-135
lines changed

llvm/include/llvm/IR/CallWideningUtils.h

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===---- StructWideningUtils.h - Utils for widening scalar to vector calls
2+
//--==//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef LLVM_IR_STRUCTWIDENINGUTILS_H
11+
#define LLVM_IR_STRUCTWIDENINGUTILS_H
12+
13+
#include "llvm/ADT/SmallVector.h"
14+
#include "llvm/IR/DerivedTypes.h"
15+
16+
namespace llvm {
17+
18+
/// A helper for converting structs of scalar types to structs of vector types.
19+
/// Note: Only unpacked literal struct types are supported.
20+
Type *ToWideStructTy(StructType *StructTy, ElementCount EC);
21+
22+
/// A helper for converting structs of vector types to structs of scalar types.
23+
/// Note: Only unpacked literal struct types are supported.
24+
Type *ToNarrowStructTy(StructType *StructTy);
25+
26+
/// Returns true if `StructTy` is an unpacked literal struct where all elements
27+
/// are vectors of matching element count. This does not include empty structs.
28+
bool isWideStructTy(StructType *StructTy);
29+
30+
} // namespace llvm
31+
32+
#endif

llvm/include/llvm/IR/VectorUtils.h

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===----------- VectorUtils.h - Vector type utility functions -*- C++ -*-===//
1+
//===----------- VectorUtils.h - Vector type utility functions -*- C++ -*-====//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -9,8 +9,8 @@
99
#ifndef LLVM_IR_VECTORUTILS_H
1010
#define LLVM_IR_VECTORUTILS_H
1111

12-
#include "llvm/ADT/SmallVector.h"
1312
#include "llvm/IR/DerivedTypes.h"
13+
#include "llvm/IR/StructWideningUtils.h"
1414

1515
namespace llvm {
1616

@@ -27,6 +27,48 @@ inline Type *ToVectorTy(Type *Scalar, unsigned VF) {
2727
return ToVectorTy(Scalar, ElementCount::getFixed(VF));
2828
}
2929

30+
/// A helper for converting to wider (vector) types. For scalar types, this is
31+
/// equivalent to calling `ToVectorTy`. For struct types, this returns a new
32+
/// struct where each element type has been widened to a vector type. Note: Only
33+
/// unpacked literal struct types are supported.
34+
inline Type *ToWideTy(Type *Ty, ElementCount EC) {
35+
if (StructType *StructTy = dyn_cast<StructType>(Ty))
36+
return ToWideStructTy(StructTy, EC);
37+
return ToVectorTy(Ty, EC);
38+
}
39+
40+
/// A helper for converting wide types to narrow (non-vector) types. For vector
41+
/// types, this is equivalent to calling .getScalarType(). For struct types,
42+
/// this returns a new struct where each element type has been converted to a
43+
/// scalar type. Note: Only unpacked literal struct types are supported.
44+
inline Type *ToNarrowTy(Type *Ty) {
45+
if (StructType *StructTy = dyn_cast<StructType>(Ty))
46+
return ToNarrowStructTy(StructTy);
47+
return Ty->getScalarType();
48+
}
49+
50+
/// Returns true if `Ty` is a vector type or a struct of vector types where all
51+
/// vector types share the same VF.
52+
inline bool isWideTy(Type *Ty) {
53+
if (StructType *StructTy = dyn_cast<StructType>(Ty))
54+
return isWideStructTy(StructTy);
55+
return Ty->isVectorTy();
56+
}
57+
58+
/// Returns the types contained in `Ty`. For struct types, it returns the
59+
/// elements, all other types are returned directly.
60+
inline ArrayRef<Type *> getContainedTypes(Type *const &Ty) {
61+
if (auto *StructTy = dyn_cast<StructType>(Ty))
62+
return StructTy->elements();
63+
return ArrayRef<Type *>{&Ty, 1};
64+
}
65+
66+
/// Returns the vectorization factor for a widened type.
67+
inline ElementCount getWideTypeVF(Type *Ty) {
68+
assert(isWideTy(Ty) && "expected widened type");
69+
return cast<VectorType>(getContainedTypes(Ty).front())->getElementCount();
70+
}
71+
3072
} // namespace llvm
3173

3274
#endif

llvm/lib/IR/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ add_llvm_component_library(LLVMCore
66
AutoUpgrade.cpp
77
BasicBlock.cpp
88
BuiltinGCs.cpp
9-
CallWideningUtils.cpp
109
Comdat.cpp
1110
ConstantFold.cpp
1211
ConstantFPRange.cpp
@@ -65,6 +64,7 @@ add_llvm_component_library(LLVMCore
6564
PseudoProbe.cpp
6665
ReplaceConstant.cpp
6766
Statepoint.cpp
67+
StructWideningUtils.cpp
6868
StructuralHash.cpp
6969
Type.cpp
7070
TypedPointerType.cpp

llvm/lib/IR/CallWideningUtils.cpp

Lines changed: 0 additions & 73 deletions
This file was deleted.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//===-- CallWideningUtils.cpp - Utils for widening scalar to vector calls --==//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/IR/StructWideningUtils.h"
10+
#include "llvm/ADT/SmallVectorExtras.h"
11+
#include "llvm/IR/VectorUtils.h"
12+
13+
using namespace llvm;
14+
15+
static bool isUnpackedStructLiteral(StructType *StructTy) {
16+
return StructTy->isLiteral() && !StructTy->isPacked();
17+
}
18+
19+
/// A helper for converting structs of scalar types to structs of vector types.
20+
/// Note: Only unpacked literal struct types are supported.
21+
Type *llvm::ToWideStructTy(StructType *StructTy, ElementCount EC) {
22+
if (EC.isScalar())
23+
return StructTy;
24+
assert(isUnpackedStructLiteral(StructTy) &&
25+
"expected unpacked struct literal");
26+
return StructType::get(
27+
StructTy->getContext(),
28+
map_to_vector(StructTy->elements(), [&](Type *ElTy) -> Type * {
29+
return VectorType::get(ElTy, EC);
30+
}));
31+
}
32+
33+
/// A helper for converting structs of vector types to structs of scalar types.
34+
/// Note: Only unpacked literal struct types are supported.
35+
Type *llvm::ToNarrowStructTy(StructType *StructTy) {
36+
assert(isUnpackedStructLiteral(StructTy) &&
37+
"expected unpacked struct literal");
38+
return StructType::get(
39+
StructTy->getContext(),
40+
map_to_vector(StructTy->elements(), [](Type *ElTy) -> Type * {
41+
return ElTy->getScalarType();
42+
}));
43+
}
44+
45+
/// Returns true if `StructTy` is an unpacked literal struct where all elements
46+
/// are vectors of matching element count. This does not include empty structs.
47+
bool llvm::isWideStructTy(StructType *StructTy) {
48+
if (!isUnpackedStructLiteral(StructTy))
49+
return false;
50+
auto ElemTys = StructTy->elements();
51+
if (ElemTys.empty() || !ElemTys.front()->isVectorTy())
52+
return false;
53+
ElementCount VF = cast<VectorType>(ElemTys.front())->getElementCount();
54+
return all_of(ElemTys, [&](Type *Ty) {
55+
return Ty->isVectorTy() && cast<VectorType>(Ty)->getElementCount() == VF;
56+
});
57+
}

llvm/lib/IR/VFABIDemangler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
#include "llvm/ADT/SetVector.h"
1111
#include "llvm/ADT/SmallString.h"
1212
#include "llvm/ADT/StringSwitch.h"
13-
#include "llvm/IR/CallWideningUtils.h"
1413
#include "llvm/IR/Module.h"
14+
#include "llvm/IR/VectorUtils.h"
1515
#include "llvm/Support/Debug.h"
1616
#include "llvm/Support/raw_ostream.h"
1717
#include <limits>

llvm/unittests/IR/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ add_llvm_unittest(IRTests
1515
AttributesTest.cpp
1616
BasicBlockTest.cpp
1717
BasicBlockDbgInfoTest.cpp
18-
CallWideningUtilsTest.cpp
1918
CFGBuilder.cpp
2019
ConstantFPRangeTest.cpp
2120
ConstantRangeTest.cpp
@@ -53,6 +52,7 @@ add_llvm_unittest(IRTests
5352
ValueTest.cpp
5453
VectorBuilderTest.cpp
5554
VectorTypesTest.cpp
55+
VectorUtilsTest.cpp
5656
VerifierTest.cpp
5757
VFABIDemanglerTest.cpp
5858
VPIntrinsicTest.cpp

llvm/unittests/IR/CallWideningUtilsTest.cpp renamed to llvm/unittests/IR/VectorUtilsTest.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
//===------- CallWideningUtilsTest.cpp - Call widening utils tests --------===//
1+
//===------- VectorUtilsTest.cpp - Vector utils tests ---------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "llvm/IR/CallWideningUtils.h"
9+
#include "llvm/IR/VectorUtils.h"
1010
#include "llvm/IR/DerivedTypes.h"
1111
#include "llvm/IR/LLVMContext.h"
1212
#include "gtest/gtest.h"
@@ -15,9 +15,9 @@ using namespace llvm;
1515

1616
namespace {
1717

18-
class CallWideningUtilsTest : public ::testing::Test {};
18+
class VectorUtilsTest : public ::testing::Test {};
1919

20-
TEST(CallWideningUtilsTest, TestToWideTy) {
20+
TEST(VectorUtilsTest, TestToWideTy) {
2121
LLVMContext C;
2222

2323
Type *ITy = Type::getInt32Ty(C);
@@ -62,7 +62,7 @@ TEST(CallWideningUtilsTest, TestToWideTy) {
6262
}
6363
}
6464

65-
TEST(CallWideningUtilsTest, TestToNarrowTy) {
65+
TEST(VectorUtilsTest, TestToNarrowTy) {
6666
LLVMContext C;
6767

6868
Type *ITy = Type::getInt32Ty(C);
@@ -80,7 +80,7 @@ TEST(CallWideningUtilsTest, TestToNarrowTy) {
8080
}
8181
}
8282

83-
TEST(CallWideningUtilsTest, TestGetContainedTypes) {
83+
TEST(VectorUtilsTest, TestGetContainedTypes) {
8484
LLVMContext C;
8585

8686
Type *ITy = Type::getInt32Ty(C);
@@ -89,15 +89,15 @@ TEST(CallWideningUtilsTest, TestGetContainedTypes) {
8989
Type *MixedStructTy = StructType::get(FTy, ITy);
9090
Type *VoidTy = Type::getVoidTy(C);
9191

92-
EXPECT_EQ(getContainedTypes(ITy), SmallVector<Type *>({ITy}));
93-
EXPECT_EQ(getContainedTypes(FTy), SmallVector<Type *>({FTy}));
94-
EXPECT_EQ(getContainedTypes(VoidTy), SmallVector<Type *>({VoidTy}));
92+
EXPECT_EQ(getContainedTypes(ITy), ArrayRef<Type *>({ITy}));
93+
EXPECT_EQ(getContainedTypes(FTy), ArrayRef<Type *>({FTy}));
94+
EXPECT_EQ(getContainedTypes(VoidTy), ArrayRef<Type *>({VoidTy}));
9595
EXPECT_EQ(getContainedTypes(HomogeneousStructTy),
96-
SmallVector<Type *>({FTy, FTy, FTy}));
97-
EXPECT_EQ(getContainedTypes(MixedStructTy), SmallVector<Type *>({FTy, ITy}));
96+
ArrayRef<Type *>({FTy, FTy, FTy}));
97+
EXPECT_EQ(getContainedTypes(MixedStructTy), ArrayRef<Type *>({FTy, ITy}));
9898
}
9999

100-
TEST(CallWideningUtilsTest, TestIsWideTy) {
100+
TEST(VectorUtilsTest, TestIsWideTy) {
101101
LLVMContext C;
102102

103103
Type *ITy = Type::getInt32Ty(C);
@@ -130,7 +130,7 @@ TEST(CallWideningUtilsTest, TestIsWideTy) {
130130
EXPECT_FALSE(isWideTy(PackedWideStruct));
131131
}
132132

133-
TEST(CallWideningUtilsTest, TestGetWideTypeVF) {
133+
TEST(VectorUtilsTest, TestGetWideTypeVF) {
134134
LLVMContext C;
135135

136136
Type *ITy = Type::getInt32Ty(C);

0 commit comments

Comments
 (0)