[ThinLTO] Add optional import message and statistics
authorTeresa Johnson <tejohnson@google.com>
Sun, 27 Mar 2016 15:27:30 +0000 (15:27 +0000)
committerTeresa Johnson <tejohnson@google.com>
Sun, 27 Mar 2016 15:27:30 +0000 (15:27 +0000)
Summary:
Add a statistic to count the number of imported functions. Also, add a
new -print-imports option to emit a trace of imported functions, that
works even for an NDEBUG build.

Note that emitOptimizationRemark does not work for the above printing as
it expects a Function object and DebugLoc, neither of which we have
with summary-based importing.

This is part 2 of D18487, the first part was committed separately as
r264536.

Reviewers: joker.eph

Subscribers: llvm-commits, joker.eph

Differential Revision: http://reviews.llvm.org/D18487

llvm-svn: 264537

llvm/lib/Transforms/IPO/FunctionImport.cpp
llvm/test/Transforms/FunctionImport/funcimport.ll

index 896ac5d..a99739c 100644 (file)
@@ -14,6 +14,7 @@
 #include "llvm/Transforms/IPO/FunctionImport.h"
 
 #include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/Statistic.h"
 #include "llvm/ADT/StringSet.h"
 #include "llvm/IR/AutoUpgrade.h"
 #include "llvm/IR/DiagnosticPrinter.h"
@@ -31,6 +32,8 @@
 
 using namespace llvm;
 
+STATISTIC(NumImported, "Number of functions imported");
+
 /// Limit on instruction count of imported functions.
 static cl::opt<unsigned> ImportInstrLimit(
     "import-instr-limit", cl::init(100), cl::Hidden, cl::value_desc("N"),
@@ -43,6 +46,9 @@ static cl::opt<float>
                                "`import-instr-limit` threshold by this factor "
                                "before processing newly imported functions"));
 
+static cl::opt<bool> PrintImports("print-imports", cl::init(false), cl::Hidden,
+                                  cl::desc("Print imported functions"));
+
 // Load lazily a module from \p FileName in \p Context.
 static std::unique_ptr<Module> loadFile(const std::string &FileName,
                                         LLVMContext &Context) {
@@ -348,6 +354,12 @@ bool FunctionImporter::importFunctions(
     if (renameModuleForThinLTO(*SrcModule, Index, &GlobalsToImport))
       return true;
 
+    if (PrintImports) {
+      for (const auto *GV : GlobalsToImport)
+        dbgs() << DestModule.getSourceFileName() << ": Import " << GV->getName()
+               << " from " << SrcModule->getSourceFileName() << "\n";
+    }
+
     if (TheLinker.linkInModule(std::move(SrcModule), Linker::Flags::None,
                                &GlobalsToImport))
       report_fatal_error("Function Import: link error");
@@ -355,6 +367,8 @@ bool FunctionImporter::importFunctions(
     ImportedCount += GlobalsToImport.size();
   }
 
+  NumImported += ImportedCount;
+
   DEBUG(dbgs() << "Imported " << ImportedCount << " functions for Module "
                << DestModule.getModuleIdentifier() << "\n");
   return ImportedCount;
index f18ed94..e477db5 100644 (file)
@@ -4,7 +4,7 @@
 ; RUN: llvm-lto -thinlto -o %t3 %t.bc %t2.bc
 
 ; Do the import now
-; RUN: opt -function-import -summary-file %t3.thinlto.bc %t.bc -S | FileCheck %s --check-prefix=CHECK --check-prefix=INSTLIMDEF
+; RUN: opt -function-import -stats -print-imports -summary-file %t3.thinlto.bc %t.bc -S 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=INSTLIMDEF
 
 ; Test import with smaller instruction limit
 ; RUN: opt -function-import -summary-file %t3.thinlto.bc %t.bc -import-instr-limit=5 -S | FileCheck %s --check-prefix=CHECK --check-prefix=INSTLIM5
@@ -34,9 +34,12 @@ declare void @analias(...) #1
 
 ; Aliases import the aliasee function
 declare void @linkoncealias(...) #1
+; INSTLIMDEF-DAG: Import linkoncealias
+; INSTLIMDEF-DAG: Import linkoncefunc
 ; CHECK-DAG: define linkonce_odr void @linkoncefunc()
 ; CHECK-DAG: @linkoncealias = alias void (...), bitcast (void ()* @linkoncefunc to void (...)*
 
+; INSTLIMDEF-DAG: Import referencestatics
 ; INSTLIMDEF-DAG: define available_externally i32 @referencestatics(i32 %i)
 ; INSTLIM5-DAG: declare i32 @referencestatics(...)
 declare i32 @referencestatics(...) #1
@@ -44,22 +47,28 @@ declare i32 @referencestatics(...) #1
 ; The import of referencestatics will expose call to staticfunc that
 ; should in turn be imported as a promoted/renamed and hidden function.
 ; Ensure that the call is to the properly-renamed function.
+; INSTLIMDEF-DAG: Import staticfunc
 ; INSTLIMDEF-DAG: %call = call i32 @staticfunc.llvm.2()
 ; INSTLIMDEF-DAG: define available_externally hidden i32 @staticfunc.llvm.2()
 
+; INSTLIMDEF-DAG: Import referenceglobals
 ; CHECK-DAG: define available_externally i32 @referenceglobals(i32 %i)
 declare i32 @referenceglobals(...) #1
 
 ; The import of referenceglobals will expose call to globalfunc1 that
 ; should in turn be imported.
+; INSTLIMDEF-DAG: Import globalfunc1
 ; CHECK-DAG: define available_externally void @globalfunc1()
 
+; INSTLIMDEF-DAG: Import referencecommon
 ; CHECK-DAG: define available_externally i32 @referencecommon(i32 %i)
 declare i32 @referencecommon(...) #1
 
+; INSTLIMDEF-DAG: Import setfuncptr
 ; CHECK-DAG: define available_externally void @setfuncptr()
 declare void @setfuncptr(...) #1
 
+; INSTLIMDEF-DAG: Import callfuncptr
 ; CHECK-DAG: define available_externally void @callfuncptr()
 declare void @callfuncptr(...) #1
 
@@ -73,5 +82,9 @@ declare void @callfuncptr(...) #1
 ; CHECK-DAG: declare void @weakfunc(...)
 declare void @weakfunc(...) #1
 
+; INSTLIMDEF-DAG: Import funcwithpersonality
 ; INSTLIMDEF-DAG: define available_externally hidden void @funcwithpersonality.llvm.2() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
 ; INSTLIM5-DAG: declare hidden void @funcwithpersonality.llvm.2()
+
+; INSTLIMDEF-DAG: Import globalfunc2
+; INSTLIMDEF: 11 function-import - Number of functions imported