LCOV - code coverage report
Current view: top level - lib/LTO - LTOBackend.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 212 240 88.3 %
Date: 2018-07-13 00:08:38 Functions: 21 23 91.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-LTOBackend.cpp - LLVM Link Time Optimizer Backend -------------------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This file implements the "backend" phase of LTO, i.e. it performs
      11             : // optimization and code generation on a loaded module. It is generally used
      12             : // internally by the LTO class but can also be used independently, for example
      13             : // to implement a standalone ThinLTO backend.
      14             : //
      15             : //===----------------------------------------------------------------------===//
      16             : 
      17             : #include "llvm/LTO/LTOBackend.h"
      18             : #include "llvm/Analysis/AliasAnalysis.h"
      19             : #include "llvm/Analysis/CGSCCPassManager.h"
      20             : #include "llvm/Analysis/TargetLibraryInfo.h"
      21             : #include "llvm/Analysis/TargetTransformInfo.h"
      22             : #include "llvm/Bitcode/BitcodeReader.h"
      23             : #include "llvm/Bitcode/BitcodeWriter.h"
      24             : #include "llvm/IR/LegacyPassManager.h"
      25             : #include "llvm/IR/PassManager.h"
      26             : #include "llvm/IR/Verifier.h"
      27             : #include "llvm/LTO/LTO.h"
      28             : #include "llvm/MC/SubtargetFeature.h"
      29             : #include "llvm/Object/ModuleSymbolTable.h"
      30             : #include "llvm/Passes/PassBuilder.h"
      31             : #include "llvm/Support/Error.h"
      32             : #include "llvm/Support/FileSystem.h"
      33             : #include "llvm/Support/MemoryBuffer.h"
      34             : #include "llvm/Support/Path.h"
      35             : #include "llvm/Support/Program.h"
      36             : #include "llvm/Support/raw_ostream.h"
      37             : #include "llvm/Support/TargetRegistry.h"
      38             : #include "llvm/Support/ThreadPool.h"
      39             : #include "llvm/Target/TargetMachine.h"
      40             : #include "llvm/Transforms/IPO.h"
      41             : #include "llvm/Transforms/IPO/PassManagerBuilder.h"
      42             : #include "llvm/Transforms/Scalar/LoopPassManager.h"
      43             : #include "llvm/Transforms/Utils/FunctionImportUtils.h"
      44             : #include "llvm/Transforms/Utils/SplitModule.h"
      45             : 
      46             : using namespace llvm;
      47             : using namespace lto;
      48             : 
      49           0 : LLVM_ATTRIBUTE_NORETURN static void reportOpenError(StringRef Path, Twine Msg) {
      50           0 :   errs() << "failed to open " << Path << ": " << Msg << '\n';
      51           0 :   errs().flush();
      52           0 :   exit(1);
      53             : }
      54             : 
      55         161 : Error Config::addSaveTemps(std::string OutputFileName,
      56             :                            bool UseInputModulePath) {
      57         161 :   ShouldDiscardValueNames = false;
      58             : 
      59             :   std::error_code EC;
      60         322 :   ResolutionFile = llvm::make_unique<raw_fd_ostream>(
      61         322 :       OutputFileName + "resolution.txt", EC, sys::fs::OpenFlags::F_Text);
      62         161 :   if (EC)
      63           0 :     return errorCodeToError(EC);
      64             : 
      65         966 :   auto setHook = [&](std::string PathSuffix, ModuleHookFn &Hook) {
      66             :     // Keep track of the hook provided by the linker, which also needs to run.
      67             :     ModuleHookFn LinkerHook = Hook;
      68       13790 :     Hook = [=](unsigned Task, const Module &M) {
      69             :       // If the linker's hook returned false, we need to pass that result
      70             :       // through.
      71        1244 :       if (LinkerHook && !LinkerHook(Task, M))
      72             :         return false;
      73             : 
      74             :       std::string PathPrefix;
      75             :       // If this is the combined module (not a ThinLTO backend compile) or the
      76             :       // user hasn't requested using the input module's path, emit to a file
      77             :       // named from the provided OutputFileName with the Task ID appended.
      78        2210 :       if (M.getModuleIdentifier() == "ld-temp.o" || !UseInputModulePath) {
      79        1958 :         PathPrefix = OutputFileName;
      80         992 :         if (Task != (unsigned)-1)
      81        2922 :           PathPrefix += utostr(Task) + ".";
      82             :       } else
      83         504 :         PathPrefix = M.getModuleIdentifier() + ".";
      84        2488 :       std::string Path = PathPrefix + PathSuffix + ".bc";
      85             :       std::error_code EC;
      86        2488 :       raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None);
      87             :       // Because -save-temps is a debugging feature, we report the error
      88             :       // directly and exit.
      89        1244 :       if (EC)
      90           0 :         reportOpenError(Path, EC.message());
      91        1244 :       WriteBitcodeToFile(M, OS, /*ShouldPreserveUseListOrder=*/false);
      92             :       return true;
      93        1932 :     };
      94        1127 :   };
      95             : 
      96         322 :   setHook("0.preopt", PreOptModuleHook);
      97         322 :   setHook("1.promote", PostPromoteModuleHook);
      98         322 :   setHook("2.internalize", PostInternalizeModuleHook);
      99         322 :   setHook("3.import", PostImportModuleHook);
     100         322 :   setHook("4.opt", PostOptModuleHook);
     101         322 :   setHook("5.precodegen", PreCodeGenModuleHook);
     102             : 
     103         382 :   CombinedIndexHook = [=](const ModuleSummaryIndex &Index) {
     104          61 :     std::string Path = OutputFileName + "index.bc";
     105             :     std::error_code EC;
     106         122 :     raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None);
     107             :     // Because -save-temps is a debugging feature, we report the error
     108             :     // directly and exit.
     109          61 :     if (EC)
     110           0 :       reportOpenError(Path, EC.message());
     111          61 :     WriteIndexToFile(Index, OS);
     112             : 
     113         122 :     Path = OutputFileName + "index.dot";
     114         122 :     raw_fd_ostream OSDot(Path, EC, sys::fs::OpenFlags::F_None);
     115          61 :     if (EC)
     116           0 :       reportOpenError(Path, EC.message());
     117          61 :     Index.exportToDot(OSDot);
     118          61 :     return true;
     119         322 :   };
     120             : 
     121             :   return Error::success();
     122             : }
     123             : 
     124             : namespace {
     125             : 
     126             : std::unique_ptr<TargetMachine>
     127         660 : createTargetMachine(Config &Conf, const Target *TheTarget, Module &M) {
     128             :   StringRef TheTriple = M.getTargetTriple();
     129         660 :   SubtargetFeatures Features;
     130        1320 :   Features.getDefaultSubtargetFeatures(Triple(TheTriple));
     131         781 :   for (const std::string &A : Conf.MAttrs)
     132         121 :     Features.AddFeature(A);
     133             : 
     134             :   Reloc::Model RelocModel;
     135         660 :   if (Conf.RelocModel)
     136         650 :     RelocModel = *Conf.RelocModel;
     137             :   else
     138             :     RelocModel =
     139          10 :         M.getPICLevel() == PICLevel::NotPIC ? Reloc::Static : Reloc::PIC_;
     140             : 
     141             :   return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
     142        1320 :       TheTriple, Conf.CPU, Features.getString(), Conf.Options, RelocModel,
     143        3960 :       Conf.CodeModel, Conf.CGOptLevel));
     144             : }
     145             : 
     146          17 : static void runNewPMPasses(Config &Conf, Module &Mod, TargetMachine *TM,
     147             :                            unsigned OptLevel, bool IsThinLTO) {
     148             :   Optional<PGOOptions> PGOOpt;
     149          17 :   if (!Conf.SampleProfile.empty())
     150          10 :     PGOOpt = PGOOptions("", "", Conf.SampleProfile, false, true);
     151             : 
     152          51 :   PassBuilder PB(TM, PGOOpt);
     153             :   AAManager AA;
     154             : 
     155             :   // Parse a custom AA pipeline if asked to.
     156          17 :   if (!PB.parseAAPipeline(AA, "default"))
     157           0 :     report_fatal_error("Error parsing default AA pipeline");
     158             : 
     159          34 :   LoopAnalysisManager LAM(Conf.DebugPassManager);
     160          34 :   FunctionAnalysisManager FAM(Conf.DebugPassManager);
     161          34 :   CGSCCAnalysisManager CGAM(Conf.DebugPassManager);
     162          34 :   ModuleAnalysisManager MAM(Conf.DebugPassManager);
     163             : 
     164             :   // Register the AA manager first so that our version is the one used.
     165          34 :   FAM.registerPass([&] { return std::move(AA); });
     166             : 
     167             :   // Register all the basic analyses with the managers.
     168          17 :   PB.registerModuleAnalyses(MAM);
     169          17 :   PB.registerCGSCCAnalyses(CGAM);
     170          17 :   PB.registerFunctionAnalyses(FAM);
     171          17 :   PB.registerLoopAnalyses(LAM);
     172          17 :   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
     173             : 
     174          17 :   ModulePassManager MPM(Conf.DebugPassManager);
     175             :   // FIXME (davide): verify the input.
     176             : 
     177             :   PassBuilder::OptimizationLevel OL;
     178             : 
     179          17 :   switch (OptLevel) {
     180           0 :   default:
     181           0 :     llvm_unreachable("Invalid optimization level");
     182             :   case 0:
     183             :     OL = PassBuilder::O0;
     184             :     break;
     185           0 :   case 1:
     186             :     OL = PassBuilder::O1;
     187           0 :     break;
     188          17 :   case 2:
     189             :     OL = PassBuilder::O2;
     190          17 :     break;
     191           0 :   case 3:
     192             :     OL = PassBuilder::O3;
     193           0 :     break;
     194             :   }
     195             : 
     196          17 :   if (IsThinLTO)
     197          16 :     MPM = PB.buildThinLTODefaultPipeline(OL, Conf.DebugPassManager);
     198             :   else
     199          18 :     MPM = PB.buildLTODefaultPipeline(OL, Conf.DebugPassManager);
     200          17 :   MPM.run(Mod, MAM);
     201             : 
     202             :   // FIXME (davide): verify the output.
     203          17 : }
     204             : 
     205           9 : static void runNewPMCustomPasses(Module &Mod, TargetMachine *TM,
     206             :                                  std::string PipelineDesc,
     207             :                                  std::string AAPipelineDesc,
     208             :                                  bool DisableVerify) {
     209          23 :   PassBuilder PB(TM);
     210             :   AAManager AA;
     211             : 
     212             :   // Parse a custom AA pipeline if asked to.
     213           9 :   if (!AAPipelineDesc.empty())
     214           4 :     if (!PB.parseAAPipeline(AA, AAPipelineDesc))
     215           2 :       report_fatal_error("unable to parse AA pipeline description: " +
     216             :                          AAPipelineDesc);
     217             : 
     218          12 :   LoopAnalysisManager LAM;
     219          12 :   FunctionAnalysisManager FAM;
     220          12 :   CGSCCAnalysisManager CGAM;
     221          12 :   ModuleAnalysisManager MAM;
     222             : 
     223             :   // Register the AA manager first so that our version is the one used.
     224          14 :   FAM.registerPass([&] { return std::move(AA); });
     225             : 
     226             :   // Register all the basic analyses with the managers.
     227           7 :   PB.registerModuleAnalyses(MAM);
     228           7 :   PB.registerCGSCCAnalyses(CGAM);
     229           7 :   PB.registerFunctionAnalyses(FAM);
     230           7 :   PB.registerLoopAnalyses(LAM);
     231           7 :   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
     232             : 
     233             :   ModulePassManager MPM;
     234             : 
     235             :   // Always verify the input.
     236           7 :   MPM.addPass(VerifierPass());
     237             : 
     238             :   // Now, add all the passes we've been requested to.
     239           7 :   if (!PB.parsePassPipeline(MPM, PipelineDesc))
     240           2 :     report_fatal_error("unable to parse pass pipeline description: " +
     241             :                        PipelineDesc);
     242             : 
     243           5 :   if (!DisableVerify)
     244           5 :     MPM.addPass(VerifierPass());
     245           5 :   MPM.run(Mod, MAM);
     246           5 : }
     247             : 
     248         624 : static void runOldPMPasses(Config &Conf, Module &Mod, TargetMachine *TM,
     249             :                            bool IsThinLTO, ModuleSummaryIndex *ExportSummary,
     250             :                            const ModuleSummaryIndex *ImportSummary) {
     251        1248 :   legacy::PassManager passes;
     252        1248 :   passes.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
     253             : 
     254        1248 :   PassManagerBuilder PMB;
     255        1248 :   PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM->getTargetTriple()));
     256         624 :   PMB.Inliner = createFunctionInliningPass();
     257         624 :   PMB.ExportSummary = ExportSummary;
     258         624 :   PMB.ImportSummary = ImportSummary;
     259             :   // Unconditionally verify input since it is not verified before this
     260             :   // point and has unknown origin.
     261         624 :   PMB.VerifyInput = true;
     262         624 :   PMB.VerifyOutput = !Conf.DisableVerify;
     263         624 :   PMB.LoopVectorize = true;
     264         624 :   PMB.SLPVectorize = true;
     265         624 :   PMB.OptLevel = Conf.OptLevel;
     266         624 :   PMB.PGOSampleUse = Conf.SampleProfile;
     267         624 :   if (IsThinLTO)
     268         227 :     PMB.populateThinLTOPassManager(passes);
     269             :   else
     270         397 :     PMB.populateLTOPassManager(passes);
     271         624 :   passes.run(Mod);
     272         624 : }
     273             : 
     274         650 : bool opt(Config &Conf, TargetMachine *TM, unsigned Task, Module &Mod,
     275             :          bool IsThinLTO, ModuleSummaryIndex *ExportSummary,
     276             :          const ModuleSummaryIndex *ImportSummary) {
     277             :   // FIXME: Plumb the combined index into the new pass manager.
     278         650 :   if (!Conf.OptPipeline.empty())
     279          28 :     runNewPMCustomPasses(Mod, TM, Conf.OptPipeline, Conf.AAPipeline,
     280           9 :                          Conf.DisableVerify);
     281         641 :   else if (Conf.UseNewPM)
     282          17 :     runNewPMPasses(Conf, Mod, TM, Conf.OptLevel, IsThinLTO);
     283             :   else
     284         624 :     runOldPMPasses(Conf, Mod, TM, IsThinLTO, ExportSummary, ImportSummary);
     285         905 :   return !Conf.PostOptModuleHook || Conf.PostOptModuleHook(Task, Mod);
     286             : }
     287             : 
     288         651 : void codegen(Config &Conf, TargetMachine *TM, AddStreamFn AddStream,
     289             :              unsigned Task, Module &Mod) {
     290         919 :   if (Conf.PreCodeGenModuleHook && !Conf.PreCodeGenModuleHook(Task, Mod))
     291           5 :     return;
     292             : 
     293             :   std::unique_ptr<ToolOutputFile> DwoOut;
     294             :   SmallString<1024> DwoFile(Conf.DwoPath);
     295         646 :   if (!Conf.DwoDir.empty()) {
     296             :     std::error_code EC;
     297           6 :     if (auto EC = llvm::sys::fs::create_directories(Conf.DwoDir))
     298           0 :       report_fatal_error("Failed to create directory " + Conf.DwoDir + ": " +
     299             :                          EC.message());
     300             : 
     301             :     DwoFile = Conf.DwoDir;
     302          12 :     sys::path::append(DwoFile, std::to_string(Task) + ".dwo");
     303             :   }
     304             : 
     305         646 :   if (!DwoFile.empty()) {
     306             :     std::error_code EC;
     307           8 :     TM->Options.MCOptions.SplitDwarfFile = DwoFile.str().str();
     308           8 :     DwoOut = llvm::make_unique<ToolOutputFile>(DwoFile, EC, sys::fs::F_None);
     309           4 :     if (EC)
     310           0 :       report_fatal_error("Failed to open " + DwoFile + ": " + EC.message());
     311             :   }
     312             : 
     313             :   auto Stream = AddStream(Task);
     314        1290 :   legacy::PassManager CodeGenPasses;
     315        1290 :   if (TM->addPassesToEmitFile(CodeGenPasses, *Stream->OS,
     316             :                               DwoOut ? &DwoOut->os() : nullptr,
     317         645 :                               Conf.CGFileType))
     318           0 :     report_fatal_error("Failed to setup codegen");
     319         645 :   CodeGenPasses.run(Mod);
     320             : 
     321         645 :   if (DwoOut)
     322             :     DwoOut->keep();
     323             : }
     324             : 
     325           5 : void splitCodeGen(Config &C, TargetMachine *TM, AddStreamFn AddStream,
     326             :                   unsigned ParallelCodeGenParallelismLevel,
     327             :                   std::unique_ptr<Module> Mod) {
     328          10 :   ThreadPool CodegenThreadPool(ParallelCodeGenParallelismLevel);
     329           5 :   unsigned ThreadCount = 0;
     330           5 :   const Target *T = &TM->getTarget();
     331             : 
     332          10 :   SplitModule(
     333             :       std::move(Mod), ParallelCodeGenParallelismLevel,
     334          10 :       [&](std::unique_ptr<Module> MPart) {
     335             :         // We want to clone the module in a new context to multi-thread the
     336             :         // codegen. We do it by serializing partition modules to bitcode
     337             :         // (while still on the main thread, in order to avoid data races) and
     338             :         // spinning up new threads which deserialize the partitions into
     339             :         // separate contexts.
     340             :         // FIXME: Provide a more direct way to do this in LLVM.
     341             :         SmallString<0> BC;
     342             :         raw_svector_ostream BCOS(BC);
     343          10 :         WriteBitcodeToFile(*MPart, BCOS);
     344             : 
     345             :         // Enqueue the task
     346          50 :         CodegenThreadPool.async(
     347          10 :             [&](const SmallString<0> &BC, unsigned ThreadId) {
     348          50 :               LTOLLVMContext Ctx(C);
     349             :               Expected<std::unique_ptr<Module>> MOrErr = parseBitcodeFile(
     350             :                   MemoryBufferRef(StringRef(BC.data(), BC.size()), "ld-temp.o"),
     351          20 :                   Ctx);
     352          10 :               if (!MOrErr)
     353           0 :                 report_fatal_error("Failed to read bitcode");
     354          10 :               std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get());
     355             : 
     356             :               std::unique_ptr<TargetMachine> TM =
     357          30 :                   createTargetMachine(C, T, *MPartInCtx);
     358             : 
     359          40 :               codegen(C, TM.get(), AddStream, ThreadId, *MPartInCtx);
     360          10 :             },
     361             :             // Pass BC using std::move to ensure that it get moved rather than
     362             :             // copied into the thread's context.
     363          10 :             std::move(BC), ThreadCount++);
     364          10 :       },
     365             :       false);
     366             : 
     367             :   // Because the inner lambda (which runs in a worker thread) captures our local
     368             :   // variables, we need to wait for the worker threads to terminate before we
     369             :   // can leave the function scope.
     370           5 :   CodegenThreadPool.wait();
     371           5 : }
     372             : 
     373         650 : Expected<const Target *> initAndLookupTarget(Config &C, Module &Mod) {
     374         650 :   if (!C.OverrideTriple.empty())
     375           2 :     Mod.setTargetTriple(C.OverrideTriple);
     376         648 :   else if (Mod.getTargetTriple().empty())
     377           2 :     Mod.setTargetTriple(C.DefaultTriple);
     378             : 
     379             :   std::string Msg;
     380         650 :   const Target *T = TargetRegistry::lookupTarget(Mod.getTargetTriple(), Msg);
     381         650 :   if (!T)
     382           0 :     return make_error<StringError>(Msg, inconvertibleErrorCode());
     383             :   return T;
     384             : }
     385             : 
     386             : }
     387             : 
     388             : static Error
     389         645 : finalizeOptimizationRemarks(std::unique_ptr<ToolOutputFile> DiagOutputFile) {
     390             :   // Make sure we flush the diagnostic remarks file in case the linker doesn't
     391             :   // call the global destructors before exiting.
     392         645 :   if (!DiagOutputFile)
     393             :     return Error::success();
     394             :   DiagOutputFile->keep();
     395           9 :   DiagOutputFile->os().flush();
     396             :   return Error::success();
     397             : }
     398             : 
     399         414 : Error lto::backend(Config &C, AddStreamFn AddStream,
     400             :                    unsigned ParallelCodeGenParallelismLevel,
     401             :                    std::unique_ptr<Module> Mod,
     402             :                    ModuleSummaryIndex &CombinedIndex) {
     403         414 :   Expected<const Target *> TOrErr = initAndLookupTarget(C, *Mod);
     404         414 :   if (!TOrErr)
     405             :     return TOrErr.takeError();
     406             : 
     407         414 :   std::unique_ptr<TargetMachine> TM = createTargetMachine(C, *TOrErr, *Mod);
     408             : 
     409             :   // Setup optimization remarks.
     410             :   auto DiagFileOrErr = lto::setupOptimizationRemarks(
     411        1237 :       Mod->getContext(), C.RemarksFilename, C.RemarksWithHotness);
     412         414 :   if (!DiagFileOrErr)
     413             :     return DiagFileOrErr.takeError();
     414             :   auto DiagnosticOutputFile = std::move(*DiagFileOrErr);
     415             : 
     416         414 :   if (!C.CodeGenOnly) {
     417         414 :     if (!opt(C, TM.get(), 0, *Mod, /*IsThinLTO=*/false,
     418             :              /*ExportSummary=*/&CombinedIndex, /*ImportSummary=*/nullptr))
     419           0 :       return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
     420             :   }
     421             : 
     422         410 :   if (ParallelCodeGenParallelismLevel == 1) {
     423         809 :     codegen(C, TM.get(), AddStream, 0, *Mod);
     424             :   } else {
     425          10 :     splitCodeGen(C, TM.get(), AddStream, ParallelCodeGenParallelismLevel,
     426             :                  std::move(Mod));
     427             :   }
     428         818 :   return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
     429             : }
     430             : 
     431         236 : static void dropDeadSymbols(Module &Mod, const GVSummaryMapTy &DefinedGlobals,
     432             :                             const ModuleSummaryIndex &Index) {
     433             :   std::vector<GlobalValue*> DeadGVs;
     434         775 :   for (auto &GV : Mod.global_values())
     435        1078 :     if (GlobalValueSummary *GVS = DefinedGlobals.lookup(GV.getGUID()))
     436         358 :       if (!Index.isGlobalValueLive(GVS)) {
     437          72 :         DeadGVs.push_back(&GV);
     438          36 :         convertToDeclaration(GV);
     439             :       }
     440             : 
     441             :   // Now that all dead bodies have been dropped, delete the actual objects
     442             :   // themselves when possible.
     443         272 :   for (GlobalValue *GV : DeadGVs) {
     444          36 :     GV->removeDeadConstantUsers();
     445             :     // Might reference something defined in native object (i.e. dropped a
     446             :     // non-prevailing IR def, but we need to keep the declaration).
     447          36 :     if (GV->use_empty())
     448          31 :       GV->eraseFromParent();
     449             :   }
     450         236 : }
     451             : 
     452         236 : Error lto::thinBackend(Config &Conf, unsigned Task, AddStreamFn AddStream,
     453             :                        Module &Mod, const ModuleSummaryIndex &CombinedIndex,
     454             :                        const FunctionImporter::ImportMapTy &ImportList,
     455             :                        const GVSummaryMapTy &DefinedGlobals,
     456             :                        MapVector<StringRef, BitcodeModule> &ModuleMap) {
     457         236 :   Expected<const Target *> TOrErr = initAndLookupTarget(Conf, Mod);
     458         236 :   if (!TOrErr)
     459             :     return TOrErr.takeError();
     460             : 
     461         236 :   std::unique_ptr<TargetMachine> TM = createTargetMachine(Conf, *TOrErr, Mod);
     462             : 
     463             :   // Setup optimization remarks.
     464             :   auto DiagFileOrErr = lto::setupOptimizationRemarks(
     465         708 :       Mod.getContext(), Conf.RemarksFilename, Conf.RemarksWithHotness, Task);
     466         236 :   if (!DiagFileOrErr)
     467             :     return DiagFileOrErr.takeError();
     468             :   auto DiagnosticOutputFile = std::move(*DiagFileOrErr);
     469             : 
     470         236 :   if (Conf.CodeGenOnly) {
     471           0 :     codegen(Conf, TM.get(), AddStream, Task, Mod);
     472           0 :     return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
     473             :   }
     474             : 
     475         338 :   if (Conf.PreOptModuleHook && !Conf.PreOptModuleHook(Task, Mod))
     476           0 :     return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
     477             : 
     478         236 :   renameModuleForThinLTO(Mod, CombinedIndex);
     479             : 
     480         236 :   dropDeadSymbols(Mod, DefinedGlobals, CombinedIndex);
     481             : 
     482         236 :   thinLTOResolveWeakForLinkerModule(Mod, DefinedGlobals);
     483             : 
     484         338 :   if (Conf.PostPromoteModuleHook && !Conf.PostPromoteModuleHook(Task, Mod))
     485           0 :     return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
     486             : 
     487         236 :   if (!DefinedGlobals.empty())
     488         226 :     thinLTOInternalizeModule(Mod, DefinedGlobals);
     489             : 
     490         338 :   if (Conf.PostInternalizeModuleHook &&
     491             :       !Conf.PostInternalizeModuleHook(Task, Mod))
     492           0 :     return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
     493             : 
     494          72 :   auto ModuleLoader = [&](StringRef Identifier) {
     495             :     assert(Mod.getContext().isODRUniquingDebugTypes() &&
     496             :            "ODR Type uniquing should be enabled on the context");
     497          72 :     auto I = ModuleMap.find(Identifier);
     498             :     assert(I != ModuleMap.end());
     499          72 :     return I->second.getLazyModule(Mod.getContext(),
     500             :                                    /*ShouldLazyLoadMetadata=*/true,
     501         144 :                                    /*IsImporting*/ true);
     502             :   };
     503             : 
     504         236 :   FunctionImporter Importer(CombinedIndex, ModuleLoader);
     505         708 :   if (Error Err = Importer.importFunctions(Mod, ImportList).takeError())
     506             :     return Err;
     507             : 
     508         338 :   if (Conf.PostImportModuleHook && !Conf.PostImportModuleHook(Task, Mod))
     509           0 :     return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
     510             : 
     511         236 :   if (!opt(Conf, TM.get(), Task, Mod, /*IsThinLTO=*/true,
     512             :            /*ExportSummary=*/nullptr, /*ImportSummary=*/&CombinedIndex))
     513           0 :     return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
     514             : 
     515         472 :   codegen(Conf, TM.get(), AddStream, Task, Mod);
     516         472 :   return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
     517             : }

Generated by: LCOV version 1.13