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/InitializePasses.h"
34 #include "llvm/Linker/IRMover.h"
35 #include "llvm/Pass.h"
36 #include "llvm/Support/Casting.h"
37 #include "llvm/Support/CommandLine.h"
38 #include "llvm/Support/Debug.h"
39 #include "llvm/Support/Errc.h"
40 #include "llvm/Support/Error.h"
41 #include "llvm/Support/ErrorHandling.h"
42 #include "llvm/Support/FileSystem.h"
43 #include "llvm/Support/SourceMgr.h"
44 #include "llvm/Support/raw_ostream.h"
45 #include "llvm/Transforms/IPO/Internalize.h"
46 #include "llvm/Transforms/Utils/Cloning.h"
47 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
48 #include "llvm/Transforms/Utils/ValueMapper.h"
53 #include <system_error>
59 #define DEBUG_TYPE "function-import"
61 STATISTIC(NumImportedFunctionsThinLink
,
62 "Number of functions thin link decided to import");
63 STATISTIC(NumImportedHotFunctionsThinLink
,
64 "Number of hot functions thin link decided to import");
65 STATISTIC(NumImportedCriticalFunctionsThinLink
,
66 "Number of critical functions thin link decided to import");
67 STATISTIC(NumImportedGlobalVarsThinLink
,
68 "Number of global variables thin link decided to import");
69 STATISTIC(NumImportedFunctions
, "Number of functions imported in backend");
70 STATISTIC(NumImportedGlobalVars
,
71 "Number of global variables imported in backend");
72 STATISTIC(NumImportedModules
, "Number of modules imported from");
73 STATISTIC(NumDeadSymbols
, "Number of dead stripped symbols in index");
74 STATISTIC(NumLiveSymbols
, "Number of live symbols in index");
76 /// Limit on instruction count of imported functions.
77 static cl::opt
<unsigned> ImportInstrLimit(
78 "import-instr-limit", cl::init(100), cl::Hidden
, cl::value_desc("N"),
79 cl::desc("Only import functions with less than N instructions"));
81 static cl::opt
<int> ImportCutoff(
82 "import-cutoff", cl::init(-1), cl::Hidden
, cl::value_desc("N"),
83 cl::desc("Only import first N functions if N>=0 (default -1)"));
86 ForceImportAll("force-import-all", cl::init(false), cl::Hidden
,
87 cl::desc("Import functions with noinline attribute"));
90 ImportInstrFactor("import-instr-evolution-factor", cl::init(0.7),
91 cl::Hidden
, cl::value_desc("x"),
92 cl::desc("As we import functions, multiply the "
93 "`import-instr-limit` threshold by this factor "
94 "before processing newly imported functions"));
96 static cl::opt
<float> ImportHotInstrFactor(
97 "import-hot-evolution-factor", cl::init(1.0), cl::Hidden
,
99 cl::desc("As we import functions called from hot callsite, multiply the "
100 "`import-instr-limit` threshold by this factor "
101 "before processing newly imported functions"));
103 static cl::opt
<float> ImportHotMultiplier(
104 "import-hot-multiplier", cl::init(10.0), cl::Hidden
, cl::value_desc("x"),
105 cl::desc("Multiply the `import-instr-limit` threshold for hot callsites"));
107 static cl::opt
<float> ImportCriticalMultiplier(
108 "import-critical-multiplier", cl::init(100.0), cl::Hidden
,
111 "Multiply the `import-instr-limit` threshold for critical callsites"));
113 // FIXME: This multiplier was not really tuned up.
114 static cl::opt
<float> ImportColdMultiplier(
115 "import-cold-multiplier", cl::init(0), cl::Hidden
, cl::value_desc("N"),
116 cl::desc("Multiply the `import-instr-limit` threshold for cold callsites"));
118 static cl::opt
<bool> PrintImports("print-imports", cl::init(false), cl::Hidden
,
119 cl::desc("Print imported functions"));
121 static cl::opt
<bool> PrintImportFailures(
122 "print-import-failures", cl::init(false), cl::Hidden
,
123 cl::desc("Print information for functions rejected for importing"));
125 static cl::opt
<bool> ComputeDead("compute-dead", cl::init(true), cl::Hidden
,
126 cl::desc("Compute dead symbols"));
128 static cl::opt
<bool> EnableImportMetadata(
129 "enable-import-metadata", cl::init(false), cl::Hidden
,
130 cl::desc("Enable import metadata like 'thinlto_src_module'"));
132 /// Summary file to use for function importing when using -function-import from
133 /// the command line.
134 static cl::opt
<std::string
>
135 SummaryFile("summary-file",
136 cl::desc("The summary file to use for function importing."));
138 /// Used when testing importing from distributed indexes via opt
141 ImportAllIndex("import-all-index",
142 cl::desc("Import all external functions in index."));
144 // Load lazily a module from \p FileName in \p Context.
145 static std::unique_ptr
<Module
> loadFile(const std::string
&FileName
,
146 LLVMContext
&Context
) {
148 LLVM_DEBUG(dbgs() << "Loading '" << FileName
<< "'\n");
149 // Metadata isn't loaded until functions are imported, to minimize
150 // the memory overhead.
151 std::unique_ptr
<Module
> Result
=
152 getLazyIRFileModule(FileName
, Err
, Context
,
153 /* ShouldLazyLoadMetadata = */ true);
155 Err
.print("function-import", errs());
156 report_fatal_error("Abort");
162 /// Given a list of possible callee implementation for a call site, select one
163 /// that fits the \p Threshold.
165 /// FIXME: select "best" instead of first that fits. But what is "best"?
166 /// - The smallest: more likely to be inlined.
167 /// - The one with the least outgoing edges (already well optimized).
168 /// - One from a module already being imported from in order to reduce the
169 /// number of source modules parsed/linked.
170 /// - One that has PGO data attached.
171 /// - [insert you fancy metric here]
172 static const GlobalValueSummary
*
173 selectCallee(const ModuleSummaryIndex
&Index
,
174 ArrayRef
<std::unique_ptr
<GlobalValueSummary
>> CalleeSummaryList
,
175 unsigned Threshold
, StringRef CallerModulePath
,
176 FunctionImporter::ImportFailureReason
&Reason
,
177 GlobalValue::GUID GUID
) {
178 Reason
= FunctionImporter::ImportFailureReason::None
;
179 auto It
= llvm::find_if(
181 [&](const std::unique_ptr
<GlobalValueSummary
> &SummaryPtr
) {
182 auto *GVSummary
= SummaryPtr
.get();
183 if (!Index
.isGlobalValueLive(GVSummary
)) {
184 Reason
= FunctionImporter::ImportFailureReason::NotLive
;
188 if (GlobalValue::isInterposableLinkage(GVSummary
->linkage())) {
189 Reason
= FunctionImporter::ImportFailureReason::InterposableLinkage
;
190 // There is no point in importing these, we can't inline them
194 auto *Summary
= cast
<FunctionSummary
>(GVSummary
->getBaseObject());
196 // If this is a local function, make sure we import the copy
197 // in the caller's module. The only time a local function can
198 // share an entry in the index is if there is a local with the same name
199 // in another module that had the same source file name (in a different
200 // directory), where each was compiled in their own directory so there
201 // was not distinguishing path.
202 // However, do the import from another module if there is only one
203 // entry in the list - in that case this must be a reference due
204 // to indirect call profile data, since a function pointer can point to
205 // a local in another module.
206 if (GlobalValue::isLocalLinkage(Summary
->linkage()) &&
207 CalleeSummaryList
.size() > 1 &&
208 Summary
->modulePath() != CallerModulePath
) {
210 FunctionImporter::ImportFailureReason::LocalLinkageNotInModule
;
214 if ((Summary
->instCount() > Threshold
) &&
215 !Summary
->fflags().AlwaysInline
&& !ForceImportAll
) {
216 Reason
= FunctionImporter::ImportFailureReason::TooLarge
;
220 // Skip if it isn't legal to import (e.g. may reference unpromotable
222 if (Summary
->notEligibleToImport()) {
223 Reason
= FunctionImporter::ImportFailureReason::NotEligible
;
227 // Don't bother importing if we can't inline it anyway.
228 if (Summary
->fflags().NoInline
&& !ForceImportAll
) {
229 Reason
= FunctionImporter::ImportFailureReason::NoInline
;
235 if (It
== CalleeSummaryList
.end())
238 return cast
<GlobalValueSummary
>(It
->get());
244 std::tuple
<const GlobalValueSummary
*, unsigned /* Threshold */>;
246 } // anonymous namespace
248 static bool shouldImportGlobal(const ValueInfo
&VI
,
249 const GVSummaryMapTy
&DefinedGVSummaries
) {
250 const auto &GVS
= DefinedGVSummaries
.find(VI
.getGUID());
251 if (GVS
== DefinedGVSummaries
.end())
253 // We should not skip import if the module contains a definition with
254 // interposable linkage type. This is required for correctness in
255 // the situation with two following conditions:
256 // * the def with interposable linkage is non-prevailing,
257 // * there is a prevailing def available for import and marked read-only.
258 // In this case, the non-prevailing def will be converted to a declaration,
259 // while the prevailing one becomes internal, thus no definitions will be
260 // available for linking. In order to prevent undefined symbol link error,
261 // the prevailing definition must be imported.
262 // FIXME: Consider adding a check that the suitable prevailing definition
263 // exists and marked read-only.
264 if (VI
.getSummaryList().size() > 1 &&
265 GlobalValue::isInterposableLinkage(GVS
->second
->linkage()))
271 static void computeImportForReferencedGlobals(
272 const GlobalValueSummary
&Summary
, const ModuleSummaryIndex
&Index
,
273 const GVSummaryMapTy
&DefinedGVSummaries
,
274 SmallVectorImpl
<EdgeInfo
> &Worklist
,
275 FunctionImporter::ImportMapTy
&ImportList
,
276 StringMap
<FunctionImporter::ExportSetTy
> *ExportLists
) {
277 for (const auto &VI
: Summary
.refs()) {
278 if (!shouldImportGlobal(VI
, DefinedGVSummaries
)) {
280 dbgs() << "Ref ignored! Target already in destination module.\n");
284 LLVM_DEBUG(dbgs() << " ref -> " << VI
<< "\n");
286 // If this is a local variable, make sure we import the copy
287 // in the caller's module. The only time a local variable can
288 // share an entry in the index is if there is a local with the same name
289 // in another module that had the same source file name (in a different
290 // directory), where each was compiled in their own directory so there
291 // was not distinguishing path.
292 auto LocalNotInModule
= [&](const GlobalValueSummary
*RefSummary
) -> bool {
293 return GlobalValue::isLocalLinkage(RefSummary
->linkage()) &&
294 RefSummary
->modulePath() != Summary
.modulePath();
297 for (const auto &RefSummary
: VI
.getSummaryList())
298 if (isa
<GlobalVarSummary
>(RefSummary
.get()) &&
299 Index
.canImportGlobalVar(RefSummary
.get(), /* AnalyzeRefs */ true) &&
300 !LocalNotInModule(RefSummary
.get())) {
301 auto ILI
= ImportList
[RefSummary
->modulePath()].insert(VI
.getGUID());
302 // Only update stat and exports if we haven't already imported this
306 NumImportedGlobalVarsThinLink
++;
307 // Any references made by this variable will be marked exported later,
308 // in ComputeCrossModuleImport, after import decisions are complete,
309 // which is more efficient than adding them here.
311 (*ExportLists
)[RefSummary
->modulePath()].insert(VI
);
313 // If variable is not writeonly we attempt to recursively analyze
314 // its references in order to import referenced constants.
315 if (!Index
.isWriteOnly(cast
<GlobalVarSummary
>(RefSummary
.get())))
316 Worklist
.emplace_back(RefSummary
.get(), 0);
323 getFailureName(FunctionImporter::ImportFailureReason Reason
) {
325 case FunctionImporter::ImportFailureReason::None
:
327 case FunctionImporter::ImportFailureReason::GlobalVar
:
329 case FunctionImporter::ImportFailureReason::NotLive
:
331 case FunctionImporter::ImportFailureReason::TooLarge
:
333 case FunctionImporter::ImportFailureReason::InterposableLinkage
:
334 return "InterposableLinkage";
335 case FunctionImporter::ImportFailureReason::LocalLinkageNotInModule
:
336 return "LocalLinkageNotInModule";
337 case FunctionImporter::ImportFailureReason::NotEligible
:
338 return "NotEligible";
339 case FunctionImporter::ImportFailureReason::NoInline
:
342 llvm_unreachable("invalid reason");
345 /// Compute the list of functions to import for a given caller. Mark these
346 /// imported functions and the symbols they reference in their source module as
347 /// exported from their source module.
348 static void computeImportForFunction(
349 const FunctionSummary
&Summary
, const ModuleSummaryIndex
&Index
,
350 const unsigned Threshold
, const GVSummaryMapTy
&DefinedGVSummaries
,
351 SmallVectorImpl
<EdgeInfo
> &Worklist
,
352 FunctionImporter::ImportMapTy
&ImportList
,
353 StringMap
<FunctionImporter::ExportSetTy
> *ExportLists
,
354 FunctionImporter::ImportThresholdsTy
&ImportThresholds
) {
355 computeImportForReferencedGlobals(Summary
, Index
, DefinedGVSummaries
,
356 Worklist
, ImportList
, ExportLists
);
357 static int ImportCount
= 0;
358 for (const auto &Edge
: Summary
.calls()) {
359 ValueInfo VI
= Edge
.first
;
360 LLVM_DEBUG(dbgs() << " edge -> " << VI
<< " Threshold:" << Threshold
363 if (ImportCutoff
>= 0 && ImportCount
>= ImportCutoff
) {
364 LLVM_DEBUG(dbgs() << "ignored! import-cutoff value of " << ImportCutoff
369 if (DefinedGVSummaries
.count(VI
.getGUID())) {
370 // FIXME: Consider not skipping import if the module contains
371 // a non-prevailing def with interposable linkage. The prevailing copy
372 // can safely be imported (see shouldImportGlobal()).
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
= std::make_unique
<FunctionImporter::ImportFailureInfo
>(
454 VI
, Edge
.second
.getHotness(), Reason
, 1);
456 if (ForceImportAll
) {
457 std::string Msg
= std::string("Failed to import function ") +
458 VI
.name().str() + " due to " +
459 getFailureName(Reason
);
460 auto Error
= make_error
<StringError
>(
461 Msg
, make_error_code(errc::not_supported
));
462 logAllUnhandledErrors(std::move(Error
), errs(),
463 "Error importing module: ");
467 << "ignored! No qualifying callee with summary found.\n");
472 // "Resolve" the summary
473 CalleeSummary
= CalleeSummary
->getBaseObject();
474 ResolvedCalleeSummary
= cast
<FunctionSummary
>(CalleeSummary
);
476 assert((ResolvedCalleeSummary
->fflags().AlwaysInline
|| ForceImportAll
||
477 (ResolvedCalleeSummary
->instCount() <= NewThreshold
)) &&
478 "selectCallee() didn't honor the threshold");
480 auto ExportModulePath
= ResolvedCalleeSummary
->modulePath();
481 auto ILI
= ImportList
[ExportModulePath
].insert(VI
.getGUID());
482 // We previously decided to import this GUID definition if it was already
483 // inserted in the set of imports from the exporting module.
484 bool PreviouslyImported
= !ILI
.second
;
485 if (!PreviouslyImported
) {
486 NumImportedFunctionsThinLink
++;
488 NumImportedHotFunctionsThinLink
++;
489 if (IsCriticalCallsite
)
490 NumImportedCriticalFunctionsThinLink
++;
493 // Any calls/references made by this function will be marked exported
494 // later, in ComputeCrossModuleImport, after import decisions are
495 // complete, which is more efficient than adding them here.
497 (*ExportLists
)[ExportModulePath
].insert(VI
);
500 auto GetAdjustedThreshold
= [](unsigned Threshold
, bool IsHotCallsite
) {
501 // Adjust the threshold for next level of imported functions.
502 // The threshold is different for hot callsites because we can then
503 // inline chains of hot calls.
505 return Threshold
* ImportHotInstrFactor
;
506 return Threshold
* ImportInstrFactor
;
509 const auto AdjThreshold
= GetAdjustedThreshold(Threshold
, IsHotCallsite
);
513 // Insert the newly imported function to the worklist.
514 Worklist
.emplace_back(ResolvedCalleeSummary
, AdjThreshold
);
518 /// Given the list of globals defined in a module, compute the list of imports
519 /// as well as the list of "exports", i.e. the list of symbols referenced from
520 /// another module (that may require promotion).
521 static void ComputeImportForModule(
522 const GVSummaryMapTy
&DefinedGVSummaries
, const ModuleSummaryIndex
&Index
,
523 StringRef ModName
, FunctionImporter::ImportMapTy
&ImportList
,
524 StringMap
<FunctionImporter::ExportSetTy
> *ExportLists
= nullptr) {
525 // Worklist contains the list of function imported in this module, for which
526 // we will analyse the callees and may import further down the callgraph.
527 SmallVector
<EdgeInfo
, 128> Worklist
;
528 FunctionImporter::ImportThresholdsTy ImportThresholds
;
530 // Populate the worklist with the import for the functions in the current
532 for (const auto &GVSummary
: DefinedGVSummaries
) {
534 // FIXME: Change the GVSummaryMapTy to hold ValueInfo instead of GUID
535 // so this map look up (and possibly others) can be avoided.
536 auto VI
= Index
.getValueInfo(GVSummary
.first
);
538 if (!Index
.isGlobalValueLive(GVSummary
.second
)) {
539 LLVM_DEBUG(dbgs() << "Ignores Dead GUID: " << VI
<< "\n");
543 dyn_cast
<FunctionSummary
>(GVSummary
.second
->getBaseObject());
545 // Skip import for global variables
547 LLVM_DEBUG(dbgs() << "Initialize import for " << VI
<< "\n");
548 computeImportForFunction(*FuncSummary
, Index
, ImportInstrLimit
,
549 DefinedGVSummaries
, Worklist
, ImportList
,
550 ExportLists
, ImportThresholds
);
553 // Process the newly imported functions and add callees to the worklist.
554 while (!Worklist
.empty()) {
555 auto GVInfo
= Worklist
.pop_back_val();
556 auto *Summary
= std::get
<0>(GVInfo
);
557 auto Threshold
= std::get
<1>(GVInfo
);
559 if (auto *FS
= dyn_cast
<FunctionSummary
>(Summary
))
560 computeImportForFunction(*FS
, Index
, Threshold
, DefinedGVSummaries
,
561 Worklist
, ImportList
, ExportLists
,
564 computeImportForReferencedGlobals(*Summary
, Index
, DefinedGVSummaries
,
565 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
, ValueInfo VI
) {
595 auto SL
= VI
.getSummaryList();
598 : SL
[0]->getSummaryKind() == GlobalValueSummary::GlobalVarKind
;
601 static bool isGlobalVarSummary(const ModuleSummaryIndex
&Index
,
602 GlobalValue::GUID G
) {
603 if (const auto &VI
= Index
.getValueInfo(G
))
604 return isGlobalVarSummary(Index
, VI
);
609 static unsigned numGlobalVarSummaries(const ModuleSummaryIndex
&Index
,
613 if (isGlobalVarSummary(Index
, V
))
621 checkVariableImport(const ModuleSummaryIndex
&Index
,
622 StringMap
<FunctionImporter::ImportMapTy
> &ImportLists
,
623 StringMap
<FunctionImporter::ExportSetTy
> &ExportLists
) {
625 DenseSet
<GlobalValue::GUID
> FlattenedImports
;
627 for (auto &ImportPerModule
: ImportLists
)
628 for (auto &ExportPerModule
: ImportPerModule
.second
)
629 FlattenedImports
.insert(ExportPerModule
.second
.begin(),
630 ExportPerModule
.second
.end());
632 // Checks that all GUIDs of read/writeonly vars we see in export lists
633 // are also in the import lists. Otherwise we my face linker undefs,
634 // because readonly and writeonly vars are internalized in their
636 auto IsReadOrWriteOnlyVar
= [&](StringRef ModulePath
, const ValueInfo
&VI
) {
637 auto *GVS
= dyn_cast_or_null
<GlobalVarSummary
>(
638 Index
.findSummaryInModule(VI
, ModulePath
));
639 return GVS
&& (Index
.isReadOnly(GVS
) || Index
.isWriteOnly(GVS
));
642 for (auto &ExportPerModule
: ExportLists
)
643 for (auto &VI
: ExportPerModule
.second
)
644 if (!FlattenedImports
.count(VI
.getGUID()) &&
645 IsReadOrWriteOnlyVar(ExportPerModule
.first(), VI
))
652 /// Compute all the import and export for every module using the Index.
653 void llvm::ComputeCrossModuleImport(
654 const ModuleSummaryIndex
&Index
,
655 const StringMap
<GVSummaryMapTy
> &ModuleToDefinedGVSummaries
,
656 StringMap
<FunctionImporter::ImportMapTy
> &ImportLists
,
657 StringMap
<FunctionImporter::ExportSetTy
> &ExportLists
) {
658 // For each module that has function defined, compute the import/export lists.
659 for (const auto &DefinedGVSummaries
: ModuleToDefinedGVSummaries
) {
660 auto &ImportList
= ImportLists
[DefinedGVSummaries
.first()];
661 LLVM_DEBUG(dbgs() << "Computing import for Module '"
662 << DefinedGVSummaries
.first() << "'\n");
663 ComputeImportForModule(DefinedGVSummaries
.second
, Index
,
664 DefinedGVSummaries
.first(), ImportList
,
668 // When computing imports we only added the variables and functions being
669 // imported to the export list. We also need to mark any references and calls
670 // they make as exported as well. We do this here, as it is more efficient
671 // since we may import the same values multiple times into different modules
672 // during the import computation.
673 for (auto &ELI
: ExportLists
) {
674 FunctionImporter::ExportSetTy NewExports
;
675 const auto &DefinedGVSummaries
=
676 ModuleToDefinedGVSummaries
.lookup(ELI
.first());
677 for (auto &EI
: ELI
.second
) {
678 // Find the copy defined in the exporting module so that we can mark the
679 // values it references in that specific definition as exported.
680 // Below we will add all references and called values, without regard to
681 // whether they are also defined in this module. We subsequently prune the
682 // list to only include those defined in the exporting module, see comment
684 auto DS
= DefinedGVSummaries
.find(EI
.getGUID());
685 // Anything marked exported during the import computation must have been
686 // defined in the exporting module.
687 assert(DS
!= DefinedGVSummaries
.end());
688 auto *S
= DS
->getSecond();
689 S
= S
->getBaseObject();
690 if (auto *GVS
= dyn_cast
<GlobalVarSummary
>(S
)) {
691 // Export referenced functions and variables. We don't export/promote
692 // objects referenced by writeonly variable initializer, because
693 // we convert such variables initializers to "zeroinitializer".
694 // See processGlobalForThinLTO.
695 if (!Index
.isWriteOnly(GVS
))
696 for (const auto &VI
: GVS
->refs())
697 NewExports
.insert(VI
);
699 auto *FS
= cast
<FunctionSummary
>(S
);
700 for (const auto &Edge
: FS
->calls())
701 NewExports
.insert(Edge
.first
);
702 for (const auto &Ref
: FS
->refs())
703 NewExports
.insert(Ref
);
706 // Prune list computed above to only include values defined in the exporting
707 // module. We do this after the above insertion since we may hit the same
708 // ref/call target multiple times in above loop, and it is more efficient to
709 // avoid a set lookup each time.
710 for (auto EI
= NewExports
.begin(); EI
!= NewExports
.end();) {
711 if (!DefinedGVSummaries
.count(EI
->getGUID()))
712 NewExports
.erase(EI
++);
716 ELI
.second
.insert(NewExports
.begin(), NewExports
.end());
719 assert(checkVariableImport(Index
, ImportLists
, ExportLists
));
721 LLVM_DEBUG(dbgs() << "Import/Export lists for " << ImportLists
.size()
723 for (auto &ModuleImports
: ImportLists
) {
724 auto ModName
= ModuleImports
.first();
725 auto &Exports
= ExportLists
[ModName
];
726 unsigned NumGVS
= numGlobalVarSummaries(Index
, Exports
);
727 LLVM_DEBUG(dbgs() << "* Module " << ModName
<< " exports "
728 << Exports
.size() - NumGVS
<< " functions and " << NumGVS
729 << " vars. Imports from " << ModuleImports
.second
.size()
731 for (auto &Src
: ModuleImports
.second
) {
732 auto SrcModName
= Src
.first();
733 unsigned NumGVSPerMod
= numGlobalVarSummaries(Index
, Src
.second
);
734 LLVM_DEBUG(dbgs() << " - " << Src
.second
.size() - NumGVSPerMod
735 << " functions imported from " << SrcModName
<< "\n");
736 LLVM_DEBUG(dbgs() << " - " << NumGVSPerMod
737 << " global vars imported from " << SrcModName
<< "\n");
744 static void dumpImportListForModule(const ModuleSummaryIndex
&Index
,
745 StringRef ModulePath
,
746 FunctionImporter::ImportMapTy
&ImportList
) {
747 LLVM_DEBUG(dbgs() << "* Module " << ModulePath
<< " imports from "
748 << ImportList
.size() << " modules.\n");
749 for (auto &Src
: ImportList
) {
750 auto SrcModName
= Src
.first();
751 unsigned NumGVSPerMod
= numGlobalVarSummaries(Index
, Src
.second
);
752 LLVM_DEBUG(dbgs() << " - " << Src
.second
.size() - NumGVSPerMod
753 << " functions imported from " << SrcModName
<< "\n");
754 LLVM_DEBUG(dbgs() << " - " << NumGVSPerMod
<< " vars imported from "
755 << SrcModName
<< "\n");
760 /// Compute all the imports for the given module in the Index.
761 void llvm::ComputeCrossModuleImportForModule(
762 StringRef ModulePath
, const ModuleSummaryIndex
&Index
,
763 FunctionImporter::ImportMapTy
&ImportList
) {
764 // Collect the list of functions this module defines.
766 GVSummaryMapTy FunctionSummaryMap
;
767 Index
.collectDefinedFunctionsForModule(ModulePath
, FunctionSummaryMap
);
769 // Compute the import list for this module.
770 LLVM_DEBUG(dbgs() << "Computing import for Module '" << ModulePath
<< "'\n");
771 ComputeImportForModule(FunctionSummaryMap
, Index
, ModulePath
, ImportList
);
774 dumpImportListForModule(Index
, ModulePath
, ImportList
);
778 // Mark all external summaries in Index for import into the given module.
779 // Used for distributed builds using a distributed index.
780 void llvm::ComputeCrossModuleImportForModuleFromIndex(
781 StringRef ModulePath
, const ModuleSummaryIndex
&Index
,
782 FunctionImporter::ImportMapTy
&ImportList
) {
783 for (const auto &GlobalList
: Index
) {
784 // Ignore entries for undefined references.
785 if (GlobalList
.second
.SummaryList
.empty())
788 auto GUID
= GlobalList
.first
;
789 assert(GlobalList
.second
.SummaryList
.size() == 1 &&
790 "Expected individual combined index to have one summary per GUID");
791 auto &Summary
= GlobalList
.second
.SummaryList
[0];
792 // Skip the summaries for the importing module. These are included to
793 // e.g. record required linkage changes.
794 if (Summary
->modulePath() == ModulePath
)
796 // Add an entry to provoke importing by thinBackend.
797 ImportList
[Summary
->modulePath()].insert(GUID
);
800 dumpImportListForModule(Index
, ModulePath
, ImportList
);
804 // For SamplePGO, the indirect call targets for local functions will
805 // have its original name annotated in profile. We try to find the
806 // corresponding PGOFuncName as the GUID, and fix up the edges
808 void updateValueInfoForIndirectCalls(ModuleSummaryIndex
&Index
,
809 FunctionSummary
*FS
) {
810 for (auto &EI
: FS
->mutableCalls()) {
811 if (!EI
.first
.getSummaryList().empty())
813 auto GUID
= Index
.getGUIDFromOriginalID(EI
.first
.getGUID());
816 // Update the edge to point directly to the correct GUID.
817 auto VI
= Index
.getValueInfo(GUID
);
820 [&](const std::unique_ptr
<GlobalValueSummary
> &SummaryPtr
) {
821 // The mapping from OriginalId to GUID may return a GUID
822 // that corresponds to a static variable. Filter it out here.
823 // This can happen when
824 // 1) There is a call to a library function which is not defined
826 // 2) There is a static variable with the OriginalGUID identical
827 // to the GUID of the library function in 1);
828 // When this happens the static variable in 2) will be found,
829 // which needs to be filtered out.
830 return SummaryPtr
->getSummaryKind() ==
831 GlobalValueSummary::GlobalVarKind
;
838 void llvm::updateIndirectCalls(ModuleSummaryIndex
&Index
) {
839 for (const auto &Entry
: Index
) {
840 for (const auto &S
: Entry
.second
.SummaryList
) {
841 if (auto *FS
= dyn_cast
<FunctionSummary
>(S
.get()))
842 updateValueInfoForIndirectCalls(Index
, FS
);
847 void llvm::computeDeadSymbolsAndUpdateIndirectCalls(
848 ModuleSummaryIndex
&Index
,
849 const DenseSet
<GlobalValue::GUID
> &GUIDPreservedSymbols
,
850 function_ref
<PrevailingType(GlobalValue::GUID
)> isPrevailing
) {
851 assert(!Index
.withGlobalValueDeadStripping());
853 // Don't do anything when nothing is live, this is friendly with tests.
854 GUIDPreservedSymbols
.empty()) {
855 // Still need to update indirect calls.
856 updateIndirectCalls(Index
);
859 unsigned LiveSymbols
= 0;
860 SmallVector
<ValueInfo
, 128> Worklist
;
861 Worklist
.reserve(GUIDPreservedSymbols
.size() * 2);
862 for (auto GUID
: GUIDPreservedSymbols
) {
863 ValueInfo VI
= Index
.getValueInfo(GUID
);
866 for (const auto &S
: VI
.getSummaryList())
870 // Add values flagged in the index as live roots to the worklist.
871 for (const auto &Entry
: Index
) {
872 auto VI
= Index
.getValueInfo(Entry
);
873 for (const auto &S
: Entry
.second
.SummaryList
) {
874 if (auto *FS
= dyn_cast
<FunctionSummary
>(S
.get()))
875 updateValueInfoForIndirectCalls(Index
, FS
);
877 LLVM_DEBUG(dbgs() << "Live root: " << VI
<< "\n");
878 Worklist
.push_back(VI
);
885 // Make value live and add it to the worklist if it was not live before.
886 auto visit
= [&](ValueInfo VI
, bool IsAliasee
) {
887 // FIXME: If we knew which edges were created for indirect call profiles,
888 // we could skip them here. Any that are live should be reached via
889 // other edges, e.g. reference edges. Otherwise, using a profile collected
890 // on a slightly different binary might provoke preserving, importing
891 // and ultimately promoting calls to functions not linked into this
892 // binary, which increases the binary size unnecessarily. Note that
893 // if this code changes, the importer needs to change so that edges
894 // to functions marked dead are skipped.
896 if (llvm::any_of(VI
.getSummaryList(),
897 [](const std::unique_ptr
<llvm::GlobalValueSummary
> &S
) {
902 // We only keep live symbols that are known to be non-prevailing if any are
903 // available_externally, linkonceodr, weakodr. Those symbols are discarded
904 // later in the EliminateAvailableExternally pass and setting them to
905 // not-live could break downstreams users of liveness information (PR36483)
906 // or limit optimization opportunities.
907 if (isPrevailing(VI
.getGUID()) == PrevailingType::No
) {
908 bool KeepAliveLinkage
= false;
909 bool Interposable
= false;
910 for (const auto &S
: VI
.getSummaryList()) {
911 if (S
->linkage() == GlobalValue::AvailableExternallyLinkage
||
912 S
->linkage() == GlobalValue::WeakODRLinkage
||
913 S
->linkage() == GlobalValue::LinkOnceODRLinkage
)
914 KeepAliveLinkage
= true;
915 else if (GlobalValue::isInterposableLinkage(S
->linkage()))
920 if (!KeepAliveLinkage
)
925 "Interposable and available_externally/linkonce_odr/weak_odr "
930 for (const auto &S
: VI
.getSummaryList())
933 Worklist
.push_back(VI
);
936 while (!Worklist
.empty()) {
937 auto VI
= Worklist
.pop_back_val();
938 for (const auto &Summary
: VI
.getSummaryList()) {
939 if (auto *AS
= dyn_cast
<AliasSummary
>(Summary
.get())) {
940 // If this is an alias, visit the aliasee VI to ensure that all copies
941 // are marked live and it is added to the worklist for further
942 // processing of its references.
943 visit(AS
->getAliaseeVI(), true);
946 for (auto Ref
: Summary
->refs())
948 if (auto *FS
= dyn_cast
<FunctionSummary
>(Summary
.get()))
949 for (auto Call
: FS
->calls())
950 visit(Call
.first
, false);
953 Index
.setWithGlobalValueDeadStripping();
955 unsigned DeadSymbols
= Index
.size() - LiveSymbols
;
956 LLVM_DEBUG(dbgs() << LiveSymbols
<< " symbols Live, and " << DeadSymbols
957 << " symbols Dead \n");
958 NumDeadSymbols
+= DeadSymbols
;
959 NumLiveSymbols
+= LiveSymbols
;
962 // Compute dead symbols and propagate constants in combined index.
963 void llvm::computeDeadSymbolsWithConstProp(
964 ModuleSummaryIndex
&Index
,
965 const DenseSet
<GlobalValue::GUID
> &GUIDPreservedSymbols
,
966 function_ref
<PrevailingType(GlobalValue::GUID
)> isPrevailing
,
967 bool ImportEnabled
) {
968 computeDeadSymbolsAndUpdateIndirectCalls(Index
, GUIDPreservedSymbols
,
971 Index
.propagateAttributes(GUIDPreservedSymbols
);
974 /// Compute the set of summaries needed for a ThinLTO backend compilation of
976 void llvm::gatherImportedSummariesForModule(
977 StringRef ModulePath
,
978 const StringMap
<GVSummaryMapTy
> &ModuleToDefinedGVSummaries
,
979 const FunctionImporter::ImportMapTy
&ImportList
,
980 std::map
<std::string
, GVSummaryMapTy
> &ModuleToSummariesForIndex
) {
981 // Include all summaries from the importing module.
982 ModuleToSummariesForIndex
[std::string(ModulePath
)] =
983 ModuleToDefinedGVSummaries
.lookup(ModulePath
);
984 // Include summaries for imports.
985 for (const auto &ILI
: ImportList
) {
986 auto &SummariesForIndex
=
987 ModuleToSummariesForIndex
[std::string(ILI
.first())];
988 const auto &DefinedGVSummaries
=
989 ModuleToDefinedGVSummaries
.lookup(ILI
.first());
990 for (const auto &GI
: ILI
.second
) {
991 const auto &DS
= DefinedGVSummaries
.find(GI
);
992 assert(DS
!= DefinedGVSummaries
.end() &&
993 "Expected a defined summary for imported global value");
994 SummariesForIndex
[GI
] = DS
->second
;
999 /// Emit the files \p ModulePath will import from into \p OutputFilename.
1000 std::error_code
llvm::EmitImportsFiles(
1001 StringRef ModulePath
, StringRef OutputFilename
,
1002 const std::map
<std::string
, GVSummaryMapTy
> &ModuleToSummariesForIndex
) {
1004 raw_fd_ostream
ImportsOS(OutputFilename
, EC
, sys::fs::OpenFlags::OF_None
);
1007 for (const auto &ILI
: ModuleToSummariesForIndex
)
1008 // The ModuleToSummariesForIndex map includes an entry for the current
1009 // Module (needed for writing out the index files). We don't want to
1010 // include it in the imports file, however, so filter it out.
1011 if (ILI
.first
!= ModulePath
)
1012 ImportsOS
<< ILI
.first
<< "\n";
1013 return std::error_code();
1016 bool llvm::convertToDeclaration(GlobalValue
&GV
) {
1017 LLVM_DEBUG(dbgs() << "Converting to a declaration: `" << GV
.getName()
1019 if (Function
*F
= dyn_cast
<Function
>(&GV
)) {
1022 F
->setComdat(nullptr);
1023 } else if (GlobalVariable
*V
= dyn_cast
<GlobalVariable
>(&GV
)) {
1024 V
->setInitializer(nullptr);
1025 V
->setLinkage(GlobalValue::ExternalLinkage
);
1027 V
->setComdat(nullptr);
1030 if (GV
.getValueType()->isFunctionTy())
1032 Function::Create(cast
<FunctionType
>(GV
.getValueType()),
1033 GlobalValue::ExternalLinkage
, GV
.getAddressSpace(),
1034 "", GV
.getParent());
1037 new GlobalVariable(*GV
.getParent(), GV
.getValueType(),
1038 /*isConstant*/ false, GlobalValue::ExternalLinkage
,
1039 /*init*/ nullptr, "",
1040 /*insertbefore*/ nullptr, GV
.getThreadLocalMode(),
1041 GV
.getType()->getAddressSpace());
1042 NewGV
->takeName(&GV
);
1043 GV
.replaceAllUsesWith(NewGV
);
1046 if (!GV
.isImplicitDSOLocal())
1047 GV
.setDSOLocal(false);
1051 void llvm::thinLTOFinalizeInModule(Module
&TheModule
,
1052 const GVSummaryMapTy
&DefinedGlobals
,
1053 bool PropagateAttrs
) {
1054 auto FinalizeInModule
= [&](GlobalValue
&GV
, bool Propagate
= false) {
1055 // See if the global summary analysis computed a new resolved linkage.
1056 const auto &GS
= DefinedGlobals
.find(GV
.getGUID());
1057 if (GS
== DefinedGlobals
.end())
1061 if (FunctionSummary
*FS
= dyn_cast
<FunctionSummary
>(GS
->second
)) {
1062 if (Function
*F
= dyn_cast
<Function
>(&GV
)) {
1063 // TODO: propagate ReadNone and ReadOnly.
1064 if (FS
->fflags().ReadNone
&& !F
->doesNotAccessMemory())
1065 F
->setDoesNotAccessMemory();
1067 if (FS
->fflags().ReadOnly
&& !F
->onlyReadsMemory())
1068 F
->setOnlyReadsMemory();
1070 if (FS
->fflags().NoRecurse
&& !F
->doesNotRecurse())
1071 F
->setDoesNotRecurse();
1073 if (FS
->fflags().NoUnwind
&& !F
->doesNotThrow())
1074 F
->setDoesNotThrow();
1078 auto NewLinkage
= GS
->second
->linkage();
1079 if (GlobalValue::isLocalLinkage(GV
.getLinkage()) ||
1080 // Don't internalize anything here, because the code below
1081 // lacks necessary correctness checks. Leave this job to
1082 // LLVM 'internalize' pass.
1083 GlobalValue::isLocalLinkage(NewLinkage
) ||
1084 // In case it was dead and already converted to declaration.
1088 // Set the potentially more constraining visibility computed from summaries.
1089 // The DefaultVisibility condition is because older GlobalValueSummary does
1090 // not record DefaultVisibility and we don't want to change protected/hidden
1092 if (GS
->second
->getVisibility() != GlobalValue::DefaultVisibility
)
1093 GV
.setVisibility(GS
->second
->getVisibility());
1095 if (NewLinkage
== GV
.getLinkage())
1098 // Check for a non-prevailing def that has interposable linkage
1099 // (e.g. non-odr weak or linkonce). In that case we can't simply
1100 // convert to available_externally, since it would lose the
1101 // interposable property and possibly get inlined. Simply drop
1102 // the definition in that case.
1103 if (GlobalValue::isAvailableExternallyLinkage(NewLinkage
) &&
1104 GlobalValue::isInterposableLinkage(GV
.getLinkage())) {
1105 if (!convertToDeclaration(GV
))
1106 // FIXME: Change this to collect replaced GVs and later erase
1107 // them from the parent module once thinLTOResolvePrevailingGUID is
1108 // changed to enable this for aliases.
1109 llvm_unreachable("Expected GV to be converted");
1111 // If all copies of the original symbol had global unnamed addr and
1112 // linkonce_odr linkage, or if all of them had local unnamed addr linkage
1113 // and are constants, then it should be an auto hide symbol. In that case
1114 // the thin link would have marked it as CanAutoHide. Add hidden
1115 // visibility to the symbol to preserve the property.
1116 if (NewLinkage
== GlobalValue::WeakODRLinkage
&&
1117 GS
->second
->canAutoHide()) {
1118 assert(GV
.canBeOmittedFromSymbolTable());
1119 GV
.setVisibility(GlobalValue::HiddenVisibility
);
1122 LLVM_DEBUG(dbgs() << "ODR fixing up linkage for `" << GV
.getName()
1123 << "` from " << GV
.getLinkage() << " to " << NewLinkage
1125 GV
.setLinkage(NewLinkage
);
1127 // Remove declarations from comdats, including available_externally
1128 // as this is a declaration for the linker, and will be dropped eventually.
1129 // It is illegal for comdats to contain declarations.
1130 auto *GO
= dyn_cast_or_null
<GlobalObject
>(&GV
);
1131 if (GO
&& GO
->isDeclarationForLinker() && GO
->hasComdat())
1132 GO
->setComdat(nullptr);
1135 // Process functions and global now
1136 for (auto &GV
: TheModule
)
1137 FinalizeInModule(GV
, PropagateAttrs
);
1138 for (auto &GV
: TheModule
.globals())
1139 FinalizeInModule(GV
);
1140 for (auto &GV
: TheModule
.aliases())
1141 FinalizeInModule(GV
);
1144 /// Run internalization on \p TheModule based on symmary analysis.
1145 void llvm::thinLTOInternalizeModule(Module
&TheModule
,
1146 const GVSummaryMapTy
&DefinedGlobals
) {
1147 // Declare a callback for the internalize pass that will ask for every
1148 // candidate GlobalValue if it can be internalized or not.
1149 auto MustPreserveGV
= [&](const GlobalValue
&GV
) -> bool {
1150 // It may be the case that GV is on a chain of an ifunc, its alias and
1151 // subsequent aliases. In this case, the summary for the value is not
1153 if (isa
<GlobalIFunc
>(&GV
) ||
1154 (isa
<GlobalAlias
>(&GV
) &&
1155 isa
<GlobalIFunc
>(cast
<GlobalAlias
>(&GV
)->getAliaseeObject())))
1158 // Lookup the linkage recorded in the summaries during global analysis.
1159 auto GS
= DefinedGlobals
.find(GV
.getGUID());
1160 if (GS
== DefinedGlobals
.end()) {
1161 // Must have been promoted (possibly conservatively). Find original
1162 // name so that we can access the correct summary and see if it can
1163 // be internalized again.
1164 // FIXME: Eventually we should control promotion instead of promoting
1165 // and internalizing again.
1166 StringRef OrigName
=
1167 ModuleSummaryIndex::getOriginalNameBeforePromote(GV
.getName());
1168 std::string OrigId
= GlobalValue::getGlobalIdentifier(
1169 OrigName
, GlobalValue::InternalLinkage
,
1170 TheModule
.getSourceFileName());
1171 GS
= DefinedGlobals
.find(GlobalValue::getGUID(OrigId
));
1172 if (GS
== DefinedGlobals
.end()) {
1173 // Also check the original non-promoted non-globalized name. In some
1174 // cases a preempted weak value is linked in as a local copy because
1175 // it is referenced by an alias (IRLinker::linkGlobalValueProto).
1176 // In that case, since it was originally not a local value, it was
1177 // recorded in the index using the original name.
1178 // FIXME: This may not be needed once PR27866 is fixed.
1179 GS
= DefinedGlobals
.find(GlobalValue::getGUID(OrigName
));
1180 assert(GS
!= DefinedGlobals
.end());
1183 return !GlobalValue::isLocalLinkage(GS
->second
->linkage());
1186 // FIXME: See if we can just internalize directly here via linkage changes
1187 // based on the index, rather than invoking internalizeModule.
1188 internalizeModule(TheModule
, MustPreserveGV
);
1191 /// Make alias a clone of its aliasee.
1192 static Function
*replaceAliasWithAliasee(Module
*SrcModule
, GlobalAlias
*GA
) {
1193 Function
*Fn
= cast
<Function
>(GA
->getAliaseeObject());
1195 ValueToValueMapTy VMap
;
1196 Function
*NewFn
= CloneFunction(Fn
, VMap
);
1197 // Clone should use the original alias's linkage, visibility and name, and we
1198 // ensure all uses of alias instead use the new clone (casted if necessary).
1199 NewFn
->setLinkage(GA
->getLinkage());
1200 NewFn
->setVisibility(GA
->getVisibility());
1201 GA
->replaceAllUsesWith(ConstantExpr::getBitCast(NewFn
, GA
->getType()));
1202 NewFn
->takeName(GA
);
1206 // Internalize values that we marked with specific attribute
1207 // in processGlobalForThinLTO.
1208 static void internalizeGVsAfterImport(Module
&M
) {
1209 for (auto &GV
: M
.globals())
1210 // Skip GVs which have been converted to declarations
1211 // by dropDeadSymbols.
1212 if (!GV
.isDeclaration() && GV
.hasAttribute("thinlto-internalize")) {
1213 GV
.setLinkage(GlobalValue::InternalLinkage
);
1214 GV
.setVisibility(GlobalValue::DefaultVisibility
);
1218 // Automatically import functions in Module \p DestModule based on the summaries
1220 Expected
<bool> FunctionImporter::importFunctions(
1221 Module
&DestModule
, const FunctionImporter::ImportMapTy
&ImportList
) {
1222 LLVM_DEBUG(dbgs() << "Starting import for Module "
1223 << DestModule
.getModuleIdentifier() << "\n");
1224 unsigned ImportedCount
= 0, ImportedGVCount
= 0;
1226 IRMover
Mover(DestModule
);
1227 // Do the actual import of functions now, one Module at a time
1228 std::set
<StringRef
> ModuleNameOrderedList
;
1229 for (const auto &FunctionsToImportPerModule
: ImportList
) {
1230 ModuleNameOrderedList
.insert(FunctionsToImportPerModule
.first());
1232 for (const auto &Name
: ModuleNameOrderedList
) {
1233 // Get the module for the import
1234 const auto &FunctionsToImportPerModule
= ImportList
.find(Name
);
1235 assert(FunctionsToImportPerModule
!= ImportList
.end());
1236 Expected
<std::unique_ptr
<Module
>> SrcModuleOrErr
= ModuleLoader(Name
);
1237 if (!SrcModuleOrErr
)
1238 return SrcModuleOrErr
.takeError();
1239 std::unique_ptr
<Module
> SrcModule
= std::move(*SrcModuleOrErr
);
1240 assert(&DestModule
.getContext() == &SrcModule
->getContext() &&
1241 "Context mismatch");
1243 // If modules were created with lazy metadata loading, materialize it
1244 // now, before linking it (otherwise this will be a noop).
1245 if (Error Err
= SrcModule
->materializeMetadata())
1246 return std::move(Err
);
1248 auto &ImportGUIDs
= FunctionsToImportPerModule
->second
;
1249 // Find the globals to import
1250 SetVector
<GlobalValue
*> GlobalsToImport
;
1251 for (Function
&F
: *SrcModule
) {
1254 auto GUID
= F
.getGUID();
1255 auto Import
= ImportGUIDs
.count(GUID
);
1256 LLVM_DEBUG(dbgs() << (Import
? "Is" : "Not") << " importing function "
1257 << GUID
<< " " << F
.getName() << " from "
1258 << SrcModule
->getSourceFileName() << "\n");
1260 if (Error Err
= F
.materialize())
1261 return std::move(Err
);
1262 if (EnableImportMetadata
) {
1263 // Add 'thinlto_src_module' metadata for statistics and debugging.
1265 "thinlto_src_module",
1266 MDNode::get(DestModule
.getContext(),
1267 {MDString::get(DestModule
.getContext(),
1268 SrcModule
->getSourceFileName())}));
1270 GlobalsToImport
.insert(&F
);
1273 for (GlobalVariable
&GV
: SrcModule
->globals()) {
1276 auto GUID
= GV
.getGUID();
1277 auto Import
= ImportGUIDs
.count(GUID
);
1278 LLVM_DEBUG(dbgs() << (Import
? "Is" : "Not") << " importing global "
1279 << GUID
<< " " << GV
.getName() << " from "
1280 << SrcModule
->getSourceFileName() << "\n");
1282 if (Error Err
= GV
.materialize())
1283 return std::move(Err
);
1284 ImportedGVCount
+= GlobalsToImport
.insert(&GV
);
1287 for (GlobalAlias
&GA
: SrcModule
->aliases()) {
1288 if (!GA
.hasName() || isa
<GlobalIFunc
>(GA
.getAliaseeObject()))
1290 auto GUID
= GA
.getGUID();
1291 auto Import
= ImportGUIDs
.count(GUID
);
1292 LLVM_DEBUG(dbgs() << (Import
? "Is" : "Not") << " importing alias "
1293 << GUID
<< " " << GA
.getName() << " from "
1294 << SrcModule
->getSourceFileName() << "\n");
1296 if (Error Err
= GA
.materialize())
1297 return std::move(Err
);
1298 // Import alias as a copy of its aliasee.
1299 GlobalObject
*GO
= GA
.getAliaseeObject();
1300 if (Error Err
= GO
->materialize())
1301 return std::move(Err
);
1302 auto *Fn
= replaceAliasWithAliasee(SrcModule
.get(), &GA
);
1303 LLVM_DEBUG(dbgs() << "Is importing aliasee fn " << GO
->getGUID() << " "
1304 << GO
->getName() << " from "
1305 << SrcModule
->getSourceFileName() << "\n");
1306 if (EnableImportMetadata
) {
1307 // Add 'thinlto_src_module' metadata for statistics and debugging.
1309 "thinlto_src_module",
1310 MDNode::get(DestModule
.getContext(),
1311 {MDString::get(DestModule
.getContext(),
1312 SrcModule
->getSourceFileName())}));
1314 GlobalsToImport
.insert(Fn
);
1318 // Upgrade debug info after we're done materializing all the globals and we
1319 // have loaded all the required metadata!
1320 UpgradeDebugInfo(*SrcModule
);
1322 // Set the partial sample profile ratio in the profile summary module flag
1323 // of the imported source module, if applicable, so that the profile summary
1324 // module flag will match with that of the destination module when it's
1326 SrcModule
->setPartialSampleProfileRatio(Index
);
1328 // Link in the specified functions.
1329 if (renameModuleForThinLTO(*SrcModule
, Index
, ClearDSOLocalOnDeclarations
,
1334 for (const auto *GV
: GlobalsToImport
)
1335 dbgs() << DestModule
.getSourceFileName() << ": Import " << GV
->getName()
1336 << " from " << SrcModule
->getSourceFileName() << "\n";
1339 if (Error Err
= Mover
.move(std::move(SrcModule
),
1340 GlobalsToImport
.getArrayRef(), nullptr,
1341 /*IsPerformingImport=*/true))
1342 report_fatal_error(Twine("Function Import: link error: ") +
1343 toString(std::move(Err
)));
1345 ImportedCount
+= GlobalsToImport
.size();
1346 NumImportedModules
++;
1349 internalizeGVsAfterImport(DestModule
);
1351 NumImportedFunctions
+= (ImportedCount
- ImportedGVCount
);
1352 NumImportedGlobalVars
+= ImportedGVCount
;
1354 LLVM_DEBUG(dbgs() << "Imported " << ImportedCount
- ImportedGVCount
1355 << " functions for Module "
1356 << DestModule
.getModuleIdentifier() << "\n");
1357 LLVM_DEBUG(dbgs() << "Imported " << ImportedGVCount
1358 << " global variables for Module "
1359 << DestModule
.getModuleIdentifier() << "\n");
1360 return ImportedCount
;
1363 static bool doImportingForModule(Module
&M
) {
1364 if (SummaryFile
.empty())
1365 report_fatal_error("error: -function-import requires -summary-file\n");
1366 Expected
<std::unique_ptr
<ModuleSummaryIndex
>> IndexPtrOrErr
=
1367 getModuleSummaryIndexForFile(SummaryFile
);
1368 if (!IndexPtrOrErr
) {
1369 logAllUnhandledErrors(IndexPtrOrErr
.takeError(), errs(),
1370 "Error loading file '" + SummaryFile
+ "': ");
1373 std::unique_ptr
<ModuleSummaryIndex
> Index
= std::move(*IndexPtrOrErr
);
1375 // First step is collecting the import list.
1376 FunctionImporter::ImportMapTy ImportList
;
1377 // If requested, simply import all functions in the index. This is used
1378 // when testing distributed backend handling via the opt tool, when
1379 // we have distributed indexes containing exactly the summaries to import.
1381 ComputeCrossModuleImportForModuleFromIndex(M
.getModuleIdentifier(), *Index
,
1384 ComputeCrossModuleImportForModule(M
.getModuleIdentifier(), *Index
,
1387 // Conservatively mark all internal values as promoted. This interface is
1388 // only used when doing importing via the function importing pass. The pass
1389 // is only enabled when testing importing via the 'opt' tool, which does
1390 // not do the ThinLink that would normally determine what values to promote.
1391 for (auto &I
: *Index
) {
1392 for (auto &S
: I
.second
.SummaryList
) {
1393 if (GlobalValue::isLocalLinkage(S
->linkage()))
1394 S
->setLinkage(GlobalValue::ExternalLinkage
);
1398 // Next we need to promote to global scope and rename any local values that
1399 // are potentially exported to other modules.
1400 if (renameModuleForThinLTO(M
, *Index
, /*ClearDSOLocalOnDeclarations=*/false,
1401 /*GlobalsToImport=*/nullptr)) {
1402 errs() << "Error renaming module\n";
1406 // Perform the import now.
1407 auto ModuleLoader
= [&M
](StringRef Identifier
) {
1408 return loadFile(std::string(Identifier
), M
.getContext());
1410 FunctionImporter
Importer(*Index
, ModuleLoader
,
1411 /*ClearDSOLocalOnDeclarations=*/false);
1412 Expected
<bool> Result
= Importer
.importFunctions(M
, ImportList
);
1414 // FIXME: Probably need to propagate Errors through the pass manager.
1416 logAllUnhandledErrors(Result
.takeError(), errs(),
1417 "Error importing module: ");
1424 PreservedAnalyses
FunctionImportPass::run(Module
&M
,
1425 ModuleAnalysisManager
&AM
) {
1426 if (!doImportingForModule(M
))
1427 return PreservedAnalyses::all();
1429 return PreservedAnalyses::none();