[PatternMatch] don't match a scalar select of bool vectors as a logical-and or logical-or
authorSanjay Patel <spatel@rotateright.com>
Tue, 1 Nov 2022 18:35:56 +0000 (14:35 -0400)
committerSanjay Patel <spatel@rotateright.com>
Tue, 1 Nov 2022 18:50:18 +0000 (14:50 -0400)
Most folds based on these matchers already check to make sure the
condition type is the same as the select type, and it seems unlikely
that a fold would want to handle a scalar-select-of-vectors pattern
(there are no regression tests for it).

This is a preliminary step for fixing #issue 58552. The fold(s)
responsible for that crash (D101807, D101375) don't use the matchers
yet, but they probably should.

Differential Revision: https://reviews.llvm.org/D137170

llvm/include/llvm/IR/PatternMatch.h
llvm/unittests/IR/PatternMatch.cpp

index 31ff63c..2901c64 100644 (file)
@@ -2513,6 +2513,12 @@ struct LogicalOp_match {
       auto *Cond = Select->getCondition();
       auto *TVal = Select->getTrueValue();
       auto *FVal = Select->getFalseValue();
+
+      // Don't match a scalar select of bool vectors.
+      // Transforms expect a single type for operands if this matches.
+      if (Cond->getType() != Select->getType())
+        return false;
+
       if (Opcode == Instruction::And) {
         auto *C = dyn_cast<Constant>(FVal);
         if (C && C->isNullValue())
index d2ec259..ab35df0 100644 (file)
@@ -1730,10 +1730,12 @@ TEST_F(PatternMatchTest, VectorLogicalSelects) {
   // select i1 Scalar, <3 x i1> <i1 1, i1 1, i1 1>, <3 x i1> Vector
   Value *MixedTypeOr = IRB.CreateSelect(Scalar, T, Vector);
 
+  // We allow matching a real vector logical select,
+  // but not a scalar select of vector bools.
   EXPECT_TRUE(match(VecAnd, m_LogicalAnd(m_Value(), m_Value())));
-  EXPECT_TRUE(match(MixedTypeAnd, m_LogicalAnd(m_Value(), m_Value())));
+  EXPECT_FALSE(match(MixedTypeAnd, m_LogicalAnd(m_Value(), m_Value())));
   EXPECT_TRUE(match(VecOr, m_LogicalOr(m_Value(), m_Value())));
-  EXPECT_TRUE(match(MixedTypeOr, m_LogicalOr(m_Value(), m_Value())));
+  EXPECT_FALSE(match(MixedTypeOr, m_LogicalOr(m_Value(), m_Value())));
 }
 
 TEST_F(PatternMatchTest, VScale) {