1292915Sdim//===- FunctionImport.cpp - ThinLTO Summary-based Function Import ---------===// 2292915Sdim// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6292915Sdim// 7292915Sdim//===----------------------------------------------------------------------===// 8292915Sdim// 9292915Sdim// This file implements Function import based on summaries. 10292915Sdim// 11292915Sdim//===----------------------------------------------------------------------===// 12292915Sdim 13292915Sdim#include "llvm/Transforms/IPO/FunctionImport.h" 14327952Sdim#include "llvm/ADT/ArrayRef.h" 15327952Sdim#include "llvm/ADT/STLExtras.h" 16327952Sdim#include "llvm/ADT/SetVector.h" 17309124Sdim#include "llvm/ADT/SmallVector.h" 18309124Sdim#include "llvm/ADT/Statistic.h" 19327952Sdim#include "llvm/ADT/StringMap.h" 20341825Sdim#include "llvm/ADT/StringRef.h" 21292915Sdim#include "llvm/ADT/StringSet.h" 22321369Sdim#include "llvm/Bitcode/BitcodeReader.h" 23292915Sdim#include "llvm/IR/AutoUpgrade.h" 24327952Sdim#include "llvm/IR/Constants.h" 25327952Sdim#include "llvm/IR/Function.h" 26327952Sdim#include "llvm/IR/GlobalAlias.h" 27327952Sdim#include "llvm/IR/GlobalObject.h" 28327952Sdim#include "llvm/IR/GlobalValue.h" 29327952Sdim#include "llvm/IR/GlobalVariable.h" 30327952Sdim#include "llvm/IR/Metadata.h" 31292915Sdim#include "llvm/IR/Module.h" 32327952Sdim#include "llvm/IR/ModuleSummaryIndex.h" 33292915Sdim#include "llvm/IRReader/IRReader.h" 34360784Sdim#include "llvm/InitializePasses.h" 35327952Sdim#include "llvm/Linker/IRMover.h" 36327952Sdim#include "llvm/Object/ModuleSymbolTable.h" 37327952Sdim#include "llvm/Object/SymbolicFile.h" 38327952Sdim#include "llvm/Pass.h" 39327952Sdim#include "llvm/Support/Casting.h" 40292915Sdim#include "llvm/Support/CommandLine.h" 41292915Sdim#include "llvm/Support/Debug.h" 42327952Sdim#include "llvm/Support/Error.h" 43327952Sdim#include "llvm/Support/ErrorHandling.h" 44327952Sdim#include "llvm/Support/FileSystem.h" 45292915Sdim#include "llvm/Support/SourceMgr.h" 46327952Sdim#include "llvm/Support/raw_ostream.h" 47309124Sdim#include "llvm/Transforms/IPO/Internalize.h" 48327952Sdim#include "llvm/Transforms/Utils/Cloning.h" 49309124Sdim#include "llvm/Transforms/Utils/FunctionImportUtils.h" 50327952Sdim#include "llvm/Transforms/Utils/ValueMapper.h" 51327952Sdim#include <cassert> 52327952Sdim#include <memory> 53327952Sdim#include <set> 54327952Sdim#include <string> 55327952Sdim#include <system_error> 56327952Sdim#include <tuple> 57327952Sdim#include <utility> 58292915Sdim 59327952Sdimusing namespace llvm; 60327952Sdim 61309124Sdim#define DEBUG_TYPE "function-import" 62292915Sdim 63344779SdimSTATISTIC(NumImportedFunctionsThinLink, 64344779Sdim "Number of functions thin link decided to import"); 65344779SdimSTATISTIC(NumImportedHotFunctionsThinLink, 66344779Sdim "Number of hot functions thin link decided to import"); 67344779SdimSTATISTIC(NumImportedCriticalFunctionsThinLink, 68344779Sdim "Number of critical functions thin link decided to import"); 69344779SdimSTATISTIC(NumImportedGlobalVarsThinLink, 70344779Sdim "Number of global variables thin link decided to import"); 71344779SdimSTATISTIC(NumImportedFunctions, "Number of functions imported in backend"); 72344779SdimSTATISTIC(NumImportedGlobalVars, 73344779Sdim "Number of global variables imported in backend"); 74314564SdimSTATISTIC(NumImportedModules, "Number of modules imported from"); 75314564SdimSTATISTIC(NumDeadSymbols, "Number of dead stripped symbols in index"); 76314564SdimSTATISTIC(NumLiveSymbols, "Number of live symbols in index"); 77292915Sdim 78292915Sdim/// Limit on instruction count of imported functions. 79292915Sdimstatic cl::opt<unsigned> ImportInstrLimit( 80292915Sdim "import-instr-limit", cl::init(100), cl::Hidden, cl::value_desc("N"), 81292915Sdim cl::desc("Only import functions with less than N instructions")); 82292915Sdim 83341825Sdimstatic cl::opt<int> ImportCutoff( 84341825Sdim "import-cutoff", cl::init(-1), cl::Hidden, cl::value_desc("N"), 85341825Sdim cl::desc("Only import first N functions if N>=0 (default -1)")); 86341825Sdim 87309124Sdimstatic cl::opt<float> 88309124Sdim ImportInstrFactor("import-instr-evolution-factor", cl::init(0.7), 89309124Sdim cl::Hidden, cl::value_desc("x"), 90309124Sdim cl::desc("As we import functions, multiply the " 91309124Sdim "`import-instr-limit` threshold by this factor " 92309124Sdim "before processing newly imported functions")); 93309124Sdim 94314564Sdimstatic cl::opt<float> ImportHotInstrFactor( 95314564Sdim "import-hot-evolution-factor", cl::init(1.0), cl::Hidden, 96314564Sdim cl::value_desc("x"), 97314564Sdim cl::desc("As we import functions called from hot callsite, multiply the " 98314564Sdim "`import-instr-limit` threshold by this factor " 99314564Sdim "before processing newly imported functions")); 100314564Sdim 101314564Sdimstatic cl::opt<float> ImportHotMultiplier( 102327952Sdim "import-hot-multiplier", cl::init(10.0), cl::Hidden, cl::value_desc("x"), 103314564Sdim cl::desc("Multiply the `import-instr-limit` threshold for hot callsites")); 104314564Sdim 105321369Sdimstatic cl::opt<float> ImportCriticalMultiplier( 106321369Sdim "import-critical-multiplier", cl::init(100.0), cl::Hidden, 107321369Sdim cl::value_desc("x"), 108321369Sdim cl::desc( 109321369Sdim "Multiply the `import-instr-limit` threshold for critical callsites")); 110321369Sdim 111314564Sdim// FIXME: This multiplier was not really tuned up. 112314564Sdimstatic cl::opt<float> ImportColdMultiplier( 113314564Sdim "import-cold-multiplier", cl::init(0), cl::Hidden, cl::value_desc("N"), 114314564Sdim cl::desc("Multiply the `import-instr-limit` threshold for cold callsites")); 115314564Sdim 116309124Sdimstatic cl::opt<bool> PrintImports("print-imports", cl::init(false), cl::Hidden, 117309124Sdim cl::desc("Print imported functions")); 118309124Sdim 119344779Sdimstatic cl::opt<bool> PrintImportFailures( 120344779Sdim "print-import-failures", cl::init(false), cl::Hidden, 121344779Sdim cl::desc("Print information for functions rejected for importing")); 122344779Sdim 123314564Sdimstatic cl::opt<bool> ComputeDead("compute-dead", cl::init(true), cl::Hidden, 124314564Sdim cl::desc("Compute dead symbols")); 125314564Sdim 126309124Sdimstatic cl::opt<bool> EnableImportMetadata( 127309124Sdim "enable-import-metadata", cl::init( 128309124Sdim#if !defined(NDEBUG) 129309124Sdim true /*Enabled with asserts.*/ 130309124Sdim#else 131309124Sdim false 132309124Sdim#endif 133309124Sdim ), 134309124Sdim cl::Hidden, cl::desc("Enable import metadata like 'thinlto_src_module'")); 135309124Sdim 136327952Sdim/// Summary file to use for function importing when using -function-import from 137327952Sdim/// the command line. 138327952Sdimstatic cl::opt<std::string> 139327952Sdim SummaryFile("summary-file", 140327952Sdim cl::desc("The summary file to use for function importing.")); 141327952Sdim 142327952Sdim/// Used when testing importing from distributed indexes via opt 143327952Sdim// -function-import. 144327952Sdimstatic cl::opt<bool> 145327952Sdim ImportAllIndex("import-all-index", 146327952Sdim cl::desc("Import all external functions in index.")); 147327952Sdim 148292915Sdim// Load lazily a module from \p FileName in \p Context. 149292915Sdimstatic std::unique_ptr<Module> loadFile(const std::string &FileName, 150292915Sdim LLVMContext &Context) { 151292915Sdim SMDiagnostic Err; 152341825Sdim LLVM_DEBUG(dbgs() << "Loading '" << FileName << "'\n"); 153309124Sdim // Metadata isn't loaded until functions are imported, to minimize 154309124Sdim // the memory overhead. 155294024Sdim std::unique_ptr<Module> Result = 156294024Sdim getLazyIRFileModule(FileName, Err, Context, 157294024Sdim /* ShouldLazyLoadMetadata = */ true); 158292915Sdim if (!Result) { 159292915Sdim Err.print("function-import", errs()); 160309124Sdim report_fatal_error("Abort"); 161292915Sdim } 162292915Sdim 163292915Sdim return Result; 164292915Sdim} 165292915Sdim 166309124Sdim/// Given a list of possible callee implementation for a call site, select one 167309124Sdim/// that fits the \p Threshold. 168309124Sdim/// 169309124Sdim/// FIXME: select "best" instead of first that fits. But what is "best"? 170309124Sdim/// - The smallest: more likely to be inlined. 171309124Sdim/// - The one with the least outgoing edges (already well optimized). 172309124Sdim/// - One from a module already being imported from in order to reduce the 173309124Sdim/// number of source modules parsed/linked. 174309124Sdim/// - One that has PGO data attached. 175309124Sdim/// - [insert you fancy metric here] 176309124Sdimstatic const GlobalValueSummary * 177309124SdimselectCallee(const ModuleSummaryIndex &Index, 178321369Sdim ArrayRef<std::unique_ptr<GlobalValueSummary>> CalleeSummaryList, 179344779Sdim unsigned Threshold, StringRef CallerModulePath, 180344779Sdim FunctionImporter::ImportFailureReason &Reason, 181344779Sdim GlobalValue::GUID GUID) { 182344779Sdim Reason = FunctionImporter::ImportFailureReason::None; 183309124Sdim auto It = llvm::find_if( 184309124Sdim CalleeSummaryList, 185309124Sdim [&](const std::unique_ptr<GlobalValueSummary> &SummaryPtr) { 186309124Sdim auto *GVSummary = SummaryPtr.get(); 187344779Sdim if (!Index.isGlobalValueLive(GVSummary)) { 188344779Sdim Reason = FunctionImporter::ImportFailureReason::NotLive; 189341825Sdim return false; 190344779Sdim } 191341825Sdim 192327952Sdim // For SamplePGO, in computeImportForFunction the OriginalId 193327952Sdim // may have been used to locate the callee summary list (See 194327952Sdim // comment there). 195327952Sdim // The mapping from OriginalId to GUID may return a GUID 196327952Sdim // that corresponds to a static variable. Filter it out here. 197327952Sdim // This can happen when 198327952Sdim // 1) There is a call to a library function which is not defined 199327952Sdim // in the index. 200327952Sdim // 2) There is a static variable with the OriginalGUID identical 201327952Sdim // to the GUID of the library function in 1); 202327952Sdim // When this happens, the logic for SamplePGO kicks in and 203327952Sdim // the static variable in 2) will be found, which needs to be 204327952Sdim // filtered out. 205344779Sdim if (GVSummary->getSummaryKind() == GlobalValueSummary::GlobalVarKind) { 206344779Sdim Reason = FunctionImporter::ImportFailureReason::GlobalVar; 207327952Sdim return false; 208344779Sdim } 209344779Sdim if (GlobalValue::isInterposableLinkage(GVSummary->linkage())) { 210344779Sdim Reason = FunctionImporter::ImportFailureReason::InterposableLinkage; 211309124Sdim // There is no point in importing these, we can't inline them 212309124Sdim return false; 213344779Sdim } 214292915Sdim 215327952Sdim auto *Summary = cast<FunctionSummary>(GVSummary->getBaseObject()); 216309124Sdim 217321369Sdim // If this is a local function, make sure we import the copy 218321369Sdim // in the caller's module. The only time a local function can 219321369Sdim // share an entry in the index is if there is a local with the same name 220321369Sdim // in another module that had the same source file name (in a different 221321369Sdim // directory), where each was compiled in their own directory so there 222321369Sdim // was not distinguishing path. 223321369Sdim // However, do the import from another module if there is only one 224321369Sdim // entry in the list - in that case this must be a reference due 225321369Sdim // to indirect call profile data, since a function pointer can point to 226321369Sdim // a local in another module. 227321369Sdim if (GlobalValue::isLocalLinkage(Summary->linkage()) && 228321369Sdim CalleeSummaryList.size() > 1 && 229344779Sdim Summary->modulePath() != CallerModulePath) { 230344779Sdim Reason = 231344779Sdim FunctionImporter::ImportFailureReason::LocalLinkageNotInModule; 232321369Sdim return false; 233344779Sdim } 234321369Sdim 235360784Sdim if ((Summary->instCount() > Threshold) && 236360784Sdim !Summary->fflags().AlwaysInline) { 237344779Sdim Reason = FunctionImporter::ImportFailureReason::TooLarge; 238309124Sdim return false; 239344779Sdim } 240309124Sdim 241344779Sdim // Skip if it isn't legal to import (e.g. may reference unpromotable 242344779Sdim // locals). 243344779Sdim if (Summary->notEligibleToImport()) { 244344779Sdim Reason = FunctionImporter::ImportFailureReason::NotEligible; 245309124Sdim return false; 246344779Sdim } 247309124Sdim 248344779Sdim // Don't bother importing if we can't inline it anyway. 249344779Sdim if (Summary->fflags().NoInline) { 250344779Sdim Reason = FunctionImporter::ImportFailureReason::NoInline; 251344779Sdim return false; 252344779Sdim } 253344779Sdim 254309124Sdim return true; 255309124Sdim }); 256309124Sdim if (It == CalleeSummaryList.end()) 257309124Sdim return nullptr; 258309124Sdim 259309124Sdim return cast<GlobalValueSummary>(It->get()); 260309124Sdim} 261309124Sdim 262327952Sdimnamespace { 263327952Sdim 264314564Sdimusing EdgeInfo = std::tuple<const FunctionSummary *, unsigned /* Threshold */, 265314564Sdim GlobalValue::GUID>; 266309124Sdim 267327952Sdim} // anonymous namespace 268327952Sdim 269327952Sdimstatic ValueInfo 270327952SdimupdateValueInfoForIndirectCalls(const ModuleSummaryIndex &Index, ValueInfo VI) { 271327952Sdim if (!VI.getSummaryList().empty()) 272327952Sdim return VI; 273327952Sdim // For SamplePGO, the indirect call targets for local functions will 274327952Sdim // have its original name annotated in profile. We try to find the 275327952Sdim // corresponding PGOFuncName as the GUID. 276327952Sdim // FIXME: Consider updating the edges in the graph after building 277327952Sdim // it, rather than needing to perform this mapping on each walk. 278327952Sdim auto GUID = Index.getGUIDFromOriginalID(VI.getGUID()); 279327952Sdim if (GUID == 0) 280341825Sdim return ValueInfo(); 281327952Sdim return Index.getValueInfo(GUID); 282327952Sdim} 283327952Sdim 284341825Sdimstatic void computeImportForReferencedGlobals( 285360784Sdim const FunctionSummary &Summary, const ModuleSummaryIndex &Index, 286360784Sdim const GVSummaryMapTy &DefinedGVSummaries, 287341825Sdim FunctionImporter::ImportMapTy &ImportList, 288341825Sdim StringMap<FunctionImporter::ExportSetTy> *ExportLists) { 289341825Sdim for (auto &VI : Summary.refs()) { 290341825Sdim if (DefinedGVSummaries.count(VI.getGUID())) { 291341825Sdim LLVM_DEBUG( 292341825Sdim dbgs() << "Ref ignored! Target already in destination module.\n"); 293341825Sdim continue; 294341825Sdim } 295341825Sdim 296341825Sdim LLVM_DEBUG(dbgs() << " ref -> " << VI << "\n"); 297341825Sdim 298344779Sdim // If this is a local variable, make sure we import the copy 299344779Sdim // in the caller's module. The only time a local variable can 300344779Sdim // share an entry in the index is if there is a local with the same name 301344779Sdim // in another module that had the same source file name (in a different 302344779Sdim // directory), where each was compiled in their own directory so there 303344779Sdim // was not distinguishing path. 304344779Sdim auto LocalNotInModule = [&](const GlobalValueSummary *RefSummary) -> bool { 305344779Sdim return GlobalValue::isLocalLinkage(RefSummary->linkage()) && 306344779Sdim RefSummary->modulePath() != Summary.modulePath(); 307344779Sdim }; 308344779Sdim 309360784Sdim auto MarkExported = [&](const ValueInfo &VI, const GlobalValueSummary *S) { 310360784Sdim if (ExportLists) 311360784Sdim (*ExportLists)[S->modulePath()].insert(VI); 312360784Sdim }; 313360784Sdim 314341825Sdim for (auto &RefSummary : VI.getSummaryList()) 315344779Sdim if (isa<GlobalVarSummary>(RefSummary.get()) && 316360784Sdim Index.canImportGlobalVar(RefSummary.get(), /* AnalyzeRefs */ true) && 317344779Sdim !LocalNotInModule(RefSummary.get())) { 318344779Sdim auto ILI = ImportList[RefSummary->modulePath()].insert(VI.getGUID()); 319344779Sdim // Only update stat if we haven't already imported this variable. 320344779Sdim if (ILI.second) 321344779Sdim NumImportedGlobalVarsThinLink++; 322360784Sdim MarkExported(VI, RefSummary.get()); 323360784Sdim // Promote referenced functions and variables. We don't promote 324360784Sdim // objects referenced by writeonly variable initializer, because 325360784Sdim // we convert such variables initializers to "zeroinitializer". 326360784Sdim // See processGlobalForThinLTO. 327360784Sdim if (!Index.isWriteOnly(cast<GlobalVarSummary>(RefSummary.get()))) 328360784Sdim for (const auto &VI : RefSummary->refs()) 329360784Sdim for (const auto &RefFn : VI.getSummaryList()) 330360784Sdim MarkExported(VI, RefFn.get()); 331341825Sdim break; 332341825Sdim } 333341825Sdim } 334341825Sdim} 335341825Sdim 336344779Sdimstatic const char * 337344779SdimgetFailureName(FunctionImporter::ImportFailureReason Reason) { 338344779Sdim switch (Reason) { 339344779Sdim case FunctionImporter::ImportFailureReason::None: 340344779Sdim return "None"; 341344779Sdim case FunctionImporter::ImportFailureReason::GlobalVar: 342344779Sdim return "GlobalVar"; 343344779Sdim case FunctionImporter::ImportFailureReason::NotLive: 344344779Sdim return "NotLive"; 345344779Sdim case FunctionImporter::ImportFailureReason::TooLarge: 346344779Sdim return "TooLarge"; 347344779Sdim case FunctionImporter::ImportFailureReason::InterposableLinkage: 348344779Sdim return "InterposableLinkage"; 349344779Sdim case FunctionImporter::ImportFailureReason::LocalLinkageNotInModule: 350344779Sdim return "LocalLinkageNotInModule"; 351344779Sdim case FunctionImporter::ImportFailureReason::NotEligible: 352344779Sdim return "NotEligible"; 353344779Sdim case FunctionImporter::ImportFailureReason::NoInline: 354344779Sdim return "NoInline"; 355344779Sdim } 356344779Sdim llvm_unreachable("invalid reason"); 357344779Sdim} 358344779Sdim 359309124Sdim/// Compute the list of functions to import for a given caller. Mark these 360309124Sdim/// imported functions and the symbols they reference in their source module as 361309124Sdim/// exported from their source module. 362309124Sdimstatic void computeImportForFunction( 363309124Sdim const FunctionSummary &Summary, const ModuleSummaryIndex &Index, 364314564Sdim const unsigned Threshold, const GVSummaryMapTy &DefinedGVSummaries, 365309124Sdim SmallVectorImpl<EdgeInfo> &Worklist, 366314564Sdim FunctionImporter::ImportMapTy &ImportList, 367341825Sdim StringMap<FunctionImporter::ExportSetTy> *ExportLists, 368341825Sdim FunctionImporter::ImportThresholdsTy &ImportThresholds) { 369360784Sdim computeImportForReferencedGlobals(Summary, Index, DefinedGVSummaries, 370360784Sdim ImportList, ExportLists); 371341825Sdim static int ImportCount = 0; 372309124Sdim for (auto &Edge : Summary.calls()) { 373321369Sdim ValueInfo VI = Edge.first; 374341825Sdim LLVM_DEBUG(dbgs() << " edge -> " << VI << " Threshold:" << Threshold 375341825Sdim << "\n"); 376309124Sdim 377341825Sdim if (ImportCutoff >= 0 && ImportCount >= ImportCutoff) { 378341825Sdim LLVM_DEBUG(dbgs() << "ignored! import-cutoff value of " << ImportCutoff 379341825Sdim << " reached.\n"); 380341825Sdim continue; 381341825Sdim } 382341825Sdim 383327952Sdim VI = updateValueInfoForIndirectCalls(Index, VI); 384327952Sdim if (!VI) 385327952Sdim continue; 386321369Sdim 387321369Sdim if (DefinedGVSummaries.count(VI.getGUID())) { 388341825Sdim LLVM_DEBUG(dbgs() << "ignored! Target already in destination module.\n"); 389292915Sdim continue; 390292915Sdim } 391292915Sdim 392314564Sdim auto GetBonusMultiplier = [](CalleeInfo::HotnessType Hotness) -> float { 393314564Sdim if (Hotness == CalleeInfo::HotnessType::Hot) 394314564Sdim return ImportHotMultiplier; 395314564Sdim if (Hotness == CalleeInfo::HotnessType::Cold) 396314564Sdim return ImportColdMultiplier; 397321369Sdim if (Hotness == CalleeInfo::HotnessType::Critical) 398321369Sdim return ImportCriticalMultiplier; 399314564Sdim return 1.0; 400314564Sdim }; 401314564Sdim 402314564Sdim const auto NewThreshold = 403341825Sdim Threshold * GetBonusMultiplier(Edge.second.getHotness()); 404314564Sdim 405344779Sdim auto IT = ImportThresholds.insert(std::make_pair( 406344779Sdim VI.getGUID(), std::make_tuple(NewThreshold, nullptr, nullptr))); 407341825Sdim bool PreviouslyVisited = !IT.second; 408344779Sdim auto &ProcessedThreshold = std::get<0>(IT.first->second); 409344779Sdim auto &CalleeSummary = std::get<1>(IT.first->second); 410344779Sdim auto &FailureInfo = std::get<2>(IT.first->second); 411292915Sdim 412344779Sdim bool IsHotCallsite = 413344779Sdim Edge.second.getHotness() == CalleeInfo::HotnessType::Hot; 414344779Sdim bool IsCriticalCallsite = 415344779Sdim Edge.second.getHotness() == CalleeInfo::HotnessType::Critical; 416344779Sdim 417341825Sdim const FunctionSummary *ResolvedCalleeSummary = nullptr; 418341825Sdim if (CalleeSummary) { 419341825Sdim assert(PreviouslyVisited); 420341825Sdim // Since the traversal of the call graph is DFS, we can revisit a function 421341825Sdim // a second time with a higher threshold. In this case, it is added back 422341825Sdim // to the worklist with the new threshold (so that its own callee chains 423341825Sdim // can be considered with the higher threshold). 424341825Sdim if (NewThreshold <= ProcessedThreshold) { 425341825Sdim LLVM_DEBUG( 426341825Sdim dbgs() << "ignored! Target was already imported with Threshold " 427341825Sdim << ProcessedThreshold << "\n"); 428341825Sdim continue; 429341825Sdim } 430341825Sdim // Update with new larger threshold. 431341825Sdim ProcessedThreshold = NewThreshold; 432341825Sdim ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary); 433341825Sdim } else { 434341825Sdim // If we already rejected importing a callee at the same or higher 435341825Sdim // threshold, don't waste time calling selectCallee. 436341825Sdim if (PreviouslyVisited && NewThreshold <= ProcessedThreshold) { 437341825Sdim LLVM_DEBUG( 438341825Sdim dbgs() << "ignored! Target was already rejected with Threshold " 439341825Sdim << ProcessedThreshold << "\n"); 440344779Sdim if (PrintImportFailures) { 441344779Sdim assert(FailureInfo && 442344779Sdim "Expected FailureInfo for previously rejected candidate"); 443344779Sdim FailureInfo->Attempts++; 444344779Sdim } 445341825Sdim continue; 446341825Sdim } 447327952Sdim 448344779Sdim FunctionImporter::ImportFailureReason Reason; 449341825Sdim CalleeSummary = selectCallee(Index, VI.getSummaryList(), NewThreshold, 450344779Sdim Summary.modulePath(), Reason, VI.getGUID()); 451341825Sdim if (!CalleeSummary) { 452341825Sdim // Update with new larger threshold if this was a retry (otherwise 453344779Sdim // we would have already inserted with NewThreshold above). Also 454344779Sdim // update failure info if requested. 455344779Sdim if (PreviouslyVisited) { 456341825Sdim ProcessedThreshold = NewThreshold; 457344779Sdim if (PrintImportFailures) { 458344779Sdim assert(FailureInfo && 459344779Sdim "Expected FailureInfo for previously rejected candidate"); 460344779Sdim FailureInfo->Reason = Reason; 461344779Sdim FailureInfo->Attempts++; 462344779Sdim FailureInfo->MaxHotness = 463344779Sdim std::max(FailureInfo->MaxHotness, Edge.second.getHotness()); 464344779Sdim } 465344779Sdim } else if (PrintImportFailures) { 466344779Sdim assert(!FailureInfo && 467344779Sdim "Expected no FailureInfo for newly rejected candidate"); 468360784Sdim FailureInfo = std::make_unique<FunctionImporter::ImportFailureInfo>( 469344779Sdim VI, Edge.second.getHotness(), Reason, 1); 470344779Sdim } 471341825Sdim LLVM_DEBUG( 472341825Sdim dbgs() << "ignored! No qualifying callee with summary found.\n"); 473341825Sdim continue; 474341825Sdim } 475309124Sdim 476341825Sdim // "Resolve" the summary 477341825Sdim CalleeSummary = CalleeSummary->getBaseObject(); 478341825Sdim ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary); 479341825Sdim 480360784Sdim assert((ResolvedCalleeSummary->fflags().AlwaysInline || 481360784Sdim (ResolvedCalleeSummary->instCount() <= NewThreshold)) && 482341825Sdim "selectCallee() didn't honor the threshold"); 483341825Sdim 484341825Sdim auto ExportModulePath = ResolvedCalleeSummary->modulePath(); 485341825Sdim auto ILI = ImportList[ExportModulePath].insert(VI.getGUID()); 486341825Sdim // We previously decided to import this GUID definition if it was already 487341825Sdim // inserted in the set of imports from the exporting module. 488341825Sdim bool PreviouslyImported = !ILI.second; 489344779Sdim if (!PreviouslyImported) { 490344779Sdim NumImportedFunctionsThinLink++; 491344779Sdim if (IsHotCallsite) 492344779Sdim NumImportedHotFunctionsThinLink++; 493344779Sdim if (IsCriticalCallsite) 494344779Sdim NumImportedCriticalFunctionsThinLink++; 495344779Sdim } 496341825Sdim 497341825Sdim // Make exports in the source module. 498341825Sdim if (ExportLists) { 499341825Sdim auto &ExportList = (*ExportLists)[ExportModulePath]; 500360784Sdim ExportList.insert(VI); 501341825Sdim if (!PreviouslyImported) { 502341825Sdim // This is the first time this function was exported from its source 503341825Sdim // module, so mark all functions and globals it references as exported 504341825Sdim // to the outside if they are defined in the same source module. 505341825Sdim // For efficiency, we unconditionally add all the referenced GUIDs 506341825Sdim // to the ExportList for this module, and will prune out any not 507341825Sdim // defined in the module later in a single pass. 508360784Sdim for (auto &Edge : ResolvedCalleeSummary->calls()) 509360784Sdim ExportList.insert(Edge.first); 510360784Sdim 511360784Sdim for (auto &Ref : ResolvedCalleeSummary->refs()) 512360784Sdim ExportList.insert(Ref); 513341825Sdim } 514341825Sdim } 515341825Sdim } 516341825Sdim 517314564Sdim auto GetAdjustedThreshold = [](unsigned Threshold, bool IsHotCallsite) { 518314564Sdim // Adjust the threshold for next level of imported functions. 519314564Sdim // The threshold is different for hot callsites because we can then 520314564Sdim // inline chains of hot calls. 521314564Sdim if (IsHotCallsite) 522314564Sdim return Threshold * ImportHotInstrFactor; 523314564Sdim return Threshold * ImportInstrFactor; 524314564Sdim }; 525314564Sdim 526314564Sdim const auto AdjThreshold = GetAdjustedThreshold(Threshold, IsHotCallsite); 527314564Sdim 528341825Sdim ImportCount++; 529292915Sdim 530309124Sdim // Insert the newly imported function to the worklist. 531321369Sdim Worklist.emplace_back(ResolvedCalleeSummary, AdjThreshold, VI.getGUID()); 532309124Sdim } 533309124Sdim} 534292915Sdim 535309124Sdim/// Given the list of globals defined in a module, compute the list of imports 536309124Sdim/// as well as the list of "exports", i.e. the list of symbols referenced from 537309124Sdim/// another module (that may require promotion). 538309124Sdimstatic void ComputeImportForModule( 539309124Sdim const GVSummaryMapTy &DefinedGVSummaries, const ModuleSummaryIndex &Index, 540344779Sdim StringRef ModName, FunctionImporter::ImportMapTy &ImportList, 541321369Sdim StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) { 542309124Sdim // Worklist contains the list of function imported in this module, for which 543309124Sdim // we will analyse the callees and may import further down the callgraph. 544309124Sdim SmallVector<EdgeInfo, 128> Worklist; 545341825Sdim FunctionImporter::ImportThresholdsTy ImportThresholds; 546292915Sdim 547309124Sdim // Populate the worklist with the import for the functions in the current 548309124Sdim // module 549309124Sdim for (auto &GVSummary : DefinedGVSummaries) { 550341825Sdim#ifndef NDEBUG 551341825Sdim // FIXME: Change the GVSummaryMapTy to hold ValueInfo instead of GUID 552341825Sdim // so this map look up (and possibly others) can be avoided. 553341825Sdim auto VI = Index.getValueInfo(GVSummary.first); 554341825Sdim#endif 555321369Sdim if (!Index.isGlobalValueLive(GVSummary.second)) { 556341825Sdim LLVM_DEBUG(dbgs() << "Ignores Dead GUID: " << VI << "\n"); 557314564Sdim continue; 558314564Sdim } 559327952Sdim auto *FuncSummary = 560327952Sdim dyn_cast<FunctionSummary>(GVSummary.second->getBaseObject()); 561309124Sdim if (!FuncSummary) 562309124Sdim // Skip import for global variables 563309124Sdim continue; 564341825Sdim LLVM_DEBUG(dbgs() << "Initialize import for " << VI << "\n"); 565309124Sdim computeImportForFunction(*FuncSummary, Index, ImportInstrLimit, 566314564Sdim DefinedGVSummaries, Worklist, ImportList, 567341825Sdim ExportLists, ImportThresholds); 568309124Sdim } 569292915Sdim 570314564Sdim // Process the newly imported functions and add callees to the worklist. 571309124Sdim while (!Worklist.empty()) { 572309124Sdim auto FuncInfo = Worklist.pop_back_val(); 573314564Sdim auto *Summary = std::get<0>(FuncInfo); 574314564Sdim auto Threshold = std::get<1>(FuncInfo); 575292915Sdim 576309124Sdim computeImportForFunction(*Summary, Index, Threshold, DefinedGVSummaries, 577341825Sdim Worklist, ImportList, ExportLists, 578341825Sdim ImportThresholds); 579309124Sdim } 580344779Sdim 581344779Sdim // Print stats about functions considered but rejected for importing 582344779Sdim // when requested. 583344779Sdim if (PrintImportFailures) { 584344779Sdim dbgs() << "Missed imports into module " << ModName << "\n"; 585344779Sdim for (auto &I : ImportThresholds) { 586344779Sdim auto &ProcessedThreshold = std::get<0>(I.second); 587344779Sdim auto &CalleeSummary = std::get<1>(I.second); 588344779Sdim auto &FailureInfo = std::get<2>(I.second); 589344779Sdim if (CalleeSummary) 590344779Sdim continue; // We are going to import. 591344779Sdim assert(FailureInfo); 592344779Sdim FunctionSummary *FS = nullptr; 593344779Sdim if (!FailureInfo->VI.getSummaryList().empty()) 594344779Sdim FS = dyn_cast<FunctionSummary>( 595344779Sdim FailureInfo->VI.getSummaryList()[0]->getBaseObject()); 596344779Sdim dbgs() << FailureInfo->VI 597344779Sdim << ": Reason = " << getFailureName(FailureInfo->Reason) 598344779Sdim << ", Threshold = " << ProcessedThreshold 599344779Sdim << ", Size = " << (FS ? (int)FS->instCount() : -1) 600344779Sdim << ", MaxHotness = " << getHotnessName(FailureInfo->MaxHotness) 601344779Sdim << ", Attempts = " << FailureInfo->Attempts << "\n"; 602344779Sdim } 603344779Sdim } 604309124Sdim} 605309124Sdim 606341825Sdim#ifndef NDEBUG 607360784Sdimstatic bool isGlobalVarSummary(const ModuleSummaryIndex &Index, ValueInfo VI) { 608360784Sdim auto SL = VI.getSummaryList(); 609360784Sdim return SL.empty() 610360784Sdim ? false 611360784Sdim : SL[0]->getSummaryKind() == GlobalValueSummary::GlobalVarKind; 612360784Sdim} 613360784Sdim 614341825Sdimstatic bool isGlobalVarSummary(const ModuleSummaryIndex &Index, 615341825Sdim GlobalValue::GUID G) { 616360784Sdim if (const auto &VI = Index.getValueInfo(G)) 617360784Sdim return isGlobalVarSummary(Index, VI); 618341825Sdim return false; 619341825Sdim} 620341825Sdim 621341825Sdimtemplate <class T> 622341825Sdimstatic unsigned numGlobalVarSummaries(const ModuleSummaryIndex &Index, 623341825Sdim T &Cont) { 624341825Sdim unsigned NumGVS = 0; 625341825Sdim for (auto &V : Cont) 626360784Sdim if (isGlobalVarSummary(Index, V)) 627341825Sdim ++NumGVS; 628341825Sdim return NumGVS; 629341825Sdim} 630341825Sdim#endif 631341825Sdim 632360784Sdim#ifndef NDEBUG 633360784Sdimstatic bool 634360784SdimcheckVariableImport(const ModuleSummaryIndex &Index, 635360784Sdim StringMap<FunctionImporter::ImportMapTy> &ImportLists, 636360784Sdim StringMap<FunctionImporter::ExportSetTy> &ExportLists) { 637360784Sdim 638360784Sdim DenseSet<GlobalValue::GUID> FlattenedImports; 639360784Sdim 640360784Sdim for (auto &ImportPerModule : ImportLists) 641360784Sdim for (auto &ExportPerModule : ImportPerModule.second) 642360784Sdim FlattenedImports.insert(ExportPerModule.second.begin(), 643360784Sdim ExportPerModule.second.end()); 644360784Sdim 645360784Sdim // Checks that all GUIDs of read/writeonly vars we see in export lists 646360784Sdim // are also in the import lists. Otherwise we my face linker undefs, 647360784Sdim // because readonly and writeonly vars are internalized in their 648360784Sdim // source modules. 649360784Sdim auto IsReadOrWriteOnlyVar = [&](StringRef ModulePath, const ValueInfo &VI) { 650360784Sdim auto *GVS = dyn_cast_or_null<GlobalVarSummary>( 651360784Sdim Index.findSummaryInModule(VI, ModulePath)); 652360784Sdim return GVS && (Index.isReadOnly(GVS) || Index.isWriteOnly(GVS)); 653360784Sdim }; 654360784Sdim 655360784Sdim for (auto &ExportPerModule : ExportLists) 656360784Sdim for (auto &VI : ExportPerModule.second) 657360784Sdim if (!FlattenedImports.count(VI.getGUID()) && 658360784Sdim IsReadOrWriteOnlyVar(ExportPerModule.first(), VI)) 659360784Sdim return false; 660360784Sdim 661360784Sdim return true; 662360784Sdim} 663360784Sdim#endif 664360784Sdim 665309124Sdim/// Compute all the import and export for every module using the Index. 666309124Sdimvoid llvm::ComputeCrossModuleImport( 667309124Sdim const ModuleSummaryIndex &Index, 668309124Sdim const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries, 669309124Sdim StringMap<FunctionImporter::ImportMapTy> &ImportLists, 670321369Sdim StringMap<FunctionImporter::ExportSetTy> &ExportLists) { 671309124Sdim // For each module that has function defined, compute the import/export lists. 672309124Sdim for (auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) { 673314564Sdim auto &ImportList = ImportLists[DefinedGVSummaries.first()]; 674341825Sdim LLVM_DEBUG(dbgs() << "Computing import for Module '" 675341825Sdim << DefinedGVSummaries.first() << "'\n"); 676344779Sdim ComputeImportForModule(DefinedGVSummaries.second, Index, 677344779Sdim DefinedGVSummaries.first(), ImportList, 678321369Sdim &ExportLists); 679309124Sdim } 680309124Sdim 681314564Sdim // When computing imports we added all GUIDs referenced by anything 682314564Sdim // imported from the module to its ExportList. Now we prune each ExportList 683314564Sdim // of any not defined in that module. This is more efficient than checking 684314564Sdim // while computing imports because some of the summary lists may be long 685314564Sdim // due to linkonce (comdat) copies. 686314564Sdim for (auto &ELI : ExportLists) { 687314564Sdim const auto &DefinedGVSummaries = 688314564Sdim ModuleToDefinedGVSummaries.lookup(ELI.first()); 689314564Sdim for (auto EI = ELI.second.begin(); EI != ELI.second.end();) { 690360784Sdim if (!DefinedGVSummaries.count(EI->getGUID())) 691360784Sdim ELI.second.erase(EI++); 692314564Sdim else 693314564Sdim ++EI; 694314564Sdim } 695314564Sdim } 696314564Sdim 697360784Sdim assert(checkVariableImport(Index, ImportLists, ExportLists)); 698309124Sdim#ifndef NDEBUG 699341825Sdim LLVM_DEBUG(dbgs() << "Import/Export lists for " << ImportLists.size() 700341825Sdim << " modules:\n"); 701309124Sdim for (auto &ModuleImports : ImportLists) { 702309124Sdim auto ModName = ModuleImports.first(); 703309124Sdim auto &Exports = ExportLists[ModName]; 704341825Sdim unsigned NumGVS = numGlobalVarSummaries(Index, Exports); 705341825Sdim LLVM_DEBUG(dbgs() << "* Module " << ModName << " exports " 706341825Sdim << Exports.size() - NumGVS << " functions and " << NumGVS 707341825Sdim << " vars. Imports from " << ModuleImports.second.size() 708341825Sdim << " modules.\n"); 709309124Sdim for (auto &Src : ModuleImports.second) { 710309124Sdim auto SrcModName = Src.first(); 711341825Sdim unsigned NumGVSPerMod = numGlobalVarSummaries(Index, Src.second); 712341825Sdim LLVM_DEBUG(dbgs() << " - " << Src.second.size() - NumGVSPerMod 713341825Sdim << " functions imported from " << SrcModName << "\n"); 714341825Sdim LLVM_DEBUG(dbgs() << " - " << NumGVSPerMod 715341825Sdim << " global vars imported from " << SrcModName << "\n"); 716292915Sdim } 717309124Sdim } 718309124Sdim#endif 719309124Sdim} 720292915Sdim 721327952Sdim#ifndef NDEBUG 722341825Sdimstatic void dumpImportListForModule(const ModuleSummaryIndex &Index, 723341825Sdim StringRef ModulePath, 724327952Sdim FunctionImporter::ImportMapTy &ImportList) { 725341825Sdim LLVM_DEBUG(dbgs() << "* Module " << ModulePath << " imports from " 726341825Sdim << ImportList.size() << " modules.\n"); 727327952Sdim for (auto &Src : ImportList) { 728327952Sdim auto SrcModName = Src.first(); 729341825Sdim unsigned NumGVSPerMod = numGlobalVarSummaries(Index, Src.second); 730341825Sdim LLVM_DEBUG(dbgs() << " - " << Src.second.size() - NumGVSPerMod 731341825Sdim << " functions imported from " << SrcModName << "\n"); 732341825Sdim LLVM_DEBUG(dbgs() << " - " << NumGVSPerMod << " vars imported from " 733341825Sdim << SrcModName << "\n"); 734327952Sdim } 735327952Sdim} 736327952Sdim#endif 737327952Sdim 738309124Sdim/// Compute all the imports for the given module in the Index. 739309124Sdimvoid llvm::ComputeCrossModuleImportForModule( 740309124Sdim StringRef ModulePath, const ModuleSummaryIndex &Index, 741309124Sdim FunctionImporter::ImportMapTy &ImportList) { 742309124Sdim // Collect the list of functions this module defines. 743309124Sdim // GUID -> Summary 744309124Sdim GVSummaryMapTy FunctionSummaryMap; 745309124Sdim Index.collectDefinedFunctionsForModule(ModulePath, FunctionSummaryMap); 746309124Sdim 747309124Sdim // Compute the import list for this module. 748341825Sdim LLVM_DEBUG(dbgs() << "Computing import for Module '" << ModulePath << "'\n"); 749344779Sdim ComputeImportForModule(FunctionSummaryMap, Index, ModulePath, ImportList); 750309124Sdim 751309124Sdim#ifndef NDEBUG 752341825Sdim dumpImportListForModule(Index, ModulePath, ImportList); 753327952Sdim#endif 754327952Sdim} 755327952Sdim 756327952Sdim// Mark all external summaries in Index for import into the given module. 757327952Sdim// Used for distributed builds using a distributed index. 758327952Sdimvoid llvm::ComputeCrossModuleImportForModuleFromIndex( 759327952Sdim StringRef ModulePath, const ModuleSummaryIndex &Index, 760327952Sdim FunctionImporter::ImportMapTy &ImportList) { 761327952Sdim for (auto &GlobalList : Index) { 762327952Sdim // Ignore entries for undefined references. 763327952Sdim if (GlobalList.second.SummaryList.empty()) 764327952Sdim continue; 765327952Sdim 766327952Sdim auto GUID = GlobalList.first; 767327952Sdim assert(GlobalList.second.SummaryList.size() == 1 && 768327952Sdim "Expected individual combined index to have one summary per GUID"); 769327952Sdim auto &Summary = GlobalList.second.SummaryList[0]; 770327952Sdim // Skip the summaries for the importing module. These are included to 771327952Sdim // e.g. record required linkage changes. 772327952Sdim if (Summary->modulePath() == ModulePath) 773327952Sdim continue; 774341825Sdim // Add an entry to provoke importing by thinBackend. 775341825Sdim ImportList[Summary->modulePath()].insert(GUID); 776292915Sdim } 777327952Sdim#ifndef NDEBUG 778341825Sdim dumpImportListForModule(Index, ModulePath, ImportList); 779309124Sdim#endif 780292915Sdim} 781292915Sdim 782321369Sdimvoid llvm::computeDeadSymbols( 783321369Sdim ModuleSummaryIndex &Index, 784341825Sdim const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols, 785341825Sdim function_ref<PrevailingType(GlobalValue::GUID)> isPrevailing) { 786321369Sdim assert(!Index.withGlobalValueDeadStripping()); 787314564Sdim if (!ComputeDead) 788321369Sdim return; 789314564Sdim if (GUIDPreservedSymbols.empty()) 790314564Sdim // Don't do anything when nothing is live, this is friendly with tests. 791321369Sdim return; 792321369Sdim unsigned LiveSymbols = 0; 793321369Sdim SmallVector<ValueInfo, 128> Worklist; 794321369Sdim Worklist.reserve(GUIDPreservedSymbols.size() * 2); 795321369Sdim for (auto GUID : GUIDPreservedSymbols) { 796321369Sdim ValueInfo VI = Index.getValueInfo(GUID); 797321369Sdim if (!VI) 798314564Sdim continue; 799321369Sdim for (auto &S : VI.getSummaryList()) 800321369Sdim S->setLive(true); 801314564Sdim } 802314564Sdim 803321369Sdim // Add values flagged in the index as live roots to the worklist. 804341825Sdim for (const auto &Entry : Index) { 805341825Sdim auto VI = Index.getValueInfo(Entry); 806321369Sdim for (auto &S : Entry.second.SummaryList) 807321369Sdim if (S->isLive()) { 808341825Sdim LLVM_DEBUG(dbgs() << "Live root: " << VI << "\n"); 809341825Sdim Worklist.push_back(VI); 810321369Sdim ++LiveSymbols; 811321369Sdim break; 812321369Sdim } 813341825Sdim } 814321369Sdim 815321369Sdim // Make value live and add it to the worklist if it was not live before. 816360784Sdim auto visit = [&](ValueInfo VI, bool IsAliasee) { 817327952Sdim // FIXME: If we knew which edges were created for indirect call profiles, 818327952Sdim // we could skip them here. Any that are live should be reached via 819327952Sdim // other edges, e.g. reference edges. Otherwise, using a profile collected 820327952Sdim // on a slightly different binary might provoke preserving, importing 821327952Sdim // and ultimately promoting calls to functions not linked into this 822327952Sdim // binary, which increases the binary size unnecessarily. Note that 823327952Sdim // if this code changes, the importer needs to change so that edges 824327952Sdim // to functions marked dead are skipped. 825327952Sdim VI = updateValueInfoForIndirectCalls(Index, VI); 826327952Sdim if (!VI) 827327952Sdim return; 828341825Sdim 829353358Sdim if (llvm::any_of(VI.getSummaryList(), 830344779Sdim [](const std::unique_ptr<llvm::GlobalValueSummary> &S) { 831344779Sdim return S->isLive(); 832344779Sdim })) 833344779Sdim return; 834344779Sdim 835341825Sdim // We only keep live symbols that are known to be non-prevailing if any are 836344779Sdim // available_externally, linkonceodr, weakodr. Those symbols are discarded 837344779Sdim // later in the EliminateAvailableExternally pass and setting them to 838344779Sdim // not-live could break downstreams users of liveness information (PR36483) 839344779Sdim // or limit optimization opportunities. 840341825Sdim if (isPrevailing(VI.getGUID()) == PrevailingType::No) { 841344779Sdim bool KeepAliveLinkage = false; 842341825Sdim bool Interposable = false; 843341825Sdim for (auto &S : VI.getSummaryList()) { 844344779Sdim if (S->linkage() == GlobalValue::AvailableExternallyLinkage || 845344779Sdim S->linkage() == GlobalValue::WeakODRLinkage || 846344779Sdim S->linkage() == GlobalValue::LinkOnceODRLinkage) 847344779Sdim KeepAliveLinkage = true; 848341825Sdim else if (GlobalValue::isInterposableLinkage(S->linkage())) 849341825Sdim Interposable = true; 850341825Sdim } 851341825Sdim 852360784Sdim if (!IsAliasee) { 853360784Sdim if (!KeepAliveLinkage) 854360784Sdim return; 855341825Sdim 856360784Sdim if (Interposable) 857360784Sdim report_fatal_error( 858360784Sdim "Interposable and available_externally/linkonce_odr/weak_odr " 859360784Sdim "symbol"); 860360784Sdim } 861341825Sdim } 862341825Sdim 863321369Sdim for (auto &S : VI.getSummaryList()) 864321369Sdim S->setLive(true); 865321369Sdim ++LiveSymbols; 866321369Sdim Worklist.push_back(VI); 867321369Sdim }; 868321369Sdim 869314564Sdim while (!Worklist.empty()) { 870321369Sdim auto VI = Worklist.pop_back_val(); 871321369Sdim for (auto &Summary : VI.getSummaryList()) { 872353358Sdim if (auto *AS = dyn_cast<AliasSummary>(Summary.get())) { 873353358Sdim // If this is an alias, visit the aliasee VI to ensure that all copies 874353358Sdim // are marked live and it is added to the worklist for further 875353358Sdim // processing of its references. 876360784Sdim visit(AS->getAliaseeVI(), true); 877353358Sdim continue; 878353358Sdim } 879353358Sdim 880353358Sdim Summary->setLive(true); 881353358Sdim for (auto Ref : Summary->refs()) 882360784Sdim visit(Ref, false); 883353358Sdim if (auto *FS = dyn_cast<FunctionSummary>(Summary.get())) 884321369Sdim for (auto Call : FS->calls()) 885360784Sdim visit(Call.first, false); 886314564Sdim } 887314564Sdim } 888321369Sdim Index.setWithGlobalValueDeadStripping(); 889321369Sdim 890321369Sdim unsigned DeadSymbols = Index.size() - LiveSymbols; 891341825Sdim LLVM_DEBUG(dbgs() << LiveSymbols << " symbols Live, and " << DeadSymbols 892341825Sdim << " symbols Dead \n"); 893321369Sdim NumDeadSymbols += DeadSymbols; 894321369Sdim NumLiveSymbols += LiveSymbols; 895314564Sdim} 896314564Sdim 897344779Sdim// Compute dead symbols and propagate constants in combined index. 898344779Sdimvoid llvm::computeDeadSymbolsWithConstProp( 899344779Sdim ModuleSummaryIndex &Index, 900344779Sdim const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols, 901344779Sdim function_ref<PrevailingType(GlobalValue::GUID)> isPrevailing, 902344779Sdim bool ImportEnabled) { 903344779Sdim computeDeadSymbols(Index, GUIDPreservedSymbols, isPrevailing); 904360784Sdim if (ImportEnabled) 905353358Sdim Index.propagateAttributes(GUIDPreservedSymbols); 906344779Sdim} 907344779Sdim 908309124Sdim/// Compute the set of summaries needed for a ThinLTO backend compilation of 909309124Sdim/// \p ModulePath. 910309124Sdimvoid llvm::gatherImportedSummariesForModule( 911309124Sdim StringRef ModulePath, 912309124Sdim const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries, 913314564Sdim const FunctionImporter::ImportMapTy &ImportList, 914309124Sdim std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) { 915309124Sdim // Include all summaries from the importing module. 916309124Sdim ModuleToSummariesForIndex[ModulePath] = 917309124Sdim ModuleToDefinedGVSummaries.lookup(ModulePath); 918314564Sdim // Include summaries for imports. 919314564Sdim for (auto &ILI : ImportList) { 920314564Sdim auto &SummariesForIndex = ModuleToSummariesForIndex[ILI.first()]; 921314564Sdim const auto &DefinedGVSummaries = 922314564Sdim ModuleToDefinedGVSummaries.lookup(ILI.first()); 923314564Sdim for (auto &GI : ILI.second) { 924341825Sdim const auto &DS = DefinedGVSummaries.find(GI); 925314564Sdim assert(DS != DefinedGVSummaries.end() && 926314564Sdim "Expected a defined summary for imported global value"); 927341825Sdim SummariesForIndex[GI] = DS->second; 928309124Sdim } 929309124Sdim } 930309124Sdim} 931309124Sdim 932309124Sdim/// Emit the files \p ModulePath will import from into \p OutputFilename. 933341825Sdimstd::error_code llvm::EmitImportsFiles( 934341825Sdim StringRef ModulePath, StringRef OutputFilename, 935341825Sdim const std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) { 936309124Sdim std::error_code EC; 937360784Sdim raw_fd_ostream ImportsOS(OutputFilename, EC, sys::fs::OpenFlags::OF_None); 938309124Sdim if (EC) 939309124Sdim return EC; 940341825Sdim for (auto &ILI : ModuleToSummariesForIndex) 941341825Sdim // The ModuleToSummariesForIndex map includes an entry for the current 942341825Sdim // Module (needed for writing out the index files). We don't want to 943341825Sdim // include it in the imports file, however, so filter it out. 944341825Sdim if (ILI.first != ModulePath) 945341825Sdim ImportsOS << ILI.first << "\n"; 946309124Sdim return std::error_code(); 947309124Sdim} 948309124Sdim 949341825Sdimbool llvm::convertToDeclaration(GlobalValue &GV) { 950341825Sdim LLVM_DEBUG(dbgs() << "Converting to a declaration: `" << GV.getName() 951341825Sdim << "\n"); 952341825Sdim if (Function *F = dyn_cast<Function>(&GV)) { 953341825Sdim F->deleteBody(); 954341825Sdim F->clearMetadata(); 955341825Sdim F->setComdat(nullptr); 956341825Sdim } else if (GlobalVariable *V = dyn_cast<GlobalVariable>(&GV)) { 957341825Sdim V->setInitializer(nullptr); 958341825Sdim V->setLinkage(GlobalValue::ExternalLinkage); 959341825Sdim V->clearMetadata(); 960341825Sdim V->setComdat(nullptr); 961341825Sdim } else { 962341825Sdim GlobalValue *NewGV; 963341825Sdim if (GV.getValueType()->isFunctionTy()) 964341825Sdim NewGV = 965341825Sdim Function::Create(cast<FunctionType>(GV.getValueType()), 966344779Sdim GlobalValue::ExternalLinkage, GV.getAddressSpace(), 967344779Sdim "", GV.getParent()); 968341825Sdim else 969341825Sdim NewGV = 970341825Sdim new GlobalVariable(*GV.getParent(), GV.getValueType(), 971341825Sdim /*isConstant*/ false, GlobalValue::ExternalLinkage, 972341825Sdim /*init*/ nullptr, "", 973341825Sdim /*insertbefore*/ nullptr, GV.getThreadLocalMode(), 974341825Sdim GV.getType()->getAddressSpace()); 975341825Sdim NewGV->takeName(&GV); 976341825Sdim GV.replaceAllUsesWith(NewGV); 977341825Sdim return false; 978341825Sdim } 979341825Sdim return true; 980341825Sdim} 981341825Sdim 982344779Sdim/// Fixup prevailing symbol linkages in \p TheModule based on summary analysis. 983344779Sdimvoid llvm::thinLTOResolvePrevailingInModule( 984309124Sdim Module &TheModule, const GVSummaryMapTy &DefinedGlobals) { 985309124Sdim auto updateLinkage = [&](GlobalValue &GV) { 986309124Sdim // See if the global summary analysis computed a new resolved linkage. 987309124Sdim const auto &GS = DefinedGlobals.find(GV.getGUID()); 988309124Sdim if (GS == DefinedGlobals.end()) 989309124Sdim return; 990309124Sdim auto NewLinkage = GS->second->linkage(); 991309124Sdim if (NewLinkage == GV.getLinkage()) 992309124Sdim return; 993344779Sdim if (GlobalValue::isLocalLinkage(GV.getLinkage()) || 994360784Sdim // Don't internalize anything here, because the code below 995360784Sdim // lacks necessary correctness checks. Leave this job to 996360784Sdim // LLVM 'internalize' pass. 997360784Sdim GlobalValue::isLocalLinkage(NewLinkage) || 998344779Sdim // In case it was dead and already converted to declaration. 999344779Sdim GV.isDeclaration()) 1000321369Sdim return; 1001360784Sdim 1002321369Sdim // Check for a non-prevailing def that has interposable linkage 1003321369Sdim // (e.g. non-odr weak or linkonce). In that case we can't simply 1004321369Sdim // convert to available_externally, since it would lose the 1005321369Sdim // interposable property and possibly get inlined. Simply drop 1006321369Sdim // the definition in that case. 1007321369Sdim if (GlobalValue::isAvailableExternallyLinkage(NewLinkage) && 1008341825Sdim GlobalValue::isInterposableLinkage(GV.getLinkage())) { 1009341825Sdim if (!convertToDeclaration(GV)) 1010341825Sdim // FIXME: Change this to collect replaced GVs and later erase 1011344779Sdim // them from the parent module once thinLTOResolvePrevailingGUID is 1012341825Sdim // changed to enable this for aliases. 1013341825Sdim llvm_unreachable("Expected GV to be converted"); 1014341825Sdim } else { 1015353358Sdim // If all copies of the original symbol had global unnamed addr and 1016353358Sdim // linkonce_odr linkage, it should be an auto hide symbol. In that case 1017353358Sdim // the thin link would have marked it as CanAutoHide. Add hidden visibility 1018353358Sdim // to the symbol to preserve the property. 1019353358Sdim if (NewLinkage == GlobalValue::WeakODRLinkage && 1020353358Sdim GS->second->canAutoHide()) { 1021353358Sdim assert(GV.hasLinkOnceODRLinkage() && GV.hasGlobalUnnamedAddr()); 1022341825Sdim GV.setVisibility(GlobalValue::HiddenVisibility); 1023353358Sdim } 1024341825Sdim 1025341825Sdim LLVM_DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName() 1026341825Sdim << "` from " << GV.getLinkage() << " to " << NewLinkage 1027341825Sdim << "\n"); 1028321369Sdim GV.setLinkage(NewLinkage); 1029321369Sdim } 1030321369Sdim // Remove declarations from comdats, including available_externally 1031314564Sdim // as this is a declaration for the linker, and will be dropped eventually. 1032314564Sdim // It is illegal for comdats to contain declarations. 1033314564Sdim auto *GO = dyn_cast_or_null<GlobalObject>(&GV); 1034321369Sdim if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) 1035314564Sdim GO->setComdat(nullptr); 1036309124Sdim }; 1037309124Sdim 1038309124Sdim // Process functions and global now 1039309124Sdim for (auto &GV : TheModule) 1040309124Sdim updateLinkage(GV); 1041309124Sdim for (auto &GV : TheModule.globals()) 1042309124Sdim updateLinkage(GV); 1043309124Sdim for (auto &GV : TheModule.aliases()) 1044309124Sdim updateLinkage(GV); 1045309124Sdim} 1046309124Sdim 1047309124Sdim/// Run internalization on \p TheModule based on symmary analysis. 1048309124Sdimvoid llvm::thinLTOInternalizeModule(Module &TheModule, 1049309124Sdim const GVSummaryMapTy &DefinedGlobals) { 1050309124Sdim // Declare a callback for the internalize pass that will ask for every 1051309124Sdim // candidate GlobalValue if it can be internalized or not. 1052309124Sdim auto MustPreserveGV = [&](const GlobalValue &GV) -> bool { 1053309124Sdim // Lookup the linkage recorded in the summaries during global analysis. 1054321369Sdim auto GS = DefinedGlobals.find(GV.getGUID()); 1055309124Sdim if (GS == DefinedGlobals.end()) { 1056309124Sdim // Must have been promoted (possibly conservatively). Find original 1057309124Sdim // name so that we can access the correct summary and see if it can 1058309124Sdim // be internalized again. 1059309124Sdim // FIXME: Eventually we should control promotion instead of promoting 1060309124Sdim // and internalizing again. 1061309124Sdim StringRef OrigName = 1062309124Sdim ModuleSummaryIndex::getOriginalNameBeforePromote(GV.getName()); 1063309124Sdim std::string OrigId = GlobalValue::getGlobalIdentifier( 1064309124Sdim OrigName, GlobalValue::InternalLinkage, 1065309124Sdim TheModule.getSourceFileName()); 1066321369Sdim GS = DefinedGlobals.find(GlobalValue::getGUID(OrigId)); 1067309124Sdim if (GS == DefinedGlobals.end()) { 1068309124Sdim // Also check the original non-promoted non-globalized name. In some 1069309124Sdim // cases a preempted weak value is linked in as a local copy because 1070309124Sdim // it is referenced by an alias (IRLinker::linkGlobalValueProto). 1071309124Sdim // In that case, since it was originally not a local value, it was 1072309124Sdim // recorded in the index using the original name. 1073309124Sdim // FIXME: This may not be needed once PR27866 is fixed. 1074321369Sdim GS = DefinedGlobals.find(GlobalValue::getGUID(OrigName)); 1075309124Sdim assert(GS != DefinedGlobals.end()); 1076309124Sdim } 1077321369Sdim } 1078321369Sdim return !GlobalValue::isLocalLinkage(GS->second->linkage()); 1079309124Sdim }; 1080309124Sdim 1081309124Sdim // FIXME: See if we can just internalize directly here via linkage changes 1082309124Sdim // based on the index, rather than invoking internalizeModule. 1083327952Sdim internalizeModule(TheModule, MustPreserveGV); 1084309124Sdim} 1085309124Sdim 1086327952Sdim/// Make alias a clone of its aliasee. 1087327952Sdimstatic Function *replaceAliasWithAliasee(Module *SrcModule, GlobalAlias *GA) { 1088327952Sdim Function *Fn = cast<Function>(GA->getBaseObject()); 1089327952Sdim 1090327952Sdim ValueToValueMapTy VMap; 1091327952Sdim Function *NewFn = CloneFunction(Fn, VMap); 1092353358Sdim // Clone should use the original alias's linkage, visibility and name, and we 1093353358Sdim // ensure all uses of alias instead use the new clone (casted if necessary). 1094327952Sdim NewFn->setLinkage(GA->getLinkage()); 1095353358Sdim NewFn->setVisibility(GA->getVisibility()); 1096327952Sdim GA->replaceAllUsesWith(ConstantExpr::getBitCast(NewFn, GA->getType())); 1097327952Sdim NewFn->takeName(GA); 1098327952Sdim return NewFn; 1099327952Sdim} 1100327952Sdim 1101344779Sdim// Internalize values that we marked with specific attribute 1102344779Sdim// in processGlobalForThinLTO. 1103353358Sdimstatic void internalizeGVsAfterImport(Module &M) { 1104344779Sdim for (auto &GV : M.globals()) 1105344779Sdim // Skip GVs which have been converted to declarations 1106344779Sdim // by dropDeadSymbols. 1107344779Sdim if (!GV.isDeclaration() && GV.hasAttribute("thinlto-internalize")) { 1108344779Sdim GV.setLinkage(GlobalValue::InternalLinkage); 1109344779Sdim GV.setVisibility(GlobalValue::DefaultVisibility); 1110344779Sdim } 1111344779Sdim} 1112344779Sdim 1113292915Sdim// Automatically import functions in Module \p DestModule based on the summaries 1114292915Sdim// index. 1115314564SdimExpected<bool> FunctionImporter::importFunctions( 1116321369Sdim Module &DestModule, const FunctionImporter::ImportMapTy &ImportList) { 1117341825Sdim LLVM_DEBUG(dbgs() << "Starting import for Module " 1118341825Sdim << DestModule.getModuleIdentifier() << "\n"); 1119341825Sdim unsigned ImportedCount = 0, ImportedGVCount = 0; 1120292915Sdim 1121321369Sdim IRMover Mover(DestModule); 1122292915Sdim // Do the actual import of functions now, one Module at a time 1123309124Sdim std::set<StringRef> ModuleNameOrderedList; 1124309124Sdim for (auto &FunctionsToImportPerModule : ImportList) { 1125309124Sdim ModuleNameOrderedList.insert(FunctionsToImportPerModule.first()); 1126309124Sdim } 1127309124Sdim for (auto &Name : ModuleNameOrderedList) { 1128292915Sdim // Get the module for the import 1129309124Sdim const auto &FunctionsToImportPerModule = ImportList.find(Name); 1130309124Sdim assert(FunctionsToImportPerModule != ImportList.end()); 1131314564Sdim Expected<std::unique_ptr<Module>> SrcModuleOrErr = ModuleLoader(Name); 1132314564Sdim if (!SrcModuleOrErr) 1133314564Sdim return SrcModuleOrErr.takeError(); 1134314564Sdim std::unique_ptr<Module> SrcModule = std::move(*SrcModuleOrErr); 1135292915Sdim assert(&DestModule.getContext() == &SrcModule->getContext() && 1136292915Sdim "Context mismatch"); 1137292915Sdim 1138309124Sdim // If modules were created with lazy metadata loading, materialize it 1139309124Sdim // now, before linking it (otherwise this will be a noop). 1140314564Sdim if (Error Err = SrcModule->materializeMetadata()) 1141314564Sdim return std::move(Err); 1142292915Sdim 1143309124Sdim auto &ImportGUIDs = FunctionsToImportPerModule->second; 1144309124Sdim // Find the globals to import 1145321369Sdim SetVector<GlobalValue *> GlobalsToImport; 1146309124Sdim for (Function &F : *SrcModule) { 1147309124Sdim if (!F.hasName()) 1148309124Sdim continue; 1149309124Sdim auto GUID = F.getGUID(); 1150309124Sdim auto Import = ImportGUIDs.count(GUID); 1151341825Sdim LLVM_DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing function " 1152341825Sdim << GUID << " " << F.getName() << " from " 1153341825Sdim << SrcModule->getSourceFileName() << "\n"); 1154309124Sdim if (Import) { 1155314564Sdim if (Error Err = F.materialize()) 1156314564Sdim return std::move(Err); 1157309124Sdim if (EnableImportMetadata) { 1158309124Sdim // Add 'thinlto_src_module' metadata for statistics and debugging. 1159309124Sdim F.setMetadata( 1160309124Sdim "thinlto_src_module", 1161327952Sdim MDNode::get(DestModule.getContext(), 1162327952Sdim {MDString::get(DestModule.getContext(), 1163327952Sdim SrcModule->getSourceFileName())})); 1164309124Sdim } 1165309124Sdim GlobalsToImport.insert(&F); 1166309124Sdim } 1167309124Sdim } 1168309124Sdim for (GlobalVariable &GV : SrcModule->globals()) { 1169309124Sdim if (!GV.hasName()) 1170309124Sdim continue; 1171309124Sdim auto GUID = GV.getGUID(); 1172309124Sdim auto Import = ImportGUIDs.count(GUID); 1173341825Sdim LLVM_DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing global " 1174341825Sdim << GUID << " " << GV.getName() << " from " 1175341825Sdim << SrcModule->getSourceFileName() << "\n"); 1176309124Sdim if (Import) { 1177314564Sdim if (Error Err = GV.materialize()) 1178314564Sdim return std::move(Err); 1179341825Sdim ImportedGVCount += GlobalsToImport.insert(&GV); 1180309124Sdim } 1181309124Sdim } 1182309124Sdim for (GlobalAlias &GA : SrcModule->aliases()) { 1183309124Sdim if (!GA.hasName()) 1184309124Sdim continue; 1185309124Sdim auto GUID = GA.getGUID(); 1186309124Sdim auto Import = ImportGUIDs.count(GUID); 1187341825Sdim LLVM_DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing alias " 1188341825Sdim << GUID << " " << GA.getName() << " from " 1189341825Sdim << SrcModule->getSourceFileName() << "\n"); 1190309124Sdim if (Import) { 1191314564Sdim if (Error Err = GA.materialize()) 1192314564Sdim return std::move(Err); 1193327952Sdim // Import alias as a copy of its aliasee. 1194327952Sdim GlobalObject *Base = GA.getBaseObject(); 1195327952Sdim if (Error Err = Base->materialize()) 1196327952Sdim return std::move(Err); 1197327952Sdim auto *Fn = replaceAliasWithAliasee(SrcModule.get(), &GA); 1198341825Sdim LLVM_DEBUG(dbgs() << "Is importing aliasee fn " << Base->getGUID() 1199341825Sdim << " " << Base->getName() << " from " 1200341825Sdim << SrcModule->getSourceFileName() << "\n"); 1201327952Sdim if (EnableImportMetadata) { 1202327952Sdim // Add 'thinlto_src_module' metadata for statistics and debugging. 1203327952Sdim Fn->setMetadata( 1204327952Sdim "thinlto_src_module", 1205327952Sdim MDNode::get(DestModule.getContext(), 1206327952Sdim {MDString::get(DestModule.getContext(), 1207327952Sdim SrcModule->getSourceFileName())})); 1208327952Sdim } 1209327952Sdim GlobalsToImport.insert(Fn); 1210309124Sdim } 1211309124Sdim } 1212309124Sdim 1213314564Sdim // Upgrade debug info after we're done materializing all the globals and we 1214314564Sdim // have loaded all the required metadata! 1215314564Sdim UpgradeDebugInfo(*SrcModule); 1216314564Sdim 1217292915Sdim // Link in the specified functions. 1218309124Sdim if (renameModuleForThinLTO(*SrcModule, Index, &GlobalsToImport)) 1219309124Sdim return true; 1220309124Sdim 1221309124Sdim if (PrintImports) { 1222309124Sdim for (const auto *GV : GlobalsToImport) 1223309124Sdim dbgs() << DestModule.getSourceFileName() << ": Import " << GV->getName() 1224309124Sdim << " from " << SrcModule->getSourceFileName() << "\n"; 1225309124Sdim } 1226309124Sdim 1227321369Sdim if (Mover.move(std::move(SrcModule), GlobalsToImport.getArrayRef(), 1228321369Sdim [](GlobalValue &, IRMover::ValueAdder) {}, 1229321369Sdim /*IsPerformingImport=*/true)) 1230292915Sdim report_fatal_error("Function Import: link error"); 1231292915Sdim 1232309124Sdim ImportedCount += GlobalsToImport.size(); 1233314564Sdim NumImportedModules++; 1234292915Sdim } 1235292915Sdim 1236353358Sdim internalizeGVsAfterImport(DestModule); 1237344779Sdim 1238341825Sdim NumImportedFunctions += (ImportedCount - ImportedGVCount); 1239341825Sdim NumImportedGlobalVars += ImportedGVCount; 1240292915Sdim 1241341825Sdim LLVM_DEBUG(dbgs() << "Imported " << ImportedCount - ImportedGVCount 1242341825Sdim << " functions for Module " 1243341825Sdim << DestModule.getModuleIdentifier() << "\n"); 1244341825Sdim LLVM_DEBUG(dbgs() << "Imported " << ImportedGVCount 1245341825Sdim << " global variables for Module " 1246341825Sdim << DestModule.getModuleIdentifier() << "\n"); 1247292915Sdim return ImportedCount; 1248292915Sdim} 1249292915Sdim 1250314564Sdimstatic bool doImportingForModule(Module &M) { 1251314564Sdim if (SummaryFile.empty()) 1252314564Sdim report_fatal_error("error: -function-import requires -summary-file\n"); 1253314564Sdim Expected<std::unique_ptr<ModuleSummaryIndex>> IndexPtrOrErr = 1254314564Sdim getModuleSummaryIndexForFile(SummaryFile); 1255314564Sdim if (!IndexPtrOrErr) { 1256314564Sdim logAllUnhandledErrors(IndexPtrOrErr.takeError(), errs(), 1257314564Sdim "Error loading file '" + SummaryFile + "': "); 1258314564Sdim return false; 1259314564Sdim } 1260314564Sdim std::unique_ptr<ModuleSummaryIndex> Index = std::move(*IndexPtrOrErr); 1261292915Sdim 1262314564Sdim // First step is collecting the import list. 1263314564Sdim FunctionImporter::ImportMapTy ImportList; 1264327952Sdim // If requested, simply import all functions in the index. This is used 1265327952Sdim // when testing distributed backend handling via the opt tool, when 1266327952Sdim // we have distributed indexes containing exactly the summaries to import. 1267327952Sdim if (ImportAllIndex) 1268327952Sdim ComputeCrossModuleImportForModuleFromIndex(M.getModuleIdentifier(), *Index, 1269327952Sdim ImportList); 1270327952Sdim else 1271327952Sdim ComputeCrossModuleImportForModule(M.getModuleIdentifier(), *Index, 1272327952Sdim ImportList); 1273314564Sdim 1274314564Sdim // Conservatively mark all internal values as promoted. This interface is 1275314564Sdim // only used when doing importing via the function importing pass. The pass 1276314564Sdim // is only enabled when testing importing via the 'opt' tool, which does 1277314564Sdim // not do the ThinLink that would normally determine what values to promote. 1278314564Sdim for (auto &I : *Index) { 1279321369Sdim for (auto &S : I.second.SummaryList) { 1280314564Sdim if (GlobalValue::isLocalLinkage(S->linkage())) 1281314564Sdim S->setLinkage(GlobalValue::ExternalLinkage); 1282314564Sdim } 1283292915Sdim } 1284314564Sdim 1285314564Sdim // Next we need to promote to global scope and rename any local values that 1286314564Sdim // are potentially exported to other modules. 1287314564Sdim if (renameModuleForThinLTO(M, *Index, nullptr)) { 1288314564Sdim errs() << "Error renaming module\n"; 1289314564Sdim return false; 1290292915Sdim } 1291314564Sdim 1292314564Sdim // Perform the import now. 1293314564Sdim auto ModuleLoader = [&M](StringRef Identifier) { 1294314564Sdim return loadFile(Identifier, M.getContext()); 1295314564Sdim }; 1296314564Sdim FunctionImporter Importer(*Index, ModuleLoader); 1297321369Sdim Expected<bool> Result = Importer.importFunctions(M, ImportList); 1298314564Sdim 1299314564Sdim // FIXME: Probably need to propagate Errors through the pass manager. 1300314564Sdim if (!Result) { 1301314564Sdim logAllUnhandledErrors(Result.takeError(), errs(), 1302314564Sdim "Error importing module: "); 1303314564Sdim return false; 1304314564Sdim } 1305314564Sdim 1306314564Sdim return *Result; 1307292915Sdim} 1308292915Sdim 1309292915Sdimnamespace { 1310327952Sdim 1311292915Sdim/// Pass that performs cross-module function import provided a summary file. 1312314564Sdimclass FunctionImportLegacyPass : public ModulePass { 1313292915Sdimpublic: 1314292915Sdim /// Pass identification, replacement for typeid 1315292915Sdim static char ID; 1316292915Sdim 1317327952Sdim explicit FunctionImportLegacyPass() : ModulePass(ID) {} 1318327952Sdim 1319292915Sdim /// Specify pass name for debug output 1320314564Sdim StringRef getPassName() const override { return "Function Importing"; } 1321292915Sdim 1322292915Sdim bool runOnModule(Module &M) override { 1323309124Sdim if (skipModule(M)) 1324309124Sdim return false; 1325309124Sdim 1326314564Sdim return doImportingForModule(M); 1327292915Sdim } 1328292915Sdim}; 1329292915Sdim 1330327952Sdim} // end anonymous namespace 1331327952Sdim 1332314564SdimPreservedAnalyses FunctionImportPass::run(Module &M, 1333314564Sdim ModuleAnalysisManager &AM) { 1334314564Sdim if (!doImportingForModule(M)) 1335314564Sdim return PreservedAnalyses::all(); 1336292915Sdim 1337314564Sdim return PreservedAnalyses::none(); 1338314564Sdim} 1339314564Sdim 1340314564Sdimchar FunctionImportLegacyPass::ID = 0; 1341314564SdimINITIALIZE_PASS(FunctionImportLegacyPass, "function-import", 1342314564Sdim "Summary Based Function Import", false, false) 1343314564Sdim 1344292915Sdimnamespace llvm { 1345327952Sdim 1346314564SdimPass *createFunctionImportPass() { 1347314564Sdim return new FunctionImportLegacyPass(); 1348292915Sdim} 1349327952Sdim 1350327952Sdim} // end namespace llvm 1351