llvm.org GIT mirror llvm / 8268477
Rework linkInModule(), making it oblivious to ThinLTO Summary: ThinLTO is relying on linkInModule to import selected function. However a lot of "magic" was hidden in linkInModule and the IRMover, who would rename and promote global variables on the fly. This is moving to an approach where the steps are decoupled and the client is reponsible to specify the list of globals to import. As a consequence some test are changed because they were relying on the previous behavior which was importing the definition of *every* single global without control on the client side. Now the burden is on the client to decide if a global has to be imported or not. Reviewers: tejohnson Subscribers: joker.eph, llvm-commits Differential Revision: http://reviews.llvm.org/D18122 From: Mehdi Amini <mehdi.amini@apple.com> git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@263863 91177308-0d34-0410-b5e6-96231b3b80d8 Mehdi Amini 3 years ago
8 changed file(s) with 70 addition(s) and 108 deletion(s). Raw diff Collapse all Expand all
99 #ifndef LLVM_LINKER_LINKER_H
1010 #define LLVM_LINKER_LINKER_H
1111
12 #include "llvm/IR/ModuleSummaryIndex.h"
1312 #include "llvm/Linker/IRMover.h"
1413
1514 namespace llvm {
3938 /// Passing OverrideSymbols as true will have symbols from Src
4039 /// shadow those in the Dest.
4140 /// For ThinLTO function importing/exporting the \p ModuleSummaryIndex
42 /// is passed. If \p FunctionsToImport is provided, only the functions that
41 /// is passed. If \p GlobalsToImport is provided, only the globals that
4342 /// are part of the set will be imported from the source module.
4443 /// The \p ValIDToTempMDMap is populated by the linker when function
4544 /// importing is performed.
4645 ///
4746 /// Returns true on error.
4847 bool linkInModule(std::unique_ptr Src, unsigned Flags = Flags::None,
49 const ModuleSummaryIndex *Index = nullptr,
50 DenseSet *FunctionsToImport = nullptr,
48 DenseSet *GlobalsToImport = nullptr,
5149 DenseMap *ValIDToTempMDMap = nullptr);
5250
5351 static bool linkModules(Module &Dest, std::unique_ptr Src,
2929 /// Module summary index passed in for function importing/exporting handling.
3030 const ModuleSummaryIndex &ImportIndex;
3131
32 /// Functions to import from this module, all other functions will be
32 /// Globals to import from this module, all other functions will be
3333 /// imported as declarations instead of definitions.
34 DenseSet *FunctionsToImport;
34 DenseSet *GlobalsToImport;
3535
3636 /// Set to true if the given ModuleSummaryIndex contains any functions
3737 /// from this source module, in which case we must conservatively assume
3939 /// as part of a different backend compilation process.
4040 bool HasExportedFunctions = false;
4141
42 /// Populated during ThinLTO global processing with locals promoted
43 /// to global scope in an exporting module, which now need to be linked
44 /// in if calling from the ModuleLinker.
45 SetVector NewExportedValues;
46
4742 /// Check if we should promote the given local value to global scope.
4843 bool doPromoteLocalToGlobal(const GlobalValue *SGV);
4944
5045 /// Helper methods to check if we are importing from or potentially
5146 /// exporting from the current source module.
52 bool isPerformingImport() const { return FunctionsToImport != nullptr; }
47 bool isPerformingImport() const { return GlobalsToImport != nullptr; }
5348 bool isModuleExporting() const { return HasExportedFunctions; }
5449
5550 /// If we are importing from the source module, checks if we should
7671 public:
7772 FunctionImportGlobalProcessing(
7873 Module &M, const ModuleSummaryIndex &Index,
79 DenseSet *FunctionsToImport = nullptr)
80 : M(M), ImportIndex(Index), FunctionsToImport(FunctionsToImport) {
74 DenseSet *GlobalsToImport = nullptr)
75 : M(M), ImportIndex(Index), GlobalsToImport(GlobalsToImport) {
8176 // If we have a ModuleSummaryIndex but no function to import,
8277 // then this is the primary module being compiled in a ThinLTO
8378 // backend compilation, and we need to see if it has functions that
8479 // may be exported to another backend compilation.
85 if (!FunctionsToImport)
80 if (!GlobalsToImport)
8681 HasExportedFunctions = ImportIndex.hasExportedFunctions(M);
8782 }
8883
9085
9186 static bool
9287 doImportAsDefinition(const GlobalValue *SGV,
93 DenseSet *FunctionsToImport);
94
95 /// Access the promoted globals that are now exported and need to be linked.
96 SetVector &getNewExportedValues() { return NewExportedValues; }
88 DenseSet *GlobalsToImport);
9789 };
9890
9991 /// Perform in-place global value handling on the given Module for
10092 /// exported local functions renamed and promoted for ThinLTO.
101 bool renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index);
93 bool renameModuleForThinLTO(
94 Module &M, const ModuleSummaryIndex &Index,
95 DenseSet *GlobalsToImport = nullptr);
10296
10397 } // End llvm namespace
10498
3434 /// For symbol clashes, prefer those from Src.
3535 unsigned Flags;
3636
37 /// Module summary index passed into ModuleLinker for using in function
38 /// importing/exporting handling.
39 const ModuleSummaryIndex *ImportIndex;
40
4137 /// Functions to import from source module, all other functions are
4238 /// imported as declarations instead of definitions.
43 DenseSet *FunctionsToImport;
44
45 /// Set to true if the given ModuleSummaryIndex contains any functions
46 /// from this source module, in which case we must conservatively assume
47 /// that any of its functions may be imported into another module
48 /// as part of a different backend compilation process.
49 bool HasExportedFunctions = false;
39 DenseSet *GlobalsToImport;
5040
5141 /// Association between metadata value id and temporary metadata that
5242 /// remains unmapped after function importing. Saved during function
115105
116106 /// Helper method to check if we are importing from the current source
117107 /// module.
118 bool isPerformingImport() const { return FunctionsToImport != nullptr; }
108 bool isPerformingImport() const { return GlobalsToImport != nullptr; }
119109
120110 /// If we are importing from the source module, checks if we should
121111 /// import SGV as a definition, otherwise import as a declaration.
123113
124114 public:
125115 ModuleLinker(IRMover &Mover, std::unique_ptr SrcM, unsigned Flags,
126 const ModuleSummaryIndex *Index = nullptr,
127 DenseSet *FunctionsToImport = nullptr,
116 DenseSet *GlobalsToImport = nullptr,
128117 DenseMap *ValIDToTempMDMap = nullptr)
129 : Mover(Mover), SrcM(std::move(SrcM)), Flags(Flags), ImportIndex(Index),
130 FunctionsToImport(FunctionsToImport),
131 ValIDToTempMDMap(ValIDToTempMDMap) {
132 assert((ImportIndex || !FunctionsToImport) &&
133 "Expect a ModuleSummaryIndex when importing");
134 // If we have a ModuleSummaryIndex but no function to import,
135 // then this is the primary module being compiled in a ThinLTO
136 // backend compilation, and we need to see if it has functions that
137 // may be exported to another backend compilation.
138 if (ImportIndex && !FunctionsToImport)
139 HasExportedFunctions = ImportIndex->hasExportedFunctions(*this->SrcM);
140 }
118 : Mover(Mover), SrcM(std::move(SrcM)), Flags(Flags),
119 GlobalsToImport(GlobalsToImport), ValIDToTempMDMap(ValIDToTempMDMap) {}
141120
142121 bool run();
143122 };
146125 bool ModuleLinker::doImportAsDefinition(const GlobalValue *SGV) {
147126 if (!isPerformingImport())
148127 return false;
149 return FunctionImportGlobalProcessing::doImportAsDefinition(
150 SGV, FunctionsToImport);
128 return FunctionImportGlobalProcessing::doImportAsDefinition(SGV,
129 GlobalsToImport);
151130 }
152131
153132 static GlobalValue::VisibilityTypes
296275 if (isa(&Src)) {
297276 // For functions, LinkFromSrc iff this is a function requested
298277 // for importing. For variables, decide below normally.
299 LinkFromSrc = FunctionsToImport->count(&Src);
278 LinkFromSrc = GlobalsToImport->count(&Src);
300279 return false;
301280 }
302281
422401 if (GV.hasAppendingLinkage() && isPerformingImport())
423402 return false;
424403
425 if (isPerformingImport() && !doImportAsDefinition(&GV))
426 return false;
427
428 if (!DGV && !shouldOverrideFromSrc() &&
429 (GV.hasLocalLinkage() || GV.hasLinkOnceLinkage() ||
430 GV.hasAvailableExternallyLinkage()))
404 if (isPerformingImport()) {
405 if (!doImportAsDefinition(&GV))
406 return false;
407 } else if (!DGV && !shouldOverrideFromSrc() &&
408 (GV.hasLocalLinkage() || GV.hasLinkOnceLinkage() ||
409 GV.hasAvailableExternallyLinkage()))
431410 return false;
432411
433412 if (GV.isDeclaration())
507486 if (linkIfNeeded(GA))
508487 return true;
509488
510 if (ImportIndex) {
511 FunctionImportGlobalProcessing ThinLTOProcessing(*SrcM, *ImportIndex,
512 FunctionsToImport);
513 if (ThinLTOProcessing.run())
514 return true;
515 for (auto *GV : ThinLTOProcessing.getNewExportedValues())
516 ValuesToLink.insert(GV);
517 }
518
519489 for (unsigned I = 0; I < ValuesToLink.size(); ++I) {
520490 GlobalValue *GV = ValuesToLink[I];
521491 const Comdat *SC = GV->getComdat();
548518 Linker::Linker(Module &M) : Mover(M) {}
549519
550520 bool Linker::linkInModule(std::unique_ptr Src, unsigned Flags,
551 const ModuleSummaryIndex *Index,
552 DenseSet *FunctionsToImport,
521 DenseSet *GlobalsToImport,
553522 DenseMap *ValIDToTempMDMap) {
554 ModuleLinker ModLinker(Mover, std::move(Src), Flags, Index, FunctionsToImport,
523 ModuleLinker ModLinker(Mover, std::move(Src), Flags, GlobalsToImport,
555524 ValIDToTempMDMap);
556525 return ModLinker.run();
557526 }
265265 if (!F && isa(SGV)) {
266266 auto *SGA = dyn_cast(SGV);
267267 F = dyn_cast(SGA->getBaseObject());
268 CalledFunctionName = F->getName();
269268 }
270269 assert(F && "Imported Function is ... not a Function");
271270
348347 UpgradeDebugInfo(*SrcModule);
349348
350349 // Link in the specified functions.
350 if (renameModuleForThinLTO(*SrcModule, Index, &FunctionsToImport))
351 return true;
352
351353 if (TheLinker.linkInModule(std::move(SrcModule), Linker::Flags::None,
352 &Index, &FunctionsToImport))
354 &FunctionsToImport))
353355 report_fatal_error("Function Import: link error");
354356
355357 ImportedCount += FunctionsToImport.size();
1717 /// Checks if we should import SGV as a definition, otherwise import as a
1818 /// declaration.
1919 bool FunctionImportGlobalProcessing::doImportAsDefinition(
20 const GlobalValue *SGV, DenseSet *FunctionsToImport) {
21 auto *GA = dyn_cast(SGV);
22 if (GA) {
20 const GlobalValue *SGV, DenseSet *GlobalsToImport) {
21
22 // For alias, we tie the definition to the base object. Extract it and recurse
23 if (auto *GA = dyn_cast(SGV)) {
2324 if (GA->hasWeakAnyLinkage())
2425 return false;
2526 const GlobalObject *GO = GA->getBaseObject();
2627 if (!GO->hasLinkOnceODRLinkage())
2728 return false;
2829 return FunctionImportGlobalProcessing::doImportAsDefinition(
29 GO, FunctionsToImport);
30 }
31 // Always import GlobalVariable definitions, except for the special
32 // case of WeakAny which are imported as ExternalWeak declarations
33 // (see comments in FunctionImportGlobalProcessing::getLinkage). The linkage
34 // changes described in FunctionImportGlobalProcessing::getLinkage ensure the
35 // correct behavior (e.g. global variables with external linkage are
36 // transformed to available_externally definitions, which are ultimately
37 // turned into declarations after the EliminateAvailableExternally pass).
38 if (isa(SGV) && !SGV->isDeclaration() &&
39 !SGV->hasWeakAnyLinkage())
40 return true;
41 // Only import the function requested for importing.
42 auto *SF = dyn_cast(SGV);
43 if (SF && FunctionsToImport->count(SF))
30 GO, GlobalsToImport);
31 }
32 // Only import the globals requested for importing.
33 if (GlobalsToImport->count(SGV))
4434 return true;
4535 // Otherwise no.
4636 return false;
5040 const GlobalValue *SGV) {
5141 if (!isPerformingImport())
5242 return false;
53 return FunctionImportGlobalProcessing::doImportAsDefinition(
54 SGV, FunctionsToImport);
43 return FunctionImportGlobalProcessing::doImportAsDefinition(SGV,
44 GlobalsToImport);
5545 }
5646
5747 bool FunctionImportGlobalProcessing::doPromoteLocalToGlobal(
197187 GV.setLinkage(getLinkage(&GV));
198188 if (!GV.hasLocalLinkage())
199189 GV.setVisibility(GlobalValue::HiddenVisibility);
200 if (isModuleExporting())
201 NewExportedValues.insert(&GV);
202190 } else
203191 GV.setLinkage(getLinkage(&GV));
204192
230218 return false;
231219 }
232220
233 bool llvm::renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index) {
234 FunctionImportGlobalProcessing ThinLTOProcessing(M, Index);
221 bool llvm::renameModuleForThinLTO(
222 Module &M, const ModuleSummaryIndex &Index,
223 DenseSet *GlobalsToImport) {
224 FunctionImportGlobalProcessing ThinLTOProcessing(M, Index, GlobalsToImport);
235225 return ThinLTOProcessing.run();
236226 }
6666 ; Ensure that imported static variable and function references are correctly
6767 ; promoted and renamed (including static constant variable).
6868 ; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=referencestatics:%t.bc -S | FileCheck %s --check-prefix=IMPORTSTATIC
69 ; IMPORTSTATIC-DAG: @staticvar.llvm.1 = available_externally hidden global
69 ; IMPORTSTATIC-DAG: @staticvar.llvm.1 = external hidden global
7070 ; IMPORTSTATIC-DAG: @staticconstvar.llvm.1 = internal unnamed_addr constant
7171 ; IMPORTSTATIC-DAG: define available_externally i32 @referencestatics
7272 ; IMPORTSTATIC-DAG: %call = call i32 @staticfunc.llvm.1
7777 ; are handled correctly (including referenced variable imported as
7878 ; available_externally definition)
7979 ; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=referenceglobals:%t.bc -S | FileCheck %s --check-prefix=IMPORTGLOBALS
80 ; IMPORTGLOBALS-DAG: @globalvar = available_externally global
80 ; IMPORTGLOBALS-DAG: @globalvar = external global
8181 ; IMPORTGLOBALS-DAG: declare void @globalfunc1()
8282 ; IMPORTGLOBALS-DAG: define available_externally i32 @referenceglobals
8383
8484 ; Ensure that common variable correctly imported as common defition.
8585 ; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=referencecommon:%t.bc -S | FileCheck %s --check-prefix=IMPORTCOMMON
86 ; IMPORTCOMMON-DAG: @commonvar = common global
86 ; IMPORTCOMMON-DAG: @commonvar = external global
8787 ; IMPORTCOMMON-DAG: define available_externally i32 @referencecommon
8888
8989 ; Ensure that imported static function pointer correctly promoted and renamed.
9090 ; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=callfuncptr:%t.bc -S | FileCheck %s --check-prefix=IMPORTFUNCPTR
91 ; IMPORTFUNCPTR-DAG: @P.llvm.1 = available_externally hidden global void ()* null
91 ; IMPORTFUNCPTR-DAG: @P.llvm.1 = external hidden global void ()*
9292 ; IMPORTFUNCPTR-DAG: define available_externally void @callfuncptr
9393 ; IMPORTFUNCPTR-DAG: %0 = load void ()*, void ()** @P.llvm.1
9494
6464
6565 ; Ensure that all uses of local variable @P which has used in setfuncptr
6666 ; and callfuncptr are to the same promoted/renamed global.
67 ; CHECK-DAG: @P.llvm.2 = available_externally hidden global void ()* null
67 ; CHECK-DAG: @P.llvm.2 = external hidden global void ()*
6868 ; CHECK-DAG: %0 = load void ()*, void ()** @P.llvm.2,
6969 ; CHECK-DAG: store void ()* @staticfunc2.llvm.2, void ()** @P.llvm.2,
7070
3232 #include "llvm/Support/SourceMgr.h"
3333 #include "llvm/Support/SystemUtils.h"
3434 #include "llvm/Support/ToolOutputFile.h"
35 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
36
3537 #include
3638 using namespace llvm;
3739
190192 if (Verbose)
191193 errs() << "Importing " << FunctionName << " from " << FileName << "\n";
192194
193 std::unique_ptr Index;
195 // Link in the specified function.
196 DenseSet GlobalsToImport;
197 GlobalsToImport.insert(F);
198
194199 if (!SummaryIndex.empty()) {
195200 ErrorOr> IndexOrErr =
196201 llvm::getModuleSummaryIndexForFile(SummaryIndex, diagnosticHandler);
199204 errs() << EC.message() << '\n';
200205 return false;
201206 }
202 Index = std::move(IndexOrErr.get());
207 auto Index = std::move(IndexOrErr.get());
208
209 // Linkage Promotion and renaming
210 if (renameModuleForThinLTO(*M, *Index, &GlobalsToImport))
211 return true;
203212 }
204213
205214 // Save the mapping of value ids to temporary metadata created when
209218 if (!TempMDVals)
210219 TempMDVals = llvm::make_unique>();
211220
212 // Link in the specified function.
213 DenseSet FunctionsToImport;
214 FunctionsToImport.insert(F);
215 if (L.linkInModule(std::move(M), Linker::Flags::None, Index.get(),
216 &FunctionsToImport, TempMDVals.get()))
221 if (L.linkInModule(std::move(M), Linker::Flags::None, &GlobalsToImport,
222 TempMDVals.get()))
217223 return false;
218224 }
219225
259265
260266 // If a module summary index is supplied, load it so linkInModule can treat
261267 // local functions/variables as exported and promote if necessary.
262 std::unique_ptr Index;
263268 if (!SummaryIndex.empty()) {
264269 ErrorOr> IndexOrErr =
265270 llvm::getModuleSummaryIndexForFile(SummaryIndex, diagnosticHandler);
268273 errs() << EC.message() << '\n';
269274 return false;
270275 }
271 Index = std::move(IndexOrErr.get());
276 auto Index = std::move(IndexOrErr.get());
277
278 // Promotion
279 if (renameModuleForThinLTO(*M, *Index))
280 return true;
272281 }
273282
274283 if (Verbose)
275284 errs() << "Linking in '" << File << "'\n";
276285
277 if (L.linkInModule(std::move(M), ApplicableFlags, Index.get()))
286 if (L.linkInModule(std::move(M), ApplicableFlags))
278287 return false;
279288 // All linker flags apply to linking of subsequent files.
280289 ApplicableFlags = Flags;