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/Bitcode/BitcodeReader.h"
22 #include "llvm/IR/AutoUpgrade.h"
23 #include "llvm/IR/Constants.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/IR/GlobalAlias.h"
26 #include "llvm/IR/GlobalObject.h"
27 #include "llvm/IR/GlobalValue.h"
28 #include "llvm/IR/GlobalVariable.h"
29 #include "llvm/IR/Metadata.h"
30 #include "llvm/IR/Module.h"
31 #include "llvm/IR/ModuleSummaryIndex.h"
32 #include "llvm/IRReader/IRReader.h"
33 #include "llvm/Linker/IRMover.h"
34 #include "llvm/Support/Casting.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/Errc.h"
38 #include "llvm/Support/Error.h"
39 #include "llvm/Support/ErrorHandling.h"
40 #include "llvm/Support/FileSystem.h"
41 #include "llvm/Support/SourceMgr.h"
42 #include "llvm/Support/raw_ostream.h"
43 #include "llvm/Transforms/IPO/Internalize.h"
44 #include "llvm/Transforms/Utils/Cloning.h"
45 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
46 #include "llvm/Transforms/Utils/ValueMapper.h"
51 #include <system_error>
57 #define DEBUG_TYPE "function-import"
59 STATISTIC(NumImportedFunctionsThinLink
,
60 "Number of functions thin link decided to import");
61 STATISTIC(NumImportedHotFunctionsThinLink
,
62 "Number of hot functions thin link decided to import");
63 STATISTIC(NumImportedCriticalFunctionsThinLink
,
64 "Number of critical functions thin link decided to import");
65 STATISTIC(NumImportedGlobalVarsThinLink
,
66 "Number of global variables thin link decided to import");
67 STATISTIC(NumImportedFunctions
, "Number of functions imported in backend");
68 STATISTIC(NumImportedGlobalVars
,
69 "Number of global variables imported in backend");
70 STATISTIC(NumImportedModules
, "Number of modules imported from");
71 STATISTIC(NumDeadSymbols
, "Number of dead stripped symbols in index");
72 STATISTIC(NumLiveSymbols
, "Number of live symbols in index");
74 /// Limit on instruction count of imported functions.
75 static cl::opt
<unsigned> ImportInstrLimit(
76 "import-instr-limit", cl::init(100), cl::Hidden
, cl::value_desc("N"),
77 cl::desc("Only import functions with less than N instructions"));
79 static cl::opt
<int> ImportCutoff(
80 "import-cutoff", cl::init(-1), cl::Hidden
, cl::value_desc("N"),
81 cl::desc("Only import first N functions if N>=0 (default -1)"));
84 ForceImportAll("force-import-all", cl::init(false), cl::Hidden
,
85 cl::desc("Import functions with noinline attribute"));
88 ImportInstrFactor("import-instr-evolution-factor", cl::init(0.7),
89 cl::Hidden
, cl::value_desc("x"),
90 cl::desc("As we import functions, multiply the "
91 "`import-instr-limit` threshold by this factor "
92 "before processing newly imported functions"));
94 static cl::opt
<float> ImportHotInstrFactor(
95 "import-hot-evolution-factor", cl::init(1.0), cl::Hidden
,
97 cl::desc("As we import functions called from hot callsite, multiply the "
98 "`import-instr-limit` threshold by this factor "
99 "before processing newly imported functions"));
101 static cl::opt
<float> ImportHotMultiplier(
102 "import-hot-multiplier", cl::init(10.0), cl::Hidden
, cl::value_desc("x"),
103 cl::desc("Multiply the `import-instr-limit` threshold for hot callsites"));
105 static cl::opt
<float> ImportCriticalMultiplier(
106 "import-critical-multiplier", cl::init(100.0), cl::Hidden
,
109 "Multiply the `import-instr-limit` threshold for critical callsites"));
111 // FIXME: This multiplier was not really tuned up.
112 static cl::opt
<float> ImportColdMultiplier(
113 "import-cold-multiplier", cl::init(0), cl::Hidden
, cl::value_desc("N"),
114 cl::desc("Multiply the `import-instr-limit` threshold for cold callsites"));
116 static cl::opt
<bool> PrintImports("print-imports", cl::init(false), cl::Hidden
,
117 cl::desc("Print imported functions"));
119 static cl::opt
<bool> PrintImportFailures(
120 "print-import-failures", cl::init(false), cl::Hidden
,
121 cl::desc("Print information for functions rejected for importing"));
123 static cl::opt
<bool> ComputeDead("compute-dead", cl::init(true), cl::Hidden
,
124 cl::desc("Compute dead symbols"));
126 static cl::opt
<bool> EnableImportMetadata(
127 "enable-import-metadata", cl::init(false), cl::Hidden
,
128 cl::desc("Enable import metadata like 'thinlto_src_module'"));
130 /// Summary file to use for function importing when using -function-import from
131 /// the command line.
132 static cl::opt
<std::string
>
133 SummaryFile("summary-file",
134 cl::desc("The summary file to use for function importing."));
136 /// Used when testing importing from distributed indexes via opt
139 ImportAllIndex("import-all-index",
140 cl::desc("Import all external functions in index."));
142 // Load lazily a module from \p FileName in \p Context.
143 static std::unique_ptr
<Module
> loadFile(const std::string
&FileName
,
144 LLVMContext
&Context
) {
146 LLVM_DEBUG(dbgs() << "Loading '" << FileName
<< "'\n");
147 // Metadata isn't loaded until functions are imported, to minimize
148 // the memory overhead.
149 std::unique_ptr
<Module
> Result
=
150 getLazyIRFileModule(FileName
, Err
, Context
,
151 /* ShouldLazyLoadMetadata = */ true);
153 Err
.print("function-import", errs());
154 report_fatal_error("Abort");
160 /// Given a list of possible callee implementation for a call site, qualify the
161 /// legality of importing each. The return is a range of pairs. Each pair
162 /// corresponds to a candidate. The first value is the ImportFailureReason for
163 /// that candidate, the second is the candidate.
164 static auto qualifyCalleeCandidates(
165 const ModuleSummaryIndex
&Index
,
166 ArrayRef
<std::unique_ptr
<GlobalValueSummary
>> CalleeSummaryList
,
167 StringRef CallerModulePath
) {
168 return llvm::map_range(
170 [&Index
, CalleeSummaryList
,
171 CallerModulePath
](const std::unique_ptr
<GlobalValueSummary
> &SummaryPtr
)
172 -> std::pair
<FunctionImporter::ImportFailureReason
,
173 const GlobalValueSummary
*> {
174 auto *GVSummary
= SummaryPtr
.get();
175 if (!Index
.isGlobalValueLive(GVSummary
))
176 return {FunctionImporter::ImportFailureReason::NotLive
, GVSummary
};
178 if (GlobalValue::isInterposableLinkage(GVSummary
->linkage()))
179 return {FunctionImporter::ImportFailureReason::InterposableLinkage
,
182 auto *Summary
= dyn_cast
<FunctionSummary
>(GVSummary
->getBaseObject());
184 // Ignore any callees that aren't actually functions. This could happen
185 // in the case of GUID hash collisions. It could also happen in theory
186 // for SamplePGO profiles collected on old versions of the code after
187 // renaming, since we synthesize edges to any inlined callees appearing
190 return {FunctionImporter::ImportFailureReason::GlobalVar
, GVSummary
};
192 // If this is a local function, make sure we import the copy
193 // in the caller's module. The only time a local function can
194 // share an entry in the index is if there is a local with the same name
195 // in another module that had the same source file name (in a different
196 // directory), where each was compiled in their own directory so there
197 // was not distinguishing path.
198 // However, do the import from another module if there is only one
199 // entry in the list - in that case this must be a reference due
200 // to indirect call profile data, since a function pointer can point to
201 // a local in another module.
202 if (GlobalValue::isLocalLinkage(Summary
->linkage()) &&
203 CalleeSummaryList
.size() > 1 &&
204 Summary
->modulePath() != CallerModulePath
)
206 FunctionImporter::ImportFailureReason::LocalLinkageNotInModule
,
209 // Skip if it isn't legal to import (e.g. may reference unpromotable
211 if (Summary
->notEligibleToImport())
212 return {FunctionImporter::ImportFailureReason::NotEligible
,
215 return {FunctionImporter::ImportFailureReason::None
, GVSummary
};
219 /// Given a list of possible callee implementation for a call site, select one
220 /// that fits the \p Threshold. If none are found, the Reason will give the last
221 /// reason for the failure (last, in the order of CalleeSummaryList entries).
223 /// FIXME: select "best" instead of first that fits. But what is "best"?
224 /// - The smallest: more likely to be inlined.
225 /// - The one with the least outgoing edges (already well optimized).
226 /// - One from a module already being imported from in order to reduce the
227 /// number of source modules parsed/linked.
228 /// - One that has PGO data attached.
229 /// - [insert you fancy metric here]
230 static const GlobalValueSummary
*
231 selectCallee(const ModuleSummaryIndex
&Index
,
232 ArrayRef
<std::unique_ptr
<GlobalValueSummary
>> CalleeSummaryList
,
233 unsigned Threshold
, StringRef CallerModulePath
,
234 FunctionImporter::ImportFailureReason
&Reason
) {
235 auto QualifiedCandidates
=
236 qualifyCalleeCandidates(Index
, CalleeSummaryList
, CallerModulePath
);
237 for (auto QualifiedValue
: QualifiedCandidates
) {
238 Reason
= QualifiedValue
.first
;
239 if (Reason
!= FunctionImporter::ImportFailureReason::None
)
242 cast
<FunctionSummary
>(QualifiedValue
.second
->getBaseObject());
244 if ((Summary
->instCount() > Threshold
) && !Summary
->fflags().AlwaysInline
&&
246 Reason
= FunctionImporter::ImportFailureReason::TooLarge
;
250 // Don't bother importing if we can't inline it anyway.
251 if (Summary
->fflags().NoInline
&& !ForceImportAll
) {
252 Reason
= FunctionImporter::ImportFailureReason::NoInline
;
263 using EdgeInfo
= std::tuple
<const FunctionSummary
*, unsigned /* Threshold */>;
265 } // anonymous namespace
267 /// Import globals referenced by a function or other globals that are being
268 /// imported, if importing such global is possible.
269 class GlobalsImporter final
{
270 const ModuleSummaryIndex
&Index
;
271 const GVSummaryMapTy
&DefinedGVSummaries
;
272 function_ref
<bool(GlobalValue::GUID
, const GlobalValueSummary
*)>
274 FunctionImporter::ImportMapTy
&ImportList
;
275 DenseMap
<StringRef
, FunctionImporter::ExportSetTy
> *const ExportLists
;
277 bool shouldImportGlobal(const ValueInfo
&VI
) {
278 const auto &GVS
= DefinedGVSummaries
.find(VI
.getGUID());
279 if (GVS
== DefinedGVSummaries
.end())
281 // We should not skip import if the module contains a non-prevailing
282 // definition with interposable linkage type. This is required for
283 // correctness in the situation where there is a prevailing def available
284 // for import and marked read-only. In this case, the non-prevailing def
285 // will be converted to a declaration, while the prevailing one becomes
286 // internal, thus no definitions will be available for linking. In order to
287 // prevent undefined symbol link error, the prevailing definition must be
289 // FIXME: Consider adding a check that the suitable prevailing definition
290 // exists and marked read-only.
291 if (VI
.getSummaryList().size() > 1 &&
292 GlobalValue::isInterposableLinkage(GVS
->second
->linkage()) &&
293 !IsPrevailing(VI
.getGUID(), GVS
->second
))
300 onImportingSummaryImpl(const GlobalValueSummary
&Summary
,
301 SmallVectorImpl
<const GlobalVarSummary
*> &Worklist
) {
302 for (const auto &VI
: Summary
.refs()) {
303 if (!shouldImportGlobal(VI
)) {
305 dbgs() << "Ref ignored! Target already in destination module.\n");
309 LLVM_DEBUG(dbgs() << " ref -> " << VI
<< "\n");
311 // If this is a local variable, make sure we import the copy
312 // in the caller's module. The only time a local variable can
313 // share an entry in the index is if there is a local with the same name
314 // in another module that had the same source file name (in a different
315 // directory), where each was compiled in their own directory so there
316 // was not distinguishing path.
317 auto LocalNotInModule
=
318 [&](const GlobalValueSummary
*RefSummary
) -> bool {
319 return GlobalValue::isLocalLinkage(RefSummary
->linkage()) &&
320 RefSummary
->modulePath() != Summary
.modulePath();
323 for (const auto &RefSummary
: VI
.getSummaryList()) {
324 const auto *GVS
= dyn_cast
<GlobalVarSummary
>(RefSummary
.get());
325 // Functions could be referenced by global vars - e.g. a vtable; but we
326 // don't currently imagine a reason those would be imported here, rather
327 // than as part of the logic deciding which functions to import (i.e.
328 // based on profile information). Should we decide to handle them here,
329 // we can refactor accordingly at that time.
330 if (!GVS
|| !Index
.canImportGlobalVar(GVS
, /* AnalyzeRefs */ true) ||
331 LocalNotInModule(GVS
))
333 auto ILI
= ImportList
[RefSummary
->modulePath()].insert(VI
.getGUID());
334 // Only update stat and exports if we haven't already imported this
338 NumImportedGlobalVarsThinLink
++;
339 // Any references made by this variable will be marked exported
340 // later, in ComputeCrossModuleImport, after import decisions are
341 // complete, which is more efficient than adding them here.
343 (*ExportLists
)[RefSummary
->modulePath()].insert(VI
);
345 // If variable is not writeonly we attempt to recursively analyze
346 // its references in order to import referenced constants.
347 if (!Index
.isWriteOnly(GVS
))
348 Worklist
.emplace_back(GVS
);
356 const ModuleSummaryIndex
&Index
, const GVSummaryMapTy
&DefinedGVSummaries
,
357 function_ref
<bool(GlobalValue::GUID
, const GlobalValueSummary
*)>
359 FunctionImporter::ImportMapTy
&ImportList
,
360 DenseMap
<StringRef
, FunctionImporter::ExportSetTy
> *ExportLists
)
361 : Index(Index
), DefinedGVSummaries(DefinedGVSummaries
),
362 IsPrevailing(IsPrevailing
), ImportList(ImportList
),
363 ExportLists(ExportLists
) {}
365 void onImportingSummary(const GlobalValueSummary
&Summary
) {
366 SmallVector
<const GlobalVarSummary
*, 128> Worklist
;
367 onImportingSummaryImpl(Summary
, Worklist
);
368 while (!Worklist
.empty())
369 onImportingSummaryImpl(*Worklist
.pop_back_val(), Worklist
);
373 /// Determine the list of imports and exports for each module.
374 class ModuleImportsManager final
{
375 function_ref
<bool(GlobalValue::GUID
, const GlobalValueSummary
*)>
377 const ModuleSummaryIndex
&Index
;
378 DenseMap
<StringRef
, FunctionImporter::ExportSetTy
> *const ExportLists
;
381 ModuleImportsManager(
382 function_ref
<bool(GlobalValue::GUID
, const GlobalValueSummary
*)>
384 const ModuleSummaryIndex
&Index
,
385 DenseMap
<StringRef
, FunctionImporter::ExportSetTy
> *ExportLists
= nullptr)
386 : IsPrevailing(IsPrevailing
), Index(Index
), ExportLists(ExportLists
) {}
388 /// Given the list of globals defined in a module, compute the list of imports
389 /// as well as the list of "exports", i.e. the list of symbols referenced from
390 /// another module (that may require promotion).
391 void computeImportForModule(const GVSummaryMapTy
&DefinedGVSummaries
,
393 FunctionImporter::ImportMapTy
&ImportList
);
397 getFailureName(FunctionImporter::ImportFailureReason Reason
) {
399 case FunctionImporter::ImportFailureReason::None
:
401 case FunctionImporter::ImportFailureReason::GlobalVar
:
403 case FunctionImporter::ImportFailureReason::NotLive
:
405 case FunctionImporter::ImportFailureReason::TooLarge
:
407 case FunctionImporter::ImportFailureReason::InterposableLinkage
:
408 return "InterposableLinkage";
409 case FunctionImporter::ImportFailureReason::LocalLinkageNotInModule
:
410 return "LocalLinkageNotInModule";
411 case FunctionImporter::ImportFailureReason::NotEligible
:
412 return "NotEligible";
413 case FunctionImporter::ImportFailureReason::NoInline
:
416 llvm_unreachable("invalid reason");
419 /// Compute the list of functions to import for a given caller. Mark these
420 /// imported functions and the symbols they reference in their source module as
421 /// exported from their source module.
422 static void computeImportForFunction(
423 const FunctionSummary
&Summary
, const ModuleSummaryIndex
&Index
,
424 const unsigned Threshold
, const GVSummaryMapTy
&DefinedGVSummaries
,
425 function_ref
<bool(GlobalValue::GUID
, const GlobalValueSummary
*)>
427 SmallVectorImpl
<EdgeInfo
> &Worklist
, GlobalsImporter
&GVImporter
,
428 FunctionImporter::ImportMapTy
&ImportList
,
429 DenseMap
<StringRef
, FunctionImporter::ExportSetTy
> *ExportLists
,
430 FunctionImporter::ImportThresholdsTy
&ImportThresholds
) {
431 GVImporter
.onImportingSummary(Summary
);
432 static int ImportCount
= 0;
433 for (const auto &Edge
: Summary
.calls()) {
434 ValueInfo VI
= Edge
.first
;
435 LLVM_DEBUG(dbgs() << " edge -> " << VI
<< " Threshold:" << Threshold
438 if (ImportCutoff
>= 0 && ImportCount
>= ImportCutoff
) {
439 LLVM_DEBUG(dbgs() << "ignored! import-cutoff value of " << ImportCutoff
444 if (DefinedGVSummaries
.count(VI
.getGUID())) {
445 // FIXME: Consider not skipping import if the module contains
446 // a non-prevailing def with interposable linkage. The prevailing copy
447 // can safely be imported (see shouldImportGlobal()).
448 LLVM_DEBUG(dbgs() << "ignored! Target already in destination module.\n");
452 auto GetBonusMultiplier
= [](CalleeInfo::HotnessType Hotness
) -> float {
453 if (Hotness
== CalleeInfo::HotnessType::Hot
)
454 return ImportHotMultiplier
;
455 if (Hotness
== CalleeInfo::HotnessType::Cold
)
456 return ImportColdMultiplier
;
457 if (Hotness
== CalleeInfo::HotnessType::Critical
)
458 return ImportCriticalMultiplier
;
462 const auto NewThreshold
=
463 Threshold
* GetBonusMultiplier(Edge
.second
.getHotness());
465 auto IT
= ImportThresholds
.insert(std::make_pair(
466 VI
.getGUID(), std::make_tuple(NewThreshold
, nullptr, nullptr)));
467 bool PreviouslyVisited
= !IT
.second
;
468 auto &ProcessedThreshold
= std::get
<0>(IT
.first
->second
);
469 auto &CalleeSummary
= std::get
<1>(IT
.first
->second
);
470 auto &FailureInfo
= std::get
<2>(IT
.first
->second
);
473 Edge
.second
.getHotness() == CalleeInfo::HotnessType::Hot
;
474 bool IsCriticalCallsite
=
475 Edge
.second
.getHotness() == CalleeInfo::HotnessType::Critical
;
477 const FunctionSummary
*ResolvedCalleeSummary
= nullptr;
479 assert(PreviouslyVisited
);
480 // Since the traversal of the call graph is DFS, we can revisit a function
481 // a second time with a higher threshold. In this case, it is added back
482 // to the worklist with the new threshold (so that its own callee chains
483 // can be considered with the higher threshold).
484 if (NewThreshold
<= ProcessedThreshold
) {
486 dbgs() << "ignored! Target was already imported with Threshold "
487 << ProcessedThreshold
<< "\n");
490 // Update with new larger threshold.
491 ProcessedThreshold
= NewThreshold
;
492 ResolvedCalleeSummary
= cast
<FunctionSummary
>(CalleeSummary
);
494 // If we already rejected importing a callee at the same or higher
495 // threshold, don't waste time calling selectCallee.
496 if (PreviouslyVisited
&& NewThreshold
<= ProcessedThreshold
) {
498 dbgs() << "ignored! Target was already rejected with Threshold "
499 << ProcessedThreshold
<< "\n");
500 if (PrintImportFailures
) {
501 assert(FailureInfo
&&
502 "Expected FailureInfo for previously rejected candidate");
503 FailureInfo
->Attempts
++;
508 FunctionImporter::ImportFailureReason Reason
{};
509 CalleeSummary
= selectCallee(Index
, VI
.getSummaryList(), NewThreshold
,
510 Summary
.modulePath(), Reason
);
511 if (!CalleeSummary
) {
512 // Update with new larger threshold if this was a retry (otherwise
513 // we would have already inserted with NewThreshold above). Also
514 // update failure info if requested.
515 if (PreviouslyVisited
) {
516 ProcessedThreshold
= NewThreshold
;
517 if (PrintImportFailures
) {
518 assert(FailureInfo
&&
519 "Expected FailureInfo for previously rejected candidate");
520 FailureInfo
->Reason
= Reason
;
521 FailureInfo
->Attempts
++;
522 FailureInfo
->MaxHotness
=
523 std::max(FailureInfo
->MaxHotness
, Edge
.second
.getHotness());
525 } else if (PrintImportFailures
) {
526 assert(!FailureInfo
&&
527 "Expected no FailureInfo for newly rejected candidate");
528 FailureInfo
= std::make_unique
<FunctionImporter::ImportFailureInfo
>(
529 VI
, Edge
.second
.getHotness(), Reason
, 1);
531 if (ForceImportAll
) {
532 std::string Msg
= std::string("Failed to import function ") +
533 VI
.name().str() + " due to " +
534 getFailureName(Reason
);
535 auto Error
= make_error
<StringError
>(
536 Msg
, make_error_code(errc::not_supported
));
537 logAllUnhandledErrors(std::move(Error
), errs(),
538 "Error importing module: ");
542 << "ignored! No qualifying callee with summary found.\n");
547 // "Resolve" the summary
548 CalleeSummary
= CalleeSummary
->getBaseObject();
549 ResolvedCalleeSummary
= cast
<FunctionSummary
>(CalleeSummary
);
551 assert((ResolvedCalleeSummary
->fflags().AlwaysInline
|| ForceImportAll
||
552 (ResolvedCalleeSummary
->instCount() <= NewThreshold
)) &&
553 "selectCallee() didn't honor the threshold");
555 auto ExportModulePath
= ResolvedCalleeSummary
->modulePath();
556 auto ILI
= ImportList
[ExportModulePath
].insert(VI
.getGUID());
557 // We previously decided to import this GUID definition if it was already
558 // inserted in the set of imports from the exporting module.
559 bool PreviouslyImported
= !ILI
.second
;
560 if (!PreviouslyImported
) {
561 NumImportedFunctionsThinLink
++;
563 NumImportedHotFunctionsThinLink
++;
564 if (IsCriticalCallsite
)
565 NumImportedCriticalFunctionsThinLink
++;
568 // Any calls/references made by this function will be marked exported
569 // later, in ComputeCrossModuleImport, after import decisions are
570 // complete, which is more efficient than adding them here.
572 (*ExportLists
)[ExportModulePath
].insert(VI
);
575 auto GetAdjustedThreshold
= [](unsigned Threshold
, bool IsHotCallsite
) {
576 // Adjust the threshold for next level of imported functions.
577 // The threshold is different for hot callsites because we can then
578 // inline chains of hot calls.
580 return Threshold
* ImportHotInstrFactor
;
581 return Threshold
* ImportInstrFactor
;
584 const auto AdjThreshold
= GetAdjustedThreshold(Threshold
, IsHotCallsite
);
588 // Insert the newly imported function to the worklist.
589 Worklist
.emplace_back(ResolvedCalleeSummary
, AdjThreshold
);
593 void ModuleImportsManager::computeImportForModule(
594 const GVSummaryMapTy
&DefinedGVSummaries
, StringRef ModName
,
595 FunctionImporter::ImportMapTy
&ImportList
) {
596 // Worklist contains the list of function imported in this module, for which
597 // we will analyse the callees and may import further down the callgraph.
598 SmallVector
<EdgeInfo
, 128> Worklist
;
599 GlobalsImporter
GVI(Index
, DefinedGVSummaries
, IsPrevailing
, ImportList
,
601 FunctionImporter::ImportThresholdsTy ImportThresholds
;
603 // Populate the worklist with the import for the functions in the current
605 for (const auto &GVSummary
: DefinedGVSummaries
) {
607 // FIXME: Change the GVSummaryMapTy to hold ValueInfo instead of GUID
608 // so this map look up (and possibly others) can be avoided.
609 auto VI
= Index
.getValueInfo(GVSummary
.first
);
611 if (!Index
.isGlobalValueLive(GVSummary
.second
)) {
612 LLVM_DEBUG(dbgs() << "Ignores Dead GUID: " << VI
<< "\n");
616 dyn_cast
<FunctionSummary
>(GVSummary
.second
->getBaseObject());
618 // Skip import for global variables
620 LLVM_DEBUG(dbgs() << "Initialize import for " << VI
<< "\n");
621 computeImportForFunction(*FuncSummary
, Index
, ImportInstrLimit
,
622 DefinedGVSummaries
, IsPrevailing
, Worklist
, GVI
,
623 ImportList
, ExportLists
, ImportThresholds
);
626 // Process the newly imported functions and add callees to the worklist.
627 while (!Worklist
.empty()) {
628 auto GVInfo
= Worklist
.pop_back_val();
629 auto *Summary
= std::get
<0>(GVInfo
);
630 auto Threshold
= std::get
<1>(GVInfo
);
632 if (auto *FS
= dyn_cast
<FunctionSummary
>(Summary
))
633 computeImportForFunction(*FS
, Index
, Threshold
, DefinedGVSummaries
,
634 IsPrevailing
, Worklist
, GVI
, ImportList
,
635 ExportLists
, ImportThresholds
);
638 // Print stats about functions considered but rejected for importing
640 if (PrintImportFailures
) {
641 dbgs() << "Missed imports into module " << ModName
<< "\n";
642 for (auto &I
: ImportThresholds
) {
643 auto &ProcessedThreshold
= std::get
<0>(I
.second
);
644 auto &CalleeSummary
= std::get
<1>(I
.second
);
645 auto &FailureInfo
= std::get
<2>(I
.second
);
647 continue; // We are going to import.
649 FunctionSummary
*FS
= nullptr;
650 if (!FailureInfo
->VI
.getSummaryList().empty())
651 FS
= dyn_cast
<FunctionSummary
>(
652 FailureInfo
->VI
.getSummaryList()[0]->getBaseObject());
653 dbgs() << FailureInfo
->VI
654 << ": Reason = " << getFailureName(FailureInfo
->Reason
)
655 << ", Threshold = " << ProcessedThreshold
656 << ", Size = " << (FS
? (int)FS
->instCount() : -1)
657 << ", MaxHotness = " << getHotnessName(FailureInfo
->MaxHotness
)
658 << ", Attempts = " << FailureInfo
->Attempts
<< "\n";
664 static bool isGlobalVarSummary(const ModuleSummaryIndex
&Index
, ValueInfo VI
) {
665 auto SL
= VI
.getSummaryList();
668 : SL
[0]->getSummaryKind() == GlobalValueSummary::GlobalVarKind
;
671 static bool isGlobalVarSummary(const ModuleSummaryIndex
&Index
,
672 GlobalValue::GUID G
) {
673 if (const auto &VI
= Index
.getValueInfo(G
))
674 return isGlobalVarSummary(Index
, VI
);
679 static unsigned numGlobalVarSummaries(const ModuleSummaryIndex
&Index
,
683 if (isGlobalVarSummary(Index
, V
))
690 static bool checkVariableImport(
691 const ModuleSummaryIndex
&Index
,
692 DenseMap
<StringRef
, FunctionImporter::ImportMapTy
> &ImportLists
,
693 DenseMap
<StringRef
, FunctionImporter::ExportSetTy
> &ExportLists
) {
695 DenseSet
<GlobalValue::GUID
> FlattenedImports
;
697 for (auto &ImportPerModule
: ImportLists
)
698 for (auto &ExportPerModule
: ImportPerModule
.second
)
699 FlattenedImports
.insert(ExportPerModule
.second
.begin(),
700 ExportPerModule
.second
.end());
702 // Checks that all GUIDs of read/writeonly vars we see in export lists
703 // are also in the import lists. Otherwise we my face linker undefs,
704 // because readonly and writeonly vars are internalized in their
705 // source modules. The exception would be if it has a linkage type indicating
706 // that there may have been a copy existing in the importing module (e.g.
707 // linkonce_odr). In that case we cannot accurately do this checking.
708 auto IsReadOrWriteOnlyVarNeedingImporting
= [&](StringRef ModulePath
,
709 const ValueInfo
&VI
) {
710 auto *GVS
= dyn_cast_or_null
<GlobalVarSummary
>(
711 Index
.findSummaryInModule(VI
, ModulePath
));
712 return GVS
&& (Index
.isReadOnly(GVS
) || Index
.isWriteOnly(GVS
)) &&
713 !(GVS
->linkage() == GlobalValue::AvailableExternallyLinkage
||
714 GVS
->linkage() == GlobalValue::WeakODRLinkage
||
715 GVS
->linkage() == GlobalValue::LinkOnceODRLinkage
);
718 for (auto &ExportPerModule
: ExportLists
)
719 for (auto &VI
: ExportPerModule
.second
)
720 if (!FlattenedImports
.count(VI
.getGUID()) &&
721 IsReadOrWriteOnlyVarNeedingImporting(ExportPerModule
.first
, VI
))
728 /// Compute all the import and export for every module using the Index.
729 void llvm::ComputeCrossModuleImport(
730 const ModuleSummaryIndex
&Index
,
731 const DenseMap
<StringRef
, GVSummaryMapTy
> &ModuleToDefinedGVSummaries
,
732 function_ref
<bool(GlobalValue::GUID
, const GlobalValueSummary
*)>
734 DenseMap
<StringRef
, FunctionImporter::ImportMapTy
> &ImportLists
,
735 DenseMap
<StringRef
, FunctionImporter::ExportSetTy
> &ExportLists
) {
736 ModuleImportsManager
MIS(isPrevailing
, Index
, &ExportLists
);
737 // For each module that has function defined, compute the import/export lists.
738 for (const auto &DefinedGVSummaries
: ModuleToDefinedGVSummaries
) {
739 auto &ImportList
= ImportLists
[DefinedGVSummaries
.first
];
740 LLVM_DEBUG(dbgs() << "Computing import for Module '"
741 << DefinedGVSummaries
.first
<< "'\n");
742 MIS
.computeImportForModule(DefinedGVSummaries
.second
,
743 DefinedGVSummaries
.first
, ImportList
);
746 // When computing imports we only added the variables and functions being
747 // imported to the export list. We also need to mark any references and calls
748 // they make as exported as well. We do this here, as it is more efficient
749 // since we may import the same values multiple times into different modules
750 // during the import computation.
751 for (auto &ELI
: ExportLists
) {
752 FunctionImporter::ExportSetTy NewExports
;
753 const auto &DefinedGVSummaries
=
754 ModuleToDefinedGVSummaries
.lookup(ELI
.first
);
755 for (auto &EI
: ELI
.second
) {
756 // Find the copy defined in the exporting module so that we can mark the
757 // values it references in that specific definition as exported.
758 // Below we will add all references and called values, without regard to
759 // whether they are also defined in this module. We subsequently prune the
760 // list to only include those defined in the exporting module, see comment
762 auto DS
= DefinedGVSummaries
.find(EI
.getGUID());
763 // Anything marked exported during the import computation must have been
764 // defined in the exporting module.
765 assert(DS
!= DefinedGVSummaries
.end());
766 auto *S
= DS
->getSecond();
767 S
= S
->getBaseObject();
768 if (auto *GVS
= dyn_cast
<GlobalVarSummary
>(S
)) {
769 // Export referenced functions and variables. We don't export/promote
770 // objects referenced by writeonly variable initializer, because
771 // we convert such variables initializers to "zeroinitializer".
772 // See processGlobalForThinLTO.
773 if (!Index
.isWriteOnly(GVS
))
774 for (const auto &VI
: GVS
->refs())
775 NewExports
.insert(VI
);
777 auto *FS
= cast
<FunctionSummary
>(S
);
778 for (const auto &Edge
: FS
->calls())
779 NewExports
.insert(Edge
.first
);
780 for (const auto &Ref
: FS
->refs())
781 NewExports
.insert(Ref
);
784 // Prune list computed above to only include values defined in the exporting
785 // module. We do this after the above insertion since we may hit the same
786 // ref/call target multiple times in above loop, and it is more efficient to
787 // avoid a set lookup each time.
788 for (auto EI
= NewExports
.begin(); EI
!= NewExports
.end();) {
789 if (!DefinedGVSummaries
.count(EI
->getGUID()))
790 NewExports
.erase(EI
++);
794 ELI
.second
.insert(NewExports
.begin(), NewExports
.end());
797 assert(checkVariableImport(Index
, ImportLists
, ExportLists
));
799 LLVM_DEBUG(dbgs() << "Import/Export lists for " << ImportLists
.size()
801 for (auto &ModuleImports
: ImportLists
) {
802 auto ModName
= ModuleImports
.first
;
803 auto &Exports
= ExportLists
[ModName
];
804 unsigned NumGVS
= numGlobalVarSummaries(Index
, Exports
);
805 LLVM_DEBUG(dbgs() << "* Module " << ModName
<< " exports "
806 << Exports
.size() - NumGVS
<< " functions and " << NumGVS
807 << " vars. Imports from " << ModuleImports
.second
.size()
809 for (auto &Src
: ModuleImports
.second
) {
810 auto SrcModName
= Src
.first
;
811 unsigned NumGVSPerMod
= numGlobalVarSummaries(Index
, Src
.second
);
812 LLVM_DEBUG(dbgs() << " - " << Src
.second
.size() - NumGVSPerMod
813 << " functions imported from " << SrcModName
<< "\n");
814 LLVM_DEBUG(dbgs() << " - " << NumGVSPerMod
815 << " global vars imported from " << SrcModName
<< "\n");
822 static void dumpImportListForModule(const ModuleSummaryIndex
&Index
,
823 StringRef ModulePath
,
824 FunctionImporter::ImportMapTy
&ImportList
) {
825 LLVM_DEBUG(dbgs() << "* Module " << ModulePath
<< " imports from "
826 << ImportList
.size() << " modules.\n");
827 for (auto &Src
: ImportList
) {
828 auto SrcModName
= Src
.first
;
829 unsigned NumGVSPerMod
= numGlobalVarSummaries(Index
, Src
.second
);
830 LLVM_DEBUG(dbgs() << " - " << Src
.second
.size() - NumGVSPerMod
831 << " functions imported from " << SrcModName
<< "\n");
832 LLVM_DEBUG(dbgs() << " - " << NumGVSPerMod
<< " vars imported from "
833 << SrcModName
<< "\n");
838 /// Compute all the imports for the given module using the Index.
840 /// \p isPrevailing is a callback that will be called with a global value's GUID
841 /// and summary and should return whether the module corresponding to the
842 /// summary contains the linker-prevailing copy of that value.
844 /// \p ImportList will be populated with a map that can be passed to
845 /// FunctionImporter::importFunctions() above (see description there).
846 static void ComputeCrossModuleImportForModuleForTest(
847 StringRef ModulePath
,
848 function_ref
<bool(GlobalValue::GUID
, const GlobalValueSummary
*)>
850 const ModuleSummaryIndex
&Index
,
851 FunctionImporter::ImportMapTy
&ImportList
) {
852 // Collect the list of functions this module defines.
854 GVSummaryMapTy FunctionSummaryMap
;
855 Index
.collectDefinedFunctionsForModule(ModulePath
, FunctionSummaryMap
);
857 // Compute the import list for this module.
858 LLVM_DEBUG(dbgs() << "Computing import for Module '" << ModulePath
<< "'\n");
859 ModuleImportsManager
MIS(isPrevailing
, Index
);
860 MIS
.computeImportForModule(FunctionSummaryMap
, ModulePath
, ImportList
);
863 dumpImportListForModule(Index
, ModulePath
, ImportList
);
867 /// Mark all external summaries in \p Index for import into the given module.
868 /// Used for testing the case of distributed builds using a distributed index.
870 /// \p ImportList will be populated with a map that can be passed to
871 /// FunctionImporter::importFunctions() above (see description there).
872 static void ComputeCrossModuleImportForModuleFromIndexForTest(
873 StringRef ModulePath
, const ModuleSummaryIndex
&Index
,
874 FunctionImporter::ImportMapTy
&ImportList
) {
875 for (const auto &GlobalList
: Index
) {
876 // Ignore entries for undefined references.
877 if (GlobalList
.second
.SummaryList
.empty())
880 auto GUID
= GlobalList
.first
;
881 assert(GlobalList
.second
.SummaryList
.size() == 1 &&
882 "Expected individual combined index to have one summary per GUID");
883 auto &Summary
= GlobalList
.second
.SummaryList
[0];
884 // Skip the summaries for the importing module. These are included to
885 // e.g. record required linkage changes.
886 if (Summary
->modulePath() == ModulePath
)
888 // Add an entry to provoke importing by thinBackend.
889 ImportList
[Summary
->modulePath()].insert(GUID
);
892 dumpImportListForModule(Index
, ModulePath
, ImportList
);
896 // For SamplePGO, the indirect call targets for local functions will
897 // have its original name annotated in profile. We try to find the
898 // corresponding PGOFuncName as the GUID, and fix up the edges
900 void updateValueInfoForIndirectCalls(ModuleSummaryIndex
&Index
,
901 FunctionSummary
*FS
) {
902 for (auto &EI
: FS
->mutableCalls()) {
903 if (!EI
.first
.getSummaryList().empty())
905 auto GUID
= Index
.getGUIDFromOriginalID(EI
.first
.getGUID());
908 // Update the edge to point directly to the correct GUID.
909 auto VI
= Index
.getValueInfo(GUID
);
912 [&](const std::unique_ptr
<GlobalValueSummary
> &SummaryPtr
) {
913 // The mapping from OriginalId to GUID may return a GUID
914 // that corresponds to a static variable. Filter it out here.
915 // This can happen when
916 // 1) There is a call to a library function which is not defined
918 // 2) There is a static variable with the OriginalGUID identical
919 // to the GUID of the library function in 1);
920 // When this happens the static variable in 2) will be found,
921 // which needs to be filtered out.
922 return SummaryPtr
->getSummaryKind() ==
923 GlobalValueSummary::GlobalVarKind
;
930 void llvm::updateIndirectCalls(ModuleSummaryIndex
&Index
) {
931 for (const auto &Entry
: Index
) {
932 for (const auto &S
: Entry
.second
.SummaryList
) {
933 if (auto *FS
= dyn_cast
<FunctionSummary
>(S
.get()))
934 updateValueInfoForIndirectCalls(Index
, FS
);
939 void llvm::computeDeadSymbolsAndUpdateIndirectCalls(
940 ModuleSummaryIndex
&Index
,
941 const DenseSet
<GlobalValue::GUID
> &GUIDPreservedSymbols
,
942 function_ref
<PrevailingType(GlobalValue::GUID
)> isPrevailing
) {
943 assert(!Index
.withGlobalValueDeadStripping());
945 // Don't do anything when nothing is live, this is friendly with tests.
946 GUIDPreservedSymbols
.empty()) {
947 // Still need to update indirect calls.
948 updateIndirectCalls(Index
);
951 unsigned LiveSymbols
= 0;
952 SmallVector
<ValueInfo
, 128> Worklist
;
953 Worklist
.reserve(GUIDPreservedSymbols
.size() * 2);
954 for (auto GUID
: GUIDPreservedSymbols
) {
955 ValueInfo VI
= Index
.getValueInfo(GUID
);
958 for (const auto &S
: VI
.getSummaryList())
962 // Add values flagged in the index as live roots to the worklist.
963 for (const auto &Entry
: Index
) {
964 auto VI
= Index
.getValueInfo(Entry
);
965 for (const auto &S
: Entry
.second
.SummaryList
) {
966 if (auto *FS
= dyn_cast
<FunctionSummary
>(S
.get()))
967 updateValueInfoForIndirectCalls(Index
, FS
);
969 LLVM_DEBUG(dbgs() << "Live root: " << VI
<< "\n");
970 Worklist
.push_back(VI
);
977 // Make value live and add it to the worklist if it was not live before.
978 auto visit
= [&](ValueInfo VI
, bool IsAliasee
) {
979 // FIXME: If we knew which edges were created for indirect call profiles,
980 // we could skip them here. Any that are live should be reached via
981 // other edges, e.g. reference edges. Otherwise, using a profile collected
982 // on a slightly different binary might provoke preserving, importing
983 // and ultimately promoting calls to functions not linked into this
984 // binary, which increases the binary size unnecessarily. Note that
985 // if this code changes, the importer needs to change so that edges
986 // to functions marked dead are skipped.
988 if (llvm::any_of(VI
.getSummaryList(),
989 [](const std::unique_ptr
<llvm::GlobalValueSummary
> &S
) {
994 // We only keep live symbols that are known to be non-prevailing if any are
995 // available_externally, linkonceodr, weakodr. Those symbols are discarded
996 // later in the EliminateAvailableExternally pass and setting them to
997 // not-live could break downstreams users of liveness information (PR36483)
998 // or limit optimization opportunities.
999 if (isPrevailing(VI
.getGUID()) == PrevailingType::No
) {
1000 bool KeepAliveLinkage
= false;
1001 bool Interposable
= false;
1002 for (const auto &S
: VI
.getSummaryList()) {
1003 if (S
->linkage() == GlobalValue::AvailableExternallyLinkage
||
1004 S
->linkage() == GlobalValue::WeakODRLinkage
||
1005 S
->linkage() == GlobalValue::LinkOnceODRLinkage
)
1006 KeepAliveLinkage
= true;
1007 else if (GlobalValue::isInterposableLinkage(S
->linkage()))
1008 Interposable
= true;
1012 if (!KeepAliveLinkage
)
1017 "Interposable and available_externally/linkonce_odr/weak_odr "
1022 for (const auto &S
: VI
.getSummaryList())
1025 Worklist
.push_back(VI
);
1028 while (!Worklist
.empty()) {
1029 auto VI
= Worklist
.pop_back_val();
1030 for (const auto &Summary
: VI
.getSummaryList()) {
1031 if (auto *AS
= dyn_cast
<AliasSummary
>(Summary
.get())) {
1032 // If this is an alias, visit the aliasee VI to ensure that all copies
1033 // are marked live and it is added to the worklist for further
1034 // processing of its references.
1035 visit(AS
->getAliaseeVI(), true);
1038 for (auto Ref
: Summary
->refs())
1040 if (auto *FS
= dyn_cast
<FunctionSummary
>(Summary
.get()))
1041 for (auto Call
: FS
->calls())
1042 visit(Call
.first
, false);
1045 Index
.setWithGlobalValueDeadStripping();
1047 unsigned DeadSymbols
= Index
.size() - LiveSymbols
;
1048 LLVM_DEBUG(dbgs() << LiveSymbols
<< " symbols Live, and " << DeadSymbols
1049 << " symbols Dead \n");
1050 NumDeadSymbols
+= DeadSymbols
;
1051 NumLiveSymbols
+= LiveSymbols
;
1054 // Compute dead symbols and propagate constants in combined index.
1055 void llvm::computeDeadSymbolsWithConstProp(
1056 ModuleSummaryIndex
&Index
,
1057 const DenseSet
<GlobalValue::GUID
> &GUIDPreservedSymbols
,
1058 function_ref
<PrevailingType(GlobalValue::GUID
)> isPrevailing
,
1059 bool ImportEnabled
) {
1060 computeDeadSymbolsAndUpdateIndirectCalls(Index
, GUIDPreservedSymbols
,
1063 Index
.propagateAttributes(GUIDPreservedSymbols
);
1066 /// Compute the set of summaries needed for a ThinLTO backend compilation of
1068 void llvm::gatherImportedSummariesForModule(
1069 StringRef ModulePath
,
1070 const DenseMap
<StringRef
, GVSummaryMapTy
> &ModuleToDefinedGVSummaries
,
1071 const FunctionImporter::ImportMapTy
&ImportList
,
1072 std::map
<std::string
, GVSummaryMapTy
> &ModuleToSummariesForIndex
) {
1073 // Include all summaries from the importing module.
1074 ModuleToSummariesForIndex
[std::string(ModulePath
)] =
1075 ModuleToDefinedGVSummaries
.lookup(ModulePath
);
1076 // Include summaries for imports.
1077 for (const auto &ILI
: ImportList
) {
1078 auto &SummariesForIndex
= ModuleToSummariesForIndex
[std::string(ILI
.first
)];
1079 const auto &DefinedGVSummaries
=
1080 ModuleToDefinedGVSummaries
.lookup(ILI
.first
);
1081 for (const auto &GI
: ILI
.second
) {
1082 const auto &DS
= DefinedGVSummaries
.find(GI
);
1083 assert(DS
!= DefinedGVSummaries
.end() &&
1084 "Expected a defined summary for imported global value");
1085 SummariesForIndex
[GI
] = DS
->second
;
1090 /// Emit the files \p ModulePath will import from into \p OutputFilename.
1091 std::error_code
llvm::EmitImportsFiles(
1092 StringRef ModulePath
, StringRef OutputFilename
,
1093 const std::map
<std::string
, GVSummaryMapTy
> &ModuleToSummariesForIndex
) {
1095 raw_fd_ostream
ImportsOS(OutputFilename
, EC
, sys::fs::OpenFlags::OF_None
);
1098 for (const auto &ILI
: ModuleToSummariesForIndex
)
1099 // The ModuleToSummariesForIndex map includes an entry for the current
1100 // Module (needed for writing out the index files). We don't want to
1101 // include it in the imports file, however, so filter it out.
1102 if (ILI
.first
!= ModulePath
)
1103 ImportsOS
<< ILI
.first
<< "\n";
1104 return std::error_code();
1107 bool llvm::convertToDeclaration(GlobalValue
&GV
) {
1108 LLVM_DEBUG(dbgs() << "Converting to a declaration: `" << GV
.getName()
1110 if (Function
*F
= dyn_cast
<Function
>(&GV
)) {
1113 F
->setComdat(nullptr);
1114 } else if (GlobalVariable
*V
= dyn_cast
<GlobalVariable
>(&GV
)) {
1115 V
->setInitializer(nullptr);
1116 V
->setLinkage(GlobalValue::ExternalLinkage
);
1118 V
->setComdat(nullptr);
1121 if (GV
.getValueType()->isFunctionTy())
1123 Function::Create(cast
<FunctionType
>(GV
.getValueType()),
1124 GlobalValue::ExternalLinkage
, GV
.getAddressSpace(),
1125 "", GV
.getParent());
1128 new GlobalVariable(*GV
.getParent(), GV
.getValueType(),
1129 /*isConstant*/ false, GlobalValue::ExternalLinkage
,
1130 /*init*/ nullptr, "",
1131 /*insertbefore*/ nullptr, GV
.getThreadLocalMode(),
1132 GV
.getType()->getAddressSpace());
1133 NewGV
->takeName(&GV
);
1134 GV
.replaceAllUsesWith(NewGV
);
1137 if (!GV
.isImplicitDSOLocal())
1138 GV
.setDSOLocal(false);
1142 void llvm::thinLTOFinalizeInModule(Module
&TheModule
,
1143 const GVSummaryMapTy
&DefinedGlobals
,
1144 bool PropagateAttrs
) {
1145 DenseSet
<Comdat
*> NonPrevailingComdats
;
1146 auto FinalizeInModule
= [&](GlobalValue
&GV
, bool Propagate
= false) {
1147 // See if the global summary analysis computed a new resolved linkage.
1148 const auto &GS
= DefinedGlobals
.find(GV
.getGUID());
1149 if (GS
== DefinedGlobals
.end())
1153 if (FunctionSummary
*FS
= dyn_cast
<FunctionSummary
>(GS
->second
)) {
1154 if (Function
*F
= dyn_cast
<Function
>(&GV
)) {
1155 // TODO: propagate ReadNone and ReadOnly.
1156 if (FS
->fflags().ReadNone
&& !F
->doesNotAccessMemory())
1157 F
->setDoesNotAccessMemory();
1159 if (FS
->fflags().ReadOnly
&& !F
->onlyReadsMemory())
1160 F
->setOnlyReadsMemory();
1162 if (FS
->fflags().NoRecurse
&& !F
->doesNotRecurse())
1163 F
->setDoesNotRecurse();
1165 if (FS
->fflags().NoUnwind
&& !F
->doesNotThrow())
1166 F
->setDoesNotThrow();
1170 auto NewLinkage
= GS
->second
->linkage();
1171 if (GlobalValue::isLocalLinkage(GV
.getLinkage()) ||
1172 // Don't internalize anything here, because the code below
1173 // lacks necessary correctness checks. Leave this job to
1174 // LLVM 'internalize' pass.
1175 GlobalValue::isLocalLinkage(NewLinkage
) ||
1176 // In case it was dead and already converted to declaration.
1180 // Set the potentially more constraining visibility computed from summaries.
1181 // The DefaultVisibility condition is because older GlobalValueSummary does
1182 // not record DefaultVisibility and we don't want to change protected/hidden
1184 if (GS
->second
->getVisibility() != GlobalValue::DefaultVisibility
)
1185 GV
.setVisibility(GS
->second
->getVisibility());
1187 if (NewLinkage
== GV
.getLinkage())
1190 // Check for a non-prevailing def that has interposable linkage
1191 // (e.g. non-odr weak or linkonce). In that case we can't simply
1192 // convert to available_externally, since it would lose the
1193 // interposable property and possibly get inlined. Simply drop
1194 // the definition in that case.
1195 if (GlobalValue::isAvailableExternallyLinkage(NewLinkage
) &&
1196 GlobalValue::isInterposableLinkage(GV
.getLinkage())) {
1197 if (!convertToDeclaration(GV
))
1198 // FIXME: Change this to collect replaced GVs and later erase
1199 // them from the parent module once thinLTOResolvePrevailingGUID is
1200 // changed to enable this for aliases.
1201 llvm_unreachable("Expected GV to be converted");
1203 // If all copies of the original symbol had global unnamed addr and
1204 // linkonce_odr linkage, or if all of them had local unnamed addr linkage
1205 // and are constants, then it should be an auto hide symbol. In that case
1206 // the thin link would have marked it as CanAutoHide. Add hidden
1207 // visibility to the symbol to preserve the property.
1208 if (NewLinkage
== GlobalValue::WeakODRLinkage
&&
1209 GS
->second
->canAutoHide()) {
1210 assert(GV
.canBeOmittedFromSymbolTable());
1211 GV
.setVisibility(GlobalValue::HiddenVisibility
);
1214 LLVM_DEBUG(dbgs() << "ODR fixing up linkage for `" << GV
.getName()
1215 << "` from " << GV
.getLinkage() << " to " << NewLinkage
1217 GV
.setLinkage(NewLinkage
);
1219 // Remove declarations from comdats, including available_externally
1220 // as this is a declaration for the linker, and will be dropped eventually.
1221 // It is illegal for comdats to contain declarations.
1222 auto *GO
= dyn_cast_or_null
<GlobalObject
>(&GV
);
1223 if (GO
&& GO
->isDeclarationForLinker() && GO
->hasComdat()) {
1224 if (GO
->getComdat()->getName() == GO
->getName())
1225 NonPrevailingComdats
.insert(GO
->getComdat());
1226 GO
->setComdat(nullptr);
1230 // Process functions and global now
1231 for (auto &GV
: TheModule
)
1232 FinalizeInModule(GV
, PropagateAttrs
);
1233 for (auto &GV
: TheModule
.globals())
1234 FinalizeInModule(GV
);
1235 for (auto &GV
: TheModule
.aliases())
1236 FinalizeInModule(GV
);
1238 // For a non-prevailing comdat, all its members must be available_externally.
1239 // FinalizeInModule has handled non-local-linkage GlobalValues. Here we handle
1240 // local linkage GlobalValues.
1241 if (NonPrevailingComdats
.empty())
1243 for (auto &GO
: TheModule
.global_objects()) {
1244 if (auto *C
= GO
.getComdat(); C
&& NonPrevailingComdats
.count(C
)) {
1245 GO
.setComdat(nullptr);
1246 GO
.setLinkage(GlobalValue::AvailableExternallyLinkage
);
1252 // If an alias references a GlobalValue in a non-prevailing comdat, change
1253 // it to available_externally. For simplicity we only handle GlobalValue and
1254 // ConstantExpr with a base object. ConstantExpr without a base object is
1255 // unlikely used in a COMDAT.
1256 for (auto &GA
: TheModule
.aliases()) {
1257 if (GA
.hasAvailableExternallyLinkage())
1259 GlobalObject
*Obj
= GA
.getAliaseeObject();
1260 assert(Obj
&& "aliasee without an base object is unimplemented");
1261 if (Obj
->hasAvailableExternallyLinkage()) {
1262 GA
.setLinkage(GlobalValue::AvailableExternallyLinkage
);
1269 /// Run internalization on \p TheModule based on symmary analysis.
1270 void llvm::thinLTOInternalizeModule(Module
&TheModule
,
1271 const GVSummaryMapTy
&DefinedGlobals
) {
1272 // Declare a callback for the internalize pass that will ask for every
1273 // candidate GlobalValue if it can be internalized or not.
1274 auto MustPreserveGV
= [&](const GlobalValue
&GV
) -> bool {
1275 // It may be the case that GV is on a chain of an ifunc, its alias and
1276 // subsequent aliases. In this case, the summary for the value is not
1278 if (isa
<GlobalIFunc
>(&GV
) ||
1279 (isa
<GlobalAlias
>(&GV
) &&
1280 isa
<GlobalIFunc
>(cast
<GlobalAlias
>(&GV
)->getAliaseeObject())))
1283 // Lookup the linkage recorded in the summaries during global analysis.
1284 auto GS
= DefinedGlobals
.find(GV
.getGUID());
1285 if (GS
== DefinedGlobals
.end()) {
1286 // Must have been promoted (possibly conservatively). Find original
1287 // name so that we can access the correct summary and see if it can
1288 // be internalized again.
1289 // FIXME: Eventually we should control promotion instead of promoting
1290 // and internalizing again.
1291 StringRef OrigName
=
1292 ModuleSummaryIndex::getOriginalNameBeforePromote(GV
.getName());
1293 std::string OrigId
= GlobalValue::getGlobalIdentifier(
1294 OrigName
, GlobalValue::InternalLinkage
,
1295 TheModule
.getSourceFileName());
1296 GS
= DefinedGlobals
.find(GlobalValue::getGUID(OrigId
));
1297 if (GS
== DefinedGlobals
.end()) {
1298 // Also check the original non-promoted non-globalized name. In some
1299 // cases a preempted weak value is linked in as a local copy because
1300 // it is referenced by an alias (IRLinker::linkGlobalValueProto).
1301 // In that case, since it was originally not a local value, it was
1302 // recorded in the index using the original name.
1303 // FIXME: This may not be needed once PR27866 is fixed.
1304 GS
= DefinedGlobals
.find(GlobalValue::getGUID(OrigName
));
1305 assert(GS
!= DefinedGlobals
.end());
1308 return !GlobalValue::isLocalLinkage(GS
->second
->linkage());
1311 // FIXME: See if we can just internalize directly here via linkage changes
1312 // based on the index, rather than invoking internalizeModule.
1313 internalizeModule(TheModule
, MustPreserveGV
);
1316 /// Make alias a clone of its aliasee.
1317 static Function
*replaceAliasWithAliasee(Module
*SrcModule
, GlobalAlias
*GA
) {
1318 Function
*Fn
= cast
<Function
>(GA
->getAliaseeObject());
1320 ValueToValueMapTy VMap
;
1321 Function
*NewFn
= CloneFunction(Fn
, VMap
);
1322 // Clone should use the original alias's linkage, visibility and name, and we
1323 // ensure all uses of alias instead use the new clone (casted if necessary).
1324 NewFn
->setLinkage(GA
->getLinkage());
1325 NewFn
->setVisibility(GA
->getVisibility());
1326 GA
->replaceAllUsesWith(ConstantExpr::getBitCast(NewFn
, GA
->getType()));
1327 NewFn
->takeName(GA
);
1331 // Internalize values that we marked with specific attribute
1332 // in processGlobalForThinLTO.
1333 static void internalizeGVsAfterImport(Module
&M
) {
1334 for (auto &GV
: M
.globals())
1335 // Skip GVs which have been converted to declarations
1336 // by dropDeadSymbols.
1337 if (!GV
.isDeclaration() && GV
.hasAttribute("thinlto-internalize")) {
1338 GV
.setLinkage(GlobalValue::InternalLinkage
);
1339 GV
.setVisibility(GlobalValue::DefaultVisibility
);
1343 // Automatically import functions in Module \p DestModule based on the summaries
1345 Expected
<bool> FunctionImporter::importFunctions(
1346 Module
&DestModule
, const FunctionImporter::ImportMapTy
&ImportList
) {
1347 LLVM_DEBUG(dbgs() << "Starting import for Module "
1348 << DestModule
.getModuleIdentifier() << "\n");
1349 unsigned ImportedCount
= 0, ImportedGVCount
= 0;
1351 IRMover
Mover(DestModule
);
1352 // Do the actual import of functions now, one Module at a time
1353 std::set
<StringRef
> ModuleNameOrderedList
;
1354 for (const auto &FunctionsToImportPerModule
: ImportList
) {
1355 ModuleNameOrderedList
.insert(FunctionsToImportPerModule
.first
);
1357 for (const auto &Name
: ModuleNameOrderedList
) {
1358 // Get the module for the import
1359 const auto &FunctionsToImportPerModule
= ImportList
.find(Name
);
1360 assert(FunctionsToImportPerModule
!= ImportList
.end());
1361 Expected
<std::unique_ptr
<Module
>> SrcModuleOrErr
= ModuleLoader(Name
);
1362 if (!SrcModuleOrErr
)
1363 return SrcModuleOrErr
.takeError();
1364 std::unique_ptr
<Module
> SrcModule
= std::move(*SrcModuleOrErr
);
1365 assert(&DestModule
.getContext() == &SrcModule
->getContext() &&
1366 "Context mismatch");
1368 // If modules were created with lazy metadata loading, materialize it
1369 // now, before linking it (otherwise this will be a noop).
1370 if (Error Err
= SrcModule
->materializeMetadata())
1371 return std::move(Err
);
1373 auto &ImportGUIDs
= FunctionsToImportPerModule
->second
;
1374 // Find the globals to import
1375 SetVector
<GlobalValue
*> GlobalsToImport
;
1376 for (Function
&F
: *SrcModule
) {
1379 auto GUID
= F
.getGUID();
1380 auto Import
= ImportGUIDs
.count(GUID
);
1381 LLVM_DEBUG(dbgs() << (Import
? "Is" : "Not") << " importing function "
1382 << GUID
<< " " << F
.getName() << " from "
1383 << SrcModule
->getSourceFileName() << "\n");
1385 if (Error Err
= F
.materialize())
1386 return std::move(Err
);
1387 if (EnableImportMetadata
) {
1388 // Add 'thinlto_src_module' metadata for statistics and debugging.
1390 "thinlto_src_module",
1391 MDNode::get(DestModule
.getContext(),
1392 {MDString::get(DestModule
.getContext(),
1393 SrcModule
->getSourceFileName())}));
1395 GlobalsToImport
.insert(&F
);
1398 for (GlobalVariable
&GV
: SrcModule
->globals()) {
1401 auto GUID
= GV
.getGUID();
1402 auto Import
= ImportGUIDs
.count(GUID
);
1403 LLVM_DEBUG(dbgs() << (Import
? "Is" : "Not") << " importing global "
1404 << GUID
<< " " << GV
.getName() << " from "
1405 << SrcModule
->getSourceFileName() << "\n");
1407 if (Error Err
= GV
.materialize())
1408 return std::move(Err
);
1409 ImportedGVCount
+= GlobalsToImport
.insert(&GV
);
1412 for (GlobalAlias
&GA
: SrcModule
->aliases()) {
1413 if (!GA
.hasName() || isa
<GlobalIFunc
>(GA
.getAliaseeObject()))
1415 auto GUID
= GA
.getGUID();
1416 auto Import
= ImportGUIDs
.count(GUID
);
1417 LLVM_DEBUG(dbgs() << (Import
? "Is" : "Not") << " importing alias "
1418 << GUID
<< " " << GA
.getName() << " from "
1419 << SrcModule
->getSourceFileName() << "\n");
1421 if (Error Err
= GA
.materialize())
1422 return std::move(Err
);
1423 // Import alias as a copy of its aliasee.
1424 GlobalObject
*GO
= GA
.getAliaseeObject();
1425 if (Error Err
= GO
->materialize())
1426 return std::move(Err
);
1427 auto *Fn
= replaceAliasWithAliasee(SrcModule
.get(), &GA
);
1428 LLVM_DEBUG(dbgs() << "Is importing aliasee fn " << GO
->getGUID() << " "
1429 << GO
->getName() << " from "
1430 << SrcModule
->getSourceFileName() << "\n");
1431 if (EnableImportMetadata
) {
1432 // Add 'thinlto_src_module' metadata for statistics and debugging.
1434 "thinlto_src_module",
1435 MDNode::get(DestModule
.getContext(),
1436 {MDString::get(DestModule
.getContext(),
1437 SrcModule
->getSourceFileName())}));
1439 GlobalsToImport
.insert(Fn
);
1443 // Upgrade debug info after we're done materializing all the globals and we
1444 // have loaded all the required metadata!
1445 UpgradeDebugInfo(*SrcModule
);
1447 // Set the partial sample profile ratio in the profile summary module flag
1448 // of the imported source module, if applicable, so that the profile summary
1449 // module flag will match with that of the destination module when it's
1451 SrcModule
->setPartialSampleProfileRatio(Index
);
1453 // Link in the specified functions.
1454 if (renameModuleForThinLTO(*SrcModule
, Index
, ClearDSOLocalOnDeclarations
,
1459 for (const auto *GV
: GlobalsToImport
)
1460 dbgs() << DestModule
.getSourceFileName() << ": Import " << GV
->getName()
1461 << " from " << SrcModule
->getSourceFileName() << "\n";
1464 if (Error Err
= Mover
.move(std::move(SrcModule
),
1465 GlobalsToImport
.getArrayRef(), nullptr,
1466 /*IsPerformingImport=*/true))
1467 return createStringError(errc::invalid_argument
,
1468 Twine("Function Import: link error: ") +
1469 toString(std::move(Err
)));
1471 ImportedCount
+= GlobalsToImport
.size();
1472 NumImportedModules
++;
1475 internalizeGVsAfterImport(DestModule
);
1477 NumImportedFunctions
+= (ImportedCount
- ImportedGVCount
);
1478 NumImportedGlobalVars
+= ImportedGVCount
;
1480 LLVM_DEBUG(dbgs() << "Imported " << ImportedCount
- ImportedGVCount
1481 << " functions for Module "
1482 << DestModule
.getModuleIdentifier() << "\n");
1483 LLVM_DEBUG(dbgs() << "Imported " << ImportedGVCount
1484 << " global variables for Module "
1485 << DestModule
.getModuleIdentifier() << "\n");
1486 return ImportedCount
;
1489 static bool doImportingForModuleForTest(
1490 Module
&M
, function_ref
<bool(GlobalValue::GUID
, const GlobalValueSummary
*)>
1492 if (SummaryFile
.empty())
1493 report_fatal_error("error: -function-import requires -summary-file\n");
1494 Expected
<std::unique_ptr
<ModuleSummaryIndex
>> IndexPtrOrErr
=
1495 getModuleSummaryIndexForFile(SummaryFile
);
1496 if (!IndexPtrOrErr
) {
1497 logAllUnhandledErrors(IndexPtrOrErr
.takeError(), errs(),
1498 "Error loading file '" + SummaryFile
+ "': ");
1501 std::unique_ptr
<ModuleSummaryIndex
> Index
= std::move(*IndexPtrOrErr
);
1503 // First step is collecting the import list.
1504 FunctionImporter::ImportMapTy ImportList
;
1505 // If requested, simply import all functions in the index. This is used
1506 // when testing distributed backend handling via the opt tool, when
1507 // we have distributed indexes containing exactly the summaries to import.
1509 ComputeCrossModuleImportForModuleFromIndexForTest(M
.getModuleIdentifier(),
1510 *Index
, ImportList
);
1512 ComputeCrossModuleImportForModuleForTest(M
.getModuleIdentifier(),
1513 isPrevailing
, *Index
, ImportList
);
1515 // Conservatively mark all internal values as promoted. This interface is
1516 // only used when doing importing via the function importing pass. The pass
1517 // is only enabled when testing importing via the 'opt' tool, which does
1518 // not do the ThinLink that would normally determine what values to promote.
1519 for (auto &I
: *Index
) {
1520 for (auto &S
: I
.second
.SummaryList
) {
1521 if (GlobalValue::isLocalLinkage(S
->linkage()))
1522 S
->setLinkage(GlobalValue::ExternalLinkage
);
1526 // Next we need to promote to global scope and rename any local values that
1527 // are potentially exported to other modules.
1528 if (renameModuleForThinLTO(M
, *Index
, /*ClearDSOLocalOnDeclarations=*/false,
1529 /*GlobalsToImport=*/nullptr)) {
1530 errs() << "Error renaming module\n";
1534 // Perform the import now.
1535 auto ModuleLoader
= [&M
](StringRef Identifier
) {
1536 return loadFile(std::string(Identifier
), M
.getContext());
1538 FunctionImporter
Importer(*Index
, ModuleLoader
,
1539 /*ClearDSOLocalOnDeclarations=*/false);
1540 Expected
<bool> Result
= Importer
.importFunctions(M
, ImportList
);
1542 // FIXME: Probably need to propagate Errors through the pass manager.
1544 logAllUnhandledErrors(Result
.takeError(), errs(),
1545 "Error importing module: ");
1552 PreservedAnalyses
FunctionImportPass::run(Module
&M
,
1553 ModuleAnalysisManager
&AM
) {
1554 // This is only used for testing the function import pass via opt, where we
1555 // don't have prevailing information from the LTO context available, so just
1556 // conservatively assume everything is prevailing (which is fine for the very
1557 // limited use of prevailing checking in this pass).
1558 auto isPrevailing
= [](GlobalValue::GUID
, const GlobalValueSummary
*) {
1561 if (!doImportingForModuleForTest(M
, isPrevailing
))
1562 return PreservedAnalyses::all();
1564 return PreservedAnalyses::none();