1 //===--- BackendUtil.cpp - LLVM Backend Utilities -------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "clang/CodeGen/BackendUtil.h"
10 #include "clang/Basic/CodeGenOptions.h"
11 #include "clang/Basic/Diagnostic.h"
12 #include "clang/Basic/LangOptions.h"
13 #include "clang/Basic/TargetOptions.h"
14 #include "clang/Frontend/FrontendDiagnostic.h"
15 #include "clang/Frontend/Utils.h"
16 #include "clang/Lex/HeaderSearchOptions.h"
17 #include "llvm/ADT/SmallSet.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/ADT/StringSwitch.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/Analysis/StackSafetyAnalysis.h"
22 #include "llvm/Analysis/TargetLibraryInfo.h"
23 #include "llvm/Analysis/TargetTransformInfo.h"
24 #include "llvm/Bitcode/BitcodeReader.h"
25 #include "llvm/Bitcode/BitcodeWriter.h"
26 #include "llvm/Bitcode/BitcodeWriterPass.h"
27 #include "llvm/CodeGen/RegAllocRegistry.h"
28 #include "llvm/CodeGen/SchedulerRegistry.h"
29 #include "llvm/CodeGen/TargetSubtargetInfo.h"
30 #include "llvm/IR/DataLayout.h"
31 #include "llvm/IR/IRPrintingPasses.h"
32 #include "llvm/IR/LegacyPassManager.h"
33 #include "llvm/IR/Module.h"
34 #include "llvm/IR/ModuleSummaryIndex.h"
35 #include "llvm/IR/Verifier.h"
36 #include "llvm/LTO/LTOBackend.h"
37 #include "llvm/MC/MCAsmInfo.h"
38 #include "llvm/MC/SubtargetFeature.h"
39 #include "llvm/Passes/PassBuilder.h"
40 #include "llvm/Passes/PassPlugin.h"
41 #include "llvm/Passes/StandardInstrumentations.h"
42 #include "llvm/Support/BuryPointer.h"
43 #include "llvm/Support/CommandLine.h"
44 #include "llvm/Support/MemoryBuffer.h"
45 #include "llvm/Support/PrettyStackTrace.h"
46 #include "llvm/Support/TargetRegistry.h"
47 #include "llvm/Support/TimeProfiler.h"
48 #include "llvm/Support/Timer.h"
49 #include "llvm/Support/raw_ostream.h"
50 #include "llvm/Target/TargetMachine.h"
51 #include "llvm/Target/TargetOptions.h"
52 #include "llvm/Transforms/Coroutines.h"
53 #include "llvm/Transforms/Coroutines/CoroCleanup.h"
54 #include "llvm/Transforms/Coroutines/CoroEarly.h"
55 #include "llvm/Transforms/Coroutines/CoroElide.h"
56 #include "llvm/Transforms/Coroutines/CoroSplit.h"
57 #include "llvm/Transforms/IPO.h"
58 #include "llvm/Transforms/IPO/AlwaysInliner.h"
59 #include "llvm/Transforms/IPO/LowerTypeTests.h"
60 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
61 #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
62 #include "llvm/Transforms/InstCombine/InstCombine.h"
63 #include "llvm/Transforms/Instrumentation.h"
64 #include "llvm/Transforms/Instrumentation/AddressSanitizer.h"
65 #include "llvm/Transforms/Instrumentation/BoundsChecking.h"
66 #include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
67 #include "llvm/Transforms/Instrumentation/HWAddressSanitizer.h"
68 #include "llvm/Transforms/Instrumentation/InstrProfiling.h"
69 #include "llvm/Transforms/Instrumentation/MemorySanitizer.h"
70 #include "llvm/Transforms/Instrumentation/SanitizerCoverage.h"
71 #include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
72 #include "llvm/Transforms/ObjCARC.h"
73 #include "llvm/Transforms/Scalar.h"
74 #include "llvm/Transforms/Scalar/GVN.h"
75 #include "llvm/Transforms/Utils.h"
76 #include "llvm/Transforms/Utils/CanonicalizeAliases.h"
77 #include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
78 #include "llvm/Transforms/Utils/NameAnonGlobals.h"
79 #include "llvm/Transforms/Utils/SymbolRewriter.h"
80 #include "llvm/Transforms/Utils/UniqueInternalLinkageNames.h"
81 #include <memory>
82 using namespace clang;
83 using namespace llvm;
84 
85 #define HANDLE_EXTENSION(Ext)                                                  \
86   llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
87 #include "llvm/Support/Extension.def"
88 
89 namespace {
90 
91 // Default filename used for profile generation.
92 static constexpr StringLiteral DefaultProfileGenName = "default_%m.profraw";
93 
94 class EmitAssemblyHelper {
95   DiagnosticsEngine &Diags;
96   const HeaderSearchOptions &HSOpts;
97   const CodeGenOptions &CodeGenOpts;
98   const clang::TargetOptions &TargetOpts;
99   const LangOptions &LangOpts;
100   Module *TheModule;
101 
102   Timer CodeGenerationTime;
103 
104   std::unique_ptr<raw_pwrite_stream> OS;
105 
106   TargetIRAnalysis getTargetIRAnalysis() const {
107     if (TM)
108       return TM->getTargetIRAnalysis();
109 
110     return TargetIRAnalysis();
111   }
112 
113   void CreatePasses(legacy::PassManager &MPM, legacy::FunctionPassManager &FPM);
114 
115   /// Generates the TargetMachine.
116   /// Leaves TM unchanged if it is unable to create the target machine.
117   /// Some of our clang tests specify triples which are not built
118   /// into clang. This is okay because these tests check the generated
119   /// IR, and they require DataLayout which depends on the triple.
120   /// In this case, we allow this method to fail and not report an error.
121   /// When MustCreateTM is used, we print an error if we are unable to load
122   /// the requested target.
123   void CreateTargetMachine(bool MustCreateTM);
124 
125   /// Add passes necessary to emit assembly or LLVM IR.
126   ///
127   /// \return True on success.
128   bool AddEmitPasses(legacy::PassManager &CodeGenPasses, BackendAction Action,
129                      raw_pwrite_stream &OS, raw_pwrite_stream *DwoOS);
130 
131   std::unique_ptr<llvm::ToolOutputFile> openOutputFile(StringRef Path) {
132     std::error_code EC;
133     auto F = std::make_unique<llvm::ToolOutputFile>(Path, EC,
134                                                      llvm::sys::fs::OF_None);
135     if (EC) {
136       Diags.Report(diag::err_fe_unable_to_open_output) << Path << EC.message();
137       F.reset();
138     }
139     return F;
140   }
141 
142 public:
143   EmitAssemblyHelper(DiagnosticsEngine &_Diags,
144                      const HeaderSearchOptions &HeaderSearchOpts,
145                      const CodeGenOptions &CGOpts,
146                      const clang::TargetOptions &TOpts,
147                      const LangOptions &LOpts, Module *M)
148       : Diags(_Diags), HSOpts(HeaderSearchOpts), CodeGenOpts(CGOpts),
149         TargetOpts(TOpts), LangOpts(LOpts), TheModule(M),
150         CodeGenerationTime("codegen", "Code Generation Time") {}
151 
152   ~EmitAssemblyHelper() {
153     if (CodeGenOpts.DisableFree)
154       BuryPointer(std::move(TM));
155   }
156 
157   std::unique_ptr<TargetMachine> TM;
158 
159   void EmitAssembly(BackendAction Action,
160                     std::unique_ptr<raw_pwrite_stream> OS);
161 
162   void EmitAssemblyWithNewPassManager(BackendAction Action,
163                                       std::unique_ptr<raw_pwrite_stream> OS);
164 };
165 
166 // We need this wrapper to access LangOpts and CGOpts from extension functions
167 // that we add to the PassManagerBuilder.
168 class PassManagerBuilderWrapper : public PassManagerBuilder {
169 public:
170   PassManagerBuilderWrapper(const Triple &TargetTriple,
171                             const CodeGenOptions &CGOpts,
172                             const LangOptions &LangOpts)
173       : PassManagerBuilder(), TargetTriple(TargetTriple), CGOpts(CGOpts),
174         LangOpts(LangOpts) {}
175   const Triple &getTargetTriple() const { return TargetTriple; }
176   const CodeGenOptions &getCGOpts() const { return CGOpts; }
177   const LangOptions &getLangOpts() const { return LangOpts; }
178 
179 private:
180   const Triple &TargetTriple;
181   const CodeGenOptions &CGOpts;
182   const LangOptions &LangOpts;
183 };
184 }
185 
186 static void addObjCARCAPElimPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
187   if (Builder.OptLevel > 0)
188     PM.add(createObjCARCAPElimPass());
189 }
190 
191 static void addObjCARCExpandPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
192   if (Builder.OptLevel > 0)
193     PM.add(createObjCARCExpandPass());
194 }
195 
196 static void addObjCARCOptPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
197   if (Builder.OptLevel > 0)
198     PM.add(createObjCARCOptPass());
199 }
200 
201 static void addAddDiscriminatorsPass(const PassManagerBuilder &Builder,
202                                      legacy::PassManagerBase &PM) {
203   PM.add(createAddDiscriminatorsPass());
204 }
205 
206 static void addBoundsCheckingPass(const PassManagerBuilder &Builder,
207                                   legacy::PassManagerBase &PM) {
208   PM.add(createBoundsCheckingLegacyPass());
209 }
210 
211 static SanitizerCoverageOptions
212 getSancovOptsFromCGOpts(const CodeGenOptions &CGOpts) {
213   SanitizerCoverageOptions Opts;
214   Opts.CoverageType =
215       static_cast<SanitizerCoverageOptions::Type>(CGOpts.SanitizeCoverageType);
216   Opts.IndirectCalls = CGOpts.SanitizeCoverageIndirectCalls;
217   Opts.TraceBB = CGOpts.SanitizeCoverageTraceBB;
218   Opts.TraceCmp = CGOpts.SanitizeCoverageTraceCmp;
219   Opts.TraceDiv = CGOpts.SanitizeCoverageTraceDiv;
220   Opts.TraceGep = CGOpts.SanitizeCoverageTraceGep;
221   Opts.Use8bitCounters = CGOpts.SanitizeCoverage8bitCounters;
222   Opts.TracePC = CGOpts.SanitizeCoverageTracePC;
223   Opts.TracePCGuard = CGOpts.SanitizeCoverageTracePCGuard;
224   Opts.NoPrune = CGOpts.SanitizeCoverageNoPrune;
225   Opts.Inline8bitCounters = CGOpts.SanitizeCoverageInline8bitCounters;
226   Opts.InlineBoolFlag = CGOpts.SanitizeCoverageInlineBoolFlag;
227   Opts.PCTable = CGOpts.SanitizeCoveragePCTable;
228   Opts.StackDepth = CGOpts.SanitizeCoverageStackDepth;
229   return Opts;
230 }
231 
232 static void addSanitizerCoveragePass(const PassManagerBuilder &Builder,
233                                      legacy::PassManagerBase &PM) {
234   const PassManagerBuilderWrapper &BuilderWrapper =
235       static_cast<const PassManagerBuilderWrapper &>(Builder);
236   const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
237   auto Opts = getSancovOptsFromCGOpts(CGOpts);
238   PM.add(createModuleSanitizerCoverageLegacyPassPass(
239       Opts, CGOpts.SanitizeCoverageWhitelistFiles,
240       CGOpts.SanitizeCoverageBlacklistFiles));
241 }
242 
243 // Check if ASan should use GC-friendly instrumentation for globals.
244 // First of all, there is no point if -fdata-sections is off (expect for MachO,
245 // where this is not a factor). Also, on ELF this feature requires an assembler
246 // extension that only works with -integrated-as at the moment.
247 static bool asanUseGlobalsGC(const Triple &T, const CodeGenOptions &CGOpts) {
248   if (!CGOpts.SanitizeAddressGlobalsDeadStripping)
249     return false;
250   switch (T.getObjectFormat()) {
251   case Triple::MachO:
252   case Triple::COFF:
253     return true;
254   case Triple::ELF:
255     return CGOpts.DataSections && !CGOpts.DisableIntegratedAS;
256   case Triple::XCOFF:
257     llvm::report_fatal_error("ASan not implemented for XCOFF.");
258   case Triple::Wasm:
259   case Triple::UnknownObjectFormat:
260     break;
261   }
262   return false;
263 }
264 
265 static void addAddressSanitizerPasses(const PassManagerBuilder &Builder,
266                                       legacy::PassManagerBase &PM) {
267   const PassManagerBuilderWrapper &BuilderWrapper =
268       static_cast<const PassManagerBuilderWrapper&>(Builder);
269   const Triple &T = BuilderWrapper.getTargetTriple();
270   const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
271   bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Address);
272   bool UseAfterScope = CGOpts.SanitizeAddressUseAfterScope;
273   bool UseOdrIndicator = CGOpts.SanitizeAddressUseOdrIndicator;
274   bool UseGlobalsGC = asanUseGlobalsGC(T, CGOpts);
275   PM.add(createAddressSanitizerFunctionPass(/*CompileKernel*/ false, Recover,
276                                             UseAfterScope));
277   PM.add(createModuleAddressSanitizerLegacyPassPass(
278       /*CompileKernel*/ false, Recover, UseGlobalsGC, UseOdrIndicator));
279 }
280 
281 static void addKernelAddressSanitizerPasses(const PassManagerBuilder &Builder,
282                                             legacy::PassManagerBase &PM) {
283   PM.add(createAddressSanitizerFunctionPass(
284       /*CompileKernel*/ true, /*Recover*/ true, /*UseAfterScope*/ false));
285   PM.add(createModuleAddressSanitizerLegacyPassPass(
286       /*CompileKernel*/ true, /*Recover*/ true, /*UseGlobalsGC*/ true,
287       /*UseOdrIndicator*/ false));
288 }
289 
290 static void addHWAddressSanitizerPasses(const PassManagerBuilder &Builder,
291                                             legacy::PassManagerBase &PM) {
292   const PassManagerBuilderWrapper &BuilderWrapper =
293       static_cast<const PassManagerBuilderWrapper &>(Builder);
294   const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
295   bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::HWAddress);
296   PM.add(
297       createHWAddressSanitizerLegacyPassPass(/*CompileKernel*/ false, Recover));
298 }
299 
300 static void addKernelHWAddressSanitizerPasses(const PassManagerBuilder &Builder,
301                                             legacy::PassManagerBase &PM) {
302   PM.add(createHWAddressSanitizerLegacyPassPass(
303       /*CompileKernel*/ true, /*Recover*/ true));
304 }
305 
306 static void addGeneralOptsForMemorySanitizer(const PassManagerBuilder &Builder,
307                                              legacy::PassManagerBase &PM,
308                                              bool CompileKernel) {
309   const PassManagerBuilderWrapper &BuilderWrapper =
310       static_cast<const PassManagerBuilderWrapper&>(Builder);
311   const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
312   int TrackOrigins = CGOpts.SanitizeMemoryTrackOrigins;
313   bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Memory);
314   PM.add(createMemorySanitizerLegacyPassPass(
315       MemorySanitizerOptions{TrackOrigins, Recover, CompileKernel}));
316 
317   // MemorySanitizer inserts complex instrumentation that mostly follows
318   // the logic of the original code, but operates on "shadow" values.
319   // It can benefit from re-running some general purpose optimization passes.
320   if (Builder.OptLevel > 0) {
321     PM.add(createEarlyCSEPass());
322     PM.add(createReassociatePass());
323     PM.add(createLICMPass());
324     PM.add(createGVNPass());
325     PM.add(createInstructionCombiningPass());
326     PM.add(createDeadStoreEliminationPass());
327   }
328 }
329 
330 static void addMemorySanitizerPass(const PassManagerBuilder &Builder,
331                                    legacy::PassManagerBase &PM) {
332   addGeneralOptsForMemorySanitizer(Builder, PM, /*CompileKernel*/ false);
333 }
334 
335 static void addKernelMemorySanitizerPass(const PassManagerBuilder &Builder,
336                                          legacy::PassManagerBase &PM) {
337   addGeneralOptsForMemorySanitizer(Builder, PM, /*CompileKernel*/ true);
338 }
339 
340 static void addThreadSanitizerPass(const PassManagerBuilder &Builder,
341                                    legacy::PassManagerBase &PM) {
342   PM.add(createThreadSanitizerLegacyPassPass());
343 }
344 
345 static void addDataFlowSanitizerPass(const PassManagerBuilder &Builder,
346                                      legacy::PassManagerBase &PM) {
347   const PassManagerBuilderWrapper &BuilderWrapper =
348       static_cast<const PassManagerBuilderWrapper&>(Builder);
349   const LangOptions &LangOpts = BuilderWrapper.getLangOpts();
350   PM.add(createDataFlowSanitizerPass(LangOpts.SanitizerBlacklistFiles));
351 }
352 
353 static void addMemTagOptimizationPasses(const PassManagerBuilder &Builder,
354                                         legacy::PassManagerBase &PM) {
355   PM.add(createStackSafetyGlobalInfoWrapperPass(/*SetMetadata=*/true));
356 }
357 
358 static TargetLibraryInfoImpl *createTLII(llvm::Triple &TargetTriple,
359                                          const CodeGenOptions &CodeGenOpts) {
360   TargetLibraryInfoImpl *TLII = new TargetLibraryInfoImpl(TargetTriple);
361 
362   switch (CodeGenOpts.getVecLib()) {
363   case CodeGenOptions::Accelerate:
364     TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::Accelerate);
365     break;
366   case CodeGenOptions::MASSV:
367     TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::MASSV);
368     break;
369   case CodeGenOptions::SVML:
370     TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::SVML);
371     break;
372   default:
373     break;
374   }
375   return TLII;
376 }
377 
378 static void addSymbolRewriterPass(const CodeGenOptions &Opts,
379                                   legacy::PassManager *MPM) {
380   llvm::SymbolRewriter::RewriteDescriptorList DL;
381 
382   llvm::SymbolRewriter::RewriteMapParser MapParser;
383   for (const auto &MapFile : Opts.RewriteMapFiles)
384     MapParser.parse(MapFile, &DL);
385 
386   MPM->add(createRewriteSymbolsPass(DL));
387 }
388 
389 static CodeGenOpt::Level getCGOptLevel(const CodeGenOptions &CodeGenOpts) {
390   switch (CodeGenOpts.OptimizationLevel) {
391   default:
392     llvm_unreachable("Invalid optimization level!");
393   case 0:
394     return CodeGenOpt::None;
395   case 1:
396     return CodeGenOpt::Less;
397   case 2:
398     return CodeGenOpt::Default; // O2/Os/Oz
399   case 3:
400     return CodeGenOpt::Aggressive;
401   }
402 }
403 
404 static Optional<llvm::CodeModel::Model>
405 getCodeModel(const CodeGenOptions &CodeGenOpts) {
406   unsigned CodeModel = llvm::StringSwitch<unsigned>(CodeGenOpts.CodeModel)
407                            .Case("tiny", llvm::CodeModel::Tiny)
408                            .Case("small", llvm::CodeModel::Small)
409                            .Case("kernel", llvm::CodeModel::Kernel)
410                            .Case("medium", llvm::CodeModel::Medium)
411                            .Case("large", llvm::CodeModel::Large)
412                            .Case("default", ~1u)
413                            .Default(~0u);
414   assert(CodeModel != ~0u && "invalid code model!");
415   if (CodeModel == ~1u)
416     return None;
417   return static_cast<llvm::CodeModel::Model>(CodeModel);
418 }
419 
420 static CodeGenFileType getCodeGenFileType(BackendAction Action) {
421   if (Action == Backend_EmitObj)
422     return CGFT_ObjectFile;
423   else if (Action == Backend_EmitMCNull)
424     return CGFT_Null;
425   else {
426     assert(Action == Backend_EmitAssembly && "Invalid action!");
427     return CGFT_AssemblyFile;
428   }
429 }
430 
431 static void initTargetOptions(llvm::TargetOptions &Options,
432                               const CodeGenOptions &CodeGenOpts,
433                               const clang::TargetOptions &TargetOpts,
434                               const LangOptions &LangOpts,
435                               const HeaderSearchOptions &HSOpts) {
436   Options.ThreadModel =
437       llvm::StringSwitch<llvm::ThreadModel::Model>(CodeGenOpts.ThreadModel)
438           .Case("posix", llvm::ThreadModel::POSIX)
439           .Case("single", llvm::ThreadModel::Single);
440 
441   // Set float ABI type.
442   assert((CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp" ||
443           CodeGenOpts.FloatABI == "hard" || CodeGenOpts.FloatABI.empty()) &&
444          "Invalid Floating Point ABI!");
445   Options.FloatABIType =
446       llvm::StringSwitch<llvm::FloatABI::ABIType>(CodeGenOpts.FloatABI)
447           .Case("soft", llvm::FloatABI::Soft)
448           .Case("softfp", llvm::FloatABI::Soft)
449           .Case("hard", llvm::FloatABI::Hard)
450           .Default(llvm::FloatABI::Default);
451 
452   // Set FP fusion mode.
453   switch (LangOpts.getDefaultFPContractMode()) {
454   case LangOptions::FPM_Off:
455     // Preserve any contraction performed by the front-end.  (Strict performs
456     // splitting of the muladd intrinsic in the backend.)
457     Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
458     break;
459   case LangOptions::FPM_On:
460     Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
461     break;
462   case LangOptions::FPM_Fast:
463     Options.AllowFPOpFusion = llvm::FPOpFusion::Fast;
464     break;
465   }
466 
467   Options.UseInitArray = CodeGenOpts.UseInitArray;
468   Options.DisableIntegratedAS = CodeGenOpts.DisableIntegratedAS;
469   Options.CompressDebugSections = CodeGenOpts.getCompressDebugSections();
470   Options.RelaxELFRelocations = CodeGenOpts.RelaxELFRelocations;
471 
472   // Set EABI version.
473   Options.EABIVersion = TargetOpts.EABIVersion;
474 
475   if (LangOpts.SjLjExceptions)
476     Options.ExceptionModel = llvm::ExceptionHandling::SjLj;
477   if (LangOpts.SEHExceptions)
478     Options.ExceptionModel = llvm::ExceptionHandling::WinEH;
479   if (LangOpts.DWARFExceptions)
480     Options.ExceptionModel = llvm::ExceptionHandling::DwarfCFI;
481   if (LangOpts.WasmExceptions)
482     Options.ExceptionModel = llvm::ExceptionHandling::Wasm;
483 
484   Options.NoInfsFPMath = CodeGenOpts.NoInfsFPMath;
485   Options.NoNaNsFPMath = CodeGenOpts.NoNaNsFPMath;
486   Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS;
487   Options.UnsafeFPMath = CodeGenOpts.UnsafeFPMath;
488   Options.StackAlignmentOverride = CodeGenOpts.StackAlignment;
489   Options.FunctionSections = CodeGenOpts.FunctionSections;
490   Options.DataSections = CodeGenOpts.DataSections;
491   Options.UniqueSectionNames = CodeGenOpts.UniqueSectionNames;
492   Options.TLSSize = CodeGenOpts.TLSSize;
493   Options.EmulatedTLS = CodeGenOpts.EmulatedTLS;
494   Options.ExplicitEmulatedTLS = CodeGenOpts.ExplicitEmulatedTLS;
495   Options.DebuggerTuning = CodeGenOpts.getDebuggerTuning();
496   Options.EmitStackSizeSection = CodeGenOpts.StackSizeSection;
497   Options.EmitAddrsig = CodeGenOpts.Addrsig;
498   Options.ForceDwarfFrameSection = CodeGenOpts.ForceDwarfFrameSection;
499   Options.EmitCallSiteInfo = CodeGenOpts.EmitCallSiteInfo;
500 
501   Options.MCOptions.SplitDwarfFile = CodeGenOpts.SplitDwarfFile;
502   Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll;
503   Options.MCOptions.MCSaveTempLabels = CodeGenOpts.SaveTempLabels;
504   Options.MCOptions.MCUseDwarfDirectory = !CodeGenOpts.NoDwarfDirectoryAsm;
505   Options.MCOptions.MCNoExecStack = CodeGenOpts.NoExecStack;
506   Options.MCOptions.MCIncrementalLinkerCompatible =
507       CodeGenOpts.IncrementalLinkerCompatible;
508   Options.MCOptions.MCFatalWarnings = CodeGenOpts.FatalWarnings;
509   Options.MCOptions.MCNoWarn = CodeGenOpts.NoWarn;
510   Options.MCOptions.AsmVerbose = CodeGenOpts.AsmVerbose;
511   Options.MCOptions.PreserveAsmComments = CodeGenOpts.PreserveAsmComments;
512   Options.MCOptions.ABIName = TargetOpts.ABI;
513   for (const auto &Entry : HSOpts.UserEntries)
514     if (!Entry.IsFramework &&
515         (Entry.Group == frontend::IncludeDirGroup::Quoted ||
516          Entry.Group == frontend::IncludeDirGroup::Angled ||
517          Entry.Group == frontend::IncludeDirGroup::System))
518       Options.MCOptions.IASSearchPaths.push_back(
519           Entry.IgnoreSysRoot ? Entry.Path : HSOpts.Sysroot + Entry.Path);
520 }
521 static Optional<GCOVOptions> getGCOVOptions(const CodeGenOptions &CodeGenOpts) {
522   if (CodeGenOpts.DisableGCov)
523     return None;
524   if (!CodeGenOpts.EmitGcovArcs && !CodeGenOpts.EmitGcovNotes)
525     return None;
526   // Not using 'GCOVOptions::getDefault' allows us to avoid exiting if
527   // LLVM's -default-gcov-version flag is set to something invalid.
528   GCOVOptions Options;
529   Options.EmitNotes = CodeGenOpts.EmitGcovNotes;
530   Options.EmitData = CodeGenOpts.EmitGcovArcs;
531   llvm::copy(CodeGenOpts.CoverageVersion, std::begin(Options.Version));
532   Options.UseCfgChecksum = CodeGenOpts.CoverageExtraChecksum;
533   Options.NoRedZone = CodeGenOpts.DisableRedZone;
534   Options.FunctionNamesInData = !CodeGenOpts.CoverageNoFunctionNamesInData;
535   Options.Filter = CodeGenOpts.ProfileFilterFiles;
536   Options.Exclude = CodeGenOpts.ProfileExcludeFiles;
537   Options.ExitBlockBeforeBody = CodeGenOpts.CoverageExitBlockBeforeBody;
538   return Options;
539 }
540 
541 static Optional<InstrProfOptions>
542 getInstrProfOptions(const CodeGenOptions &CodeGenOpts,
543                     const LangOptions &LangOpts) {
544   if (!CodeGenOpts.hasProfileClangInstr())
545     return None;
546   InstrProfOptions Options;
547   Options.NoRedZone = CodeGenOpts.DisableRedZone;
548   Options.InstrProfileOutput = CodeGenOpts.InstrProfileOutput;
549 
550   // TODO: Surface the option to emit atomic profile counter increments at
551   // the driver level.
552   Options.Atomic = LangOpts.Sanitize.has(SanitizerKind::Thread);
553   return Options;
554 }
555 
556 void EmitAssemblyHelper::CreatePasses(legacy::PassManager &MPM,
557                                       legacy::FunctionPassManager &FPM) {
558   // Handle disabling of all LLVM passes, where we want to preserve the
559   // internal module before any optimization.
560   if (CodeGenOpts.DisableLLVMPasses)
561     return;
562 
563   // Figure out TargetLibraryInfo.  This needs to be added to MPM and FPM
564   // manually (and not via PMBuilder), since some passes (eg. InstrProfiling)
565   // are inserted before PMBuilder ones - they'd get the default-constructed
566   // TLI with an unknown target otherwise.
567   Triple TargetTriple(TheModule->getTargetTriple());
568   std::unique_ptr<TargetLibraryInfoImpl> TLII(
569       createTLII(TargetTriple, CodeGenOpts));
570 
571   // If we reached here with a non-empty index file name, then the index file
572   // was empty and we are not performing ThinLTO backend compilation (used in
573   // testing in a distributed build environment). Drop any the type test
574   // assume sequences inserted for whole program vtables so that codegen doesn't
575   // complain.
576   if (!CodeGenOpts.ThinLTOIndexFile.empty())
577     MPM.add(createLowerTypeTestsPass(/*ExportSummary=*/nullptr,
578                                      /*ImportSummary=*/nullptr,
579                                      /*DropTypeTests=*/true));
580 
581   PassManagerBuilderWrapper PMBuilder(TargetTriple, CodeGenOpts, LangOpts);
582 
583   // At O0 and O1 we only run the always inliner which is more efficient. At
584   // higher optimization levels we run the normal inliner.
585   if (CodeGenOpts.OptimizationLevel <= 1) {
586     bool InsertLifetimeIntrinsics = ((CodeGenOpts.OptimizationLevel != 0 &&
587                                       !CodeGenOpts.DisableLifetimeMarkers) ||
588                                      LangOpts.Coroutines);
589     PMBuilder.Inliner = createAlwaysInlinerLegacyPass(InsertLifetimeIntrinsics);
590   } else {
591     // We do not want to inline hot callsites for SamplePGO module-summary build
592     // because profile annotation will happen again in ThinLTO backend, and we
593     // want the IR of the hot path to match the profile.
594     PMBuilder.Inliner = createFunctionInliningPass(
595         CodeGenOpts.OptimizationLevel, CodeGenOpts.OptimizeSize,
596         (!CodeGenOpts.SampleProfileFile.empty() &&
597          CodeGenOpts.PrepareForThinLTO));
598   }
599 
600   PMBuilder.OptLevel = CodeGenOpts.OptimizationLevel;
601   PMBuilder.SizeLevel = CodeGenOpts.OptimizeSize;
602   PMBuilder.SLPVectorize = CodeGenOpts.VectorizeSLP;
603   PMBuilder.LoopVectorize = CodeGenOpts.VectorizeLoop;
604 
605   PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops;
606   // Loop interleaving in the loop vectorizer has historically been set to be
607   // enabled when loop unrolling is enabled.
608   PMBuilder.LoopsInterleaved = CodeGenOpts.UnrollLoops;
609   PMBuilder.MergeFunctions = CodeGenOpts.MergeFunctions;
610   PMBuilder.PrepareForThinLTO = CodeGenOpts.PrepareForThinLTO;
611   PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO;
612   PMBuilder.RerollLoops = CodeGenOpts.RerollLoops;
613 
614   MPM.add(new TargetLibraryInfoWrapperPass(*TLII));
615 
616   if (TM)
617     TM->adjustPassManager(PMBuilder);
618 
619   if (CodeGenOpts.DebugInfoForProfiling ||
620       !CodeGenOpts.SampleProfileFile.empty())
621     PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
622                            addAddDiscriminatorsPass);
623 
624   // In ObjC ARC mode, add the main ARC optimization passes.
625   if (LangOpts.ObjCAutoRefCount) {
626     PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
627                            addObjCARCExpandPass);
628     PMBuilder.addExtension(PassManagerBuilder::EP_ModuleOptimizerEarly,
629                            addObjCARCAPElimPass);
630     PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
631                            addObjCARCOptPass);
632   }
633 
634   if (LangOpts.Coroutines)
635     addCoroutinePassesToExtensionPoints(PMBuilder);
636 
637   if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds)) {
638     PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
639                            addBoundsCheckingPass);
640     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
641                            addBoundsCheckingPass);
642   }
643 
644   if (CodeGenOpts.SanitizeCoverageType ||
645       CodeGenOpts.SanitizeCoverageIndirectCalls ||
646       CodeGenOpts.SanitizeCoverageTraceCmp) {
647     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
648                            addSanitizerCoveragePass);
649     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
650                            addSanitizerCoveragePass);
651   }
652 
653   if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
654     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
655                            addAddressSanitizerPasses);
656     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
657                            addAddressSanitizerPasses);
658   }
659 
660   if (LangOpts.Sanitize.has(SanitizerKind::KernelAddress)) {
661     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
662                            addKernelAddressSanitizerPasses);
663     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
664                            addKernelAddressSanitizerPasses);
665   }
666 
667   if (LangOpts.Sanitize.has(SanitizerKind::HWAddress)) {
668     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
669                            addHWAddressSanitizerPasses);
670     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
671                            addHWAddressSanitizerPasses);
672   }
673 
674   if (LangOpts.Sanitize.has(SanitizerKind::KernelHWAddress)) {
675     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
676                            addKernelHWAddressSanitizerPasses);
677     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
678                            addKernelHWAddressSanitizerPasses);
679   }
680 
681   if (LangOpts.Sanitize.has(SanitizerKind::Memory)) {
682     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
683                            addMemorySanitizerPass);
684     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
685                            addMemorySanitizerPass);
686   }
687 
688   if (LangOpts.Sanitize.has(SanitizerKind::KernelMemory)) {
689     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
690                            addKernelMemorySanitizerPass);
691     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
692                            addKernelMemorySanitizerPass);
693   }
694 
695   if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
696     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
697                            addThreadSanitizerPass);
698     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
699                            addThreadSanitizerPass);
700   }
701 
702   if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) {
703     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
704                            addDataFlowSanitizerPass);
705     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
706                            addDataFlowSanitizerPass);
707   }
708 
709   if (LangOpts.Sanitize.has(SanitizerKind::MemTag)) {
710     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
711                            addMemTagOptimizationPasses);
712   }
713 
714   // Set up the per-function pass manager.
715   FPM.add(new TargetLibraryInfoWrapperPass(*TLII));
716   if (CodeGenOpts.VerifyModule)
717     FPM.add(createVerifierPass());
718 
719   // Set up the per-module pass manager.
720   if (!CodeGenOpts.RewriteMapFiles.empty())
721     addSymbolRewriterPass(CodeGenOpts, &MPM);
722 
723   // Add UniqueInternalLinkageNames Pass which renames internal linkage symbols
724   // with unique names.
725   if (CodeGenOpts.UniqueInternalLinkageNames) {
726     MPM.add(createUniqueInternalLinkageNamesPass());
727   }
728 
729   if (Optional<GCOVOptions> Options = getGCOVOptions(CodeGenOpts)) {
730     MPM.add(createGCOVProfilerPass(*Options));
731     if (CodeGenOpts.getDebugInfo() == codegenoptions::NoDebugInfo)
732       MPM.add(createStripSymbolsPass(true));
733   }
734 
735   if (Optional<InstrProfOptions> Options =
736           getInstrProfOptions(CodeGenOpts, LangOpts))
737     MPM.add(createInstrProfilingLegacyPass(*Options, false));
738 
739   bool hasIRInstr = false;
740   if (CodeGenOpts.hasProfileIRInstr()) {
741     PMBuilder.EnablePGOInstrGen = true;
742     hasIRInstr = true;
743   }
744   if (CodeGenOpts.hasProfileCSIRInstr()) {
745     assert(!CodeGenOpts.hasProfileCSIRUse() &&
746            "Cannot have both CSProfileUse pass and CSProfileGen pass at the "
747            "same time");
748     assert(!hasIRInstr &&
749            "Cannot have both ProfileGen pass and CSProfileGen pass at the "
750            "same time");
751     PMBuilder.EnablePGOCSInstrGen = true;
752     hasIRInstr = true;
753   }
754   if (hasIRInstr) {
755     if (!CodeGenOpts.InstrProfileOutput.empty())
756       PMBuilder.PGOInstrGen = CodeGenOpts.InstrProfileOutput;
757     else
758       PMBuilder.PGOInstrGen = std::string(DefaultProfileGenName);
759   }
760   if (CodeGenOpts.hasProfileIRUse()) {
761     PMBuilder.PGOInstrUse = CodeGenOpts.ProfileInstrumentUsePath;
762     PMBuilder.EnablePGOCSInstrUse = CodeGenOpts.hasProfileCSIRUse();
763   }
764 
765   if (!CodeGenOpts.SampleProfileFile.empty())
766     PMBuilder.PGOSampleUse = CodeGenOpts.SampleProfileFile;
767 
768   PMBuilder.populateFunctionPassManager(FPM);
769   PMBuilder.populateModulePassManager(MPM);
770 }
771 
772 static void setCommandLineOpts(const CodeGenOptions &CodeGenOpts) {
773   SmallVector<const char *, 16> BackendArgs;
774   BackendArgs.push_back("clang"); // Fake program name.
775   if (!CodeGenOpts.DebugPass.empty()) {
776     BackendArgs.push_back("-debug-pass");
777     BackendArgs.push_back(CodeGenOpts.DebugPass.c_str());
778   }
779   if (!CodeGenOpts.LimitFloatPrecision.empty()) {
780     BackendArgs.push_back("-limit-float-precision");
781     BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str());
782   }
783   BackendArgs.push_back(nullptr);
784   llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
785                                     BackendArgs.data());
786 }
787 
788 void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
789   // Create the TargetMachine for generating code.
790   std::string Error;
791   std::string Triple = TheModule->getTargetTriple();
792   const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
793   if (!TheTarget) {
794     if (MustCreateTM)
795       Diags.Report(diag::err_fe_unable_to_create_target) << Error;
796     return;
797   }
798 
799   Optional<llvm::CodeModel::Model> CM = getCodeModel(CodeGenOpts);
800   std::string FeaturesStr =
801       llvm::join(TargetOpts.Features.begin(), TargetOpts.Features.end(), ",");
802   llvm::Reloc::Model RM = CodeGenOpts.RelocationModel;
803   CodeGenOpt::Level OptLevel = getCGOptLevel(CodeGenOpts);
804 
805   llvm::TargetOptions Options;
806   initTargetOptions(Options, CodeGenOpts, TargetOpts, LangOpts, HSOpts);
807   TM.reset(TheTarget->createTargetMachine(Triple, TargetOpts.CPU, FeaturesStr,
808                                           Options, RM, CM, OptLevel));
809 }
810 
811 bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager &CodeGenPasses,
812                                        BackendAction Action,
813                                        raw_pwrite_stream &OS,
814                                        raw_pwrite_stream *DwoOS) {
815   // Add LibraryInfo.
816   llvm::Triple TargetTriple(TheModule->getTargetTriple());
817   std::unique_ptr<TargetLibraryInfoImpl> TLII(
818       createTLII(TargetTriple, CodeGenOpts));
819   CodeGenPasses.add(new TargetLibraryInfoWrapperPass(*TLII));
820 
821   // Normal mode, emit a .s or .o file by running the code generator. Note,
822   // this also adds codegenerator level optimization passes.
823   CodeGenFileType CGFT = getCodeGenFileType(Action);
824 
825   // Add ObjC ARC final-cleanup optimizations. This is done as part of the
826   // "codegen" passes so that it isn't run multiple times when there is
827   // inlining happening.
828   if (CodeGenOpts.OptimizationLevel > 0)
829     CodeGenPasses.add(createObjCARCContractPass());
830 
831   if (TM->addPassesToEmitFile(CodeGenPasses, OS, DwoOS, CGFT,
832                               /*DisableVerify=*/!CodeGenOpts.VerifyModule)) {
833     Diags.Report(diag::err_fe_unable_to_interface_with_target);
834     return false;
835   }
836 
837   return true;
838 }
839 
840 void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
841                                       std::unique_ptr<raw_pwrite_stream> OS) {
842   TimeRegion Region(FrontendTimesIsEnabled ? &CodeGenerationTime : nullptr);
843 
844   setCommandLineOpts(CodeGenOpts);
845 
846   bool UsesCodeGen = (Action != Backend_EmitNothing &&
847                       Action != Backend_EmitBC &&
848                       Action != Backend_EmitLL);
849   CreateTargetMachine(UsesCodeGen);
850 
851   if (UsesCodeGen && !TM)
852     return;
853   if (TM)
854     TheModule->setDataLayout(TM->createDataLayout());
855 
856   legacy::PassManager PerModulePasses;
857   PerModulePasses.add(
858       createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
859 
860   legacy::FunctionPassManager PerFunctionPasses(TheModule);
861   PerFunctionPasses.add(
862       createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
863 
864   CreatePasses(PerModulePasses, PerFunctionPasses);
865 
866   legacy::PassManager CodeGenPasses;
867   CodeGenPasses.add(
868       createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
869 
870   std::unique_ptr<llvm::ToolOutputFile> ThinLinkOS, DwoOS;
871 
872   switch (Action) {
873   case Backend_EmitNothing:
874     break;
875 
876   case Backend_EmitBC:
877     if (CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.DisableLLVMPasses) {
878       if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
879         ThinLinkOS = openOutputFile(CodeGenOpts.ThinLinkBitcodeFile);
880         if (!ThinLinkOS)
881           return;
882       }
883       TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
884                                CodeGenOpts.EnableSplitLTOUnit);
885       PerModulePasses.add(createWriteThinLTOBitcodePass(
886           *OS, ThinLinkOS ? &ThinLinkOS->os() : nullptr));
887     } else {
888       // Emit a module summary by default for Regular LTO except for ld64
889       // targets
890       bool EmitLTOSummary =
891           (CodeGenOpts.PrepareForLTO &&
892            !CodeGenOpts.DisableLLVMPasses &&
893            llvm::Triple(TheModule->getTargetTriple()).getVendor() !=
894                llvm::Triple::Apple);
895       if (EmitLTOSummary) {
896         if (!TheModule->getModuleFlag("ThinLTO"))
897           TheModule->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
898         TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
899                                  uint32_t(1));
900       }
901 
902       PerModulePasses.add(createBitcodeWriterPass(
903           *OS, CodeGenOpts.EmitLLVMUseLists, EmitLTOSummary));
904     }
905     break;
906 
907   case Backend_EmitLL:
908     PerModulePasses.add(
909         createPrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
910     break;
911 
912   default:
913     if (!CodeGenOpts.SplitDwarfOutput.empty()) {
914       DwoOS = openOutputFile(CodeGenOpts.SplitDwarfOutput);
915       if (!DwoOS)
916         return;
917     }
918     if (!AddEmitPasses(CodeGenPasses, Action, *OS,
919                        DwoOS ? &DwoOS->os() : nullptr))
920       return;
921   }
922 
923   // Before executing passes, print the final values of the LLVM options.
924   cl::PrintOptionValues();
925 
926   // Run passes. For now we do all passes at once, but eventually we
927   // would like to have the option of streaming code generation.
928 
929   {
930     PrettyStackTraceString CrashInfo("Per-function optimization");
931     llvm::TimeTraceScope TimeScope("PerFunctionPasses");
932 
933     PerFunctionPasses.doInitialization();
934     for (Function &F : *TheModule)
935       if (!F.isDeclaration())
936         PerFunctionPasses.run(F);
937     PerFunctionPasses.doFinalization();
938   }
939 
940   {
941     PrettyStackTraceString CrashInfo("Per-module optimization passes");
942     llvm::TimeTraceScope TimeScope("PerModulePasses");
943     PerModulePasses.run(*TheModule);
944   }
945 
946   {
947     PrettyStackTraceString CrashInfo("Code generation");
948     llvm::TimeTraceScope TimeScope("CodeGenPasses");
949     CodeGenPasses.run(*TheModule);
950   }
951 
952   if (ThinLinkOS)
953     ThinLinkOS->keep();
954   if (DwoOS)
955     DwoOS->keep();
956 }
957 
958 static PassBuilder::OptimizationLevel mapToLevel(const CodeGenOptions &Opts) {
959   switch (Opts.OptimizationLevel) {
960   default:
961     llvm_unreachable("Invalid optimization level!");
962 
963   case 1:
964     return PassBuilder::OptimizationLevel::O1;
965 
966   case 2:
967     switch (Opts.OptimizeSize) {
968     default:
969       llvm_unreachable("Invalid optimization level for size!");
970 
971     case 0:
972       return PassBuilder::OptimizationLevel::O2;
973 
974     case 1:
975       return PassBuilder::OptimizationLevel::Os;
976 
977     case 2:
978       return PassBuilder::OptimizationLevel::Oz;
979     }
980 
981   case 3:
982     return PassBuilder::OptimizationLevel::O3;
983   }
984 }
985 
986 static void addCoroutinePassesAtO0(ModulePassManager &MPM,
987                                    const LangOptions &LangOpts,
988                                    const CodeGenOptions &CodeGenOpts) {
989   if (!LangOpts.Coroutines)
990     return;
991 
992   MPM.addPass(createModuleToFunctionPassAdaptor(CoroEarlyPass()));
993 
994   CGSCCPassManager CGPM(CodeGenOpts.DebugPassManager);
995   CGPM.addPass(CoroSplitPass());
996   CGPM.addPass(createCGSCCToFunctionPassAdaptor(CoroElidePass()));
997   MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
998 
999   MPM.addPass(createModuleToFunctionPassAdaptor(CoroCleanupPass()));
1000 }
1001 
1002 static void addSanitizersAtO0(ModulePassManager &MPM,
1003                               const Triple &TargetTriple,
1004                               const LangOptions &LangOpts,
1005                               const CodeGenOptions &CodeGenOpts) {
1006   auto ASanPass = [&](SanitizerMask Mask, bool CompileKernel) {
1007     MPM.addPass(RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>());
1008     bool Recover = CodeGenOpts.SanitizeRecover.has(Mask);
1009     MPM.addPass(createModuleToFunctionPassAdaptor(AddressSanitizerPass(
1010         CompileKernel, Recover, CodeGenOpts.SanitizeAddressUseAfterScope)));
1011     bool ModuleUseAfterScope = asanUseGlobalsGC(TargetTriple, CodeGenOpts);
1012     MPM.addPass(
1013         ModuleAddressSanitizerPass(CompileKernel, Recover, ModuleUseAfterScope,
1014                                    CodeGenOpts.SanitizeAddressUseOdrIndicator));
1015   };
1016 
1017   if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
1018     ASanPass(SanitizerKind::Address, /*CompileKernel=*/false);
1019   }
1020 
1021   if (LangOpts.Sanitize.has(SanitizerKind::KernelAddress)) {
1022     ASanPass(SanitizerKind::KernelAddress, /*CompileKernel=*/true);
1023   }
1024 
1025   if (LangOpts.Sanitize.has(SanitizerKind::Memory)) {
1026     bool Recover = CodeGenOpts.SanitizeRecover.has(SanitizerKind::Memory);
1027     int TrackOrigins = CodeGenOpts.SanitizeMemoryTrackOrigins;
1028     MPM.addPass(MemorySanitizerPass({TrackOrigins, Recover, false}));
1029     MPM.addPass(createModuleToFunctionPassAdaptor(
1030         MemorySanitizerPass({TrackOrigins, Recover, false})));
1031   }
1032 
1033   if (LangOpts.Sanitize.has(SanitizerKind::KernelMemory)) {
1034     MPM.addPass(createModuleToFunctionPassAdaptor(
1035         MemorySanitizerPass({0, false, /*Kernel=*/true})));
1036   }
1037 
1038   if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
1039     MPM.addPass(ThreadSanitizerPass());
1040     MPM.addPass(createModuleToFunctionPassAdaptor(ThreadSanitizerPass()));
1041   }
1042 }
1043 
1044 /// A clean version of `EmitAssembly` that uses the new pass manager.
1045 ///
1046 /// Not all features are currently supported in this system, but where
1047 /// necessary it falls back to the legacy pass manager to at least provide
1048 /// basic functionality.
1049 ///
1050 /// This API is planned to have its functionality finished and then to replace
1051 /// `EmitAssembly` at some point in the future when the default switches.
1052 void EmitAssemblyHelper::EmitAssemblyWithNewPassManager(
1053     BackendAction Action, std::unique_ptr<raw_pwrite_stream> OS) {
1054   TimeRegion Region(FrontendTimesIsEnabled ? &CodeGenerationTime : nullptr);
1055   setCommandLineOpts(CodeGenOpts);
1056 
1057   bool RequiresCodeGen = (Action != Backend_EmitNothing &&
1058                           Action != Backend_EmitBC &&
1059                           Action != Backend_EmitLL);
1060   CreateTargetMachine(RequiresCodeGen);
1061 
1062   if (RequiresCodeGen && !TM)
1063     return;
1064   if (TM)
1065     TheModule->setDataLayout(TM->createDataLayout());
1066 
1067   Optional<PGOOptions> PGOOpt;
1068 
1069   if (CodeGenOpts.hasProfileIRInstr())
1070     // -fprofile-generate.
1071     PGOOpt = PGOOptions(CodeGenOpts.InstrProfileOutput.empty()
1072                             ? std::string(DefaultProfileGenName)
1073                             : CodeGenOpts.InstrProfileOutput,
1074                         "", "", PGOOptions::IRInstr, PGOOptions::NoCSAction,
1075                         CodeGenOpts.DebugInfoForProfiling);
1076   else if (CodeGenOpts.hasProfileIRUse()) {
1077     // -fprofile-use.
1078     auto CSAction = CodeGenOpts.hasProfileCSIRUse() ? PGOOptions::CSIRUse
1079                                                     : PGOOptions::NoCSAction;
1080     PGOOpt = PGOOptions(CodeGenOpts.ProfileInstrumentUsePath, "",
1081                         CodeGenOpts.ProfileRemappingFile, PGOOptions::IRUse,
1082                         CSAction, CodeGenOpts.DebugInfoForProfiling);
1083   } else if (!CodeGenOpts.SampleProfileFile.empty())
1084     // -fprofile-sample-use
1085     PGOOpt =
1086         PGOOptions(CodeGenOpts.SampleProfileFile, "",
1087                    CodeGenOpts.ProfileRemappingFile, PGOOptions::SampleUse,
1088                    PGOOptions::NoCSAction, CodeGenOpts.DebugInfoForProfiling);
1089   else if (CodeGenOpts.DebugInfoForProfiling)
1090     // -fdebug-info-for-profiling
1091     PGOOpt = PGOOptions("", "", "", PGOOptions::NoAction,
1092                         PGOOptions::NoCSAction, true);
1093 
1094   // Check to see if we want to generate a CS profile.
1095   if (CodeGenOpts.hasProfileCSIRInstr()) {
1096     assert(!CodeGenOpts.hasProfileCSIRUse() &&
1097            "Cannot have both CSProfileUse pass and CSProfileGen pass at "
1098            "the same time");
1099     if (PGOOpt.hasValue()) {
1100       assert(PGOOpt->Action != PGOOptions::IRInstr &&
1101              PGOOpt->Action != PGOOptions::SampleUse &&
1102              "Cannot run CSProfileGen pass with ProfileGen or SampleUse "
1103              " pass");
1104       PGOOpt->CSProfileGenFile = CodeGenOpts.InstrProfileOutput.empty()
1105                                      ? std::string(DefaultProfileGenName)
1106                                      : CodeGenOpts.InstrProfileOutput;
1107       PGOOpt->CSAction = PGOOptions::CSIRInstr;
1108     } else
1109       PGOOpt = PGOOptions("",
1110                           CodeGenOpts.InstrProfileOutput.empty()
1111                               ? std::string(DefaultProfileGenName)
1112                               : CodeGenOpts.InstrProfileOutput,
1113                           "", PGOOptions::NoAction, PGOOptions::CSIRInstr,
1114                           CodeGenOpts.DebugInfoForProfiling);
1115   }
1116 
1117   PipelineTuningOptions PTO;
1118   PTO.LoopUnrolling = CodeGenOpts.UnrollLoops;
1119   // For historical reasons, loop interleaving is set to mirror setting for loop
1120   // unrolling.
1121   PTO.LoopInterleaving = CodeGenOpts.UnrollLoops;
1122   PTO.LoopVectorization = CodeGenOpts.VectorizeLoop;
1123   PTO.SLPVectorization = CodeGenOpts.VectorizeSLP;
1124   PTO.CallGraphProfile = CodeGenOpts.CallGraphProfile;
1125   PTO.Coroutines = LangOpts.Coroutines;
1126 
1127   PassInstrumentationCallbacks PIC;
1128   StandardInstrumentations SI;
1129   SI.registerCallbacks(PIC);
1130   PassBuilder PB(TM.get(), PTO, PGOOpt, &PIC);
1131 
1132   // Attempt to load pass plugins and register their callbacks with PB.
1133   for (auto &PluginFN : CodeGenOpts.PassPlugins) {
1134     auto PassPlugin = PassPlugin::Load(PluginFN);
1135     if (PassPlugin) {
1136       PassPlugin->registerPassBuilderCallbacks(PB);
1137     } else {
1138       Diags.Report(diag::err_fe_unable_to_load_plugin)
1139           << PluginFN << toString(PassPlugin.takeError());
1140     }
1141   }
1142 #define HANDLE_EXTENSION(Ext)                                                  \
1143   get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
1144 #include "llvm/Support/Extension.def"
1145 
1146   LoopAnalysisManager LAM(CodeGenOpts.DebugPassManager);
1147   FunctionAnalysisManager FAM(CodeGenOpts.DebugPassManager);
1148   CGSCCAnalysisManager CGAM(CodeGenOpts.DebugPassManager);
1149   ModuleAnalysisManager MAM(CodeGenOpts.DebugPassManager);
1150 
1151   // Register the AA manager first so that our version is the one used.
1152   FAM.registerPass([&] { return PB.buildDefaultAAPipeline(); });
1153 
1154   // Register the target library analysis directly and give it a customized
1155   // preset TLI.
1156   Triple TargetTriple(TheModule->getTargetTriple());
1157   std::unique_ptr<TargetLibraryInfoImpl> TLII(
1158       createTLII(TargetTriple, CodeGenOpts));
1159   FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
1160 
1161   // Register all the basic analyses with the managers.
1162   PB.registerModuleAnalyses(MAM);
1163   PB.registerCGSCCAnalyses(CGAM);
1164   PB.registerFunctionAnalyses(FAM);
1165   PB.registerLoopAnalyses(LAM);
1166   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
1167 
1168   ModulePassManager MPM(CodeGenOpts.DebugPassManager);
1169 
1170   if (!CodeGenOpts.DisableLLVMPasses) {
1171     bool IsThinLTO = CodeGenOpts.PrepareForThinLTO;
1172     bool IsLTO = CodeGenOpts.PrepareForLTO;
1173 
1174     if (CodeGenOpts.OptimizationLevel == 0) {
1175       // If we reached here with a non-empty index file name, then the index
1176       // file was empty and we are not performing ThinLTO backend compilation
1177       // (used in testing in a distributed build environment). Drop any the type
1178       // test assume sequences inserted for whole program vtables so that
1179       // codegen doesn't complain.
1180       if (!CodeGenOpts.ThinLTOIndexFile.empty())
1181         MPM.addPass(LowerTypeTestsPass(/*ExportSummary=*/nullptr,
1182                                        /*ImportSummary=*/nullptr,
1183                                        /*DropTypeTests=*/true));
1184       if (Optional<GCOVOptions> Options = getGCOVOptions(CodeGenOpts))
1185         MPM.addPass(GCOVProfilerPass(*Options));
1186       if (Optional<InstrProfOptions> Options =
1187               getInstrProfOptions(CodeGenOpts, LangOpts))
1188         MPM.addPass(InstrProfiling(*Options, false));
1189 
1190       // Build a minimal pipeline based on the semantics required by Clang,
1191       // which is just that always inlining occurs. Further, disable generating
1192       // lifetime intrinsics to avoid enabling further optimizations during
1193       // code generation.
1194       // However, we need to insert lifetime intrinsics to avoid invalid access
1195       // caused by multithreaded coroutines.
1196       MPM.addPass(
1197           AlwaysInlinerPass(/*InsertLifetimeIntrinsics=*/LangOpts.Coroutines));
1198 
1199       // At -O0, we can still do PGO. Add all the requested passes for
1200       // instrumentation PGO, if requested.
1201       if (PGOOpt && (PGOOpt->Action == PGOOptions::IRInstr ||
1202                      PGOOpt->Action == PGOOptions::IRUse))
1203         PB.addPGOInstrPassesForO0(
1204             MPM, CodeGenOpts.DebugPassManager,
1205             /* RunProfileGen */ (PGOOpt->Action == PGOOptions::IRInstr),
1206             /* IsCS */ false, PGOOpt->ProfileFile,
1207             PGOOpt->ProfileRemappingFile);
1208 
1209       // At -O0 we directly run necessary sanitizer passes.
1210       if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds))
1211         MPM.addPass(createModuleToFunctionPassAdaptor(BoundsCheckingPass()));
1212 
1213       // Add UniqueInternalLinkageNames Pass which renames internal linkage
1214       // symbols with unique names.
1215       if (CodeGenOpts.UniqueInternalLinkageNames) {
1216         MPM.addPass(UniqueInternalLinkageNamesPass());
1217       }
1218 
1219       // Lastly, add semantically necessary passes for LTO.
1220       if (IsLTO || IsThinLTO) {
1221         MPM.addPass(CanonicalizeAliasesPass());
1222         MPM.addPass(NameAnonGlobalPass());
1223       }
1224     } else {
1225       // Map our optimization levels into one of the distinct levels used to
1226       // configure the pipeline.
1227       PassBuilder::OptimizationLevel Level = mapToLevel(CodeGenOpts);
1228 
1229       // If we reached here with a non-empty index file name, then the index
1230       // file was empty and we are not performing ThinLTO backend compilation
1231       // (used in testing in a distributed build environment). Drop any the type
1232       // test assume sequences inserted for whole program vtables so that
1233       // codegen doesn't complain.
1234       if (!CodeGenOpts.ThinLTOIndexFile.empty())
1235         PB.registerPipelineStartEPCallback([](ModulePassManager &MPM) {
1236           MPM.addPass(LowerTypeTestsPass(/*ExportSummary=*/nullptr,
1237                                          /*ImportSummary=*/nullptr,
1238                                          /*DropTypeTests=*/true));
1239         });
1240 
1241       PB.registerPipelineStartEPCallback([](ModulePassManager &MPM) {
1242         MPM.addPass(createModuleToFunctionPassAdaptor(
1243             EntryExitInstrumenterPass(/*PostInlining=*/false)));
1244       });
1245 
1246       // Register callbacks to schedule sanitizer passes at the appropriate part of
1247       // the pipeline.
1248       // FIXME: either handle asan/the remaining sanitizers or error out
1249       if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds))
1250         PB.registerScalarOptimizerLateEPCallback(
1251             [](FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
1252               FPM.addPass(BoundsCheckingPass());
1253             });
1254       if (LangOpts.Sanitize.has(SanitizerKind::Memory)) {
1255         int TrackOrigins = CodeGenOpts.SanitizeMemoryTrackOrigins;
1256         bool Recover = CodeGenOpts.SanitizeRecover.has(SanitizerKind::Memory);
1257         PB.registerPipelineStartEPCallback(
1258             [TrackOrigins, Recover](ModulePassManager &MPM) {
1259               MPM.addPass(MemorySanitizerPass({TrackOrigins, Recover, false}));
1260             });
1261         PB.registerOptimizerLastEPCallback(
1262             [TrackOrigins, Recover](FunctionPassManager &FPM,
1263                                     PassBuilder::OptimizationLevel Level) {
1264               FPM.addPass(MemorySanitizerPass({TrackOrigins, Recover, false}));
1265             });
1266       }
1267       if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
1268         PB.registerPipelineStartEPCallback(
1269             [](ModulePassManager &MPM) { MPM.addPass(ThreadSanitizerPass()); });
1270         PB.registerOptimizerLastEPCallback(
1271             [](FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
1272               FPM.addPass(ThreadSanitizerPass());
1273             });
1274       }
1275       if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
1276         PB.registerPipelineStartEPCallback([&](ModulePassManager &MPM) {
1277           MPM.addPass(
1278               RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>());
1279         });
1280         bool Recover = CodeGenOpts.SanitizeRecover.has(SanitizerKind::Address);
1281         bool UseAfterScope = CodeGenOpts.SanitizeAddressUseAfterScope;
1282         PB.registerOptimizerLastEPCallback(
1283             [Recover, UseAfterScope](FunctionPassManager &FPM,
1284                                      PassBuilder::OptimizationLevel Level) {
1285               FPM.addPass(AddressSanitizerPass(
1286                   /*CompileKernel=*/false, Recover, UseAfterScope));
1287             });
1288         bool ModuleUseAfterScope = asanUseGlobalsGC(TargetTriple, CodeGenOpts);
1289         bool UseOdrIndicator = CodeGenOpts.SanitizeAddressUseOdrIndicator;
1290         PB.registerPipelineStartEPCallback(
1291             [Recover, ModuleUseAfterScope,
1292              UseOdrIndicator](ModulePassManager &MPM) {
1293               MPM.addPass(ModuleAddressSanitizerPass(
1294                   /*CompileKernel=*/false, Recover, ModuleUseAfterScope,
1295                   UseOdrIndicator));
1296             });
1297       }
1298       if (Optional<GCOVOptions> Options = getGCOVOptions(CodeGenOpts))
1299         PB.registerPipelineStartEPCallback([Options](ModulePassManager &MPM) {
1300           MPM.addPass(GCOVProfilerPass(*Options));
1301         });
1302       if (Optional<InstrProfOptions> Options =
1303               getInstrProfOptions(CodeGenOpts, LangOpts))
1304         PB.registerPipelineStartEPCallback([Options](ModulePassManager &MPM) {
1305           MPM.addPass(InstrProfiling(*Options, false));
1306         });
1307 
1308       // Add UniqueInternalLinkageNames Pass which renames internal linkage
1309       // symbols with unique names.
1310       if (CodeGenOpts.UniqueInternalLinkageNames) {
1311         MPM.addPass(UniqueInternalLinkageNamesPass());
1312       }
1313 
1314       if (IsThinLTO) {
1315         MPM = PB.buildThinLTOPreLinkDefaultPipeline(
1316             Level, CodeGenOpts.DebugPassManager);
1317         MPM.addPass(CanonicalizeAliasesPass());
1318         MPM.addPass(NameAnonGlobalPass());
1319       } else if (IsLTO) {
1320         MPM = PB.buildLTOPreLinkDefaultPipeline(Level,
1321                                                 CodeGenOpts.DebugPassManager);
1322         MPM.addPass(CanonicalizeAliasesPass());
1323         MPM.addPass(NameAnonGlobalPass());
1324       } else {
1325         MPM = PB.buildPerModuleDefaultPipeline(Level,
1326                                                CodeGenOpts.DebugPassManager);
1327       }
1328     }
1329 
1330     if (CodeGenOpts.SanitizeCoverageType ||
1331         CodeGenOpts.SanitizeCoverageIndirectCalls ||
1332         CodeGenOpts.SanitizeCoverageTraceCmp) {
1333       auto SancovOpts = getSancovOptsFromCGOpts(CodeGenOpts);
1334       MPM.addPass(ModuleSanitizerCoveragePass(SancovOpts));
1335     }
1336 
1337     if (LangOpts.Sanitize.has(SanitizerKind::HWAddress)) {
1338       bool Recover = CodeGenOpts.SanitizeRecover.has(SanitizerKind::HWAddress);
1339       MPM.addPass(HWAddressSanitizerPass(
1340           /*CompileKernel=*/false, Recover));
1341     }
1342     if (LangOpts.Sanitize.has(SanitizerKind::KernelHWAddress)) {
1343       MPM.addPass(HWAddressSanitizerPass(
1344           /*CompileKernel=*/true, /*Recover=*/true));
1345     }
1346 
1347     if (CodeGenOpts.OptimizationLevel > 0 &&
1348         LangOpts.Sanitize.has(SanitizerKind::MemTag)) {
1349       MPM.addPass(StackSafetyGlobalAnnotatorPass());
1350     }
1351 
1352     if (CodeGenOpts.OptimizationLevel == 0) {
1353       addCoroutinePassesAtO0(MPM, LangOpts, CodeGenOpts);
1354       addSanitizersAtO0(MPM, TargetTriple, LangOpts, CodeGenOpts);
1355     }
1356   }
1357 
1358   // FIXME: We still use the legacy pass manager to do code generation. We
1359   // create that pass manager here and use it as needed below.
1360   legacy::PassManager CodeGenPasses;
1361   bool NeedCodeGen = false;
1362   std::unique_ptr<llvm::ToolOutputFile> ThinLinkOS, DwoOS;
1363 
1364   // Append any output we need to the pass manager.
1365   switch (Action) {
1366   case Backend_EmitNothing:
1367     break;
1368 
1369   case Backend_EmitBC:
1370     if (CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.DisableLLVMPasses) {
1371       if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
1372         ThinLinkOS = openOutputFile(CodeGenOpts.ThinLinkBitcodeFile);
1373         if (!ThinLinkOS)
1374           return;
1375       }
1376       TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
1377                                CodeGenOpts.EnableSplitLTOUnit);
1378       MPM.addPass(ThinLTOBitcodeWriterPass(*OS, ThinLinkOS ? &ThinLinkOS->os()
1379                                                            : nullptr));
1380     } else {
1381       // Emit a module summary by default for Regular LTO except for ld64
1382       // targets
1383       bool EmitLTOSummary =
1384           (CodeGenOpts.PrepareForLTO &&
1385            !CodeGenOpts.DisableLLVMPasses &&
1386            llvm::Triple(TheModule->getTargetTriple()).getVendor() !=
1387                llvm::Triple::Apple);
1388       if (EmitLTOSummary) {
1389         if (!TheModule->getModuleFlag("ThinLTO"))
1390           TheModule->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
1391         TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
1392                                  uint32_t(1));
1393       }
1394       MPM.addPass(
1395           BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists, EmitLTOSummary));
1396     }
1397     break;
1398 
1399   case Backend_EmitLL:
1400     MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
1401     break;
1402 
1403   case Backend_EmitAssembly:
1404   case Backend_EmitMCNull:
1405   case Backend_EmitObj:
1406     NeedCodeGen = true;
1407     CodeGenPasses.add(
1408         createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
1409     if (!CodeGenOpts.SplitDwarfOutput.empty()) {
1410       DwoOS = openOutputFile(CodeGenOpts.SplitDwarfOutput);
1411       if (!DwoOS)
1412         return;
1413     }
1414     if (!AddEmitPasses(CodeGenPasses, Action, *OS,
1415                        DwoOS ? &DwoOS->os() : nullptr))
1416       // FIXME: Should we handle this error differently?
1417       return;
1418     break;
1419   }
1420 
1421   // Before executing passes, print the final values of the LLVM options.
1422   cl::PrintOptionValues();
1423 
1424   // Now that we have all of the passes ready, run them.
1425   {
1426     PrettyStackTraceString CrashInfo("Optimizer");
1427     MPM.run(*TheModule, MAM);
1428   }
1429 
1430   // Now if needed, run the legacy PM for codegen.
1431   if (NeedCodeGen) {
1432     PrettyStackTraceString CrashInfo("Code generation");
1433     CodeGenPasses.run(*TheModule);
1434   }
1435 
1436   if (ThinLinkOS)
1437     ThinLinkOS->keep();
1438   if (DwoOS)
1439     DwoOS->keep();
1440 }
1441 
1442 Expected<BitcodeModule> clang::FindThinLTOModule(MemoryBufferRef MBRef) {
1443   Expected<std::vector<BitcodeModule>> BMsOrErr = getBitcodeModuleList(MBRef);
1444   if (!BMsOrErr)
1445     return BMsOrErr.takeError();
1446 
1447   // The bitcode file may contain multiple modules, we want the one that is
1448   // marked as being the ThinLTO module.
1449   if (const BitcodeModule *Bm = FindThinLTOModule(*BMsOrErr))
1450     return *Bm;
1451 
1452   return make_error<StringError>("Could not find module summary",
1453                                  inconvertibleErrorCode());
1454 }
1455 
1456 BitcodeModule *clang::FindThinLTOModule(MutableArrayRef<BitcodeModule> BMs) {
1457   for (BitcodeModule &BM : BMs) {
1458     Expected<BitcodeLTOInfo> LTOInfo = BM.getLTOInfo();
1459     if (LTOInfo && LTOInfo->IsThinLTO)
1460       return &BM;
1461   }
1462   return nullptr;
1463 }
1464 
1465 static void runThinLTOBackend(ModuleSummaryIndex *CombinedIndex, Module *M,
1466                               const HeaderSearchOptions &HeaderOpts,
1467                               const CodeGenOptions &CGOpts,
1468                               const clang::TargetOptions &TOpts,
1469                               const LangOptions &LOpts,
1470                               std::unique_ptr<raw_pwrite_stream> OS,
1471                               std::string SampleProfile,
1472                               std::string ProfileRemapping,
1473                               BackendAction Action) {
1474   StringMap<DenseMap<GlobalValue::GUID, GlobalValueSummary *>>
1475       ModuleToDefinedGVSummaries;
1476   CombinedIndex->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
1477 
1478   setCommandLineOpts(CGOpts);
1479 
1480   // We can simply import the values mentioned in the combined index, since
1481   // we should only invoke this using the individual indexes written out
1482   // via a WriteIndexesThinBackend.
1483   FunctionImporter::ImportMapTy ImportList;
1484   for (auto &GlobalList : *CombinedIndex) {
1485     // Ignore entries for undefined references.
1486     if (GlobalList.second.SummaryList.empty())
1487       continue;
1488 
1489     auto GUID = GlobalList.first;
1490     for (auto &Summary : GlobalList.second.SummaryList) {
1491       // Skip the summaries for the importing module. These are included to
1492       // e.g. record required linkage changes.
1493       if (Summary->modulePath() == M->getModuleIdentifier())
1494         continue;
1495       // Add an entry to provoke importing by thinBackend.
1496       ImportList[Summary->modulePath()].insert(GUID);
1497     }
1498   }
1499 
1500   std::vector<std::unique_ptr<llvm::MemoryBuffer>> OwnedImports;
1501   MapVector<llvm::StringRef, llvm::BitcodeModule> ModuleMap;
1502 
1503   for (auto &I : ImportList) {
1504     ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MBOrErr =
1505         llvm::MemoryBuffer::getFile(I.first());
1506     if (!MBOrErr) {
1507       errs() << "Error loading imported file '" << I.first()
1508              << "': " << MBOrErr.getError().message() << "\n";
1509       return;
1510     }
1511 
1512     Expected<BitcodeModule> BMOrErr = FindThinLTOModule(**MBOrErr);
1513     if (!BMOrErr) {
1514       handleAllErrors(BMOrErr.takeError(), [&](ErrorInfoBase &EIB) {
1515         errs() << "Error loading imported file '" << I.first()
1516                << "': " << EIB.message() << '\n';
1517       });
1518       return;
1519     }
1520     ModuleMap.insert({I.first(), *BMOrErr});
1521 
1522     OwnedImports.push_back(std::move(*MBOrErr));
1523   }
1524   auto AddStream = [&](size_t Task) {
1525     return std::make_unique<lto::NativeObjectStream>(std::move(OS));
1526   };
1527   lto::Config Conf;
1528   if (CGOpts.SaveTempsFilePrefix != "") {
1529     if (Error E = Conf.addSaveTemps(CGOpts.SaveTempsFilePrefix + ".",
1530                                     /* UseInputModulePath */ false)) {
1531       handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
1532         errs() << "Error setting up ThinLTO save-temps: " << EIB.message()
1533                << '\n';
1534       });
1535     }
1536   }
1537   Conf.CPU = TOpts.CPU;
1538   Conf.CodeModel = getCodeModel(CGOpts);
1539   Conf.MAttrs = TOpts.Features;
1540   Conf.RelocModel = CGOpts.RelocationModel;
1541   Conf.CGOptLevel = getCGOptLevel(CGOpts);
1542   Conf.OptLevel = CGOpts.OptimizationLevel;
1543   initTargetOptions(Conf.Options, CGOpts, TOpts, LOpts, HeaderOpts);
1544   Conf.SampleProfile = std::move(SampleProfile);
1545   Conf.PTO.LoopUnrolling = CGOpts.UnrollLoops;
1546   // For historical reasons, loop interleaving is set to mirror setting for loop
1547   // unrolling.
1548   Conf.PTO.LoopInterleaving = CGOpts.UnrollLoops;
1549   Conf.PTO.LoopVectorization = CGOpts.VectorizeLoop;
1550   Conf.PTO.SLPVectorization = CGOpts.VectorizeSLP;
1551   Conf.PTO.CallGraphProfile = CGOpts.CallGraphProfile;
1552 
1553   // Context sensitive profile.
1554   if (CGOpts.hasProfileCSIRInstr()) {
1555     Conf.RunCSIRInstr = true;
1556     Conf.CSIRProfile = std::move(CGOpts.InstrProfileOutput);
1557   } else if (CGOpts.hasProfileCSIRUse()) {
1558     Conf.RunCSIRInstr = false;
1559     Conf.CSIRProfile = std::move(CGOpts.ProfileInstrumentUsePath);
1560   }
1561 
1562   Conf.ProfileRemapping = std::move(ProfileRemapping);
1563   Conf.UseNewPM = CGOpts.ExperimentalNewPassManager;
1564   Conf.DebugPassManager = CGOpts.DebugPassManager;
1565   Conf.RemarksWithHotness = CGOpts.DiagnosticsWithHotness;
1566   Conf.RemarksFilename = CGOpts.OptRecordFile;
1567   Conf.RemarksPasses = CGOpts.OptRecordPasses;
1568   Conf.RemarksFormat = CGOpts.OptRecordFormat;
1569   Conf.SplitDwarfFile = CGOpts.SplitDwarfFile;
1570   Conf.SplitDwarfOutput = CGOpts.SplitDwarfOutput;
1571   switch (Action) {
1572   case Backend_EmitNothing:
1573     Conf.PreCodeGenModuleHook = [](size_t Task, const Module &Mod) {
1574       return false;
1575     };
1576     break;
1577   case Backend_EmitLL:
1578     Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
1579       M->print(*OS, nullptr, CGOpts.EmitLLVMUseLists);
1580       return false;
1581     };
1582     break;
1583   case Backend_EmitBC:
1584     Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
1585       WriteBitcodeToFile(*M, *OS, CGOpts.EmitLLVMUseLists);
1586       return false;
1587     };
1588     break;
1589   default:
1590     Conf.CGFileType = getCodeGenFileType(Action);
1591     break;
1592   }
1593   if (Error E = thinBackend(
1594           Conf, -1, AddStream, *M, *CombinedIndex, ImportList,
1595           ModuleToDefinedGVSummaries[M->getModuleIdentifier()], ModuleMap)) {
1596     handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
1597       errs() << "Error running ThinLTO backend: " << EIB.message() << '\n';
1598     });
1599   }
1600 }
1601 
1602 void clang::EmitBackendOutput(DiagnosticsEngine &Diags,
1603                               const HeaderSearchOptions &HeaderOpts,
1604                               const CodeGenOptions &CGOpts,
1605                               const clang::TargetOptions &TOpts,
1606                               const LangOptions &LOpts,
1607                               const llvm::DataLayout &TDesc, Module *M,
1608                               BackendAction Action,
1609                               std::unique_ptr<raw_pwrite_stream> OS) {
1610 
1611   llvm::TimeTraceScope TimeScope("Backend");
1612 
1613   std::unique_ptr<llvm::Module> EmptyModule;
1614   if (!CGOpts.ThinLTOIndexFile.empty()) {
1615     // If we are performing a ThinLTO importing compile, load the function index
1616     // into memory and pass it into runThinLTOBackend, which will run the
1617     // function importer and invoke LTO passes.
1618     Expected<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr =
1619         llvm::getModuleSummaryIndexForFile(CGOpts.ThinLTOIndexFile,
1620                                            /*IgnoreEmptyThinLTOIndexFile*/true);
1621     if (!IndexOrErr) {
1622       logAllUnhandledErrors(IndexOrErr.takeError(), errs(),
1623                             "Error loading index file '" +
1624                             CGOpts.ThinLTOIndexFile + "': ");
1625       return;
1626     }
1627     std::unique_ptr<ModuleSummaryIndex> CombinedIndex = std::move(*IndexOrErr);
1628     // A null CombinedIndex means we should skip ThinLTO compilation
1629     // (LLVM will optionally ignore empty index files, returning null instead
1630     // of an error).
1631     if (CombinedIndex) {
1632       if (!CombinedIndex->skipModuleByDistributedBackend()) {
1633         runThinLTOBackend(CombinedIndex.get(), M, HeaderOpts, CGOpts, TOpts,
1634                           LOpts, std::move(OS), CGOpts.SampleProfileFile,
1635                           CGOpts.ProfileRemappingFile, Action);
1636         return;
1637       }
1638       // Distributed indexing detected that nothing from the module is needed
1639       // for the final linking. So we can skip the compilation. We sill need to
1640       // output an empty object file to make sure that a linker does not fail
1641       // trying to read it. Also for some features, like CFI, we must skip
1642       // the compilation as CombinedIndex does not contain all required
1643       // information.
1644       EmptyModule = std::make_unique<llvm::Module>("empty", M->getContext());
1645       EmptyModule->setTargetTriple(M->getTargetTriple());
1646       M = EmptyModule.get();
1647     }
1648   }
1649 
1650   EmitAssemblyHelper AsmHelper(Diags, HeaderOpts, CGOpts, TOpts, LOpts, M);
1651 
1652   if (CGOpts.ExperimentalNewPassManager)
1653     AsmHelper.EmitAssemblyWithNewPassManager(Action, std::move(OS));
1654   else
1655     AsmHelper.EmitAssembly(Action, std::move(OS));
1656 
1657   // Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's
1658   // DataLayout.
1659   if (AsmHelper.TM) {
1660     std::string DLDesc = M->getDataLayout().getStringRepresentation();
1661     if (DLDesc != TDesc.getStringRepresentation()) {
1662       unsigned DiagID = Diags.getCustomDiagID(
1663           DiagnosticsEngine::Error, "backend data layout '%0' does not match "
1664                                     "expected target description '%1'");
1665       Diags.Report(DiagID) << DLDesc << TDesc.getStringRepresentation();
1666     }
1667   }
1668 }
1669 
1670 // With -fembed-bitcode, save a copy of the llvm IR as data in the
1671 // __LLVM,__bitcode section.
1672 void clang::EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts,
1673                          llvm::MemoryBufferRef Buf) {
1674   if (CGOpts.getEmbedBitcode() == CodeGenOptions::Embed_Off)
1675     return;
1676   llvm::EmbedBitcodeInModule(
1677       *M, Buf, CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Marker,
1678       CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Bitcode,
1679       &CGOpts.CmdArgs);
1680 }
1681