llvm.org GIT mirror llvm / 621de7b
[ThinLTO] Rework llvm-link to use the FunctionImporter 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 Teresa Johnson 2 years ago
4 changed file(s) with 21 addition(s) and 39 deletion(s). Raw diff Collapse all Expand all
22
33 ; The imported module has out-of-date debug information, let's make sure we can
44 ; drop them without crashing when materializing later.
5 ; RUN: llvm-lto -thinlto-action=import %t.bc -thinlto-index=%t.index.bc -o - | llvm-dis -o - | FileCheck %s
5 ; RUN: llvm-link %t.bc -summary-index=%t.index.bc -import=globalfunc:%p/Inputs/drop-debug-info.bc | llvm-dis -o - | FileCheck %s
66 ; CHECK: define available_externally void @globalfunc
77 ; CHECK-NOT: llvm.dbg.value
88
1616 ret i32 0
1717 }
1818
19 declare void @globalfunc(...)
19 declare void @globalfunc(...)
55 Object
66 Support
77 TransformUtils
8 IPO
89 )
910
1011 add_llvm_tool(llvm-link
1818 type = Tool
1919 name = llvm-link
2020 parent = Tools
21 required_libraries = AsmParser BitReader BitWriter IRReader Linker Object TransformUtils
21 required_libraries = AsmParser BitReader BitWriter IRReader Linker Object TransformUtils IPO
3232 #include "llvm/Support/SourceMgr.h"
3333 #include "llvm/Support/SystemUtils.h"
3434 #include "llvm/Support/ToolOutputFile.h"
35 #include "llvm/Transforms/IPO/FunctionImport.h"
3536 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
3637
3738 #include
201202 }
202203
203204 /// Import any functions requested via the -import option.
204 static bool importFunctions(const char *argv0, LLVMContext &Context,
205 Linker &L) {
205 static bool importFunctions(const char *argv0, Module &DestModule) {
206206 if (SummaryIndex.empty())
207207 return true;
208208 std::unique_ptr Index =
209209 ExitOnErr(llvm::getModuleSummaryIndexForFile(SummaryIndex));
210210
211211 // Map of Module -> List of globals to import from the Module
212 std::map> ModuleToGlobalsToImportMap;
213 auto ModuleLoader = [&Context](const char *argv0,
214 const std::string &Identifier) {
215 return loadFile(argv0, Identifier, Context, false);
212 FunctionImporter::ImportMapTy ImportList;
213
214 auto ModuleLoader = [&DestModule](const char *argv0,
215 const std::string &Identifier) {
216 return loadFile(argv0, Identifier, DestModule.getContext(), false);
216217 };
218
217219 ModuleLazyLoaderCache ModuleLoaderCache(ModuleLoader);
218220 for (const auto &Import : Imports) {
219221 // Identify the requested function and its bitcode source file.
252254 if (Verbose)
253255 errs() << "Importing " << FunctionName << " from " << FileName << "\n";
254256
255 auto &Entry = ModuleToGlobalsToImportMap[SrcModule.getModuleIdentifier()];
256 Entry.insert(F);
257
258 ExitOnErr(F->materialize());
259 }
260
261 // Do the actual import of globals now, one Module at a time
262 for (auto &GlobalsToImportPerModule : ModuleToGlobalsToImportMap) {
263 // Get the module for the import
264 auto &GlobalsToImport = GlobalsToImportPerModule.second;
265 std::unique_ptr SrcModule =
266 ModuleLoaderCache.takeModule(GlobalsToImportPerModule.first);
267 assert(&Context == &SrcModule->getContext() && "Context mismatch");
268
269 // If modules were created with lazy metadata loading, materialize it
270 // now, before linking it (otherwise this will be a noop).
271 ExitOnErr(SrcModule->materializeMetadata());
272 UpgradeDebugInfo(*SrcModule);
273
274 // Linkage Promotion and renaming
275 if (renameModuleForThinLTO(*SrcModule, *Index, &GlobalsToImport))
276 return true;
277
278 // Instruct the linker to not automatically import linkonce defintion.
279 unsigned Flags = Linker::Flags::DontForceLinkLinkonceODR;
280
281 if (L.linkInModule(std::move(SrcModule), Flags, &GlobalsToImport))
282 return false;
283 }
257 auto &Entry = ImportList[FileName];
258 Entry.insert(std::make_pair(F->getGUID(), /* (Unused) threshold */ 1.0));
259 }
260 auto CachedModuleLoader = [&](StringRef Identifier) {
261 return ModuleLoaderCache.takeModule(Identifier);
262 };
263 FunctionImporter Importer(*Index, CachedModuleLoader);
264 ExitOnErr(Importer.importFunctions(DestModule, ImportList));
284265
285266 return true;
286267 }
373354 return 1;
374355
375356 // Import any functions requested via -import
376 if (!importFunctions(argv[0], Context, L))
357 if (!importFunctions(argv[0], *Composite))
377358 return 1;
378359
379360 if (DumpAsm) errs() << "Here's the assembly:\n" << *Composite;