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