Skip to content

Commit e027017

Browse files
authored
[PatternMatch] Fix issue of stale reference in new m_{I,F,}Cmp matchers (#98866)
The new matchers don't output pred. Previously we where just creating a value on the stack and using it as a dummy output for the matchers, but this results in a stale reference upon return. To fix, this patch changes the output variable to a pointer, and passes in `nullptr` for the matchers that don't output `pred.`
1 parent e8e4060 commit e027017

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

llvm/include/llvm/IR/PatternMatch.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,23 +1550,27 @@ template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
15501550
template <typename LHS_t, typename RHS_t, typename Class, typename PredicateTy,
15511551
bool Commutable = false>
15521552
struct CmpClass_match {
1553-
PredicateTy &Predicate;
1553+
PredicateTy *Predicate;
15541554
LHS_t L;
15551555
RHS_t R;
15561556

15571557
// The evaluation order is always stable, regardless of Commutability.
15581558
// The LHS is always matched first.
15591559
CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
1560-
: Predicate(Pred), L(LHS), R(RHS) {}
1560+
: Predicate(&Pred), L(LHS), R(RHS) {}
1561+
CmpClass_match(const LHS_t &LHS, const RHS_t &RHS)
1562+
: Predicate(nullptr), L(LHS), R(RHS) {}
15611563

15621564
template <typename OpTy> bool match(OpTy *V) {
15631565
if (auto *I = dyn_cast<Class>(V)) {
15641566
if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
1565-
Predicate = I->getPredicate();
1567+
if (Predicate)
1568+
*Predicate = I->getPredicate();
15661569
return true;
15671570
} else if (Commutable && L.match(I->getOperand(1)) &&
15681571
R.match(I->getOperand(0))) {
1569-
Predicate = I->getSwappedPredicate();
1572+
if (Predicate)
1573+
*Predicate = I->getSwappedPredicate();
15701574
return true;
15711575
}
15721576
}
@@ -1595,22 +1599,19 @@ m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
15951599
template <typename LHS, typename RHS>
15961600
inline CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>
15971601
m_Cmp(const LHS &L, const RHS &R) {
1598-
CmpInst::Predicate Unused;
1599-
return CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>(Unused, L, R);
1602+
return CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>(L, R);
16001603
}
16011604

16021605
template <typename LHS, typename RHS>
16031606
inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
16041607
m_ICmp(const LHS &L, const RHS &R) {
1605-
ICmpInst::Predicate Unused;
1606-
return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>(Unused, L, R);
1608+
return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>(L, R);
16071609
}
16081610

16091611
template <typename LHS, typename RHS>
16101612
inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
16111613
m_FCmp(const LHS &L, const RHS &R) {
1612-
FCmpInst::Predicate Unused;
1613-
return CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>(Unused, L, R);
1614+
return CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>(L, R);
16141615
}
16151616

16161617
// Same as CmpClass, but instead of saving Pred as out output variable, match a

llvm/unittests/IR/PatternMatch.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,7 +2235,7 @@ typedef ::testing::Types<std::tuple<Value*, Instruction*>,
22352235
MutableConstTestTypes;
22362236
TYPED_TEST_SUITE(MutableConstTest, MutableConstTestTypes, );
22372237

2238-
TYPED_TEST(MutableConstTest, /* FIXME: UAR bug */ DISABLED_ICmp) {
2238+
TYPED_TEST(MutableConstTest, ICmp) {
22392239
auto &IRB = PatternMatchTest::IRB;
22402240

22412241
typedef std::tuple_element_t<0, TypeParam> ValueType;
@@ -2319,7 +2319,7 @@ TYPED_TEST(MutableConstTest, /* FIXME: UAR bug */ DISABLED_ICmp) {
23192319
.match((InstructionType)IRB.CreateICmp(Pred, L, R)));
23202320
}
23212321

2322-
TYPED_TEST(MutableConstTest, /* FIXME: UAR bug */ DISABLED_FCmp) {
2322+
TYPED_TEST(MutableConstTest, FCmp) {
23232323
auto &IRB = PatternMatchTest::IRB;
23242324

23252325
typedef std::tuple_element_t<0, TypeParam> ValueType;

0 commit comments

Comments
 (0)