142418abaSMehdi Amini //===- FunctionImport.cpp - ThinLTO Summary-based Function Import ---------===//
242418abaSMehdi Amini //
342418abaSMehdi Amini //                     The LLVM Compiler Infrastructure
442418abaSMehdi Amini //
542418abaSMehdi Amini // This file is distributed under the University of Illinois Open Source
642418abaSMehdi Amini // License. See LICENSE.TXT for details.
742418abaSMehdi Amini //
842418abaSMehdi Amini //===----------------------------------------------------------------------===//
942418abaSMehdi Amini //
1042418abaSMehdi Amini // This file implements Function import based on summaries.
1142418abaSMehdi Amini //
1242418abaSMehdi Amini //===----------------------------------------------------------------------===//
1342418abaSMehdi Amini 
1442418abaSMehdi Amini #include "llvm/Transforms/IPO/FunctionImport.h"
1542418abaSMehdi Amini 
1601e32130SMehdi Amini #include "llvm/ADT/SmallVector.h"
17d29478f7STeresa Johnson #include "llvm/ADT/Statistic.h"
1842418abaSMehdi Amini #include "llvm/ADT/StringSet.h"
1904c9a2d6STeresa Johnson #include "llvm/ADT/Triple.h"
20c15d60b7SPeter Collingbourne #include "llvm/Bitcode/BitcodeReader.h"
2142418abaSMehdi Amini #include "llvm/IR/AutoUpgrade.h"
2242418abaSMehdi Amini #include "llvm/IR/DiagnosticPrinter.h"
2342418abaSMehdi Amini #include "llvm/IR/IntrinsicInst.h"
2442418abaSMehdi Amini #include "llvm/IR/Module.h"
25fc06b83eSMehdi Amini #include "llvm/IR/Verifier.h"
2642418abaSMehdi Amini #include "llvm/IRReader/IRReader.h"
2742418abaSMehdi Amini #include "llvm/Linker/Linker.h"
2804c9a2d6STeresa Johnson #include "llvm/Object/IRObjectFile.h"
2942418abaSMehdi Amini #include "llvm/Support/CommandLine.h"
3042418abaSMehdi Amini #include "llvm/Support/Debug.h"
3142418abaSMehdi Amini #include "llvm/Support/SourceMgr.h"
3204c9a2d6STeresa Johnson #include "llvm/Transforms/IPO/Internalize.h"
33488a800aSTeresa Johnson #include "llvm/Transforms/Utils/FunctionImportUtils.h"
347e88d0daSMehdi Amini 
3501e32130SMehdi Amini #define DEBUG_TYPE "function-import"
367e88d0daSMehdi Amini 
3742418abaSMehdi Amini using namespace llvm;
3842418abaSMehdi Amini 
396c475a75STeresa Johnson STATISTIC(NumImportedFunctions, "Number of functions imported");
406c475a75STeresa Johnson STATISTIC(NumImportedModules, "Number of modules imported from");
416c475a75STeresa Johnson STATISTIC(NumDeadSymbols, "Number of dead stripped symbols in index");
426c475a75STeresa Johnson STATISTIC(NumLiveSymbols, "Number of live symbols in index");
43d29478f7STeresa Johnson 
4439303619STeresa Johnson /// Limit on instruction count of imported functions.
4539303619STeresa Johnson static cl::opt<unsigned> ImportInstrLimit(
4639303619STeresa Johnson     "import-instr-limit", cl::init(100), cl::Hidden, cl::value_desc("N"),
4739303619STeresa Johnson     cl::desc("Only import functions with less than N instructions"));
4839303619STeresa Johnson 
4940641748SMehdi Amini static cl::opt<float>
5040641748SMehdi Amini     ImportInstrFactor("import-instr-evolution-factor", cl::init(0.7),
5140641748SMehdi Amini                       cl::Hidden, cl::value_desc("x"),
5240641748SMehdi Amini                       cl::desc("As we import functions, multiply the "
5340641748SMehdi Amini                                "`import-instr-limit` threshold by this factor "
5440641748SMehdi Amini                                "before processing newly imported functions"));
55ba72b95fSPiotr Padlewski 
56d2869473SPiotr Padlewski static cl::opt<float> ImportHotInstrFactor(
57d2869473SPiotr Padlewski     "import-hot-evolution-factor", cl::init(1.0), cl::Hidden,
58d2869473SPiotr Padlewski     cl::value_desc("x"),
59d2869473SPiotr Padlewski     cl::desc("As we import functions called from hot callsite, multiply the "
60d2869473SPiotr Padlewski              "`import-instr-limit` threshold by this factor "
61d2869473SPiotr Padlewski              "before processing newly imported functions"));
62d2869473SPiotr Padlewski 
63d9830eb7SPiotr Padlewski static cl::opt<float> ImportHotMultiplier(
648260d665SDehao Chen     "import-hot-multiplier", cl::init(10.0), cl::Hidden, cl::value_desc("x"),
65ba72b95fSPiotr Padlewski     cl::desc("Multiply the `import-instr-limit` threshold for hot callsites"));
66ba72b95fSPiotr Padlewski 
6764c46574SDehao Chen static cl::opt<float> ImportCriticalMultiplier(
6864c46574SDehao Chen     "import-critical-multiplier", cl::init(100.0), cl::Hidden,
6964c46574SDehao Chen     cl::value_desc("x"),
7064c46574SDehao Chen     cl::desc(
7164c46574SDehao Chen         "Multiply the `import-instr-limit` threshold for critical callsites"));
7264c46574SDehao Chen 
73ba72b95fSPiotr Padlewski // FIXME: This multiplier was not really tuned up.
74ba72b95fSPiotr Padlewski static cl::opt<float> ImportColdMultiplier(
75ba72b95fSPiotr Padlewski     "import-cold-multiplier", cl::init(0), cl::Hidden, cl::value_desc("N"),
76ba72b95fSPiotr Padlewski     cl::desc("Multiply the `import-instr-limit` threshold for cold callsites"));
7740641748SMehdi Amini 
78d29478f7STeresa Johnson static cl::opt<bool> PrintImports("print-imports", cl::init(false), cl::Hidden,
79d29478f7STeresa Johnson                                   cl::desc("Print imported functions"));
80d29478f7STeresa Johnson 
816c475a75STeresa Johnson static cl::opt<bool> ComputeDead("compute-dead", cl::init(true), cl::Hidden,
826c475a75STeresa Johnson                                  cl::desc("Compute dead symbols"));
836c475a75STeresa Johnson 
843b776128SPiotr Padlewski static cl::opt<bool> EnableImportMetadata(
853b776128SPiotr Padlewski     "enable-import-metadata", cl::init(
863b776128SPiotr Padlewski #if !defined(NDEBUG)
873b776128SPiotr Padlewski                                   true /*Enabled with asserts.*/
883b776128SPiotr Padlewski #else
893b776128SPiotr Padlewski                                   false
903b776128SPiotr Padlewski #endif
913b776128SPiotr Padlewski                                   ),
923b776128SPiotr Padlewski     cl::Hidden, cl::desc("Enable import metadata like 'thinlto_src_module'"));
933b776128SPiotr Padlewski 
9442418abaSMehdi Amini // Load lazily a module from \p FileName in \p Context.
9542418abaSMehdi Amini static std::unique_ptr<Module> loadFile(const std::string &FileName,
9642418abaSMehdi Amini                                         LLVMContext &Context) {
9742418abaSMehdi Amini   SMDiagnostic Err;
9842418abaSMehdi Amini   DEBUG(dbgs() << "Loading '" << FileName << "'\n");
996cba37ceSTeresa Johnson   // Metadata isn't loaded until functions are imported, to minimize
1006cba37ceSTeresa Johnson   // the memory overhead.
101a1080ee6STeresa Johnson   std::unique_ptr<Module> Result =
102a1080ee6STeresa Johnson       getLazyIRFileModule(FileName, Err, Context,
103a1080ee6STeresa Johnson                           /* ShouldLazyLoadMetadata = */ true);
10442418abaSMehdi Amini   if (!Result) {
10542418abaSMehdi Amini     Err.print("function-import", errs());
106d7ad221cSMehdi Amini     report_fatal_error("Abort");
10742418abaSMehdi Amini   }
10842418abaSMehdi Amini 
10942418abaSMehdi Amini   return Result;
11042418abaSMehdi Amini }
11142418abaSMehdi Amini 
1127e88d0daSMehdi Amini namespace {
11340641748SMehdi Amini 
11401e32130SMehdi Amini /// Given a list of possible callee implementation for a call site, select one
11501e32130SMehdi Amini /// that fits the \p Threshold.
11601e32130SMehdi Amini ///
11701e32130SMehdi Amini /// FIXME: select "best" instead of first that fits. But what is "best"?
11801e32130SMehdi Amini /// - The smallest: more likely to be inlined.
11901e32130SMehdi Amini /// - The one with the least outgoing edges (already well optimized).
12001e32130SMehdi Amini /// - One from a module already being imported from in order to reduce the
12101e32130SMehdi Amini ///   number of source modules parsed/linked.
12201e32130SMehdi Amini /// - One that has PGO data attached.
12301e32130SMehdi Amini /// - [insert you fancy metric here]
1242d28f7aaSMehdi Amini static const GlobalValueSummary *
125b4e1e829SMehdi Amini selectCallee(const ModuleSummaryIndex &Index,
1269667b91bSPeter Collingbourne              ArrayRef<std::unique_ptr<GlobalValueSummary>> CalleeSummaryList,
12783aaf358STeresa Johnson              unsigned Threshold, StringRef CallerModulePath) {
12801e32130SMehdi Amini   auto It = llvm::find_if(
12928e457bcSTeresa Johnson       CalleeSummaryList,
13028e457bcSTeresa Johnson       [&](const std::unique_ptr<GlobalValueSummary> &SummaryPtr) {
13128e457bcSTeresa Johnson         auto *GVSummary = SummaryPtr.get();
13273305f82STeresa Johnson         // For SamplePGO, in computeImportForFunction the OriginalId
13373305f82STeresa Johnson         // may have been used to locate the callee summary list (See
13473305f82STeresa Johnson         // comment there).
13573305f82STeresa Johnson         // The mapping from OriginalId to GUID may return a GUID
13673305f82STeresa Johnson         // that corresponds to a static variable. Filter it out here.
13773305f82STeresa Johnson         // This can happen when
13873305f82STeresa Johnson         // 1) There is a call to a library function which is not defined
13973305f82STeresa Johnson         // in the index.
14073305f82STeresa Johnson         // 2) There is a static variable with the  OriginalGUID identical
14173305f82STeresa Johnson         // to the GUID of the library function in 1);
14273305f82STeresa Johnson         // When this happens, the logic for SamplePGO kicks in and
14373305f82STeresa Johnson         // the static variable in 2) will be found, which needs to be
14473305f82STeresa Johnson         // filtered out.
14573305f82STeresa Johnson         if (GVSummary->getSummaryKind() == GlobalValueSummary::GlobalVarKind)
14673305f82STeresa Johnson           return false;
147f329be83SRafael Espindola         if (GlobalValue::isInterposableLinkage(GVSummary->linkage()))
1485b85d8d6SMehdi Amini           // There is no point in importing these, we can't inline them
14901e32130SMehdi Amini           return false;
15082c7d376SDavide Italiano         if (isa<AliasSummary>(GVSummary))
1512f0cc477SDavid Blaikie           // Aliases can't point to "available_externally".
1522c719cc1SMehdi Amini           // FIXME: we should import alias as available_externally *function*,
1532f0cc477SDavid Blaikie           // the destination module does not need to know it is an alias.
1542c719cc1SMehdi Amini           return false;
1552c719cc1SMehdi Amini 
1562c719cc1SMehdi Amini         auto *Summary = cast<FunctionSummary>(GVSummary);
1577e88d0daSMehdi Amini 
15883aaf358STeresa Johnson         // If this is a local function, make sure we import the copy
15983aaf358STeresa Johnson         // in the caller's module. The only time a local function can
16083aaf358STeresa Johnson         // share an entry in the index is if there is a local with the same name
16183aaf358STeresa Johnson         // in another module that had the same source file name (in a different
16283aaf358STeresa Johnson         // directory), where each was compiled in their own directory so there
16383aaf358STeresa Johnson         // was not distinguishing path.
16483aaf358STeresa Johnson         // However, do the import from another module if there is only one
16583aaf358STeresa Johnson         // entry in the list - in that case this must be a reference due
16683aaf358STeresa Johnson         // to indirect call profile data, since a function pointer can point to
16783aaf358STeresa Johnson         // a local in another module.
16883aaf358STeresa Johnson         if (GlobalValue::isLocalLinkage(Summary->linkage()) &&
16983aaf358STeresa Johnson             CalleeSummaryList.size() > 1 &&
17083aaf358STeresa Johnson             Summary->modulePath() != CallerModulePath)
17183aaf358STeresa Johnson           return false;
17283aaf358STeresa Johnson 
173f9dc3deaSTeresa Johnson         if (Summary->instCount() > Threshold)
174f9dc3deaSTeresa Johnson           return false;
175f9dc3deaSTeresa Johnson 
176519465b9STeresa Johnson         if (Summary->notEligibleToImport())
177b4e1e829SMehdi Amini           return false;
178b4e1e829SMehdi Amini 
17901e32130SMehdi Amini         return true;
18001e32130SMehdi Amini       });
18128e457bcSTeresa Johnson   if (It == CalleeSummaryList.end())
18201e32130SMehdi Amini     return nullptr;
1837e88d0daSMehdi Amini 
184f9dc3deaSTeresa Johnson   return cast<GlobalValueSummary>(It->get());
185434e9561SRafael Espindola }
1867e88d0daSMehdi Amini 
187475b51a7STeresa Johnson using EdgeInfo = std::tuple<const FunctionSummary *, unsigned /* Threshold */,
188475b51a7STeresa Johnson                             GlobalValue::GUID>;
18901e32130SMehdi Amini 
1901958083dSTeresa Johnson static ValueInfo
1911958083dSTeresa Johnson updateValueInfoForIndirectCalls(const ModuleSummaryIndex &Index, ValueInfo VI) {
1921958083dSTeresa Johnson   if (!VI.getSummaryList().empty())
1931958083dSTeresa Johnson     return VI;
1941958083dSTeresa Johnson   // For SamplePGO, the indirect call targets for local functions will
1951958083dSTeresa Johnson   // have its original name annotated in profile. We try to find the
1961958083dSTeresa Johnson   // corresponding PGOFuncName as the GUID.
1971958083dSTeresa Johnson   // FIXME: Consider updating the edges in the graph after building
1981958083dSTeresa Johnson   // it, rather than needing to perform this mapping on each walk.
1991958083dSTeresa Johnson   auto GUID = Index.getGUIDFromOriginalID(VI.getGUID());
2001958083dSTeresa Johnson   if (GUID == 0)
2011958083dSTeresa Johnson     return nullptr;
2021958083dSTeresa Johnson   return Index.getValueInfo(GUID);
2031958083dSTeresa Johnson }
2041958083dSTeresa Johnson 
20501e32130SMehdi Amini /// Compute the list of functions to import for a given caller. Mark these
20601e32130SMehdi Amini /// imported functions and the symbols they reference in their source module as
20701e32130SMehdi Amini /// exported from their source module.
20801e32130SMehdi Amini static void computeImportForFunction(
2093255eec1STeresa Johnson     const FunctionSummary &Summary, const ModuleSummaryIndex &Index,
210d9830eb7SPiotr Padlewski     const unsigned Threshold, const GVSummaryMapTy &DefinedGVSummaries,
21101e32130SMehdi Amini     SmallVectorImpl<EdgeInfo> &Worklist,
2129b490f10SMehdi Amini     FunctionImporter::ImportMapTy &ImportList,
213c86af334STeresa Johnson     StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) {
21401e32130SMehdi Amini   for (auto &Edge : Summary.calls()) {
2159667b91bSPeter Collingbourne     ValueInfo VI = Edge.first;
2169667b91bSPeter Collingbourne     DEBUG(dbgs() << " edge -> " << VI.getGUID() << " Threshold:" << Threshold
2179667b91bSPeter Collingbourne                  << "\n");
21801e32130SMehdi Amini 
2191958083dSTeresa Johnson     VI = updateValueInfoForIndirectCalls(Index, VI);
2209667b91bSPeter Collingbourne     if (!VI)
2219667b91bSPeter Collingbourne       continue;
2224a435e08SDehao Chen 
2239667b91bSPeter Collingbourne     if (DefinedGVSummaries.count(VI.getGUID())) {
22401e32130SMehdi Amini       DEBUG(dbgs() << "ignored! Target already in destination module.\n");
2257e88d0daSMehdi Amini       continue;
226d450da32STeresa Johnson     }
22740641748SMehdi Amini 
228ba72b95fSPiotr Padlewski     auto GetBonusMultiplier = [](CalleeInfo::HotnessType Hotness) -> float {
229ba72b95fSPiotr Padlewski       if (Hotness == CalleeInfo::HotnessType::Hot)
230ba72b95fSPiotr Padlewski         return ImportHotMultiplier;
231ba72b95fSPiotr Padlewski       if (Hotness == CalleeInfo::HotnessType::Cold)
232ba72b95fSPiotr Padlewski         return ImportColdMultiplier;
23364c46574SDehao Chen       if (Hotness == CalleeInfo::HotnessType::Critical)
23464c46574SDehao Chen         return ImportCriticalMultiplier;
235ba72b95fSPiotr Padlewski       return 1.0;
236ba72b95fSPiotr Padlewski     };
237ba72b95fSPiotr Padlewski 
238d9830eb7SPiotr Padlewski     const auto NewThreshold =
239ba72b95fSPiotr Padlewski         Threshold * GetBonusMultiplier(Edge.second.Hotness);
240d2869473SPiotr Padlewski 
2419667b91bSPeter Collingbourne     auto *CalleeSummary = selectCallee(Index, VI.getSummaryList(), NewThreshold,
2429667b91bSPeter Collingbourne                                        Summary.modulePath());
24301e32130SMehdi Amini     if (!CalleeSummary) {
24401e32130SMehdi Amini       DEBUG(dbgs() << "ignored! No qualifying callee with summary found.\n");
2457e88d0daSMehdi Amini       continue;
2467e88d0daSMehdi Amini     }
2472f0cc477SDavid Blaikie 
2482f0cc477SDavid Blaikie     // "Resolve" the summary
2492f0cc477SDavid Blaikie     assert(!isa<AliasSummary>(CalleeSummary) &&
2502f0cc477SDavid Blaikie            "Unexpected alias in import list");
2512f0cc477SDavid Blaikie     const auto *ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary);
2522d28f7aaSMehdi Amini 
253d9830eb7SPiotr Padlewski     assert(ResolvedCalleeSummary->instCount() <= NewThreshold &&
25401e32130SMehdi Amini            "selectCallee() didn't honor the threshold");
25501e32130SMehdi Amini 
256d2869473SPiotr Padlewski     auto GetAdjustedThreshold = [](unsigned Threshold, bool IsHotCallsite) {
257d2869473SPiotr Padlewski       // Adjust the threshold for next level of imported functions.
258d2869473SPiotr Padlewski       // The threshold is different for hot callsites because we can then
259d2869473SPiotr Padlewski       // inline chains of hot calls.
260d2869473SPiotr Padlewski       if (IsHotCallsite)
261d2869473SPiotr Padlewski         return Threshold * ImportHotInstrFactor;
262d2869473SPiotr Padlewski       return Threshold * ImportInstrFactor;
263d2869473SPiotr Padlewski     };
264d2869473SPiotr Padlewski 
265d2869473SPiotr Padlewski     bool IsHotCallsite = Edge.second.Hotness == CalleeInfo::HotnessType::Hot;
2661b859a23STeresa Johnson     const auto AdjThreshold = GetAdjustedThreshold(Threshold, IsHotCallsite);
2671b859a23STeresa Johnson 
2681b859a23STeresa Johnson     auto ExportModulePath = ResolvedCalleeSummary->modulePath();
2699667b91bSPeter Collingbourne     auto &ProcessedThreshold = ImportList[ExportModulePath][VI.getGUID()];
2701b859a23STeresa Johnson     /// Since the traversal of the call graph is DFS, we can revisit a function
2711b859a23STeresa Johnson     /// a second time with a higher threshold. In this case, it is added back to
2721b859a23STeresa Johnson     /// the worklist with the new threshold.
2731b859a23STeresa Johnson     if (ProcessedThreshold && ProcessedThreshold >= AdjThreshold) {
2741b859a23STeresa Johnson       DEBUG(dbgs() << "ignored! Target was already seen with Threshold "
2751b859a23STeresa Johnson                    << ProcessedThreshold << "\n");
2761b859a23STeresa Johnson       continue;
2771b859a23STeresa Johnson     }
27819f2aa78STeresa Johnson     bool PreviouslyImported = ProcessedThreshold != 0;
2791b859a23STeresa Johnson     // Mark this function as imported in this module, with the current Threshold
2801b859a23STeresa Johnson     ProcessedThreshold = AdjThreshold;
2811b859a23STeresa Johnson 
2821b859a23STeresa Johnson     // Make exports in the source module.
2831b859a23STeresa Johnson     if (ExportLists) {
2841b859a23STeresa Johnson       auto &ExportList = (*ExportLists)[ExportModulePath];
2859667b91bSPeter Collingbourne       ExportList.insert(VI.getGUID());
28619f2aa78STeresa Johnson       if (!PreviouslyImported) {
28719f2aa78STeresa Johnson         // This is the first time this function was exported from its source
28819f2aa78STeresa Johnson         // module, so mark all functions and globals it references as exported
2891b859a23STeresa Johnson         // to the outside if they are defined in the same source module.
290edddca22STeresa Johnson         // For efficiency, we unconditionally add all the referenced GUIDs
291edddca22STeresa Johnson         // to the ExportList for this module, and will prune out any not
292edddca22STeresa Johnson         // defined in the module later in a single pass.
2931b859a23STeresa Johnson         for (auto &Edge : ResolvedCalleeSummary->calls()) {
2941b859a23STeresa Johnson           auto CalleeGUID = Edge.first.getGUID();
295edddca22STeresa Johnson           ExportList.insert(CalleeGUID);
2961b859a23STeresa Johnson         }
2971b859a23STeresa Johnson         for (auto &Ref : ResolvedCalleeSummary->refs()) {
2981b859a23STeresa Johnson           auto GUID = Ref.getGUID();
299edddca22STeresa Johnson           ExportList.insert(GUID);
3001b859a23STeresa Johnson         }
3011b859a23STeresa Johnson       }
30219f2aa78STeresa Johnson     }
303d2869473SPiotr Padlewski 
30401e32130SMehdi Amini     // Insert the newly imported function to the worklist.
3059667b91bSPeter Collingbourne     Worklist.emplace_back(ResolvedCalleeSummary, AdjThreshold, VI.getGUID());
306d450da32STeresa Johnson   }
307d450da32STeresa Johnson }
308d450da32STeresa Johnson 
30901e32130SMehdi Amini /// Given the list of globals defined in a module, compute the list of imports
31001e32130SMehdi Amini /// as well as the list of "exports", i.e. the list of symbols referenced from
31101e32130SMehdi Amini /// another module (that may require promotion).
31201e32130SMehdi Amini static void ComputeImportForModule(
313c851d216STeresa Johnson     const GVSummaryMapTy &DefinedGVSummaries, const ModuleSummaryIndex &Index,
3149b490f10SMehdi Amini     FunctionImporter::ImportMapTy &ImportList,
31556584bbfSEvgeniy Stepanov     StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) {
31601e32130SMehdi Amini   // Worklist contains the list of function imported in this module, for which
31701e32130SMehdi Amini   // we will analyse the callees and may import further down the callgraph.
31801e32130SMehdi Amini   SmallVector<EdgeInfo, 128> Worklist;
31901e32130SMehdi Amini 
32001e32130SMehdi Amini   // Populate the worklist with the import for the functions in the current
32101e32130SMehdi Amini   // module
32228e457bcSTeresa Johnson   for (auto &GVSummary : DefinedGVSummaries) {
32356584bbfSEvgeniy Stepanov     if (!Index.isGlobalValueLive(GVSummary.second)) {
3246c475a75STeresa Johnson       DEBUG(dbgs() << "Ignores Dead GUID: " << GVSummary.first << "\n");
3256c475a75STeresa Johnson       continue;
3266c475a75STeresa Johnson     }
327*cfbd0892SPeter Collingbourne     auto *FuncSummary =
328*cfbd0892SPeter Collingbourne         dyn_cast<FunctionSummary>(GVSummary.second->getBaseObject());
3291aafabf7SMehdi Amini     if (!FuncSummary)
3301aafabf7SMehdi Amini       // Skip import for global variables
3311aafabf7SMehdi Amini       continue;
33224524f31SXinliang David Li     DEBUG(dbgs() << "Initialize import for " << GVSummary.first << "\n");
3332d28f7aaSMehdi Amini     computeImportForFunction(*FuncSummary, Index, ImportInstrLimit,
3349b490f10SMehdi Amini                              DefinedGVSummaries, Worklist, ImportList,
33501e32130SMehdi Amini                              ExportLists);
33601e32130SMehdi Amini   }
33701e32130SMehdi Amini 
338d2869473SPiotr Padlewski   // Process the newly imported functions and add callees to the worklist.
33942418abaSMehdi Amini   while (!Worklist.empty()) {
34001e32130SMehdi Amini     auto FuncInfo = Worklist.pop_back_val();
341475b51a7STeresa Johnson     auto *Summary = std::get<0>(FuncInfo);
342475b51a7STeresa Johnson     auto Threshold = std::get<1>(FuncInfo);
343475b51a7STeresa Johnson     auto GUID = std::get<2>(FuncInfo);
344475b51a7STeresa Johnson 
345475b51a7STeresa Johnson     // Check if we later added this summary with a higher threshold.
346475b51a7STeresa Johnson     // If so, skip this entry.
347475b51a7STeresa Johnson     auto ExportModulePath = Summary->modulePath();
348475b51a7STeresa Johnson     auto &LatestProcessedThreshold = ImportList[ExportModulePath][GUID];
349475b51a7STeresa Johnson     if (LatestProcessedThreshold > Threshold)
350475b51a7STeresa Johnson       continue;
35142418abaSMehdi Amini 
3521aafabf7SMehdi Amini     computeImportForFunction(*Summary, Index, Threshold, DefinedGVSummaries,
3539b490f10SMehdi Amini                              Worklist, ImportList, ExportLists);
354c8c55170SMehdi Amini   }
35542418abaSMehdi Amini }
356ffe2e4aaSMehdi Amini 
35701e32130SMehdi Amini } // anonymous namespace
35801e32130SMehdi Amini 
359c86af334STeresa Johnson /// Compute all the import and export for every module using the Index.
36001e32130SMehdi Amini void llvm::ComputeCrossModuleImport(
36101e32130SMehdi Amini     const ModuleSummaryIndex &Index,
362c851d216STeresa Johnson     const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
36301e32130SMehdi Amini     StringMap<FunctionImporter::ImportMapTy> &ImportLists,
36456584bbfSEvgeniy Stepanov     StringMap<FunctionImporter::ExportSetTy> &ExportLists) {
36501e32130SMehdi Amini   // For each module that has function defined, compute the import/export lists.
3661aafabf7SMehdi Amini   for (auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) {
3679b490f10SMehdi Amini     auto &ImportList = ImportLists[DefinedGVSummaries.first()];
3681aafabf7SMehdi Amini     DEBUG(dbgs() << "Computing import for Module '"
3691aafabf7SMehdi Amini                  << DefinedGVSummaries.first() << "'\n");
3709b490f10SMehdi Amini     ComputeImportForModule(DefinedGVSummaries.second, Index, ImportList,
37156584bbfSEvgeniy Stepanov                            &ExportLists);
37201e32130SMehdi Amini   }
37301e32130SMehdi Amini 
374edddca22STeresa Johnson   // When computing imports we added all GUIDs referenced by anything
375edddca22STeresa Johnson   // imported from the module to its ExportList. Now we prune each ExportList
376edddca22STeresa Johnson   // of any not defined in that module. This is more efficient than checking
377edddca22STeresa Johnson   // while computing imports because some of the summary lists may be long
378edddca22STeresa Johnson   // due to linkonce (comdat) copies.
379edddca22STeresa Johnson   for (auto &ELI : ExportLists) {
380edddca22STeresa Johnson     const auto &DefinedGVSummaries =
381edddca22STeresa Johnson         ModuleToDefinedGVSummaries.lookup(ELI.first());
382edddca22STeresa Johnson     for (auto EI = ELI.second.begin(); EI != ELI.second.end();) {
383edddca22STeresa Johnson       if (!DefinedGVSummaries.count(*EI))
384edddca22STeresa Johnson         EI = ELI.second.erase(EI);
385edddca22STeresa Johnson       else
386edddca22STeresa Johnson         ++EI;
387edddca22STeresa Johnson     }
388edddca22STeresa Johnson   }
389edddca22STeresa Johnson 
39001e32130SMehdi Amini #ifndef NDEBUG
39101e32130SMehdi Amini   DEBUG(dbgs() << "Import/Export lists for " << ImportLists.size()
39201e32130SMehdi Amini                << " modules:\n");
39301e32130SMehdi Amini   for (auto &ModuleImports : ImportLists) {
39401e32130SMehdi Amini     auto ModName = ModuleImports.first();
39501e32130SMehdi Amini     auto &Exports = ExportLists[ModName];
39601e32130SMehdi Amini     DEBUG(dbgs() << "* Module " << ModName << " exports " << Exports.size()
39701e32130SMehdi Amini                  << " functions. Imports from " << ModuleImports.second.size()
39801e32130SMehdi Amini                  << " modules.\n");
39901e32130SMehdi Amini     for (auto &Src : ModuleImports.second) {
40001e32130SMehdi Amini       auto SrcModName = Src.first();
40101e32130SMehdi Amini       DEBUG(dbgs() << " - " << Src.second.size() << " functions imported from "
40201e32130SMehdi Amini                    << SrcModName << "\n");
40301e32130SMehdi Amini     }
40401e32130SMehdi Amini   }
40501e32130SMehdi Amini #endif
40601e32130SMehdi Amini }
40701e32130SMehdi Amini 
408c86af334STeresa Johnson /// Compute all the imports for the given module in the Index.
409c86af334STeresa Johnson void llvm::ComputeCrossModuleImportForModule(
410c86af334STeresa Johnson     StringRef ModulePath, const ModuleSummaryIndex &Index,
411c86af334STeresa Johnson     FunctionImporter::ImportMapTy &ImportList) {
412c86af334STeresa Johnson 
413c86af334STeresa Johnson   // Collect the list of functions this module defines.
414c86af334STeresa Johnson   // GUID -> Summary
415c851d216STeresa Johnson   GVSummaryMapTy FunctionSummaryMap;
41628e457bcSTeresa Johnson   Index.collectDefinedFunctionsForModule(ModulePath, FunctionSummaryMap);
417c86af334STeresa Johnson 
418c86af334STeresa Johnson   // Compute the import list for this module.
419c86af334STeresa Johnson   DEBUG(dbgs() << "Computing import for Module '" << ModulePath << "'\n");
42028e457bcSTeresa Johnson   ComputeImportForModule(FunctionSummaryMap, Index, ImportList);
421c86af334STeresa Johnson 
422c86af334STeresa Johnson #ifndef NDEBUG
423c86af334STeresa Johnson   DEBUG(dbgs() << "* Module " << ModulePath << " imports from "
424c86af334STeresa Johnson                << ImportList.size() << " modules.\n");
425c86af334STeresa Johnson   for (auto &Src : ImportList) {
426c86af334STeresa Johnson     auto SrcModName = Src.first();
427c86af334STeresa Johnson     DEBUG(dbgs() << " - " << Src.second.size() << " functions imported from "
428c86af334STeresa Johnson                  << SrcModName << "\n");
429c86af334STeresa Johnson   }
430c86af334STeresa Johnson #endif
431c86af334STeresa Johnson }
432c86af334STeresa Johnson 
43356584bbfSEvgeniy Stepanov void llvm::computeDeadSymbols(
43456584bbfSEvgeniy Stepanov     ModuleSummaryIndex &Index,
4356c475a75STeresa Johnson     const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
43656584bbfSEvgeniy Stepanov   assert(!Index.withGlobalValueDeadStripping());
4376c475a75STeresa Johnson   if (!ComputeDead)
43856584bbfSEvgeniy Stepanov     return;
4396c475a75STeresa Johnson   if (GUIDPreservedSymbols.empty())
4406c475a75STeresa Johnson     // Don't do anything when nothing is live, this is friendly with tests.
44156584bbfSEvgeniy Stepanov     return;
44256584bbfSEvgeniy Stepanov   unsigned LiveSymbols = 0;
4439667b91bSPeter Collingbourne   SmallVector<ValueInfo, 128> Worklist;
4449667b91bSPeter Collingbourne   Worklist.reserve(GUIDPreservedSymbols.size() * 2);
4459667b91bSPeter Collingbourne   for (auto GUID : GUIDPreservedSymbols) {
4469667b91bSPeter Collingbourne     ValueInfo VI = Index.getValueInfo(GUID);
4479667b91bSPeter Collingbourne     if (!VI)
4489667b91bSPeter Collingbourne       continue;
44956584bbfSEvgeniy Stepanov     for (auto &S : VI.getSummaryList())
45056584bbfSEvgeniy Stepanov       S->setLive(true);
4516c475a75STeresa Johnson   }
45256584bbfSEvgeniy Stepanov 
4536c475a75STeresa Johnson   // Add values flagged in the index as live roots to the worklist.
45456584bbfSEvgeniy Stepanov   for (const auto &Entry : Index)
45556584bbfSEvgeniy Stepanov     for (auto &S : Entry.second.SummaryList)
45656584bbfSEvgeniy Stepanov       if (S->isLive()) {
45756584bbfSEvgeniy Stepanov         DEBUG(dbgs() << "Live root: " << Entry.first << "\n");
4589667b91bSPeter Collingbourne         Worklist.push_back(ValueInfo(&Entry));
45956584bbfSEvgeniy Stepanov         ++LiveSymbols;
46056584bbfSEvgeniy Stepanov         break;
4616c475a75STeresa Johnson       }
4626c475a75STeresa Johnson 
46356584bbfSEvgeniy Stepanov   // Make value live and add it to the worklist if it was not live before.
46456584bbfSEvgeniy Stepanov   // FIXME: we should only make the prevailing copy live here
46556584bbfSEvgeniy Stepanov   auto visit = [&](ValueInfo VI) {
46656584bbfSEvgeniy Stepanov     for (auto &S : VI.getSummaryList())
46756584bbfSEvgeniy Stepanov       if (S->isLive())
46856584bbfSEvgeniy Stepanov         return;
4691958083dSTeresa Johnson     // FIXME: If we knew which edges were created for indirect call profiles,
4701958083dSTeresa Johnson     // we could skip them here. Any that are live should be reached via
4711958083dSTeresa Johnson     // other edges, e.g. reference edges. Otherwise, using a profile collected
4721958083dSTeresa Johnson     // on a slightly different binary might provoke preserving, importing
4731958083dSTeresa Johnson     // and ultimately promoting calls to functions not linked into this
4741958083dSTeresa Johnson     // binary, which increases the binary size unnecessarily. Note that
4751958083dSTeresa Johnson     // if this code changes, the importer needs to change so that edges
4761958083dSTeresa Johnson     // to functions marked dead are skipped.
4771958083dSTeresa Johnson     VI = updateValueInfoForIndirectCalls(Index, VI);
4781958083dSTeresa Johnson     if (!VI)
4791958083dSTeresa Johnson       return;
48056584bbfSEvgeniy Stepanov     for (auto &S : VI.getSummaryList())
48156584bbfSEvgeniy Stepanov       S->setLive(true);
48256584bbfSEvgeniy Stepanov     ++LiveSymbols;
48356584bbfSEvgeniy Stepanov     Worklist.push_back(VI);
48456584bbfSEvgeniy Stepanov   };
48556584bbfSEvgeniy Stepanov 
4866c475a75STeresa Johnson   while (!Worklist.empty()) {
4879667b91bSPeter Collingbourne     auto VI = Worklist.pop_back_val();
4889667b91bSPeter Collingbourne     for (auto &Summary : VI.getSummaryList()) {
489*cfbd0892SPeter Collingbourne       GlobalValueSummary *Base = Summary->getBaseObject();
490*cfbd0892SPeter Collingbourne       for (auto Ref : Base->refs())
49156584bbfSEvgeniy Stepanov         visit(Ref);
492*cfbd0892SPeter Collingbourne       if (auto *FS = dyn_cast<FunctionSummary>(Base))
49356584bbfSEvgeniy Stepanov         for (auto Call : FS->calls())
49456584bbfSEvgeniy Stepanov           visit(Call.first);
4956c475a75STeresa Johnson     }
4966c475a75STeresa Johnson   }
49756584bbfSEvgeniy Stepanov   Index.setWithGlobalValueDeadStripping();
49856584bbfSEvgeniy Stepanov 
49956584bbfSEvgeniy Stepanov   unsigned DeadSymbols = Index.size() - LiveSymbols;
50056584bbfSEvgeniy Stepanov   DEBUG(dbgs() << LiveSymbols << " symbols Live, and " << DeadSymbols
50156584bbfSEvgeniy Stepanov                << " symbols Dead \n");
50256584bbfSEvgeniy Stepanov   NumDeadSymbols += DeadSymbols;
50356584bbfSEvgeniy Stepanov   NumLiveSymbols += LiveSymbols;
5046c475a75STeresa Johnson }
5056c475a75STeresa Johnson 
50684174c37STeresa Johnson /// Compute the set of summaries needed for a ThinLTO backend compilation of
50784174c37STeresa Johnson /// \p ModulePath.
50884174c37STeresa Johnson void llvm::gatherImportedSummariesForModule(
50984174c37STeresa Johnson     StringRef ModulePath,
51084174c37STeresa Johnson     const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
511cdbcbf74SMehdi Amini     const FunctionImporter::ImportMapTy &ImportList,
51284174c37STeresa Johnson     std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
51384174c37STeresa Johnson   // Include all summaries from the importing module.
51484174c37STeresa Johnson   ModuleToSummariesForIndex[ModulePath] =
51584174c37STeresa Johnson       ModuleToDefinedGVSummaries.lookup(ModulePath);
51684174c37STeresa Johnson   // Include summaries for imports.
51788c491ddSMehdi Amini   for (auto &ILI : ImportList) {
51884174c37STeresa Johnson     auto &SummariesForIndex = ModuleToSummariesForIndex[ILI.first()];
51984174c37STeresa Johnson     const auto &DefinedGVSummaries =
52084174c37STeresa Johnson         ModuleToDefinedGVSummaries.lookup(ILI.first());
52184174c37STeresa Johnson     for (auto &GI : ILI.second) {
52284174c37STeresa Johnson       const auto &DS = DefinedGVSummaries.find(GI.first);
52384174c37STeresa Johnson       assert(DS != DefinedGVSummaries.end() &&
52484174c37STeresa Johnson              "Expected a defined summary for imported global value");
52584174c37STeresa Johnson       SummariesForIndex[GI.first] = DS->second;
52684174c37STeresa Johnson     }
52784174c37STeresa Johnson   }
52884174c37STeresa Johnson }
52984174c37STeresa Johnson 
5308570fe47STeresa Johnson /// Emit the files \p ModulePath will import from into \p OutputFilename.
531cdbcbf74SMehdi Amini std::error_code
532cdbcbf74SMehdi Amini llvm::EmitImportsFiles(StringRef ModulePath, StringRef OutputFilename,
533cdbcbf74SMehdi Amini                        const FunctionImporter::ImportMapTy &ModuleImports) {
5348570fe47STeresa Johnson   std::error_code EC;
5358570fe47STeresa Johnson   raw_fd_ostream ImportsOS(OutputFilename, EC, sys::fs::OpenFlags::F_None);
5368570fe47STeresa Johnson   if (EC)
5378570fe47STeresa Johnson     return EC;
538cdbcbf74SMehdi Amini   for (auto &ILI : ModuleImports)
5398570fe47STeresa Johnson     ImportsOS << ILI.first() << "\n";
5408570fe47STeresa Johnson   return std::error_code();
5418570fe47STeresa Johnson }
5428570fe47STeresa Johnson 
54304c9a2d6STeresa Johnson /// Fixup WeakForLinker linkages in \p TheModule based on summary analysis.
54404c9a2d6STeresa Johnson void llvm::thinLTOResolveWeakForLinkerModule(
54504c9a2d6STeresa Johnson     Module &TheModule, const GVSummaryMapTy &DefinedGlobals) {
5464566c6dbSTeresa Johnson   auto ConvertToDeclaration = [](GlobalValue &GV) {
5474566c6dbSTeresa Johnson     DEBUG(dbgs() << "Converting to a declaration: `" << GV.getName() << "\n");
5484566c6dbSTeresa Johnson     if (Function *F = dyn_cast<Function>(&GV)) {
5494566c6dbSTeresa Johnson       F->deleteBody();
5504566c6dbSTeresa Johnson       F->clearMetadata();
5514566c6dbSTeresa Johnson     } else if (GlobalVariable *V = dyn_cast<GlobalVariable>(&GV)) {
5524566c6dbSTeresa Johnson       V->setInitializer(nullptr);
5534566c6dbSTeresa Johnson       V->setLinkage(GlobalValue::ExternalLinkage);
5544566c6dbSTeresa Johnson       V->clearMetadata();
5554566c6dbSTeresa Johnson     } else
5564566c6dbSTeresa Johnson       // For now we don't resolve or drop aliases. Once we do we'll
5574566c6dbSTeresa Johnson       // need to add support here for creating either a function or
5584566c6dbSTeresa Johnson       // variable declaration, and return the new GlobalValue* for
5594566c6dbSTeresa Johnson       // the caller to use.
56091239088SDavide Italiano       llvm_unreachable("Expected function or variable");
5614566c6dbSTeresa Johnson   };
5624566c6dbSTeresa Johnson 
56304c9a2d6STeresa Johnson   auto updateLinkage = [&](GlobalValue &GV) {
56404c9a2d6STeresa Johnson     // See if the global summary analysis computed a new resolved linkage.
56504c9a2d6STeresa Johnson     const auto &GS = DefinedGlobals.find(GV.getGUID());
56604c9a2d6STeresa Johnson     if (GS == DefinedGlobals.end())
56704c9a2d6STeresa Johnson       return;
56804c9a2d6STeresa Johnson     auto NewLinkage = GS->second->linkage();
56904c9a2d6STeresa Johnson     if (NewLinkage == GV.getLinkage())
57004c9a2d6STeresa Johnson       return;
5716a5fbe52SDavide Italiano 
5726a5fbe52SDavide Italiano     // Switch the linkage to weakany if asked for, e.g. we do this for
5736a5fbe52SDavide Italiano     // linker redefined symbols (via --wrap or --defsym).
574f4891d29SDavide Italiano     // We record that the visibility should be changed here in `addThinLTO`
575f4891d29SDavide Italiano     // as we need access to the resolution vectors for each input file in
576f4891d29SDavide Italiano     // order to find which symbols have been redefined.
577f4891d29SDavide Italiano     // We may consider reorganizing this code and moving the linkage recording
578f4891d29SDavide Italiano     // somewhere else, e.g. in thinLTOResolveWeakForLinkerInIndex.
5796a5fbe52SDavide Italiano     if (NewLinkage == GlobalValue::WeakAnyLinkage) {
5806a5fbe52SDavide Italiano       GV.setLinkage(NewLinkage);
5816a5fbe52SDavide Italiano       return;
5826a5fbe52SDavide Italiano     }
5836a5fbe52SDavide Italiano 
5846a5fbe52SDavide Italiano     if (!GlobalValue::isWeakForLinker(GV.getLinkage()))
5856a5fbe52SDavide Italiano       return;
5864566c6dbSTeresa Johnson     // Check for a non-prevailing def that has interposable linkage
5874566c6dbSTeresa Johnson     // (e.g. non-odr weak or linkonce). In that case we can't simply
5884566c6dbSTeresa Johnson     // convert to available_externally, since it would lose the
5894566c6dbSTeresa Johnson     // interposable property and possibly get inlined. Simply drop
5904566c6dbSTeresa Johnson     // the definition in that case.
5914566c6dbSTeresa Johnson     if (GlobalValue::isAvailableExternallyLinkage(NewLinkage) &&
5924566c6dbSTeresa Johnson         GlobalValue::isInterposableLinkage(GV.getLinkage()))
5934566c6dbSTeresa Johnson       ConvertToDeclaration(GV);
5944566c6dbSTeresa Johnson     else {
59504c9a2d6STeresa Johnson       DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName() << "` from "
59604c9a2d6STeresa Johnson                    << GV.getLinkage() << " to " << NewLinkage << "\n");
59704c9a2d6STeresa Johnson       GV.setLinkage(NewLinkage);
5984566c6dbSTeresa Johnson     }
5994566c6dbSTeresa Johnson     // Remove declarations from comdats, including available_externally
6006107a419STeresa Johnson     // as this is a declaration for the linker, and will be dropped eventually.
6016107a419STeresa Johnson     // It is illegal for comdats to contain declarations.
6026107a419STeresa Johnson     auto *GO = dyn_cast_or_null<GlobalObject>(&GV);
6034566c6dbSTeresa Johnson     if (GO && GO->isDeclarationForLinker() && GO->hasComdat())
6046107a419STeresa Johnson       GO->setComdat(nullptr);
60504c9a2d6STeresa Johnson   };
60604c9a2d6STeresa Johnson 
60704c9a2d6STeresa Johnson   // Process functions and global now
60804c9a2d6STeresa Johnson   for (auto &GV : TheModule)
60904c9a2d6STeresa Johnson     updateLinkage(GV);
61004c9a2d6STeresa Johnson   for (auto &GV : TheModule.globals())
61104c9a2d6STeresa Johnson     updateLinkage(GV);
61204c9a2d6STeresa Johnson   for (auto &GV : TheModule.aliases())
61304c9a2d6STeresa Johnson     updateLinkage(GV);
61404c9a2d6STeresa Johnson }
61504c9a2d6STeresa Johnson 
61604c9a2d6STeresa Johnson /// Run internalization on \p TheModule based on symmary analysis.
61704c9a2d6STeresa Johnson void llvm::thinLTOInternalizeModule(Module &TheModule,
61804c9a2d6STeresa Johnson                                     const GVSummaryMapTy &DefinedGlobals) {
61904c9a2d6STeresa Johnson   // Parse inline ASM and collect the list of symbols that are not defined in
62004c9a2d6STeresa Johnson   // the current module.
62104c9a2d6STeresa Johnson   StringSet<> AsmUndefinedRefs;
622863cbfbeSPeter Collingbourne   ModuleSymbolTable::CollectAsmSymbols(
623d8204472STeresa Johnson       TheModule,
62404c9a2d6STeresa Johnson       [&AsmUndefinedRefs](StringRef Name, object::BasicSymbolRef::Flags Flags) {
62504c9a2d6STeresa Johnson         if (Flags & object::BasicSymbolRef::SF_Undefined)
62604c9a2d6STeresa Johnson           AsmUndefinedRefs.insert(Name);
62704c9a2d6STeresa Johnson       });
62804c9a2d6STeresa Johnson 
62904c9a2d6STeresa Johnson   // Declare a callback for the internalize pass that will ask for every
63004c9a2d6STeresa Johnson   // candidate GlobalValue if it can be internalized or not.
63104c9a2d6STeresa Johnson   auto MustPreserveGV = [&](const GlobalValue &GV) -> bool {
63204c9a2d6STeresa Johnson     // Can't be internalized if referenced in inline asm.
63304c9a2d6STeresa Johnson     if (AsmUndefinedRefs.count(GV.getName()))
63404c9a2d6STeresa Johnson       return true;
63504c9a2d6STeresa Johnson 
63604c9a2d6STeresa Johnson     // Lookup the linkage recorded in the summaries during global analysis.
637c3d677f9SPeter Collingbourne     auto GS = DefinedGlobals.find(GV.getGUID());
63804c9a2d6STeresa Johnson     if (GS == DefinedGlobals.end()) {
63904c9a2d6STeresa Johnson       // Must have been promoted (possibly conservatively). Find original
64004c9a2d6STeresa Johnson       // name so that we can access the correct summary and see if it can
64104c9a2d6STeresa Johnson       // be internalized again.
64204c9a2d6STeresa Johnson       // FIXME: Eventually we should control promotion instead of promoting
64304c9a2d6STeresa Johnson       // and internalizing again.
64404c9a2d6STeresa Johnson       StringRef OrigName =
64504c9a2d6STeresa Johnson           ModuleSummaryIndex::getOriginalNameBeforePromote(GV.getName());
64604c9a2d6STeresa Johnson       std::string OrigId = GlobalValue::getGlobalIdentifier(
64704c9a2d6STeresa Johnson           OrigName, GlobalValue::InternalLinkage,
64804c9a2d6STeresa Johnson           TheModule.getSourceFileName());
649c3d677f9SPeter Collingbourne       GS = DefinedGlobals.find(GlobalValue::getGUID(OrigId));
6507ab1f692STeresa Johnson       if (GS == DefinedGlobals.end()) {
6517ab1f692STeresa Johnson         // Also check the original non-promoted non-globalized name. In some
6527ab1f692STeresa Johnson         // cases a preempted weak value is linked in as a local copy because
6537ab1f692STeresa Johnson         // it is referenced by an alias (IRLinker::linkGlobalValueProto).
6547ab1f692STeresa Johnson         // In that case, since it was originally not a local value, it was
6557ab1f692STeresa Johnson         // recorded in the index using the original name.
6567ab1f692STeresa Johnson         // FIXME: This may not be needed once PR27866 is fixed.
657c3d677f9SPeter Collingbourne         GS = DefinedGlobals.find(GlobalValue::getGUID(OrigName));
65804c9a2d6STeresa Johnson         assert(GS != DefinedGlobals.end());
6597ab1f692STeresa Johnson       }
660c3d677f9SPeter Collingbourne     }
661c3d677f9SPeter Collingbourne     return !GlobalValue::isLocalLinkage(GS->second->linkage());
66204c9a2d6STeresa Johnson   };
66304c9a2d6STeresa Johnson 
66404c9a2d6STeresa Johnson   // FIXME: See if we can just internalize directly here via linkage changes
66504c9a2d6STeresa Johnson   // based on the index, rather than invoking internalizeModule.
66604c9a2d6STeresa Johnson   llvm::internalizeModule(TheModule, MustPreserveGV);
66704c9a2d6STeresa Johnson }
66804c9a2d6STeresa Johnson 
669c8c55170SMehdi Amini // Automatically import functions in Module \p DestModule based on the summaries
670c8c55170SMehdi Amini // index.
671c8c55170SMehdi Amini //
6727f00d0a1SPeter Collingbourne Expected<bool> FunctionImporter::importFunctions(
67366043797SAdrian Prantl     Module &DestModule, const FunctionImporter::ImportMapTy &ImportList) {
6745411d051SMehdi Amini   DEBUG(dbgs() << "Starting import for Module "
675311fef6eSMehdi Amini                << DestModule.getModuleIdentifier() << "\n");
676c8c55170SMehdi Amini   unsigned ImportedCount = 0;
677c8c55170SMehdi Amini 
6786d8f817fSPeter Collingbourne   IRMover Mover(DestModule);
6797e88d0daSMehdi Amini   // Do the actual import of functions now, one Module at a time
68001e32130SMehdi Amini   std::set<StringRef> ModuleNameOrderedList;
68101e32130SMehdi Amini   for (auto &FunctionsToImportPerModule : ImportList) {
68201e32130SMehdi Amini     ModuleNameOrderedList.insert(FunctionsToImportPerModule.first());
68301e32130SMehdi Amini   }
68401e32130SMehdi Amini   for (auto &Name : ModuleNameOrderedList) {
6857e88d0daSMehdi Amini     // Get the module for the import
68601e32130SMehdi Amini     const auto &FunctionsToImportPerModule = ImportList.find(Name);
68701e32130SMehdi Amini     assert(FunctionsToImportPerModule != ImportList.end());
688d9445c49SPeter Collingbourne     Expected<std::unique_ptr<Module>> SrcModuleOrErr = ModuleLoader(Name);
689d9445c49SPeter Collingbourne     if (!SrcModuleOrErr)
690d9445c49SPeter Collingbourne       return SrcModuleOrErr.takeError();
691d9445c49SPeter Collingbourne     std::unique_ptr<Module> SrcModule = std::move(*SrcModuleOrErr);
6927e88d0daSMehdi Amini     assert(&DestModule.getContext() == &SrcModule->getContext() &&
6937e88d0daSMehdi Amini            "Context mismatch");
6947e88d0daSMehdi Amini 
6956cba37ceSTeresa Johnson     // If modules were created with lazy metadata loading, materialize it
6966cba37ceSTeresa Johnson     // now, before linking it (otherwise this will be a noop).
6977f00d0a1SPeter Collingbourne     if (Error Err = SrcModule->materializeMetadata())
6987f00d0a1SPeter Collingbourne       return std::move(Err);
699e5a61917STeresa Johnson 
70001e32130SMehdi Amini     auto &ImportGUIDs = FunctionsToImportPerModule->second;
70101e32130SMehdi Amini     // Find the globals to import
7026d8f817fSPeter Collingbourne     SetVector<GlobalValue *> GlobalsToImport;
7031f685e01SPiotr Padlewski     for (Function &F : *SrcModule) {
7041f685e01SPiotr Padlewski       if (!F.hasName())
7050beb858eSTeresa Johnson         continue;
7061f685e01SPiotr Padlewski       auto GUID = F.getGUID();
7070beb858eSTeresa Johnson       auto Import = ImportGUIDs.count(GUID);
708aeb1e59bSMehdi Amini       DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing function " << GUID
7091f685e01SPiotr Padlewski                    << " " << F.getName() << " from "
710aeb1e59bSMehdi Amini                    << SrcModule->getSourceFileName() << "\n");
7110beb858eSTeresa Johnson       if (Import) {
7127f00d0a1SPeter Collingbourne         if (Error Err = F.materialize())
7137f00d0a1SPeter Collingbourne           return std::move(Err);
7143b776128SPiotr Padlewski         if (EnableImportMetadata) {
7156deaa6afSPiotr Padlewski           // Add 'thinlto_src_module' metadata for statistics and debugging.
7163b776128SPiotr Padlewski           F.setMetadata(
7173b776128SPiotr Padlewski               "thinlto_src_module",
7183b776128SPiotr Padlewski               llvm::MDNode::get(
7196deaa6afSPiotr Padlewski                   DestModule.getContext(),
7203b776128SPiotr Padlewski                   {llvm::MDString::get(DestModule.getContext(),
7216deaa6afSPiotr Padlewski                                        SrcModule->getSourceFileName())}));
7223b776128SPiotr Padlewski         }
7231f685e01SPiotr Padlewski         GlobalsToImport.insert(&F);
72401e32130SMehdi Amini       }
72501e32130SMehdi Amini     }
7261f685e01SPiotr Padlewski     for (GlobalVariable &GV : SrcModule->globals()) {
7272d28f7aaSMehdi Amini       if (!GV.hasName())
7282d28f7aaSMehdi Amini         continue;
7292d28f7aaSMehdi Amini       auto GUID = GV.getGUID();
7302d28f7aaSMehdi Amini       auto Import = ImportGUIDs.count(GUID);
731aeb1e59bSMehdi Amini       DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing global " << GUID
732aeb1e59bSMehdi Amini                    << " " << GV.getName() << " from "
733aeb1e59bSMehdi Amini                    << SrcModule->getSourceFileName() << "\n");
7342d28f7aaSMehdi Amini       if (Import) {
7357f00d0a1SPeter Collingbourne         if (Error Err = GV.materialize())
7367f00d0a1SPeter Collingbourne           return std::move(Err);
7372d28f7aaSMehdi Amini         GlobalsToImport.insert(&GV);
7382d28f7aaSMehdi Amini       }
7392d28f7aaSMehdi Amini     }
740154411e0SBenjamin Kramer #ifndef NDEBUG
7411f685e01SPiotr Padlewski     for (GlobalAlias &GA : SrcModule->aliases()) {
7421f685e01SPiotr Padlewski       if (!GA.hasName())
74301e32130SMehdi Amini         continue;
7441f685e01SPiotr Padlewski       auto GUID = GA.getGUID();
7452df7fc79STeresa Johnson       assert(!ImportGUIDs.count(GUID) && "Unexpected alias in import list");
7462df7fc79STeresa Johnson       DEBUG(dbgs() << "Not importing alias " << GUID
7471f685e01SPiotr Padlewski                    << " " << GA.getName() << " from "
748aeb1e59bSMehdi Amini                    << SrcModule->getSourceFileName() << "\n");
74901e32130SMehdi Amini     }
750154411e0SBenjamin Kramer #endif
75101e32130SMehdi Amini 
75219ef4fadSMehdi Amini     // Upgrade debug info after we're done materializing all the globals and we
75319ef4fadSMehdi Amini     // have loaded all the required metadata!
75419ef4fadSMehdi Amini     UpgradeDebugInfo(*SrcModule);
75519ef4fadSMehdi Amini 
7567e88d0daSMehdi Amini     // Link in the specified functions.
75701e32130SMehdi Amini     if (renameModuleForThinLTO(*SrcModule, Index, &GlobalsToImport))
7588d05185aSMehdi Amini       return true;
7598d05185aSMehdi Amini 
760d29478f7STeresa Johnson     if (PrintImports) {
761d29478f7STeresa Johnson       for (const auto *GV : GlobalsToImport)
762d29478f7STeresa Johnson         dbgs() << DestModule.getSourceFileName() << ": Import " << GV->getName()
763d29478f7STeresa Johnson                << " from " << SrcModule->getSourceFileName() << "\n";
764d29478f7STeresa Johnson     }
765d29478f7STeresa Johnson 
7666d8f817fSPeter Collingbourne     if (Mover.move(std::move(SrcModule), GlobalsToImport.getArrayRef(),
7676d8f817fSPeter Collingbourne                    [](GlobalValue &, IRMover::ValueAdder) {},
768e6fd9ff9SPeter Collingbourne                    /*IsPerformingImport=*/true))
7697e88d0daSMehdi Amini       report_fatal_error("Function Import: link error");
7707e88d0daSMehdi Amini 
77101e32130SMehdi Amini     ImportedCount += GlobalsToImport.size();
7726c475a75STeresa Johnson     NumImportedModules++;
7737e88d0daSMehdi Amini   }
774e5a61917STeresa Johnson 
7756c475a75STeresa Johnson   NumImportedFunctions += ImportedCount;
776d29478f7STeresa Johnson 
7777e88d0daSMehdi Amini   DEBUG(dbgs() << "Imported " << ImportedCount << " functions for Module "
778c8c55170SMehdi Amini                << DestModule.getModuleIdentifier() << "\n");
779c8c55170SMehdi Amini   return ImportedCount;
78042418abaSMehdi Amini }
78142418abaSMehdi Amini 
78242418abaSMehdi Amini /// Summary file to use for function importing when using -function-import from
78342418abaSMehdi Amini /// the command line.
78442418abaSMehdi Amini static cl::opt<std::string>
78542418abaSMehdi Amini     SummaryFile("summary-file",
78642418abaSMehdi Amini                 cl::desc("The summary file to use for function importing."));
78742418abaSMehdi Amini 
788598bd2a2SPeter Collingbourne static bool doImportingForModule(Module &M) {
789598bd2a2SPeter Collingbourne   if (SummaryFile.empty())
790598bd2a2SPeter Collingbourne     report_fatal_error("error: -function-import requires -summary-file\n");
7916de481a3SPeter Collingbourne   Expected<std::unique_ptr<ModuleSummaryIndex>> IndexPtrOrErr =
7926de481a3SPeter Collingbourne       getModuleSummaryIndexForFile(SummaryFile);
7936de481a3SPeter Collingbourne   if (!IndexPtrOrErr) {
7946de481a3SPeter Collingbourne     logAllUnhandledErrors(IndexPtrOrErr.takeError(), errs(),
7956de481a3SPeter Collingbourne                           "Error loading file '" + SummaryFile + "': ");
79642418abaSMehdi Amini     return false;
79742418abaSMehdi Amini   }
798598bd2a2SPeter Collingbourne   std::unique_ptr<ModuleSummaryIndex> Index = std::move(*IndexPtrOrErr);
79942418abaSMehdi Amini 
800c86af334STeresa Johnson   // First step is collecting the import list.
801c86af334STeresa Johnson   FunctionImporter::ImportMapTy ImportList;
802c86af334STeresa Johnson   ComputeCrossModuleImportForModule(M.getModuleIdentifier(), *Index,
803c86af334STeresa Johnson                                     ImportList);
80401e32130SMehdi Amini 
8054fef68cbSTeresa Johnson   // Conservatively mark all internal values as promoted. This interface is
8064fef68cbSTeresa Johnson   // only used when doing importing via the function importing pass. The pass
8074fef68cbSTeresa Johnson   // is only enabled when testing importing via the 'opt' tool, which does
8084fef68cbSTeresa Johnson   // not do the ThinLink that would normally determine what values to promote.
8094fef68cbSTeresa Johnson   for (auto &I : *Index) {
8109667b91bSPeter Collingbourne     for (auto &S : I.second.SummaryList) {
8114fef68cbSTeresa Johnson       if (GlobalValue::isLocalLinkage(S->linkage()))
8124fef68cbSTeresa Johnson         S->setLinkage(GlobalValue::ExternalLinkage);
8134fef68cbSTeresa Johnson     }
8144fef68cbSTeresa Johnson   }
8154fef68cbSTeresa Johnson 
81601e32130SMehdi Amini   // Next we need to promote to global scope and rename any local values that
8171b00f2d9STeresa Johnson   // are potentially exported to other modules.
81801e32130SMehdi Amini   if (renameModuleForThinLTO(M, *Index, nullptr)) {
8191b00f2d9STeresa Johnson     errs() << "Error renaming module\n";
8201b00f2d9STeresa Johnson     return false;
8211b00f2d9STeresa Johnson   }
8221b00f2d9STeresa Johnson 
82342418abaSMehdi Amini   // Perform the import now.
824d16c8065SMehdi Amini   auto ModuleLoader = [&M](StringRef Identifier) {
825d16c8065SMehdi Amini     return loadFile(Identifier, M.getContext());
826d16c8065SMehdi Amini   };
8279d2bfc48SRafael Espindola   FunctionImporter Importer(*Index, ModuleLoader);
82837e24591SPeter Collingbourne   Expected<bool> Result = Importer.importFunctions(M, ImportList);
8297f00d0a1SPeter Collingbourne 
8307f00d0a1SPeter Collingbourne   // FIXME: Probably need to propagate Errors through the pass manager.
8317f00d0a1SPeter Collingbourne   if (!Result) {
8327f00d0a1SPeter Collingbourne     logAllUnhandledErrors(Result.takeError(), errs(),
8337f00d0a1SPeter Collingbourne                           "Error importing module: ");
8347f00d0a1SPeter Collingbourne     return false;
8357f00d0a1SPeter Collingbourne   }
8367f00d0a1SPeter Collingbourne 
8377f00d0a1SPeter Collingbourne   return *Result;
83821241571STeresa Johnson }
83921241571STeresa Johnson 
84021241571STeresa Johnson namespace {
84121241571STeresa Johnson /// Pass that performs cross-module function import provided a summary file.
84221241571STeresa Johnson class FunctionImportLegacyPass : public ModulePass {
84321241571STeresa Johnson public:
84421241571STeresa Johnson   /// Pass identification, replacement for typeid
84521241571STeresa Johnson   static char ID;
84621241571STeresa Johnson 
84721241571STeresa Johnson   /// Specify pass name for debug output
848117296c0SMehdi Amini   StringRef getPassName() const override { return "Function Importing"; }
84921241571STeresa Johnson 
850598bd2a2SPeter Collingbourne   explicit FunctionImportLegacyPass() : ModulePass(ID) {}
85121241571STeresa Johnson 
85221241571STeresa Johnson   bool runOnModule(Module &M) override {
85321241571STeresa Johnson     if (skipModule(M))
85421241571STeresa Johnson       return false;
85521241571STeresa Johnson 
856598bd2a2SPeter Collingbourne     return doImportingForModule(M);
85742418abaSMehdi Amini   }
85842418abaSMehdi Amini };
859fe2b5415SBenjamin Kramer } // anonymous namespace
86042418abaSMehdi Amini 
86121241571STeresa Johnson PreservedAnalyses FunctionImportPass::run(Module &M,
862fd03ac6aSSean Silva                                           ModuleAnalysisManager &AM) {
863598bd2a2SPeter Collingbourne   if (!doImportingForModule(M))
86421241571STeresa Johnson     return PreservedAnalyses::all();
86521241571STeresa Johnson 
86621241571STeresa Johnson   return PreservedAnalyses::none();
86721241571STeresa Johnson }
86821241571STeresa Johnson 
86921241571STeresa Johnson char FunctionImportLegacyPass::ID = 0;
87021241571STeresa Johnson INITIALIZE_PASS(FunctionImportLegacyPass, "function-import",
87142418abaSMehdi Amini                 "Summary Based Function Import", false, false)
87242418abaSMehdi Amini 
87342418abaSMehdi Amini namespace llvm {
874598bd2a2SPeter Collingbourne Pass *createFunctionImportPass() {
875598bd2a2SPeter Collingbourne   return new FunctionImportLegacyPass();
8765fcbdb71STeresa Johnson }
87742418abaSMehdi Amini }
878