1 //===- FunctionImport.cpp - ThinLTO Summary-based Function Import ---------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements Function import based on summaries.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Transforms/IPO/FunctionImport.h"
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SetVector.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/StringSet.h"
22 #include "llvm/Bitcode/BitcodeReader.h"
23 #include "llvm/IR/AutoUpgrade.h"
24 #include "llvm/IR/Constants.h"
25 #include "llvm/IR/Function.h"
26 #include "llvm/IR/GlobalAlias.h"
27 #include "llvm/IR/GlobalObject.h"
28 #include "llvm/IR/GlobalValue.h"
29 #include "llvm/IR/GlobalVariable.h"
30 #include "llvm/IR/Metadata.h"
31 #include "llvm/IR/Module.h"
32 #include "llvm/IR/ModuleSummaryIndex.h"
33 #include "llvm/IRReader/IRReader.h"
34 #include "llvm/Linker/IRMover.h"
35 #include "llvm/Object/ModuleSymbolTable.h"
36 #include "llvm/Object/SymbolicFile.h"
37 #include "llvm/Pass.h"
38 #include "llvm/Support/Casting.h"
39 #include "llvm/Support/CommandLine.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/Error.h"
42 #include "llvm/Support/ErrorHandling.h"
43 #include "llvm/Support/FileSystem.h"
44 #include "llvm/Support/SourceMgr.h"
45 #include "llvm/Support/raw_ostream.h"
46 #include "llvm/Transforms/IPO/Internalize.h"
47 #include "llvm/Transforms/Utils/Cloning.h"
48 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
49 #include "llvm/Transforms/Utils/ValueMapper.h"
54 #include <system_error>
60 #define DEBUG_TYPE "function-import"
62 STATISTIC(NumImportedFunctionsThinLink
,
63 "Number of functions thin link decided to import");
64 STATISTIC(NumImportedHotFunctionsThinLink
,
65 "Number of hot functions thin link decided to import");
66 STATISTIC(NumImportedCriticalFunctionsThinLink
,
67 "Number of critical functions thin link decided to import");
68 STATISTIC(NumImportedGlobalVarsThinLink
,
69 "Number of global variables thin link decided to import");
70 STATISTIC(NumImportedFunctions
, "Number of functions imported in backend");
71 STATISTIC(NumImportedGlobalVars
,
72 "Number of global variables imported in backend");
73 STATISTIC(NumImportedModules
, "Number of modules imported from");
74 STATISTIC(NumDeadSymbols
, "Number of dead stripped symbols in index");
75 STATISTIC(NumLiveSymbols
, "Number of live symbols in index");
77 /// Limit on instruction count of imported functions.
78 static cl::opt
<unsigned> ImportInstrLimit(
79 "import-instr-limit", cl::init(100), cl::Hidden
, cl::value_desc("N"),
80 cl::desc("Only import functions with less than N instructions"));
82 static cl::opt
<int> ImportCutoff(
83 "import-cutoff", cl::init(-1), cl::Hidden
, cl::value_desc("N"),
84 cl::desc("Only import first N functions if N>=0 (default -1)"));
87 ImportInstrFactor("import-instr-evolution-factor", cl::init(0.7),
88 cl::Hidden
, cl::value_desc("x"),
89 cl::desc("As we import functions, multiply the "
90 "`import-instr-limit` threshold by this factor "
91 "before processing newly imported functions"));
93 static cl::opt
<float> ImportHotInstrFactor(
94 "import-hot-evolution-factor", cl::init(1.0), cl::Hidden
,
96 cl::desc("As we import functions called from hot callsite, multiply the "
97 "`import-instr-limit` threshold by this factor "
98 "before processing newly imported functions"));
100 static cl::opt
<float> ImportHotMultiplier(
101 "import-hot-multiplier", cl::init(10.0), cl::Hidden
, cl::value_desc("x"),
102 cl::desc("Multiply the `import-instr-limit` threshold for hot callsites"));
104 static cl::opt
<float> ImportCriticalMultiplier(
105 "import-critical-multiplier", cl::init(100.0), cl::Hidden
,
108 "Multiply the `import-instr-limit` threshold for critical callsites"));
110 // FIXME: This multiplier was not really tuned up.
111 static cl::opt
<float> ImportColdMultiplier(
112 "import-cold-multiplier", cl::init(0), cl::Hidden
, cl::value_desc("N"),
113 cl::desc("Multiply the `import-instr-limit` threshold for cold callsites"));
115 static cl::opt
<bool> PrintImports("print-imports", cl::init(false), cl::Hidden
,
116 cl::desc("Print imported functions"));
118 static cl::opt
<bool> PrintImportFailures(
119 "print-import-failures", cl::init(false), cl::Hidden
,
120 cl::desc("Print information for functions rejected for importing"));
122 static cl::opt
<bool> ComputeDead("compute-dead", cl::init(true), cl::Hidden
,
123 cl::desc("Compute dead symbols"));
125 static cl::opt
<bool> EnableImportMetadata(
126 "enable-import-metadata", cl::init(
128 true /*Enabled with asserts.*/
133 cl::Hidden
, cl::desc("Enable import metadata like 'thinlto_src_module'"));
135 /// Summary file to use for function importing when using -function-import from
136 /// the command line.
137 static cl::opt
<std::string
>
138 SummaryFile("summary-file",
139 cl::desc("The summary file to use for function importing."));
141 /// Used when testing importing from distributed indexes via opt
144 ImportAllIndex("import-all-index",
145 cl::desc("Import all external functions in index."));
147 // Load lazily a module from \p FileName in \p Context.
148 static std::unique_ptr
<Module
> loadFile(const std::string
&FileName
,
149 LLVMContext
&Context
) {
151 LLVM_DEBUG(dbgs() << "Loading '" << FileName
<< "'\n");
152 // Metadata isn't loaded until functions are imported, to minimize
153 // the memory overhead.
154 std::unique_ptr
<Module
> Result
=
155 getLazyIRFileModule(FileName
, Err
, Context
,
156 /* ShouldLazyLoadMetadata = */ true);
158 Err
.print("function-import", errs());
159 report_fatal_error("Abort");
165 /// Given a list of possible callee implementation for a call site, select one
166 /// that fits the \p Threshold.
168 /// FIXME: select "best" instead of first that fits. But what is "best"?
169 /// - The smallest: more likely to be inlined.
170 /// - The one with the least outgoing edges (already well optimized).
171 /// - One from a module already being imported from in order to reduce the
172 /// number of source modules parsed/linked.
173 /// - One that has PGO data attached.
174 /// - [insert you fancy metric here]
175 static const GlobalValueSummary
*
176 selectCallee(const ModuleSummaryIndex
&Index
,
177 ArrayRef
<std::unique_ptr
<GlobalValueSummary
>> CalleeSummaryList
,
178 unsigned Threshold
, StringRef CallerModulePath
,
179 FunctionImporter::ImportFailureReason
&Reason
,
180 GlobalValue::GUID GUID
) {
181 Reason
= FunctionImporter::ImportFailureReason::None
;
182 auto It
= llvm::find_if(
184 [&](const std::unique_ptr
<GlobalValueSummary
> &SummaryPtr
) {
185 auto *GVSummary
= SummaryPtr
.get();
186 if (!Index
.isGlobalValueLive(GVSummary
)) {
187 Reason
= FunctionImporter::ImportFailureReason::NotLive
;
191 // For SamplePGO, in computeImportForFunction the OriginalId
192 // may have been used to locate the callee summary list (See
194 // The mapping from OriginalId to GUID may return a GUID
195 // that corresponds to a static variable. Filter it out here.
196 // This can happen when
197 // 1) There is a call to a library function which is not defined
199 // 2) There is a static variable with the OriginalGUID identical
200 // to the GUID of the library function in 1);
201 // When this happens, the logic for SamplePGO kicks in and
202 // the static variable in 2) will be found, which needs to be
204 if (GVSummary
->getSummaryKind() == GlobalValueSummary::GlobalVarKind
) {
205 Reason
= FunctionImporter::ImportFailureReason::GlobalVar
;
208 if (GlobalValue::isInterposableLinkage(GVSummary
->linkage())) {
209 Reason
= FunctionImporter::ImportFailureReason::InterposableLinkage
;
210 // There is no point in importing these, we can't inline them
214 auto *Summary
= cast
<FunctionSummary
>(GVSummary
->getBaseObject());
216 // If this is a local function, make sure we import the copy
217 // in the caller's module. The only time a local function can
218 // share an entry in the index is if there is a local with the same name
219 // in another module that had the same source file name (in a different
220 // directory), where each was compiled in their own directory so there
221 // was not distinguishing path.
222 // However, do the import from another module if there is only one
223 // entry in the list - in that case this must be a reference due
224 // to indirect call profile data, since a function pointer can point to
225 // a local in another module.
226 if (GlobalValue::isLocalLinkage(Summary
->linkage()) &&
227 CalleeSummaryList
.size() > 1 &&
228 Summary
->modulePath() != CallerModulePath
) {
230 FunctionImporter::ImportFailureReason::LocalLinkageNotInModule
;
234 if (Summary
->instCount() > Threshold
) {
235 Reason
= FunctionImporter::ImportFailureReason::TooLarge
;
239 // Skip if it isn't legal to import (e.g. may reference unpromotable
241 if (Summary
->notEligibleToImport()) {
242 Reason
= FunctionImporter::ImportFailureReason::NotEligible
;
246 // Don't bother importing if we can't inline it anyway.
247 if (Summary
->fflags().NoInline
) {
248 Reason
= FunctionImporter::ImportFailureReason::NoInline
;
254 if (It
== CalleeSummaryList
.end())
257 return cast
<GlobalValueSummary
>(It
->get());
262 using EdgeInfo
= std::tuple
<const FunctionSummary
*, unsigned /* Threshold */,
265 } // anonymous namespace
268 updateValueInfoForIndirectCalls(const ModuleSummaryIndex
&Index
, ValueInfo VI
) {
269 if (!VI
.getSummaryList().empty())
271 // For SamplePGO, the indirect call targets for local functions will
272 // have its original name annotated in profile. We try to find the
273 // corresponding PGOFuncName as the GUID.
274 // FIXME: Consider updating the edges in the graph after building
275 // it, rather than needing to perform this mapping on each walk.
276 auto GUID
= Index
.getGUIDFromOriginalID(VI
.getGUID());
279 return Index
.getValueInfo(GUID
);
282 static void computeImportForReferencedGlobals(
283 const FunctionSummary
&Summary
, const GVSummaryMapTy
&DefinedGVSummaries
,
284 FunctionImporter::ImportMapTy
&ImportList
,
285 StringMap
<FunctionImporter::ExportSetTy
> *ExportLists
) {
286 for (auto &VI
: Summary
.refs()) {
287 if (DefinedGVSummaries
.count(VI
.getGUID())) {
289 dbgs() << "Ref ignored! Target already in destination module.\n");
293 LLVM_DEBUG(dbgs() << " ref -> " << VI
<< "\n");
295 // If this is a local variable, make sure we import the copy
296 // in the caller's module. The only time a local variable can
297 // share an entry in the index is if there is a local with the same name
298 // in another module that had the same source file name (in a different
299 // directory), where each was compiled in their own directory so there
300 // was not distinguishing path.
301 auto LocalNotInModule
= [&](const GlobalValueSummary
*RefSummary
) -> bool {
302 return GlobalValue::isLocalLinkage(RefSummary
->linkage()) &&
303 RefSummary
->modulePath() != Summary
.modulePath();
306 for (auto &RefSummary
: VI
.getSummaryList())
307 if (isa
<GlobalVarSummary
>(RefSummary
.get()) &&
308 canImportGlobalVar(RefSummary
.get()) &&
309 !LocalNotInModule(RefSummary
.get())) {
310 auto ILI
= ImportList
[RefSummary
->modulePath()].insert(VI
.getGUID());
311 // Only update stat if we haven't already imported this variable.
313 NumImportedGlobalVarsThinLink
++;
315 (*ExportLists
)[RefSummary
->modulePath()].insert(VI
.getGUID());
322 getFailureName(FunctionImporter::ImportFailureReason Reason
) {
324 case FunctionImporter::ImportFailureReason::None
:
326 case FunctionImporter::ImportFailureReason::GlobalVar
:
328 case FunctionImporter::ImportFailureReason::NotLive
:
330 case FunctionImporter::ImportFailureReason::TooLarge
:
332 case FunctionImporter::ImportFailureReason::InterposableLinkage
:
333 return "InterposableLinkage";
334 case FunctionImporter::ImportFailureReason::LocalLinkageNotInModule
:
335 return "LocalLinkageNotInModule";
336 case FunctionImporter::ImportFailureReason::NotEligible
:
337 return "NotEligible";
338 case FunctionImporter::ImportFailureReason::NoInline
:
341 llvm_unreachable("invalid reason");
344 /// Compute the list of functions to import for a given caller. Mark these
345 /// imported functions and the symbols they reference in their source module as
346 /// exported from their source module.
347 static void computeImportForFunction(
348 const FunctionSummary
&Summary
, const ModuleSummaryIndex
&Index
,
349 const unsigned Threshold
, const GVSummaryMapTy
&DefinedGVSummaries
,
350 SmallVectorImpl
<EdgeInfo
> &Worklist
,
351 FunctionImporter::ImportMapTy
&ImportList
,
352 StringMap
<FunctionImporter::ExportSetTy
> *ExportLists
,
353 FunctionImporter::ImportThresholdsTy
&ImportThresholds
) {
354 computeImportForReferencedGlobals(Summary
, DefinedGVSummaries
, ImportList
,
356 static int ImportCount
= 0;
357 for (auto &Edge
: Summary
.calls()) {
358 ValueInfo VI
= Edge
.first
;
359 LLVM_DEBUG(dbgs() << " edge -> " << VI
<< " Threshold:" << Threshold
362 if (ImportCutoff
>= 0 && ImportCount
>= ImportCutoff
) {
363 LLVM_DEBUG(dbgs() << "ignored! import-cutoff value of " << ImportCutoff
368 VI
= updateValueInfoForIndirectCalls(Index
, VI
);
372 if (DefinedGVSummaries
.count(VI
.getGUID())) {
373 LLVM_DEBUG(dbgs() << "ignored! Target already in destination module.\n");
377 auto GetBonusMultiplier
= [](CalleeInfo::HotnessType Hotness
) -> float {
378 if (Hotness
== CalleeInfo::HotnessType::Hot
)
379 return ImportHotMultiplier
;
380 if (Hotness
== CalleeInfo::HotnessType::Cold
)
381 return ImportColdMultiplier
;
382 if (Hotness
== CalleeInfo::HotnessType::Critical
)
383 return ImportCriticalMultiplier
;
387 const auto NewThreshold
=
388 Threshold
* GetBonusMultiplier(Edge
.second
.getHotness());
390 auto IT
= ImportThresholds
.insert(std::make_pair(
391 VI
.getGUID(), std::make_tuple(NewThreshold
, nullptr, nullptr)));
392 bool PreviouslyVisited
= !IT
.second
;
393 auto &ProcessedThreshold
= std::get
<0>(IT
.first
->second
);
394 auto &CalleeSummary
= std::get
<1>(IT
.first
->second
);
395 auto &FailureInfo
= std::get
<2>(IT
.first
->second
);
398 Edge
.second
.getHotness() == CalleeInfo::HotnessType::Hot
;
399 bool IsCriticalCallsite
=
400 Edge
.second
.getHotness() == CalleeInfo::HotnessType::Critical
;
402 const FunctionSummary
*ResolvedCalleeSummary
= nullptr;
404 assert(PreviouslyVisited
);
405 // Since the traversal of the call graph is DFS, we can revisit a function
406 // a second time with a higher threshold. In this case, it is added back
407 // to the worklist with the new threshold (so that its own callee chains
408 // can be considered with the higher threshold).
409 if (NewThreshold
<= ProcessedThreshold
) {
411 dbgs() << "ignored! Target was already imported with Threshold "
412 << ProcessedThreshold
<< "\n");
415 // Update with new larger threshold.
416 ProcessedThreshold
= NewThreshold
;
417 ResolvedCalleeSummary
= cast
<FunctionSummary
>(CalleeSummary
);
419 // If we already rejected importing a callee at the same or higher
420 // threshold, don't waste time calling selectCallee.
421 if (PreviouslyVisited
&& NewThreshold
<= ProcessedThreshold
) {
423 dbgs() << "ignored! Target was already rejected with Threshold "
424 << ProcessedThreshold
<< "\n");
425 if (PrintImportFailures
) {
426 assert(FailureInfo
&&
427 "Expected FailureInfo for previously rejected candidate");
428 FailureInfo
->Attempts
++;
433 FunctionImporter::ImportFailureReason Reason
;
434 CalleeSummary
= selectCallee(Index
, VI
.getSummaryList(), NewThreshold
,
435 Summary
.modulePath(), Reason
, VI
.getGUID());
436 if (!CalleeSummary
) {
437 // Update with new larger threshold if this was a retry (otherwise
438 // we would have already inserted with NewThreshold above). Also
439 // update failure info if requested.
440 if (PreviouslyVisited
) {
441 ProcessedThreshold
= NewThreshold
;
442 if (PrintImportFailures
) {
443 assert(FailureInfo
&&
444 "Expected FailureInfo for previously rejected candidate");
445 FailureInfo
->Reason
= Reason
;
446 FailureInfo
->Attempts
++;
447 FailureInfo
->MaxHotness
=
448 std::max(FailureInfo
->MaxHotness
, Edge
.second
.getHotness());
450 } else if (PrintImportFailures
) {
451 assert(!FailureInfo
&&
452 "Expected no FailureInfo for newly rejected candidate");
453 FailureInfo
= llvm::make_unique
<FunctionImporter::ImportFailureInfo
>(
454 VI
, Edge
.second
.getHotness(), Reason
, 1);
457 dbgs() << "ignored! No qualifying callee with summary found.\n");
461 // "Resolve" the summary
462 CalleeSummary
= CalleeSummary
->getBaseObject();
463 ResolvedCalleeSummary
= cast
<FunctionSummary
>(CalleeSummary
);
465 assert(ResolvedCalleeSummary
->instCount() <= NewThreshold
&&
466 "selectCallee() didn't honor the threshold");
468 auto ExportModulePath
= ResolvedCalleeSummary
->modulePath();
469 auto ILI
= ImportList
[ExportModulePath
].insert(VI
.getGUID());
470 // We previously decided to import this GUID definition if it was already
471 // inserted in the set of imports from the exporting module.
472 bool PreviouslyImported
= !ILI
.second
;
473 if (!PreviouslyImported
) {
474 NumImportedFunctionsThinLink
++;
476 NumImportedHotFunctionsThinLink
++;
477 if (IsCriticalCallsite
)
478 NumImportedCriticalFunctionsThinLink
++;
481 // Make exports in the source module.
483 auto &ExportList
= (*ExportLists
)[ExportModulePath
];
484 ExportList
.insert(VI
.getGUID());
485 if (!PreviouslyImported
) {
486 // This is the first time this function was exported from its source
487 // module, so mark all functions and globals it references as exported
488 // to the outside if they are defined in the same source module.
489 // For efficiency, we unconditionally add all the referenced GUIDs
490 // to the ExportList for this module, and will prune out any not
491 // defined in the module later in a single pass.
492 for (auto &Edge
: ResolvedCalleeSummary
->calls()) {
493 auto CalleeGUID
= Edge
.first
.getGUID();
494 ExportList
.insert(CalleeGUID
);
496 for (auto &Ref
: ResolvedCalleeSummary
->refs()) {
497 auto GUID
= Ref
.getGUID();
498 ExportList
.insert(GUID
);
504 auto GetAdjustedThreshold
= [](unsigned Threshold
, bool IsHotCallsite
) {
505 // Adjust the threshold for next level of imported functions.
506 // The threshold is different for hot callsites because we can then
507 // inline chains of hot calls.
509 return Threshold
* ImportHotInstrFactor
;
510 return Threshold
* ImportInstrFactor
;
513 const auto AdjThreshold
= GetAdjustedThreshold(Threshold
, IsHotCallsite
);
517 // Insert the newly imported function to the worklist.
518 Worklist
.emplace_back(ResolvedCalleeSummary
, AdjThreshold
, VI
.getGUID());
522 /// Given the list of globals defined in a module, compute the list of imports
523 /// as well as the list of "exports", i.e. the list of symbols referenced from
524 /// another module (that may require promotion).
525 static void ComputeImportForModule(
526 const GVSummaryMapTy
&DefinedGVSummaries
, const ModuleSummaryIndex
&Index
,
527 StringRef ModName
, FunctionImporter::ImportMapTy
&ImportList
,
528 StringMap
<FunctionImporter::ExportSetTy
> *ExportLists
= nullptr) {
529 // Worklist contains the list of function imported in this module, for which
530 // we will analyse the callees and may import further down the callgraph.
531 SmallVector
<EdgeInfo
, 128> Worklist
;
532 FunctionImporter::ImportThresholdsTy ImportThresholds
;
534 // Populate the worklist with the import for the functions in the current
536 for (auto &GVSummary
: DefinedGVSummaries
) {
538 // FIXME: Change the GVSummaryMapTy to hold ValueInfo instead of GUID
539 // so this map look up (and possibly others) can be avoided.
540 auto VI
= Index
.getValueInfo(GVSummary
.first
);
542 if (!Index
.isGlobalValueLive(GVSummary
.second
)) {
543 LLVM_DEBUG(dbgs() << "Ignores Dead GUID: " << VI
<< "\n");
547 dyn_cast
<FunctionSummary
>(GVSummary
.second
->getBaseObject());
549 // Skip import for global variables
551 LLVM_DEBUG(dbgs() << "Initialize import for " << VI
<< "\n");
552 computeImportForFunction(*FuncSummary
, Index
, ImportInstrLimit
,
553 DefinedGVSummaries
, Worklist
, ImportList
,
554 ExportLists
, ImportThresholds
);
557 // Process the newly imported functions and add callees to the worklist.
558 while (!Worklist
.empty()) {
559 auto FuncInfo
= Worklist
.pop_back_val();
560 auto *Summary
= std::get
<0>(FuncInfo
);
561 auto Threshold
= std::get
<1>(FuncInfo
);
563 computeImportForFunction(*Summary
, Index
, Threshold
, DefinedGVSummaries
,
564 Worklist
, ImportList
, ExportLists
,
568 // Print stats about functions considered but rejected for importing
570 if (PrintImportFailures
) {
571 dbgs() << "Missed imports into module " << ModName
<< "\n";
572 for (auto &I
: ImportThresholds
) {
573 auto &ProcessedThreshold
= std::get
<0>(I
.second
);
574 auto &CalleeSummary
= std::get
<1>(I
.second
);
575 auto &FailureInfo
= std::get
<2>(I
.second
);
577 continue; // We are going to import.
579 FunctionSummary
*FS
= nullptr;
580 if (!FailureInfo
->VI
.getSummaryList().empty())
581 FS
= dyn_cast
<FunctionSummary
>(
582 FailureInfo
->VI
.getSummaryList()[0]->getBaseObject());
583 dbgs() << FailureInfo
->VI
584 << ": Reason = " << getFailureName(FailureInfo
->Reason
)
585 << ", Threshold = " << ProcessedThreshold
586 << ", Size = " << (FS
? (int)FS
->instCount() : -1)
587 << ", MaxHotness = " << getHotnessName(FailureInfo
->MaxHotness
)
588 << ", Attempts = " << FailureInfo
->Attempts
<< "\n";
594 static bool isGlobalVarSummary(const ModuleSummaryIndex
&Index
,
595 GlobalValue::GUID G
) {
596 if (const auto &VI
= Index
.getValueInfo(G
)) {
597 auto SL
= VI
.getSummaryList();
599 return SL
[0]->getSummaryKind() == GlobalValueSummary::GlobalVarKind
;
604 static GlobalValue::GUID
getGUID(GlobalValue::GUID G
) { return G
; }
607 static unsigned numGlobalVarSummaries(const ModuleSummaryIndex
&Index
,
611 if (isGlobalVarSummary(Index
, getGUID(V
)))
617 /// Compute all the import and export for every module using the Index.
618 void llvm::ComputeCrossModuleImport(
619 const ModuleSummaryIndex
&Index
,
620 const StringMap
<GVSummaryMapTy
> &ModuleToDefinedGVSummaries
,
621 StringMap
<FunctionImporter::ImportMapTy
> &ImportLists
,
622 StringMap
<FunctionImporter::ExportSetTy
> &ExportLists
) {
623 // For each module that has function defined, compute the import/export lists.
624 for (auto &DefinedGVSummaries
: ModuleToDefinedGVSummaries
) {
625 auto &ImportList
= ImportLists
[DefinedGVSummaries
.first()];
626 LLVM_DEBUG(dbgs() << "Computing import for Module '"
627 << DefinedGVSummaries
.first() << "'\n");
628 ComputeImportForModule(DefinedGVSummaries
.second
, Index
,
629 DefinedGVSummaries
.first(), ImportList
,
633 // When computing imports we added all GUIDs referenced by anything
634 // imported from the module to its ExportList. Now we prune each ExportList
635 // of any not defined in that module. This is more efficient than checking
636 // while computing imports because some of the summary lists may be long
637 // due to linkonce (comdat) copies.
638 for (auto &ELI
: ExportLists
) {
639 const auto &DefinedGVSummaries
=
640 ModuleToDefinedGVSummaries
.lookup(ELI
.first());
641 for (auto EI
= ELI
.second
.begin(); EI
!= ELI
.second
.end();) {
642 if (!DefinedGVSummaries
.count(*EI
))
643 EI
= ELI
.second
.erase(EI
);
650 LLVM_DEBUG(dbgs() << "Import/Export lists for " << ImportLists
.size()
652 for (auto &ModuleImports
: ImportLists
) {
653 auto ModName
= ModuleImports
.first();
654 auto &Exports
= ExportLists
[ModName
];
655 unsigned NumGVS
= numGlobalVarSummaries(Index
, Exports
);
656 LLVM_DEBUG(dbgs() << "* Module " << ModName
<< " exports "
657 << Exports
.size() - NumGVS
<< " functions and " << NumGVS
658 << " vars. Imports from " << ModuleImports
.second
.size()
660 for (auto &Src
: ModuleImports
.second
) {
661 auto SrcModName
= Src
.first();
662 unsigned NumGVSPerMod
= numGlobalVarSummaries(Index
, Src
.second
);
663 LLVM_DEBUG(dbgs() << " - " << Src
.second
.size() - NumGVSPerMod
664 << " functions imported from " << SrcModName
<< "\n");
665 LLVM_DEBUG(dbgs() << " - " << NumGVSPerMod
666 << " global vars imported from " << SrcModName
<< "\n");
673 static void dumpImportListForModule(const ModuleSummaryIndex
&Index
,
674 StringRef ModulePath
,
675 FunctionImporter::ImportMapTy
&ImportList
) {
676 LLVM_DEBUG(dbgs() << "* Module " << ModulePath
<< " imports from "
677 << ImportList
.size() << " modules.\n");
678 for (auto &Src
: ImportList
) {
679 auto SrcModName
= Src
.first();
680 unsigned NumGVSPerMod
= numGlobalVarSummaries(Index
, Src
.second
);
681 LLVM_DEBUG(dbgs() << " - " << Src
.second
.size() - NumGVSPerMod
682 << " functions imported from " << SrcModName
<< "\n");
683 LLVM_DEBUG(dbgs() << " - " << NumGVSPerMod
<< " vars imported from "
684 << SrcModName
<< "\n");
689 /// Compute all the imports for the given module in the Index.
690 void llvm::ComputeCrossModuleImportForModule(
691 StringRef ModulePath
, const ModuleSummaryIndex
&Index
,
692 FunctionImporter::ImportMapTy
&ImportList
) {
693 // Collect the list of functions this module defines.
695 GVSummaryMapTy FunctionSummaryMap
;
696 Index
.collectDefinedFunctionsForModule(ModulePath
, FunctionSummaryMap
);
698 // Compute the import list for this module.
699 LLVM_DEBUG(dbgs() << "Computing import for Module '" << ModulePath
<< "'\n");
700 ComputeImportForModule(FunctionSummaryMap
, Index
, ModulePath
, ImportList
);
703 dumpImportListForModule(Index
, ModulePath
, ImportList
);
707 // Mark all external summaries in Index for import into the given module.
708 // Used for distributed builds using a distributed index.
709 void llvm::ComputeCrossModuleImportForModuleFromIndex(
710 StringRef ModulePath
, const ModuleSummaryIndex
&Index
,
711 FunctionImporter::ImportMapTy
&ImportList
) {
712 for (auto &GlobalList
: Index
) {
713 // Ignore entries for undefined references.
714 if (GlobalList
.second
.SummaryList
.empty())
717 auto GUID
= GlobalList
.first
;
718 assert(GlobalList
.second
.SummaryList
.size() == 1 &&
719 "Expected individual combined index to have one summary per GUID");
720 auto &Summary
= GlobalList
.second
.SummaryList
[0];
721 // Skip the summaries for the importing module. These are included to
722 // e.g. record required linkage changes.
723 if (Summary
->modulePath() == ModulePath
)
725 // Add an entry to provoke importing by thinBackend.
726 ImportList
[Summary
->modulePath()].insert(GUID
);
729 dumpImportListForModule(Index
, ModulePath
, ImportList
);
733 void llvm::computeDeadSymbols(
734 ModuleSummaryIndex
&Index
,
735 const DenseSet
<GlobalValue::GUID
> &GUIDPreservedSymbols
,
736 function_ref
<PrevailingType(GlobalValue::GUID
)> isPrevailing
) {
737 assert(!Index
.withGlobalValueDeadStripping());
740 if (GUIDPreservedSymbols
.empty())
741 // Don't do anything when nothing is live, this is friendly with tests.
743 unsigned LiveSymbols
= 0;
744 SmallVector
<ValueInfo
, 128> Worklist
;
745 Worklist
.reserve(GUIDPreservedSymbols
.size() * 2);
746 for (auto GUID
: GUIDPreservedSymbols
) {
747 ValueInfo VI
= Index
.getValueInfo(GUID
);
750 for (auto &S
: VI
.getSummaryList())
754 // Add values flagged in the index as live roots to the worklist.
755 for (const auto &Entry
: Index
) {
756 auto VI
= Index
.getValueInfo(Entry
);
757 for (auto &S
: Entry
.second
.SummaryList
)
759 LLVM_DEBUG(dbgs() << "Live root: " << VI
<< "\n");
760 Worklist
.push_back(VI
);
766 // Make value live and add it to the worklist if it was not live before.
767 auto visit
= [&](ValueInfo VI
) {
768 // FIXME: If we knew which edges were created for indirect call profiles,
769 // we could skip them here. Any that are live should be reached via
770 // other edges, e.g. reference edges. Otherwise, using a profile collected
771 // on a slightly different binary might provoke preserving, importing
772 // and ultimately promoting calls to functions not linked into this
773 // binary, which increases the binary size unnecessarily. Note that
774 // if this code changes, the importer needs to change so that edges
775 // to functions marked dead are skipped.
776 VI
= updateValueInfoForIndirectCalls(Index
, VI
);
780 if (llvm::any_of(VI
.getSummaryList(),
781 [](const std::unique_ptr
<llvm::GlobalValueSummary
> &S
) {
786 // We only keep live symbols that are known to be non-prevailing if any are
787 // available_externally, linkonceodr, weakodr. Those symbols are discarded
788 // later in the EliminateAvailableExternally pass and setting them to
789 // not-live could break downstreams users of liveness information (PR36483)
790 // or limit optimization opportunities.
791 if (isPrevailing(VI
.getGUID()) == PrevailingType::No
) {
792 bool KeepAliveLinkage
= false;
793 bool Interposable
= false;
794 for (auto &S
: VI
.getSummaryList()) {
795 if (S
->linkage() == GlobalValue::AvailableExternallyLinkage
||
796 S
->linkage() == GlobalValue::WeakODRLinkage
||
797 S
->linkage() == GlobalValue::LinkOnceODRLinkage
)
798 KeepAliveLinkage
= true;
799 else if (GlobalValue::isInterposableLinkage(S
->linkage()))
803 if (!KeepAliveLinkage
)
808 "Interposable and available_externally/linkonce_odr/weak_odr symbol");
811 for (auto &S
: VI
.getSummaryList())
814 Worklist
.push_back(VI
);
817 while (!Worklist
.empty()) {
818 auto VI
= Worklist
.pop_back_val();
819 for (auto &Summary
: VI
.getSummaryList()) {
820 if (auto *AS
= dyn_cast
<AliasSummary
>(Summary
.get())) {
821 // If this is an alias, visit the aliasee VI to ensure that all copies
822 // are marked live and it is added to the worklist for further
823 // processing of its references.
824 // FIXME: The aliasee GUID is only populated in the summary when we
825 // read them from bitcode, which is currently the only way we can
826 // get here (we don't yet support reading the summary index directly
827 // from LLVM assembly code in tools that can perform a thin link).
828 // If that ever changes, the below call to getAliaseGUID will assert.
829 visit(Index
.getValueInfo(AS
->getAliaseeGUID()));
833 Summary
->setLive(true);
834 for (auto Ref
: Summary
->refs())
836 if (auto *FS
= dyn_cast
<FunctionSummary
>(Summary
.get()))
837 for (auto Call
: FS
->calls())
841 Index
.setWithGlobalValueDeadStripping();
843 unsigned DeadSymbols
= Index
.size() - LiveSymbols
;
844 LLVM_DEBUG(dbgs() << LiveSymbols
<< " symbols Live, and " << DeadSymbols
845 << " symbols Dead \n");
846 NumDeadSymbols
+= DeadSymbols
;
847 NumLiveSymbols
+= LiveSymbols
;
850 // Compute dead symbols and propagate constants in combined index.
851 void llvm::computeDeadSymbolsWithConstProp(
852 ModuleSummaryIndex
&Index
,
853 const DenseSet
<GlobalValue::GUID
> &GUIDPreservedSymbols
,
854 function_ref
<PrevailingType(GlobalValue::GUID
)> isPrevailing
,
855 bool ImportEnabled
) {
856 computeDeadSymbols(Index
, GUIDPreservedSymbols
, isPrevailing
);
858 Index
.propagateConstants(GUIDPreservedSymbols
);
860 // If import is disabled we should drop read-only attribute
861 // from all summaries to prevent internalization.
862 for (auto &P
: Index
)
863 for (auto &S
: P
.second
.SummaryList
)
864 if (auto *GVS
= dyn_cast
<GlobalVarSummary
>(S
.get()))
865 GVS
->setReadOnly(false);
869 /// Compute the set of summaries needed for a ThinLTO backend compilation of
871 void llvm::gatherImportedSummariesForModule(
872 StringRef ModulePath
,
873 const StringMap
<GVSummaryMapTy
> &ModuleToDefinedGVSummaries
,
874 const FunctionImporter::ImportMapTy
&ImportList
,
875 std::map
<std::string
, GVSummaryMapTy
> &ModuleToSummariesForIndex
) {
876 // Include all summaries from the importing module.
877 ModuleToSummariesForIndex
[ModulePath
] =
878 ModuleToDefinedGVSummaries
.lookup(ModulePath
);
879 // Include summaries for imports.
880 for (auto &ILI
: ImportList
) {
881 auto &SummariesForIndex
= ModuleToSummariesForIndex
[ILI
.first()];
882 const auto &DefinedGVSummaries
=
883 ModuleToDefinedGVSummaries
.lookup(ILI
.first());
884 for (auto &GI
: ILI
.second
) {
885 const auto &DS
= DefinedGVSummaries
.find(GI
);
886 assert(DS
!= DefinedGVSummaries
.end() &&
887 "Expected a defined summary for imported global value");
888 SummariesForIndex
[GI
] = DS
->second
;
893 /// Emit the files \p ModulePath will import from into \p OutputFilename.
894 std::error_code
llvm::EmitImportsFiles(
895 StringRef ModulePath
, StringRef OutputFilename
,
896 const std::map
<std::string
, GVSummaryMapTy
> &ModuleToSummariesForIndex
) {
898 raw_fd_ostream
ImportsOS(OutputFilename
, EC
, sys::fs::OpenFlags::F_None
);
901 for (auto &ILI
: ModuleToSummariesForIndex
)
902 // The ModuleToSummariesForIndex map includes an entry for the current
903 // Module (needed for writing out the index files). We don't want to
904 // include it in the imports file, however, so filter it out.
905 if (ILI
.first
!= ModulePath
)
906 ImportsOS
<< ILI
.first
<< "\n";
907 return std::error_code();
910 bool llvm::convertToDeclaration(GlobalValue
&GV
) {
911 LLVM_DEBUG(dbgs() << "Converting to a declaration: `" << GV
.getName()
913 if (Function
*F
= dyn_cast
<Function
>(&GV
)) {
916 F
->setComdat(nullptr);
917 } else if (GlobalVariable
*V
= dyn_cast
<GlobalVariable
>(&GV
)) {
918 V
->setInitializer(nullptr);
919 V
->setLinkage(GlobalValue::ExternalLinkage
);
921 V
->setComdat(nullptr);
924 if (GV
.getValueType()->isFunctionTy())
926 Function::Create(cast
<FunctionType
>(GV
.getValueType()),
927 GlobalValue::ExternalLinkage
, GV
.getAddressSpace(),
931 new GlobalVariable(*GV
.getParent(), GV
.getValueType(),
932 /*isConstant*/ false, GlobalValue::ExternalLinkage
,
933 /*init*/ nullptr, "",
934 /*insertbefore*/ nullptr, GV
.getThreadLocalMode(),
935 GV
.getType()->getAddressSpace());
936 NewGV
->takeName(&GV
);
937 GV
.replaceAllUsesWith(NewGV
);
943 /// Fixup prevailing symbol linkages in \p TheModule based on summary analysis.
944 void llvm::thinLTOResolvePrevailingInModule(
945 Module
&TheModule
, const GVSummaryMapTy
&DefinedGlobals
) {
946 auto updateLinkage
= [&](GlobalValue
&GV
) {
947 // See if the global summary analysis computed a new resolved linkage.
948 const auto &GS
= DefinedGlobals
.find(GV
.getGUID());
949 if (GS
== DefinedGlobals
.end())
951 auto NewLinkage
= GS
->second
->linkage();
952 if (NewLinkage
== GV
.getLinkage())
955 // Switch the linkage to weakany if asked for, e.g. we do this for
956 // linker redefined symbols (via --wrap or --defsym).
957 // We record that the visibility should be changed here in `addThinLTO`
958 // as we need access to the resolution vectors for each input file in
959 // order to find which symbols have been redefined.
960 // We may consider reorganizing this code and moving the linkage recording
961 // somewhere else, e.g. in thinLTOResolvePrevailingInIndex.
962 if (NewLinkage
== GlobalValue::WeakAnyLinkage
) {
963 GV
.setLinkage(NewLinkage
);
967 if (GlobalValue::isLocalLinkage(GV
.getLinkage()) ||
968 // In case it was dead and already converted to declaration.
971 // Check for a non-prevailing def that has interposable linkage
972 // (e.g. non-odr weak or linkonce). In that case we can't simply
973 // convert to available_externally, since it would lose the
974 // interposable property and possibly get inlined. Simply drop
975 // the definition in that case.
976 if (GlobalValue::isAvailableExternallyLinkage(NewLinkage
) &&
977 GlobalValue::isInterposableLinkage(GV
.getLinkage())) {
978 if (!convertToDeclaration(GV
))
979 // FIXME: Change this to collect replaced GVs and later erase
980 // them from the parent module once thinLTOResolvePrevailingGUID is
981 // changed to enable this for aliases.
982 llvm_unreachable("Expected GV to be converted");
984 // If the original symbols has global unnamed addr and linkonce_odr linkage,
985 // it should be an auto hide symbol. Add hidden visibility to the symbol to
986 // preserve the property.
987 if (GV
.hasLinkOnceODRLinkage() && GV
.hasGlobalUnnamedAddr() &&
988 NewLinkage
== GlobalValue::WeakODRLinkage
)
989 GV
.setVisibility(GlobalValue::HiddenVisibility
);
991 LLVM_DEBUG(dbgs() << "ODR fixing up linkage for `" << GV
.getName()
992 << "` from " << GV
.getLinkage() << " to " << NewLinkage
994 GV
.setLinkage(NewLinkage
);
996 // Remove declarations from comdats, including available_externally
997 // as this is a declaration for the linker, and will be dropped eventually.
998 // It is illegal for comdats to contain declarations.
999 auto *GO
= dyn_cast_or_null
<GlobalObject
>(&GV
);
1000 if (GO
&& GO
->isDeclarationForLinker() && GO
->hasComdat())
1001 GO
->setComdat(nullptr);
1004 // Process functions and global now
1005 for (auto &GV
: TheModule
)
1007 for (auto &GV
: TheModule
.globals())
1009 for (auto &GV
: TheModule
.aliases())
1013 /// Run internalization on \p TheModule based on symmary analysis.
1014 void llvm::thinLTOInternalizeModule(Module
&TheModule
,
1015 const GVSummaryMapTy
&DefinedGlobals
) {
1016 // Declare a callback for the internalize pass that will ask for every
1017 // candidate GlobalValue if it can be internalized or not.
1018 auto MustPreserveGV
= [&](const GlobalValue
&GV
) -> bool {
1019 // Lookup the linkage recorded in the summaries during global analysis.
1020 auto GS
= DefinedGlobals
.find(GV
.getGUID());
1021 if (GS
== DefinedGlobals
.end()) {
1022 // Must have been promoted (possibly conservatively). Find original
1023 // name so that we can access the correct summary and see if it can
1024 // be internalized again.
1025 // FIXME: Eventually we should control promotion instead of promoting
1026 // and internalizing again.
1027 StringRef OrigName
=
1028 ModuleSummaryIndex::getOriginalNameBeforePromote(GV
.getName());
1029 std::string OrigId
= GlobalValue::getGlobalIdentifier(
1030 OrigName
, GlobalValue::InternalLinkage
,
1031 TheModule
.getSourceFileName());
1032 GS
= DefinedGlobals
.find(GlobalValue::getGUID(OrigId
));
1033 if (GS
== DefinedGlobals
.end()) {
1034 // Also check the original non-promoted non-globalized name. In some
1035 // cases a preempted weak value is linked in as a local copy because
1036 // it is referenced by an alias (IRLinker::linkGlobalValueProto).
1037 // In that case, since it was originally not a local value, it was
1038 // recorded in the index using the original name.
1039 // FIXME: This may not be needed once PR27866 is fixed.
1040 GS
= DefinedGlobals
.find(GlobalValue::getGUID(OrigName
));
1041 assert(GS
!= DefinedGlobals
.end());
1044 return !GlobalValue::isLocalLinkage(GS
->second
->linkage());
1047 // FIXME: See if we can just internalize directly here via linkage changes
1048 // based on the index, rather than invoking internalizeModule.
1049 internalizeModule(TheModule
, MustPreserveGV
);
1052 /// Make alias a clone of its aliasee.
1053 static Function
*replaceAliasWithAliasee(Module
*SrcModule
, GlobalAlias
*GA
) {
1054 Function
*Fn
= cast
<Function
>(GA
->getBaseObject());
1056 ValueToValueMapTy VMap
;
1057 Function
*NewFn
= CloneFunction(Fn
, VMap
);
1058 // Clone should use the original alias's linkage and name, and we ensure
1059 // all uses of alias instead use the new clone (casted if necessary).
1060 NewFn
->setLinkage(GA
->getLinkage());
1061 GA
->replaceAllUsesWith(ConstantExpr::getBitCast(NewFn
, GA
->getType()));
1062 NewFn
->takeName(GA
);
1066 // Internalize values that we marked with specific attribute
1067 // in processGlobalForThinLTO.
1068 static void internalizeImmutableGVs(Module
&M
) {
1069 for (auto &GV
: M
.globals())
1070 // Skip GVs which have been converted to declarations
1071 // by dropDeadSymbols.
1072 if (!GV
.isDeclaration() && GV
.hasAttribute("thinlto-internalize")) {
1073 GV
.setLinkage(GlobalValue::InternalLinkage
);
1074 GV
.setVisibility(GlobalValue::DefaultVisibility
);
1078 // Automatically import functions in Module \p DestModule based on the summaries
1080 Expected
<bool> FunctionImporter::importFunctions(
1081 Module
&DestModule
, const FunctionImporter::ImportMapTy
&ImportList
) {
1082 LLVM_DEBUG(dbgs() << "Starting import for Module "
1083 << DestModule
.getModuleIdentifier() << "\n");
1084 unsigned ImportedCount
= 0, ImportedGVCount
= 0;
1086 IRMover
Mover(DestModule
);
1087 // Do the actual import of functions now, one Module at a time
1088 std::set
<StringRef
> ModuleNameOrderedList
;
1089 for (auto &FunctionsToImportPerModule
: ImportList
) {
1090 ModuleNameOrderedList
.insert(FunctionsToImportPerModule
.first());
1092 for (auto &Name
: ModuleNameOrderedList
) {
1093 // Get the module for the import
1094 const auto &FunctionsToImportPerModule
= ImportList
.find(Name
);
1095 assert(FunctionsToImportPerModule
!= ImportList
.end());
1096 Expected
<std::unique_ptr
<Module
>> SrcModuleOrErr
= ModuleLoader(Name
);
1097 if (!SrcModuleOrErr
)
1098 return SrcModuleOrErr
.takeError();
1099 std::unique_ptr
<Module
> SrcModule
= std::move(*SrcModuleOrErr
);
1100 assert(&DestModule
.getContext() == &SrcModule
->getContext() &&
1101 "Context mismatch");
1103 // If modules were created with lazy metadata loading, materialize it
1104 // now, before linking it (otherwise this will be a noop).
1105 if (Error Err
= SrcModule
->materializeMetadata())
1106 return std::move(Err
);
1108 auto &ImportGUIDs
= FunctionsToImportPerModule
->second
;
1109 // Find the globals to import
1110 SetVector
<GlobalValue
*> GlobalsToImport
;
1111 for (Function
&F
: *SrcModule
) {
1114 auto GUID
= F
.getGUID();
1115 auto Import
= ImportGUIDs
.count(GUID
);
1116 LLVM_DEBUG(dbgs() << (Import
? "Is" : "Not") << " importing function "
1117 << GUID
<< " " << F
.getName() << " from "
1118 << SrcModule
->getSourceFileName() << "\n");
1120 if (Error Err
= F
.materialize())
1121 return std::move(Err
);
1122 if (EnableImportMetadata
) {
1123 // Add 'thinlto_src_module' metadata for statistics and debugging.
1125 "thinlto_src_module",
1126 MDNode::get(DestModule
.getContext(),
1127 {MDString::get(DestModule
.getContext(),
1128 SrcModule
->getSourceFileName())}));
1130 GlobalsToImport
.insert(&F
);
1133 for (GlobalVariable
&GV
: SrcModule
->globals()) {
1136 auto GUID
= GV
.getGUID();
1137 auto Import
= ImportGUIDs
.count(GUID
);
1138 LLVM_DEBUG(dbgs() << (Import
? "Is" : "Not") << " importing global "
1139 << GUID
<< " " << GV
.getName() << " from "
1140 << SrcModule
->getSourceFileName() << "\n");
1142 if (Error Err
= GV
.materialize())
1143 return std::move(Err
);
1144 ImportedGVCount
+= GlobalsToImport
.insert(&GV
);
1147 for (GlobalAlias
&GA
: SrcModule
->aliases()) {
1150 auto GUID
= GA
.getGUID();
1151 auto Import
= ImportGUIDs
.count(GUID
);
1152 LLVM_DEBUG(dbgs() << (Import
? "Is" : "Not") << " importing alias "
1153 << GUID
<< " " << GA
.getName() << " from "
1154 << SrcModule
->getSourceFileName() << "\n");
1156 if (Error Err
= GA
.materialize())
1157 return std::move(Err
);
1158 // Import alias as a copy of its aliasee.
1159 GlobalObject
*Base
= GA
.getBaseObject();
1160 if (Error Err
= Base
->materialize())
1161 return std::move(Err
);
1162 auto *Fn
= replaceAliasWithAliasee(SrcModule
.get(), &GA
);
1163 LLVM_DEBUG(dbgs() << "Is importing aliasee fn " << Base
->getGUID()
1164 << " " << Base
->getName() << " from "
1165 << SrcModule
->getSourceFileName() << "\n");
1166 if (EnableImportMetadata
) {
1167 // Add 'thinlto_src_module' metadata for statistics and debugging.
1169 "thinlto_src_module",
1170 MDNode::get(DestModule
.getContext(),
1171 {MDString::get(DestModule
.getContext(),
1172 SrcModule
->getSourceFileName())}));
1174 GlobalsToImport
.insert(Fn
);
1178 // Upgrade debug info after we're done materializing all the globals and we
1179 // have loaded all the required metadata!
1180 UpgradeDebugInfo(*SrcModule
);
1182 // Link in the specified functions.
1183 if (renameModuleForThinLTO(*SrcModule
, Index
, &GlobalsToImport
))
1187 for (const auto *GV
: GlobalsToImport
)
1188 dbgs() << DestModule
.getSourceFileName() << ": Import " << GV
->getName()
1189 << " from " << SrcModule
->getSourceFileName() << "\n";
1192 if (Mover
.move(std::move(SrcModule
), GlobalsToImport
.getArrayRef(),
1193 [](GlobalValue
&, IRMover::ValueAdder
) {},
1194 /*IsPerformingImport=*/true))
1195 report_fatal_error("Function Import: link error");
1197 ImportedCount
+= GlobalsToImport
.size();
1198 NumImportedModules
++;
1201 internalizeImmutableGVs(DestModule
);
1203 NumImportedFunctions
+= (ImportedCount
- ImportedGVCount
);
1204 NumImportedGlobalVars
+= ImportedGVCount
;
1206 LLVM_DEBUG(dbgs() << "Imported " << ImportedCount
- ImportedGVCount
1207 << " functions for Module "
1208 << DestModule
.getModuleIdentifier() << "\n");
1209 LLVM_DEBUG(dbgs() << "Imported " << ImportedGVCount
1210 << " global variables for Module "
1211 << DestModule
.getModuleIdentifier() << "\n");
1212 return ImportedCount
;
1215 static bool doImportingForModule(Module
&M
) {
1216 if (SummaryFile
.empty())
1217 report_fatal_error("error: -function-import requires -summary-file\n");
1218 Expected
<std::unique_ptr
<ModuleSummaryIndex
>> IndexPtrOrErr
=
1219 getModuleSummaryIndexForFile(SummaryFile
);
1220 if (!IndexPtrOrErr
) {
1221 logAllUnhandledErrors(IndexPtrOrErr
.takeError(), errs(),
1222 "Error loading file '" + SummaryFile
+ "': ");
1225 std::unique_ptr
<ModuleSummaryIndex
> Index
= std::move(*IndexPtrOrErr
);
1227 // First step is collecting the import list.
1228 FunctionImporter::ImportMapTy ImportList
;
1229 // If requested, simply import all functions in the index. This is used
1230 // when testing distributed backend handling via the opt tool, when
1231 // we have distributed indexes containing exactly the summaries to import.
1233 ComputeCrossModuleImportForModuleFromIndex(M
.getModuleIdentifier(), *Index
,
1236 ComputeCrossModuleImportForModule(M
.getModuleIdentifier(), *Index
,
1239 // Conservatively mark all internal values as promoted. This interface is
1240 // only used when doing importing via the function importing pass. The pass
1241 // is only enabled when testing importing via the 'opt' tool, which does
1242 // not do the ThinLink that would normally determine what values to promote.
1243 for (auto &I
: *Index
) {
1244 for (auto &S
: I
.second
.SummaryList
) {
1245 if (GlobalValue::isLocalLinkage(S
->linkage()))
1246 S
->setLinkage(GlobalValue::ExternalLinkage
);
1250 // Next we need to promote to global scope and rename any local values that
1251 // are potentially exported to other modules.
1252 if (renameModuleForThinLTO(M
, *Index
, nullptr)) {
1253 errs() << "Error renaming module\n";
1257 // Perform the import now.
1258 auto ModuleLoader
= [&M
](StringRef Identifier
) {
1259 return loadFile(Identifier
, M
.getContext());
1261 FunctionImporter
Importer(*Index
, ModuleLoader
);
1262 Expected
<bool> Result
= Importer
.importFunctions(M
, ImportList
);
1264 // FIXME: Probably need to propagate Errors through the pass manager.
1266 logAllUnhandledErrors(Result
.takeError(), errs(),
1267 "Error importing module: ");
1276 /// Pass that performs cross-module function import provided a summary file.
1277 class FunctionImportLegacyPass
: public ModulePass
{
1279 /// Pass identification, replacement for typeid
1282 explicit FunctionImportLegacyPass() : ModulePass(ID
) {}
1284 /// Specify pass name for debug output
1285 StringRef
getPassName() const override
{ return "Function Importing"; }
1287 bool runOnModule(Module
&M
) override
{
1291 return doImportingForModule(M
);
1295 } // end anonymous namespace
1297 PreservedAnalyses
FunctionImportPass::run(Module
&M
,
1298 ModuleAnalysisManager
&AM
) {
1299 if (!doImportingForModule(M
))
1300 return PreservedAnalyses::all();
1302 return PreservedAnalyses::none();
1305 char FunctionImportLegacyPass::ID
= 0;
1306 INITIALIZE_PASS(FunctionImportLegacyPass
, "function-import",
1307 "Summary Based Function Import", false, false)
1311 Pass
*createFunctionImportPass() {
1312 return new FunctionImportLegacyPass();
1315 } // end namespace llvm