Skip to content

Commit 80e0bd1

Browse files
[SVE][IR] Fix Binary op matching in PatternMatch::m_VScale
Reviewed By: sdesmalen Differential Revision: https://reviews.llvm.org/D105978
1 parent 424fe90 commit 80e0bd1

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

llvm/include/llvm/IR/PatternMatch.h

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2421,27 +2421,23 @@ inline InsertValue_match<Ind, Val_t, Elt_t> m_InsertValue(const Val_t &Val,
24212421
/// `ptrtoint(gep <vscale x 1 x i8>, <vscale x 1 x i8>* null, i32 1>`
24222422
/// under the right conditions determined by DataLayout.
24232423
struct VScaleVal_match {
2424-
private:
2425-
template <typename Base, typename Offset>
2426-
inline BinaryOp_match<Base, Offset, Instruction::GetElementPtr>
2427-
m_OffsetGep(const Base &B, const Offset &O) {
2428-
return BinaryOp_match<Base, Offset, Instruction::GetElementPtr>(B, O);
2429-
}
2430-
2431-
public:
24322424
const DataLayout &DL;
24332425
VScaleVal_match(const DataLayout &DL) : DL(DL) {}
24342426

24352427
template <typename ITy> bool match(ITy *V) {
24362428
if (m_Intrinsic<Intrinsic::vscale>().match(V))
24372429
return true;
24382430

2439-
if (m_PtrToInt(m_OffsetGep(m_Zero(), m_SpecificInt(1))).match(V)) {
2440-
auto *GEP = cast<GEPOperator>(cast<Operator>(V)->getOperand(0));
2441-
auto *DerefTy = GEP->getSourceElementType();
2442-
if (isa<ScalableVectorType>(DerefTy) &&
2443-
DL.getTypeAllocSizeInBits(DerefTy).getKnownMinSize() == 8)
2444-
return true;
2431+
Value *Ptr;
2432+
if (m_PtrToInt(m_Value(Ptr)).match(V)) {
2433+
if (auto *GEP = dyn_cast<GEPOperator>(Ptr)) {
2434+
auto *DerefTy = GEP->getSourceElementType();
2435+
if (GEP->getNumIndices() == 1 && isa<ScalableVectorType>(DerefTy) &&
2436+
m_Zero().match(GEP->getPointerOperand()) &&
2437+
m_SpecificInt(1).match(GEP->idx_begin()->get()) &&
2438+
DL.getTypeAllocSizeInBits(DerefTy).getKnownMinSize() == 8)
2439+
return true;
2440+
}
24452441
}
24462442

24472443
return false;

llvm/unittests/IR/PatternMatch.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,6 +1636,26 @@ TEST_F(PatternMatchTest, InsertValue) {
16361636
EXPECT_FALSE(match(IRB.getInt64(99), m_InsertValue<0>(m_Value(), m_Value())));
16371637
}
16381638

1639+
TEST_F(PatternMatchTest, VScale) {
1640+
DataLayout DL = M->getDataLayout();
1641+
1642+
Type *VecTy = ScalableVectorType::get(IRB.getInt8Ty(), 1);
1643+
Type *VecPtrTy = VecTy->getPointerTo();
1644+
Value *NullPtrVec = Constant::getNullValue(VecPtrTy);
1645+
Value *GEP = IRB.CreateGEP(VecTy, NullPtrVec, IRB.getInt64(1));
1646+
Value *PtrToInt = IRB.CreatePtrToInt(GEP, DL.getIntPtrType(GEP->getType()));
1647+
EXPECT_TRUE(match(PtrToInt, m_VScale(DL)));
1648+
1649+
// Prior to this patch, this case would cause assertion failures when attempting to match m_VScale
1650+
Type *VecTy2 = ScalableVectorType::get(IRB.getInt8Ty(), 2);
1651+
Value *NullPtrVec2 = Constant::getNullValue(VecTy2->getPointerTo());
1652+
Value *BitCast = IRB.CreateBitCast(NullPtrVec2, VecPtrTy);
1653+
Value *GEP2 = IRB.CreateGEP(VecTy, BitCast, IRB.getInt64(1));
1654+
Value *PtrToInt2 =
1655+
IRB.CreatePtrToInt(GEP2, DL.getIntPtrType(GEP2->getType()));
1656+
EXPECT_FALSE(match(PtrToInt2, m_VScale(DL)));
1657+
}
1658+
16391659
template <typename T> struct MutableConstTest : PatternMatchTest { };
16401660

16411661
typedef ::testing::Types<std::tuple<Value*, Instruction*>,

0 commit comments

Comments
 (0)