[llvm] 5668eda - Revert "[CGSCC] Detect devirtualization in more cases"

Arthur Eubanks via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 23 09:54:47 PDT 2020


Author: Arthur Eubanks
Date: 2020-10-23T09:53:52-07:00
New Revision: 5668eda864e36baed92854fd13a89042d978ffa4

URL: https://github.com/llvm/llvm-project/commit/5668eda864e36baed92854fd13a89042d978ffa4
DIFF: https://github.com/llvm/llvm-project/commit/5668eda864e36baed92854fd13a89042d978ffa4.diff

LOG: Revert "[CGSCC] Detect devirtualization in more cases"

This reverts commit 3024fe5b55ed72633915f613bd5e2826583c396f.

Causes major compile time regressions:
https://llvm-compile-time-tracker.com/compare.php?from=3b8d8954bf2c192502d757019b9fe434864068e9&to=3024fe5b55ed72633915f613bd5e2826583c396f&stat=instructions

Added: 
    

Modified: 
    llvm/include/llvm/Analysis/CGSCCPassManager.h
    llvm/lib/Analysis/CGSCCPassManager.cpp
    llvm/test/Transforms/Inline/devirtualize-3.ll
    llvm/test/Transforms/Inline/devirtualize.ll

Removed: 
    llvm/test/Transforms/Inline/devirtualize-5.ll


################################################################################
diff  --git a/llvm/include/llvm/Analysis/CGSCCPassManager.h b/llvm/include/llvm/Analysis/CGSCCPassManager.h
index 5f2e50e922c0..755bc92ddccf 100644
--- a/llvm/include/llvm/Analysis/CGSCCPassManager.h
+++ b/llvm/include/llvm/Analysis/CGSCCPassManager.h
@@ -314,9 +314,6 @@ struct CGSCCUpdateResult {
   /// for a better technique.
   SmallDenseSet<std::pair<LazyCallGraph::Node *, LazyCallGraph::SCC *>, 4>
       &InlinedInternalEdges;
-
-  /// If any calls in the current SCC were devirtualized.
-  bool Devirtualized;
 };
 
 /// The core module pass which does a post-order walk of the SCCs and
@@ -599,6 +596,9 @@ class DevirtSCCRepeatedPass
     // a pointer that we can update.
     LazyCallGraph::SCC *C = &InitialC;
 
+    // Collect value handles for all of the indirect call sites.
+    SmallVector<WeakTrackingVH, 8> CallHandles;
+
     // Struct to track the counts of direct and indirect calls in each function
     // of the SCC.
     struct CallCount {
@@ -608,7 +608,10 @@ class DevirtSCCRepeatedPass
 
     // Put value handles on all of the indirect calls and return the number of
     // direct calls for each function in the SCC.
-    auto ScanSCC = [](LazyCallGraph::SCC &C) {
+    auto ScanSCC = [](LazyCallGraph::SCC &C,
+                      SmallVectorImpl<WeakTrackingVH> &CallHandles) {
+      assert(CallHandles.empty() && "Must start with a clear set of handles.");
+
       SmallDenseMap<Function *, CallCount> CallCounts;
       CallCount CountLocal = {0, 0};
       for (LazyCallGraph::Node &N : C) {
@@ -621,6 +624,7 @@ class DevirtSCCRepeatedPass
               ++Count.Direct;
             } else {
               ++Count.Indirect;
+              CallHandles.push_back(WeakTrackingVH(&I));
             }
           }
       }
@@ -629,14 +633,13 @@ class DevirtSCCRepeatedPass
     };
 
     // Populate the initial call handles and get the initial call counts.
