[PATCH] D75307: [CallPromotionUtils] Add missing promotion legality check to tryPromoteCall.

Hiroshi Yamauchi via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 27 14:41:49 PST 2020


yamauchi created this revision.
yamauchi added a reviewer: davidxl.
Herald added a subscriber: hiraditya.
Herald added a project: LLVM.

This fixes the crash that led to the revert of D69591 <https://reviews.llvm.org/D69591>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D75307

Files:
  llvm/lib/Transforms/Utils/CallPromotionUtils.cpp
  llvm/unittests/Transforms/Utils/CallPromotionUtilsTest.cpp


Index: llvm/unittests/Transforms/Utils/CallPromotionUtilsTest.cpp
===================================================================
--- llvm/unittests/Transforms/Utils/CallPromotionUtilsTest.cpp
+++ llvm/unittests/Transforms/Utils/CallPromotionUtilsTest.cpp
@@ -330,3 +330,50 @@
   F = dyn_cast<Function>(GV);
   EXPECT_EQ(F, CS2.getCalledFunction());
 }
+
+// Check that it isn't crashing due to missing promotion legality.
+TEST(CallPromotionUtilsTest, TryPromoteCall_Legality) {
+  LLVMContext C;
+  std::unique_ptr<Module> M = parseIR(C,
+                                      R"IR(
+%struct1 = type <{ i32, i64 }>
+%struct2 = type <{ i32, i64 }>
+
+%class.Impl = type <{ %class.Interface, i32, [4 x i8] }>
+%class.Interface = type { i32 (...)** }
+
+ at _ZTV4Impl = constant { [3 x i8*] } { [3 x i8*] [i8* null, i8* null, i8* bitcast (%struct2 (%class.Impl*)* @_ZN4Impl3RunEv to i8*)] }
+
+define %struct1 @f() {
+entry:
+  %o = alloca %class.Impl
+  %base = getelementptr %class.Impl, %class.Impl* %o, i64 0, i32 0, i32 0
+  store i32 (...)** bitcast (i8** getelementptr inbounds ({ [3 x i8*] }, { [3 x i8*] }* @_ZTV4Impl, i64 0, inrange i32 0, i64 2) to i32 (...)**), i32 (...)*** %base
+  %f = getelementptr inbounds %class.Impl, %class.Impl* %o, i64 0, i32 1
+  store i32 3, i32* %f
+  %base.i = getelementptr inbounds %class.Impl, %class.Impl* %o, i64 0, i32 0
+  %c = bitcast %class.Interface* %base.i to %struct1 (%class.Interface*)***
+  %vtable.i = load %struct1 (%class.Interface*)**, %struct1 (%class.Interface*)*** %c
+  %fp = load %struct1 (%class.Interface*)*, %struct1 (%class.Interface*)** %vtable.i
+  %rv = call %struct1 %fp(%class.Interface* nonnull %base.i)
+  ret %struct1 %rv
+}
+
+declare %struct2 @_ZN4Impl3RunEv(%class.Impl* %this)
+)IR");
+
+  auto *GV = M->getNamedValue("f");
+  ASSERT_TRUE(GV);
+  auto *F = dyn_cast<Function>(GV);
+  ASSERT_TRUE(F);
+  Instruction *Inst = &F->front().front();
+  auto *AI = dyn_cast<AllocaInst>(Inst);
+  ASSERT_TRUE(AI);
+  Inst = &*++F->front().rbegin();
+  auto *CI = dyn_cast<CallInst>(Inst);
+  ASSERT_TRUE(CI);
+  CallSite CS(CI);
+  ASSERT_FALSE(CS.getCalledFunction());
+  bool IsPromoted = tryPromoteCall(CS);
+  EXPECT_FALSE(IsPromoted);
+}
Index: llvm/lib/Transforms/Utils/CallPromotionUtils.cpp
===================================================================
--- llvm/lib/Transforms/Utils/CallPromotionUtils.cpp
+++ llvm/lib/Transforms/Utils/CallPromotionUtils.cpp
@@ -511,6 +511,9 @@
   if (!DirectCallee)
     return false; // No function pointer found.
 
+  if (!isLegalToPromote(CS, DirectCallee))
+    return false;
+
   // Success.
   promoteCall(CS, DirectCallee);
   return true;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D75307.247113.patch
Type: text/x-patch
Size: 2672 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200227/b76ea48f/attachment.bin>


More information about the llvm-commits mailing list