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"
2042418abaSMehdi Amini #include "llvm/IR/AutoUpgrade.h"
2142418abaSMehdi Amini #include "llvm/IR/DiagnosticPrinter.h"
2242418abaSMehdi Amini #include "llvm/IR/IntrinsicInst.h"
2342418abaSMehdi Amini #include "llvm/IR/Module.h"
24fc06b83eSMehdi Amini #include "llvm/IR/Verifier.h"
2542418abaSMehdi Amini #include "llvm/IRReader/IRReader.h"
2642418abaSMehdi Amini #include "llvm/Linker/Linker.h"
2704c9a2d6STeresa Johnson #include "llvm/Object/IRObjectFile.h"
2826ab5772STeresa Johnson #include "llvm/Object/ModuleSummaryIndexObjectFile.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(
64d9830eb7SPiotr Padlewski     "import-hot-multiplier", cl::init(3.0), cl::Hidden, cl::value_desc("x"),
65ba72b95fSPiotr Padlewski     cl::desc("Multiply the `import-instr-limit` threshold for hot callsites"));
66ba72b95fSPiotr Padlewski 
67ba72b95fSPiotr Padlewski // FIXME: This multiplier was not really tuned up.
68ba72b95fSPiotr Padlewski static cl::opt<float> ImportColdMultiplier(
69ba72b95fSPiotr Padlewski     "import-cold-multiplier", cl::init(0), cl::Hidden, cl::value_desc("N"),
70ba72b95fSPiotr Padlewski     cl::desc("Multiply the `import-instr-limit` threshold for cold callsites"));
7140641748SMehdi Amini 
72d29478f7STeresa Johnson static cl::opt<bool> PrintImports("print-imports", cl::init(false), cl::Hidden,
73d29478f7STeresa Johnson                                   cl::desc("Print imported functions"));
74d29478f7STeresa Johnson 
756c475a75STeresa Johnson static cl::opt<bool> ComputeDead("compute-dead", cl::init(true), cl::Hidden,
766c475a75STeresa Johnson                                  cl::desc("Compute dead symbols"));
776c475a75STeresa Johnson 
783b776128SPiotr Padlewski static cl::opt<bool> EnableImportMetadata(
793b776128SPiotr Padlewski     "enable-import-metadata", cl::init(
803b776128SPiotr Padlewski #if !defined(NDEBUG)
813b776128SPiotr Padlewski                                   true /*Enabled with asserts.*/
823b776128SPiotr Padlewski #else
833b776128SPiotr Padlewski                                   false
843b776128SPiotr Padlewski #endif
853b776128SPiotr Padlewski                                   ),
863b776128SPiotr Padlewski     cl::Hidden, cl::desc("Enable import metadata like 'thinlto_src_module'"));
873b776128SPiotr Padlewski 
8842418abaSMehdi Amini // Load lazily a module from \p FileName in \p Context.
8942418abaSMehdi Amini static std::unique_ptr<Module> loadFile(const std::string &FileName,
9042418abaSMehdi Amini                                         LLVMContext &Context) {
9142418abaSMehdi Amini   SMDiagnostic Err;
9242418abaSMehdi Amini   DEBUG(dbgs() << "Loading '" << FileName << "'\n");
936cba37ceSTeresa Johnson   // Metadata isn't loaded until functions are imported, to minimize
946cba37ceSTeresa Johnson   // the memory overhead.
95a1080ee6STeresa Johnson   std::unique_ptr<Module> Result =
96a1080ee6STeresa Johnson       getLazyIRFileModule(FileName, Err, Context,
97a1080ee6STeresa Johnson                           /* ShouldLazyLoadMetadata = */ true);
9842418abaSMehdi Amini   if (!Result) {
9942418abaSMehdi Amini     Err.print("function-import", errs());
100d7ad221cSMehdi Amini     report_fatal_error("Abort");
10142418abaSMehdi Amini   }
10242418abaSMehdi Amini 
10342418abaSMehdi Amini   return Result;
10442418abaSMehdi Amini }
10542418abaSMehdi Amini 
1067e88d0daSMehdi Amini namespace {
10740641748SMehdi Amini 
10801e32130SMehdi Amini /// Given a list of possible callee implementation for a call site, select one
10901e32130SMehdi Amini /// that fits the \p Threshold.
11001e32130SMehdi Amini ///
11101e32130SMehdi Amini /// FIXME: select "best" instead of first that fits. But what is "best"?
11201e32130SMehdi Amini /// - The smallest: more likely to be inlined.
11301e32130SMehdi Amini /// - The one with the least outgoing edges (already well optimized).
11401e32130SMehdi Amini /// - One from a module already being imported from in order to reduce the
11501e32130SMehdi Amini ///   number of source modules parsed/linked.
11601e32130SMehdi Amini /// - One that has PGO data attached.
11701e32130SMehdi Amini /// - [insert you fancy metric here]
1182d28f7aaSMehdi Amini static const GlobalValueSummary *
119b4e1e829SMehdi Amini selectCallee(const ModuleSummaryIndex &Index,
120b4e1e829SMehdi Amini              const GlobalValueSummaryList &CalleeSummaryList,
12183aaf358STeresa Johnson              unsigned Threshold, StringRef CallerModulePath) {
12201e32130SMehdi Amini   auto It = llvm::find_if(
12328e457bcSTeresa Johnson       CalleeSummaryList,
12428e457bcSTeresa Johnson       [&](const std::unique_ptr<GlobalValueSummary> &SummaryPtr) {
12528e457bcSTeresa Johnson         auto *GVSummary = SummaryPtr.get();
126f329be83SRafael Espindola         if (GlobalValue::isInterposableLinkage(GVSummary->linkage()))
1275b85d8d6SMehdi Amini           // There is no point in importing these, we can't inline them
12801e32130SMehdi Amini           return false;
1292c719cc1SMehdi Amini         if (auto *AS = dyn_cast<AliasSummary>(GVSummary)) {
1302c719cc1SMehdi Amini           GVSummary = &AS->getAliasee();
1312c719cc1SMehdi Amini           // Alias can't point to "available_externally". However when we import
1322c719cc1SMehdi Amini           // linkOnceODR the linkage does not change. So we import the alias
1332c719cc1SMehdi Amini           // and aliasee only in this case.
1342c719cc1SMehdi Amini           // FIXME: we should import alias as available_externally *function*,
1352c719cc1SMehdi Amini           // the destination module does need to know it is an alias.
1362c719cc1SMehdi Amini           if (!GlobalValue::isLinkOnceODRLinkage(GVSummary->linkage()))
1372c719cc1SMehdi Amini             return false;
1382c719cc1SMehdi Amini         }
1392c719cc1SMehdi Amini 
1402c719cc1SMehdi Amini         auto *Summary = cast<FunctionSummary>(GVSummary);
1417e88d0daSMehdi Amini 
14283aaf358STeresa Johnson         // If this is a local function, make sure we import the copy
14383aaf358STeresa Johnson         // in the caller's module. The only time a local function can
14483aaf358STeresa Johnson         // share an entry in the index is if there is a local with the same name
14583aaf358STeresa Johnson         // in another module that had the same source file name (in a different
14683aaf358STeresa Johnson         // directory), where each was compiled in their own directory so there
14783aaf358STeresa Johnson         // was not distinguishing path.
14883aaf358STeresa Johnson         // However, do the import from another module if there is only one
14983aaf358STeresa Johnson         // entry in the list - in that case this must be a reference due
15083aaf358STeresa Johnson         // to indirect call profile data, since a function pointer can point to
15183aaf358STeresa Johnson         // a local in another module.
15283aaf358STeresa Johnson         if (GlobalValue::isLocalLinkage(Summary->linkage()) &&
15383aaf358STeresa Johnson             CalleeSummaryList.size() > 1 &&
15483aaf358STeresa Johnson             Summary->modulePath() != CallerModulePath)
15583aaf358STeresa Johnson           return false;
15683aaf358STeresa Johnson 
15701e32130SMehdi Amini         if (Summary->instCount() > Threshold)
15801e32130SMehdi Amini           return false;
1597e88d0daSMehdi Amini 
160519465b9STeresa Johnson         if (Summary->notEligibleToImport())
161b4e1e829SMehdi Amini           return false;
162b4e1e829SMehdi Amini 
16301e32130SMehdi Amini         return true;
16401e32130SMehdi Amini       });
16528e457bcSTeresa Johnson   if (It == CalleeSummaryList.end())
16601e32130SMehdi Amini     return nullptr;
1677e88d0daSMehdi Amini 
16828e457bcSTeresa Johnson   return cast<GlobalValueSummary>(It->get());
169434e9561SRafael Espindola }
1707e88d0daSMehdi Amini 
17101e32130SMehdi Amini /// Return the summary for the function \p GUID that fits the \p Threshold, or
17201e32130SMehdi Amini /// null if there's no match.
1732d28f7aaSMehdi Amini static const GlobalValueSummary *selectCallee(GlobalValue::GUID GUID,
174ad5741b0SMehdi Amini                                               unsigned Threshold,
17583aaf358STeresa Johnson                                               const ModuleSummaryIndex &Index,
17683aaf358STeresa Johnson                                               StringRef CallerModulePath) {
17728e457bcSTeresa Johnson   auto CalleeSummaryList = Index.findGlobalValueSummaryList(GUID);
178b4e1e829SMehdi Amini   if (CalleeSummaryList == Index.end())
17901e32130SMehdi Amini     return nullptr; // This function does not have a summary
18083aaf358STeresa Johnson   return selectCallee(Index, CalleeSummaryList->second, Threshold,
18183aaf358STeresa Johnson                       CallerModulePath);
18201e32130SMehdi Amini }
1837e88d0daSMehdi Amini 
184475b51a7STeresa Johnson using EdgeInfo = std::tuple<const FunctionSummary *, unsigned /* Threshold */,
185475b51a7STeresa Johnson                             GlobalValue::GUID>;
18601e32130SMehdi Amini 
18701e32130SMehdi Amini /// Compute the list of functions to import for a given caller. Mark these
18801e32130SMehdi Amini /// imported functions and the symbols they reference in their source module as
18901e32130SMehdi Amini /// exported from their source module.
19001e32130SMehdi Amini static void computeImportForFunction(
1913255eec1STeresa Johnson     const FunctionSummary &Summary, const ModuleSummaryIndex &Index,
192d9830eb7SPiotr Padlewski     const unsigned Threshold, const GVSummaryMapTy &DefinedGVSummaries,
19301e32130SMehdi Amini     SmallVectorImpl<EdgeInfo> &Worklist,
1949b490f10SMehdi Amini     FunctionImporter::ImportMapTy &ImportList,
195c86af334STeresa Johnson     StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) {
19601e32130SMehdi Amini   for (auto &Edge : Summary.calls()) {
1972d5487cfSTeresa Johnson     auto GUID = Edge.first.getGUID();
19801e32130SMehdi Amini     DEBUG(dbgs() << " edge -> " << GUID << " Threshold:" << Threshold << "\n");
19901e32130SMehdi Amini 
200*4a435e08SDehao Chen     if (Index.findGlobalValueSummaryList(GUID) == Index.end()) {
201*4a435e08SDehao Chen       // For SamplePGO, the indirect call targets for local functions will
202*4a435e08SDehao Chen       // have its original name annotated in profile. We try to find the
203*4a435e08SDehao Chen       // corresponding PGOFuncName as the GUID.
204*4a435e08SDehao Chen       GUID = Index.getGUIDFromOriginalID(GUID);
205*4a435e08SDehao Chen       if (GUID == 0)
206*4a435e08SDehao Chen         continue;
207*4a435e08SDehao Chen     }
208*4a435e08SDehao Chen 
2091aafabf7SMehdi Amini     if (DefinedGVSummaries.count(GUID)) {
21001e32130SMehdi Amini       DEBUG(dbgs() << "ignored! Target already in destination module.\n");
2117e88d0daSMehdi Amini       continue;
212d450da32STeresa Johnson     }
21340641748SMehdi Amini 
214ba72b95fSPiotr Padlewski     auto GetBonusMultiplier = [](CalleeInfo::HotnessType Hotness) -> float {
215ba72b95fSPiotr Padlewski       if (Hotness == CalleeInfo::HotnessType::Hot)
216ba72b95fSPiotr Padlewski         return ImportHotMultiplier;
217ba72b95fSPiotr Padlewski       if (Hotness == CalleeInfo::HotnessType::Cold)
218ba72b95fSPiotr Padlewski         return ImportColdMultiplier;
219ba72b95fSPiotr Padlewski       return 1.0;
220ba72b95fSPiotr Padlewski     };
221ba72b95fSPiotr Padlewski 
222d9830eb7SPiotr Padlewski     const auto NewThreshold =
223ba72b95fSPiotr Padlewski         Threshold * GetBonusMultiplier(Edge.second.Hotness);
224d2869473SPiotr Padlewski 
22583aaf358STeresa Johnson     auto *CalleeSummary =
22683aaf358STeresa Johnson         selectCallee(GUID, NewThreshold, Index, Summary.modulePath());
22701e32130SMehdi Amini     if (!CalleeSummary) {
22801e32130SMehdi Amini       DEBUG(dbgs() << "ignored! No qualifying callee with summary found.\n");
2297e88d0daSMehdi Amini       continue;
2307e88d0daSMehdi Amini     }
2312d28f7aaSMehdi Amini     // "Resolve" the summary, traversing alias,
2322d28f7aaSMehdi Amini     const FunctionSummary *ResolvedCalleeSummary;
2336968ef77SMehdi Amini     if (isa<AliasSummary>(CalleeSummary)) {
2342d28f7aaSMehdi Amini       ResolvedCalleeSummary = cast<FunctionSummary>(
2352d28f7aaSMehdi Amini           &cast<AliasSummary>(CalleeSummary)->getAliasee());
2362c719cc1SMehdi Amini       assert(
2372c719cc1SMehdi Amini           GlobalValue::isLinkOnceODRLinkage(ResolvedCalleeSummary->linkage()) &&
2382c719cc1SMehdi Amini           "Unexpected alias to a non-linkonceODR in import list");
2396968ef77SMehdi Amini     } else
2402d28f7aaSMehdi Amini       ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary);
2412d28f7aaSMehdi Amini 
242d9830eb7SPiotr Padlewski     assert(ResolvedCalleeSummary->instCount() <= NewThreshold &&
24301e32130SMehdi Amini            "selectCallee() didn't honor the threshold");
24401e32130SMehdi Amini 
245d2869473SPiotr Padlewski     auto GetAdjustedThreshold = [](unsigned Threshold, bool IsHotCallsite) {
246d2869473SPiotr Padlewski       // Adjust the threshold for next level of imported functions.
247d2869473SPiotr Padlewski       // The threshold is different for hot callsites because we can then
248d2869473SPiotr Padlewski       // inline chains of hot calls.
249d2869473SPiotr Padlewski       if (IsHotCallsite)
250d2869473SPiotr Padlewski         return Threshold * ImportHotInstrFactor;
251d2869473SPiotr Padlewski       return Threshold * ImportInstrFactor;
252d2869473SPiotr Padlewski     };
253d2869473SPiotr Padlewski 
254d2869473SPiotr Padlewski     bool IsHotCallsite = Edge.second.Hotness == CalleeInfo::HotnessType::Hot;
2551b859a23STeresa Johnson     const auto AdjThreshold = GetAdjustedThreshold(Threshold, IsHotCallsite);
2561b859a23STeresa Johnson 
2571b859a23STeresa Johnson     auto ExportModulePath = ResolvedCalleeSummary->modulePath();
2581b859a23STeresa Johnson     auto &ProcessedThreshold = ImportList[ExportModulePath][GUID];
2591b859a23STeresa Johnson     /// Since the traversal of the call graph is DFS, we can revisit a function
2601b859a23STeresa Johnson     /// a second time with a higher threshold. In this case, it is added back to
2611b859a23STeresa Johnson     /// the worklist with the new threshold.
2621b859a23STeresa Johnson     if (ProcessedThreshold && ProcessedThreshold >= AdjThreshold) {
2631b859a23STeresa Johnson       DEBUG(dbgs() << "ignored! Target was already seen with Threshold "
2641b859a23STeresa Johnson                    << ProcessedThreshold << "\n");
2651b859a23STeresa Johnson       continue;
2661b859a23STeresa Johnson     }
26719f2aa78STeresa Johnson     bool PreviouslyImported = ProcessedThreshold != 0;
2681b859a23STeresa Johnson     // Mark this function as imported in this module, with the current Threshold
2691b859a23STeresa Johnson     ProcessedThreshold = AdjThreshold;
2701b859a23STeresa Johnson 
2711b859a23STeresa Johnson     // Make exports in the source module.
2721b859a23STeresa Johnson     if (ExportLists) {
2731b859a23STeresa Johnson       auto &ExportList = (*ExportLists)[ExportModulePath];
2741b859a23STeresa Johnson       ExportList.insert(GUID);
27519f2aa78STeresa Johnson       if (!PreviouslyImported) {
27619f2aa78STeresa Johnson         // This is the first time this function was exported from its source
27719f2aa78STeresa Johnson         // module, so mark all functions and globals it references as exported
2781b859a23STeresa Johnson         // to the outside if they are defined in the same source module.
279edddca22STeresa Johnson         // For efficiency, we unconditionally add all the referenced GUIDs
280edddca22STeresa Johnson         // to the ExportList for this module, and will prune out any not
281edddca22STeresa Johnson         // defined in the module later in a single pass.
2821b859a23STeresa Johnson         for (auto &Edge : ResolvedCalleeSummary->calls()) {
2831b859a23STeresa Johnson           auto CalleeGUID = Edge.first.getGUID();
284edddca22STeresa Johnson           ExportList.insert(CalleeGUID);
2851b859a23STeresa Johnson         }
2861b859a23STeresa Johnson         for (auto &Ref : ResolvedCalleeSummary->refs()) {
2871b859a23STeresa Johnson           auto GUID = Ref.getGUID();
288edddca22STeresa Johnson           ExportList.insert(GUID);
2891b859a23STeresa Johnson         }
2901b859a23STeresa Johnson       }
29119f2aa78STeresa Johnson     }
292d2869473SPiotr Padlewski 
29301e32130SMehdi Amini     // Insert the newly imported function to the worklist.
294475b51a7STeresa Johnson     Worklist.emplace_back(ResolvedCalleeSummary, AdjThreshold, GUID);
295d450da32STeresa Johnson   }
296d450da32STeresa Johnson }
297d450da32STeresa Johnson 
29801e32130SMehdi Amini /// Given the list of globals defined in a module, compute the list of imports
29901e32130SMehdi Amini /// as well as the list of "exports", i.e. the list of symbols referenced from
30001e32130SMehdi Amini /// another module (that may require promotion).
30101e32130SMehdi Amini static void ComputeImportForModule(
302c851d216STeresa Johnson     const GVSummaryMapTy &DefinedGVSummaries, const ModuleSummaryIndex &Index,
3039b490f10SMehdi Amini     FunctionImporter::ImportMapTy &ImportList,
3046c475a75STeresa Johnson     StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr,
3056c475a75STeresa Johnson     const DenseSet<GlobalValue::GUID> *DeadSymbols = nullptr) {
30601e32130SMehdi Amini   // Worklist contains the list of function imported in this module, for which
30701e32130SMehdi Amini   // we will analyse the callees and may import further down the callgraph.
30801e32130SMehdi Amini   SmallVector<EdgeInfo, 128> Worklist;
30901e32130SMehdi Amini 
31001e32130SMehdi Amini   // Populate the worklist with the import for the functions in the current
31101e32130SMehdi Amini   // module
31228e457bcSTeresa Johnson   for (auto &GVSummary : DefinedGVSummaries) {
3136c475a75STeresa Johnson     if (DeadSymbols && DeadSymbols->count(GVSummary.first)) {
3146c475a75STeresa Johnson       DEBUG(dbgs() << "Ignores Dead GUID: " << GVSummary.first << "\n");
3156c475a75STeresa Johnson       continue;
3166c475a75STeresa Johnson     }
31728e457bcSTeresa Johnson     auto *Summary = GVSummary.second;
3182d28f7aaSMehdi Amini     if (auto *AS = dyn_cast<AliasSummary>(Summary))
3192d28f7aaSMehdi Amini       Summary = &AS->getAliasee();
3201aafabf7SMehdi Amini     auto *FuncSummary = dyn_cast<FunctionSummary>(Summary);
3211aafabf7SMehdi Amini     if (!FuncSummary)
3221aafabf7SMehdi Amini       // Skip import for global variables
3231aafabf7SMehdi Amini       continue;
32428e457bcSTeresa Johnson     DEBUG(dbgs() << "Initalize import for " << GVSummary.first << "\n");
3252d28f7aaSMehdi Amini     computeImportForFunction(*FuncSummary, Index, ImportInstrLimit,
3269b490f10SMehdi Amini                              DefinedGVSummaries, Worklist, ImportList,
32701e32130SMehdi Amini                              ExportLists);
32801e32130SMehdi Amini   }
32901e32130SMehdi Amini 
330d2869473SPiotr Padlewski   // Process the newly imported functions and add callees to the worklist.
33142418abaSMehdi Amini   while (!Worklist.empty()) {
33201e32130SMehdi Amini     auto FuncInfo = Worklist.pop_back_val();
333475b51a7STeresa Johnson     auto *Summary = std::get<0>(FuncInfo);
334475b51a7STeresa Johnson     auto Threshold = std::get<1>(FuncInfo);
335475b51a7STeresa Johnson     auto GUID = std::get<2>(FuncInfo);
336475b51a7STeresa Johnson 
337475b51a7STeresa Johnson     // Check if we later added this summary with a higher threshold.
338475b51a7STeresa Johnson     // If so, skip this entry.
339475b51a7STeresa Johnson     auto ExportModulePath = Summary->modulePath();
340475b51a7STeresa Johnson     auto &LatestProcessedThreshold = ImportList[ExportModulePath][GUID];
341475b51a7STeresa Johnson     if (LatestProcessedThreshold > Threshold)
342475b51a7STeresa Johnson       continue;
34342418abaSMehdi Amini 
3441aafabf7SMehdi Amini     computeImportForFunction(*Summary, Index, Threshold, DefinedGVSummaries,
3459b490f10SMehdi Amini                              Worklist, ImportList, ExportLists);
346c8c55170SMehdi Amini   }
34742418abaSMehdi Amini }
348ffe2e4aaSMehdi Amini 
34901e32130SMehdi Amini } // anonymous namespace
35001e32130SMehdi Amini 
351c86af334STeresa Johnson /// Compute all the import and export for every module using the Index.
35201e32130SMehdi Amini void llvm::ComputeCrossModuleImport(
35301e32130SMehdi Amini     const ModuleSummaryIndex &Index,
354c851d216STeresa Johnson     const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
35501e32130SMehdi Amini     StringMap<FunctionImporter::ImportMapTy> &ImportLists,
3566c475a75STeresa Johnson     StringMap<FunctionImporter::ExportSetTy> &ExportLists,
3576c475a75STeresa Johnson     const DenseSet<GlobalValue::GUID> *DeadSymbols) {
35801e32130SMehdi Amini   // For each module that has function defined, compute the import/export lists.
3591aafabf7SMehdi Amini   for (auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) {
3609b490f10SMehdi Amini     auto &ImportList = ImportLists[DefinedGVSummaries.first()];
3611aafabf7SMehdi Amini     DEBUG(dbgs() << "Computing import for Module '"
3621aafabf7SMehdi Amini                  << DefinedGVSummaries.first() << "'\n");
3639b490f10SMehdi Amini     ComputeImportForModule(DefinedGVSummaries.second, Index, ImportList,
3646c475a75STeresa Johnson                            &ExportLists, DeadSymbols);
36501e32130SMehdi Amini   }
36601e32130SMehdi Amini 
367edddca22STeresa Johnson   // When computing imports we added all GUIDs referenced by anything
368edddca22STeresa Johnson   // imported from the module to its ExportList. Now we prune each ExportList
369edddca22STeresa Johnson   // of any not defined in that module. This is more efficient than checking
370edddca22STeresa Johnson   // while computing imports because some of the summary lists may be long
371edddca22STeresa Johnson   // due to linkonce (comdat) copies.
372edddca22STeresa Johnson   for (auto &ELI : ExportLists) {
373edddca22STeresa Johnson     const auto &DefinedGVSummaries =
374edddca22STeresa Johnson         ModuleToDefinedGVSummaries.lookup(ELI.first());
375edddca22STeresa Johnson     for (auto EI = ELI.second.begin(); EI != ELI.second.end();) {
376edddca22STeresa Johnson       if (!DefinedGVSummaries.count(*EI))
377edddca22STeresa Johnson         EI = ELI.second.erase(EI);
378edddca22STeresa Johnson       else
379edddca22STeresa Johnson         ++EI;
380edddca22STeresa Johnson     }
381edddca22STeresa Johnson   }
382edddca22STeresa Johnson 
38301e32130SMehdi Amini #ifndef NDEBUG
38401e32130SMehdi Amini   DEBUG(dbgs() << "Import/Export lists for " << ImportLists.size()
38501e32130SMehdi Amini                << " modules:\n");
38601e32130SMehdi Amini   for (auto &ModuleImports : ImportLists) {
38701e32130SMehdi Amini     auto ModName = ModuleImports.first();
38801e32130SMehdi Amini     auto &Exports = ExportLists[ModName];
38901e32130SMehdi Amini     DEBUG(dbgs() << "* Module " << ModName << " exports " << Exports.size()
39001e32130SMehdi Amini                  << " functions. Imports from " << ModuleImports.second.size()
39101e32130SMehdi Amini                  << " modules.\n");
39201e32130SMehdi Amini     for (auto &Src : ModuleImports.second) {
39301e32130SMehdi Amini       auto SrcModName = Src.first();
39401e32130SMehdi Amini       DEBUG(dbgs() << " - " << Src.second.size() << " functions imported from "
39501e32130SMehdi Amini                    << SrcModName << "\n");
39601e32130SMehdi Amini     }
39701e32130SMehdi Amini   }
39801e32130SMehdi Amini #endif
39901e32130SMehdi Amini }
40001e32130SMehdi Amini 
401c86af334STeresa Johnson /// Compute all the imports for the given module in the Index.
402c86af334STeresa Johnson void llvm::ComputeCrossModuleImportForModule(
403c86af334STeresa Johnson     StringRef ModulePath, const ModuleSummaryIndex &Index,
404c86af334STeresa Johnson     FunctionImporter::ImportMapTy &ImportList) {
405c86af334STeresa Johnson 
406c86af334STeresa Johnson   // Collect the list of functions this module defines.
407c86af334STeresa Johnson   // GUID -> Summary
408c851d216STeresa Johnson   GVSummaryMapTy FunctionSummaryMap;
40928e457bcSTeresa Johnson   Index.collectDefinedFunctionsForModule(ModulePath, FunctionSummaryMap);
410c86af334STeresa Johnson 
411c86af334STeresa Johnson   // Compute the import list for this module.
412c86af334STeresa Johnson   DEBUG(dbgs() << "Computing import for Module '" << ModulePath << "'\n");
41328e457bcSTeresa Johnson   ComputeImportForModule(FunctionSummaryMap, Index, ImportList);
414c86af334STeresa Johnson 
415c86af334STeresa Johnson #ifndef NDEBUG
416c86af334STeresa Johnson   DEBUG(dbgs() << "* Module " << ModulePath << " imports from "
417c86af334STeresa Johnson                << ImportList.size() << " modules.\n");
418c86af334STeresa Johnson   for (auto &Src : ImportList) {
419c86af334STeresa Johnson     auto SrcModName = Src.first();
420c86af334STeresa Johnson     DEBUG(dbgs() << " - " << Src.second.size() << " functions imported from "
421c86af334STeresa Johnson                  << SrcModName << "\n");
422c86af334STeresa Johnson   }
423c86af334STeresa Johnson #endif
424c86af334STeresa Johnson }
425c86af334STeresa Johnson 
4266c475a75STeresa Johnson DenseSet<GlobalValue::GUID> llvm::computeDeadSymbols(
4276c475a75STeresa Johnson     const ModuleSummaryIndex &Index,
4286c475a75STeresa Johnson     const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
4296c475a75STeresa Johnson   if (!ComputeDead)
4306c475a75STeresa Johnson     return DenseSet<GlobalValue::GUID>();
4316c475a75STeresa Johnson   if (GUIDPreservedSymbols.empty())
4326c475a75STeresa Johnson     // Don't do anything when nothing is live, this is friendly with tests.
4336c475a75STeresa Johnson     return DenseSet<GlobalValue::GUID>();
4346c475a75STeresa Johnson   DenseSet<GlobalValue::GUID> LiveSymbols = GUIDPreservedSymbols;
4356c475a75STeresa Johnson   SmallVector<GlobalValue::GUID, 128> Worklist;
4366c475a75STeresa Johnson   Worklist.reserve(LiveSymbols.size() * 2);
4376c475a75STeresa Johnson   for (auto GUID : LiveSymbols) {
4386c475a75STeresa Johnson     DEBUG(dbgs() << "Live root: " << GUID << "\n");
4396c475a75STeresa Johnson     Worklist.push_back(GUID);
4406c475a75STeresa Johnson   }
4416c475a75STeresa Johnson   // Add values flagged in the index as live roots to the worklist.
4426c475a75STeresa Johnson   for (const auto &Entry : Index) {
4436c475a75STeresa Johnson     bool IsLiveRoot = llvm::any_of(
4446c475a75STeresa Johnson         Entry.second,
4456c475a75STeresa Johnson         [&](const std::unique_ptr<llvm::GlobalValueSummary> &Summary) {
4466c475a75STeresa Johnson           return Summary->liveRoot();
4476c475a75STeresa Johnson         });
4486c475a75STeresa Johnson     if (!IsLiveRoot)
4496c475a75STeresa Johnson       continue;
4506c475a75STeresa Johnson     DEBUG(dbgs() << "Live root (summary): " << Entry.first << "\n");
4516c475a75STeresa Johnson     Worklist.push_back(Entry.first);
4526c475a75STeresa Johnson   }
4536c475a75STeresa Johnson 
4546c475a75STeresa Johnson   while (!Worklist.empty()) {
4556c475a75STeresa Johnson     auto GUID = Worklist.pop_back_val();
4566c475a75STeresa Johnson     auto It = Index.findGlobalValueSummaryList(GUID);
4576c475a75STeresa Johnson     if (It == Index.end()) {
4586c475a75STeresa Johnson       DEBUG(dbgs() << "Not in index: " << GUID << "\n");
4596c475a75STeresa Johnson       continue;
4606c475a75STeresa Johnson     }
4616c475a75STeresa Johnson 
4626c475a75STeresa Johnson     // FIXME: we should only make the prevailing copy live here
4636c475a75STeresa Johnson     for (auto &Summary : It->second) {
4646c475a75STeresa Johnson       for (auto Ref : Summary->refs()) {
4656c475a75STeresa Johnson         auto RefGUID = Ref.getGUID();
4666c475a75STeresa Johnson         if (LiveSymbols.insert(RefGUID).second) {
4676c475a75STeresa Johnson           DEBUG(dbgs() << "Marking live (ref): " << RefGUID << "\n");
4686c475a75STeresa Johnson           Worklist.push_back(RefGUID);
4696c475a75STeresa Johnson         }
4706c475a75STeresa Johnson       }
4716c475a75STeresa Johnson       if (auto *FS = dyn_cast<FunctionSummary>(Summary.get())) {
4726c475a75STeresa Johnson         for (auto Call : FS->calls()) {
4736c475a75STeresa Johnson           auto CallGUID = Call.first.getGUID();
4746c475a75STeresa Johnson           if (LiveSymbols.insert(CallGUID).second) {
4756c475a75STeresa Johnson             DEBUG(dbgs() << "Marking live (call): " << CallGUID << "\n");
4766c475a75STeresa Johnson             Worklist.push_back(CallGUID);
4776c475a75STeresa Johnson           }
4786c475a75STeresa Johnson         }
4796c475a75STeresa Johnson       }
4806c475a75STeresa Johnson       if (auto *AS = dyn_cast<AliasSummary>(Summary.get())) {
4816c475a75STeresa Johnson         auto AliaseeGUID = AS->getAliasee().getOriginalName();
4826c475a75STeresa Johnson         if (LiveSymbols.insert(AliaseeGUID).second) {
4836c475a75STeresa Johnson           DEBUG(dbgs() << "Marking live (alias): " << AliaseeGUID << "\n");
4846c475a75STeresa Johnson           Worklist.push_back(AliaseeGUID);
4856c475a75STeresa Johnson         }
4866c475a75STeresa Johnson       }
4876c475a75STeresa Johnson     }
4886c475a75STeresa Johnson   }
4896c475a75STeresa Johnson   DenseSet<GlobalValue::GUID> DeadSymbols;
4906c475a75STeresa Johnson   DeadSymbols.reserve(
4916c475a75STeresa Johnson       std::min(Index.size(), Index.size() - LiveSymbols.size()));
4926c475a75STeresa Johnson   for (auto &Entry : Index) {
4936c475a75STeresa Johnson     auto GUID = Entry.first;
4946c475a75STeresa Johnson     if (!LiveSymbols.count(GUID)) {
4956c475a75STeresa Johnson       DEBUG(dbgs() << "Marking dead: " << GUID << "\n");
4966c475a75STeresa Johnson       DeadSymbols.insert(GUID);
4976c475a75STeresa Johnson     }
4986c475a75STeresa Johnson   }
4996c475a75STeresa Johnson   DEBUG(dbgs() << LiveSymbols.size() << " symbols Live, and "
5006c475a75STeresa Johnson                << DeadSymbols.size() << " symbols Dead \n");
5016c475a75STeresa Johnson   NumDeadSymbols += DeadSymbols.size();
5026c475a75STeresa Johnson   NumLiveSymbols += LiveSymbols.size();
5036c475a75STeresa Johnson   return DeadSymbols;
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.
5604566c6dbSTeresa Johnson       assert(false && "Expected function or variable");
5614566c6dbSTeresa Johnson   };
5624566c6dbSTeresa Johnson 
56304c9a2d6STeresa Johnson   auto updateLinkage = [&](GlobalValue &GV) {
56404c9a2d6STeresa Johnson     if (!GlobalValue::isWeakForLinker(GV.getLinkage()))
56504c9a2d6STeresa Johnson       return;
56604c9a2d6STeresa Johnson     // See if the global summary analysis computed a new resolved linkage.
56704c9a2d6STeresa Johnson     const auto &GS = DefinedGlobals.find(GV.getGUID());
56804c9a2d6STeresa Johnson     if (GS == DefinedGlobals.end())
56904c9a2d6STeresa Johnson       return;
57004c9a2d6STeresa Johnson     auto NewLinkage = GS->second->linkage();
57104c9a2d6STeresa Johnson     if (NewLinkage == GV.getLinkage())
57204c9a2d6STeresa Johnson       return;
5734566c6dbSTeresa Johnson     // Check for a non-prevailing def that has interposable linkage
5744566c6dbSTeresa Johnson     // (e.g. non-odr weak or linkonce). In that case we can't simply
5754566c6dbSTeresa Johnson     // convert to available_externally, since it would lose the
5764566c6dbSTeresa Johnson     // interposable property and possibly get inlined. Simply drop
5774566c6dbSTeresa Johnson     // the definition in that case.
5784566c6dbSTeresa Johnson     if (GlobalValue::isAvailableExternallyLinkage(NewLinkage) &&
5794566c6dbSTeresa Johnson         GlobalValue::isInterposableLinkage(GV.getLinkage()))
5804566c6dbSTeresa Johnson       ConvertToDeclaration(GV);
5814566c6dbSTeresa Johnson     else {
58204c9a2d6STeresa Johnson       DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName() << "` from "
58304c9a2d6STeresa Johnson                    << GV.getLinkage() << " to " << NewLinkage << "\n");
58404c9a2d6STeresa Johnson       GV.setLinkage(NewLinkage);
5854566c6dbSTeresa Johnson     }
5864566c6dbSTeresa Johnson     // Remove declarations from comdats, including available_externally
5876107a419STeresa Johnson     // as this is a declaration for the linker, and will be dropped eventually.
5886107a419STeresa Johnson     // It is illegal for comdats to contain declarations.
5896107a419STeresa Johnson     auto *GO = dyn_cast_or_null<GlobalObject>(&GV);
5904566c6dbSTeresa Johnson     if (GO && GO->isDeclarationForLinker() && GO->hasComdat())
5916107a419STeresa Johnson       GO->setComdat(nullptr);
59204c9a2d6STeresa Johnson   };
59304c9a2d6STeresa Johnson 
59404c9a2d6STeresa Johnson   // Process functions and global now
59504c9a2d6STeresa Johnson   for (auto &GV : TheModule)
59604c9a2d6STeresa Johnson     updateLinkage(GV);
59704c9a2d6STeresa Johnson   for (auto &GV : TheModule.globals())
59804c9a2d6STeresa Johnson     updateLinkage(GV);
59904c9a2d6STeresa Johnson   for (auto &GV : TheModule.aliases())
60004c9a2d6STeresa Johnson     updateLinkage(GV);
60104c9a2d6STeresa Johnson }
60204c9a2d6STeresa Johnson 
60304c9a2d6STeresa Johnson /// Run internalization on \p TheModule based on symmary analysis.
60404c9a2d6STeresa Johnson void llvm::thinLTOInternalizeModule(Module &TheModule,
60504c9a2d6STeresa Johnson                                     const GVSummaryMapTy &DefinedGlobals) {
60604c9a2d6STeresa Johnson   // Parse inline ASM and collect the list of symbols that are not defined in
60704c9a2d6STeresa Johnson   // the current module.
60804c9a2d6STeresa Johnson   StringSet<> AsmUndefinedRefs;
609863cbfbeSPeter Collingbourne   ModuleSymbolTable::CollectAsmSymbols(
610d8204472STeresa Johnson       TheModule,
61104c9a2d6STeresa Johnson       [&AsmUndefinedRefs](StringRef Name, object::BasicSymbolRef::Flags Flags) {
61204c9a2d6STeresa Johnson         if (Flags & object::BasicSymbolRef::SF_Undefined)
61304c9a2d6STeresa Johnson           AsmUndefinedRefs.insert(Name);
61404c9a2d6STeresa Johnson       });
61504c9a2d6STeresa Johnson 
61604c9a2d6STeresa Johnson   // Declare a callback for the internalize pass that will ask for every
61704c9a2d6STeresa Johnson   // candidate GlobalValue if it can be internalized or not.
61804c9a2d6STeresa Johnson   auto MustPreserveGV = [&](const GlobalValue &GV) -> bool {
61904c9a2d6STeresa Johnson     // Can't be internalized if referenced in inline asm.
62004c9a2d6STeresa Johnson     if (AsmUndefinedRefs.count(GV.getName()))
62104c9a2d6STeresa Johnson       return true;
62204c9a2d6STeresa Johnson 
62304c9a2d6STeresa Johnson     // Lookup the linkage recorded in the summaries during global analysis.
62404c9a2d6STeresa Johnson     const auto &GS = DefinedGlobals.find(GV.getGUID());
62504c9a2d6STeresa Johnson     GlobalValue::LinkageTypes Linkage;
62604c9a2d6STeresa Johnson     if (GS == DefinedGlobals.end()) {
62704c9a2d6STeresa Johnson       // Must have been promoted (possibly conservatively). Find original
62804c9a2d6STeresa Johnson       // name so that we can access the correct summary and see if it can
62904c9a2d6STeresa Johnson       // be internalized again.
63004c9a2d6STeresa Johnson       // FIXME: Eventually we should control promotion instead of promoting
63104c9a2d6STeresa Johnson       // and internalizing again.
63204c9a2d6STeresa Johnson       StringRef OrigName =
63304c9a2d6STeresa Johnson           ModuleSummaryIndex::getOriginalNameBeforePromote(GV.getName());
63404c9a2d6STeresa Johnson       std::string OrigId = GlobalValue::getGlobalIdentifier(
63504c9a2d6STeresa Johnson           OrigName, GlobalValue::InternalLinkage,
63604c9a2d6STeresa Johnson           TheModule.getSourceFileName());
63704c9a2d6STeresa Johnson       const auto &GS = DefinedGlobals.find(GlobalValue::getGUID(OrigId));
6387ab1f692STeresa Johnson       if (GS == DefinedGlobals.end()) {
6397ab1f692STeresa Johnson         // Also check the original non-promoted non-globalized name. In some
6407ab1f692STeresa Johnson         // cases a preempted weak value is linked in as a local copy because
6417ab1f692STeresa Johnson         // it is referenced by an alias (IRLinker::linkGlobalValueProto).
6427ab1f692STeresa Johnson         // In that case, since it was originally not a local value, it was
6437ab1f692STeresa Johnson         // recorded in the index using the original name.
6447ab1f692STeresa Johnson         // FIXME: This may not be needed once PR27866 is fixed.
6457ab1f692STeresa Johnson         const auto &GS = DefinedGlobals.find(GlobalValue::getGUID(OrigName));
64604c9a2d6STeresa Johnson         assert(GS != DefinedGlobals.end());
64704c9a2d6STeresa Johnson         Linkage = GS->second->linkage();
6487ab1f692STeresa Johnson       } else {
6497ab1f692STeresa Johnson         Linkage = GS->second->linkage();
6507ab1f692STeresa Johnson       }
65104c9a2d6STeresa Johnson     } else
65204c9a2d6STeresa Johnson       Linkage = GS->second->linkage();
65304c9a2d6STeresa Johnson     return !GlobalValue::isLocalLinkage(Linkage);
65404c9a2d6STeresa Johnson   };
65504c9a2d6STeresa Johnson 
65604c9a2d6STeresa Johnson   // FIXME: See if we can just internalize directly here via linkage changes
65704c9a2d6STeresa Johnson   // based on the index, rather than invoking internalizeModule.
65804c9a2d6STeresa Johnson   llvm::internalizeModule(TheModule, MustPreserveGV);
65904c9a2d6STeresa Johnson }
66004c9a2d6STeresa Johnson 
661c8c55170SMehdi Amini // Automatically import functions in Module \p DestModule based on the summaries
662c8c55170SMehdi Amini // index.
663c8c55170SMehdi Amini //
6647f00d0a1SPeter Collingbourne Expected<bool> FunctionImporter::importFunctions(
66537e24591SPeter Collingbourne     Module &DestModule, const FunctionImporter::ImportMapTy &ImportList) {
6665411d051SMehdi Amini   DEBUG(dbgs() << "Starting import for Module "
667311fef6eSMehdi Amini                << DestModule.getModuleIdentifier() << "\n");
668c8c55170SMehdi Amini   unsigned ImportedCount = 0;
669c8c55170SMehdi Amini 
6706d8f817fSPeter Collingbourne   IRMover Mover(DestModule);
6717e88d0daSMehdi Amini   // Do the actual import of functions now, one Module at a time
67201e32130SMehdi Amini   std::set<StringRef> ModuleNameOrderedList;
67301e32130SMehdi Amini   for (auto &FunctionsToImportPerModule : ImportList) {
67401e32130SMehdi Amini     ModuleNameOrderedList.insert(FunctionsToImportPerModule.first());
67501e32130SMehdi Amini   }
67601e32130SMehdi Amini   for (auto &Name : ModuleNameOrderedList) {
6777e88d0daSMehdi Amini     // Get the module for the import
67801e32130SMehdi Amini     const auto &FunctionsToImportPerModule = ImportList.find(Name);
67901e32130SMehdi Amini     assert(FunctionsToImportPerModule != ImportList.end());
680d9445c49SPeter Collingbourne     Expected<std::unique_ptr<Module>> SrcModuleOrErr = ModuleLoader(Name);
681d9445c49SPeter Collingbourne     if (!SrcModuleOrErr)
682d9445c49SPeter Collingbourne       return SrcModuleOrErr.takeError();
683d9445c49SPeter Collingbourne     std::unique_ptr<Module> SrcModule = std::move(*SrcModuleOrErr);
6847e88d0daSMehdi Amini     assert(&DestModule.getContext() == &SrcModule->getContext() &&
6857e88d0daSMehdi Amini            "Context mismatch");
6867e88d0daSMehdi Amini 
6876cba37ceSTeresa Johnson     // If modules were created with lazy metadata loading, materialize it
6886cba37ceSTeresa Johnson     // now, before linking it (otherwise this will be a noop).
6897f00d0a1SPeter Collingbourne     if (Error Err = SrcModule->materializeMetadata())
6907f00d0a1SPeter Collingbourne       return std::move(Err);
691e5a61917STeresa Johnson 
69201e32130SMehdi Amini     auto &ImportGUIDs = FunctionsToImportPerModule->second;
69301e32130SMehdi Amini     // Find the globals to import
6946d8f817fSPeter Collingbourne     SetVector<GlobalValue *> GlobalsToImport;
6951f685e01SPiotr Padlewski     for (Function &F : *SrcModule) {
6961f685e01SPiotr Padlewski       if (!F.hasName())
6970beb858eSTeresa Johnson         continue;
6981f685e01SPiotr Padlewski       auto GUID = F.getGUID();
6990beb858eSTeresa Johnson       auto Import = ImportGUIDs.count(GUID);
700aeb1e59bSMehdi Amini       DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing function " << GUID
7011f685e01SPiotr Padlewski                    << " " << F.getName() << " from "
702aeb1e59bSMehdi Amini                    << SrcModule->getSourceFileName() << "\n");
7030beb858eSTeresa Johnson       if (Import) {
7047f00d0a1SPeter Collingbourne         if (Error Err = F.materialize())
7057f00d0a1SPeter Collingbourne           return std::move(Err);
7063b776128SPiotr Padlewski         if (EnableImportMetadata) {
7076deaa6afSPiotr Padlewski           // Add 'thinlto_src_module' metadata for statistics and debugging.
7083b776128SPiotr Padlewski           F.setMetadata(
7093b776128SPiotr Padlewski               "thinlto_src_module",
7103b776128SPiotr Padlewski               llvm::MDNode::get(
7116deaa6afSPiotr Padlewski                   DestModule.getContext(),
7123b776128SPiotr Padlewski                   {llvm::MDString::get(DestModule.getContext(),
7136deaa6afSPiotr Padlewski                                        SrcModule->getSourceFileName())}));
7143b776128SPiotr Padlewski         }
7151f685e01SPiotr Padlewski         GlobalsToImport.insert(&F);
71601e32130SMehdi Amini       }
71701e32130SMehdi Amini     }
7181f685e01SPiotr Padlewski     for (GlobalVariable &GV : SrcModule->globals()) {
7192d28f7aaSMehdi Amini       if (!GV.hasName())
7202d28f7aaSMehdi Amini         continue;
7212d28f7aaSMehdi Amini       auto GUID = GV.getGUID();
7222d28f7aaSMehdi Amini       auto Import = ImportGUIDs.count(GUID);
723aeb1e59bSMehdi Amini       DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing global " << GUID
724aeb1e59bSMehdi Amini                    << " " << GV.getName() << " from "
725aeb1e59bSMehdi Amini                    << SrcModule->getSourceFileName() << "\n");
7262d28f7aaSMehdi Amini       if (Import) {
7277f00d0a1SPeter Collingbourne         if (Error Err = GV.materialize())
7287f00d0a1SPeter Collingbourne           return std::move(Err);
7292d28f7aaSMehdi Amini         GlobalsToImport.insert(&GV);
7302d28f7aaSMehdi Amini       }
7312d28f7aaSMehdi Amini     }
7321f685e01SPiotr Padlewski     for (GlobalAlias &GA : SrcModule->aliases()) {
7336d8f817fSPeter Collingbourne       // FIXME: This should eventually be controlled entirely by the summary.
7346d8f817fSPeter Collingbourne       if (FunctionImportGlobalProcessing::doImportAsDefinition(
7356d8f817fSPeter Collingbourne               &GA, &GlobalsToImport)) {
7366d8f817fSPeter Collingbourne         GlobalsToImport.insert(&GA);
7376d8f817fSPeter Collingbourne         continue;
7386d8f817fSPeter Collingbourne       }
7396d8f817fSPeter Collingbourne 
7401f685e01SPiotr Padlewski       if (!GA.hasName())
74101e32130SMehdi Amini         continue;
7421f685e01SPiotr Padlewski       auto GUID = GA.getGUID();
7430beb858eSTeresa Johnson       auto Import = ImportGUIDs.count(GUID);
744aeb1e59bSMehdi Amini       DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing alias " << GUID
7451f685e01SPiotr Padlewski                    << " " << GA.getName() << " from "
746aeb1e59bSMehdi Amini                    << SrcModule->getSourceFileName() << "\n");
7470beb858eSTeresa Johnson       if (Import) {
74801e32130SMehdi Amini         // Alias can't point to "available_externally". However when we import
7499aae395fSTeresa Johnson         // linkOnceODR the linkage does not change. So we import the alias
7506968ef77SMehdi Amini         // and aliasee only in this case. This has been handled by
7516968ef77SMehdi Amini         // computeImportForFunction()
7521f685e01SPiotr Padlewski         GlobalObject *GO = GA.getBaseObject();
7536968ef77SMehdi Amini         assert(GO->hasLinkOnceODRLinkage() &&
7546968ef77SMehdi Amini                "Unexpected alias to a non-linkonceODR in import list");
7552d28f7aaSMehdi Amini #ifndef NDEBUG
7562d28f7aaSMehdi Amini         if (!GlobalsToImport.count(GO))
7572d28f7aaSMehdi Amini           DEBUG(dbgs() << " alias triggers importing aliasee " << GO->getGUID()
7582d28f7aaSMehdi Amini                        << " " << GO->getName() << " from "
7592d28f7aaSMehdi Amini                        << SrcModule->getSourceFileName() << "\n");
7602d28f7aaSMehdi Amini #endif
7617f00d0a1SPeter Collingbourne         if (Error Err = GO->materialize())
7627f00d0a1SPeter Collingbourne           return std::move(Err);
76301e32130SMehdi Amini         GlobalsToImport.insert(GO);
7647f00d0a1SPeter Collingbourne         if (Error Err = GA.materialize())
7657f00d0a1SPeter Collingbourne           return std::move(Err);
7661f685e01SPiotr Padlewski         GlobalsToImport.insert(&GA);
76701e32130SMehdi Amini       }
76801e32130SMehdi Amini     }
76901e32130SMehdi Amini 
77019ef4fadSMehdi Amini     // Upgrade debug info after we're done materializing all the globals and we
77119ef4fadSMehdi Amini     // have loaded all the required metadata!
77219ef4fadSMehdi Amini     UpgradeDebugInfo(*SrcModule);
77319ef4fadSMehdi Amini 
7747e88d0daSMehdi Amini     // Link in the specified functions.
77501e32130SMehdi Amini     if (renameModuleForThinLTO(*SrcModule, Index, &GlobalsToImport))
7768d05185aSMehdi Amini       return true;
7778d05185aSMehdi Amini 
778d29478f7STeresa Johnson     if (PrintImports) {
779d29478f7STeresa Johnson       for (const auto *GV : GlobalsToImport)
780d29478f7STeresa Johnson         dbgs() << DestModule.getSourceFileName() << ": Import " << GV->getName()
781d29478f7STeresa Johnson                << " from " << SrcModule->getSourceFileName() << "\n";
782d29478f7STeresa Johnson     }
783d29478f7STeresa Johnson 
7846d8f817fSPeter Collingbourne     if (Mover.move(std::move(SrcModule), GlobalsToImport.getArrayRef(),
7856d8f817fSPeter Collingbourne                    [](GlobalValue &, IRMover::ValueAdder) {},
786e6fd9ff9SPeter Collingbourne                    /*IsPerformingImport=*/true))
7877e88d0daSMehdi Amini       report_fatal_error("Function Import: link error");
7887e88d0daSMehdi Amini 
78901e32130SMehdi Amini     ImportedCount += GlobalsToImport.size();
7906c475a75STeresa Johnson     NumImportedModules++;
7917e88d0daSMehdi Amini   }
792e5a61917STeresa Johnson 
7936c475a75STeresa Johnson   NumImportedFunctions += ImportedCount;
794d29478f7STeresa Johnson 
7957e88d0daSMehdi Amini   DEBUG(dbgs() << "Imported " << ImportedCount << " functions for Module "
796c8c55170SMehdi Amini                << DestModule.getModuleIdentifier() << "\n");
797c8c55170SMehdi Amini   return ImportedCount;
79842418abaSMehdi Amini }
79942418abaSMehdi Amini 
80042418abaSMehdi Amini /// Summary file to use for function importing when using -function-import from
80142418abaSMehdi Amini /// the command line.
80242418abaSMehdi Amini static cl::opt<std::string>
80342418abaSMehdi Amini     SummaryFile("summary-file",
80442418abaSMehdi Amini                 cl::desc("The summary file to use for function importing."));
80542418abaSMehdi Amini 
806598bd2a2SPeter Collingbourne static bool doImportingForModule(Module &M) {
807598bd2a2SPeter Collingbourne   if (SummaryFile.empty())
808598bd2a2SPeter Collingbourne     report_fatal_error("error: -function-import requires -summary-file\n");
8096de481a3SPeter Collingbourne   Expected<std::unique_ptr<ModuleSummaryIndex>> IndexPtrOrErr =
8106de481a3SPeter Collingbourne       getModuleSummaryIndexForFile(SummaryFile);
8116de481a3SPeter Collingbourne   if (!IndexPtrOrErr) {
8126de481a3SPeter Collingbourne     logAllUnhandledErrors(IndexPtrOrErr.takeError(), errs(),
8136de481a3SPeter Collingbourne                           "Error loading file '" + SummaryFile + "': ");
81442418abaSMehdi Amini     return false;
81542418abaSMehdi Amini   }
816598bd2a2SPeter Collingbourne   std::unique_ptr<ModuleSummaryIndex> Index = std::move(*IndexPtrOrErr);
81742418abaSMehdi Amini 
818c86af334STeresa Johnson   // First step is collecting the import list.
819c86af334STeresa Johnson   FunctionImporter::ImportMapTy ImportList;
820c86af334STeresa Johnson   ComputeCrossModuleImportForModule(M.getModuleIdentifier(), *Index,
821c86af334STeresa Johnson                                     ImportList);
82201e32130SMehdi Amini 
8234fef68cbSTeresa Johnson   // Conservatively mark all internal values as promoted. This interface is
8244fef68cbSTeresa Johnson   // only used when doing importing via the function importing pass. The pass
8254fef68cbSTeresa Johnson   // is only enabled when testing importing via the 'opt' tool, which does
8264fef68cbSTeresa Johnson   // not do the ThinLink that would normally determine what values to promote.
8274fef68cbSTeresa Johnson   for (auto &I : *Index) {
8284fef68cbSTeresa Johnson     for (auto &S : I.second) {
8294fef68cbSTeresa Johnson       if (GlobalValue::isLocalLinkage(S->linkage()))
8304fef68cbSTeresa Johnson         S->setLinkage(GlobalValue::ExternalLinkage);
8314fef68cbSTeresa Johnson     }
8324fef68cbSTeresa Johnson   }
8334fef68cbSTeresa Johnson 
83401e32130SMehdi Amini   // Next we need to promote to global scope and rename any local values that
8351b00f2d9STeresa Johnson   // are potentially exported to other modules.
83601e32130SMehdi Amini   if (renameModuleForThinLTO(M, *Index, nullptr)) {
8371b00f2d9STeresa Johnson     errs() << "Error renaming module\n";
8381b00f2d9STeresa Johnson     return false;
8391b00f2d9STeresa Johnson   }
8401b00f2d9STeresa Johnson 
84142418abaSMehdi Amini   // Perform the import now.
842d16c8065SMehdi Amini   auto ModuleLoader = [&M](StringRef Identifier) {
843d16c8065SMehdi Amini     return loadFile(Identifier, M.getContext());
844d16c8065SMehdi Amini   };
8459d2bfc48SRafael Espindola   FunctionImporter Importer(*Index, ModuleLoader);
84637e24591SPeter Collingbourne   Expected<bool> Result = Importer.importFunctions(M, ImportList);
8477f00d0a1SPeter Collingbourne 
8487f00d0a1SPeter Collingbourne   // FIXME: Probably need to propagate Errors through the pass manager.
8497f00d0a1SPeter Collingbourne   if (!Result) {
8507f00d0a1SPeter Collingbourne     logAllUnhandledErrors(Result.takeError(), errs(),
8517f00d0a1SPeter Collingbourne                           "Error importing module: ");
8527f00d0a1SPeter Collingbourne     return false;
8537f00d0a1SPeter Collingbourne   }
8547f00d0a1SPeter Collingbourne 
8557f00d0a1SPeter Collingbourne   return *Result;
85621241571STeresa Johnson }
85721241571STeresa Johnson 
85821241571STeresa Johnson namespace {
85921241571STeresa Johnson /// Pass that performs cross-module function import provided a summary file.
86021241571STeresa Johnson class FunctionImportLegacyPass : public ModulePass {
86121241571STeresa Johnson public:
86221241571STeresa Johnson   /// Pass identification, replacement for typeid
86321241571STeresa Johnson   static char ID;
86421241571STeresa Johnson 
86521241571STeresa Johnson   /// Specify pass name for debug output
866117296c0SMehdi Amini   StringRef getPassName() const override { return "Function Importing"; }
86721241571STeresa Johnson 
868598bd2a2SPeter Collingbourne   explicit FunctionImportLegacyPass() : ModulePass(ID) {}
86921241571STeresa Johnson 
87021241571STeresa Johnson   bool runOnModule(Module &M) override {
87121241571STeresa Johnson     if (skipModule(M))
87221241571STeresa Johnson       return false;
87321241571STeresa Johnson 
874598bd2a2SPeter Collingbourne     return doImportingForModule(M);
87542418abaSMehdi Amini   }
87642418abaSMehdi Amini };
877fe2b5415SBenjamin Kramer } // anonymous namespace
87842418abaSMehdi Amini 
87921241571STeresa Johnson PreservedAnalyses FunctionImportPass::run(Module &M,
880fd03ac6aSSean Silva                                           ModuleAnalysisManager &AM) {
881598bd2a2SPeter Collingbourne   if (!doImportingForModule(M))
88221241571STeresa Johnson     return PreservedAnalyses::all();
88321241571STeresa Johnson 
88421241571STeresa Johnson   return PreservedAnalyses::none();
88521241571STeresa Johnson }
88621241571STeresa Johnson 
88721241571STeresa Johnson char FunctionImportLegacyPass::ID = 0;
88821241571STeresa Johnson INITIALIZE_PASS(FunctionImportLegacyPass, "function-import",
88942418abaSMehdi Amini                 "Summary Based Function Import", false, false)
89042418abaSMehdi Amini 
89142418abaSMehdi Amini namespace llvm {
892598bd2a2SPeter Collingbourne Pass *createFunctionImportPass() {
893598bd2a2SPeter Collingbourne   return new FunctionImportLegacyPass();
8945fcbdb71STeresa Johnson }
89542418abaSMehdi Amini }
896