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