[PATCH] D73669: [ConstantFold][SVE] Do not iterate on scalable vector for ConstantFoldSelectInstruction.

Huihui Zhang via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 29 15:57:19 PST 2020


huihuiz created this revision.
huihuiz added reviewers: sdesmalen, efriedma, apazos, huntergr, willlovett.
huihuiz added a project: LLVM.
Herald added subscribers: psnobl, rkruppe, hiraditya, kristof.beyls, tschuett.

Scanning through ConstantFoldSelectInstruction, we should not fold elementwise for scalable vectors.
Although I cannot create a crashing test case for select instruction, this will require select condition
to be a ConstantVector also scalable.

Add a positive test case, as the rest of the foldings in ConstantFoldSelectInstruction is harmless for scalable vector.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D73669

Files:
  llvm/lib/IR/ConstantFold.cpp
  llvm/test/Analysis/ConstantFolding/vscale.ll


Index: llvm/test/Analysis/ConstantFolding/vscale.ll
===================================================================
--- llvm/test/Analysis/ConstantFolding/vscale.ll
+++ llvm/test/Analysis/ConstantFolding/vscale.ll
@@ -153,3 +153,15 @@
   %r = xor <vscale x 4 x i32> undef, undef
   ret <vscale x 4 x i32> %r
 }
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; Other Operations
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+define <vscale x 4 x i32> @select() {
+; CHECK-LABEL: @select(
+; CHECK-NEXT:    ret <vscale x 4 x i32> undef
+;
+  %r = select <vscale x 4 x i1> undef, <vscale x 4 x i32> zeroinitializer, <vscale x 4 x i32> undef
+  ret <vscale x 4 x i32> %r
+}
Index: llvm/lib/IR/ConstantFold.cpp
===================================================================
--- llvm/lib/IR/ConstantFold.cpp
+++ llvm/lib/IR/ConstantFold.cpp
@@ -736,32 +736,37 @@
   if (Cond->isNullValue()) return V2;
   if (Cond->isAllOnesValue()) return V1;
 
-  // If the condition is a vector constant, fold the result elementwise.
-  if (ConstantVector *CondV = dyn_cast<ConstantVector>(Cond)) {
-    SmallVector<Constant*, 16> Result;
-    Type *Ty = IntegerType::get(CondV->getContext(), 32);
-    for (unsigned i = 0, e = V1->getType()->getVectorNumElements(); i != e;++i){
-      Constant *V;
-      Constant *V1Element = ConstantExpr::getExtractElement(V1,
-                                                    ConstantInt::get(Ty, i));
-      Constant *V2Element = ConstantExpr::getExtractElement(V2,
-                                                    ConstantInt::get(Ty, i));
-      auto *Cond = cast<Constant>(CondV->getOperand(i));
-      if (V1Element == V2Element) {
-        V = V1Element;
-      } else if (isa<UndefValue>(Cond)) {
-        V = isa<UndefValue>(V1Element) ? V1Element : V2Element;
-      } else {
-        if (!isa<ConstantInt>(Cond)) break;
-        V = Cond->isNullValue() ? V2Element : V1Element;
+  // Do not iterate on scalable vector. The number of elements is unknown at
+  // compile-time.
+  if (!V1->getType()->getVectorIsScalable())
+    // If the condition is a vector constant, fold the result elementwise.
+    if (ConstantVector *CondV = dyn_cast<ConstantVector>(Cond)) {
+      SmallVector<Constant *, 16> Result;
+      Type *Ty = IntegerType::get(CondV->getContext(), 32);
+      for (unsigned i = 0, e = V1->getType()->getVectorNumElements(); i != e;
+           ++i) {
+        Constant *V;
+        Constant *V1Element =
+            ConstantExpr::getExtractElement(V1, ConstantInt::get(Ty, i));
+        Constant *V2Element =
+            ConstantExpr::getExtractElement(V2, ConstantInt::get(Ty, i));
+        auto *Cond = cast<Constant>(CondV->getOperand(i));
+        if (V1Element == V2Element) {
+          V = V1Element;
+        } else if (isa<UndefValue>(Cond)) {
+          V = isa<UndefValue>(V1Element) ? V1Element : V2Element;
+        } else {
+          if (!isa<ConstantInt>(Cond))
+            break;
+          V = Cond->isNullValue() ? V2Element : V1Element;
+        }
+        Result.push_back(V);
       }
-      Result.push_back(V);
-    }
 
-    // If we were able to build the vector, return it.
-    if (Result.size() == V1->getType()->getVectorNumElements())
-      return ConstantVector::get(Result);
-  }
+      // If we were able to build the vector, return it.
+      if (Result.size() == V1->getType()->getVectorNumElements())
+        return ConstantVector::get(Result);
+    }
 
   if (isa<UndefValue>(Cond)) {
     if (isa<UndefValue>(V1)) return V1;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D73669.241307.patch
Type: text/x-patch
Size: 3606 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200129/0e0fcf25/attachment.bin>


More information about the llvm-commits mailing list