[PATCH] D109053: [BitcodeReader] Delay select until all constants resolved

guopeilin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 8 05:19:56 PDT 2021


guopeilin updated this revision to Diff 371319.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D109053/new/

https://reviews.llvm.org/D109053

Files:
  llvm/lib/Bitcode/Reader/BitcodeReader.cpp
  llvm/test/Bitcode/bitcode-parseconstant-delay-select.ll


Index: llvm/test/Bitcode/bitcode-parseconstant-delay-select.ll
===================================================================
--- /dev/null
+++ llvm/test/Bitcode/bitcode-parseconstant-delay-select.ll
@@ -0,0 +1,13 @@
+; RUN: llvm-as < %s -preserve-bc-uselistorder=false | llvm-dis -disable-output
+ at a = external dso_local global i32, align 4
+ at c = external dso_local global [3 x i32], align 4
+ at b = external dso_local local_unnamed_addr global i32, align 4
+
+define dso_local i32 @main() local_unnamed_addr {
+middle.block:
+  br label %for.cond.for.end_crit_edge
+
+for.cond.for.end_crit_edge:                       ; preds = %middle.block
+  store i32 extractelement (<4 x i32> select (<4 x i1> select (<4 x i1> <i1 icmp sle (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 0), i1 icmp sle (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 0), i1 icmp sle (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 0), i1 icmp sle (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 0)>, <4 x i1> <i1 icmp sge (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 0), i1 icmp sge (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 0), i1 icmp sge (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 0), i1 icmp sge (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 0)>, <4 x i1> zeroinitializer), <4 x i32> zeroinitializer, <4 x i32> select (<4 x i1> select (<4 x i1> <i1 icmp sgt (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 0), i1 icmp sgt (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 0), i1 icmp sgt (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 0), i1 icmp sgt (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 0)>, <4 x i1> <i1 icmp eq (i32 sdiv (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 3), i32 0), i1 icmp eq (i32 sdiv (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 3), i32 0), i1 icmp eq (i32 sdiv (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 3), i32 0), i1 icmp eq (i32 sdiv (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 3), i32 0)>, <4 x i1> zeroinitializer), <4 x i32> <i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32)>, <4 x i32> <i32 lshr (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 31), i32 lshr (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 31), i32 lshr (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 31), i32 lshr (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 31)>)), i32 3), i32* @b, align 4
+  ret i32 undef
+}
Index: llvm/lib/Bitcode/Reader/BitcodeReader.cpp
===================================================================
--- llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -2353,6 +2353,15 @@
     unsigned CstNo;
   };
   std::vector<DelayedShufTy> DelayedShuffles;
+  struct DelayedSelTy {
+    Type *OpTy;
+    uint64_t Op0Idx;
+    uint64_t Op1Idx;
+    uint64_t Op2Idx;
+    unsigned CstNo;
+  };
+  std::vector<DelayedSelTy> DelayedSelectors;
+
   while (true) {
     Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
     if (!MaybeEntry)
@@ -2389,6 +2398,27 @@
         Value *V = ConstantExpr::getShuffleVector(Op0, Op1, Mask);
         ValueList.assignValue(V, CstNo);
       }
+      for (auto &DelayedSelector : DelayedSelectors) {
+        Type *OpTy = DelayedSelector.OpTy;
+        Type *SelectorTy = Type::getInt1Ty(Context);
+        uint64_t Op0Idx = DelayedSelector.Op0Idx;
+        uint64_t Op1Idx = DelayedSelector.Op1Idx;
+        uint64_t Op2Idx = DelayedSelector.Op2Idx;
+        uint64_t CstNo = DelayedSelector.CstNo;
+        Constant *Op1 = ValueList.getConstantFwdRef(Op1Idx, OpTy);
+        Constant *Op2 = ValueList.getConstantFwdRef(Op2Idx, OpTy);
+        // The selector might be an i1 or an <n x i1>
+        // Get the type from the ValueList before getting a forward ref.
+        if (VectorType *VTy = dyn_cast<VectorType>(OpTy)) {
+          Value *V = ValueList[Op0Idx];
+          assert(V);
+          if (SelectorTy != V->getType())
+            SelectorTy = VectorType::get(SelectorTy, VTy->getElementCount());
+        }
+        Constant *Op0 = ValueList.getConstantFwdRef(Op0Idx, SelectorTy);
+        Value *V = ConstantExpr::getSelect(Op0, Op1, Op2);
+        ValueList.assignValue(V, CstNo);
+      }
 
       if (NextCstNo != ValueList.size())
         return error("Invalid constant reference");
@@ -2685,21 +2715,11 @@
       if (Record.size() < 3)
         return error("Invalid record");
 
-      Type *SelectorTy = Type::getInt1Ty(Context);
-
-      // The selector might be an i1, an <n x i1>, or a <vscale x n x i1>
-      // Get the type from the ValueList before getting a forward ref.
-      if (VectorType *VTy = dyn_cast<VectorType>(CurTy))
-        if (Value *V = ValueList[Record[0]])
-          if (SelectorTy != V->getType())
-            SelectorTy = VectorType::get(SelectorTy,
-                                         VTy->getElementCount());
-
-      V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0],
-                                                              SelectorTy),
-                                  ValueList.getConstantFwdRef(Record[1],CurTy),
-                                  ValueList.getConstantFwdRef(Record[2],CurTy));
-      break;
+      DelayedSelectors.push_back(
+          {CurTy, Record[0], Record[1], Record[2], NextCstNo});
+      (void)ValueList.getConstantFwdRef(NextCstNo, CurTy);
+      ++NextCstNo;
+      continue;
     }
     case bitc::CST_CODE_CE_EXTRACTELT
         : { // CE_EXTRACTELT: [opty, opval, opty, opval]


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D109053.371319.patch
Type: text/x-patch
Size: 7168 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210908/80d1516d/attachment.bin>


More information about the llvm-commits mailing list