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