Skip to content

Commit 0723828

Browse files
committed
GlobalISel: Fix moreElementsToNextPow2
This was completely broken. The condition was inverted, and changed the element type for vectors of pointers. Fixes bug 40592. llvm-svn: 353069
1 parent 5745c5f commit 0723828

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

llvm/lib/CodeGen/GlobalISel/LegalityPredicates.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ LegalityPredicate LegalityPredicates::memSizeInBytesNotPow2(unsigned MMOIdx) {
115115

116116
LegalityPredicate LegalityPredicates::numElementsNotPow2(unsigned TypeIdx) {
117117
return [=](const LegalityQuery &Query) {
118-
const LLT &QueryTy = Query.Types[TypeIdx];
119-
return QueryTy.isVector() && isPowerOf2_32(QueryTy.getNumElements());
118+
const LLT QueryTy = Query.Types[TypeIdx];
119+
return QueryTy.isVector() && !isPowerOf2_32(QueryTy.getNumElements());
120120
};
121121
}
122122

llvm/lib/CodeGen/GlobalISel/LegalizeMutations.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@ LegalizeMutation LegalizeMutations::widenScalarToNextPow2(unsigned TypeIdx,
4040
LegalizeMutation LegalizeMutations::moreElementsToNextPow2(unsigned TypeIdx,
4141
unsigned Min) {
4242
return [=](const LegalityQuery &Query) {
43-
const LLT &VecTy = Query.Types[TypeIdx];
44-
unsigned NewNumElements = 1 << Log2_32_Ceil(VecTy.getNumElements());
45-
if (NewNumElements < Min)
46-
NewNumElements = Min;
47-
return std::make_pair(
48-
TypeIdx, LLT::vector(NewNumElements, VecTy.getScalarSizeInBits()));
43+
const LLT VecTy = Query.Types[TypeIdx];
44+
unsigned NewNumElements =
45+
std::max(1u << Log2_32_Ceil(VecTy.getNumElements()), Min);
46+
return std::make_pair(TypeIdx,
47+
LLT::vector(NewNumElements, VecTy.getElementType()));
4948
};
5049
}
5150

llvm/unittests/CodeGen/GlobalISel/LegalizerInfoTest.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ operator<<(std::ostream &OS, const llvm::LLT Ty) {
4141
OS << SS.str();
4242
return OS;
4343
}
44+
45+
std::ostream &operator<<(std::ostream &OS, const llvm::LegalizeActionStep Ty) {
46+
OS << "LegalizeActionStep(" << Ty.Action << ", " << Ty.TypeIdx << ", "
47+
<< Ty.NewType << ')';
48+
return OS;
49+
}
4450
}
4551

4652
namespace {
@@ -198,3 +204,37 @@ TEST(LegalizerInfoTest, SizeChangeStrategy) {
198204
LegalizeActionStep(Unsupported, 0, LLT::scalar(33)));
199205
}
200206
}
207+
208+
#define EXPECT_ACTION(Action, Index, Type, Query) \
209+
do { \
210+
auto A = LI.getAction(Query); \
211+
EXPECT_EQ(LegalizeActionStep(Action, Index, Type), A) << A; \
212+
} while (0)
213+
214+
TEST(LegalizerInfoTest, RuleSets) {
215+
using namespace TargetOpcode;
216+
217+
const LLT s32 = LLT::scalar(32);
218+
219+
const LLT v2s32 = LLT::vector(2, 32);
220+
const LLT v3s32 = LLT::vector(3, 32);
221+
const LLT v4s32 = LLT::vector(4, 32);
222+
223+
const LLT p0 = LLT::pointer(0, 32);
224+
const LLT v3p0 = LLT::vector(3, p0);
225+
const LLT v4p0 = LLT::vector(4, p0);
226+
227+
{
228+
LegalizerInfo LI;
229+
230+
LI.getActionDefinitionsBuilder(G_IMPLICIT_DEF)
231+
.legalFor({v4s32, v4p0})
232+
.moreElementsToNextPow2(0);
233+
LI.computeTables();
234+
235+
EXPECT_ACTION(Unsupported, 0, LLT(), LegalityQuery(G_IMPLICIT_DEF, {s32}));
236+
EXPECT_ACTION(Unsupported, 0, LLT(), LegalityQuery(G_IMPLICIT_DEF, {v2s32}));
237+
EXPECT_ACTION(MoreElements, 0, v4p0, LegalityQuery(G_IMPLICIT_DEF, {v3p0}));
238+
EXPECT_ACTION(MoreElements, 0, v4s32, LegalityQuery(G_IMPLICIT_DEF, {v3s32}));
239+
}
240+
}

0 commit comments

Comments
 (0)