]> granicus.if.org Git - llvm/commitdiff
[ThinLTO] Rework llvm-link to use the FunctionImporter
authorTeresa Johnson <tejohnson@google.com>
Wed, 4 Jan 2017 14:27:31 +0000 (14:27 +0000)
committerTeresa Johnson <tejohnson@google.com>
Wed, 4 Jan 2017 14:27:31 +0000 (14:27 +0000)
Summary:
Change llvm-link to use the FunctionImporter handling, instead of
manually invoking the Linker. We still need to load the module
in llvm-link to do the desired testing for invalid import requests
(weak functions), and to get the GUID (in case the function is local).

Also change the drop-debug-info test to use llvm-link so that importing
is forced (in order to test debug info handling) and independent of
import logic changes.

Reviewers: mehdi_amini

Subscribers: mgorny, llvm-commits, aprantl

Differential Revision: https://reviews.llvm.org/D28277

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@290964 91177308-0d34-0410-b5e6-96231b3b80d8

test/ThinLTO/X86/drop-debug-info.ll
tools/llvm-link/CMakeLists.txt
tools/llvm-link/LLVMBuild.txt
tools/llvm-link/llvm-link.cpp

index f8ed8dff420ddc440acb0c6bcd30ffc2a64ac1d2..a097d6bac98eeac6a17d7483e66407ebee696191 100644 (file)
@@ -3,7 +3,7 @@
 
 ; The imported module has out-of-date debug information, let's make sure we can
 ; drop them without crashing when materializing later.
-; RUN: llvm-lto -thinlto-action=import %t.bc -thinlto-index=%t.index.bc -o - | llvm-dis -o - | FileCheck %s
+; RUN: llvm-link %t.bc -summary-index=%t.index.bc -import=globalfunc:%p/Inputs/drop-debug-info.bc | llvm-dis -o - | FileCheck %s
 ; CHECK: define available_externally void @globalfunc
 ; CHECK-NOT: llvm.dbg.value
 
@@ -17,4 +17,4 @@ entry:
   ret i32 0
 }
 
-declare void @globalfunc(...)
\ No newline at end of file
+declare void @globalfunc(...)
index 5aae69b4ca0b1b7c73495e349e579511ee548318..7317792232483645606e2d9d6c262d041f7fcef5 100644 (file)
@@ -6,6 +6,7 @@ set(LLVM_LINK_COMPONENTS
   Object
   Support
   TransformUtils
+  IPO
   )
 
 add_llvm_tool(llvm-link
index 1dba5c0adb3f0805467e9ee3c0c85d97b62a8ca4..c7476043164a7100a8633dd7543d6c10521bb111 100644 (file)
@@ -19,4 +19,4 @@
 type = Tool
 name = llvm-link
 parent = Tools
-required_libraries = AsmParser BitReader BitWriter IRReader Linker Object TransformUtils
+required_libraries = AsmParser BitReader BitWriter IRReader Linker Object TransformUtils IPO
index 43431ac3398ae413012b811cb0f8bb82d8d53db5..e89696e7e7c245176ddc2d4c306e9013997190e7 100644 (file)
@@ -33,6 +33,7 @@
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/SystemUtils.h"
 #include "llvm/Support/ToolOutputFile.h"
+#include "llvm/Transforms/IPO/FunctionImport.h"
 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
 
 #include <memory>
@@ -202,19 +203,20 @@ static void diagnosticHandler(const DiagnosticInfo &DI, void *C) {
 }
 
 /// Import any functions requested via the -import option.
-static bool importFunctions(const char *argv0, LLVMContext &Context,
-                            Linker &L) {
+static bool importFunctions(const char *argv0, Module &DestModule) {
   if (SummaryIndex.empty())
     return true;
   std::unique_ptr<ModuleSummaryIndex> Index =
       ExitOnErr(llvm::getModuleSummaryIndexForFile(SummaryIndex));
 
   // Map of Module -> List of globals to import from the Module
-  std::map<StringRef, DenseSet<const GlobalValue *>> ModuleToGlobalsToImportMap;
-  auto ModuleLoader = [&Context](const char *argv0,
-                                 const std::string &Identifier) {
-    return loadFile(argv0, Identifier, Context, false);
+  FunctionImporter::ImportMapTy ImportList;
+
+  auto ModuleLoader = [&DestModule](const char *argv0,
+                                    const std::string &Identifier) {
+    return loadFile(argv0, Identifier, DestModule.getContext(), false);
   };
+
   ModuleLazyLoaderCache ModuleLoaderCache(ModuleLoader);
   for (const auto &Import : Imports) {
     // Identify the requested function and its bitcode source file.
@@ -253,35 +255,14 @@ static bool importFunctions(const char *argv0, LLVMContext &Context,
     if (Verbose)
       errs() << "Importing " << FunctionName << " from " << FileName << "\n";
 
-    auto &Entry = ModuleToGlobalsToImportMap[SrcModule.getModuleIdentifier()];
-    Entry.insert(F);
-
-    ExitOnErr(F->materialize());
-  }
-
-  // Do the actual import of globals now, one Module at a time
-  for (auto &GlobalsToImportPerModule : ModuleToGlobalsToImportMap) {
-    // Get the module for the import
-    auto &GlobalsToImport = GlobalsToImportPerModule.second;
-    std::unique_ptr<Module> SrcModule =
-        ModuleLoaderCache.takeModule(GlobalsToImportPerModule.first);
-    assert(&Context == &SrcModule->getContext() && "Context mismatch");
-
-    // If modules were created with lazy metadata loading, materialize it
-    // now, before linking it (otherwise this will be a noop).
-    ExitOnErr(SrcModule->materializeMetadata());
-    UpgradeDebugInfo(*SrcModule);
-
-    // Linkage Promotion and renaming
-    if (renameModuleForThinLTO(*SrcModule, *Index, &GlobalsToImport))
-      return true;
-
-    // Instruct the linker to not automatically import linkonce defintion.
-    unsigned Flags = Linker::Flags::DontForceLinkLinkonceODR;
-
-    if (L.linkInModule(std::move(SrcModule), Flags, &GlobalsToImport))
-      return false;
+    auto &Entry = ImportList[FileName];
+    Entry.insert(std::make_pair(F->getGUID(), /* (Unused) threshold */ 1.0));
   }
+  auto CachedModuleLoader = [&](StringRef Identifier) {
+    return ModuleLoaderCache.takeModule(Identifier);
+  };
+  FunctionImporter Importer(*Index, CachedModuleLoader);
+  ExitOnErr(Importer.importFunctions(DestModule, ImportList));
 
   return true;
 }
@@ -374,7 +355,7 @@ int main(int argc, char **argv) {
     return 1;
 
   // Import any functions requested via -import
-  if (!importFunctions(argv[0], Context, L))
+  if (!importFunctions(argv[0], *Composite))
     return 1;
 
   if (DumpAsm) errs() << "Here's the assembly:\n" << *Composite;