[llvm] r254011 - [ThinLTO] Enable iterative importing in FunctionImport pass

Teresa Johnson via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 24 11:55:04 PST 2015


Author: tejohnson
Date: Tue Nov 24 13:55:04 2015
New Revision: 254011

URL: http://llvm.org/viewvc/llvm-project?rev=254011&view=rev
Log:
[ThinLTO] Enable iterative importing in FunctionImport pass

Analyze imported function bodies and add any new external calls to
the worklist for importing. Currently no controls on the importing
so this will end up importing everything possible in the call tree
below the importing module. Basic profitability checks coming next.

Update test to check for iteratively inlined functions.

Modified:
    llvm/trunk/lib/Transforms/IPO/FunctionImport.cpp
    llvm/trunk/test/Transforms/FunctionImport/funcimport.ll

Modified: llvm/trunk/lib/Transforms/IPO/FunctionImport.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/FunctionImport.cpp?rev=254011&r1=254010&r2=254011&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/IPO/FunctionImport.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/FunctionImport.cpp Tue Nov 24 13:55:04 2015
@@ -131,10 +131,24 @@ bool FunctionImporter::importFunctions(M
 
     // The function that we will import!
     GlobalValue *SGV = Module.getNamedValue(CalledFunctionName);
+    StringRef ImportFunctionName = CalledFunctionName;
+    if (!SGV) {
+      // Might be local in source Module, promoted/renamed in dest Module M.
+      std::pair<StringRef, StringRef> Split =
+          CalledFunctionName.split(".llvm.");
+      SGV = Module.getNamedValue(Split.first);
+#ifndef NDEBUG
+      // Assert that Split.second is module id
+      uint64_t ModuleId;
+      assert(!Split.second.getAsInteger(10, ModuleId));
+      assert(ModuleId == Index.getModuleId(FileName));
+#endif
+    }
     Function *F = dyn_cast<Function>(SGV);
     if (!F && isa<GlobalAlias>(SGV)) {
       auto *SGA = dyn_cast<GlobalAlias>(SGV);
       F = dyn_cast<Function>(SGA->getBaseObject());
+      ImportFunctionName = F->getName();
     }
     if (!F) {
       errs() << "Can't load function '" << CalledFunctionName << "' in Module '"
@@ -156,8 +170,28 @@ bool FunctionImporter::importFunctions(M
     if (L.linkInModule(&Module, Linker::Flags::None, &Index, F))
       report_fatal_error("Function Import: link error");
 
-    // TODO: Process the newly imported function and add callees to the
-    // worklist.
+    // Process the newly imported function and add callees to the worklist.
+    GlobalValue *NewGV = M.getNamedValue(ImportFunctionName);
+    assert(NewGV);
+    Function *NewF = dyn_cast<Function>(NewGV);
+    assert(NewF);
+
+    for (auto &BB : *NewF) {
+      for (auto &I : BB) {
+        if (isa<CallInst>(I)) {
+          DEBUG(dbgs() << "Found a call: '" << I << "'\n");
+          auto CalledFunction = cast<CallInst>(I).getCalledFunction();
+          // Insert any new external calls that have not already been
+          // added to set/worklist.
+          if (CalledFunction && CalledFunction->hasName() &&
+              CalledFunction->isDeclaration() &&
+              !CalledFunctions.count(CalledFunction->getName())) {
+            CalledFunctions.insert(CalledFunction->getName());
+            Worklist.push_back(CalledFunction->getName());
+          }
+        }
+      }
+    }
 
     Changed = true;
   }

Modified: llvm/trunk/test/Transforms/FunctionImport/funcimport.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/FunctionImport/funcimport.ll?rev=254011&r1=254010&r2=254011&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/FunctionImport/funcimport.ll (original)
+++ llvm/trunk/test/Transforms/FunctionImport/funcimport.ll Tue Nov 24 13:55:04 2015
@@ -31,9 +31,19 @@ declare void @analias(...) #1
 ; CHECK-DAG: define available_externally i32 @referencestatics(i32 %i)
 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.
+; CHECK-DAG: %call = call i32 @staticfunc.llvm.2()
+; CHECK-DAG: define available_externally hidden i32 @staticfunc.llvm.2()
+
 ; 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.
+; CHECK-DAG: define available_externally void @globalfunc1()
+
 ; CHECK-DAG: define available_externally i32 @referencecommon(i32 %i)
 declare i32 @referencecommon(...) #1
 




More information about the llvm-commits mailing list