1 //===- FunctionImport.cpp - ThinLTO Summary-based Function Import ---------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements Function import based on summaries.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Transforms/IPO/FunctionImport.h"
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SetVector.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/StringSet.h"
22 #include "llvm/Bitcode/BitcodeReader.h"
23 #include "llvm/IR/AutoUpgrade.h"
24 #include "llvm/IR/Constants.h"
25 #include "llvm/IR/Function.h"
26 #include "llvm/IR/GlobalAlias.h"
27 #include "llvm/IR/GlobalObject.h"
28 #include "llvm/IR/GlobalValue.h"
29 #include "llvm/IR/GlobalVariable.h"
30 #include "llvm/IR/Metadata.h"
31 #include "llvm/IR/Module.h"
32 #include "llvm/IR/ModuleSummaryIndex.h"
33 #include "llvm/IRReader/IRReader.h"
34 #include "llvm/InitializePasses.h"
35 #include "llvm/Linker/IRMover.h"
36 #include "llvm/Object/ModuleSymbolTable.h"
37 #include "llvm/Object/SymbolicFile.h"
38 #include "llvm/Pass.h"
39 #include "llvm/Support/Casting.h"
40 #include "llvm/Support/CommandLine.h"
41 #include "llvm/Support/Debug.h"
42 #include "llvm/Support/Errc.h"
43 #include "llvm/Support/Error.h"
44 #include "llvm/Support/ErrorHandling.h"
45 #include "llvm/Support/FileSystem.h"
46 #include "llvm/Support/SourceMgr.h"
47 #include "llvm/Support/raw_ostream.h"
48 #include "llvm/Transforms/IPO/Internalize.h"
49 #include "llvm/Transforms/Utils/Cloning.h"
50 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
51 #include "llvm/Transforms/Utils/ValueMapper.h"
52 #include <cassert>
53 #include <memory>
54 #include <set>
55 #include <string>
56 #include <system_error>
57 #include <tuple>
58 #include <utility>
59 
60 using namespace llvm;
61 
62 #define DEBUG_TYPE "function-import"
63 
64 STATISTIC(NumImportedFunctionsThinLink,
65           "Number of functions thin link decided to import");
66 STATISTIC(NumImportedHotFunctionsThinLink,
67           "Number of hot functions thin link decided to import");
68 STATISTIC(NumImportedCriticalFunctionsThinLink,
69           "Number of critical functions thin link decided to import");
70 STATISTIC(NumImportedGlobalVarsThinLink,
71           "Number of global variables thin link decided to import");
72 STATISTIC(NumImportedFunctions, "Number of functions imported in backend");
73 STATISTIC(NumImportedGlobalVars,
74           "Number of global variables imported in backend");
75 STATISTIC(NumImportedModules, "Number of modules imported from");
76 STATISTIC(NumDeadSymbols, "Number of dead stripped symbols in index");
77 STATISTIC(NumLiveSymbols, "Number of live symbols in index");
78 
79 /// Limit on instruction count of imported functions.
80 static cl::opt<unsigned> ImportInstrLimit(
81     "import-instr-limit", cl::init(100), cl::Hidden, cl::value_desc("N"),
82     cl::desc("Only import functions with less than N instructions"));
83 
84 static cl::opt<int> ImportCutoff(
85     "import-cutoff", cl::init(-1), cl::Hidden, cl::value_desc("N"),
86     cl::desc("Only import first N functions if N>=0 (default -1)"));
87 
88 static cl::opt<bool>
89     ForceImportAll("force-import-all", cl::init(false), cl::Hidden,
90                    cl::desc("Import functions with noinline attribute"));
91 
92 static cl::opt<float>
93     ImportInstrFactor("import-instr-evolution-factor", cl::init(0.7),
94                       cl::Hidden, cl::value_desc("x"),
95                       cl::desc("As we import functions, multiply the "
96                                "`import-instr-limit` threshold by this factor "
97                                "before processing newly imported functions"));
98 
99 static cl::opt<float> ImportHotInstrFactor(
100     "import-hot-evolution-factor", cl::init(1.0), cl::Hidden,
101     cl::value_desc("x"),
102     cl::desc("As we import functions called from hot callsite, multiply the "
103              "`import-instr-limit` threshold by this factor "
104              "before processing newly imported functions"));
105 
106 static cl::opt<float> ImportHotMultiplier(
107     "import-hot-multiplier", cl::init(10.0), cl::Hidden, cl::value_desc("x"),
108     cl::desc("Multiply the `import-instr-limit` threshold for hot callsites"));
109 
110 static cl::opt<float> ImportCriticalMultiplier(
111     "import-critical-multiplier", cl::init(100.0), cl::Hidden,
112     cl::value_desc("x"),
113     cl::desc(
114         "Multiply the `import-instr-limit` threshold for critical callsites"));
115 
116 // FIXME: This multiplier was not really tuned up.
117 static cl::opt<float> ImportColdMultiplier(
118     "import-cold-multiplier", cl::init(0), cl::Hidden, cl::value_desc("N"),
119     cl::desc("Multiply the `import-instr-limit` threshold for cold callsites"));
120 
121 static cl::opt<bool> PrintImports("print-imports", cl::init(false), cl::Hidden,
122                                   cl::desc("Print imported functions"));
123 
124 static cl::opt<bool> PrintImportFailures(
125     "print-import-failures", cl::init(false), cl::Hidden,
126     cl::desc("Print information for functions rejected for importing"));
127 
128 static cl::opt<bool> ComputeDead("compute-dead", cl::init(true), cl::Hidden,
129                                  cl::desc("Compute dead symbols"));
130 
131 static cl::opt<bool> EnableImportMetadata(
132     "enable-import-metadata", cl::init(false), cl::Hidden,
133     cl::desc("Enable import metadata like 'thinlto_src_module'"));
134 
135 /// Summary file to use for function importing when using -function-import from
136 /// the command line.
137 static cl::opt<std::string>
138     SummaryFile("summary-file",
139                 cl::desc("The summary file to use for function importing."));
140 
141 /// Used when testing importing from distributed indexes via opt
142 // -function-import.
143 static cl::opt<bool>
144     ImportAllIndex("import-all-index",
145                    cl::desc("Import all external functions in index."));
146 
147 // Load lazily a module from \p FileName in \p Context.
loadFile(const std::string & FileName,LLVMContext & Context)148 static std::unique_ptr<Module> loadFile(const std::string &FileName,
149                                         LLVMContext &Context) {
150   SMDiagnostic Err;
151   LLVM_DEBUG(dbgs() << "Loading '" << FileName << "'\n");
152   // Metadata isn't loaded until functions are imported, to minimize
153   // the memory overhead.
154   std::unique_ptr<Module> Result =
155       getLazyIRFileModule(FileName, Err, Context,
156                           /* ShouldLazyLoadMetadata = */ true);
157   if (!Result) {
158     Err.print("function-import", errs());
159     report_fatal_error("Abort");
160   }
161 
162   return Result;
163 }
164 
165 /// Given a list of possible callee implementation for a call site, select one
166 /// that fits the \p Threshold.
167 ///
168 /// FIXME: select "best" instead of first that fits. But what is "best"?
169 /// - The smallest: more likely to be inlined.
170 /// - The one with the least outgoing edges (already well optimized).
171 /// - One from a module already being imported from in order to reduce the
172 ///   number of source modules parsed/linked.
173 /// - One that has PGO data attached.
174 /// - [insert you fancy metric here]
175 static const GlobalValueSummary *
selectCallee(const ModuleSummaryIndex & Index,ArrayRef<std::unique_ptr<GlobalValueSummary>> CalleeSummaryList,unsigned Threshold,StringRef CallerModulePath,FunctionImporter::ImportFailureReason & Reason,GlobalValue::GUID GUID)176 selectCallee(const ModuleSummaryIndex &Index,
177              ArrayRef<std::unique_ptr<GlobalValueSummary>> CalleeSummaryList,
178              unsigned Threshold, StringRef CallerModulePath,
179              FunctionImporter::ImportFailureReason &Reason,
180              GlobalValue::GUID GUID) {
181   Reason = FunctionImporter::ImportFailureReason::None;
182   auto It = llvm::find_if(
183       CalleeSummaryList,
184       [&](const std::unique_ptr<GlobalValueSummary> &SummaryPtr) {
185         auto *GVSummary = SummaryPtr.get();
186         if (!Index.isGlobalValueLive(GVSummary)) {
187           Reason = FunctionImporter::ImportFailureReason::NotLive;
188           return false;
189         }
190 
191         // For SamplePGO, in computeImportForFunction the OriginalId
192         // may have been used to locate the callee summary list (See
193         // comment there).
194         // The mapping from OriginalId to GUID may return a GUID
195         // that corresponds to a static variable. Filter it out here.
196         // This can happen when
197         // 1) There is a call to a library function which is not defined
198         // in the index.
199         // 2) There is a static variable with the  OriginalGUID identical
200         // to the GUID of the library function in 1);
201         // When this happens, the logic for SamplePGO kicks in and
202         // the static variable in 2) will be found, which needs to be
203         // filtered out.
204         if (GVSummary->getSummaryKind() == GlobalValueSummary::GlobalVarKind) {
205           Reason = FunctionImporter::ImportFailureReason::GlobalVar;
206           return false;
207         }
208         if (GlobalValue::isInterposableLinkage(GVSummary->linkage())) {
209           Reason = FunctionImporter::ImportFailureReason::InterposableLinkage;
210           // There is no point in importing these, we can't inline them
211           return false;
212         }
213 
214         auto *Summary = cast<FunctionSummary>(GVSummary->getBaseObject());
215 
216         // If this is a local function, make sure we import the copy
217         // in the caller's module. The only time a local function can
218         // share an entry in the index is if there is a local with the same name
219         // in another module that had the same source file name (in a different
220         // directory), where each was compiled in their own directory so there
221         // was not distinguishing path.
222         // However, do the import from another module if there is only one
223         // entry in the list - in that case this must be a reference due
224         // to indirect call profile data, since a function pointer can point to
225         // a local in another module.
226         if (GlobalValue::isLocalLinkage(Summary->linkage()) &&
227             CalleeSummaryList.size() > 1 &&
228             Summary->modulePath() != CallerModulePath) {
229           Reason =
230               FunctionImporter::ImportFailureReason::LocalLinkageNotInModule;
231           return false;
232         }
233 
234         if ((Summary->instCount() > Threshold) &&
235             !Summary->fflags().AlwaysInline && !ForceImportAll) {
236           Reason = FunctionImporter::ImportFailureReason::TooLarge;
237           return false;
238         }
239 
240         // Skip if it isn't legal to import (e.g. may reference unpromotable
241         // locals).
242         if (Summary->notEligibleToImport()) {
243           Reason = FunctionImporter::ImportFailureReason::NotEligible;
244           return false;
245         }
246 
247         // Don't bother importing if we can't inline it anyway.
248         if (Summary->fflags().NoInline && !ForceImportAll) {
249           Reason = FunctionImporter::ImportFailureReason::NoInline;
250           return false;
251         }
252 
253         return true;
254       });
255   if (It == CalleeSummaryList.end())
256     return nullptr;
257 
258   return cast<GlobalValueSummary>(It->get());
259 }
260 
261 namespace {
262 
263 using EdgeInfo =
264     std::tuple<const GlobalValueSummary *, unsigned /* Threshold */>;
265 
266 } // anonymous namespace
267 
268 static ValueInfo
updateValueInfoForIndirectCalls(const ModuleSummaryIndex & Index,ValueInfo VI)269 updateValueInfoForIndirectCalls(const ModuleSummaryIndex &Index, ValueInfo VI) {
270   if (!VI.getSummaryList().empty())
271     return VI;
272   // For SamplePGO, the indirect call targets for local functions will
273   // have its original name annotated in profile. We try to find the
274   // corresponding PGOFuncName as the GUID.
275   // FIXME: Consider updating the edges in the graph after building
276   // it, rather than needing to perform this mapping on each walk.
277   auto GUID = Index.getGUIDFromOriginalID(VI.getGUID());
278   if (GUID == 0)
279     return ValueInfo();
280   return Index.getValueInfo(GUID);
281 }
282 
shouldImportGlobal(const ValueInfo & VI,const GVSummaryMapTy & DefinedGVSummaries)283 static bool shouldImportGlobal(const ValueInfo &VI,
284                                const GVSummaryMapTy &DefinedGVSummaries) {
285   const auto &GVS = DefinedGVSummaries.find(VI.getGUID());
286   if (GVS == DefinedGVSummaries.end())
287     return true;
288   // We should not skip import if the module contains a definition with
289   // interposable linkage type. This is required for correctness in
290   // the situation with two following conditions:
291   // * the def with interposable linkage is non-prevailing,
292   // * there is a prevailing def available for import and marked read-only.
293   // In this case, the non-prevailing def will be converted to a declaration,
294   // while the prevailing one becomes internal, thus no definitions will be
295   // available for linking. In order to prevent undefined symbol link error,
296   // the prevailing definition must be imported.
297   // FIXME: Consider adding a check that the suitable prevailing definition
298   // exists and marked read-only.
299   if (VI.getSummaryList().size() > 1 &&
300       GlobalValue::isInterposableLinkage(GVS->second->linkage()))
301     return true;
302 
303   return false;
304 }
305 
computeImportForReferencedGlobals(const GlobalValueSummary & Summary,const ModuleSummaryIndex & Index,const GVSummaryMapTy & DefinedGVSummaries,SmallVectorImpl<EdgeInfo> & Worklist,FunctionImporter::ImportMapTy & ImportList,StringMap<FunctionImporter::ExportSetTy> * ExportLists)306 static void computeImportForReferencedGlobals(
307     const GlobalValueSummary &Summary, const ModuleSummaryIndex &Index,
308     const GVSummaryMapTy &DefinedGVSummaries,
309     SmallVectorImpl<EdgeInfo> &Worklist,
310     FunctionImporter::ImportMapTy &ImportList,
311     StringMap<FunctionImporter::ExportSetTy> *ExportLists) {
312   for (auto &VI : Summary.refs()) {
313     if (!shouldImportGlobal(VI, DefinedGVSummaries)) {
314       LLVM_DEBUG(
315           dbgs() << "Ref ignored! Target already in destination module.\n");
316       continue;
317     }
318 
319     LLVM_DEBUG(dbgs() << " ref -> " << VI << "\n");
320 
321     // If this is a local variable, make sure we import the copy
322     // in the caller's module. The only time a local variable can
323     // share an entry in the index is if there is a local with the same name
324     // in another module that had the same source file name (in a different
325     // directory), where each was compiled in their own directory so there
326     // was not distinguishing path.
327     auto LocalNotInModule = [&](const GlobalValueSummary *RefSummary) -> bool {
328       return GlobalValue::isLocalLinkage(RefSummary->linkage()) &&
329              RefSummary->modulePath() != Summary.modulePath();
330     };
331 
332     for (auto &RefSummary : VI.getSummaryList())
333       if (isa<GlobalVarSummary>(RefSummary.get()) &&
334           Index.canImportGlobalVar(RefSummary.get(), /* AnalyzeRefs */ true) &&
335           !LocalNotInModule(RefSummary.get())) {
336         auto ILI = ImportList[RefSummary->modulePath()].insert(VI.getGUID());
337         // Only update stat and exports if we haven't already imported this
338         // variable.
339         if (!ILI.second)
340           break;
341         NumImportedGlobalVarsThinLink++;
342         // Any references made by this variable will be marked exported later,
343         // in ComputeCrossModuleImport, after import decisions are complete,
344         // which is more efficient than adding them here.
345         if (ExportLists)
346           (*ExportLists)[RefSummary->modulePath()].insert(VI);
347 
348         // If variable is not writeonly we attempt to recursively analyze
349         // its references in order to import referenced constants.
350         if (!Index.isWriteOnly(cast<GlobalVarSummary>(RefSummary.get())))
351           Worklist.emplace_back(RefSummary.get(), 0);
352         break;
353       }
354   }
355 }
356 
357 static const char *
getFailureName(FunctionImporter::ImportFailureReason Reason)358 getFailureName(FunctionImporter::ImportFailureReason Reason) {
359   switch (Reason) {
360   case FunctionImporter::ImportFailureReason::None:
361     return "None";
362   case FunctionImporter::ImportFailureReason::GlobalVar:
363     return "GlobalVar";
364   case FunctionImporter::ImportFailureReason::NotLive:
365     return "NotLive";
366   case FunctionImporter::ImportFailureReason::TooLarge:
367     return "TooLarge";
368   case FunctionImporter::ImportFailureReason::InterposableLinkage:
369     return "InterposableLinkage";
370   case FunctionImporter::ImportFailureReason::LocalLinkageNotInModule:
371     return "LocalLinkageNotInModule";
372   case FunctionImporter::ImportFailureReason::NotEligible:
373     return "NotEligible";
374   case FunctionImporter::ImportFailureReason::NoInline:
375     return "NoInline";
376   }
377   llvm_unreachable("invalid reason");
378 }
379 
380 /// Compute the list of functions to import for a given caller. Mark these
381 /// imported functions and the symbols they reference in their source module as
382 /// exported from their source module.
computeImportForFunction(const FunctionSummary & Summary,const ModuleSummaryIndex & Index,const unsigned Threshold,const GVSummaryMapTy & DefinedGVSummaries,SmallVectorImpl<EdgeInfo> & Worklist,FunctionImporter::ImportMapTy & ImportList,StringMap<FunctionImporter::ExportSetTy> * ExportLists,FunctionImporter::ImportThresholdsTy & ImportThresholds)383 static void computeImportForFunction(
384     const FunctionSummary &Summary, const ModuleSummaryIndex &Index,
385     const unsigned Threshold, const GVSummaryMapTy &DefinedGVSummaries,
386     SmallVectorImpl<EdgeInfo> &Worklist,
387     FunctionImporter::ImportMapTy &ImportList,
388     StringMap<FunctionImporter::ExportSetTy> *ExportLists,
389     FunctionImporter::ImportThresholdsTy &ImportThresholds) {
390   computeImportForReferencedGlobals(Summary, Index, DefinedGVSummaries,
391                                     Worklist, ImportList, ExportLists);
392   static int ImportCount = 0;
393   for (auto &Edge : Summary.calls()) {
394     ValueInfo VI = Edge.first;
395     LLVM_DEBUG(dbgs() << " edge -> " << VI << " Threshold:" << Threshold
396                       << "\n");
397 
398     if (ImportCutoff >= 0 && ImportCount >= ImportCutoff) {
399       LLVM_DEBUG(dbgs() << "ignored! import-cutoff value of " << ImportCutoff
400                         << " reached.\n");
401       continue;
402     }
403 
404     VI = updateValueInfoForIndirectCalls(Index, VI);
405     if (!VI)
406       continue;
407 
408     if (DefinedGVSummaries.count(VI.getGUID())) {
409       // FIXME: Consider not skipping import if the module contains
410       // a non-prevailing def with interposable linkage. The prevailing copy
411       // can safely be imported (see shouldImportGlobal()).
412       LLVM_DEBUG(dbgs() << "ignored! Target already in destination module.\n");
413       continue;
414     }
415 
416     auto GetBonusMultiplier = [](CalleeInfo::HotnessType Hotness) -> float {
417       if (Hotness == CalleeInfo::HotnessType::Hot)
418         return ImportHotMultiplier;
419       if (Hotness == CalleeInfo::HotnessType::Cold)
420         return ImportColdMultiplier;
421       if (Hotness == CalleeInfo::HotnessType::Critical)
422         return ImportCriticalMultiplier;
423       return 1.0;
424     };
425 
426     const auto NewThreshold =
427         Threshold * GetBonusMultiplier(Edge.second.getHotness());
428 
429     auto IT = ImportThresholds.insert(std::make_pair(
430         VI.getGUID(), std::make_tuple(NewThreshold, nullptr, nullptr)));
431     bool PreviouslyVisited = !IT.second;
432     auto &ProcessedThreshold = std::get<0>(IT.first->second);
433     auto &CalleeSummary = std::get<1>(IT.first->second);
434     auto &FailureInfo = std::get<2>(IT.first->second);
435 
436     bool IsHotCallsite =
437         Edge.second.getHotness() == CalleeInfo::HotnessType::Hot;
438     bool IsCriticalCallsite =
439         Edge.second.getHotness() == CalleeInfo::HotnessType::Critical;
440 
441     const FunctionSummary *ResolvedCalleeSummary = nullptr;
442     if (CalleeSummary) {
443       assert(PreviouslyVisited);
444       // Since the traversal of the call graph is DFS, we can revisit a function
445       // a second time with a higher threshold. In this case, it is added back
446       // to the worklist with the new threshold (so that its own callee chains
447       // can be considered with the higher threshold).
448       if (NewThreshold <= ProcessedThreshold) {
449         LLVM_DEBUG(
450             dbgs() << "ignored! Target was already imported with Threshold "
451                    << ProcessedThreshold << "\n");
452         continue;
453       }
454       // Update with new larger threshold.
455       ProcessedThreshold = NewThreshold;
456       ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary);
457     } else {
458       // If we already rejected importing a callee at the same or higher
459       // threshold, don't waste time calling selectCallee.
460       if (PreviouslyVisited && NewThreshold <= ProcessedThreshold) {
461         LLVM_DEBUG(
462             dbgs() << "ignored! Target was already rejected with Threshold "
463             << ProcessedThreshold << "\n");
464         if (PrintImportFailures) {
465           assert(FailureInfo &&
466                  "Expected FailureInfo for previously rejected candidate");
467           FailureInfo->Attempts++;
468         }
469         continue;
470       }
471 
472       FunctionImporter::ImportFailureReason Reason;
473       CalleeSummary = selectCallee(Index, VI.getSummaryList(), NewThreshold,
474                                    Summary.modulePath(), Reason, VI.getGUID());
475       if (!CalleeSummary) {
476         // Update with new larger threshold if this was a retry (otherwise
477         // we would have already inserted with NewThreshold above). Also
478         // update failure info if requested.
479         if (PreviouslyVisited) {
480           ProcessedThreshold = NewThreshold;
481           if (PrintImportFailures) {
482             assert(FailureInfo &&
483                    "Expected FailureInfo for previously rejected candidate");
484             FailureInfo->Reason = Reason;
485             FailureInfo->Attempts++;
486             FailureInfo->MaxHotness =
487                 std::max(FailureInfo->MaxHotness, Edge.second.getHotness());
488           }
489         } else if (PrintImportFailures) {
490           assert(!FailureInfo &&
491                  "Expected no FailureInfo for newly rejected candidate");
492           FailureInfo = std::make_unique<FunctionImporter::ImportFailureInfo>(
493               VI, Edge.second.getHotness(), Reason, 1);
494         }
495         if (ForceImportAll) {
496           std::string Msg = std::string("Failed to import function ") +
497                             VI.name().str() + " due to " +
498                             getFailureName(Reason);
499           auto Error = make_error<StringError>(
500               Msg, make_error_code(errc::not_supported));
501           logAllUnhandledErrors(std::move(Error), errs(),
502                                 "Error importing module: ");
503           break;
504         } else {
505           LLVM_DEBUG(dbgs()
506                      << "ignored! No qualifying callee with summary found.\n");
507           continue;
508         }
509       }
510 
511       // "Resolve" the summary
512       CalleeSummary = CalleeSummary->getBaseObject();
513       ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary);
514 
515       assert((ResolvedCalleeSummary->fflags().AlwaysInline || ForceImportAll ||
516               (ResolvedCalleeSummary->instCount() <= NewThreshold)) &&
517              "selectCallee() didn't honor the threshold");
518 
519       auto ExportModulePath = ResolvedCalleeSummary->modulePath();
520       auto ILI = ImportList[ExportModulePath].insert(VI.getGUID());
521       // We previously decided to import this GUID definition if it was already
522       // inserted in the set of imports from the exporting module.
523       bool PreviouslyImported = !ILI.second;
524       if (!PreviouslyImported) {
525         NumImportedFunctionsThinLink++;
526         if (IsHotCallsite)
527           NumImportedHotFunctionsThinLink++;
528         if (IsCriticalCallsite)
529           NumImportedCriticalFunctionsThinLink++;
530       }
531 
532       // Any calls/references made by this function will be marked exported
533       // later, in ComputeCrossModuleImport, after import decisions are
534       // complete, which is more efficient than adding them here.
535       if (ExportLists)
536         (*ExportLists)[ExportModulePath].insert(VI);
537     }
538 
539     auto GetAdjustedThreshold = [](unsigned Threshold, bool IsHotCallsite) {
540       // Adjust the threshold for next level of imported functions.
541       // The threshold is different for hot callsites because we can then
542       // inline chains of hot calls.
543       if (IsHotCallsite)
544         return Threshold * ImportHotInstrFactor;
545       return Threshold * ImportInstrFactor;
546     };
547 
548     const auto AdjThreshold = GetAdjustedThreshold(Threshold, IsHotCallsite);
549 
550     ImportCount++;
551 
552     // Insert the newly imported function to the worklist.
553     Worklist.emplace_back(ResolvedCalleeSummary, AdjThreshold);
554   }
555 }
556 
557 /// Given the list of globals defined in a module, compute the list of imports
558 /// as well as the list of "exports", i.e. the list of symbols referenced from
559 /// another module (that may require promotion).
ComputeImportForModule(const GVSummaryMapTy & DefinedGVSummaries,const ModuleSummaryIndex & Index,StringRef ModName,FunctionImporter::ImportMapTy & ImportList,StringMap<FunctionImporter::ExportSetTy> * ExportLists=nullptr)560 static void ComputeImportForModule(
561     const GVSummaryMapTy &DefinedGVSummaries, const ModuleSummaryIndex &Index,
562     StringRef ModName, FunctionImporter::ImportMapTy &ImportList,
563     StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) {
564   // Worklist contains the list of function imported in this module, for which
565   // we will analyse the callees and may import further down the callgraph.
566   SmallVector<EdgeInfo, 128> Worklist;
567   FunctionImporter::ImportThresholdsTy ImportThresholds;
568 
569   // Populate the worklist with the import for the functions in the current
570   // module
571   for (auto &GVSummary : DefinedGVSummaries) {
572 #ifndef NDEBUG
573     // FIXME: Change the GVSummaryMapTy to hold ValueInfo instead of GUID
574     // so this map look up (and possibly others) can be avoided.
575     auto VI = Index.getValueInfo(GVSummary.first);
576 #endif
577     if (!Index.isGlobalValueLive(GVSummary.second)) {
578       LLVM_DEBUG(dbgs() << "Ignores Dead GUID: " << VI << "\n");
579       continue;
580     }
581     auto *FuncSummary =
582         dyn_cast<FunctionSummary>(GVSummary.second->getBaseObject());
583     if (!FuncSummary)
584       // Skip import for global variables
585       continue;
586     LLVM_DEBUG(dbgs() << "Initialize import for " << VI << "\n");
587     computeImportForFunction(*FuncSummary, Index, ImportInstrLimit,
588                              DefinedGVSummaries, Worklist, ImportList,
589                              ExportLists, ImportThresholds);
590   }
591 
592   // Process the newly imported functions and add callees to the worklist.
593   while (!Worklist.empty()) {
594     auto GVInfo = Worklist.pop_back_val();
595     auto *Summary = std::get<0>(GVInfo);
596     auto Threshold = std::get<1>(GVInfo);
597 
598     if (auto *FS = dyn_cast<FunctionSummary>(Summary))
599       computeImportForFunction(*FS, Index, Threshold, DefinedGVSummaries,
600                                Worklist, ImportList, ExportLists,
601                                ImportThresholds);
602     else
603       computeImportForReferencedGlobals(*Summary, Index, DefinedGVSummaries,
604                                         Worklist, ImportList, ExportLists);
605   }
606 
607   // Print stats about functions considered but rejected for importing
608   // when requested.
609   if (PrintImportFailures) {
610     dbgs() << "Missed imports into module " << ModName << "\n";
611     for (auto &I : ImportThresholds) {
612       auto &ProcessedThreshold = std::get<0>(I.second);
613       auto &CalleeSummary = std::get<1>(I.second);
614       auto &FailureInfo = std::get<2>(I.second);
615       if (CalleeSummary)
616         continue; // We are going to import.
617       assert(FailureInfo);
618       FunctionSummary *FS = nullptr;
619       if (!FailureInfo->VI.getSummaryList().empty())
620         FS = dyn_cast<FunctionSummary>(
621             FailureInfo->VI.getSummaryList()[0]->getBaseObject());
622       dbgs() << FailureInfo->VI
623              << ": Reason = " << getFailureName(FailureInfo->Reason)
624              << ", Threshold = " << ProcessedThreshold
625              << ", Size = " << (FS ? (int)FS->instCount() : -1)
626              << ", MaxHotness = " << getHotnessName(FailureInfo->MaxHotness)
627              << ", Attempts = " << FailureInfo->Attempts << "\n";
628     }
629   }
630 }
631 
632 #ifndef NDEBUG
isGlobalVarSummary(const ModuleSummaryIndex & Index,ValueInfo VI)633 static bool isGlobalVarSummary(const ModuleSummaryIndex &Index, ValueInfo VI) {
634   auto SL = VI.getSummaryList();
635   return SL.empty()
636              ? false
637              : SL[0]->getSummaryKind() == GlobalValueSummary::GlobalVarKind;
638 }
639 
isGlobalVarSummary(const ModuleSummaryIndex & Index,GlobalValue::GUID G)640 static bool isGlobalVarSummary(const ModuleSummaryIndex &Index,
641                                GlobalValue::GUID G) {
642   if (const auto &VI = Index.getValueInfo(G))
643     return isGlobalVarSummary(Index, VI);
644   return false;
645 }
646 
647 template <class T>
numGlobalVarSummaries(const ModuleSummaryIndex & Index,T & Cont)648 static unsigned numGlobalVarSummaries(const ModuleSummaryIndex &Index,
649                                       T &Cont) {
650   unsigned NumGVS = 0;
651   for (auto &V : Cont)
652     if (isGlobalVarSummary(Index, V))
653       ++NumGVS;
654   return NumGVS;
655 }
656 #endif
657 
658 #ifndef NDEBUG
659 static bool
checkVariableImport(const ModuleSummaryIndex & Index,StringMap<FunctionImporter::ImportMapTy> & ImportLists,StringMap<FunctionImporter::ExportSetTy> & ExportLists)660 checkVariableImport(const ModuleSummaryIndex &Index,
661                     StringMap<FunctionImporter::ImportMapTy> &ImportLists,
662                     StringMap<FunctionImporter::ExportSetTy> &ExportLists) {
663 
664   DenseSet<GlobalValue::GUID> FlattenedImports;
665 
666   for (auto &ImportPerModule : ImportLists)
667     for (auto &ExportPerModule : ImportPerModule.second)
668       FlattenedImports.insert(ExportPerModule.second.begin(),
669                               ExportPerModule.second.end());
670 
671   // Checks that all GUIDs of read/writeonly vars we see in export lists
672   // are also in the import lists. Otherwise we my face linker undefs,
673   // because readonly and writeonly vars are internalized in their
674   // source modules.
675   auto IsReadOrWriteOnlyVar = [&](StringRef ModulePath, const ValueInfo &VI) {
676     auto *GVS = dyn_cast_or_null<GlobalVarSummary>(
677         Index.findSummaryInModule(VI, ModulePath));
678     return GVS && (Index.isReadOnly(GVS) || Index.isWriteOnly(GVS));
679   };
680 
681   for (auto &ExportPerModule : ExportLists)
682     for (auto &VI : ExportPerModule.second)
683       if (!FlattenedImports.count(VI.getGUID()) &&
684           IsReadOrWriteOnlyVar(ExportPerModule.first(), VI))
685         return false;
686 
687   return true;
688 }
689 #endif
690 
691 /// Compute all the import and export for every module using the Index.
ComputeCrossModuleImport(const ModuleSummaryIndex & Index,const StringMap<GVSummaryMapTy> & ModuleToDefinedGVSummaries,StringMap<FunctionImporter::ImportMapTy> & ImportLists,StringMap<FunctionImporter::ExportSetTy> & ExportLists)692 void llvm::ComputeCrossModuleImport(
693     const ModuleSummaryIndex &Index,
694     const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
695     StringMap<FunctionImporter::ImportMapTy> &ImportLists,
696     StringMap<FunctionImporter::ExportSetTy> &ExportLists) {
697   // For each module that has function defined, compute the import/export lists.
698   for (auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) {
699     auto &ImportList = ImportLists[DefinedGVSummaries.first()];
700     LLVM_DEBUG(dbgs() << "Computing import for Module '"
701                       << DefinedGVSummaries.first() << "'\n");
702     ComputeImportForModule(DefinedGVSummaries.second, Index,
703                            DefinedGVSummaries.first(), ImportList,
704                            &ExportLists);
705   }
706 
707   // When computing imports we only added the variables and functions being
708   // imported to the export list. We also need to mark any references and calls
709   // they make as exported as well. We do this here, as it is more efficient
710   // since we may import the same values multiple times into different modules
711   // during the import computation.
712   for (auto &ELI : ExportLists) {
713     FunctionImporter::ExportSetTy NewExports;
714     const auto &DefinedGVSummaries =
715         ModuleToDefinedGVSummaries.lookup(ELI.first());
716     for (auto &EI : ELI.second) {
717       // Find the copy defined in the exporting module so that we can mark the
718       // values it references in that specific definition as exported.
719       // Below we will add all references and called values, without regard to
720       // whether they are also defined in this module. We subsequently prune the
721       // list to only include those defined in the exporting module, see comment
722       // there as to why.
723       auto DS = DefinedGVSummaries.find(EI.getGUID());
724       // Anything marked exported during the import computation must have been
725       // defined in the exporting module.
726       assert(DS != DefinedGVSummaries.end());
727       auto *S = DS->getSecond();
728       S = S->getBaseObject();
729       if (auto *GVS = dyn_cast<GlobalVarSummary>(S)) {
730         // Export referenced functions and variables. We don't export/promote
731         // objects referenced by writeonly variable initializer, because
732         // we convert such variables initializers to "zeroinitializer".
733         // See processGlobalForThinLTO.
734         if (!Index.isWriteOnly(GVS))
735           for (const auto &VI : GVS->refs())
736             NewExports.insert(VI);
737       } else {
738         auto *FS = cast<FunctionSummary>(S);
739         for (auto &Edge : FS->calls())
740           NewExports.insert(Edge.first);
741         for (auto &Ref : FS->refs())
742           NewExports.insert(Ref);
743       }
744     }
745     // Prune list computed above to only include values defined in the exporting
746     // module. We do this after the above insertion since we may hit the same
747     // ref/call target multiple times in above loop, and it is more efficient to
748     // avoid a set lookup each time.
749     for (auto EI = NewExports.begin(); EI != NewExports.end();) {
750       if (!DefinedGVSummaries.count(EI->getGUID()))
751         NewExports.erase(EI++);
752       else
753         ++EI;
754     }
755     ELI.second.insert(NewExports.begin(), NewExports.end());
756   }
757 
758   assert(checkVariableImport(Index, ImportLists, ExportLists));
759 #ifndef NDEBUG
760   LLVM_DEBUG(dbgs() << "Import/Export lists for " << ImportLists.size()
761                     << " modules:\n");
762   for (auto &ModuleImports : ImportLists) {
763     auto ModName = ModuleImports.first();
764     auto &Exports = ExportLists[ModName];
765     unsigned NumGVS = numGlobalVarSummaries(Index, Exports);
766     LLVM_DEBUG(dbgs() << "* Module " << ModName << " exports "
767                       << Exports.size() - NumGVS << " functions and " << NumGVS
768                       << " vars. Imports from " << ModuleImports.second.size()
769                       << " modules.\n");
770     for (auto &Src : ModuleImports.second) {
771       auto SrcModName = Src.first();
772       unsigned NumGVSPerMod = numGlobalVarSummaries(Index, Src.second);
773       LLVM_DEBUG(dbgs() << " - " << Src.second.size() - NumGVSPerMod
774                         << " functions imported from " << SrcModName << "\n");
775       LLVM_DEBUG(dbgs() << " - " << NumGVSPerMod
776                         << " global vars imported from " << SrcModName << "\n");
777     }
778   }
779 #endif
780 }
781 
782 #ifndef NDEBUG
dumpImportListForModule(const ModuleSummaryIndex & Index,StringRef ModulePath,FunctionImporter::ImportMapTy & ImportList)783 static void dumpImportListForModule(const ModuleSummaryIndex &Index,
784                                     StringRef ModulePath,
785                                     FunctionImporter::ImportMapTy &ImportList) {
786   LLVM_DEBUG(dbgs() << "* Module " << ModulePath << " imports from "
787                     << ImportList.size() << " modules.\n");
788   for (auto &Src : ImportList) {
789     auto SrcModName = Src.first();
790     unsigned NumGVSPerMod = numGlobalVarSummaries(Index, Src.second);
791     LLVM_DEBUG(dbgs() << " - " << Src.second.size() - NumGVSPerMod
792                       << " functions imported from " << SrcModName << "\n");
793     LLVM_DEBUG(dbgs() << " - " << NumGVSPerMod << " vars imported from "
794                       << SrcModName << "\n");
795   }
796 }
797 #endif
798 
799 /// Compute all the imports for the given module in the Index.
ComputeCrossModuleImportForModule(StringRef ModulePath,const ModuleSummaryIndex & Index,FunctionImporter::ImportMapTy & ImportList)800 void llvm::ComputeCrossModuleImportForModule(
801     StringRef ModulePath, const ModuleSummaryIndex &Index,
802     FunctionImporter::ImportMapTy &ImportList) {
803   // Collect the list of functions this module defines.
804   // GUID -> Summary
805   GVSummaryMapTy FunctionSummaryMap;
806   Index.collectDefinedFunctionsForModule(ModulePath, FunctionSummaryMap);
807 
808   // Compute the import list for this module.
809   LLVM_DEBUG(dbgs() << "Computing import for Module '" << ModulePath << "'\n");
810   ComputeImportForModule(FunctionSummaryMap, Index, ModulePath, ImportList);
811 
812 #ifndef NDEBUG
813   dumpImportListForModule(Index, ModulePath, ImportList);
814 #endif
815 }
816 
817 // Mark all external summaries in Index for import into the given module.
818 // Used for distributed builds using a distributed index.
ComputeCrossModuleImportForModuleFromIndex(StringRef ModulePath,const ModuleSummaryIndex & Index,FunctionImporter::ImportMapTy & ImportList)819 void llvm::ComputeCrossModuleImportForModuleFromIndex(
820     StringRef ModulePath, const ModuleSummaryIndex &Index,
821     FunctionImporter::ImportMapTy &ImportList) {
822   for (auto &GlobalList : Index) {
823     // Ignore entries for undefined references.
824     if (GlobalList.second.SummaryList.empty())
825       continue;
826 
827     auto GUID = GlobalList.first;
828     assert(GlobalList.second.SummaryList.size() == 1 &&
829            "Expected individual combined index to have one summary per GUID");
830     auto &Summary = GlobalList.second.SummaryList[0];
831     // Skip the summaries for the importing module. These are included to
832     // e.g. record required linkage changes.
833     if (Summary->modulePath() == ModulePath)
834       continue;
835     // Add an entry to provoke importing by thinBackend.
836     ImportList[Summary->modulePath()].insert(GUID);
837   }
838 #ifndef NDEBUG
839   dumpImportListForModule(Index, ModulePath, ImportList);
840 #endif
841 }
842 
computeDeadSymbols(ModuleSummaryIndex & Index,const DenseSet<GlobalValue::GUID> & GUIDPreservedSymbols,function_ref<PrevailingType (GlobalValue::GUID)> isPrevailing)843 void llvm::computeDeadSymbols(
844     ModuleSummaryIndex &Index,
845     const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
846     function_ref<PrevailingType(GlobalValue::GUID)> isPrevailing) {
847   assert(!Index.withGlobalValueDeadStripping());
848   if (!ComputeDead)
849     return;
850   if (GUIDPreservedSymbols.empty())
851     // Don't do anything when nothing is live, this is friendly with tests.
852     return;
853   unsigned LiveSymbols = 0;
854   SmallVector<ValueInfo, 128> Worklist;
855   Worklist.reserve(GUIDPreservedSymbols.size() * 2);
856   for (auto GUID : GUIDPreservedSymbols) {
857     ValueInfo VI = Index.getValueInfo(GUID);
858     if (!VI)
859       continue;
860     for (auto &S : VI.getSummaryList())
861       S->setLive(true);
862   }
863 
864   // Add values flagged in the index as live roots to the worklist.
865   for (const auto &Entry : Index) {
866     auto VI = Index.getValueInfo(Entry);
867     for (auto &S : Entry.second.SummaryList)
868       if (S->isLive()) {
869         LLVM_DEBUG(dbgs() << "Live root: " << VI << "\n");
870         Worklist.push_back(VI);
871         ++LiveSymbols;
872         break;
873       }
874   }
875 
876   // Make value live and add it to the worklist if it was not live before.
877   auto visit = [&](ValueInfo VI, bool IsAliasee) {
878     // FIXME: If we knew which edges were created for indirect call profiles,
879     // we could skip them here. Any that are live should be reached via
880     // other edges, e.g. reference edges. Otherwise, using a profile collected
881     // on a slightly different binary might provoke preserving, importing
882     // and ultimately promoting calls to functions not linked into this
883     // binary, which increases the binary size unnecessarily. Note that
884     // if this code changes, the importer needs to change so that edges
885     // to functions marked dead are skipped.
886     VI = updateValueInfoForIndirectCalls(Index, VI);
887     if (!VI)
888       return;
889 
890     if (llvm::any_of(VI.getSummaryList(),
891                      [](const std::unique_ptr<llvm::GlobalValueSummary> &S) {
892                        return S->isLive();
893                      }))
894       return;
895 
896     // We only keep live symbols that are known to be non-prevailing if any are
897     // available_externally, linkonceodr, weakodr. Those symbols are discarded
898     // later in the EliminateAvailableExternally pass and setting them to
899     // not-live could break downstreams users of liveness information (PR36483)
900     // or limit optimization opportunities.
901     if (isPrevailing(VI.getGUID()) == PrevailingType::No) {
902       bool KeepAliveLinkage = false;
903       bool Interposable = false;
904       for (auto &S : VI.getSummaryList()) {
905         if (S->linkage() == GlobalValue::AvailableExternallyLinkage ||
906             S->linkage() == GlobalValue::WeakODRLinkage ||
907             S->linkage() == GlobalValue::LinkOnceODRLinkage)
908           KeepAliveLinkage = true;
909         else if (GlobalValue::isInterposableLinkage(S->linkage()))
910           Interposable = true;
911       }
912 
913       if (!IsAliasee) {
914         if (!KeepAliveLinkage)
915           return;
916 
917         if (Interposable)
918           report_fatal_error(
919               "Interposable and available_externally/linkonce_odr/weak_odr "
920               "symbol");
921       }
922     }
923 
924     for (auto &S : VI.getSummaryList())
925       S->setLive(true);
926     ++LiveSymbols;
927     Worklist.push_back(VI);
928   };
929 
930   while (!Worklist.empty()) {
931     auto VI = Worklist.pop_back_val();
932     for (auto &Summary : VI.getSummaryList()) {
933       if (auto *AS = dyn_cast<AliasSummary>(Summary.get())) {
934         // If this is an alias, visit the aliasee VI to ensure that all copies
935         // are marked live and it is added to the worklist for further
936         // processing of its references.
937         visit(AS->getAliaseeVI(), true);
938         continue;
939       }
940       for (auto Ref : Summary->refs())
941         visit(Ref, false);
942       if (auto *FS = dyn_cast<FunctionSummary>(Summary.get()))
943         for (auto Call : FS->calls())
944           visit(Call.first, false);
945     }
946   }
947   Index.setWithGlobalValueDeadStripping();
948 
949   unsigned DeadSymbols = Index.size() - LiveSymbols;
950   LLVM_DEBUG(dbgs() << LiveSymbols << " symbols Live, and " << DeadSymbols
951                     << " symbols Dead \n");
952   NumDeadSymbols += DeadSymbols;
953   NumLiveSymbols += LiveSymbols;
954 }
955 
956 // Compute dead symbols and propagate constants in combined index.
computeDeadSymbolsWithConstProp(ModuleSummaryIndex & Index,const DenseSet<GlobalValue::GUID> & GUIDPreservedSymbols,function_ref<PrevailingType (GlobalValue::GUID)> isPrevailing,bool ImportEnabled)957 void llvm::computeDeadSymbolsWithConstProp(
958     ModuleSummaryIndex &Index,
959     const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
960     function_ref<PrevailingType(GlobalValue::GUID)> isPrevailing,
961     bool ImportEnabled) {
962   computeDeadSymbols(Index, GUIDPreservedSymbols, isPrevailing);
963   if (ImportEnabled)
964     Index.propagateAttributes(GUIDPreservedSymbols);
965 }
966 
967 /// Compute the set of summaries needed for a ThinLTO backend compilation of
968 /// \p ModulePath.
gatherImportedSummariesForModule(StringRef ModulePath,const StringMap<GVSummaryMapTy> & ModuleToDefinedGVSummaries,const FunctionImporter::ImportMapTy & ImportList,std::map<std::string,GVSummaryMapTy> & ModuleToSummariesForIndex)969 void llvm::gatherImportedSummariesForModule(
970     StringRef ModulePath,
971     const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
972     const FunctionImporter::ImportMapTy &ImportList,
973     std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
974   // Include all summaries from the importing module.
975   ModuleToSummariesForIndex[std::string(ModulePath)] =
976       ModuleToDefinedGVSummaries.lookup(ModulePath);
977   // Include summaries for imports.
978   for (auto &ILI : ImportList) {
979     auto &SummariesForIndex =
980         ModuleToSummariesForIndex[std::string(ILI.first())];
981     const auto &DefinedGVSummaries =
982         ModuleToDefinedGVSummaries.lookup(ILI.first());
983     for (auto &GI : ILI.second) {
984       const auto &DS = DefinedGVSummaries.find(GI);
985       assert(DS != DefinedGVSummaries.end() &&
986              "Expected a defined summary for imported global value");
987       SummariesForIndex[GI] = DS->second;
988     }
989   }
990 }
991 
992 /// Emit the files \p ModulePath will import from into \p OutputFilename.
EmitImportsFiles(StringRef ModulePath,StringRef OutputFilename,const std::map<std::string,GVSummaryMapTy> & ModuleToSummariesForIndex)993 std::error_code llvm::EmitImportsFiles(
994     StringRef ModulePath, StringRef OutputFilename,
995     const std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
996   std::error_code EC;
997   raw_fd_ostream ImportsOS(OutputFilename, EC, sys::fs::OpenFlags::OF_None);
998   if (EC)
999     return EC;
1000   for (auto &ILI : ModuleToSummariesForIndex)
1001     // The ModuleToSummariesForIndex map includes an entry for the current
1002     // Module (needed for writing out the index files). We don't want to
1003     // include it in the imports file, however, so filter it out.
1004     if (ILI.first != ModulePath)
1005       ImportsOS << ILI.first << "\n";
1006   return std::error_code();
1007 }
1008 
convertToDeclaration(GlobalValue & GV)1009 bool llvm::convertToDeclaration(GlobalValue &GV) {
1010   LLVM_DEBUG(dbgs() << "Converting to a declaration: `" << GV.getName()
1011                     << "\n");
1012   if (Function *F = dyn_cast<Function>(&GV)) {
1013     F->deleteBody();
1014     F->clearMetadata();
1015     F->setComdat(nullptr);
1016   } else if (GlobalVariable *V = dyn_cast<GlobalVariable>(&GV)) {
1017     V->setInitializer(nullptr);
1018     V->setLinkage(GlobalValue::ExternalLinkage);
1019     V->clearMetadata();
1020     V->setComdat(nullptr);
1021   } else {
1022     GlobalValue *NewGV;
1023     if (GV.getValueType()->isFunctionTy())
1024       NewGV =
1025           Function::Create(cast<FunctionType>(GV.getValueType()),
1026                            GlobalValue::ExternalLinkage, GV.getAddressSpace(),
1027                            "", GV.getParent());
1028     else
1029       NewGV =
1030           new GlobalVariable(*GV.getParent(), GV.getValueType(),
1031                              /*isConstant*/ false, GlobalValue::ExternalLinkage,
1032                              /*init*/ nullptr, "",
1033                              /*insertbefore*/ nullptr, GV.getThreadLocalMode(),
1034                              GV.getType()->getAddressSpace());
1035     NewGV->takeName(&GV);
1036     GV.replaceAllUsesWith(NewGV);
1037     return false;
1038   }
1039   if (!GV.isImplicitDSOLocal())
1040     GV.setDSOLocal(false);
1041   return true;
1042 }
1043 
thinLTOResolvePrevailingInModule(Module & TheModule,const GVSummaryMapTy & DefinedGlobals)1044 void llvm::thinLTOResolvePrevailingInModule(
1045     Module &TheModule, const GVSummaryMapTy &DefinedGlobals) {
1046   auto updateLinkage = [&](GlobalValue &GV) {
1047     // See if the global summary analysis computed a new resolved linkage.
1048     const auto &GS = DefinedGlobals.find(GV.getGUID());
1049     if (GS == DefinedGlobals.end())
1050       return;
1051     auto NewLinkage = GS->second->linkage();
1052     if (GlobalValue::isLocalLinkage(GV.getLinkage()) ||
1053         // Don't internalize anything here, because the code below
1054         // lacks necessary correctness checks. Leave this job to
1055         // LLVM 'internalize' pass.
1056         GlobalValue::isLocalLinkage(NewLinkage) ||
1057         // In case it was dead and already converted to declaration.
1058         GV.isDeclaration())
1059       return;
1060 
1061     // Set the potentially more constraining visibility computed from summaries.
1062     // The DefaultVisibility condition is because older GlobalValueSummary does
1063     // not record DefaultVisibility and we don't want to change protected/hidden
1064     // to default.
1065     if (GS->second->getVisibility() != GlobalValue::DefaultVisibility)
1066       GV.setVisibility(GS->second->getVisibility());
1067 
1068     if (NewLinkage == GV.getLinkage())
1069       return;
1070 
1071     // Check for a non-prevailing def that has interposable linkage
1072     // (e.g. non-odr weak or linkonce). In that case we can't simply
1073     // convert to available_externally, since it would lose the
1074     // interposable property and possibly get inlined. Simply drop
1075     // the definition in that case.
1076     if (GlobalValue::isAvailableExternallyLinkage(NewLinkage) &&
1077         GlobalValue::isInterposableLinkage(GV.getLinkage())) {
1078       if (!convertToDeclaration(GV))
1079         // FIXME: Change this to collect replaced GVs and later erase
1080         // them from the parent module once thinLTOResolvePrevailingGUID is
1081         // changed to enable this for aliases.
1082         llvm_unreachable("Expected GV to be converted");
1083     } else {
1084       // If all copies of the original symbol had global unnamed addr and
1085       // linkonce_odr linkage, it should be an auto hide symbol. In that case
1086       // the thin link would have marked it as CanAutoHide. Add hidden visibility
1087       // to the symbol to preserve the property.
1088       if (NewLinkage == GlobalValue::WeakODRLinkage &&
1089           GS->second->canAutoHide()) {
1090         assert(GV.hasLinkOnceODRLinkage() && GV.hasGlobalUnnamedAddr());
1091         GV.setVisibility(GlobalValue::HiddenVisibility);
1092       }
1093 
1094       LLVM_DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName()
1095                         << "` from " << GV.getLinkage() << " to " << NewLinkage
1096                         << "\n");
1097       GV.setLinkage(NewLinkage);
1098     }
1099     // Remove declarations from comdats, including available_externally
1100     // as this is a declaration for the linker, and will be dropped eventually.
1101     // It is illegal for comdats to contain declarations.
1102     auto *GO = dyn_cast_or_null<GlobalObject>(&GV);
1103     if (GO && GO->isDeclarationForLinker() && GO->hasComdat())
1104       GO->setComdat(nullptr);
1105   };
1106 
1107   // Process functions and global now
1108   for (auto &GV : TheModule)
1109     updateLinkage(GV);
1110   for (auto &GV : TheModule.globals())
1111     updateLinkage(GV);
1112   for (auto &GV : TheModule.aliases())
1113     updateLinkage(GV);
1114 }
1115 
1116 /// Run internalization on \p TheModule based on symmary analysis.
thinLTOInternalizeModule(Module & TheModule,const GVSummaryMapTy & DefinedGlobals)1117 void llvm::thinLTOInternalizeModule(Module &TheModule,
1118                                     const GVSummaryMapTy &DefinedGlobals) {
1119   // Declare a callback for the internalize pass that will ask for every
1120   // candidate GlobalValue if it can be internalized or not.
1121   auto MustPreserveGV = [&](const GlobalValue &GV) -> bool {
1122     // Lookup the linkage recorded in the summaries during global analysis.
1123     auto GS = DefinedGlobals.find(GV.getGUID());
1124     if (GS == DefinedGlobals.end()) {
1125       // Must have been promoted (possibly conservatively). Find original
1126       // name so that we can access the correct summary and see if it can
1127       // be internalized again.
1128       // FIXME: Eventually we should control promotion instead of promoting
1129       // and internalizing again.
1130       StringRef OrigName =
1131           ModuleSummaryIndex::getOriginalNameBeforePromote(GV.getName());
1132       std::string OrigId = GlobalValue::getGlobalIdentifier(
1133           OrigName, GlobalValue::InternalLinkage,
1134           TheModule.getSourceFileName());
1135       GS = DefinedGlobals.find(GlobalValue::getGUID(OrigId));
1136       if (GS == DefinedGlobals.end()) {
1137         // Also check the original non-promoted non-globalized name. In some
1138         // cases a preempted weak value is linked in as a local copy because
1139         // it is referenced by an alias (IRLinker::linkGlobalValueProto).
1140         // In that case, since it was originally not a local value, it was
1141         // recorded in the index using the original name.
1142         // FIXME: This may not be needed once PR27866 is fixed.
1143         GS = DefinedGlobals.find(GlobalValue::getGUID(OrigName));
1144         assert(GS != DefinedGlobals.end());
1145       }
1146     }
1147     return !GlobalValue::isLocalLinkage(GS->second->linkage());
1148   };
1149 
1150   // FIXME: See if we can just internalize directly here via linkage changes
1151   // based on the index, rather than invoking internalizeModule.
1152   internalizeModule(TheModule, MustPreserveGV);
1153 }
1154 
1155 /// Make alias a clone of its aliasee.
replaceAliasWithAliasee(Module * SrcModule,GlobalAlias * GA)1156 static Function *replaceAliasWithAliasee(Module *SrcModule, GlobalAlias *GA) {
1157   Function *Fn = cast<Function>(GA->getBaseObject());
1158 
1159   ValueToValueMapTy VMap;
1160   Function *NewFn = CloneFunction(Fn, VMap);
1161   // Clone should use the original alias's linkage, visibility and name, and we
1162   // ensure all uses of alias instead use the new clone (casted if necessary).
1163   NewFn->setLinkage(GA->getLinkage());
1164   NewFn->setVisibility(GA->getVisibility());
1165   GA->replaceAllUsesWith(ConstantExpr::getBitCast(NewFn, GA->getType()));
1166   NewFn->takeName(GA);
1167   return NewFn;
1168 }
1169 
1170 // Internalize values that we marked with specific attribute
1171 // in processGlobalForThinLTO.
internalizeGVsAfterImport(Module & M)1172 static void internalizeGVsAfterImport(Module &M) {
1173   for (auto &GV : M.globals())
1174     // Skip GVs which have been converted to declarations
1175     // by dropDeadSymbols.
1176     if (!GV.isDeclaration() && GV.hasAttribute("thinlto-internalize")) {
1177       GV.setLinkage(GlobalValue::InternalLinkage);
1178       GV.setVisibility(GlobalValue::DefaultVisibility);
1179     }
1180 }
1181 
1182 // Automatically import functions in Module \p DestModule based on the summaries
1183 // index.
importFunctions(Module & DestModule,const FunctionImporter::ImportMapTy & ImportList)1184 Expected<bool> FunctionImporter::importFunctions(
1185     Module &DestModule, const FunctionImporter::ImportMapTy &ImportList) {
1186   LLVM_DEBUG(dbgs() << "Starting import for Module "
1187                     << DestModule.getModuleIdentifier() << "\n");
1188   unsigned ImportedCount = 0, ImportedGVCount = 0;
1189 
1190   IRMover Mover(DestModule);
1191   // Do the actual import of functions now, one Module at a time
1192   std::set<StringRef> ModuleNameOrderedList;
1193   for (auto &FunctionsToImportPerModule : ImportList) {
1194     ModuleNameOrderedList.insert(FunctionsToImportPerModule.first());
1195   }
1196   for (auto &Name : ModuleNameOrderedList) {
1197     // Get the module for the import
1198     const auto &FunctionsToImportPerModule = ImportList.find(Name);
1199     assert(FunctionsToImportPerModule != ImportList.end());
1200     Expected<std::unique_ptr<Module>> SrcModuleOrErr = ModuleLoader(Name);
1201     if (!SrcModuleOrErr)
1202       return SrcModuleOrErr.takeError();
1203     std::unique_ptr<Module> SrcModule = std::move(*SrcModuleOrErr);
1204     assert(&DestModule.getContext() == &SrcModule->getContext() &&
1205            "Context mismatch");
1206 
1207     // If modules were created with lazy metadata loading, materialize it
1208     // now, before linking it (otherwise this will be a noop).
1209     if (Error Err = SrcModule->materializeMetadata())
1210       return std::move(Err);
1211 
1212     auto &ImportGUIDs = FunctionsToImportPerModule->second;
1213     // Find the globals to import
1214     SetVector<GlobalValue *> GlobalsToImport;
1215     for (Function &F : *SrcModule) {
1216       if (!F.hasName())
1217         continue;
1218       auto GUID = F.getGUID();
1219       auto Import = ImportGUIDs.count(GUID);
1220       LLVM_DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing function "
1221                         << GUID << " " << F.getName() << " from "
1222                         << SrcModule->getSourceFileName() << "\n");
1223       if (Import) {
1224         if (Error Err = F.materialize())
1225           return std::move(Err);
1226         if (EnableImportMetadata) {
1227           // Add 'thinlto_src_module' metadata for statistics and debugging.
1228           F.setMetadata(
1229               "thinlto_src_module",
1230               MDNode::get(DestModule.getContext(),
1231                           {MDString::get(DestModule.getContext(),
1232                                          SrcModule->getSourceFileName())}));
1233         }
1234         GlobalsToImport.insert(&F);
1235       }
1236     }
1237     for (GlobalVariable &GV : SrcModule->globals()) {
1238       if (!GV.hasName())
1239         continue;
1240       auto GUID = GV.getGUID();
1241       auto Import = ImportGUIDs.count(GUID);
1242       LLVM_DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing global "
1243                         << GUID << " " << GV.getName() << " from "
1244                         << SrcModule->getSourceFileName() << "\n");
1245       if (Import) {
1246         if (Error Err = GV.materialize())
1247           return std::move(Err);
1248         ImportedGVCount += GlobalsToImport.insert(&GV);
1249       }
1250     }
1251     for (GlobalAlias &GA : SrcModule->aliases()) {
1252       if (!GA.hasName())
1253         continue;
1254       auto GUID = GA.getGUID();
1255       auto Import = ImportGUIDs.count(GUID);
1256       LLVM_DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing alias "
1257                         << GUID << " " << GA.getName() << " from "
1258                         << SrcModule->getSourceFileName() << "\n");
1259       if (Import) {
1260         if (Error Err = GA.materialize())
1261           return std::move(Err);
1262         // Import alias as a copy of its aliasee.
1263         GlobalObject *Base = GA.getBaseObject();
1264         if (Error Err = Base->materialize())
1265           return std::move(Err);
1266         auto *Fn = replaceAliasWithAliasee(SrcModule.get(), &GA);
1267         LLVM_DEBUG(dbgs() << "Is importing aliasee fn " << Base->getGUID()
1268                           << " " << Base->getName() << " from "
1269                           << SrcModule->getSourceFileName() << "\n");
1270         if (EnableImportMetadata) {
1271           // Add 'thinlto_src_module' metadata for statistics and debugging.
1272           Fn->setMetadata(
1273               "thinlto_src_module",
1274               MDNode::get(DestModule.getContext(),
1275                           {MDString::get(DestModule.getContext(),
1276                                          SrcModule->getSourceFileName())}));
1277         }
1278         GlobalsToImport.insert(Fn);
1279       }
1280     }
1281 
1282     // Upgrade debug info after we're done materializing all the globals and we
1283     // have loaded all the required metadata!
1284     UpgradeDebugInfo(*SrcModule);
1285 
1286     // Set the partial sample profile ratio in the profile summary module flag
1287     // of the imported source module, if applicable, so that the profile summary
1288     // module flag will match with that of the destination module when it's
1289     // imported.
1290     SrcModule->setPartialSampleProfileRatio(Index);
1291 
1292     // Link in the specified functions.
1293     if (renameModuleForThinLTO(*SrcModule, Index, ClearDSOLocalOnDeclarations,
1294                                &GlobalsToImport))
1295       return true;
1296 
1297     if (PrintImports) {
1298       for (const auto *GV : GlobalsToImport)
1299         dbgs() << DestModule.getSourceFileName() << ": Import " << GV->getName()
1300                << " from " << SrcModule->getSourceFileName() << "\n";
1301     }
1302 
1303     if (Error Err = Mover.move(
1304             std::move(SrcModule), GlobalsToImport.getArrayRef(),
1305             [](GlobalValue &, IRMover::ValueAdder) {},
1306             /*IsPerformingImport=*/true))
1307       report_fatal_error("Function Import: link error: " +
1308                          toString(std::move(Err)));
1309 
1310     ImportedCount += GlobalsToImport.size();
1311     NumImportedModules++;
1312   }
1313 
1314   internalizeGVsAfterImport(DestModule);
1315 
1316   NumImportedFunctions += (ImportedCount - ImportedGVCount);
1317   NumImportedGlobalVars += ImportedGVCount;
1318 
1319   LLVM_DEBUG(dbgs() << "Imported " << ImportedCount - ImportedGVCount
1320                     << " functions for Module "
1321                     << DestModule.getModuleIdentifier() << "\n");
1322   LLVM_DEBUG(dbgs() << "Imported " << ImportedGVCount
1323                     << " global variables for Module "
1324                     << DestModule.getModuleIdentifier() << "\n");
1325   return ImportedCount;
1326 }
1327 
doImportingForModule(Module & M)1328 static bool doImportingForModule(Module &M) {
1329   if (SummaryFile.empty())
1330     report_fatal_error("error: -function-import requires -summary-file\n");
1331   Expected<std::unique_ptr<ModuleSummaryIndex>> IndexPtrOrErr =
1332       getModuleSummaryIndexForFile(SummaryFile);
1333   if (!IndexPtrOrErr) {
1334     logAllUnhandledErrors(IndexPtrOrErr.takeError(), errs(),
1335                           "Error loading file '" + SummaryFile + "': ");
1336     return false;
1337   }
1338   std::unique_ptr<ModuleSummaryIndex> Index = std::move(*IndexPtrOrErr);
1339 
1340   // First step is collecting the import list.
1341   FunctionImporter::ImportMapTy ImportList;
1342   // If requested, simply import all functions in the index. This is used
1343   // when testing distributed backend handling via the opt tool, when
1344   // we have distributed indexes containing exactly the summaries to import.
1345   if (ImportAllIndex)
1346     ComputeCrossModuleImportForModuleFromIndex(M.getModuleIdentifier(), *Index,
1347                                                ImportList);
1348   else
1349     ComputeCrossModuleImportForModule(M.getModuleIdentifier(), *Index,
1350                                       ImportList);
1351 
1352   // Conservatively mark all internal values as promoted. This interface is
1353   // only used when doing importing via the function importing pass. The pass
1354   // is only enabled when testing importing via the 'opt' tool, which does
1355   // not do the ThinLink that would normally determine what values to promote.
1356   for (auto &I : *Index) {
1357     for (auto &S : I.second.SummaryList) {
1358       if (GlobalValue::isLocalLinkage(S->linkage()))
1359         S->setLinkage(GlobalValue::ExternalLinkage);
1360     }
1361   }
1362 
1363   // Next we need to promote to global scope and rename any local values that
1364   // are potentially exported to other modules.
1365   if (renameModuleForThinLTO(M, *Index, /*ClearDSOLocalOnDeclarations=*/false,
1366                              /*GlobalsToImport=*/nullptr)) {
1367     errs() << "Error renaming module\n";
1368     return false;
1369   }
1370 
1371   // Perform the import now.
1372   auto ModuleLoader = [&M](StringRef Identifier) {
1373     return loadFile(std::string(Identifier), M.getContext());
1374   };
1375   FunctionImporter Importer(*Index, ModuleLoader,
1376                             /*ClearDSOLocalOnDeclarations=*/false);
1377   Expected<bool> Result = Importer.importFunctions(M, ImportList);
1378 
1379   // FIXME: Probably need to propagate Errors through the pass manager.
1380   if (!Result) {
1381     logAllUnhandledErrors(Result.takeError(), errs(),
1382                           "Error importing module: ");
1383     return false;
1384   }
1385 
1386   return *Result;
1387 }
1388 
1389 namespace {
1390 
1391 /// Pass that performs cross-module function import provided a summary file.
1392 class FunctionImportLegacyPass : public ModulePass {
1393 public:
1394   /// Pass identification, replacement for typeid
1395   static char ID;
1396 
FunctionImportLegacyPass()1397   explicit FunctionImportLegacyPass() : ModulePass(ID) {}
1398 
1399   /// Specify pass name for debug output
getPassName() const1400   StringRef getPassName() const override { return "Function Importing"; }
1401 
runOnModule(Module & M)1402   bool runOnModule(Module &M) override {
1403     if (skipModule(M))
1404       return false;
1405 
1406     return doImportingForModule(M);
1407   }
1408 };
1409 
1410 } // end anonymous namespace
1411 
run(Module & M,ModuleAnalysisManager & AM)1412 PreservedAnalyses FunctionImportPass::run(Module &M,
1413                                           ModuleAnalysisManager &AM) {
1414   if (!doImportingForModule(M))
1415     return PreservedAnalyses::all();
1416 
1417   return PreservedAnalyses::none();
1418 }
1419 
1420 char FunctionImportLegacyPass::ID = 0;
1421 INITIALIZE_PASS(FunctionImportLegacyPass, "function-import",
1422                 "Summary Based Function Import", false, false)
1423 
1424 namespace llvm {
1425 
createFunctionImportPass()1426 Pass *createFunctionImportPass() {
1427   return new FunctionImportLegacyPass();
1428 }
1429 
1430 } // end namespace llvm
1431