142418abaSMehdi Amini //===- FunctionImport.cpp - ThinLTO Summary-based Function Import ---------===//
242418abaSMehdi Amini //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
642418abaSMehdi Amini //
742418abaSMehdi Amini //===----------------------------------------------------------------------===//
842418abaSMehdi Amini //
942418abaSMehdi Amini // This file implements Function import based on summaries.
1042418abaSMehdi Amini //
1142418abaSMehdi Amini //===----------------------------------------------------------------------===//
1242418abaSMehdi Amini 
1342418abaSMehdi Amini #include "llvm/Transforms/IPO/FunctionImport.h"
14e9ea08a0SEugene Zelenko #include "llvm/ADT/ArrayRef.h"
15e9ea08a0SEugene Zelenko #include "llvm/ADT/STLExtras.h"
16e9ea08a0SEugene Zelenko #include "llvm/ADT/SetVector.h"
1701e32130SMehdi Amini #include "llvm/ADT/SmallVector.h"
18d29478f7STeresa Johnson #include "llvm/ADT/Statistic.h"
19e9ea08a0SEugene Zelenko #include "llvm/ADT/StringMap.h"
200efed325SSimon Pilgrim #include "llvm/ADT/StringRef.h"
21b040fcc6SCharles Saternos #include "llvm/ADT/StringSet.h"
22c15d60b7SPeter Collingbourne #include "llvm/Bitcode/BitcodeReader.h"
2342418abaSMehdi Amini #include "llvm/IR/AutoUpgrade.h"
2481bbf742STeresa Johnson #include "llvm/IR/Constants.h"
25e9ea08a0SEugene Zelenko #include "llvm/IR/Function.h"
26e9ea08a0SEugene Zelenko #include "llvm/IR/GlobalAlias.h"
27e9ea08a0SEugene Zelenko #include "llvm/IR/GlobalObject.h"
28e9ea08a0SEugene Zelenko #include "llvm/IR/GlobalValue.h"
29e9ea08a0SEugene Zelenko #include "llvm/IR/GlobalVariable.h"
30e9ea08a0SEugene Zelenko #include "llvm/IR/Metadata.h"
3142418abaSMehdi Amini #include "llvm/IR/Module.h"
32e9ea08a0SEugene Zelenko #include "llvm/IR/ModuleSummaryIndex.h"
3342418abaSMehdi Amini #include "llvm/IRReader/IRReader.h"
3405da2fe5SReid Kleckner #include "llvm/InitializePasses.h"
35e9ea08a0SEugene Zelenko #include "llvm/Linker/IRMover.h"
36e9ea08a0SEugene Zelenko #include "llvm/Object/ModuleSymbolTable.h"
37e9ea08a0SEugene Zelenko #include "llvm/Object/SymbolicFile.h"
38e9ea08a0SEugene Zelenko #include "llvm/Pass.h"
39e9ea08a0SEugene Zelenko #include "llvm/Support/Casting.h"
4042418abaSMehdi Amini #include "llvm/Support/CommandLine.h"
4142418abaSMehdi Amini #include "llvm/Support/Debug.h"
42e9ea08a0SEugene Zelenko #include "llvm/Support/Error.h"
43e9ea08a0SEugene Zelenko #include "llvm/Support/ErrorHandling.h"
44e9ea08a0SEugene Zelenko #include "llvm/Support/FileSystem.h"
4542418abaSMehdi Amini #include "llvm/Support/SourceMgr.h"
46e9ea08a0SEugene Zelenko #include "llvm/Support/raw_ostream.h"
4704c9a2d6STeresa Johnson #include "llvm/Transforms/IPO/Internalize.h"
4881bbf742STeresa Johnson #include "llvm/Transforms/Utils/Cloning.h"
49488a800aSTeresa Johnson #include "llvm/Transforms/Utils/FunctionImportUtils.h"
5081bbf742STeresa Johnson #include "llvm/Transforms/Utils/ValueMapper.h"
51e9ea08a0SEugene Zelenko #include <cassert>
52e9ea08a0SEugene Zelenko #include <memory>
53e9ea08a0SEugene Zelenko #include <set>
54e9ea08a0SEugene Zelenko #include <string>
55e9ea08a0SEugene Zelenko #include <system_error>
56e9ea08a0SEugene Zelenko #include <tuple>
57e9ea08a0SEugene Zelenko #include <utility>
587e88d0daSMehdi Amini 
5942418abaSMehdi Amini using namespace llvm;
6042418abaSMehdi Amini 
61e9ea08a0SEugene Zelenko #define DEBUG_TYPE "function-import"
62e9ea08a0SEugene Zelenko 
63d2c234a4STeresa Johnson STATISTIC(NumImportedFunctionsThinLink,
64d2c234a4STeresa Johnson           "Number of functions thin link decided to import");
65d2c234a4STeresa Johnson STATISTIC(NumImportedHotFunctionsThinLink,
66d2c234a4STeresa Johnson           "Number of hot functions thin link decided to import");
67d2c234a4STeresa Johnson STATISTIC(NumImportedCriticalFunctionsThinLink,
68d2c234a4STeresa Johnson           "Number of critical functions thin link decided to import");
69d2c234a4STeresa Johnson STATISTIC(NumImportedGlobalVarsThinLink,
70d2c234a4STeresa Johnson           "Number of global variables thin link decided to import");
71d2c234a4STeresa Johnson STATISTIC(NumImportedFunctions, "Number of functions imported in backend");
72d2c234a4STeresa Johnson STATISTIC(NumImportedGlobalVars,
73d2c234a4STeresa Johnson           "Number of global variables imported in backend");
746c475a75STeresa Johnson STATISTIC(NumImportedModules, "Number of modules imported from");
756c475a75STeresa Johnson STATISTIC(NumDeadSymbols, "Number of dead stripped symbols in index");
766c475a75STeresa Johnson STATISTIC(NumLiveSymbols, "Number of live symbols in index");
77d29478f7STeresa Johnson 
7839303619STeresa Johnson /// Limit on instruction count of imported functions.
7939303619STeresa Johnson static cl::opt<unsigned> ImportInstrLimit(
8039303619STeresa Johnson     "import-instr-limit", cl::init(100), cl::Hidden, cl::value_desc("N"),
8139303619STeresa Johnson     cl::desc("Only import functions with less than N instructions"));
8239303619STeresa Johnson 
83974706ebSTeresa Johnson static cl::opt<int> ImportCutoff(
84974706ebSTeresa Johnson     "import-cutoff", cl::init(-1), cl::Hidden, cl::value_desc("N"),
85974706ebSTeresa Johnson     cl::desc("Only import first N functions if N>=0 (default -1)"));
86974706ebSTeresa Johnson 
8740641748SMehdi Amini static cl::opt<float>
8840641748SMehdi Amini     ImportInstrFactor("import-instr-evolution-factor", cl::init(0.7),
8940641748SMehdi Amini                       cl::Hidden, cl::value_desc("x"),
9040641748SMehdi Amini                       cl::desc("As we import functions, multiply the "
9140641748SMehdi Amini                                "`import-instr-limit` threshold by this factor "
9240641748SMehdi Amini                                "before processing newly imported functions"));
93ba72b95fSPiotr Padlewski 
94d2869473SPiotr Padlewski static cl::opt<float> ImportHotInstrFactor(
95d2869473SPiotr Padlewski     "import-hot-evolution-factor", cl::init(1.0), cl::Hidden,
96d2869473SPiotr Padlewski     cl::value_desc("x"),
97d2869473SPiotr Padlewski     cl::desc("As we import functions called from hot callsite, multiply the "
98d2869473SPiotr Padlewski              "`import-instr-limit` threshold by this factor "
99d2869473SPiotr Padlewski              "before processing newly imported functions"));
100d2869473SPiotr Padlewski 
101d9830eb7SPiotr Padlewski static cl::opt<float> ImportHotMultiplier(
1028260d665SDehao Chen     "import-hot-multiplier", cl::init(10.0), cl::Hidden, cl::value_desc("x"),
103ba72b95fSPiotr Padlewski     cl::desc("Multiply the `import-instr-limit` threshold for hot callsites"));
104ba72b95fSPiotr Padlewski 
10564c46574SDehao Chen static cl::opt<float> ImportCriticalMultiplier(
10664c46574SDehao Chen     "import-critical-multiplier", cl::init(100.0), cl::Hidden,
10764c46574SDehao Chen     cl::value_desc("x"),
10864c46574SDehao Chen     cl::desc(
10964c46574SDehao Chen         "Multiply the `import-instr-limit` threshold for critical callsites"));
11064c46574SDehao Chen 
111ba72b95fSPiotr Padlewski // FIXME: This multiplier was not really tuned up.
112ba72b95fSPiotr Padlewski static cl::opt<float> ImportColdMultiplier(
113ba72b95fSPiotr Padlewski     "import-cold-multiplier", cl::init(0), cl::Hidden, cl::value_desc("N"),
114ba72b95fSPiotr Padlewski     cl::desc("Multiply the `import-instr-limit` threshold for cold callsites"));
11540641748SMehdi Amini 
116d29478f7STeresa Johnson static cl::opt<bool> PrintImports("print-imports", cl::init(false), cl::Hidden,
117d29478f7STeresa Johnson                                   cl::desc("Print imported functions"));
118d29478f7STeresa Johnson 
119cb9a82fcSTeresa Johnson static cl::opt<bool> PrintImportFailures(
120cb9a82fcSTeresa Johnson     "print-import-failures", cl::init(false), cl::Hidden,
121cb9a82fcSTeresa Johnson     cl::desc("Print information for functions rejected for importing"));
122cb9a82fcSTeresa Johnson 
1236c475a75STeresa Johnson static cl::opt<bool> ComputeDead("compute-dead", cl::init(true), cl::Hidden,
1246c475a75STeresa Johnson                                  cl::desc("Compute dead symbols"));
1256c475a75STeresa Johnson 
1263b776128SPiotr Padlewski static cl::opt<bool> EnableImportMetadata(
1273b776128SPiotr Padlewski     "enable-import-metadata", cl::init(
1283b776128SPiotr Padlewski #if !defined(NDEBUG)
1293b776128SPiotr Padlewski                                   true /*Enabled with asserts.*/
1303b776128SPiotr Padlewski #else
1313b776128SPiotr Padlewski                                   false
1323b776128SPiotr Padlewski #endif
1333b776128SPiotr Padlewski                                   ),
1343b776128SPiotr Padlewski     cl::Hidden, cl::desc("Enable import metadata like 'thinlto_src_module'"));
1353b776128SPiotr Padlewski 
136e9ea08a0SEugene Zelenko /// Summary file to use for function importing when using -function-import from
137e9ea08a0SEugene Zelenko /// the command line.
138e9ea08a0SEugene Zelenko static cl::opt<std::string>
139e9ea08a0SEugene Zelenko     SummaryFile("summary-file",
140e9ea08a0SEugene Zelenko                 cl::desc("The summary file to use for function importing."));
141e9ea08a0SEugene Zelenko 
14281bbf742STeresa Johnson /// Used when testing importing from distributed indexes via opt
14381bbf742STeresa Johnson // -function-import.
14481bbf742STeresa Johnson static cl::opt<bool>
14581bbf742STeresa Johnson     ImportAllIndex("import-all-index",
14681bbf742STeresa Johnson                    cl::desc("Import all external functions in index."));
14781bbf742STeresa Johnson 
14842418abaSMehdi Amini // Load lazily a module from \p FileName in \p Context.
14942418abaSMehdi Amini static std::unique_ptr<Module> loadFile(const std::string &FileName,
15042418abaSMehdi Amini                                         LLVMContext &Context) {
15142418abaSMehdi Amini   SMDiagnostic Err;
152d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "Loading '" << FileName << "'\n");
1536cba37ceSTeresa Johnson   // Metadata isn't loaded until functions are imported, to minimize
1546cba37ceSTeresa Johnson   // the memory overhead.
155a1080ee6STeresa Johnson   std::unique_ptr<Module> Result =
156a1080ee6STeresa Johnson       getLazyIRFileModule(FileName, Err, Context,
157a1080ee6STeresa Johnson                           /* ShouldLazyLoadMetadata = */ true);
15842418abaSMehdi Amini   if (!Result) {
15942418abaSMehdi Amini     Err.print("function-import", errs());
160d7ad221cSMehdi Amini     report_fatal_error("Abort");
16142418abaSMehdi Amini   }
16242418abaSMehdi Amini 
16342418abaSMehdi Amini   return Result;
16442418abaSMehdi Amini }
16542418abaSMehdi Amini 
16601e32130SMehdi Amini /// Given a list of possible callee implementation for a call site, select one
16701e32130SMehdi Amini /// that fits the \p Threshold.
16801e32130SMehdi Amini ///
16901e32130SMehdi Amini /// FIXME: select "best" instead of first that fits. But what is "best"?
17001e32130SMehdi Amini /// - The smallest: more likely to be inlined.
17101e32130SMehdi Amini /// - The one with the least outgoing edges (already well optimized).
17201e32130SMehdi Amini /// - One from a module already being imported from in order to reduce the
17301e32130SMehdi Amini ///   number of source modules parsed/linked.
17401e32130SMehdi Amini /// - One that has PGO data attached.
17501e32130SMehdi Amini /// - [insert you fancy metric here]
1762d28f7aaSMehdi Amini static const GlobalValueSummary *
177b4e1e829SMehdi Amini selectCallee(const ModuleSummaryIndex &Index,
1789667b91bSPeter Collingbourne              ArrayRef<std::unique_ptr<GlobalValueSummary>> CalleeSummaryList,
179cb9a82fcSTeresa Johnson              unsigned Threshold, StringRef CallerModulePath,
180cb9a82fcSTeresa Johnson              FunctionImporter::ImportFailureReason &Reason,
181cb9a82fcSTeresa Johnson              GlobalValue::GUID GUID) {
182cb9a82fcSTeresa Johnson   Reason = FunctionImporter::ImportFailureReason::None;
18301e32130SMehdi Amini   auto It = llvm::find_if(
18428e457bcSTeresa Johnson       CalleeSummaryList,
18528e457bcSTeresa Johnson       [&](const std::unique_ptr<GlobalValueSummary> &SummaryPtr) {
18628e457bcSTeresa Johnson         auto *GVSummary = SummaryPtr.get();
187cb9a82fcSTeresa Johnson         if (!Index.isGlobalValueLive(GVSummary)) {
188cb9a82fcSTeresa Johnson           Reason = FunctionImporter::ImportFailureReason::NotLive;
189eaf5172cSGeorge Rimar           return false;
190cb9a82fcSTeresa Johnson         }
191eaf5172cSGeorge Rimar 
19273305f82STeresa Johnson         // For SamplePGO, in computeImportForFunction the OriginalId
19373305f82STeresa Johnson         // may have been used to locate the callee summary list (See
19473305f82STeresa Johnson         // comment there).
19573305f82STeresa Johnson         // The mapping from OriginalId to GUID may return a GUID
19673305f82STeresa Johnson         // that corresponds to a static variable. Filter it out here.
19773305f82STeresa Johnson         // This can happen when
19873305f82STeresa Johnson         // 1) There is a call to a library function which is not defined
19973305f82STeresa Johnson         // in the index.
20073305f82STeresa Johnson         // 2) There is a static variable with the  OriginalGUID identical
20173305f82STeresa Johnson         // to the GUID of the library function in 1);
20273305f82STeresa Johnson         // When this happens, the logic for SamplePGO kicks in and
20373305f82STeresa Johnson         // the static variable in 2) will be found, which needs to be
20473305f82STeresa Johnson         // filtered out.
205cb9a82fcSTeresa Johnson         if (GVSummary->getSummaryKind() == GlobalValueSummary::GlobalVarKind) {
206cb9a82fcSTeresa Johnson           Reason = FunctionImporter::ImportFailureReason::GlobalVar;
20773305f82STeresa Johnson           return false;
208cb9a82fcSTeresa Johnson         }
209cb9a82fcSTeresa Johnson         if (GlobalValue::isInterposableLinkage(GVSummary->linkage())) {
210cb9a82fcSTeresa Johnson           Reason = FunctionImporter::ImportFailureReason::InterposableLinkage;
2115b85d8d6SMehdi Amini           // There is no point in importing these, we can't inline them
21201e32130SMehdi Amini           return false;
213cb9a82fcSTeresa Johnson         }
2142c719cc1SMehdi Amini 
21581bbf742STeresa Johnson         auto *Summary = cast<FunctionSummary>(GVSummary->getBaseObject());
2167e88d0daSMehdi Amini 
21783aaf358STeresa Johnson         // If this is a local function, make sure we import the copy
21883aaf358STeresa Johnson         // in the caller's module. The only time a local function can
21983aaf358STeresa Johnson         // share an entry in the index is if there is a local with the same name
22083aaf358STeresa Johnson         // in another module that had the same source file name (in a different
22183aaf358STeresa Johnson         // directory), where each was compiled in their own directory so there
22283aaf358STeresa Johnson         // was not distinguishing path.
22383aaf358STeresa Johnson         // However, do the import from another module if there is only one
22483aaf358STeresa Johnson         // entry in the list - in that case this must be a reference due
22583aaf358STeresa Johnson         // to indirect call profile data, since a function pointer can point to
22683aaf358STeresa Johnson         // a local in another module.
22783aaf358STeresa Johnson         if (GlobalValue::isLocalLinkage(Summary->linkage()) &&
22883aaf358STeresa Johnson             CalleeSummaryList.size() > 1 &&
229cb9a82fcSTeresa Johnson             Summary->modulePath() != CallerModulePath) {
230cb9a82fcSTeresa Johnson           Reason =
231cb9a82fcSTeresa Johnson               FunctionImporter::ImportFailureReason::LocalLinkageNotInModule;
23283aaf358STeresa Johnson           return false;
233cb9a82fcSTeresa Johnson         }
23483aaf358STeresa Johnson 
235b11391bbSTeresa Johnson         if ((Summary->instCount() > Threshold) &&
236b11391bbSTeresa Johnson             !Summary->fflags().AlwaysInline) {
237cb9a82fcSTeresa Johnson           Reason = FunctionImporter::ImportFailureReason::TooLarge;
238f9dc3deaSTeresa Johnson           return false;
239cb9a82fcSTeresa Johnson         }
240f9dc3deaSTeresa Johnson 
241cb397461STeresa Johnson         // Skip if it isn't legal to import (e.g. may reference unpromotable
242cb397461STeresa Johnson         // locals).
243cb9a82fcSTeresa Johnson         if (Summary->notEligibleToImport()) {
244cb9a82fcSTeresa Johnson           Reason = FunctionImporter::ImportFailureReason::NotEligible;
245b4e1e829SMehdi Amini           return false;
246cb9a82fcSTeresa Johnson         }
247b4e1e829SMehdi Amini 
248cb397461STeresa Johnson         // Don't bother importing if we can't inline it anyway.
249cb397461STeresa Johnson         if (Summary->fflags().NoInline) {
250cb397461STeresa Johnson           Reason = FunctionImporter::ImportFailureReason::NoInline;
251cb397461STeresa Johnson           return false;
252cb397461STeresa Johnson         }
253cb397461STeresa Johnson 
25401e32130SMehdi Amini         return true;
25501e32130SMehdi Amini       });
25628e457bcSTeresa Johnson   if (It == CalleeSummaryList.end())
25701e32130SMehdi Amini     return nullptr;
2587e88d0daSMehdi Amini 
259f9dc3deaSTeresa Johnson   return cast<GlobalValueSummary>(It->get());
260434e9561SRafael Espindola }
2617e88d0daSMehdi Amini 
262e9ea08a0SEugene Zelenko namespace {
263e9ea08a0SEugene Zelenko 
264475b51a7STeresa Johnson using EdgeInfo = std::tuple<const FunctionSummary *, unsigned /* Threshold */,
265475b51a7STeresa Johnson                             GlobalValue::GUID>;
26601e32130SMehdi Amini 
267e9ea08a0SEugene Zelenko } // anonymous namespace
268e9ea08a0SEugene Zelenko 
2691958083dSTeresa Johnson static ValueInfo
2701958083dSTeresa Johnson updateValueInfoForIndirectCalls(const ModuleSummaryIndex &Index, ValueInfo VI) {
2711958083dSTeresa Johnson   if (!VI.getSummaryList().empty())
2721958083dSTeresa Johnson     return VI;
2731958083dSTeresa Johnson   // For SamplePGO, the indirect call targets for local functions will
2741958083dSTeresa Johnson   // have its original name annotated in profile. We try to find the
2751958083dSTeresa Johnson   // corresponding PGOFuncName as the GUID.
2761958083dSTeresa Johnson   // FIXME: Consider updating the edges in the graph after building
2771958083dSTeresa Johnson   // it, rather than needing to perform this mapping on each walk.
2781958083dSTeresa Johnson   auto GUID = Index.getGUIDFromOriginalID(VI.getGUID());
2791958083dSTeresa Johnson   if (GUID == 0)
28028d8a49fSEugene Leviant     return ValueInfo();
2811958083dSTeresa Johnson   return Index.getValueInfo(GUID);
2821958083dSTeresa Johnson }
2831958083dSTeresa Johnson 
28419e23874SEugene Leviant static void computeImportForReferencedGlobals(
285dde58938Sevgeny     const FunctionSummary &Summary, const ModuleSummaryIndex &Index,
286dde58938Sevgeny     const GVSummaryMapTy &DefinedGVSummaries,
28719e23874SEugene Leviant     FunctionImporter::ImportMapTy &ImportList,
28819e23874SEugene Leviant     StringMap<FunctionImporter::ExportSetTy> *ExportLists) {
28919e23874SEugene Leviant   for (auto &VI : Summary.refs()) {
29019e23874SEugene Leviant     if (DefinedGVSummaries.count(VI.getGUID())) {
291d34e60caSNicola Zaghen       LLVM_DEBUG(
292d34e60caSNicola Zaghen           dbgs() << "Ref ignored! Target already in destination module.\n");
29319e23874SEugene Leviant       continue;
29419e23874SEugene Leviant     }
29519e23874SEugene Leviant 
2967e7b13d0STeresa Johnson     LLVM_DEBUG(dbgs() << " ref -> " << VI << "\n");
29719e23874SEugene Leviant 
29893f99962STeresa Johnson     // If this is a local variable, make sure we import the copy
29993f99962STeresa Johnson     // in the caller's module. The only time a local variable can
30093f99962STeresa Johnson     // share an entry in the index is if there is a local with the same name
30193f99962STeresa Johnson     // in another module that had the same source file name (in a different
30293f99962STeresa Johnson     // directory), where each was compiled in their own directory so there
30393f99962STeresa Johnson     // was not distinguishing path.
30493f99962STeresa Johnson     auto LocalNotInModule = [&](const GlobalValueSummary *RefSummary) -> bool {
30593f99962STeresa Johnson       return GlobalValue::isLocalLinkage(RefSummary->linkage()) &&
30693f99962STeresa Johnson              RefSummary->modulePath() != Summary.modulePath();
30793f99962STeresa Johnson     };
30893f99962STeresa Johnson 
30919e23874SEugene Leviant     for (auto &RefSummary : VI.getSummaryList())
310bf46e741SEugene Leviant       if (isa<GlobalVarSummary>(RefSummary.get()) &&
311dde58938Sevgeny           Index.canImportGlobalVar(RefSummary.get(), /* AnalyzeRefs */ true) &&
31293f99962STeresa Johnson           !LocalNotInModule(RefSummary.get())) {
313d2c234a4STeresa Johnson         auto ILI = ImportList[RefSummary->modulePath()].insert(VI.getGUID());
314*bed4d9c8STeresa Johnson         // Only update stat and exports if we haven't already imported this
315*bed4d9c8STeresa Johnson         // variable.
316*bed4d9c8STeresa Johnson         if (!ILI.second)
317*bed4d9c8STeresa Johnson           break;
318d2c234a4STeresa Johnson         NumImportedGlobalVarsThinLink++;
319*bed4d9c8STeresa Johnson         // Any references made by this variable will be marked exported later,
320*bed4d9c8STeresa Johnson         // in ComputeCrossModuleImport, after import decisions are complete,
321*bed4d9c8STeresa Johnson         // which is more efficient than adding them here.
322*bed4d9c8STeresa Johnson         if (ExportLists)
323*bed4d9c8STeresa Johnson           (*ExportLists)[RefSummary->modulePath()].insert(VI);
32419e23874SEugene Leviant         break;
32519e23874SEugene Leviant       }
32619e23874SEugene Leviant   }
32719e23874SEugene Leviant }
32819e23874SEugene Leviant 
329cb9a82fcSTeresa Johnson static const char *
330cb9a82fcSTeresa Johnson getFailureName(FunctionImporter::ImportFailureReason Reason) {
331cb9a82fcSTeresa Johnson   switch (Reason) {
332cb9a82fcSTeresa Johnson   case FunctionImporter::ImportFailureReason::None:
333cb9a82fcSTeresa Johnson     return "None";
334cb9a82fcSTeresa Johnson   case FunctionImporter::ImportFailureReason::GlobalVar:
335cb9a82fcSTeresa Johnson     return "GlobalVar";
336cb9a82fcSTeresa Johnson   case FunctionImporter::ImportFailureReason::NotLive:
337cb9a82fcSTeresa Johnson     return "NotLive";
338cb9a82fcSTeresa Johnson   case FunctionImporter::ImportFailureReason::TooLarge:
339cb9a82fcSTeresa Johnson     return "TooLarge";
340cb9a82fcSTeresa Johnson   case FunctionImporter::ImportFailureReason::InterposableLinkage:
341cb9a82fcSTeresa Johnson     return "InterposableLinkage";
342cb9a82fcSTeresa Johnson   case FunctionImporter::ImportFailureReason::LocalLinkageNotInModule:
343cb9a82fcSTeresa Johnson     return "LocalLinkageNotInModule";
344cb9a82fcSTeresa Johnson   case FunctionImporter::ImportFailureReason::NotEligible:
345cb9a82fcSTeresa Johnson     return "NotEligible";
346cb397461STeresa Johnson   case FunctionImporter::ImportFailureReason::NoInline:
347cb397461STeresa Johnson     return "NoInline";
348cb9a82fcSTeresa Johnson   }
349cb9a82fcSTeresa Johnson   llvm_unreachable("invalid reason");
350cb9a82fcSTeresa Johnson }
351cb9a82fcSTeresa Johnson 
35201e32130SMehdi Amini /// Compute the list of functions to import for a given caller. Mark these
35301e32130SMehdi Amini /// imported functions and the symbols they reference in their source module as
35401e32130SMehdi Amini /// exported from their source module.
35501e32130SMehdi Amini static void computeImportForFunction(
3563255eec1STeresa Johnson     const FunctionSummary &Summary, const ModuleSummaryIndex &Index,
357d9830eb7SPiotr Padlewski     const unsigned Threshold, const GVSummaryMapTy &DefinedGVSummaries,
35801e32130SMehdi Amini     SmallVectorImpl<EdgeInfo> &Worklist,
3599b490f10SMehdi Amini     FunctionImporter::ImportMapTy &ImportList,
360d68935c5STeresa Johnson     StringMap<FunctionImporter::ExportSetTy> *ExportLists,
361d68935c5STeresa Johnson     FunctionImporter::ImportThresholdsTy &ImportThresholds) {
362dde58938Sevgeny   computeImportForReferencedGlobals(Summary, Index, DefinedGVSummaries,
363dde58938Sevgeny                                     ImportList, ExportLists);
364974706ebSTeresa Johnson   static int ImportCount = 0;
36501e32130SMehdi Amini   for (auto &Edge : Summary.calls()) {
3669667b91bSPeter Collingbourne     ValueInfo VI = Edge.first;
3677e7b13d0STeresa Johnson     LLVM_DEBUG(dbgs() << " edge -> " << VI << " Threshold:" << Threshold
3687e7b13d0STeresa Johnson                       << "\n");
36901e32130SMehdi Amini 
370974706ebSTeresa Johnson     if (ImportCutoff >= 0 && ImportCount >= ImportCutoff) {
371d34e60caSNicola Zaghen       LLVM_DEBUG(dbgs() << "ignored! import-cutoff value of " << ImportCutoff
372974706ebSTeresa Johnson                         << " reached.\n");
373974706ebSTeresa Johnson       continue;
374974706ebSTeresa Johnson     }
375974706ebSTeresa Johnson 
3761958083dSTeresa Johnson     VI = updateValueInfoForIndirectCalls(Index, VI);
3779667b91bSPeter Collingbourne     if (!VI)
3789667b91bSPeter Collingbourne       continue;
3794a435e08SDehao Chen 
3809667b91bSPeter Collingbourne     if (DefinedGVSummaries.count(VI.getGUID())) {
381d34e60caSNicola Zaghen       LLVM_DEBUG(dbgs() << "ignored! Target already in destination module.\n");
3827e88d0daSMehdi Amini       continue;
383d450da32STeresa Johnson     }
38440641748SMehdi Amini 
385ba72b95fSPiotr Padlewski     auto GetBonusMultiplier = [](CalleeInfo::HotnessType Hotness) -> float {
386ba72b95fSPiotr Padlewski       if (Hotness == CalleeInfo::HotnessType::Hot)
387ba72b95fSPiotr Padlewski         return ImportHotMultiplier;
388ba72b95fSPiotr Padlewski       if (Hotness == CalleeInfo::HotnessType::Cold)
389ba72b95fSPiotr Padlewski         return ImportColdMultiplier;
39064c46574SDehao Chen       if (Hotness == CalleeInfo::HotnessType::Critical)
39164c46574SDehao Chen         return ImportCriticalMultiplier;
392ba72b95fSPiotr Padlewski       return 1.0;
393ba72b95fSPiotr Padlewski     };
394ba72b95fSPiotr Padlewski 
395d9830eb7SPiotr Padlewski     const auto NewThreshold =
396c73cec84SEaswaran Raman         Threshold * GetBonusMultiplier(Edge.second.getHotness());
397d2869473SPiotr Padlewski 
398cb9a82fcSTeresa Johnson     auto IT = ImportThresholds.insert(std::make_pair(
399cb9a82fcSTeresa Johnson         VI.getGUID(), std::make_tuple(NewThreshold, nullptr, nullptr)));
400d68935c5STeresa Johnson     bool PreviouslyVisited = !IT.second;
401cb9a82fcSTeresa Johnson     auto &ProcessedThreshold = std::get<0>(IT.first->second);
402cb9a82fcSTeresa Johnson     auto &CalleeSummary = std::get<1>(IT.first->second);
403cb9a82fcSTeresa Johnson     auto &FailureInfo = std::get<2>(IT.first->second);
404d68935c5STeresa Johnson 
405d2c234a4STeresa Johnson     bool IsHotCallsite =
406d2c234a4STeresa Johnson         Edge.second.getHotness() == CalleeInfo::HotnessType::Hot;
407d2c234a4STeresa Johnson     bool IsCriticalCallsite =
408d2c234a4STeresa Johnson         Edge.second.getHotness() == CalleeInfo::HotnessType::Critical;
409d2c234a4STeresa Johnson 
410d68935c5STeresa Johnson     const FunctionSummary *ResolvedCalleeSummary = nullptr;
411d68935c5STeresa Johnson     if (CalleeSummary) {
412d68935c5STeresa Johnson       assert(PreviouslyVisited);
413d68935c5STeresa Johnson       // Since the traversal of the call graph is DFS, we can revisit a function
414d68935c5STeresa Johnson       // a second time with a higher threshold. In this case, it is added back
415d68935c5STeresa Johnson       // to the worklist with the new threshold (so that its own callee chains
416d68935c5STeresa Johnson       // can be considered with the higher threshold).
417d68935c5STeresa Johnson       if (NewThreshold <= ProcessedThreshold) {
418d68935c5STeresa Johnson         LLVM_DEBUG(
419d68935c5STeresa Johnson             dbgs() << "ignored! Target was already imported with Threshold "
420d68935c5STeresa Johnson                    << ProcessedThreshold << "\n");
421d68935c5STeresa Johnson         continue;
422d68935c5STeresa Johnson       }
423d68935c5STeresa Johnson       // Update with new larger threshold.
424d68935c5STeresa Johnson       ProcessedThreshold = NewThreshold;
425d68935c5STeresa Johnson       ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary);
426d68935c5STeresa Johnson     } else {
427d68935c5STeresa Johnson       // If we already rejected importing a callee at the same or higher
428d68935c5STeresa Johnson       // threshold, don't waste time calling selectCallee.
429d68935c5STeresa Johnson       if (PreviouslyVisited && NewThreshold <= ProcessedThreshold) {
430d68935c5STeresa Johnson         LLVM_DEBUG(
431d68935c5STeresa Johnson             dbgs() << "ignored! Target was already rejected with Threshold "
432d68935c5STeresa Johnson             << ProcessedThreshold << "\n");
433cb9a82fcSTeresa Johnson         if (PrintImportFailures) {
434cb9a82fcSTeresa Johnson           assert(FailureInfo &&
435cb9a82fcSTeresa Johnson                  "Expected FailureInfo for previously rejected candidate");
436cb9a82fcSTeresa Johnson           FailureInfo->Attempts++;
437cb9a82fcSTeresa Johnson         }
438d68935c5STeresa Johnson         continue;
439d68935c5STeresa Johnson       }
440d68935c5STeresa Johnson 
441cb9a82fcSTeresa Johnson       FunctionImporter::ImportFailureReason Reason;
442d68935c5STeresa Johnson       CalleeSummary = selectCallee(Index, VI.getSummaryList(), NewThreshold,
443cb9a82fcSTeresa Johnson                                    Summary.modulePath(), Reason, VI.getGUID());
44401e32130SMehdi Amini       if (!CalleeSummary) {
445d68935c5STeresa Johnson         // Update with new larger threshold if this was a retry (otherwise
446cb9a82fcSTeresa Johnson         // we would have already inserted with NewThreshold above). Also
447cb9a82fcSTeresa Johnson         // update failure info if requested.
448cb9a82fcSTeresa Johnson         if (PreviouslyVisited) {
449d68935c5STeresa Johnson           ProcessedThreshold = NewThreshold;
450cb9a82fcSTeresa Johnson           if (PrintImportFailures) {
451cb9a82fcSTeresa Johnson             assert(FailureInfo &&
452cb9a82fcSTeresa Johnson                    "Expected FailureInfo for previously rejected candidate");
453cb9a82fcSTeresa Johnson             FailureInfo->Reason = Reason;
454cb9a82fcSTeresa Johnson             FailureInfo->Attempts++;
455cb9a82fcSTeresa Johnson             FailureInfo->MaxHotness =
456cb9a82fcSTeresa Johnson                 std::max(FailureInfo->MaxHotness, Edge.second.getHotness());
457cb9a82fcSTeresa Johnson           }
458cb9a82fcSTeresa Johnson         } else if (PrintImportFailures) {
459cb9a82fcSTeresa Johnson           assert(!FailureInfo &&
460cb9a82fcSTeresa Johnson                  "Expected no FailureInfo for newly rejected candidate");
4610eaee545SJonas Devlieghere           FailureInfo = std::make_unique<FunctionImporter::ImportFailureInfo>(
462cb9a82fcSTeresa Johnson               VI, Edge.second.getHotness(), Reason, 1);
463cb9a82fcSTeresa Johnson         }
464d34e60caSNicola Zaghen         LLVM_DEBUG(
465d34e60caSNicola Zaghen             dbgs() << "ignored! No qualifying callee with summary found.\n");
4667e88d0daSMehdi Amini         continue;
4677e88d0daSMehdi Amini       }
4682f0cc477SDavid Blaikie 
4692f0cc477SDavid Blaikie       // "Resolve" the summary
470d68935c5STeresa Johnson       CalleeSummary = CalleeSummary->getBaseObject();
471d68935c5STeresa Johnson       ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary);
4722d28f7aaSMehdi Amini 
473b11391bbSTeresa Johnson       assert((ResolvedCalleeSummary->fflags().AlwaysInline ||
474b11391bbSTeresa Johnson 	     (ResolvedCalleeSummary->instCount() <= NewThreshold)) &&
47501e32130SMehdi Amini              "selectCallee() didn't honor the threshold");
47601e32130SMehdi Amini 
4771b859a23STeresa Johnson       auto ExportModulePath = ResolvedCalleeSummary->modulePath();
478d68935c5STeresa Johnson       auto ILI = ImportList[ExportModulePath].insert(VI.getGUID());
479d68935c5STeresa Johnson       // We previously decided to import this GUID definition if it was already
480d68935c5STeresa Johnson       // inserted in the set of imports from the exporting module.
481d68935c5STeresa Johnson       bool PreviouslyImported = !ILI.second;
482d2c234a4STeresa Johnson       if (!PreviouslyImported) {
483d2c234a4STeresa Johnson         NumImportedFunctionsThinLink++;
484d2c234a4STeresa Johnson         if (IsHotCallsite)
485d2c234a4STeresa Johnson           NumImportedHotFunctionsThinLink++;
486d2c234a4STeresa Johnson         if (IsCriticalCallsite)
487d2c234a4STeresa Johnson           NumImportedCriticalFunctionsThinLink++;
488d2c234a4STeresa Johnson       }
489974706ebSTeresa Johnson 
490*bed4d9c8STeresa Johnson       // Any calls/references made by this function will be marked exported
491*bed4d9c8STeresa Johnson       // later, in ComputeCrossModuleImport, after import decisions are
492*bed4d9c8STeresa Johnson       // complete, which is more efficient than adding them here.
493*bed4d9c8STeresa Johnson       if (ExportLists)
494*bed4d9c8STeresa Johnson         (*ExportLists)[ExportModulePath].insert(VI);
495d68935c5STeresa Johnson     }
496d68935c5STeresa Johnson 
497d68935c5STeresa Johnson     auto GetAdjustedThreshold = [](unsigned Threshold, bool IsHotCallsite) {
498d68935c5STeresa Johnson       // Adjust the threshold for next level of imported functions.
499d68935c5STeresa Johnson       // The threshold is different for hot callsites because we can then
500d68935c5STeresa Johnson       // inline chains of hot calls.
501d68935c5STeresa Johnson       if (IsHotCallsite)
502d68935c5STeresa Johnson         return Threshold * ImportHotInstrFactor;
503d68935c5STeresa Johnson       return Threshold * ImportInstrFactor;
504d68935c5STeresa Johnson     };
505d68935c5STeresa Johnson 
506d68935c5STeresa Johnson     const auto AdjThreshold = GetAdjustedThreshold(Threshold, IsHotCallsite);
507d68935c5STeresa Johnson 
508d68935c5STeresa Johnson     ImportCount++;
509d2869473SPiotr Padlewski 
51001e32130SMehdi Amini     // Insert the newly imported function to the worklist.
5119667b91bSPeter Collingbourne     Worklist.emplace_back(ResolvedCalleeSummary, AdjThreshold, VI.getGUID());
512d450da32STeresa Johnson   }
513d450da32STeresa Johnson }
514d450da32STeresa Johnson 
51501e32130SMehdi Amini /// Given the list of globals defined in a module, compute the list of imports
51601e32130SMehdi Amini /// as well as the list of "exports", i.e. the list of symbols referenced from
51701e32130SMehdi Amini /// another module (that may require promotion).
51801e32130SMehdi Amini static void ComputeImportForModule(
519c851d216STeresa Johnson     const GVSummaryMapTy &DefinedGVSummaries, const ModuleSummaryIndex &Index,
520cb9a82fcSTeresa Johnson     StringRef ModName, FunctionImporter::ImportMapTy &ImportList,
52156584bbfSEvgeniy Stepanov     StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) {
52201e32130SMehdi Amini   // Worklist contains the list of function imported in this module, for which
52301e32130SMehdi Amini   // we will analyse the callees and may import further down the callgraph.
52401e32130SMehdi Amini   SmallVector<EdgeInfo, 128> Worklist;
525d68935c5STeresa Johnson   FunctionImporter::ImportThresholdsTy ImportThresholds;
52601e32130SMehdi Amini 
52701e32130SMehdi Amini   // Populate the worklist with the import for the functions in the current
52801e32130SMehdi Amini   // module
52928e457bcSTeresa Johnson   for (auto &GVSummary : DefinedGVSummaries) {
5307e7b13d0STeresa Johnson #ifndef NDEBUG
5317e7b13d0STeresa Johnson     // FIXME: Change the GVSummaryMapTy to hold ValueInfo instead of GUID
5327e7b13d0STeresa Johnson     // so this map look up (and possibly others) can be avoided.
5337e7b13d0STeresa Johnson     auto VI = Index.getValueInfo(GVSummary.first);
5347e7b13d0STeresa Johnson #endif
53556584bbfSEvgeniy Stepanov     if (!Index.isGlobalValueLive(GVSummary.second)) {
5367e7b13d0STeresa Johnson       LLVM_DEBUG(dbgs() << "Ignores Dead GUID: " << VI << "\n");
5376c475a75STeresa Johnson       continue;
5386c475a75STeresa Johnson     }
539cfbd0892SPeter Collingbourne     auto *FuncSummary =
540cfbd0892SPeter Collingbourne         dyn_cast<FunctionSummary>(GVSummary.second->getBaseObject());
5411aafabf7SMehdi Amini     if (!FuncSummary)
5421aafabf7SMehdi Amini       // Skip import for global variables
5431aafabf7SMehdi Amini       continue;
5447e7b13d0STeresa Johnson     LLVM_DEBUG(dbgs() << "Initialize import for " << VI << "\n");
5452d28f7aaSMehdi Amini     computeImportForFunction(*FuncSummary, Index, ImportInstrLimit,
5469b490f10SMehdi Amini                              DefinedGVSummaries, Worklist, ImportList,
547d68935c5STeresa Johnson                              ExportLists, ImportThresholds);
54801e32130SMehdi Amini   }
54901e32130SMehdi Amini 
550d2869473SPiotr Padlewski   // Process the newly imported functions and add callees to the worklist.
55142418abaSMehdi Amini   while (!Worklist.empty()) {
55201e32130SMehdi Amini     auto FuncInfo = Worklist.pop_back_val();
553475b51a7STeresa Johnson     auto *Summary = std::get<0>(FuncInfo);
554475b51a7STeresa Johnson     auto Threshold = std::get<1>(FuncInfo);
55542418abaSMehdi Amini 
5561aafabf7SMehdi Amini     computeImportForFunction(*Summary, Index, Threshold, DefinedGVSummaries,
557d68935c5STeresa Johnson                              Worklist, ImportList, ExportLists,
558d68935c5STeresa Johnson                              ImportThresholds);
559c8c55170SMehdi Amini   }
560cb9a82fcSTeresa Johnson 
561cb9a82fcSTeresa Johnson   // Print stats about functions considered but rejected for importing
562cb9a82fcSTeresa Johnson   // when requested.
563cb9a82fcSTeresa Johnson   if (PrintImportFailures) {
564cb9a82fcSTeresa Johnson     dbgs() << "Missed imports into module " << ModName << "\n";
565cb9a82fcSTeresa Johnson     for (auto &I : ImportThresholds) {
566cb9a82fcSTeresa Johnson       auto &ProcessedThreshold = std::get<0>(I.second);
567cb9a82fcSTeresa Johnson       auto &CalleeSummary = std::get<1>(I.second);
568cb9a82fcSTeresa Johnson       auto &FailureInfo = std::get<2>(I.second);
569cb9a82fcSTeresa Johnson       if (CalleeSummary)
570cb9a82fcSTeresa Johnson         continue; // We are going to import.
571cb9a82fcSTeresa Johnson       assert(FailureInfo);
572cb9a82fcSTeresa Johnson       FunctionSummary *FS = nullptr;
573cb9a82fcSTeresa Johnson       if (!FailureInfo->VI.getSummaryList().empty())
574cb9a82fcSTeresa Johnson         FS = dyn_cast<FunctionSummary>(
575cb9a82fcSTeresa Johnson             FailureInfo->VI.getSummaryList()[0]->getBaseObject());
576cb9a82fcSTeresa Johnson       dbgs() << FailureInfo->VI
577cb9a82fcSTeresa Johnson              << ": Reason = " << getFailureName(FailureInfo->Reason)
578cb9a82fcSTeresa Johnson              << ", Threshold = " << ProcessedThreshold
579cb9a82fcSTeresa Johnson              << ", Size = " << (FS ? (int)FS->instCount() : -1)
580cb9a82fcSTeresa Johnson              << ", MaxHotness = " << getHotnessName(FailureInfo->MaxHotness)
581cb9a82fcSTeresa Johnson              << ", Attempts = " << FailureInfo->Attempts << "\n";
582cb9a82fcSTeresa Johnson     }
583cb9a82fcSTeresa Johnson   }
58442418abaSMehdi Amini }
585ffe2e4aaSMehdi Amini 
58619e23874SEugene Leviant #ifndef NDEBUG
5874ef9315cSevgeny static bool isGlobalVarSummary(const ModuleSummaryIndex &Index, ValueInfo VI) {
58819e23874SEugene Leviant   auto SL = VI.getSummaryList();
5894ef9315cSevgeny   return SL.empty()
5904ef9315cSevgeny              ? false
5914ef9315cSevgeny              : SL[0]->getSummaryKind() == GlobalValueSummary::GlobalVarKind;
59219e23874SEugene Leviant }
59319e23874SEugene Leviant 
5944ef9315cSevgeny static bool isGlobalVarSummary(const ModuleSummaryIndex &Index,
5954ef9315cSevgeny                                GlobalValue::GUID G) {
5964ef9315cSevgeny   if (const auto &VI = Index.getValueInfo(G))
5974ef9315cSevgeny     return isGlobalVarSummary(Index, VI);
5984ef9315cSevgeny   return false;
5994ef9315cSevgeny }
60019e23874SEugene Leviant 
60119e23874SEugene Leviant template <class T>
6021fc0da48SBenjamin Kramer static unsigned numGlobalVarSummaries(const ModuleSummaryIndex &Index,
6031fc0da48SBenjamin Kramer                                       T &Cont) {
60419e23874SEugene Leviant   unsigned NumGVS = 0;
60519e23874SEugene Leviant   for (auto &V : Cont)
6064ef9315cSevgeny     if (isGlobalVarSummary(Index, V))
60719e23874SEugene Leviant       ++NumGVS;
60819e23874SEugene Leviant   return NumGVS;
60919e23874SEugene Leviant }
61019e23874SEugene Leviant #endif
61119e23874SEugene Leviant 
6128bcd01f4SFangrui Song #ifndef NDEBUG
6133d708bf5Sevgeny static bool
6143d708bf5Sevgeny checkVariableImport(const ModuleSummaryIndex &Index,
6153d708bf5Sevgeny                     StringMap<FunctionImporter::ImportMapTy> &ImportLists,
6163d708bf5Sevgeny                     StringMap<FunctionImporter::ExportSetTy> &ExportLists) {
6173d708bf5Sevgeny 
6183d708bf5Sevgeny   DenseSet<GlobalValue::GUID> FlattenedImports;
6193d708bf5Sevgeny 
6203d708bf5Sevgeny   for (auto &ImportPerModule : ImportLists)
6213d708bf5Sevgeny     for (auto &ExportPerModule : ImportPerModule.second)
6223d708bf5Sevgeny       FlattenedImports.insert(ExportPerModule.second.begin(),
6233d708bf5Sevgeny                               ExportPerModule.second.end());
6243d708bf5Sevgeny 
6253d708bf5Sevgeny   // Checks that all GUIDs of read/writeonly vars we see in export lists
6263d708bf5Sevgeny   // are also in the import lists. Otherwise we my face linker undefs,
6273d708bf5Sevgeny   // because readonly and writeonly vars are internalized in their
6283d708bf5Sevgeny   // source modules.
6293d708bf5Sevgeny   auto IsReadOrWriteOnlyVar = [&](StringRef ModulePath, const ValueInfo &VI) {
6303d708bf5Sevgeny     auto *GVS = dyn_cast_or_null<GlobalVarSummary>(
6313d708bf5Sevgeny         Index.findSummaryInModule(VI, ModulePath));
6323d708bf5Sevgeny     return GVS && (Index.isReadOnly(GVS) || Index.isWriteOnly(GVS));
6333d708bf5Sevgeny   };
6343d708bf5Sevgeny 
6353d708bf5Sevgeny   for (auto &ExportPerModule : ExportLists)
6363d708bf5Sevgeny     for (auto &VI : ExportPerModule.second)
6373d708bf5Sevgeny       if (!FlattenedImports.count(VI.getGUID()) &&
6383d708bf5Sevgeny           IsReadOrWriteOnlyVar(ExportPerModule.first(), VI))
6393d708bf5Sevgeny         return false;
6403d708bf5Sevgeny 
6413d708bf5Sevgeny   return true;
6423d708bf5Sevgeny }
6438bcd01f4SFangrui Song #endif
6443d708bf5Sevgeny 
645c86af334STeresa Johnson /// Compute all the import and export for every module using the Index.
64601e32130SMehdi Amini void llvm::ComputeCrossModuleImport(
64701e32130SMehdi Amini     const ModuleSummaryIndex &Index,
648c851d216STeresa Johnson     const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
64901e32130SMehdi Amini     StringMap<FunctionImporter::ImportMapTy> &ImportLists,
65056584bbfSEvgeniy Stepanov     StringMap<FunctionImporter::ExportSetTy> &ExportLists) {
65101e32130SMehdi Amini   // For each module that has function defined, compute the import/export lists.
6521aafabf7SMehdi Amini   for (auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) {
6539b490f10SMehdi Amini     auto &ImportList = ImportLists[DefinedGVSummaries.first()];
654d34e60caSNicola Zaghen     LLVM_DEBUG(dbgs() << "Computing import for Module '"
6551aafabf7SMehdi Amini                       << DefinedGVSummaries.first() << "'\n");
656cb9a82fcSTeresa Johnson     ComputeImportForModule(DefinedGVSummaries.second, Index,
657cb9a82fcSTeresa Johnson                            DefinedGVSummaries.first(), ImportList,
65856584bbfSEvgeniy Stepanov                            &ExportLists);
65901e32130SMehdi Amini   }
66001e32130SMehdi Amini 
661*bed4d9c8STeresa Johnson   // When computing imports we only added the variables and functions being
662*bed4d9c8STeresa Johnson   // imported to the export list. We also need to mark any references and calls
663*bed4d9c8STeresa Johnson   // they make as exported as well. We do this here, as it is more efficient
664*bed4d9c8STeresa Johnson   // since we may import the same values multiple times into different modules
665*bed4d9c8STeresa Johnson   // during the import computation.
666edddca22STeresa Johnson   for (auto &ELI : ExportLists) {
667*bed4d9c8STeresa Johnson     FunctionImporter::ExportSetTy NewExports;
668edddca22STeresa Johnson     const auto &DefinedGVSummaries =
669edddca22STeresa Johnson         ModuleToDefinedGVSummaries.lookup(ELI.first());
670*bed4d9c8STeresa Johnson     for (auto &EI : ELI.second) {
671*bed4d9c8STeresa Johnson       // Find the copy defined in the exporting module so that we can mark the
672*bed4d9c8STeresa Johnson       // values it references in that specific definition as exported.
673*bed4d9c8STeresa Johnson       // Below we will add all references and called values, without regard to
674*bed4d9c8STeresa Johnson       // whether they are also defined in this module. We subsequently prune the
675*bed4d9c8STeresa Johnson       // list to only include those defined in the exporting module, see comment
676*bed4d9c8STeresa Johnson       // there as to why.
677*bed4d9c8STeresa Johnson       auto DS = DefinedGVSummaries.find(EI.getGUID());
678*bed4d9c8STeresa Johnson       // Anything marked exported during the import computation must have been
679*bed4d9c8STeresa Johnson       // defined in the exporting module.
680*bed4d9c8STeresa Johnson       assert(DS != DefinedGVSummaries.end());
681*bed4d9c8STeresa Johnson       auto *S = DS->getSecond();
682*bed4d9c8STeresa Johnson       S = S->getBaseObject();
683*bed4d9c8STeresa Johnson       if (auto *GVS = dyn_cast<GlobalVarSummary>(S)) {
684*bed4d9c8STeresa Johnson         // Export referenced functions and variables. We don't export/promote
685*bed4d9c8STeresa Johnson         // objects referenced by writeonly variable initializer, because
686*bed4d9c8STeresa Johnson         // we convert such variables initializers to "zeroinitializer".
687*bed4d9c8STeresa Johnson         // See processGlobalForThinLTO.
688*bed4d9c8STeresa Johnson         if (!Index.isWriteOnly(GVS))
689*bed4d9c8STeresa Johnson           for (const auto &VI : GVS->refs())
690*bed4d9c8STeresa Johnson             NewExports.insert(VI);
691*bed4d9c8STeresa Johnson       } else {
692*bed4d9c8STeresa Johnson         auto *FS = cast<FunctionSummary>(S);
693*bed4d9c8STeresa Johnson         for (auto &Edge : FS->calls())
694*bed4d9c8STeresa Johnson           NewExports.insert(Edge.first);
695*bed4d9c8STeresa Johnson         for (auto &Ref : FS->refs())
696*bed4d9c8STeresa Johnson           NewExports.insert(Ref);
697*bed4d9c8STeresa Johnson       }
698*bed4d9c8STeresa Johnson     }
699*bed4d9c8STeresa Johnson     // Prune list computed above to only include values defined in the exporting
700*bed4d9c8STeresa Johnson     // module. We do this after the above insertion since we may hit the same
701*bed4d9c8STeresa Johnson     // ref/call target multiple times in above loop, and it is more efficient to
702*bed4d9c8STeresa Johnson     // avoid a set lookup each time.
703*bed4d9c8STeresa Johnson     for (auto EI = NewExports.begin(); EI != NewExports.end();) {
7043d708bf5Sevgeny       if (!DefinedGVSummaries.count(EI->getGUID()))
705*bed4d9c8STeresa Johnson         NewExports.erase(EI++);
706360f6617SBenjamin Kramer       else
707360f6617SBenjamin Kramer         ++EI;
708360f6617SBenjamin Kramer     }
709*bed4d9c8STeresa Johnson     ELI.second.insert(NewExports.begin(), NewExports.end());
710edddca22STeresa Johnson   }
711edddca22STeresa Johnson 
7123d708bf5Sevgeny   assert(checkVariableImport(Index, ImportLists, ExportLists));
71301e32130SMehdi Amini #ifndef NDEBUG
714d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "Import/Export lists for " << ImportLists.size()
71501e32130SMehdi Amini                     << " modules:\n");
71601e32130SMehdi Amini   for (auto &ModuleImports : ImportLists) {
71701e32130SMehdi Amini     auto ModName = ModuleImports.first();
71801e32130SMehdi Amini     auto &Exports = ExportLists[ModName];
71919e23874SEugene Leviant     unsigned NumGVS = numGlobalVarSummaries(Index, Exports);
720d34e60caSNicola Zaghen     LLVM_DEBUG(dbgs() << "* Module " << ModName << " exports "
72119e23874SEugene Leviant                       << Exports.size() - NumGVS << " functions and " << NumGVS
722d34e60caSNicola Zaghen                       << " vars. Imports from " << ModuleImports.second.size()
723d34e60caSNicola Zaghen                       << " modules.\n");
72401e32130SMehdi Amini     for (auto &Src : ModuleImports.second) {
72501e32130SMehdi Amini       auto SrcModName = Src.first();
72619e23874SEugene Leviant       unsigned NumGVSPerMod = numGlobalVarSummaries(Index, Src.second);
727d34e60caSNicola Zaghen       LLVM_DEBUG(dbgs() << " - " << Src.second.size() - NumGVSPerMod
72819e23874SEugene Leviant                         << " functions imported from " << SrcModName << "\n");
729d34e60caSNicola Zaghen       LLVM_DEBUG(dbgs() << " - " << NumGVSPerMod
730d34e60caSNicola Zaghen                         << " global vars imported from " << SrcModName << "\n");
73101e32130SMehdi Amini     }
73201e32130SMehdi Amini   }
73301e32130SMehdi Amini #endif
73401e32130SMehdi Amini }
73501e32130SMehdi Amini 
73681bbf742STeresa Johnson #ifndef NDEBUG
73719e23874SEugene Leviant static void dumpImportListForModule(const ModuleSummaryIndex &Index,
73819e23874SEugene Leviant                                     StringRef ModulePath,
73981bbf742STeresa Johnson                                     FunctionImporter::ImportMapTy &ImportList) {
740d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "* Module " << ModulePath << " imports from "
74181bbf742STeresa Johnson                     << ImportList.size() << " modules.\n");
74281bbf742STeresa Johnson   for (auto &Src : ImportList) {
74381bbf742STeresa Johnson     auto SrcModName = Src.first();
74419e23874SEugene Leviant     unsigned NumGVSPerMod = numGlobalVarSummaries(Index, Src.second);
745d34e60caSNicola Zaghen     LLVM_DEBUG(dbgs() << " - " << Src.second.size() - NumGVSPerMod
74619e23874SEugene Leviant                       << " functions imported from " << SrcModName << "\n");
747d34e60caSNicola Zaghen     LLVM_DEBUG(dbgs() << " - " << NumGVSPerMod << " vars imported from "
74881bbf742STeresa Johnson                       << SrcModName << "\n");
74981bbf742STeresa Johnson   }
75081bbf742STeresa Johnson }
75169b2de84STeresa Johnson #endif
75281bbf742STeresa Johnson 
753c86af334STeresa Johnson /// Compute all the imports for the given module in the Index.
754c86af334STeresa Johnson void llvm::ComputeCrossModuleImportForModule(
755c86af334STeresa Johnson     StringRef ModulePath, const ModuleSummaryIndex &Index,
756c86af334STeresa Johnson     FunctionImporter::ImportMapTy &ImportList) {
757c86af334STeresa Johnson   // Collect the list of functions this module defines.
758c86af334STeresa Johnson   // GUID -> Summary
759c851d216STeresa Johnson   GVSummaryMapTy FunctionSummaryMap;
76028e457bcSTeresa Johnson   Index.collectDefinedFunctionsForModule(ModulePath, FunctionSummaryMap);
761c86af334STeresa Johnson 
762c86af334STeresa Johnson   // Compute the import list for this module.
763d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "Computing import for Module '" << ModulePath << "'\n");
764cb9a82fcSTeresa Johnson   ComputeImportForModule(FunctionSummaryMap, Index, ModulePath, ImportList);
765c86af334STeresa Johnson 
766c86af334STeresa Johnson #ifndef NDEBUG
76719e23874SEugene Leviant   dumpImportListForModule(Index, ModulePath, ImportList);
76881bbf742STeresa Johnson #endif
769c86af334STeresa Johnson }
77081bbf742STeresa Johnson 
77181bbf742STeresa Johnson // Mark all external summaries in Index for import into the given module.
77281bbf742STeresa Johnson // Used for distributed builds using a distributed index.
77381bbf742STeresa Johnson void llvm::ComputeCrossModuleImportForModuleFromIndex(
77481bbf742STeresa Johnson     StringRef ModulePath, const ModuleSummaryIndex &Index,
77581bbf742STeresa Johnson     FunctionImporter::ImportMapTy &ImportList) {
77681bbf742STeresa Johnson   for (auto &GlobalList : Index) {
77781bbf742STeresa Johnson     // Ignore entries for undefined references.
77881bbf742STeresa Johnson     if (GlobalList.second.SummaryList.empty())
77981bbf742STeresa Johnson       continue;
78081bbf742STeresa Johnson 
78181bbf742STeresa Johnson     auto GUID = GlobalList.first;
78281bbf742STeresa Johnson     assert(GlobalList.second.SummaryList.size() == 1 &&
78381bbf742STeresa Johnson            "Expected individual combined index to have one summary per GUID");
78481bbf742STeresa Johnson     auto &Summary = GlobalList.second.SummaryList[0];
78581bbf742STeresa Johnson     // Skip the summaries for the importing module. These are included to
78681bbf742STeresa Johnson     // e.g. record required linkage changes.
78781bbf742STeresa Johnson     if (Summary->modulePath() == ModulePath)
78881bbf742STeresa Johnson       continue;
789d68935c5STeresa Johnson     // Add an entry to provoke importing by thinBackend.
790d68935c5STeresa Johnson     ImportList[Summary->modulePath()].insert(GUID);
79181bbf742STeresa Johnson   }
79281bbf742STeresa Johnson #ifndef NDEBUG
79319e23874SEugene Leviant   dumpImportListForModule(Index, ModulePath, ImportList);
794c86af334STeresa Johnson #endif
795c86af334STeresa Johnson }
796c86af334STeresa Johnson 
79756584bbfSEvgeniy Stepanov void llvm::computeDeadSymbols(
79856584bbfSEvgeniy Stepanov     ModuleSummaryIndex &Index,
799eaf5172cSGeorge Rimar     const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
800eaf5172cSGeorge Rimar     function_ref<PrevailingType(GlobalValue::GUID)> isPrevailing) {
80156584bbfSEvgeniy Stepanov   assert(!Index.withGlobalValueDeadStripping());
8026c475a75STeresa Johnson   if (!ComputeDead)
80356584bbfSEvgeniy Stepanov     return;
8046c475a75STeresa Johnson   if (GUIDPreservedSymbols.empty())
8056c475a75STeresa Johnson     // Don't do anything when nothing is live, this is friendly with tests.
80656584bbfSEvgeniy Stepanov     return;
80756584bbfSEvgeniy Stepanov   unsigned LiveSymbols = 0;
8089667b91bSPeter Collingbourne   SmallVector<ValueInfo, 128> Worklist;
8099667b91bSPeter Collingbourne   Worklist.reserve(GUIDPreservedSymbols.size() * 2);
8109667b91bSPeter Collingbourne   for (auto GUID : GUIDPreservedSymbols) {
8119667b91bSPeter Collingbourne     ValueInfo VI = Index.getValueInfo(GUID);
8129667b91bSPeter Collingbourne     if (!VI)
8139667b91bSPeter Collingbourne       continue;
81456584bbfSEvgeniy Stepanov     for (auto &S : VI.getSummaryList())
81556584bbfSEvgeniy Stepanov       S->setLive(true);
8166c475a75STeresa Johnson   }
81756584bbfSEvgeniy Stepanov 
8186c475a75STeresa Johnson   // Add values flagged in the index as live roots to the worklist.
8197e7b13d0STeresa Johnson   for (const auto &Entry : Index) {
8207e7b13d0STeresa Johnson     auto VI = Index.getValueInfo(Entry);
82156584bbfSEvgeniy Stepanov     for (auto &S : Entry.second.SummaryList)
82256584bbfSEvgeniy Stepanov       if (S->isLive()) {
8237e7b13d0STeresa Johnson         LLVM_DEBUG(dbgs() << "Live root: " << VI << "\n");
8247e7b13d0STeresa Johnson         Worklist.push_back(VI);
82556584bbfSEvgeniy Stepanov         ++LiveSymbols;
82656584bbfSEvgeniy Stepanov         break;
8276c475a75STeresa Johnson       }
8287e7b13d0STeresa Johnson   }
8296c475a75STeresa Johnson 
83056584bbfSEvgeniy Stepanov   // Make value live and add it to the worklist if it was not live before.
831ea314fd4STeresa Johnson   auto visit = [&](ValueInfo VI, bool IsAliasee) {
8321958083dSTeresa Johnson     // FIXME: If we knew which edges were created for indirect call profiles,
8331958083dSTeresa Johnson     // we could skip them here. Any that are live should be reached via
8341958083dSTeresa Johnson     // other edges, e.g. reference edges. Otherwise, using a profile collected
8351958083dSTeresa Johnson     // on a slightly different binary might provoke preserving, importing
8361958083dSTeresa Johnson     // and ultimately promoting calls to functions not linked into this
8371958083dSTeresa Johnson     // binary, which increases the binary size unnecessarily. Note that
8381958083dSTeresa Johnson     // if this code changes, the importer needs to change so that edges
8391958083dSTeresa Johnson     // to functions marked dead are skipped.
8401958083dSTeresa Johnson     VI = updateValueInfoForIndirectCalls(Index, VI);
8411958083dSTeresa Johnson     if (!VI)
8421958083dSTeresa Johnson       return;
84333e3b4b9SXin Tong 
8445b2f6a1bSTeresa Johnson     if (llvm::any_of(VI.getSummaryList(),
84533e3b4b9SXin Tong                      [](const std::unique_ptr<llvm::GlobalValueSummary> &S) {
84633e3b4b9SXin Tong                        return S->isLive();
84733e3b4b9SXin Tong                      }))
848f625118eSTeresa Johnson       return;
849eaf5172cSGeorge Rimar 
850aab60006SVlad Tsyrklevich     // We only keep live symbols that are known to be non-prevailing if any are
851bfdad33bSXin Tong     // available_externally, linkonceodr, weakodr. Those symbols are discarded
852bfdad33bSXin Tong     // later in the EliminateAvailableExternally pass and setting them to
853bfdad33bSXin Tong     // not-live could break downstreams users of liveness information (PR36483)
854bfdad33bSXin Tong     // or limit optimization opportunities.
855aab60006SVlad Tsyrklevich     if (isPrevailing(VI.getGUID()) == PrevailingType::No) {
856bfdad33bSXin Tong       bool KeepAliveLinkage = false;
857aab60006SVlad Tsyrklevich       bool Interposable = false;
858aab60006SVlad Tsyrklevich       for (auto &S : VI.getSummaryList()) {
859bfdad33bSXin Tong         if (S->linkage() == GlobalValue::AvailableExternallyLinkage ||
860bfdad33bSXin Tong             S->linkage() == GlobalValue::WeakODRLinkage ||
861bfdad33bSXin Tong             S->linkage() == GlobalValue::LinkOnceODRLinkage)
862bfdad33bSXin Tong           KeepAliveLinkage = true;
863aab60006SVlad Tsyrklevich         else if (GlobalValue::isInterposableLinkage(S->linkage()))
864aab60006SVlad Tsyrklevich           Interposable = true;
865aab60006SVlad Tsyrklevich       }
866aab60006SVlad Tsyrklevich 
867ea314fd4STeresa Johnson       if (!IsAliasee) {
868bfdad33bSXin Tong         if (!KeepAliveLinkage)
869eaf5172cSGeorge Rimar           return;
870eaf5172cSGeorge Rimar 
871aab60006SVlad Tsyrklevich         if (Interposable)
872bfdad33bSXin Tong           report_fatal_error(
873ea314fd4STeresa Johnson               "Interposable and available_externally/linkonce_odr/weak_odr "
874ea314fd4STeresa Johnson               "symbol");
875ea314fd4STeresa Johnson       }
876aab60006SVlad Tsyrklevich     }
877aab60006SVlad Tsyrklevich 
878f625118eSTeresa Johnson     for (auto &S : VI.getSummaryList())
87956584bbfSEvgeniy Stepanov       S->setLive(true);
88056584bbfSEvgeniy Stepanov     ++LiveSymbols;
88156584bbfSEvgeniy Stepanov     Worklist.push_back(VI);
88256584bbfSEvgeniy Stepanov   };
88356584bbfSEvgeniy Stepanov 
8846c475a75STeresa Johnson   while (!Worklist.empty()) {
8859667b91bSPeter Collingbourne     auto VI = Worklist.pop_back_val();
8869667b91bSPeter Collingbourne     for (auto &Summary : VI.getSummaryList()) {
8875b2f6a1bSTeresa Johnson       if (auto *AS = dyn_cast<AliasSummary>(Summary.get())) {
8885b2f6a1bSTeresa Johnson         // If this is an alias, visit the aliasee VI to ensure that all copies
8895b2f6a1bSTeresa Johnson         // are marked live and it is added to the worklist for further
8905b2f6a1bSTeresa Johnson         // processing of its references.
891ea314fd4STeresa Johnson         visit(AS->getAliaseeVI(), true);
8925b2f6a1bSTeresa Johnson         continue;
8935b2f6a1bSTeresa Johnson       }
8945b2f6a1bSTeresa Johnson 
8955b2f6a1bSTeresa Johnson       Summary->setLive(true);
8965b2f6a1bSTeresa Johnson       for (auto Ref : Summary->refs())
897ea314fd4STeresa Johnson         visit(Ref, false);
8985b2f6a1bSTeresa Johnson       if (auto *FS = dyn_cast<FunctionSummary>(Summary.get()))
89956584bbfSEvgeniy Stepanov         for (auto Call : FS->calls())
900ea314fd4STeresa Johnson           visit(Call.first, false);
9016c475a75STeresa Johnson     }
9026c475a75STeresa Johnson   }
90356584bbfSEvgeniy Stepanov   Index.setWithGlobalValueDeadStripping();
90456584bbfSEvgeniy Stepanov 
90556584bbfSEvgeniy Stepanov   unsigned DeadSymbols = Index.size() - LiveSymbols;
906d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << LiveSymbols << " symbols Live, and " << DeadSymbols
90756584bbfSEvgeniy Stepanov                     << " symbols Dead \n");
90856584bbfSEvgeniy Stepanov   NumDeadSymbols += DeadSymbols;
90956584bbfSEvgeniy Stepanov   NumLiveSymbols += LiveSymbols;
9106c475a75STeresa Johnson }
9116c475a75STeresa Johnson 
912bf46e741SEugene Leviant // Compute dead symbols and propagate constants in combined index.
913bf46e741SEugene Leviant void llvm::computeDeadSymbolsWithConstProp(
914bf46e741SEugene Leviant     ModuleSummaryIndex &Index,
915bf46e741SEugene Leviant     const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
916bf46e741SEugene Leviant     function_ref<PrevailingType(GlobalValue::GUID)> isPrevailing,
917bf46e741SEugene Leviant     bool ImportEnabled) {
918bf46e741SEugene Leviant   computeDeadSymbols(Index, GUIDPreservedSymbols, isPrevailing);
91954a3c2a8STeresa Johnson   if (ImportEnabled)
9203aef3528SEugene Leviant     Index.propagateAttributes(GUIDPreservedSymbols);
921bf46e741SEugene Leviant }
922bf46e741SEugene Leviant 
92384174c37STeresa Johnson /// Compute the set of summaries needed for a ThinLTO backend compilation of
92484174c37STeresa Johnson /// \p ModulePath.
92584174c37STeresa Johnson void llvm::gatherImportedSummariesForModule(
92684174c37STeresa Johnson     StringRef ModulePath,
92784174c37STeresa Johnson     const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
928cdbcbf74SMehdi Amini     const FunctionImporter::ImportMapTy &ImportList,
92984174c37STeresa Johnson     std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
93084174c37STeresa Johnson   // Include all summaries from the importing module.
931adcd0268SBenjamin Kramer   ModuleToSummariesForIndex[std::string(ModulePath)] =
93284174c37STeresa Johnson       ModuleToDefinedGVSummaries.lookup(ModulePath);
93384174c37STeresa Johnson   // Include summaries for imports.
93488c491ddSMehdi Amini   for (auto &ILI : ImportList) {
935adcd0268SBenjamin Kramer     auto &SummariesForIndex =
936adcd0268SBenjamin Kramer         ModuleToSummariesForIndex[std::string(ILI.first())];
93784174c37STeresa Johnson     const auto &DefinedGVSummaries =
93884174c37STeresa Johnson         ModuleToDefinedGVSummaries.lookup(ILI.first());
93984174c37STeresa Johnson     for (auto &GI : ILI.second) {
940d68935c5STeresa Johnson       const auto &DS = DefinedGVSummaries.find(GI);
94184174c37STeresa Johnson       assert(DS != DefinedGVSummaries.end() &&
94284174c37STeresa Johnson              "Expected a defined summary for imported global value");
943d68935c5STeresa Johnson       SummariesForIndex[GI] = DS->second;
94484174c37STeresa Johnson     }
94584174c37STeresa Johnson   }
94684174c37STeresa Johnson }
94784174c37STeresa Johnson 
9488570fe47STeresa Johnson /// Emit the files \p ModulePath will import from into \p OutputFilename.
949c0320ef4STeresa Johnson std::error_code llvm::EmitImportsFiles(
950c0320ef4STeresa Johnson     StringRef ModulePath, StringRef OutputFilename,
951c0320ef4STeresa Johnson     const std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
9528570fe47STeresa Johnson   std::error_code EC;
953d9b948b6SFangrui Song   raw_fd_ostream ImportsOS(OutputFilename, EC, sys::fs::OpenFlags::OF_None);
9548570fe47STeresa Johnson   if (EC)
9558570fe47STeresa Johnson     return EC;
956c0320ef4STeresa Johnson   for (auto &ILI : ModuleToSummariesForIndex)
957c0320ef4STeresa Johnson     // The ModuleToSummariesForIndex map includes an entry for the current
958c0320ef4STeresa Johnson     // Module (needed for writing out the index files). We don't want to
959c0320ef4STeresa Johnson     // include it in the imports file, however, so filter it out.
960c0320ef4STeresa Johnson     if (ILI.first != ModulePath)
961c0320ef4STeresa Johnson       ImportsOS << ILI.first << "\n";
9628570fe47STeresa Johnson   return std::error_code();
9638570fe47STeresa Johnson }
9648570fe47STeresa Johnson 
9655a95c477STeresa Johnson bool llvm::convertToDeclaration(GlobalValue &GV) {
966d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "Converting to a declaration: `" << GV.getName()
967d34e60caSNicola Zaghen                     << "\n");
9684566c6dbSTeresa Johnson   if (Function *F = dyn_cast<Function>(&GV)) {
9694566c6dbSTeresa Johnson     F->deleteBody();
9704566c6dbSTeresa Johnson     F->clearMetadata();
9717873669bSPeter Collingbourne     F->setComdat(nullptr);
9724566c6dbSTeresa Johnson   } else if (GlobalVariable *V = dyn_cast<GlobalVariable>(&GV)) {
9734566c6dbSTeresa Johnson     V->setInitializer(nullptr);
9744566c6dbSTeresa Johnson     V->setLinkage(GlobalValue::ExternalLinkage);
9754566c6dbSTeresa Johnson     V->clearMetadata();
9767873669bSPeter Collingbourne     V->setComdat(nullptr);
9775a95c477STeresa Johnson   } else {
9785a95c477STeresa Johnson     GlobalValue *NewGV;
9795a95c477STeresa Johnson     if (GV.getValueType()->isFunctionTy())
9805a95c477STeresa Johnson       NewGV =
9815a95c477STeresa Johnson           Function::Create(cast<FunctionType>(GV.getValueType()),
982f920da00SDylan McKay                            GlobalValue::ExternalLinkage, GV.getAddressSpace(),
983f920da00SDylan McKay                            "", GV.getParent());
9845a95c477STeresa Johnson     else
9855a95c477STeresa Johnson       NewGV =
9865a95c477STeresa Johnson           new GlobalVariable(*GV.getParent(), GV.getValueType(),
9875a95c477STeresa Johnson                              /*isConstant*/ false, GlobalValue::ExternalLinkage,
9885a95c477STeresa Johnson                              /*init*/ nullptr, "",
9895a95c477STeresa Johnson                              /*insertbefore*/ nullptr, GV.getThreadLocalMode(),
9905a95c477STeresa Johnson                              GV.getType()->getAddressSpace());
9915a95c477STeresa Johnson     NewGV->takeName(&GV);
9925a95c477STeresa Johnson     GV.replaceAllUsesWith(NewGV);
9935a95c477STeresa Johnson     return false;
9945a95c477STeresa Johnson   }
9955a95c477STeresa Johnson   return true;
996eaf5172cSGeorge Rimar }
9974566c6dbSTeresa Johnson 
998e61652a3SPirama Arumuga Nainar /// Fixup prevailing symbol linkages in \p TheModule based on summary analysis.
999e61652a3SPirama Arumuga Nainar void llvm::thinLTOResolvePrevailingInModule(
1000eaf5172cSGeorge Rimar     Module &TheModule, const GVSummaryMapTy &DefinedGlobals) {
100104c9a2d6STeresa Johnson   auto updateLinkage = [&](GlobalValue &GV) {
100204c9a2d6STeresa Johnson     // See if the global summary analysis computed a new resolved linkage.
100304c9a2d6STeresa Johnson     const auto &GS = DefinedGlobals.find(GV.getGUID());
100404c9a2d6STeresa Johnson     if (GS == DefinedGlobals.end())
100504c9a2d6STeresa Johnson       return;
100604c9a2d6STeresa Johnson     auto NewLinkage = GS->second->linkage();
100704c9a2d6STeresa Johnson     if (NewLinkage == GV.getLinkage())
100804c9a2d6STeresa Johnson       return;
1009e61652a3SPirama Arumuga Nainar     if (GlobalValue::isLocalLinkage(GV.getLinkage()) ||
1010e5dd30f7SEugene Leviant         // Don't internalize anything here, because the code below
1011e5dd30f7SEugene Leviant         // lacks necessary correctness checks. Leave this job to
1012e5dd30f7SEugene Leviant         // LLVM 'internalize' pass.
10130f418677SEugene Leviant         GlobalValue::isLocalLinkage(NewLinkage) ||
1014e61652a3SPirama Arumuga Nainar         // In case it was dead and already converted to declaration.
1015e61652a3SPirama Arumuga Nainar         GV.isDeclaration())
10166a5fbe52SDavide Italiano       return;
1017ea314fd4STeresa Johnson 
10184566c6dbSTeresa Johnson     // Check for a non-prevailing def that has interposable linkage
10194566c6dbSTeresa Johnson     // (e.g. non-odr weak or linkonce). In that case we can't simply
10204566c6dbSTeresa Johnson     // convert to available_externally, since it would lose the
10214566c6dbSTeresa Johnson     // interposable property and possibly get inlined. Simply drop
10224566c6dbSTeresa Johnson     // the definition in that case.
10234566c6dbSTeresa Johnson     if (GlobalValue::isAvailableExternallyLinkage(NewLinkage) &&
10245a95c477STeresa Johnson         GlobalValue::isInterposableLinkage(GV.getLinkage())) {
10255a95c477STeresa Johnson       if (!convertToDeclaration(GV))
10265a95c477STeresa Johnson         // FIXME: Change this to collect replaced GVs and later erase
1027e61652a3SPirama Arumuga Nainar         // them from the parent module once thinLTOResolvePrevailingGUID is
10285a95c477STeresa Johnson         // changed to enable this for aliases.
10295a95c477STeresa Johnson         llvm_unreachable("Expected GV to be converted");
10305a95c477STeresa Johnson     } else {
103137b80122STeresa Johnson       // If all copies of the original symbol had global unnamed addr and
103237b80122STeresa Johnson       // linkonce_odr linkage, it should be an auto hide symbol. In that case
103337b80122STeresa Johnson       // the thin link would have marked it as CanAutoHide. Add hidden visibility
103437b80122STeresa Johnson       // to the symbol to preserve the property.
103537b80122STeresa Johnson       if (NewLinkage == GlobalValue::WeakODRLinkage &&
103637b80122STeresa Johnson           GS->second->canAutoHide()) {
103737b80122STeresa Johnson         assert(GV.hasLinkOnceODRLinkage() && GV.hasGlobalUnnamedAddr());
103833ba93c2SSteven Wu         GV.setVisibility(GlobalValue::HiddenVisibility);
103937b80122STeresa Johnson       }
104033ba93c2SSteven Wu 
1041d34e60caSNicola Zaghen       LLVM_DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName()
1042d34e60caSNicola Zaghen                         << "` from " << GV.getLinkage() << " to " << NewLinkage
1043d34e60caSNicola Zaghen                         << "\n");
104404c9a2d6STeresa Johnson       GV.setLinkage(NewLinkage);
10454566c6dbSTeresa Johnson     }
10464566c6dbSTeresa Johnson     // Remove declarations from comdats, including available_externally
10476107a419STeresa Johnson     // as this is a declaration for the linker, and will be dropped eventually.
10486107a419STeresa Johnson     // It is illegal for comdats to contain declarations.
10496107a419STeresa Johnson     auto *GO = dyn_cast_or_null<GlobalObject>(&GV);
10504566c6dbSTeresa Johnson     if (GO && GO->isDeclarationForLinker() && GO->hasComdat())
10516107a419STeresa Johnson       GO->setComdat(nullptr);
105204c9a2d6STeresa Johnson   };
105304c9a2d6STeresa Johnson 
105404c9a2d6STeresa Johnson   // Process functions and global now
105504c9a2d6STeresa Johnson   for (auto &GV : TheModule)
105604c9a2d6STeresa Johnson     updateLinkage(GV);
105704c9a2d6STeresa Johnson   for (auto &GV : TheModule.globals())
105804c9a2d6STeresa Johnson     updateLinkage(GV);
105904c9a2d6STeresa Johnson   for (auto &GV : TheModule.aliases())
106004c9a2d6STeresa Johnson     updateLinkage(GV);
106104c9a2d6STeresa Johnson }
106204c9a2d6STeresa Johnson 
106304c9a2d6STeresa Johnson /// Run internalization on \p TheModule based on symmary analysis.
106404c9a2d6STeresa Johnson void llvm::thinLTOInternalizeModule(Module &TheModule,
106504c9a2d6STeresa Johnson                                     const GVSummaryMapTy &DefinedGlobals) {
106604c9a2d6STeresa Johnson   // Declare a callback for the internalize pass that will ask for every
106704c9a2d6STeresa Johnson   // candidate GlobalValue if it can be internalized or not.
106804c9a2d6STeresa Johnson   auto MustPreserveGV = [&](const GlobalValue &GV) -> bool {
106904c9a2d6STeresa Johnson     // Lookup the linkage recorded in the summaries during global analysis.
1070c3d677f9SPeter Collingbourne     auto GS = DefinedGlobals.find(GV.getGUID());
107104c9a2d6STeresa Johnson     if (GS == DefinedGlobals.end()) {
107204c9a2d6STeresa Johnson       // Must have been promoted (possibly conservatively). Find original
107304c9a2d6STeresa Johnson       // name so that we can access the correct summary and see if it can
107404c9a2d6STeresa Johnson       // be internalized again.
107504c9a2d6STeresa Johnson       // FIXME: Eventually we should control promotion instead of promoting
107604c9a2d6STeresa Johnson       // and internalizing again.
107704c9a2d6STeresa Johnson       StringRef OrigName =
107804c9a2d6STeresa Johnson           ModuleSummaryIndex::getOriginalNameBeforePromote(GV.getName());
107904c9a2d6STeresa Johnson       std::string OrigId = GlobalValue::getGlobalIdentifier(
108004c9a2d6STeresa Johnson           OrigName, GlobalValue::InternalLinkage,
108104c9a2d6STeresa Johnson           TheModule.getSourceFileName());
1082c3d677f9SPeter Collingbourne       GS = DefinedGlobals.find(GlobalValue::getGUID(OrigId));
10837ab1f692STeresa Johnson       if (GS == DefinedGlobals.end()) {
10847ab1f692STeresa Johnson         // Also check the original non-promoted non-globalized name. In some
10857ab1f692STeresa Johnson         // cases a preempted weak value is linked in as a local copy because
10867ab1f692STeresa Johnson         // it is referenced by an alias (IRLinker::linkGlobalValueProto).
10877ab1f692STeresa Johnson         // In that case, since it was originally not a local value, it was
10887ab1f692STeresa Johnson         // recorded in the index using the original name.
10897ab1f692STeresa Johnson         // FIXME: This may not be needed once PR27866 is fixed.
1090c3d677f9SPeter Collingbourne         GS = DefinedGlobals.find(GlobalValue::getGUID(OrigName));
109104c9a2d6STeresa Johnson         assert(GS != DefinedGlobals.end());
10927ab1f692STeresa Johnson       }
1093c3d677f9SPeter Collingbourne     }
1094c3d677f9SPeter Collingbourne     return !GlobalValue::isLocalLinkage(GS->second->linkage());
109504c9a2d6STeresa Johnson   };
109604c9a2d6STeresa Johnson 
109704c9a2d6STeresa Johnson   // FIXME: See if we can just internalize directly here via linkage changes
109804c9a2d6STeresa Johnson   // based on the index, rather than invoking internalizeModule.
1099e9ea08a0SEugene Zelenko   internalizeModule(TheModule, MustPreserveGV);
110004c9a2d6STeresa Johnson }
110104c9a2d6STeresa Johnson 
110281bbf742STeresa Johnson /// Make alias a clone of its aliasee.
110381bbf742STeresa Johnson static Function *replaceAliasWithAliasee(Module *SrcModule, GlobalAlias *GA) {
110481bbf742STeresa Johnson   Function *Fn = cast<Function>(GA->getBaseObject());
110581bbf742STeresa Johnson 
110681bbf742STeresa Johnson   ValueToValueMapTy VMap;
110781bbf742STeresa Johnson   Function *NewFn = CloneFunction(Fn, VMap);
11085b2088d1STeresa Johnson   // Clone should use the original alias's linkage, visibility and name, and we
11095b2088d1STeresa Johnson   // ensure all uses of alias instead use the new clone (casted if necessary).
111081bbf742STeresa Johnson   NewFn->setLinkage(GA->getLinkage());
11115b2088d1STeresa Johnson   NewFn->setVisibility(GA->getVisibility());
111281bbf742STeresa Johnson   GA->replaceAllUsesWith(ConstantExpr::getBitCast(NewFn, GA->getType()));
111381bbf742STeresa Johnson   NewFn->takeName(GA);
111481bbf742STeresa Johnson   return NewFn;
111581bbf742STeresa Johnson }
111681bbf742STeresa Johnson 
1117bf46e741SEugene Leviant // Internalize values that we marked with specific attribute
1118bf46e741SEugene Leviant // in processGlobalForThinLTO.
11193aef3528SEugene Leviant static void internalizeGVsAfterImport(Module &M) {
1120bf46e741SEugene Leviant   for (auto &GV : M.globals())
1121bf46e741SEugene Leviant     // Skip GVs which have been converted to declarations
1122bf46e741SEugene Leviant     // by dropDeadSymbols.
1123bf46e741SEugene Leviant     if (!GV.isDeclaration() && GV.hasAttribute("thinlto-internalize")) {
1124bf46e741SEugene Leviant       GV.setLinkage(GlobalValue::InternalLinkage);
1125bf46e741SEugene Leviant       GV.setVisibility(GlobalValue::DefaultVisibility);
1126bf46e741SEugene Leviant     }
1127bf46e741SEugene Leviant }
1128bf46e741SEugene Leviant 
1129c8c55170SMehdi Amini // Automatically import functions in Module \p DestModule based on the summaries
1130c8c55170SMehdi Amini // index.
11317f00d0a1SPeter Collingbourne Expected<bool> FunctionImporter::importFunctions(
113266043797SAdrian Prantl     Module &DestModule, const FunctionImporter::ImportMapTy &ImportList) {
1133d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "Starting import for Module "
1134311fef6eSMehdi Amini                     << DestModule.getModuleIdentifier() << "\n");
113519e23874SEugene Leviant   unsigned ImportedCount = 0, ImportedGVCount = 0;
1136c8c55170SMehdi Amini 
11376d8f817fSPeter Collingbourne   IRMover Mover(DestModule);
11387e88d0daSMehdi Amini   // Do the actual import of functions now, one Module at a time
113901e32130SMehdi Amini   std::set<StringRef> ModuleNameOrderedList;
114001e32130SMehdi Amini   for (auto &FunctionsToImportPerModule : ImportList) {
114101e32130SMehdi Amini     ModuleNameOrderedList.insert(FunctionsToImportPerModule.first());
114201e32130SMehdi Amini   }
114301e32130SMehdi Amini   for (auto &Name : ModuleNameOrderedList) {
11447e88d0daSMehdi Amini     // Get the module for the import
114501e32130SMehdi Amini     const auto &FunctionsToImportPerModule = ImportList.find(Name);
114601e32130SMehdi Amini     assert(FunctionsToImportPerModule != ImportList.end());
1147d9445c49SPeter Collingbourne     Expected<std::unique_ptr<Module>> SrcModuleOrErr = ModuleLoader(Name);
1148d9445c49SPeter Collingbourne     if (!SrcModuleOrErr)
1149d9445c49SPeter Collingbourne       return SrcModuleOrErr.takeError();
1150d9445c49SPeter Collingbourne     std::unique_ptr<Module> SrcModule = std::move(*SrcModuleOrErr);
11517e88d0daSMehdi Amini     assert(&DestModule.getContext() == &SrcModule->getContext() &&
11527e88d0daSMehdi Amini            "Context mismatch");
11537e88d0daSMehdi Amini 
11546cba37ceSTeresa Johnson     // If modules were created with lazy metadata loading, materialize it
11556cba37ceSTeresa Johnson     // now, before linking it (otherwise this will be a noop).
11567f00d0a1SPeter Collingbourne     if (Error Err = SrcModule->materializeMetadata())
11577f00d0a1SPeter Collingbourne       return std::move(Err);
1158e5a61917STeresa Johnson 
115901e32130SMehdi Amini     auto &ImportGUIDs = FunctionsToImportPerModule->second;
116001e32130SMehdi Amini     // Find the globals to import
11616d8f817fSPeter Collingbourne     SetVector<GlobalValue *> GlobalsToImport;
11621f685e01SPiotr Padlewski     for (Function &F : *SrcModule) {
11631f685e01SPiotr Padlewski       if (!F.hasName())
11640beb858eSTeresa Johnson         continue;
11651f685e01SPiotr Padlewski       auto GUID = F.getGUID();
11660beb858eSTeresa Johnson       auto Import = ImportGUIDs.count(GUID);
1167d34e60caSNicola Zaghen       LLVM_DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing function "
1168d34e60caSNicola Zaghen                         << GUID << " " << F.getName() << " from "
1169aeb1e59bSMehdi Amini                         << SrcModule->getSourceFileName() << "\n");
11700beb858eSTeresa Johnson       if (Import) {
11717f00d0a1SPeter Collingbourne         if (Error Err = F.materialize())
11727f00d0a1SPeter Collingbourne           return std::move(Err);
11733b776128SPiotr Padlewski         if (EnableImportMetadata) {
11746deaa6afSPiotr Padlewski           // Add 'thinlto_src_module' metadata for statistics and debugging.
11753b776128SPiotr Padlewski           F.setMetadata(
11763b776128SPiotr Padlewski               "thinlto_src_module",
1177e9ea08a0SEugene Zelenko               MDNode::get(DestModule.getContext(),
1178e9ea08a0SEugene Zelenko                           {MDString::get(DestModule.getContext(),
11796deaa6afSPiotr Padlewski                                          SrcModule->getSourceFileName())}));
11803b776128SPiotr Padlewski         }
11811f685e01SPiotr Padlewski         GlobalsToImport.insert(&F);
118201e32130SMehdi Amini       }
118301e32130SMehdi Amini     }
11841f685e01SPiotr Padlewski     for (GlobalVariable &GV : SrcModule->globals()) {
11852d28f7aaSMehdi Amini       if (!GV.hasName())
11862d28f7aaSMehdi Amini         continue;
11872d28f7aaSMehdi Amini       auto GUID = GV.getGUID();
11882d28f7aaSMehdi Amini       auto Import = ImportGUIDs.count(GUID);
1189d34e60caSNicola Zaghen       LLVM_DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing global "
1190d34e60caSNicola Zaghen                         << GUID << " " << GV.getName() << " from "
1191aeb1e59bSMehdi Amini                         << SrcModule->getSourceFileName() << "\n");
11922d28f7aaSMehdi Amini       if (Import) {
11937f00d0a1SPeter Collingbourne         if (Error Err = GV.materialize())
11947f00d0a1SPeter Collingbourne           return std::move(Err);
119519e23874SEugene Leviant         ImportedGVCount += GlobalsToImport.insert(&GV);
11962d28f7aaSMehdi Amini       }
11972d28f7aaSMehdi Amini     }
11981f685e01SPiotr Padlewski     for (GlobalAlias &GA : SrcModule->aliases()) {
11991f685e01SPiotr Padlewski       if (!GA.hasName())
120001e32130SMehdi Amini         continue;
12011f685e01SPiotr Padlewski       auto GUID = GA.getGUID();
120281bbf742STeresa Johnson       auto Import = ImportGUIDs.count(GUID);
1203d34e60caSNicola Zaghen       LLVM_DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing alias "
1204d34e60caSNicola Zaghen                         << GUID << " " << GA.getName() << " from "
1205aeb1e59bSMehdi Amini                         << SrcModule->getSourceFileName() << "\n");
120681bbf742STeresa Johnson       if (Import) {
120781bbf742STeresa Johnson         if (Error Err = GA.materialize())
120881bbf742STeresa Johnson           return std::move(Err);
120981bbf742STeresa Johnson         // Import alias as a copy of its aliasee.
121081bbf742STeresa Johnson         GlobalObject *Base = GA.getBaseObject();
121181bbf742STeresa Johnson         if (Error Err = Base->materialize())
121281bbf742STeresa Johnson           return std::move(Err);
121381bbf742STeresa Johnson         auto *Fn = replaceAliasWithAliasee(SrcModule.get(), &GA);
1214d34e60caSNicola Zaghen         LLVM_DEBUG(dbgs() << "Is importing aliasee fn " << Base->getGUID()
121581bbf742STeresa Johnson                           << " " << Base->getName() << " from "
121681bbf742STeresa Johnson                           << SrcModule->getSourceFileName() << "\n");
121781bbf742STeresa Johnson         if (EnableImportMetadata) {
121881bbf742STeresa Johnson           // Add 'thinlto_src_module' metadata for statistics and debugging.
121981bbf742STeresa Johnson           Fn->setMetadata(
122081bbf742STeresa Johnson               "thinlto_src_module",
122181bbf742STeresa Johnson               MDNode::get(DestModule.getContext(),
122281bbf742STeresa Johnson                           {MDString::get(DestModule.getContext(),
122381bbf742STeresa Johnson                                          SrcModule->getSourceFileName())}));
122401e32130SMehdi Amini         }
122581bbf742STeresa Johnson         GlobalsToImport.insert(Fn);
122681bbf742STeresa Johnson       }
122781bbf742STeresa Johnson     }
122801e32130SMehdi Amini 
122919ef4fadSMehdi Amini     // Upgrade debug info after we're done materializing all the globals and we
123019ef4fadSMehdi Amini     // have loaded all the required metadata!
123119ef4fadSMehdi Amini     UpgradeDebugInfo(*SrcModule);
123219ef4fadSMehdi Amini 
12337e88d0daSMehdi Amini     // Link in the specified functions.
123401e32130SMehdi Amini     if (renameModuleForThinLTO(*SrcModule, Index, &GlobalsToImport))
12358d05185aSMehdi Amini       return true;
12368d05185aSMehdi Amini 
1237d29478f7STeresa Johnson     if (PrintImports) {
1238d29478f7STeresa Johnson       for (const auto *GV : GlobalsToImport)
1239d29478f7STeresa Johnson         dbgs() << DestModule.getSourceFileName() << ": Import " << GV->getName()
1240d29478f7STeresa Johnson                << " from " << SrcModule->getSourceFileName() << "\n";
1241d29478f7STeresa Johnson     }
1242d29478f7STeresa Johnson 
12436d8f817fSPeter Collingbourne     if (Mover.move(std::move(SrcModule), GlobalsToImport.getArrayRef(),
12446d8f817fSPeter Collingbourne                    [](GlobalValue &, IRMover::ValueAdder) {},
1245e6fd9ff9SPeter Collingbourne                    /*IsPerformingImport=*/true))
12467e88d0daSMehdi Amini       report_fatal_error("Function Import: link error");
12477e88d0daSMehdi Amini 
124801e32130SMehdi Amini     ImportedCount += GlobalsToImport.size();
12496c475a75STeresa Johnson     NumImportedModules++;
12507e88d0daSMehdi Amini   }
1251e5a61917STeresa Johnson 
12523aef3528SEugene Leviant   internalizeGVsAfterImport(DestModule);
1253bf46e741SEugene Leviant 
125419e23874SEugene Leviant   NumImportedFunctions += (ImportedCount - ImportedGVCount);
125519e23874SEugene Leviant   NumImportedGlobalVars += ImportedGVCount;
1256d29478f7STeresa Johnson 
1257d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "Imported " << ImportedCount - ImportedGVCount
1258d34e60caSNicola Zaghen                     << " functions for Module "
1259d34e60caSNicola Zaghen                     << DestModule.getModuleIdentifier() << "\n");
1260d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "Imported " << ImportedGVCount
126119e23874SEugene Leviant                     << " global variables for Module "
1262c8c55170SMehdi Amini                     << DestModule.getModuleIdentifier() << "\n");
1263c8c55170SMehdi Amini   return ImportedCount;
126442418abaSMehdi Amini }
126542418abaSMehdi Amini 
1266598bd2a2SPeter Collingbourne static bool doImportingForModule(Module &M) {
1267598bd2a2SPeter Collingbourne   if (SummaryFile.empty())
1268598bd2a2SPeter Collingbourne     report_fatal_error("error: -function-import requires -summary-file\n");
12696de481a3SPeter Collingbourne   Expected<std::unique_ptr<ModuleSummaryIndex>> IndexPtrOrErr =
12706de481a3SPeter Collingbourne       getModuleSummaryIndexForFile(SummaryFile);
12716de481a3SPeter Collingbourne   if (!IndexPtrOrErr) {
12726de481a3SPeter Collingbourne     logAllUnhandledErrors(IndexPtrOrErr.takeError(), errs(),
12736de481a3SPeter Collingbourne                           "Error loading file '" + SummaryFile + "': ");
127442418abaSMehdi Amini     return false;
127542418abaSMehdi Amini   }
1276598bd2a2SPeter Collingbourne   std::unique_ptr<ModuleSummaryIndex> Index = std::move(*IndexPtrOrErr);
127742418abaSMehdi Amini 
1278c86af334STeresa Johnson   // First step is collecting the import list.
1279c86af334STeresa Johnson   FunctionImporter::ImportMapTy ImportList;
128081bbf742STeresa Johnson   // If requested, simply import all functions in the index. This is used
128181bbf742STeresa Johnson   // when testing distributed backend handling via the opt tool, when
128281bbf742STeresa Johnson   // we have distributed indexes containing exactly the summaries to import.
128381bbf742STeresa Johnson   if (ImportAllIndex)
128481bbf742STeresa Johnson     ComputeCrossModuleImportForModuleFromIndex(M.getModuleIdentifier(), *Index,
128581bbf742STeresa Johnson                                                ImportList);
128681bbf742STeresa Johnson   else
1287c86af334STeresa Johnson     ComputeCrossModuleImportForModule(M.getModuleIdentifier(), *Index,
1288c86af334STeresa Johnson                                       ImportList);
128901e32130SMehdi Amini 
12904fef68cbSTeresa Johnson   // Conservatively mark all internal values as promoted. This interface is
12914fef68cbSTeresa Johnson   // only used when doing importing via the function importing pass. The pass
12924fef68cbSTeresa Johnson   // is only enabled when testing importing via the 'opt' tool, which does
12934fef68cbSTeresa Johnson   // not do the ThinLink that would normally determine what values to promote.
12944fef68cbSTeresa Johnson   for (auto &I : *Index) {
12959667b91bSPeter Collingbourne     for (auto &S : I.second.SummaryList) {
12964fef68cbSTeresa Johnson       if (GlobalValue::isLocalLinkage(S->linkage()))
12974fef68cbSTeresa Johnson         S->setLinkage(GlobalValue::ExternalLinkage);
12984fef68cbSTeresa Johnson     }
12994fef68cbSTeresa Johnson   }
13004fef68cbSTeresa Johnson 
130101e32130SMehdi Amini   // Next we need to promote to global scope and rename any local values that
13021b00f2d9STeresa Johnson   // are potentially exported to other modules.
130301e32130SMehdi Amini   if (renameModuleForThinLTO(M, *Index, nullptr)) {
13041b00f2d9STeresa Johnson     errs() << "Error renaming module\n";
13051b00f2d9STeresa Johnson     return false;
13061b00f2d9STeresa Johnson   }
13071b00f2d9STeresa Johnson 
130842418abaSMehdi Amini   // Perform the import now.
1309d16c8065SMehdi Amini   auto ModuleLoader = [&M](StringRef Identifier) {
1310adcd0268SBenjamin Kramer     return loadFile(std::string(Identifier), M.getContext());
1311d16c8065SMehdi Amini   };
13129d2bfc48SRafael Espindola   FunctionImporter Importer(*Index, ModuleLoader);
131337e24591SPeter Collingbourne   Expected<bool> Result = Importer.importFunctions(M, ImportList);
13147f00d0a1SPeter Collingbourne 
13157f00d0a1SPeter Collingbourne   // FIXME: Probably need to propagate Errors through the pass manager.
13167f00d0a1SPeter Collingbourne   if (!Result) {
13177f00d0a1SPeter Collingbourne     logAllUnhandledErrors(Result.takeError(), errs(),
13187f00d0a1SPeter Collingbourne                           "Error importing module: ");
13197f00d0a1SPeter Collingbourne     return false;
13207f00d0a1SPeter Collingbourne   }
13217f00d0a1SPeter Collingbourne 
13227f00d0a1SPeter Collingbourne   return *Result;
132321241571STeresa Johnson }
132421241571STeresa Johnson 
132521241571STeresa Johnson namespace {
1326e9ea08a0SEugene Zelenko 
132721241571STeresa Johnson /// Pass that performs cross-module function import provided a summary file.
132821241571STeresa Johnson class FunctionImportLegacyPass : public ModulePass {
132921241571STeresa Johnson public:
133021241571STeresa Johnson   /// Pass identification, replacement for typeid
133121241571STeresa Johnson   static char ID;
133221241571STeresa Johnson 
1333e9ea08a0SEugene Zelenko   explicit FunctionImportLegacyPass() : ModulePass(ID) {}
1334e9ea08a0SEugene Zelenko 
133521241571STeresa Johnson   /// Specify pass name for debug output
1336117296c0SMehdi Amini   StringRef getPassName() const override { return "Function Importing"; }
133721241571STeresa Johnson 
133821241571STeresa Johnson   bool runOnModule(Module &M) override {
133921241571STeresa Johnson     if (skipModule(M))
134021241571STeresa Johnson       return false;
134121241571STeresa Johnson 
1342598bd2a2SPeter Collingbourne     return doImportingForModule(M);
134342418abaSMehdi Amini   }
134442418abaSMehdi Amini };
1345e9ea08a0SEugene Zelenko 
1346e9ea08a0SEugene Zelenko } // end anonymous namespace
134742418abaSMehdi Amini 
134821241571STeresa Johnson PreservedAnalyses FunctionImportPass::run(Module &M,
1349fd03ac6aSSean Silva                                           ModuleAnalysisManager &AM) {
1350598bd2a2SPeter Collingbourne   if (!doImportingForModule(M))
135121241571STeresa Johnson     return PreservedAnalyses::all();
135221241571STeresa Johnson 
135321241571STeresa Johnson   return PreservedAnalyses::none();
135421241571STeresa Johnson }
135521241571STeresa Johnson 
135621241571STeresa Johnson char FunctionImportLegacyPass::ID = 0;
135721241571STeresa Johnson INITIALIZE_PASS(FunctionImportLegacyPass, "function-import",
135842418abaSMehdi Amini                 "Summary Based Function Import", false, false)
135942418abaSMehdi Amini 
136042418abaSMehdi Amini namespace llvm {
1361e9ea08a0SEugene Zelenko 
1362598bd2a2SPeter Collingbourne Pass *createFunctionImportPass() {
1363598bd2a2SPeter Collingbourne   return new FunctionImportLegacyPass();
13645fcbdb71STeresa Johnson }
1365e9ea08a0SEugene Zelenko 
1366e9ea08a0SEugene Zelenko } // end namespace llvm
1367