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"
15e9ea08a0SEugene Zelenko #include "llvm/ADT/ArrayRef.h"
16e9ea08a0SEugene Zelenko #include "llvm/ADT/STLExtras.h"
17e9ea08a0SEugene Zelenko #include "llvm/ADT/SetVector.h"
1801e32130SMehdi Amini #include "llvm/ADT/SmallVector.h"
19d29478f7STeresa Johnson #include "llvm/ADT/Statistic.h"
20e9ea08a0SEugene Zelenko #include "llvm/ADT/StringMap.h"
2142418abaSMehdi Amini #include "llvm/ADT/StringSet.h"
22e9ea08a0SEugene Zelenko #include "llvm/ADT/StringRef.h"
23c15d60b7SPeter Collingbourne #include "llvm/Bitcode/BitcodeReader.h"
2442418abaSMehdi Amini #include "llvm/IR/AutoUpgrade.h"
2581bbf742STeresa Johnson #include "llvm/IR/Constants.h"
26e9ea08a0SEugene Zelenko #include "llvm/IR/Function.h"
27e9ea08a0SEugene Zelenko #include "llvm/IR/GlobalAlias.h"
28e9ea08a0SEugene Zelenko #include "llvm/IR/GlobalObject.h"
29e9ea08a0SEugene Zelenko #include "llvm/IR/GlobalValue.h"
30e9ea08a0SEugene Zelenko #include "llvm/IR/GlobalVariable.h"
31e9ea08a0SEugene Zelenko #include "llvm/IR/Metadata.h"
3242418abaSMehdi Amini #include "llvm/IR/Module.h"
33e9ea08a0SEugene Zelenko #include "llvm/IR/ModuleSummaryIndex.h"
3442418abaSMehdi Amini #include "llvm/IRReader/IRReader.h"
35e9ea08a0SEugene Zelenko #include "llvm/Linker/IRMover.h"
36e9ea08a0SEugene Zelenko #include "llvm/Object/ModuleSymbolTable.h"
37e9ea08a0SEugene Zelenko #include "llvm/Object/SymbolicFile.h"
38e9ea08a0SEugene Zelenko #include "llvm/Pass.h"
39e9ea08a0SEugene Zelenko #include "llvm/Support/Casting.h"
4042418abaSMehdi Amini #include "llvm/Support/CommandLine.h"
4142418abaSMehdi Amini #include "llvm/Support/Debug.h"
42e9ea08a0SEugene Zelenko #include "llvm/Support/Error.h"
43e9ea08a0SEugene Zelenko #include "llvm/Support/ErrorHandling.h"
44e9ea08a0SEugene Zelenko #include "llvm/Support/FileSystem.h"
4542418abaSMehdi Amini #include "llvm/Support/SourceMgr.h"
46e9ea08a0SEugene Zelenko #include "llvm/Support/raw_ostream.h"
4704c9a2d6STeresa Johnson #include "llvm/Transforms/IPO/Internalize.h"
4881bbf742STeresa Johnson #include "llvm/Transforms/Utils/Cloning.h"
49488a800aSTeresa Johnson #include "llvm/Transforms/Utils/FunctionImportUtils.h"
5081bbf742STeresa Johnson #include "llvm/Transforms/Utils/ValueMapper.h"
51e9ea08a0SEugene Zelenko #include <cassert>
52e9ea08a0SEugene Zelenko #include <memory>
53e9ea08a0SEugene Zelenko #include <set>
54e9ea08a0SEugene Zelenko #include <string>
55e9ea08a0SEugene Zelenko #include <system_error>
56e9ea08a0SEugene Zelenko #include <tuple>
57e9ea08a0SEugene Zelenko #include <utility>
587e88d0daSMehdi Amini 
5942418abaSMehdi Amini using namespace llvm;
6042418abaSMehdi Amini 
61e9ea08a0SEugene Zelenko #define DEBUG_TYPE "function-import"
62e9ea08a0SEugene Zelenko 
636c475a75STeresa Johnson STATISTIC(NumImportedFunctions, "Number of functions imported");
646c475a75STeresa Johnson STATISTIC(NumImportedModules, "Number of modules imported from");
656c475a75STeresa Johnson STATISTIC(NumDeadSymbols, "Number of dead stripped symbols in index");
666c475a75STeresa Johnson STATISTIC(NumLiveSymbols, "Number of live symbols in index");
67d29478f7STeresa Johnson 
6839303619STeresa Johnson /// Limit on instruction count of imported functions.
6939303619STeresa Johnson static cl::opt<unsigned> ImportInstrLimit(
7039303619STeresa Johnson     "import-instr-limit", cl::init(100), cl::Hidden, cl::value_desc("N"),
7139303619STeresa Johnson     cl::desc("Only import functions with less than N instructions"));
7239303619STeresa Johnson 
7340641748SMehdi Amini static cl::opt<float>
7440641748SMehdi Amini     ImportInstrFactor("import-instr-evolution-factor", cl::init(0.7),
7540641748SMehdi Amini                       cl::Hidden, cl::value_desc("x"),
7640641748SMehdi Amini                       cl::desc("As we import functions, multiply the "
7740641748SMehdi Amini                                "`import-instr-limit` threshold by this factor "
7840641748SMehdi Amini                                "before processing newly imported functions"));
79ba72b95fSPiotr Padlewski 
80d2869473SPiotr Padlewski static cl::opt<float> ImportHotInstrFactor(
81d2869473SPiotr Padlewski     "import-hot-evolution-factor", cl::init(1.0), cl::Hidden,
82d2869473SPiotr Padlewski     cl::value_desc("x"),
83d2869473SPiotr Padlewski     cl::desc("As we import functions called from hot callsite, multiply the "
84d2869473SPiotr Padlewski              "`import-instr-limit` threshold by this factor "
85d2869473SPiotr Padlewski              "before processing newly imported functions"));
86d2869473SPiotr Padlewski 
87d9830eb7SPiotr Padlewski static cl::opt<float> ImportHotMultiplier(
888260d665SDehao Chen     "import-hot-multiplier", cl::init(10.0), cl::Hidden, cl::value_desc("x"),
89ba72b95fSPiotr Padlewski     cl::desc("Multiply the `import-instr-limit` threshold for hot callsites"));
90ba72b95fSPiotr Padlewski 
9164c46574SDehao Chen static cl::opt<float> ImportCriticalMultiplier(
9264c46574SDehao Chen     "import-critical-multiplier", cl::init(100.0), cl::Hidden,
9364c46574SDehao Chen     cl::value_desc("x"),
9464c46574SDehao Chen     cl::desc(
9564c46574SDehao Chen         "Multiply the `import-instr-limit` threshold for critical callsites"));
9664c46574SDehao Chen 
97ba72b95fSPiotr Padlewski // FIXME: This multiplier was not really tuned up.
98ba72b95fSPiotr Padlewski static cl::opt<float> ImportColdMultiplier(
99ba72b95fSPiotr Padlewski     "import-cold-multiplier", cl::init(0), cl::Hidden, cl::value_desc("N"),
100ba72b95fSPiotr Padlewski     cl::desc("Multiply the `import-instr-limit` threshold for cold callsites"));
10140641748SMehdi Amini 
102d29478f7STeresa Johnson static cl::opt<bool> PrintImports("print-imports", cl::init(false), cl::Hidden,
103d29478f7STeresa Johnson                                   cl::desc("Print imported functions"));
104d29478f7STeresa Johnson 
1056c475a75STeresa Johnson static cl::opt<bool> ComputeDead("compute-dead", cl::init(true), cl::Hidden,
1066c475a75STeresa Johnson                                  cl::desc("Compute dead symbols"));
1076c475a75STeresa Johnson 
1083b776128SPiotr Padlewski static cl::opt<bool> EnableImportMetadata(
1093b776128SPiotr Padlewski     "enable-import-metadata", cl::init(
1103b776128SPiotr Padlewski #if !defined(NDEBUG)
1113b776128SPiotr Padlewski                                   true /*Enabled with asserts.*/
1123b776128SPiotr Padlewski #else
1133b776128SPiotr Padlewski                                   false
1143b776128SPiotr Padlewski #endif
1153b776128SPiotr Padlewski                                   ),
1163b776128SPiotr Padlewski     cl::Hidden, cl::desc("Enable import metadata like 'thinlto_src_module'"));
1173b776128SPiotr Padlewski 
118e9ea08a0SEugene Zelenko /// Summary file to use for function importing when using -function-import from
119e9ea08a0SEugene Zelenko /// the command line.
120e9ea08a0SEugene Zelenko static cl::opt<std::string>
121e9ea08a0SEugene Zelenko     SummaryFile("summary-file",
122e9ea08a0SEugene Zelenko                 cl::desc("The summary file to use for function importing."));
123e9ea08a0SEugene Zelenko 
12481bbf742STeresa Johnson /// Used when testing importing from distributed indexes via opt
12581bbf742STeresa Johnson // -function-import.
12681bbf742STeresa Johnson static cl::opt<bool>
12781bbf742STeresa Johnson     ImportAllIndex("import-all-index",
12881bbf742STeresa Johnson                    cl::desc("Import all external functions in index."));
12981bbf742STeresa Johnson 
13042418abaSMehdi Amini // Load lazily a module from \p FileName in \p Context.
13142418abaSMehdi Amini static std::unique_ptr<Module> loadFile(const std::string &FileName,
13242418abaSMehdi Amini                                         LLVMContext &Context) {
13342418abaSMehdi Amini   SMDiagnostic Err;
13442418abaSMehdi Amini   DEBUG(dbgs() << "Loading '" << FileName << "'\n");
1356cba37ceSTeresa Johnson   // Metadata isn't loaded until functions are imported, to minimize
1366cba37ceSTeresa Johnson   // the memory overhead.
137a1080ee6STeresa Johnson   std::unique_ptr<Module> Result =
138a1080ee6STeresa Johnson       getLazyIRFileModule(FileName, Err, Context,
139a1080ee6STeresa Johnson                           /* ShouldLazyLoadMetadata = */ true);
14042418abaSMehdi Amini   if (!Result) {
14142418abaSMehdi Amini     Err.print("function-import", errs());
142d7ad221cSMehdi Amini     report_fatal_error("Abort");
14342418abaSMehdi Amini   }
14442418abaSMehdi Amini 
14542418abaSMehdi Amini   return Result;
14642418abaSMehdi Amini }
14742418abaSMehdi Amini 
14801e32130SMehdi Amini /// Given a list of possible callee implementation for a call site, select one
14901e32130SMehdi Amini /// that fits the \p Threshold.
15001e32130SMehdi Amini ///
15101e32130SMehdi Amini /// FIXME: select "best" instead of first that fits. But what is "best"?
15201e32130SMehdi Amini /// - The smallest: more likely to be inlined.
15301e32130SMehdi Amini /// - The one with the least outgoing edges (already well optimized).
15401e32130SMehdi Amini /// - One from a module already being imported from in order to reduce the
15501e32130SMehdi Amini ///   number of source modules parsed/linked.
15601e32130SMehdi Amini /// - One that has PGO data attached.
15701e32130SMehdi Amini /// - [insert you fancy metric here]
1582d28f7aaSMehdi Amini static const GlobalValueSummary *
159b4e1e829SMehdi Amini selectCallee(const ModuleSummaryIndex &Index,
1609667b91bSPeter Collingbourne              ArrayRef<std::unique_ptr<GlobalValueSummary>> CalleeSummaryList,
16183aaf358STeresa Johnson              unsigned Threshold, StringRef CallerModulePath) {
16201e32130SMehdi Amini   auto It = llvm::find_if(
16328e457bcSTeresa Johnson       CalleeSummaryList,
16428e457bcSTeresa Johnson       [&](const std::unique_ptr<GlobalValueSummary> &SummaryPtr) {
16528e457bcSTeresa Johnson         auto *GVSummary = SummaryPtr.get();
16673305f82STeresa Johnson         // For SamplePGO, in computeImportForFunction the OriginalId
16773305f82STeresa Johnson         // may have been used to locate the callee summary list (See
16873305f82STeresa Johnson         // comment there).
16973305f82STeresa Johnson         // The mapping from OriginalId to GUID may return a GUID
17073305f82STeresa Johnson         // that corresponds to a static variable. Filter it out here.
17173305f82STeresa Johnson         // This can happen when
17273305f82STeresa Johnson         // 1) There is a call to a library function which is not defined
17373305f82STeresa Johnson         // in the index.
17473305f82STeresa Johnson         // 2) There is a static variable with the  OriginalGUID identical
17573305f82STeresa Johnson         // to the GUID of the library function in 1);
17673305f82STeresa Johnson         // When this happens, the logic for SamplePGO kicks in and
17773305f82STeresa Johnson         // the static variable in 2) will be found, which needs to be
17873305f82STeresa Johnson         // filtered out.
17973305f82STeresa Johnson         if (GVSummary->getSummaryKind() == GlobalValueSummary::GlobalVarKind)
18073305f82STeresa Johnson           return false;
181f329be83SRafael Espindola         if (GlobalValue::isInterposableLinkage(GVSummary->linkage()))
1825b85d8d6SMehdi Amini           // There is no point in importing these, we can't inline them
18301e32130SMehdi Amini           return false;
1842c719cc1SMehdi Amini 
18581bbf742STeresa Johnson         auto *Summary = cast<FunctionSummary>(GVSummary->getBaseObject());
1867e88d0daSMehdi Amini 
18783aaf358STeresa Johnson         // If this is a local function, make sure we import the copy
18883aaf358STeresa Johnson         // in the caller's module. The only time a local function can
18983aaf358STeresa Johnson         // share an entry in the index is if there is a local with the same name
19083aaf358STeresa Johnson         // in another module that had the same source file name (in a different
19183aaf358STeresa Johnson         // directory), where each was compiled in their own directory so there
19283aaf358STeresa Johnson         // was not distinguishing path.
19383aaf358STeresa Johnson         // However, do the import from another module if there is only one
19483aaf358STeresa Johnson         // entry in the list - in that case this must be a reference due
19583aaf358STeresa Johnson         // to indirect call profile data, since a function pointer can point to
19683aaf358STeresa Johnson         // a local in another module.
19783aaf358STeresa Johnson         if (GlobalValue::isLocalLinkage(Summary->linkage()) &&
19883aaf358STeresa Johnson             CalleeSummaryList.size() > 1 &&
19983aaf358STeresa Johnson             Summary->modulePath() != CallerModulePath)
20083aaf358STeresa Johnson           return false;
20183aaf358STeresa Johnson 
202f9dc3deaSTeresa Johnson         if (Summary->instCount() > Threshold)
203f9dc3deaSTeresa Johnson           return false;
204f9dc3deaSTeresa Johnson 
205519465b9STeresa Johnson         if (Summary->notEligibleToImport())
206b4e1e829SMehdi Amini           return false;
207b4e1e829SMehdi Amini 
20801e32130SMehdi Amini         return true;
20901e32130SMehdi Amini       });
21028e457bcSTeresa Johnson   if (It == CalleeSummaryList.end())
21101e32130SMehdi Amini     return nullptr;
2127e88d0daSMehdi Amini 
213f9dc3deaSTeresa Johnson   return cast<GlobalValueSummary>(It->get());
214434e9561SRafael Espindola }
2157e88d0daSMehdi Amini 
216e9ea08a0SEugene Zelenko namespace {
217e9ea08a0SEugene Zelenko 
218475b51a7STeresa Johnson using EdgeInfo = std::tuple<const FunctionSummary *, unsigned /* Threshold */,
219475b51a7STeresa Johnson                             GlobalValue::GUID>;
22001e32130SMehdi Amini 
221e9ea08a0SEugene Zelenko } // anonymous namespace
222e9ea08a0SEugene Zelenko 
2231958083dSTeresa Johnson static ValueInfo
2241958083dSTeresa Johnson updateValueInfoForIndirectCalls(const ModuleSummaryIndex &Index, ValueInfo VI) {
2251958083dSTeresa Johnson   if (!VI.getSummaryList().empty())
2261958083dSTeresa Johnson     return VI;
2271958083dSTeresa Johnson   // For SamplePGO, the indirect call targets for local functions will
2281958083dSTeresa Johnson   // have its original name annotated in profile. We try to find the
2291958083dSTeresa Johnson   // corresponding PGOFuncName as the GUID.
2301958083dSTeresa Johnson   // FIXME: Consider updating the edges in the graph after building
2311958083dSTeresa Johnson   // it, rather than needing to perform this mapping on each walk.
2321958083dSTeresa Johnson   auto GUID = Index.getGUIDFromOriginalID(VI.getGUID());
2331958083dSTeresa Johnson   if (GUID == 0)
234*28d8a49fSEugene Leviant     return ValueInfo();
2351958083dSTeresa Johnson   return Index.getValueInfo(GUID);
2361958083dSTeresa Johnson }
2371958083dSTeresa Johnson 
23801e32130SMehdi Amini /// Compute the list of functions to import for a given caller. Mark these
23901e32130SMehdi Amini /// imported functions and the symbols they reference in their source module as
24001e32130SMehdi Amini /// exported from their source module.
24101e32130SMehdi Amini static void computeImportForFunction(
2423255eec1STeresa Johnson     const FunctionSummary &Summary, const ModuleSummaryIndex &Index,
243d9830eb7SPiotr Padlewski     const unsigned Threshold, const GVSummaryMapTy &DefinedGVSummaries,
24401e32130SMehdi Amini     SmallVectorImpl<EdgeInfo> &Worklist,
2459b490f10SMehdi Amini     FunctionImporter::ImportMapTy &ImportList,
246c86af334STeresa Johnson     StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) {
24701e32130SMehdi Amini   for (auto &Edge : Summary.calls()) {
2489667b91bSPeter Collingbourne     ValueInfo VI = Edge.first;
2499667b91bSPeter Collingbourne     DEBUG(dbgs() << " edge -> " << VI.getGUID() << " Threshold:" << Threshold
2509667b91bSPeter Collingbourne                  << "\n");
25101e32130SMehdi Amini 
2521958083dSTeresa Johnson     VI = updateValueInfoForIndirectCalls(Index, VI);
2539667b91bSPeter Collingbourne     if (!VI)
2549667b91bSPeter Collingbourne       continue;
2554a435e08SDehao Chen 
2569667b91bSPeter Collingbourne     if (DefinedGVSummaries.count(VI.getGUID())) {
25701e32130SMehdi Amini       DEBUG(dbgs() << "ignored! Target already in destination module.\n");
2587e88d0daSMehdi Amini       continue;
259d450da32STeresa Johnson     }
26040641748SMehdi Amini 
261ba72b95fSPiotr Padlewski     auto GetBonusMultiplier = [](CalleeInfo::HotnessType Hotness) -> float {
262ba72b95fSPiotr Padlewski       if (Hotness == CalleeInfo::HotnessType::Hot)
263ba72b95fSPiotr Padlewski         return ImportHotMultiplier;
264ba72b95fSPiotr Padlewski       if (Hotness == CalleeInfo::HotnessType::Cold)
265ba72b95fSPiotr Padlewski         return ImportColdMultiplier;
26664c46574SDehao Chen       if (Hotness == CalleeInfo::HotnessType::Critical)
26764c46574SDehao Chen         return ImportCriticalMultiplier;
268ba72b95fSPiotr Padlewski       return 1.0;
269ba72b95fSPiotr Padlewski     };
270ba72b95fSPiotr Padlewski 
271d9830eb7SPiotr Padlewski     const auto NewThreshold =
272ba72b95fSPiotr Padlewski         Threshold * GetBonusMultiplier(Edge.second.Hotness);
273d2869473SPiotr Padlewski 
2749667b91bSPeter Collingbourne     auto *CalleeSummary = selectCallee(Index, VI.getSummaryList(), NewThreshold,
2759667b91bSPeter Collingbourne                                        Summary.modulePath());
27601e32130SMehdi Amini     if (!CalleeSummary) {
27701e32130SMehdi Amini       DEBUG(dbgs() << "ignored! No qualifying callee with summary found.\n");
2787e88d0daSMehdi Amini       continue;
2797e88d0daSMehdi Amini     }
2802f0cc477SDavid Blaikie 
2812f0cc477SDavid Blaikie     // "Resolve" the summary
28281bbf742STeresa Johnson     const auto *ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary->getBaseObject());
2832d28f7aaSMehdi Amini 
284d9830eb7SPiotr Padlewski     assert(ResolvedCalleeSummary->instCount() <= NewThreshold &&
28501e32130SMehdi Amini            "selectCallee() didn't honor the threshold");
28601e32130SMehdi Amini 
287d2869473SPiotr Padlewski     auto GetAdjustedThreshold = [](unsigned Threshold, bool IsHotCallsite) {
288d2869473SPiotr Padlewski       // Adjust the threshold for next level of imported functions.
289d2869473SPiotr Padlewski       // The threshold is different for hot callsites because we can then
290d2869473SPiotr Padlewski       // inline chains of hot calls.
291d2869473SPiotr Padlewski       if (IsHotCallsite)
292d2869473SPiotr Padlewski         return Threshold * ImportHotInstrFactor;
293d2869473SPiotr Padlewski       return Threshold * ImportInstrFactor;
294d2869473SPiotr Padlewski     };
295d2869473SPiotr Padlewski 
296d2869473SPiotr Padlewski     bool IsHotCallsite = Edge.second.Hotness == CalleeInfo::HotnessType::Hot;
2971b859a23STeresa Johnson     const auto AdjThreshold = GetAdjustedThreshold(Threshold, IsHotCallsite);
2981b859a23STeresa Johnson 
2991b859a23STeresa Johnson     auto ExportModulePath = ResolvedCalleeSummary->modulePath();
3009667b91bSPeter Collingbourne     auto &ProcessedThreshold = ImportList[ExportModulePath][VI.getGUID()];
3011b859a23STeresa Johnson     /// Since the traversal of the call graph is DFS, we can revisit a function
3021b859a23STeresa Johnson     /// a second time with a higher threshold. In this case, it is added back to
3031b859a23STeresa Johnson     /// the worklist with the new threshold.
3041b859a23STeresa Johnson     if (ProcessedThreshold && ProcessedThreshold >= AdjThreshold) {
3051b859a23STeresa Johnson       DEBUG(dbgs() << "ignored! Target was already seen with Threshold "
3061b859a23STeresa Johnson                    << ProcessedThreshold << "\n");
3071b859a23STeresa Johnson       continue;
3081b859a23STeresa Johnson     }
30919f2aa78STeresa Johnson     bool PreviouslyImported = ProcessedThreshold != 0;
3101b859a23STeresa Johnson     // Mark this function as imported in this module, with the current Threshold
3111b859a23STeresa Johnson     ProcessedThreshold = AdjThreshold;
3121b859a23STeresa Johnson 
3131b859a23STeresa Johnson     // Make exports in the source module.
3141b859a23STeresa Johnson     if (ExportLists) {
3151b859a23STeresa Johnson       auto &ExportList = (*ExportLists)[ExportModulePath];
3169667b91bSPeter Collingbourne       ExportList.insert(VI.getGUID());
31719f2aa78STeresa Johnson       if (!PreviouslyImported) {
31819f2aa78STeresa Johnson         // This is the first time this function was exported from its source
31919f2aa78STeresa Johnson         // module, so mark all functions and globals it references as exported
3201b859a23STeresa Johnson         // to the outside if they are defined in the same source module.
321edddca22STeresa Johnson         // For efficiency, we unconditionally add all the referenced GUIDs
322edddca22STeresa Johnson         // to the ExportList for this module, and will prune out any not
323edddca22STeresa Johnson         // defined in the module later in a single pass.
3241b859a23STeresa Johnson         for (auto &Edge : ResolvedCalleeSummary->calls()) {
3251b859a23STeresa Johnson           auto CalleeGUID = Edge.first.getGUID();
326edddca22STeresa Johnson           ExportList.insert(CalleeGUID);
3271b859a23STeresa Johnson         }
3281b859a23STeresa Johnson         for (auto &Ref : ResolvedCalleeSummary->refs()) {
3291b859a23STeresa Johnson           auto GUID = Ref.getGUID();
330edddca22STeresa Johnson           ExportList.insert(GUID);
3311b859a23STeresa Johnson         }
3321b859a23STeresa Johnson       }
33319f2aa78STeresa Johnson     }
334d2869473SPiotr Padlewski 
33501e32130SMehdi Amini     // Insert the newly imported function to the worklist.
3369667b91bSPeter Collingbourne     Worklist.emplace_back(ResolvedCalleeSummary, AdjThreshold, VI.getGUID());
337d450da32STeresa Johnson   }
338d450da32STeresa Johnson }
339d450da32STeresa Johnson 
34001e32130SMehdi Amini /// Given the list of globals defined in a module, compute the list of imports
34101e32130SMehdi Amini /// as well as the list of "exports", i.e. the list of symbols referenced from
34201e32130SMehdi Amini /// another module (that may require promotion).
34301e32130SMehdi Amini static void ComputeImportForModule(
344c851d216STeresa Johnson     const GVSummaryMapTy &DefinedGVSummaries, const ModuleSummaryIndex &Index,
3459b490f10SMehdi Amini     FunctionImporter::ImportMapTy &ImportList,
34656584bbfSEvgeniy Stepanov     StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) {
34701e32130SMehdi Amini   // Worklist contains the list of function imported in this module, for which
34801e32130SMehdi Amini   // we will analyse the callees and may import further down the callgraph.
34901e32130SMehdi Amini   SmallVector<EdgeInfo, 128> Worklist;
35001e32130SMehdi Amini 
35101e32130SMehdi Amini   // Populate the worklist with the import for the functions in the current
35201e32130SMehdi Amini   // module
35328e457bcSTeresa Johnson   for (auto &GVSummary : DefinedGVSummaries) {
35456584bbfSEvgeniy Stepanov     if (!Index.isGlobalValueLive(GVSummary.second)) {
3556c475a75STeresa Johnson       DEBUG(dbgs() << "Ignores Dead GUID: " << GVSummary.first << "\n");
3566c475a75STeresa Johnson       continue;
3576c475a75STeresa Johnson     }
358cfbd0892SPeter Collingbourne     auto *FuncSummary =
359cfbd0892SPeter Collingbourne         dyn_cast<FunctionSummary>(GVSummary.second->getBaseObject());
3601aafabf7SMehdi Amini     if (!FuncSummary)
3611aafabf7SMehdi Amini       // Skip import for global variables
3621aafabf7SMehdi Amini       continue;
36324524f31SXinliang David Li     DEBUG(dbgs() << "Initialize import for " << GVSummary.first << "\n");
3642d28f7aaSMehdi Amini     computeImportForFunction(*FuncSummary, Index, ImportInstrLimit,
3659b490f10SMehdi Amini                              DefinedGVSummaries, Worklist, ImportList,
36601e32130SMehdi Amini                              ExportLists);
36701e32130SMehdi Amini   }
36801e32130SMehdi Amini 
369d2869473SPiotr Padlewski   // Process the newly imported functions and add callees to the worklist.
37042418abaSMehdi Amini   while (!Worklist.empty()) {
37101e32130SMehdi Amini     auto FuncInfo = Worklist.pop_back_val();
372475b51a7STeresa Johnson     auto *Summary = std::get<0>(FuncInfo);
373475b51a7STeresa Johnson     auto Threshold = std::get<1>(FuncInfo);
374475b51a7STeresa Johnson     auto GUID = std::get<2>(FuncInfo);
375475b51a7STeresa Johnson 
376475b51a7STeresa Johnson     // Check if we later added this summary with a higher threshold.
377475b51a7STeresa Johnson     // If so, skip this entry.
378475b51a7STeresa Johnson     auto ExportModulePath = Summary->modulePath();
379475b51a7STeresa Johnson     auto &LatestProcessedThreshold = ImportList[ExportModulePath][GUID];
380475b51a7STeresa Johnson     if (LatestProcessedThreshold > Threshold)
381475b51a7STeresa Johnson       continue;
38242418abaSMehdi Amini 
3831aafabf7SMehdi Amini     computeImportForFunction(*Summary, Index, Threshold, DefinedGVSummaries,
3849b490f10SMehdi Amini                              Worklist, ImportList, ExportLists);
385c8c55170SMehdi Amini   }
38642418abaSMehdi Amini }
387ffe2e4aaSMehdi Amini 
388c86af334STeresa Johnson /// Compute all the import and export for every module using the Index.
38901e32130SMehdi Amini void llvm::ComputeCrossModuleImport(
39001e32130SMehdi Amini     const ModuleSummaryIndex &Index,
391c851d216STeresa Johnson     const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
39201e32130SMehdi Amini     StringMap<FunctionImporter::ImportMapTy> &ImportLists,
39356584bbfSEvgeniy Stepanov     StringMap<FunctionImporter::ExportSetTy> &ExportLists) {
39401e32130SMehdi Amini   // For each module that has function defined, compute the import/export lists.
3951aafabf7SMehdi Amini   for (auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) {
3969b490f10SMehdi Amini     auto &ImportList = ImportLists[DefinedGVSummaries.first()];
3971aafabf7SMehdi Amini     DEBUG(dbgs() << "Computing import for Module '"
3981aafabf7SMehdi Amini                  << DefinedGVSummaries.first() << "'\n");
3999b490f10SMehdi Amini     ComputeImportForModule(DefinedGVSummaries.second, Index, ImportList,
40056584bbfSEvgeniy Stepanov                            &ExportLists);
40101e32130SMehdi Amini   }
40201e32130SMehdi Amini 
403edddca22STeresa Johnson   // When computing imports we added all GUIDs referenced by anything
404edddca22STeresa Johnson   // imported from the module to its ExportList. Now we prune each ExportList
405edddca22STeresa Johnson   // of any not defined in that module. This is more efficient than checking
406edddca22STeresa Johnson   // while computing imports because some of the summary lists may be long
407edddca22STeresa Johnson   // due to linkonce (comdat) copies.
408edddca22STeresa Johnson   for (auto &ELI : ExportLists) {
409edddca22STeresa Johnson     const auto &DefinedGVSummaries =
410edddca22STeresa Johnson         ModuleToDefinedGVSummaries.lookup(ELI.first());
411edddca22STeresa Johnson     for (auto EI = ELI.second.begin(); EI != ELI.second.end();) {
412edddca22STeresa Johnson       if (!DefinedGVSummaries.count(*EI))
413edddca22STeresa Johnson         EI = ELI.second.erase(EI);
414edddca22STeresa Johnson       else
415edddca22STeresa Johnson         ++EI;
416edddca22STeresa Johnson     }
417edddca22STeresa Johnson   }
418edddca22STeresa Johnson 
41901e32130SMehdi Amini #ifndef NDEBUG
42001e32130SMehdi Amini   DEBUG(dbgs() << "Import/Export lists for " << ImportLists.size()
42101e32130SMehdi Amini                << " modules:\n");
42201e32130SMehdi Amini   for (auto &ModuleImports : ImportLists) {
42301e32130SMehdi Amini     auto ModName = ModuleImports.first();
42401e32130SMehdi Amini     auto &Exports = ExportLists[ModName];
42501e32130SMehdi Amini     DEBUG(dbgs() << "* Module " << ModName << " exports " << Exports.size()
42601e32130SMehdi Amini                  << " functions. Imports from " << ModuleImports.second.size()
42701e32130SMehdi Amini                  << " modules.\n");
42801e32130SMehdi Amini     for (auto &Src : ModuleImports.second) {
42901e32130SMehdi Amini       auto SrcModName = Src.first();
43001e32130SMehdi Amini       DEBUG(dbgs() << " - " << Src.second.size() << " functions imported from "
43101e32130SMehdi Amini                    << SrcModName << "\n");
43201e32130SMehdi Amini     }
43301e32130SMehdi Amini   }
43401e32130SMehdi Amini #endif
43501e32130SMehdi Amini }
43601e32130SMehdi Amini 
43781bbf742STeresa Johnson #ifndef NDEBUG
43881bbf742STeresa Johnson static void dumpImportListForModule(StringRef ModulePath,
43981bbf742STeresa Johnson                                     FunctionImporter::ImportMapTy &ImportList) {
44081bbf742STeresa Johnson   DEBUG(dbgs() << "* Module " << ModulePath << " imports from "
44181bbf742STeresa Johnson                << ImportList.size() << " modules.\n");
44281bbf742STeresa Johnson   for (auto &Src : ImportList) {
44381bbf742STeresa Johnson     auto SrcModName = Src.first();
44481bbf742STeresa Johnson     DEBUG(dbgs() << " - " << Src.second.size() << " functions imported from "
44581bbf742STeresa Johnson                  << SrcModName << "\n");
44681bbf742STeresa Johnson   }
44781bbf742STeresa Johnson }
44869b2de84STeresa Johnson #endif
44981bbf742STeresa Johnson 
450c86af334STeresa Johnson /// Compute all the imports for the given module in the Index.
451c86af334STeresa Johnson void llvm::ComputeCrossModuleImportForModule(
452c86af334STeresa Johnson     StringRef ModulePath, const ModuleSummaryIndex &Index,
453c86af334STeresa Johnson     FunctionImporter::ImportMapTy &ImportList) {
454c86af334STeresa Johnson   // Collect the list of functions this module defines.
455c86af334STeresa Johnson   // GUID -> Summary
456c851d216STeresa Johnson   GVSummaryMapTy FunctionSummaryMap;
45728e457bcSTeresa Johnson   Index.collectDefinedFunctionsForModule(ModulePath, FunctionSummaryMap);
458c86af334STeresa Johnson 
459c86af334STeresa Johnson   // Compute the import list for this module.
460c86af334STeresa Johnson   DEBUG(dbgs() << "Computing import for Module '" << ModulePath << "'\n");
46128e457bcSTeresa Johnson   ComputeImportForModule(FunctionSummaryMap, Index, ImportList);
462c86af334STeresa Johnson 
463c86af334STeresa Johnson #ifndef NDEBUG
46481bbf742STeresa Johnson   dumpImportListForModule(ModulePath, ImportList);
46581bbf742STeresa Johnson #endif
466c86af334STeresa Johnson }
46781bbf742STeresa Johnson 
46881bbf742STeresa Johnson // Mark all external summaries in Index for import into the given module.
46981bbf742STeresa Johnson // Used for distributed builds using a distributed index.
47081bbf742STeresa Johnson void llvm::ComputeCrossModuleImportForModuleFromIndex(
47181bbf742STeresa Johnson     StringRef ModulePath, const ModuleSummaryIndex &Index,
47281bbf742STeresa Johnson     FunctionImporter::ImportMapTy &ImportList) {
47381bbf742STeresa Johnson   for (auto &GlobalList : Index) {
47481bbf742STeresa Johnson     // Ignore entries for undefined references.
47581bbf742STeresa Johnson     if (GlobalList.second.SummaryList.empty())
47681bbf742STeresa Johnson       continue;
47781bbf742STeresa Johnson 
47881bbf742STeresa Johnson     auto GUID = GlobalList.first;
47981bbf742STeresa Johnson     assert(GlobalList.second.SummaryList.size() == 1 &&
48081bbf742STeresa Johnson            "Expected individual combined index to have one summary per GUID");
48181bbf742STeresa Johnson     auto &Summary = GlobalList.second.SummaryList[0];
48281bbf742STeresa Johnson     // Skip the summaries for the importing module. These are included to
48381bbf742STeresa Johnson     // e.g. record required linkage changes.
48481bbf742STeresa Johnson     if (Summary->modulePath() == ModulePath)
48581bbf742STeresa Johnson       continue;
48681bbf742STeresa Johnson     // Doesn't matter what value we plug in to the map, just needs an entry
48781bbf742STeresa Johnson     // to provoke importing by thinBackend.
48881bbf742STeresa Johnson     ImportList[Summary->modulePath()][GUID] = 1;
48981bbf742STeresa Johnson   }
49081bbf742STeresa Johnson #ifndef NDEBUG
49181bbf742STeresa Johnson   dumpImportListForModule(ModulePath, ImportList);
492c86af334STeresa Johnson #endif
493c86af334STeresa Johnson }
494c86af334STeresa Johnson 
49556584bbfSEvgeniy Stepanov void llvm::computeDeadSymbols(
49656584bbfSEvgeniy Stepanov     ModuleSummaryIndex &Index,
4976c475a75STeresa Johnson     const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
49856584bbfSEvgeniy Stepanov   assert(!Index.withGlobalValueDeadStripping());
4996c475a75STeresa Johnson   if (!ComputeDead)
50056584bbfSEvgeniy Stepanov     return;
5016c475a75STeresa Johnson   if (GUIDPreservedSymbols.empty())
5026c475a75STeresa Johnson     // Don't do anything when nothing is live, this is friendly with tests.
50356584bbfSEvgeniy Stepanov     return;
50456584bbfSEvgeniy Stepanov   unsigned LiveSymbols = 0;
5059667b91bSPeter Collingbourne   SmallVector<ValueInfo, 128> Worklist;
5069667b91bSPeter Collingbourne   Worklist.reserve(GUIDPreservedSymbols.size() * 2);
5079667b91bSPeter Collingbourne   for (auto GUID : GUIDPreservedSymbols) {
5089667b91bSPeter Collingbourne     ValueInfo VI = Index.getValueInfo(GUID);
5099667b91bSPeter Collingbourne     if (!VI)
5109667b91bSPeter Collingbourne       continue;
51156584bbfSEvgeniy Stepanov     for (auto &S : VI.getSummaryList())
51256584bbfSEvgeniy Stepanov       S->setLive(true);
5136c475a75STeresa Johnson   }
51456584bbfSEvgeniy Stepanov 
5156c475a75STeresa Johnson   // Add values flagged in the index as live roots to the worklist.
51656584bbfSEvgeniy Stepanov   for (const auto &Entry : Index)
51756584bbfSEvgeniy Stepanov     for (auto &S : Entry.second.SummaryList)
51856584bbfSEvgeniy Stepanov       if (S->isLive()) {
51956584bbfSEvgeniy Stepanov         DEBUG(dbgs() << "Live root: " << Entry.first << "\n");
520*28d8a49fSEugene Leviant         Worklist.push_back(ValueInfo(/*IsAnalysis=*/false, &Entry));
52156584bbfSEvgeniy Stepanov         ++LiveSymbols;
52256584bbfSEvgeniy Stepanov         break;
5236c475a75STeresa Johnson       }
5246c475a75STeresa Johnson 
52556584bbfSEvgeniy Stepanov   // Make value live and add it to the worklist if it was not live before.
52656584bbfSEvgeniy Stepanov   // FIXME: we should only make the prevailing copy live here
52756584bbfSEvgeniy Stepanov   auto visit = [&](ValueInfo VI) {
5281958083dSTeresa Johnson     // FIXME: If we knew which edges were created for indirect call profiles,
5291958083dSTeresa Johnson     // we could skip them here. Any that are live should be reached via
5301958083dSTeresa Johnson     // other edges, e.g. reference edges. Otherwise, using a profile collected
5311958083dSTeresa Johnson     // on a slightly different binary might provoke preserving, importing
5321958083dSTeresa Johnson     // and ultimately promoting calls to functions not linked into this
5331958083dSTeresa Johnson     // binary, which increases the binary size unnecessarily. Note that
5341958083dSTeresa Johnson     // if this code changes, the importer needs to change so that edges
5351958083dSTeresa Johnson     // to functions marked dead are skipped.
5361958083dSTeresa Johnson     VI = updateValueInfoForIndirectCalls(Index, VI);
5371958083dSTeresa Johnson     if (!VI)
5381958083dSTeresa Johnson       return;
53956584bbfSEvgeniy Stepanov     for (auto &S : VI.getSummaryList())
540f625118eSTeresa Johnson       if (S->isLive())
541f625118eSTeresa Johnson         return;
542f625118eSTeresa Johnson     for (auto &S : VI.getSummaryList())
54356584bbfSEvgeniy Stepanov       S->setLive(true);
54456584bbfSEvgeniy Stepanov     ++LiveSymbols;
54556584bbfSEvgeniy Stepanov     Worklist.push_back(VI);
54656584bbfSEvgeniy Stepanov   };
54756584bbfSEvgeniy Stepanov 
5486c475a75STeresa Johnson   while (!Worklist.empty()) {
5499667b91bSPeter Collingbourne     auto VI = Worklist.pop_back_val();
5509667b91bSPeter Collingbourne     for (auto &Summary : VI.getSummaryList()) {
551cfbd0892SPeter Collingbourne       GlobalValueSummary *Base = Summary->getBaseObject();
552cfbd0892SPeter Collingbourne       for (auto Ref : Base->refs())
55356584bbfSEvgeniy Stepanov         visit(Ref);
554cfbd0892SPeter Collingbourne       if (auto *FS = dyn_cast<FunctionSummary>(Base))
55556584bbfSEvgeniy Stepanov         for (auto Call : FS->calls())
55656584bbfSEvgeniy Stepanov           visit(Call.first);
5576c475a75STeresa Johnson     }
5586c475a75STeresa Johnson   }
55956584bbfSEvgeniy Stepanov   Index.setWithGlobalValueDeadStripping();
56056584bbfSEvgeniy Stepanov 
56156584bbfSEvgeniy Stepanov   unsigned DeadSymbols = Index.size() - LiveSymbols;
56256584bbfSEvgeniy Stepanov   DEBUG(dbgs() << LiveSymbols << " symbols Live, and " << DeadSymbols
56356584bbfSEvgeniy Stepanov                << " symbols Dead \n");
56456584bbfSEvgeniy Stepanov   NumDeadSymbols += DeadSymbols;
56556584bbfSEvgeniy Stepanov   NumLiveSymbols += LiveSymbols;
5666c475a75STeresa Johnson }
5676c475a75STeresa Johnson 
56884174c37STeresa Johnson /// Compute the set of summaries needed for a ThinLTO backend compilation of
56984174c37STeresa Johnson /// \p ModulePath.
57084174c37STeresa Johnson void llvm::gatherImportedSummariesForModule(
57184174c37STeresa Johnson     StringRef ModulePath,
57284174c37STeresa Johnson     const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
573cdbcbf74SMehdi Amini     const FunctionImporter::ImportMapTy &ImportList,
57484174c37STeresa Johnson     std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
57584174c37STeresa Johnson   // Include all summaries from the importing module.
57684174c37STeresa Johnson   ModuleToSummariesForIndex[ModulePath] =
57784174c37STeresa Johnson       ModuleToDefinedGVSummaries.lookup(ModulePath);
57884174c37STeresa Johnson   // Include summaries for imports.
57988c491ddSMehdi Amini   for (auto &ILI : ImportList) {
58084174c37STeresa Johnson     auto &SummariesForIndex = ModuleToSummariesForIndex[ILI.first()];
58184174c37STeresa Johnson     const auto &DefinedGVSummaries =
58284174c37STeresa Johnson         ModuleToDefinedGVSummaries.lookup(ILI.first());
58384174c37STeresa Johnson     for (auto &GI : ILI.second) {
58484174c37STeresa Johnson       const auto &DS = DefinedGVSummaries.find(GI.first);
58584174c37STeresa Johnson       assert(DS != DefinedGVSummaries.end() &&
58684174c37STeresa Johnson              "Expected a defined summary for imported global value");
58784174c37STeresa Johnson       SummariesForIndex[GI.first] = DS->second;
58884174c37STeresa Johnson     }
58984174c37STeresa Johnson   }
59084174c37STeresa Johnson }
59184174c37STeresa Johnson 
5928570fe47STeresa Johnson /// Emit the files \p ModulePath will import from into \p OutputFilename.
593cdbcbf74SMehdi Amini std::error_code
594cdbcbf74SMehdi Amini llvm::EmitImportsFiles(StringRef ModulePath, StringRef OutputFilename,
595cdbcbf74SMehdi Amini                        const FunctionImporter::ImportMapTy &ModuleImports) {
5968570fe47STeresa Johnson   std::error_code EC;
5978570fe47STeresa Johnson   raw_fd_ostream ImportsOS(OutputFilename, EC, sys::fs::OpenFlags::F_None);
5988570fe47STeresa Johnson   if (EC)
5998570fe47STeresa Johnson     return EC;
600cdbcbf74SMehdi Amini   for (auto &ILI : ModuleImports)
6018570fe47STeresa Johnson     ImportsOS << ILI.first() << "\n";
6028570fe47STeresa Johnson   return std::error_code();
6038570fe47STeresa Johnson }
6048570fe47STeresa Johnson 
60504c9a2d6STeresa Johnson /// Fixup WeakForLinker linkages in \p TheModule based on summary analysis.
60604c9a2d6STeresa Johnson void llvm::thinLTOResolveWeakForLinkerModule(
60704c9a2d6STeresa Johnson     Module &TheModule, const GVSummaryMapTy &DefinedGlobals) {
6084566c6dbSTeresa Johnson   auto ConvertToDeclaration = [](GlobalValue &GV) {
6094566c6dbSTeresa Johnson     DEBUG(dbgs() << "Converting to a declaration: `" << GV.getName() << "\n");
6104566c6dbSTeresa Johnson     if (Function *F = dyn_cast<Function>(&GV)) {
6114566c6dbSTeresa Johnson       F->deleteBody();
6124566c6dbSTeresa Johnson       F->clearMetadata();
6134566c6dbSTeresa Johnson     } else if (GlobalVariable *V = dyn_cast<GlobalVariable>(&GV)) {
6144566c6dbSTeresa Johnson       V->setInitializer(nullptr);
6154566c6dbSTeresa Johnson       V->setLinkage(GlobalValue::ExternalLinkage);
6164566c6dbSTeresa Johnson       V->clearMetadata();
6174566c6dbSTeresa Johnson     } else
6184566c6dbSTeresa Johnson       // For now we don't resolve or drop aliases. Once we do we'll
6194566c6dbSTeresa Johnson       // need to add support here for creating either a function or
6204566c6dbSTeresa Johnson       // variable declaration, and return the new GlobalValue* for
6214566c6dbSTeresa Johnson       // the caller to use.
62291239088SDavide Italiano       llvm_unreachable("Expected function or variable");
6234566c6dbSTeresa Johnson   };
6244566c6dbSTeresa Johnson 
62504c9a2d6STeresa Johnson   auto updateLinkage = [&](GlobalValue &GV) {
62604c9a2d6STeresa Johnson     // See if the global summary analysis computed a new resolved linkage.
62704c9a2d6STeresa Johnson     const auto &GS = DefinedGlobals.find(GV.getGUID());
62804c9a2d6STeresa Johnson     if (GS == DefinedGlobals.end())
62904c9a2d6STeresa Johnson       return;
63004c9a2d6STeresa Johnson     auto NewLinkage = GS->second->linkage();
63104c9a2d6STeresa Johnson     if (NewLinkage == GV.getLinkage())
63204c9a2d6STeresa Johnson       return;
6336a5fbe52SDavide Italiano 
6346a5fbe52SDavide Italiano     // Switch the linkage to weakany if asked for, e.g. we do this for
6356a5fbe52SDavide Italiano     // linker redefined symbols (via --wrap or --defsym).
636f4891d29SDavide Italiano     // We record that the visibility should be changed here in `addThinLTO`
637f4891d29SDavide Italiano     // as we need access to the resolution vectors for each input file in
638f4891d29SDavide Italiano     // order to find which symbols have been redefined.
639f4891d29SDavide Italiano     // We may consider reorganizing this code and moving the linkage recording
640f4891d29SDavide Italiano     // somewhere else, e.g. in thinLTOResolveWeakForLinkerInIndex.
6416a5fbe52SDavide Italiano     if (NewLinkage == GlobalValue::WeakAnyLinkage) {
6426a5fbe52SDavide Italiano       GV.setLinkage(NewLinkage);
6436a5fbe52SDavide Italiano       return;
6446a5fbe52SDavide Italiano     }
6456a5fbe52SDavide Italiano 
6466a5fbe52SDavide Italiano     if (!GlobalValue::isWeakForLinker(GV.getLinkage()))
6476a5fbe52SDavide Italiano       return;
6484566c6dbSTeresa Johnson     // Check for a non-prevailing def that has interposable linkage
6494566c6dbSTeresa Johnson     // (e.g. non-odr weak or linkonce). In that case we can't simply
6504566c6dbSTeresa Johnson     // convert to available_externally, since it would lose the
6514566c6dbSTeresa Johnson     // interposable property and possibly get inlined. Simply drop
6524566c6dbSTeresa Johnson     // the definition in that case.
6534566c6dbSTeresa Johnson     if (GlobalValue::isAvailableExternallyLinkage(NewLinkage) &&
6544566c6dbSTeresa Johnson         GlobalValue::isInterposableLinkage(GV.getLinkage()))
6554566c6dbSTeresa Johnson       ConvertToDeclaration(GV);
6564566c6dbSTeresa Johnson     else {
65704c9a2d6STeresa Johnson       DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName() << "` from "
65804c9a2d6STeresa Johnson                    << GV.getLinkage() << " to " << NewLinkage << "\n");
65904c9a2d6STeresa Johnson       GV.setLinkage(NewLinkage);
6604566c6dbSTeresa Johnson     }
6614566c6dbSTeresa Johnson     // Remove declarations from comdats, including available_externally
6626107a419STeresa Johnson     // as this is a declaration for the linker, and will be dropped eventually.
6636107a419STeresa Johnson     // It is illegal for comdats to contain declarations.
6646107a419STeresa Johnson     auto *GO = dyn_cast_or_null<GlobalObject>(&GV);
6654566c6dbSTeresa Johnson     if (GO && GO->isDeclarationForLinker() && GO->hasComdat())
6666107a419STeresa Johnson       GO->setComdat(nullptr);
66704c9a2d6STeresa Johnson   };
66804c9a2d6STeresa Johnson 
66904c9a2d6STeresa Johnson   // Process functions and global now
67004c9a2d6STeresa Johnson   for (auto &GV : TheModule)
67104c9a2d6STeresa Johnson     updateLinkage(GV);
67204c9a2d6STeresa Johnson   for (auto &GV : TheModule.globals())
67304c9a2d6STeresa Johnson     updateLinkage(GV);
67404c9a2d6STeresa Johnson   for (auto &GV : TheModule.aliases())
67504c9a2d6STeresa Johnson     updateLinkage(GV);
67604c9a2d6STeresa Johnson }
67704c9a2d6STeresa Johnson 
67804c9a2d6STeresa Johnson /// Run internalization on \p TheModule based on symmary analysis.
67904c9a2d6STeresa Johnson void llvm::thinLTOInternalizeModule(Module &TheModule,
68004c9a2d6STeresa Johnson                                     const GVSummaryMapTy &DefinedGlobals) {
68104c9a2d6STeresa Johnson   // Declare a callback for the internalize pass that will ask for every
68204c9a2d6STeresa Johnson   // candidate GlobalValue if it can be internalized or not.
68304c9a2d6STeresa Johnson   auto MustPreserveGV = [&](const GlobalValue &GV) -> bool {
68404c9a2d6STeresa Johnson     // Lookup the linkage recorded in the summaries during global analysis.
685c3d677f9SPeter Collingbourne     auto GS = DefinedGlobals.find(GV.getGUID());
68604c9a2d6STeresa Johnson     if (GS == DefinedGlobals.end()) {
68704c9a2d6STeresa Johnson       // Must have been promoted (possibly conservatively). Find original
68804c9a2d6STeresa Johnson       // name so that we can access the correct summary and see if it can
68904c9a2d6STeresa Johnson       // be internalized again.
69004c9a2d6STeresa Johnson       // FIXME: Eventually we should control promotion instead of promoting
69104c9a2d6STeresa Johnson       // and internalizing again.
69204c9a2d6STeresa Johnson       StringRef OrigName =
69304c9a2d6STeresa Johnson           ModuleSummaryIndex::getOriginalNameBeforePromote(GV.getName());
69404c9a2d6STeresa Johnson       std::string OrigId = GlobalValue::getGlobalIdentifier(
69504c9a2d6STeresa Johnson           OrigName, GlobalValue::InternalLinkage,
69604c9a2d6STeresa Johnson           TheModule.getSourceFileName());
697c3d677f9SPeter Collingbourne       GS = DefinedGlobals.find(GlobalValue::getGUID(OrigId));
6987ab1f692STeresa Johnson       if (GS == DefinedGlobals.end()) {
6997ab1f692STeresa Johnson         // Also check the original non-promoted non-globalized name. In some
7007ab1f692STeresa Johnson         // cases a preempted weak value is linked in as a local copy because
7017ab1f692STeresa Johnson         // it is referenced by an alias (IRLinker::linkGlobalValueProto).
7027ab1f692STeresa Johnson         // In that case, since it was originally not a local value, it was
7037ab1f692STeresa Johnson         // recorded in the index using the original name.
7047ab1f692STeresa Johnson         // FIXME: This may not be needed once PR27866 is fixed.
705c3d677f9SPeter Collingbourne         GS = DefinedGlobals.find(GlobalValue::getGUID(OrigName));
70604c9a2d6STeresa Johnson         assert(GS != DefinedGlobals.end());
7077ab1f692STeresa Johnson       }
708c3d677f9SPeter Collingbourne     }
709c3d677f9SPeter Collingbourne     return !GlobalValue::isLocalLinkage(GS->second->linkage());
71004c9a2d6STeresa Johnson   };
71104c9a2d6STeresa Johnson 
71204c9a2d6STeresa Johnson   // FIXME: See if we can just internalize directly here via linkage changes
71304c9a2d6STeresa Johnson   // based on the index, rather than invoking internalizeModule.
714e9ea08a0SEugene Zelenko   internalizeModule(TheModule, MustPreserveGV);
71504c9a2d6STeresa Johnson }
71604c9a2d6STeresa Johnson 
71781bbf742STeresa Johnson /// Make alias a clone of its aliasee.
71881bbf742STeresa Johnson static Function *replaceAliasWithAliasee(Module *SrcModule, GlobalAlias *GA) {
71981bbf742STeresa Johnson   Function *Fn = cast<Function>(GA->getBaseObject());
72081bbf742STeresa Johnson 
72181bbf742STeresa Johnson   ValueToValueMapTy VMap;
72281bbf742STeresa Johnson   Function *NewFn = CloneFunction(Fn, VMap);
72381bbf742STeresa Johnson   // Clone should use the original alias's linkage and name, and we ensure
72481bbf742STeresa Johnson   // all uses of alias instead use the new clone (casted if necessary).
72581bbf742STeresa Johnson   NewFn->setLinkage(GA->getLinkage());
72681bbf742STeresa Johnson   GA->replaceAllUsesWith(ConstantExpr::getBitCast(NewFn, GA->getType()));
72781bbf742STeresa Johnson   NewFn->takeName(GA);
72881bbf742STeresa Johnson   return NewFn;
72981bbf742STeresa Johnson }
73081bbf742STeresa Johnson 
731c8c55170SMehdi Amini // Automatically import functions in Module \p DestModule based on the summaries
732c8c55170SMehdi Amini // index.
7337f00d0a1SPeter Collingbourne Expected<bool> FunctionImporter::importFunctions(
73466043797SAdrian Prantl     Module &DestModule, const FunctionImporter::ImportMapTy &ImportList) {
7355411d051SMehdi Amini   DEBUG(dbgs() << "Starting import for Module "
736311fef6eSMehdi Amini                << DestModule.getModuleIdentifier() << "\n");
737c8c55170SMehdi Amini   unsigned ImportedCount = 0;
738c8c55170SMehdi Amini 
7396d8f817fSPeter Collingbourne   IRMover Mover(DestModule);
7407e88d0daSMehdi Amini   // Do the actual import of functions now, one Module at a time
74101e32130SMehdi Amini   std::set<StringRef> ModuleNameOrderedList;
74201e32130SMehdi Amini   for (auto &FunctionsToImportPerModule : ImportList) {
74301e32130SMehdi Amini     ModuleNameOrderedList.insert(FunctionsToImportPerModule.first());
74401e32130SMehdi Amini   }
74501e32130SMehdi Amini   for (auto &Name : ModuleNameOrderedList) {
7467e88d0daSMehdi Amini     // Get the module for the import
74701e32130SMehdi Amini     const auto &FunctionsToImportPerModule = ImportList.find(Name);
74801e32130SMehdi Amini     assert(FunctionsToImportPerModule != ImportList.end());
749d9445c49SPeter Collingbourne     Expected<std::unique_ptr<Module>> SrcModuleOrErr = ModuleLoader(Name);
750d9445c49SPeter Collingbourne     if (!SrcModuleOrErr)
751d9445c49SPeter Collingbourne       return SrcModuleOrErr.takeError();
752d9445c49SPeter Collingbourne     std::unique_ptr<Module> SrcModule = std::move(*SrcModuleOrErr);
7537e88d0daSMehdi Amini     assert(&DestModule.getContext() == &SrcModule->getContext() &&
7547e88d0daSMehdi Amini            "Context mismatch");
7557e88d0daSMehdi Amini 
7566cba37ceSTeresa Johnson     // If modules were created with lazy metadata loading, materialize it
7576cba37ceSTeresa Johnson     // now, before linking it (otherwise this will be a noop).
7587f00d0a1SPeter Collingbourne     if (Error Err = SrcModule->materializeMetadata())
7597f00d0a1SPeter Collingbourne       return std::move(Err);
760e5a61917STeresa Johnson 
76101e32130SMehdi Amini     auto &ImportGUIDs = FunctionsToImportPerModule->second;
76201e32130SMehdi Amini     // Find the globals to import
7636d8f817fSPeter Collingbourne     SetVector<GlobalValue *> GlobalsToImport;
7641f685e01SPiotr Padlewski     for (Function &F : *SrcModule) {
7651f685e01SPiotr Padlewski       if (!F.hasName())
7660beb858eSTeresa Johnson         continue;
7671f685e01SPiotr Padlewski       auto GUID = F.getGUID();
7680beb858eSTeresa Johnson       auto Import = ImportGUIDs.count(GUID);
769aeb1e59bSMehdi Amini       DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing function " << GUID
7701f685e01SPiotr Padlewski                    << " " << F.getName() << " from "
771aeb1e59bSMehdi Amini                    << SrcModule->getSourceFileName() << "\n");
7720beb858eSTeresa Johnson       if (Import) {
7737f00d0a1SPeter Collingbourne         if (Error Err = F.materialize())
7747f00d0a1SPeter Collingbourne           return std::move(Err);
7753b776128SPiotr Padlewski         if (EnableImportMetadata) {
7766deaa6afSPiotr Padlewski           // Add 'thinlto_src_module' metadata for statistics and debugging.
7773b776128SPiotr Padlewski           F.setMetadata(
7783b776128SPiotr Padlewski               "thinlto_src_module",
779e9ea08a0SEugene Zelenko               MDNode::get(DestModule.getContext(),
780e9ea08a0SEugene Zelenko                           {MDString::get(DestModule.getContext(),
7816deaa6afSPiotr Padlewski                                          SrcModule->getSourceFileName())}));
7823b776128SPiotr Padlewski         }
7831f685e01SPiotr Padlewski         GlobalsToImport.insert(&F);
78401e32130SMehdi Amini       }
78501e32130SMehdi Amini     }
7861f685e01SPiotr Padlewski     for (GlobalVariable &GV : SrcModule->globals()) {
7872d28f7aaSMehdi Amini       if (!GV.hasName())
7882d28f7aaSMehdi Amini         continue;
7892d28f7aaSMehdi Amini       auto GUID = GV.getGUID();
7902d28f7aaSMehdi Amini       auto Import = ImportGUIDs.count(GUID);
791aeb1e59bSMehdi Amini       DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing global " << GUID
792aeb1e59bSMehdi Amini                    << " " << GV.getName() << " from "
793aeb1e59bSMehdi Amini                    << SrcModule->getSourceFileName() << "\n");
7942d28f7aaSMehdi Amini       if (Import) {
7957f00d0a1SPeter Collingbourne         if (Error Err = GV.materialize())
7967f00d0a1SPeter Collingbourne           return std::move(Err);
7972d28f7aaSMehdi Amini         GlobalsToImport.insert(&GV);
7982d28f7aaSMehdi Amini       }
7992d28f7aaSMehdi Amini     }
8001f685e01SPiotr Padlewski     for (GlobalAlias &GA : SrcModule->aliases()) {
8011f685e01SPiotr Padlewski       if (!GA.hasName())
80201e32130SMehdi Amini         continue;
8031f685e01SPiotr Padlewski       auto GUID = GA.getGUID();
80481bbf742STeresa Johnson       auto Import = ImportGUIDs.count(GUID);
80581bbf742STeresa Johnson       DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing alias " << GUID
8061f685e01SPiotr Padlewski                    << " " << GA.getName() << " from "
807aeb1e59bSMehdi Amini                    << SrcModule->getSourceFileName() << "\n");
80881bbf742STeresa Johnson       if (Import) {
80981bbf742STeresa Johnson         if (Error Err = GA.materialize())
81081bbf742STeresa Johnson           return std::move(Err);
81181bbf742STeresa Johnson         // Import alias as a copy of its aliasee.
81281bbf742STeresa Johnson         GlobalObject *Base = GA.getBaseObject();
81381bbf742STeresa Johnson         if (Error Err = Base->materialize())
81481bbf742STeresa Johnson           return std::move(Err);
81581bbf742STeresa Johnson         auto *Fn = replaceAliasWithAliasee(SrcModule.get(), &GA);
81681bbf742STeresa Johnson         DEBUG(dbgs() << "Is importing aliasee fn " << Base->getGUID()
81781bbf742STeresa Johnson               << " " << Base->getName() << " from "
81881bbf742STeresa Johnson               << SrcModule->getSourceFileName() << "\n");
81981bbf742STeresa Johnson         if (EnableImportMetadata) {
82081bbf742STeresa Johnson           // Add 'thinlto_src_module' metadata for statistics and debugging.
82181bbf742STeresa Johnson           Fn->setMetadata(
82281bbf742STeresa Johnson               "thinlto_src_module",
82381bbf742STeresa Johnson               MDNode::get(DestModule.getContext(),
82481bbf742STeresa Johnson                           {MDString::get(DestModule.getContext(),
82581bbf742STeresa Johnson                                          SrcModule->getSourceFileName())}));
82601e32130SMehdi Amini         }
82781bbf742STeresa Johnson         GlobalsToImport.insert(Fn);
82881bbf742STeresa Johnson       }
82981bbf742STeresa Johnson     }
83001e32130SMehdi Amini 
83119ef4fadSMehdi Amini     // Upgrade debug info after we're done materializing all the globals and we
83219ef4fadSMehdi Amini     // have loaded all the required metadata!
83319ef4fadSMehdi Amini     UpgradeDebugInfo(*SrcModule);
83419ef4fadSMehdi Amini 
8357e88d0daSMehdi Amini     // Link in the specified functions.
83601e32130SMehdi Amini     if (renameModuleForThinLTO(*SrcModule, Index, &GlobalsToImport))
8378d05185aSMehdi Amini       return true;
8388d05185aSMehdi Amini 
839d29478f7STeresa Johnson     if (PrintImports) {
840d29478f7STeresa Johnson       for (const auto *GV : GlobalsToImport)
841d29478f7STeresa Johnson         dbgs() << DestModule.getSourceFileName() << ": Import " << GV->getName()
842d29478f7STeresa Johnson                << " from " << SrcModule->getSourceFileName() << "\n";
843d29478f7STeresa Johnson     }
844d29478f7STeresa Johnson 
8456d8f817fSPeter Collingbourne     if (Mover.move(std::move(SrcModule), GlobalsToImport.getArrayRef(),
8466d8f817fSPeter Collingbourne                    [](GlobalValue &, IRMover::ValueAdder) {},
847e6fd9ff9SPeter Collingbourne                    /*IsPerformingImport=*/true))
8487e88d0daSMehdi Amini       report_fatal_error("Function Import: link error");
8497e88d0daSMehdi Amini 
85001e32130SMehdi Amini     ImportedCount += GlobalsToImport.size();
8516c475a75STeresa Johnson     NumImportedModules++;
8527e88d0daSMehdi Amini   }
853e5a61917STeresa Johnson 
8546c475a75STeresa Johnson   NumImportedFunctions += ImportedCount;
855d29478f7STeresa Johnson 
8567e88d0daSMehdi Amini   DEBUG(dbgs() << "Imported " << ImportedCount << " functions for Module "
857c8c55170SMehdi Amini                << DestModule.getModuleIdentifier() << "\n");
858c8c55170SMehdi Amini   return ImportedCount;
85942418abaSMehdi Amini }
86042418abaSMehdi Amini 
861598bd2a2SPeter Collingbourne static bool doImportingForModule(Module &M) {
862598bd2a2SPeter Collingbourne   if (SummaryFile.empty())
863598bd2a2SPeter Collingbourne     report_fatal_error("error: -function-import requires -summary-file\n");
8646de481a3SPeter Collingbourne   Expected<std::unique_ptr<ModuleSummaryIndex>> IndexPtrOrErr =
8656de481a3SPeter Collingbourne       getModuleSummaryIndexForFile(SummaryFile);
8666de481a3SPeter Collingbourne   if (!IndexPtrOrErr) {
8676de481a3SPeter Collingbourne     logAllUnhandledErrors(IndexPtrOrErr.takeError(), errs(),
8686de481a3SPeter Collingbourne                           "Error loading file '" + SummaryFile + "': ");
86942418abaSMehdi Amini     return false;
87042418abaSMehdi Amini   }
871598bd2a2SPeter Collingbourne   std::unique_ptr<ModuleSummaryIndex> Index = std::move(*IndexPtrOrErr);
87242418abaSMehdi Amini 
873c86af334STeresa Johnson   // First step is collecting the import list.
874c86af334STeresa Johnson   FunctionImporter::ImportMapTy ImportList;
87581bbf742STeresa Johnson   // If requested, simply import all functions in the index. This is used
87681bbf742STeresa Johnson   // when testing distributed backend handling via the opt tool, when
87781bbf742STeresa Johnson   // we have distributed indexes containing exactly the summaries to import.
87881bbf742STeresa Johnson   if (ImportAllIndex)
87981bbf742STeresa Johnson     ComputeCrossModuleImportForModuleFromIndex(M.getModuleIdentifier(), *Index,
88081bbf742STeresa Johnson                                                ImportList);
88181bbf742STeresa Johnson   else
882c86af334STeresa Johnson     ComputeCrossModuleImportForModule(M.getModuleIdentifier(), *Index,
883c86af334STeresa Johnson                                       ImportList);
88401e32130SMehdi Amini 
8854fef68cbSTeresa Johnson   // Conservatively mark all internal values as promoted. This interface is
8864fef68cbSTeresa Johnson   // only used when doing importing via the function importing pass. The pass
8874fef68cbSTeresa Johnson   // is only enabled when testing importing via the 'opt' tool, which does
8884fef68cbSTeresa Johnson   // not do the ThinLink that would normally determine what values to promote.
8894fef68cbSTeresa Johnson   for (auto &I : *Index) {
8909667b91bSPeter Collingbourne     for (auto &S : I.second.SummaryList) {
8914fef68cbSTeresa Johnson       if (GlobalValue::isLocalLinkage(S->linkage()))
8924fef68cbSTeresa Johnson         S->setLinkage(GlobalValue::ExternalLinkage);
8934fef68cbSTeresa Johnson     }
8944fef68cbSTeresa Johnson   }
8954fef68cbSTeresa Johnson 
89601e32130SMehdi Amini   // Next we need to promote to global scope and rename any local values that
8971b00f2d9STeresa Johnson   // are potentially exported to other modules.
89801e32130SMehdi Amini   if (renameModuleForThinLTO(M, *Index, nullptr)) {
8991b00f2d9STeresa Johnson     errs() << "Error renaming module\n";
9001b00f2d9STeresa Johnson     return false;
9011b00f2d9STeresa Johnson   }
9021b00f2d9STeresa Johnson 
90342418abaSMehdi Amini   // Perform the import now.
904d16c8065SMehdi Amini   auto ModuleLoader = [&M](StringRef Identifier) {
905d16c8065SMehdi Amini     return loadFile(Identifier, M.getContext());
906d16c8065SMehdi Amini   };
9079d2bfc48SRafael Espindola   FunctionImporter Importer(*Index, ModuleLoader);
90837e24591SPeter Collingbourne   Expected<bool> Result = Importer.importFunctions(M, ImportList);
9097f00d0a1SPeter Collingbourne 
9107f00d0a1SPeter Collingbourne   // FIXME: Probably need to propagate Errors through the pass manager.
9117f00d0a1SPeter Collingbourne   if (!Result) {
9127f00d0a1SPeter Collingbourne     logAllUnhandledErrors(Result.takeError(), errs(),
9137f00d0a1SPeter Collingbourne                           "Error importing module: ");
9147f00d0a1SPeter Collingbourne     return false;
9157f00d0a1SPeter Collingbourne   }
9167f00d0a1SPeter Collingbourne 
9177f00d0a1SPeter Collingbourne   return *Result;
91821241571STeresa Johnson }
91921241571STeresa Johnson 
92021241571STeresa Johnson namespace {
921e9ea08a0SEugene Zelenko 
92221241571STeresa Johnson /// Pass that performs cross-module function import provided a summary file.
92321241571STeresa Johnson class FunctionImportLegacyPass : public ModulePass {
92421241571STeresa Johnson public:
92521241571STeresa Johnson   /// Pass identification, replacement for typeid
92621241571STeresa Johnson   static char ID;
92721241571STeresa Johnson 
928e9ea08a0SEugene Zelenko   explicit FunctionImportLegacyPass() : ModulePass(ID) {}
929e9ea08a0SEugene Zelenko 
93021241571STeresa Johnson   /// Specify pass name for debug output
931117296c0SMehdi Amini   StringRef getPassName() const override { return "Function Importing"; }
93221241571STeresa Johnson 
93321241571STeresa Johnson   bool runOnModule(Module &M) override {
93421241571STeresa Johnson     if (skipModule(M))
93521241571STeresa Johnson       return false;
93621241571STeresa Johnson 
937598bd2a2SPeter Collingbourne     return doImportingForModule(M);
93842418abaSMehdi Amini   }
93942418abaSMehdi Amini };
940e9ea08a0SEugene Zelenko 
941e9ea08a0SEugene Zelenko } // end anonymous namespace
94242418abaSMehdi Amini 
94321241571STeresa Johnson PreservedAnalyses FunctionImportPass::run(Module &M,
944fd03ac6aSSean Silva                                           ModuleAnalysisManager &AM) {
945598bd2a2SPeter Collingbourne   if (!doImportingForModule(M))
94621241571STeresa Johnson     return PreservedAnalyses::all();
94721241571STeresa Johnson 
94821241571STeresa Johnson   return PreservedAnalyses::none();
94921241571STeresa Johnson }
95021241571STeresa Johnson 
95121241571STeresa Johnson char FunctionImportLegacyPass::ID = 0;
95221241571STeresa Johnson INITIALIZE_PASS(FunctionImportLegacyPass, "function-import",
95342418abaSMehdi Amini                 "Summary Based Function Import", false, false)
95442418abaSMehdi Amini 
95542418abaSMehdi Amini namespace llvm {
956e9ea08a0SEugene Zelenko 
957598bd2a2SPeter Collingbourne Pass *createFunctionImportPass() {
958598bd2a2SPeter Collingbourne   return new FunctionImportLegacyPass();
9595fcbdb71STeresa Johnson }
960e9ea08a0SEugene Zelenko 
961e9ea08a0SEugene Zelenko } // end namespace llvm
962