-    auto CallCounts = ScanSCC(*C);
+    auto CallCounts = ScanSCC(*C, CallHandles);
 
     for (int Iteration = 0;; ++Iteration) {
+
       if (!PI.runBeforePass<LazyCallGraph::SCC>(Pass, *C))
         continue;
 
-      UR.Devirtualized = false;
-
       PreservedAnalyses PassPA = Pass.run(*C, AM, CG, UR);
 
       if (UR.InvalidatedSCCs.count(C))
@@ -655,12 +658,34 @@ class DevirtSCCRepeatedPass
       assert(!UR.InvalidatedSCCs.count(C) && "Processing an invalid SCC!");
       assert(C->begin() != C->end() && "Cannot have an empty SCC!");
 
-      bool Devirt = UR.Devirtualized;
+      // Check whether any of the handles were devirtualized.
+      auto IsDevirtualizedHandle = [&](WeakTrackingVH &CallH) {
+        if (!CallH)
+          return false;
+        auto *CB = dyn_cast<CallBase>(CallH);
+        if (!CB)
+          return false;
+
+        // If the call is still indirect, leave it alone.
+        Function *F = CB->getCalledFunction();
+        if (!F)
+          return false;
+
+        LLVM_DEBUG(dbgs() << "Found devirtualized call from "
+                          << CB->getParent()->getParent()->getName() << " to "
+                          << F->getName() << "\n");
+
+        // We now have a direct call where previously we had an indirect call,
+        // so iterate to process this devirtualization site.
+        return true;
+      };
+      bool Devirt = llvm::any_of(CallHandles, IsDevirtualizedHandle);
 
       // Rescan to build up a new set of handles and count how many direct
       // calls remain. If we decide to iterate, this also sets up the input to
       // the next iteration.
-      auto NewCallCounts = ScanSCC(*C);
+      CallHandles.clear();
+      auto NewCallCounts = ScanSCC(*C, CallHandles);
 
       // If we haven't found an explicit devirtualization already see if we
       // have decreased the number of indirect calls and increased the number
@@ -765,8 +790,7 @@ ModuleToPostOrderCGSCCPassAdaptor<CGSCCPassT>::run(Module &M,
 
   CGSCCUpdateResult UR = {
       RCWorklist, CWorklist, InvalidRefSCCSet,         InvalidSCCSet,
-      nullptr,    nullptr,   PreservedAnalyses::all(), InlinedInternalEdges,
-      false};
+      nullptr,    nullptr,   PreservedAnalyses::all(), InlinedInternalEdges};
 
   // Request PassInstrumentation from analysis manager, will use it to run
   // instrumenting callbacks for the passes later.

diff  --git a/llvm/lib/Analysis/CGSCCPassManager.cpp b/llvm/lib/Analysis/CGSCCPassManager.cpp
index 206088b7895f..61c03f9bddfa 100644
--- a/llvm/lib/Analysis/CGSCCPassManager.cpp
+++ b/llvm/lib/Analysis/CGSCCPassManager.cpp
@@ -666,7 +666,6 @@ static LazyCallGraph::SCC &updateCGAndAnalysisManagerForPass(
 
   // Now promote ref edges into call edges.
   for (Node *CallTarget : PromotedRefTargets) {
-    UR.Devirtualized = true;
     SCC &TargetC = *G.lookupSCC(*CallTarget);
     RefSCC &TargetRC = TargetC.getOuterRefSCC();
 

diff  --git a/llvm/test/Transforms/Inline/devirtualize-3.ll b/llvm/test/Transforms/Inline/devirtualize-3.ll
index 987463a03766..4165b2125dfc 100644
--- a/llvm/test/Transforms/Inline/devirtualize-3.ll
+++ b/llvm/test/Transforms/Inline/devirtualize-3.ll
@@ -1,5 +1,4 @@
 ; RUN: opt -basic-aa -S -O2 < %s | FileCheck %s
-; RUN: opt -aa-pipeline=basic-aa -S -passes='default<O2>' < %s | FileCheck %s
 ; PR5009
 
 ; CHECK: define i32 @main() 

diff  --git a/llvm/test/Transforms/Inline/devirtualize-5.ll b/llvm/test/Transforms/Inline/devirtualize-5.ll
deleted file mode 100644
index 359c8e183de4..000000000000
--- a/llvm/test/Transforms/Inline/devirtualize-5.ll
+++ /dev/null
@@ -1,22 +0,0 @@
-; RUN: opt -abort-on-max-devirt-iterations-reached -passes='cgscc(devirt<2>(inline,instcombine))' -S < %s | FileCheck %s
-; RUN: opt -abort-on-max-devirt-iterations-reached -passes='default<O2>' -S < %s | FileCheck %s
-
-define i32 @i() alwaysinline {
-  ret i32 45
-}
-
-; CHECK-LABEL: define i32 @main
-; CHECK-NEXT: ret i32 45
-
-define i32 @main() {
-  %a = alloca i32 ()*
-  store i32 ()* @i, i32 ()** %a
-  %r = call i32 @call(i32 ()** %a)
-  ret i32 %r
-}
-
-define i32 @call(i32 ()** %a) alwaysinline {
-  %c = load i32 ()*, i32 ()** %a
-  %r = call i32 %c()
-  ret i32 %r
-}

diff  --git a/llvm/test/Transforms/Inline/devirtualize.ll b/llvm/test/Transforms/Inline/devirtualize.ll
index eaba1a8c19c5..561bb62ae644 100644
--- a/llvm/test/Transforms/Inline/devirtualize.ll
+++ b/llvm/test/Transforms/Inline/devirtualize.ll
@@ -1,5 +1,4 @@
 ; RUN: opt -S -Os < %s | FileCheck %s
-; RUN: opt -S -aa-pipeline=basic-aa -passes='default<Os>' < %s | FileCheck %s
 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
 target triple = "x86_64-apple-darwin10.0.0"
 


        


More information about the llvm-commits mailing list