xref: /freebsd/contrib/llvm-project/clang/lib/CodeGen/BackendUtil.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
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/Analysis/AliasAnalysis.h"
21 #include "llvm/Analysis/GlobalsModRef.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/DebugInfo.h"
32 #include "llvm/IR/LegacyPassManager.h"
33 #include "llvm/IR/Module.h"
34 #include "llvm/IR/ModuleSummaryIndex.h"
35 #include "llvm/IR/PassManager.h"
36 #include "llvm/IR/Verifier.h"
37 #include "llvm/IRPrinter/IRPrintingPasses.h"
38 #include "llvm/LTO/LTOBackend.h"
39 #include "llvm/MC/MCAsmInfo.h"
40 #include "llvm/MC/TargetRegistry.h"
41 #include "llvm/Object/OffloadBinary.h"
42 #include "llvm/Passes/PassBuilder.h"
43 #include "llvm/Passes/PassPlugin.h"
44 #include "llvm/Passes/StandardInstrumentations.h"
45 #include "llvm/Support/BuryPointer.h"
46 #include "llvm/Support/CommandLine.h"
47 #include "llvm/Support/MemoryBuffer.h"
48 #include "llvm/Support/PrettyStackTrace.h"
49 #include "llvm/Support/TimeProfiler.h"
50 #include "llvm/Support/Timer.h"
51 #include "llvm/Support/ToolOutputFile.h"
52 #include "llvm/Support/VirtualFileSystem.h"
53 #include "llvm/Support/raw_ostream.h"
54 #include "llvm/Target/TargetMachine.h"
55 #include "llvm/Target/TargetOptions.h"
56 #include "llvm/TargetParser/SubtargetFeature.h"
57 #include "llvm/TargetParser/Triple.h"
58 #include "llvm/Transforms/IPO/EmbedBitcodePass.h"
59 #include "llvm/Transforms/IPO/LowerTypeTests.h"
60 #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
61 #include "llvm/Transforms/InstCombine/InstCombine.h"
62 #include "llvm/Transforms/Instrumentation.h"
63 #include "llvm/Transforms/Instrumentation/AddressSanitizer.h"
64 #include "llvm/Transforms/Instrumentation/AddressSanitizerOptions.h"
65 #include "llvm/Transforms/Instrumentation/BoundsChecking.h"
66 #include "llvm/Transforms/Instrumentation/DataFlowSanitizer.h"
67 #include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
68 #include "llvm/Transforms/Instrumentation/HWAddressSanitizer.h"
69 #include "llvm/Transforms/Instrumentation/InstrProfiling.h"
70 #include "llvm/Transforms/Instrumentation/KCFI.h"
71 #include "llvm/Transforms/Instrumentation/MemProfiler.h"
72 #include "llvm/Transforms/Instrumentation/MemorySanitizer.h"
73 #include "llvm/Transforms/Instrumentation/SanitizerBinaryMetadata.h"
74 #include "llvm/Transforms/Instrumentation/SanitizerCoverage.h"
75 #include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
76 #include "llvm/Transforms/ObjCARC.h"
77 #include "llvm/Transforms/Scalar/EarlyCSE.h"
78 #include "llvm/Transforms/Scalar/GVN.h"
79 #include "llvm/Transforms/Scalar/JumpThreading.h"
80 #include "llvm/Transforms/Utils/Debugify.h"
81 #include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
82 #include "llvm/Transforms/Utils/ModuleUtils.h"
83 #include <memory>
84 #include <optional>
85 using namespace clang;
86 using namespace llvm;
87 
88 #define HANDLE_EXTENSION(Ext)                                                  \
89   llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
90 #include "llvm/Support/Extension.def"
91 
92 namespace llvm {
93 extern cl::opt<bool> DebugInfoCorrelate;
94 
95 // Experiment to move sanitizers earlier.
96 static cl::opt<bool> ClSanitizeOnOptimizerEarlyEP(
97     "sanitizer-early-opt-ep", cl::Optional,
98     cl::desc("Insert sanitizers on OptimizerEarlyEP."), cl::init(false));
99 }
100 
101 namespace {
102 
103 // Default filename used for profile generation.
104 std::string getDefaultProfileGenName() {
105   return DebugInfoCorrelate ? "default_%p.proflite" : "default_%m.profraw";
106 }
107 
108 class EmitAssemblyHelper {
109   DiagnosticsEngine &Diags;
110   const HeaderSearchOptions &HSOpts;
111   const CodeGenOptions &CodeGenOpts;
112   const clang::TargetOptions &TargetOpts;
113   const LangOptions &LangOpts;
114   Module *TheModule;
115   IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS;
116 
117   Timer CodeGenerationTime;
118 
119   std::unique_ptr<raw_pwrite_stream> OS;
120 
121   Triple TargetTriple;
122 
123   TargetIRAnalysis getTargetIRAnalysis() const {
124     if (TM)
125       return TM->getTargetIRAnalysis();
126 
127     return TargetIRAnalysis();
128   }
129 
130   /// Generates the TargetMachine.
131   /// Leaves TM unchanged if it is unable to create the target machine.
132   /// Some of our clang tests specify triples which are not built
133   /// into clang. This is okay because these tests check the generated
134   /// IR, and they require DataLayout which depends on the triple.
135   /// In this case, we allow this method to fail and not report an error.
136   /// When MustCreateTM is used, we print an error if we are unable to load
137   /// the requested target.
138   void CreateTargetMachine(bool MustCreateTM);
139 
140   /// Add passes necessary to emit assembly or LLVM IR.
141   ///
142   /// \return True on success.
143   bool AddEmitPasses(legacy::PassManager &CodeGenPasses, BackendAction Action,
144                      raw_pwrite_stream &OS, raw_pwrite_stream *DwoOS);
145 
146   std::unique_ptr<llvm::ToolOutputFile> openOutputFile(StringRef Path) {
147     std::error_code EC;
148     auto F = std::make_unique<llvm::ToolOutputFile>(Path, EC,
149                                                      llvm::sys::fs::OF_None);
150     if (EC) {
151       Diags.Report(diag::err_fe_unable_to_open_output) << Path << EC.message();
152       F.reset();
153     }
154     return F;
155   }
156 
157   void
158   RunOptimizationPipeline(BackendAction Action,
159                           std::unique_ptr<raw_pwrite_stream> &OS,
160                           std::unique_ptr<llvm::ToolOutputFile> &ThinLinkOS);
161   void RunCodegenPipeline(BackendAction Action,
162                           std::unique_ptr<raw_pwrite_stream> &OS,
163                           std::unique_ptr<llvm::ToolOutputFile> &DwoOS);
164 
165   /// Check whether we should emit a module summary for regular LTO.
166   /// The module summary should be emitted by default for regular LTO
167   /// except for ld64 targets.
168   ///
169   /// \return True if the module summary should be emitted.
170   bool shouldEmitRegularLTOSummary() const {
171     return CodeGenOpts.PrepareForLTO && !CodeGenOpts.DisableLLVMPasses &&
172            TargetTriple.getVendor() != llvm::Triple::Apple;
173   }
174 
175 public:
176   EmitAssemblyHelper(DiagnosticsEngine &_Diags,
177                      const HeaderSearchOptions &HeaderSearchOpts,
178                      const CodeGenOptions &CGOpts,
179                      const clang::TargetOptions &TOpts,
180                      const LangOptions &LOpts, Module *M,
181                      IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS)
182       : Diags(_Diags), HSOpts(HeaderSearchOpts), CodeGenOpts(CGOpts),
183         TargetOpts(TOpts), LangOpts(LOpts), TheModule(M), VFS(std::move(VFS)),
184         CodeGenerationTime("codegen", "Code Generation Time"),
185         TargetTriple(TheModule->getTargetTriple()) {}
186 
187   ~EmitAssemblyHelper() {
188     if (CodeGenOpts.DisableFree)
189       BuryPointer(std::move(TM));
190   }
191 
192   std::unique_ptr<TargetMachine> TM;
193 
194   // Emit output using the new pass manager for the optimization pipeline.
195   void EmitAssembly(BackendAction Action,
196                     std::unique_ptr<raw_pwrite_stream> OS);
197 };
198 }
199 
200 static SanitizerCoverageOptions
201 getSancovOptsFromCGOpts(const CodeGenOptions &CGOpts) {
202   SanitizerCoverageOptions Opts;
203   Opts.CoverageType =
204       static_cast<SanitizerCoverageOptions::Type>(CGOpts.SanitizeCoverageType);
205   Opts.IndirectCalls = CGOpts.SanitizeCoverageIndirectCalls;
206   Opts.TraceBB = CGOpts.SanitizeCoverageTraceBB;
207   Opts.TraceCmp = CGOpts.SanitizeCoverageTraceCmp;
208   Opts.TraceDiv = CGOpts.SanitizeCoverageTraceDiv;
209   Opts.TraceGep = CGOpts.SanitizeCoverageTraceGep;
210   Opts.Use8bitCounters = CGOpts.SanitizeCoverage8bitCounters;
211   Opts.TracePC = CGOpts.SanitizeCoverageTracePC;
212   Opts.TracePCGuard = CGOpts.SanitizeCoverageTracePCGuard;
213   Opts.NoPrune = CGOpts.SanitizeCoverageNoPrune;
214   Opts.Inline8bitCounters = CGOpts.SanitizeCoverageInline8bitCounters;
215   Opts.InlineBoolFlag = CGOpts.SanitizeCoverageInlineBoolFlag;
216   Opts.PCTable = CGOpts.SanitizeCoveragePCTable;
217   Opts.StackDepth = CGOpts.SanitizeCoverageStackDepth;
218   Opts.TraceLoads = CGOpts.SanitizeCoverageTraceLoads;
219   Opts.TraceStores = CGOpts.SanitizeCoverageTraceStores;
220   Opts.CollectControlFlow = CGOpts.SanitizeCoverageControlFlow;
221   return Opts;
222 }
223 
224 static SanitizerBinaryMetadataOptions
225 getSanitizerBinaryMetadataOptions(const CodeGenOptions &CGOpts) {
226   SanitizerBinaryMetadataOptions Opts;
227   Opts.Covered = CGOpts.SanitizeBinaryMetadataCovered;
228   Opts.Atomics = CGOpts.SanitizeBinaryMetadataAtomics;
229   Opts.UAR = CGOpts.SanitizeBinaryMetadataUAR;
230   return Opts;
231 }
232 
233 // Check if ASan should use GC-friendly instrumentation for globals.
234 // First of all, there is no point if -fdata-sections is off (expect for MachO,
235 // where this is not a factor). Also, on ELF this feature requires an assembler
236 // extension that only works with -integrated-as at the moment.
237 static bool asanUseGlobalsGC(const Triple &T, const CodeGenOptions &CGOpts) {
238   if (!CGOpts.SanitizeAddressGlobalsDeadStripping)
239     return false;
240   switch (T.getObjectFormat()) {
241   case Triple::MachO:
242   case Triple::COFF:
243     return true;
244   case Triple::ELF:
245     return !CGOpts.DisableIntegratedAS;
246   case Triple::GOFF:
247     llvm::report_fatal_error("ASan not implemented for GOFF");
248   case Triple::XCOFF:
249     llvm::report_fatal_error("ASan not implemented for XCOFF.");
250   case Triple::Wasm:
251   case Triple::DXContainer:
252   case Triple::SPIRV:
253   case Triple::UnknownObjectFormat:
254     break;
255   }
256   return false;
257 }
258 
259 static TargetLibraryInfoImpl *createTLII(llvm::Triple &TargetTriple,
260                                          const CodeGenOptions &CodeGenOpts) {
261   TargetLibraryInfoImpl *TLII = new TargetLibraryInfoImpl(TargetTriple);
262 
263   switch (CodeGenOpts.getVecLib()) {
264   case CodeGenOptions::Accelerate:
265     TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::Accelerate,
266                                              TargetTriple);
267     break;
268   case CodeGenOptions::LIBMVEC:
269     TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::LIBMVEC_X86,
270                                              TargetTriple);
271     break;
272   case CodeGenOptions::MASSV:
273     TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::MASSV,
274                                              TargetTriple);
275     break;
276   case CodeGenOptions::SVML:
277     TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::SVML,
278                                              TargetTriple);
279     break;
280   case CodeGenOptions::SLEEF:
281     TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::SLEEFGNUABI,
282                                              TargetTriple);
283     break;
284   case CodeGenOptions::Darwin_libsystem_m:
285     TLII->addVectorizableFunctionsFromVecLib(
286         TargetLibraryInfoImpl::DarwinLibSystemM, TargetTriple);
287     break;
288   case CodeGenOptions::ArmPL:
289     TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::ArmPL,
290                                              TargetTriple);
291     break;
292   default:
293     break;
294   }
295   return TLII;
296 }
297 
298 static std::optional<llvm::CodeModel::Model>
299 getCodeModel(const CodeGenOptions &CodeGenOpts) {
300   unsigned CodeModel = llvm::StringSwitch<unsigned>(CodeGenOpts.CodeModel)
301                            .Case("tiny", llvm::CodeModel::Tiny)
302                            .Case("small", llvm::CodeModel::Small)
303                            .Case("kernel", llvm::CodeModel::Kernel)
304                            .Case("medium", llvm::CodeModel::Medium)
305                            .Case("large", llvm::CodeModel::Large)
306                            .Case("default", ~1u)
307                            .Default(~0u);
308   assert(CodeModel != ~0u && "invalid code model!");
309   if (CodeModel == ~1u)
310     return std::nullopt;
311   return static_cast<llvm::CodeModel::Model>(CodeModel);
312 }
313 
314 static CodeGenFileType getCodeGenFileType(BackendAction Action) {
315   if (Action == Backend_EmitObj)
316     return CGFT_ObjectFile;
317   else if (Action == Backend_EmitMCNull)
318     return CGFT_Null;
319   else {
320     assert(Action == Backend_EmitAssembly && "Invalid action!");
321     return CGFT_AssemblyFile;
322   }
323 }
324 
325 static bool actionRequiresCodeGen(BackendAction Action) {
326   return Action != Backend_EmitNothing && Action != Backend_EmitBC &&
327          Action != Backend_EmitLL;
328 }
329 
330 static bool initTargetOptions(DiagnosticsEngine &Diags,
331                               llvm::TargetOptions &Options,
332                               const CodeGenOptions &CodeGenOpts,
333                               const clang::TargetOptions &TargetOpts,
334                               const LangOptions &LangOpts,
335                               const HeaderSearchOptions &HSOpts) {
336   switch (LangOpts.getThreadModel()) {
337   case LangOptions::ThreadModelKind::POSIX:
338     Options.ThreadModel = llvm::ThreadModel::POSIX;
339     break;
340   case LangOptions::ThreadModelKind::Single:
341     Options.ThreadModel = llvm::ThreadModel::Single;
342     break;
343   }
344 
345   // Set float ABI type.
346   assert((CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp" ||
347           CodeGenOpts.FloatABI == "hard" || CodeGenOpts.FloatABI.empty()) &&
348          "Invalid Floating Point ABI!");
349   Options.FloatABIType =
350       llvm::StringSwitch<llvm::FloatABI::ABIType>(CodeGenOpts.FloatABI)
351           .Case("soft", llvm::FloatABI::Soft)
352           .Case("softfp", llvm::FloatABI::Soft)
353           .Case("hard", llvm::FloatABI::Hard)
354           .Default(llvm::FloatABI::Default);
355 
356   // Set FP fusion mode.
357   switch (LangOpts.getDefaultFPContractMode()) {
358   case LangOptions::FPM_Off:
359     // Preserve any contraction performed by the front-end.  (Strict performs
360     // splitting of the muladd intrinsic in the backend.)
361     Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
362     break;
363   case LangOptions::FPM_On:
364   case LangOptions::FPM_FastHonorPragmas:
365     Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
366     break;
367   case LangOptions::FPM_Fast:
368     Options.AllowFPOpFusion = llvm::FPOpFusion::Fast;
369     break;
370   }
371 
372   Options.BinutilsVersion =
373       llvm::TargetMachine::parseBinutilsVersion(CodeGenOpts.BinutilsVersion);
374   Options.UseInitArray = CodeGenOpts.UseInitArray;
375   Options.DisableIntegratedAS = CodeGenOpts.DisableIntegratedAS;
376   Options.CompressDebugSections = CodeGenOpts.getCompressDebugSections();
377   Options.RelaxELFRelocations = CodeGenOpts.RelaxELFRelocations;
378 
379   // Set EABI version.
380   Options.EABIVersion = TargetOpts.EABIVersion;
381 
382   if (LangOpts.hasSjLjExceptions())
383     Options.ExceptionModel = llvm::ExceptionHandling::SjLj;
384   if (LangOpts.hasSEHExceptions())
385     Options.ExceptionModel = llvm::ExceptionHandling::WinEH;
386   if (LangOpts.hasDWARFExceptions())
387     Options.ExceptionModel = llvm::ExceptionHandling::DwarfCFI;
388   if (LangOpts.hasWasmExceptions())
389     Options.ExceptionModel = llvm::ExceptionHandling::Wasm;
390 
391   Options.NoInfsFPMath = LangOpts.NoHonorInfs;
392   Options.NoNaNsFPMath = LangOpts.NoHonorNaNs;
393   Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS;
394   Options.UnsafeFPMath = LangOpts.AllowFPReassoc && LangOpts.AllowRecip &&
395                          LangOpts.NoSignedZero && LangOpts.ApproxFunc &&
396                          (LangOpts.getDefaultFPContractMode() ==
397                               LangOptions::FPModeKind::FPM_Fast ||
398                           LangOpts.getDefaultFPContractMode() ==
399                               LangOptions::FPModeKind::FPM_FastHonorPragmas);
400   Options.ApproxFuncFPMath = LangOpts.ApproxFunc;
401 
402   Options.BBSections =
403       llvm::StringSwitch<llvm::BasicBlockSection>(CodeGenOpts.BBSections)
404           .Case("all", llvm::BasicBlockSection::All)
405           .Case("labels", llvm::BasicBlockSection::Labels)
406           .StartsWith("list=", llvm::BasicBlockSection::List)
407           .Case("none", llvm::BasicBlockSection::None)
408           .Default(llvm::BasicBlockSection::None);
409 
410   if (Options.BBSections == llvm::BasicBlockSection::List) {
411     ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
412         MemoryBuffer::getFile(CodeGenOpts.BBSections.substr(5));
413     if (!MBOrErr) {
414       Diags.Report(diag::err_fe_unable_to_load_basic_block_sections_file)
415           << MBOrErr.getError().message();
416       return false;
417     }
418     Options.BBSectionsFuncListBuf = std::move(*MBOrErr);
419   }
420 
421   Options.EnableMachineFunctionSplitter = CodeGenOpts.SplitMachineFunctions;
422   Options.FunctionSections = CodeGenOpts.FunctionSections;
423   Options.DataSections = CodeGenOpts.DataSections;
424   Options.IgnoreXCOFFVisibility = LangOpts.IgnoreXCOFFVisibility;
425   Options.UniqueSectionNames = CodeGenOpts.UniqueSectionNames;
426   Options.UniqueBasicBlockSectionNames =
427       CodeGenOpts.UniqueBasicBlockSectionNames;
428   Options.TLSSize = CodeGenOpts.TLSSize;
429   Options.EmulatedTLS = CodeGenOpts.EmulatedTLS;
430   Options.DebuggerTuning = CodeGenOpts.getDebuggerTuning();
431   Options.EmitStackSizeSection = CodeGenOpts.StackSizeSection;
432   Options.StackUsageOutput = CodeGenOpts.StackUsageOutput;
433   Options.EmitAddrsig = CodeGenOpts.Addrsig;
434   Options.ForceDwarfFrameSection = CodeGenOpts.ForceDwarfFrameSection;
435   Options.EmitCallSiteInfo = CodeGenOpts.EmitCallSiteInfo;
436   Options.EnableAIXExtendedAltivecABI = LangOpts.EnableAIXExtendedAltivecABI;
437   Options.XRayFunctionIndex = CodeGenOpts.XRayFunctionIndex;
438   Options.LoopAlignment = CodeGenOpts.LoopAlignment;
439   Options.DebugStrictDwarf = CodeGenOpts.DebugStrictDwarf;
440   Options.ObjectFilenameForDebug = CodeGenOpts.ObjectFilenameForDebug;
441   Options.Hotpatch = CodeGenOpts.HotPatch;
442   Options.JMCInstrument = CodeGenOpts.JMCInstrument;
443   Options.XCOFFReadOnlyPointers = CodeGenOpts.XCOFFReadOnlyPointers;
444 
445   switch (CodeGenOpts.getSwiftAsyncFramePointer()) {
446   case CodeGenOptions::SwiftAsyncFramePointerKind::Auto:
447     Options.SwiftAsyncFramePointer =
448         SwiftAsyncFramePointerMode::DeploymentBased;
449     break;
450 
451   case CodeGenOptions::SwiftAsyncFramePointerKind::Always:
452     Options.SwiftAsyncFramePointer = SwiftAsyncFramePointerMode::Always;
453     break;
454 
455   case CodeGenOptions::SwiftAsyncFramePointerKind::Never:
456     Options.SwiftAsyncFramePointer = SwiftAsyncFramePointerMode::Never;
457     break;
458   }
459 
460   Options.MCOptions.SplitDwarfFile = CodeGenOpts.SplitDwarfFile;
461   Options.MCOptions.EmitDwarfUnwind = CodeGenOpts.getEmitDwarfUnwind();
462   Options.MCOptions.EmitCompactUnwindNonCanonical =
463       CodeGenOpts.EmitCompactUnwindNonCanonical;
464   Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll;
465   Options.MCOptions.MCSaveTempLabels = CodeGenOpts.SaveTempLabels;
466   Options.MCOptions.MCUseDwarfDirectory =
467       CodeGenOpts.NoDwarfDirectoryAsm
468           ? llvm::MCTargetOptions::DisableDwarfDirectory
469           : llvm::MCTargetOptions::EnableDwarfDirectory;
470   Options.MCOptions.MCNoExecStack = CodeGenOpts.NoExecStack;
471   Options.MCOptions.MCIncrementalLinkerCompatible =
472       CodeGenOpts.IncrementalLinkerCompatible;
473   Options.MCOptions.MCFatalWarnings = CodeGenOpts.FatalWarnings;
474   Options.MCOptions.MCNoWarn = CodeGenOpts.NoWarn;
475   Options.MCOptions.AsmVerbose = CodeGenOpts.AsmVerbose;
476   Options.MCOptions.Dwarf64 = CodeGenOpts.Dwarf64;
477   Options.MCOptions.PreserveAsmComments = CodeGenOpts.PreserveAsmComments;
478   Options.MCOptions.ABIName = TargetOpts.ABI;
479   for (const auto &Entry : HSOpts.UserEntries)
480     if (!Entry.IsFramework &&
481         (Entry.Group == frontend::IncludeDirGroup::Quoted ||
482          Entry.Group == frontend::IncludeDirGroup::Angled ||
483          Entry.Group == frontend::IncludeDirGroup::System))
484       Options.MCOptions.IASSearchPaths.push_back(
485           Entry.IgnoreSysRoot ? Entry.Path : HSOpts.Sysroot + Entry.Path);
486   Options.MCOptions.Argv0 = CodeGenOpts.Argv0;
487   Options.MCOptions.CommandLineArgs = CodeGenOpts.CommandLineArgs;
488   Options.MCOptions.AsSecureLogFile = CodeGenOpts.AsSecureLogFile;
489   Options.MisExpect = CodeGenOpts.MisExpect;
490 
491   return true;
492 }
493 
494 static std::optional<GCOVOptions>
495 getGCOVOptions(const CodeGenOptions &CodeGenOpts, const LangOptions &LangOpts) {
496   if (CodeGenOpts.CoverageNotesFile.empty() &&
497       CodeGenOpts.CoverageDataFile.empty())
498     return std::nullopt;
499   // Not using 'GCOVOptions::getDefault' allows us to avoid exiting if
500   // LLVM's -default-gcov-version flag is set to something invalid.
501   GCOVOptions Options;
502   Options.EmitNotes = !CodeGenOpts.CoverageNotesFile.empty();
503   Options.EmitData = !CodeGenOpts.CoverageDataFile.empty();
504   llvm::copy(CodeGenOpts.CoverageVersion, std::begin(Options.Version));
505   Options.NoRedZone = CodeGenOpts.DisableRedZone;
506   Options.Filter = CodeGenOpts.ProfileFilterFiles;
507   Options.Exclude = CodeGenOpts.ProfileExcludeFiles;
508   Options.Atomic = CodeGenOpts.AtomicProfileUpdate;
509   return Options;
510 }
511 
512 static std::optional<InstrProfOptions>
513 getInstrProfOptions(const CodeGenOptions &CodeGenOpts,
514                     const LangOptions &LangOpts) {
515   if (!CodeGenOpts.hasProfileClangInstr())
516     return std::nullopt;
517   InstrProfOptions Options;
518   Options.NoRedZone = CodeGenOpts.DisableRedZone;
519   Options.InstrProfileOutput = CodeGenOpts.InstrProfileOutput;
520   Options.Atomic = CodeGenOpts.AtomicProfileUpdate;
521   return Options;
522 }
523 
524 static void setCommandLineOpts(const CodeGenOptions &CodeGenOpts) {
525   SmallVector<const char *, 16> BackendArgs;
526   BackendArgs.push_back("clang"); // Fake program name.
527   if (!CodeGenOpts.DebugPass.empty()) {
528     BackendArgs.push_back("-debug-pass");
529     BackendArgs.push_back(CodeGenOpts.DebugPass.c_str());
530   }
531   if (!CodeGenOpts.LimitFloatPrecision.empty()) {
532     BackendArgs.push_back("-limit-float-precision");
533     BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str());
534   }
535   // Check for the default "clang" invocation that won't set any cl::opt values.
536   // Skip trying to parse the command line invocation to avoid the issues
537   // described below.
538   if (BackendArgs.size() == 1)
539     return;
540   BackendArgs.push_back(nullptr);
541   // FIXME: The command line parser below is not thread-safe and shares a global
542   // state, so this call might crash or overwrite the options of another Clang
543   // instance in the same process.
544   llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
545                                     BackendArgs.data());
546 }
547 
548 void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
549   // Create the TargetMachine for generating code.
550   std::string Error;
551   std::string Triple = TheModule->getTargetTriple();
552   const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
553   if (!TheTarget) {
554     if (MustCreateTM)
555       Diags.Report(diag::err_fe_unable_to_create_target) << Error;
556     return;
557   }
558 
559   std::optional<llvm::CodeModel::Model> CM = getCodeModel(CodeGenOpts);
560   std::string FeaturesStr =
561       llvm::join(TargetOpts.Features.begin(), TargetOpts.Features.end(), ",");
562   llvm::Reloc::Model RM = CodeGenOpts.RelocationModel;
563   std::optional<CodeGenOpt::Level> OptLevelOrNone =
564       CodeGenOpt::getLevel(CodeGenOpts.OptimizationLevel);
565   assert(OptLevelOrNone && "Invalid optimization level!");
566   CodeGenOpt::Level OptLevel = *OptLevelOrNone;
567 
568   llvm::TargetOptions Options;
569   if (!initTargetOptions(Diags, Options, CodeGenOpts, TargetOpts, LangOpts,
570                          HSOpts))
571     return;
572   TM.reset(TheTarget->createTargetMachine(Triple, TargetOpts.CPU, FeaturesStr,
573                                           Options, RM, CM, OptLevel));
574 }
575 
576 bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager &CodeGenPasses,
577                                        BackendAction Action,
578                                        raw_pwrite_stream &OS,
579                                        raw_pwrite_stream *DwoOS) {
580   // Add LibraryInfo.
581   std::unique_ptr<TargetLibraryInfoImpl> TLII(
582       createTLII(TargetTriple, CodeGenOpts));
583   CodeGenPasses.add(new TargetLibraryInfoWrapperPass(*TLII));
584 
585   // Normal mode, emit a .s or .o file by running the code generator. Note,
586   // this also adds codegenerator level optimization passes.
587   CodeGenFileType CGFT = getCodeGenFileType(Action);
588 
589   // Add ObjC ARC final-cleanup optimizations. This is done as part of the
590   // "codegen" passes so that it isn't run multiple times when there is
591   // inlining happening.
592   if (CodeGenOpts.OptimizationLevel > 0)
593     CodeGenPasses.add(createObjCARCContractPass());
594 
595   if (TM->addPassesToEmitFile(CodeGenPasses, OS, DwoOS, CGFT,
596                               /*DisableVerify=*/!CodeGenOpts.VerifyModule)) {
597     Diags.Report(diag::err_fe_unable_to_interface_with_target);
598     return false;
599   }
600 
601   return true;
602 }
603 
604 static OptimizationLevel mapToLevel(const CodeGenOptions &Opts) {
605   switch (Opts.OptimizationLevel) {
606   default:
607     llvm_unreachable("Invalid optimization level!");
608 
609   case 0:
610     return OptimizationLevel::O0;
611 
612   case 1:
613     return OptimizationLevel::O1;
614 
615   case 2:
616     switch (Opts.OptimizeSize) {
617     default:
618       llvm_unreachable("Invalid optimization level for size!");
619 
620     case 0:
621       return OptimizationLevel::O2;
622 
623     case 1:
624       return OptimizationLevel::Os;
625 
626     case 2:
627       return OptimizationLevel::Oz;
628     }
629 
630   case 3:
631     return OptimizationLevel::O3;
632   }
633 }
634 
635 static void addKCFIPass(const Triple &TargetTriple, const LangOptions &LangOpts,
636                         PassBuilder &PB) {
637   // If the back-end supports KCFI operand bundle lowering, skip KCFIPass.
638   if (TargetTriple.getArch() == llvm::Triple::x86_64 ||
639       TargetTriple.isAArch64(64) || TargetTriple.isRISCV())
640     return;
641 
642   // Ensure we lower KCFI operand bundles with -O0.
643   PB.registerOptimizerLastEPCallback(
644       [&](ModulePassManager &MPM, OptimizationLevel Level) {
645         if (Level == OptimizationLevel::O0 &&
646             LangOpts.Sanitize.has(SanitizerKind::KCFI))
647           MPM.addPass(createModuleToFunctionPassAdaptor(KCFIPass()));
648       });
649 
650   // When optimizations are requested, run KCIFPass after InstCombine to
651   // avoid unnecessary checks.
652   PB.registerPeepholeEPCallback(
653       [&](FunctionPassManager &FPM, OptimizationLevel Level) {
654         if (Level != OptimizationLevel::O0 &&
655             LangOpts.Sanitize.has(SanitizerKind::KCFI))
656           FPM.addPass(KCFIPass());
657       });
658 }
659 
660 static void addSanitizers(const Triple &TargetTriple,
661                           const CodeGenOptions &CodeGenOpts,
662                           const LangOptions &LangOpts, PassBuilder &PB) {
663   auto SanitizersCallback = [&](ModulePassManager &MPM,
664                                 OptimizationLevel Level) {
665     if (CodeGenOpts.hasSanitizeCoverage()) {
666       auto SancovOpts = getSancovOptsFromCGOpts(CodeGenOpts);
667       MPM.addPass(SanitizerCoveragePass(
668           SancovOpts, CodeGenOpts.SanitizeCoverageAllowlistFiles,
669           CodeGenOpts.SanitizeCoverageIgnorelistFiles));
670     }
671 
672     if (CodeGenOpts.hasSanitizeBinaryMetadata()) {
673       MPM.addPass(SanitizerBinaryMetadataPass(
674           getSanitizerBinaryMetadataOptions(CodeGenOpts),
675           CodeGenOpts.SanitizeMetadataIgnorelistFiles));
676     }
677 
678     auto MSanPass = [&](SanitizerMask Mask, bool CompileKernel) {
679       if (LangOpts.Sanitize.has(Mask)) {
680         int TrackOrigins = CodeGenOpts.SanitizeMemoryTrackOrigins;
681         bool Recover = CodeGenOpts.SanitizeRecover.has(Mask);
682 
683         MemorySanitizerOptions options(TrackOrigins, Recover, CompileKernel,
684                                        CodeGenOpts.SanitizeMemoryParamRetval);
685         MPM.addPass(MemorySanitizerPass(options));
686         if (Level != OptimizationLevel::O0) {
687           // MemorySanitizer inserts complex instrumentation that mostly follows
688           // the logic of the original code, but operates on "shadow" values. It
689           // can benefit from re-running some general purpose optimization
690           // passes.
691           MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
692           FunctionPassManager FPM;
693           FPM.addPass(EarlyCSEPass(true /* Enable mem-ssa. */));
694           FPM.addPass(InstCombinePass());
695           FPM.addPass(JumpThreadingPass());
696           FPM.addPass(GVNPass());
697           FPM.addPass(InstCombinePass());
698           MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
699         }
700       }
701     };
702     MSanPass(SanitizerKind::Memory, false);
703     MSanPass(SanitizerKind::KernelMemory, true);
704 
705     if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
706       MPM.addPass(ModuleThreadSanitizerPass());
707       MPM.addPass(createModuleToFunctionPassAdaptor(ThreadSanitizerPass()));
708     }
709 
710     auto ASanPass = [&](SanitizerMask Mask, bool CompileKernel) {
711       if (LangOpts.Sanitize.has(Mask)) {
712         bool UseGlobalGC = asanUseGlobalsGC(TargetTriple, CodeGenOpts);
713         bool UseOdrIndicator = CodeGenOpts.SanitizeAddressUseOdrIndicator;
714         llvm::AsanDtorKind DestructorKind =
715             CodeGenOpts.getSanitizeAddressDtor();
716         AddressSanitizerOptions Opts;
717         Opts.CompileKernel = CompileKernel;
718         Opts.Recover = CodeGenOpts.SanitizeRecover.has(Mask);
719         Opts.UseAfterScope = CodeGenOpts.SanitizeAddressUseAfterScope;
720         Opts.UseAfterReturn = CodeGenOpts.getSanitizeAddressUseAfterReturn();
721         MPM.addPass(AddressSanitizerPass(Opts, UseGlobalGC, UseOdrIndicator,
722                                          DestructorKind));
723       }
724     };
725     ASanPass(SanitizerKind::Address, false);
726     ASanPass(SanitizerKind::KernelAddress, true);
727 
728     auto HWASanPass = [&](SanitizerMask Mask, bool CompileKernel) {
729       if (LangOpts.Sanitize.has(Mask)) {
730         bool Recover = CodeGenOpts.SanitizeRecover.has(Mask);
731         MPM.addPass(HWAddressSanitizerPass(
732             {CompileKernel, Recover,
733              /*DisableOptimization=*/CodeGenOpts.OptimizationLevel == 0}));
734       }
735     };
736     HWASanPass(SanitizerKind::HWAddress, false);
737     HWASanPass(SanitizerKind::KernelHWAddress, true);
738 
739     if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) {
740       MPM.addPass(DataFlowSanitizerPass(LangOpts.NoSanitizeFiles));
741     }
742   };
743   if (ClSanitizeOnOptimizerEarlyEP) {
744     PB.registerOptimizerEarlyEPCallback(
745         [SanitizersCallback](ModulePassManager &MPM, OptimizationLevel Level) {
746           ModulePassManager NewMPM;
747           SanitizersCallback(NewMPM, Level);
748           if (!NewMPM.isEmpty()) {
749             // Sanitizers can abandon<GlobalsAA>.
750             NewMPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
751             MPM.addPass(std::move(NewMPM));
752           }
753         });
754   } else {
755     // LastEP does not need GlobalsAA.
756     PB.registerOptimizerLastEPCallback(SanitizersCallback);
757   }
758 }
759 
760 void EmitAssemblyHelper::RunOptimizationPipeline(
761     BackendAction Action, std::unique_ptr<raw_pwrite_stream> &OS,
762     std::unique_ptr<llvm::ToolOutputFile> &ThinLinkOS) {
763   std::optional<PGOOptions> PGOOpt;
764 
765   if (CodeGenOpts.hasProfileIRInstr())
766     // -fprofile-generate.
767     PGOOpt = PGOOptions(
768         CodeGenOpts.InstrProfileOutput.empty() ? getDefaultProfileGenName()
769                                                : CodeGenOpts.InstrProfileOutput,
770         "", "", CodeGenOpts.MemoryProfileUsePath, nullptr, PGOOptions::IRInstr,
771         PGOOptions::NoCSAction, CodeGenOpts.DebugInfoForProfiling);
772   else if (CodeGenOpts.hasProfileIRUse()) {
773     // -fprofile-use.
774     auto CSAction = CodeGenOpts.hasProfileCSIRUse() ? PGOOptions::CSIRUse
775                                                     : PGOOptions::NoCSAction;
776     PGOOpt = PGOOptions(
777         CodeGenOpts.ProfileInstrumentUsePath, "",
778         CodeGenOpts.ProfileRemappingFile, CodeGenOpts.MemoryProfileUsePath, VFS,
779         PGOOptions::IRUse, CSAction, CodeGenOpts.DebugInfoForProfiling);
780   } else if (!CodeGenOpts.SampleProfileFile.empty())
781     // -fprofile-sample-use
782     PGOOpt = PGOOptions(
783         CodeGenOpts.SampleProfileFile, "", CodeGenOpts.ProfileRemappingFile,
784         CodeGenOpts.MemoryProfileUsePath, VFS, PGOOptions::SampleUse,
785         PGOOptions::NoCSAction, CodeGenOpts.DebugInfoForProfiling,
786         CodeGenOpts.PseudoProbeForProfiling);
787   else if (!CodeGenOpts.MemoryProfileUsePath.empty())
788     // -fmemory-profile-use (without any of the above options)
789     PGOOpt = PGOOptions("", "", "", CodeGenOpts.MemoryProfileUsePath, VFS,
790                         PGOOptions::NoAction, PGOOptions::NoCSAction,
791                         CodeGenOpts.DebugInfoForProfiling);
792   else if (CodeGenOpts.PseudoProbeForProfiling)
793     // -fpseudo-probe-for-profiling
794     PGOOpt = PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr,
795                         PGOOptions::NoAction, PGOOptions::NoCSAction,
796                         CodeGenOpts.DebugInfoForProfiling, true);
797   else if (CodeGenOpts.DebugInfoForProfiling)
798     // -fdebug-info-for-profiling
799     PGOOpt = PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr,
800                         PGOOptions::NoAction, PGOOptions::NoCSAction, true);
801 
802   // Check to see if we want to generate a CS profile.
803   if (CodeGenOpts.hasProfileCSIRInstr()) {
804     assert(!CodeGenOpts.hasProfileCSIRUse() &&
805            "Cannot have both CSProfileUse pass and CSProfileGen pass at "
806            "the same time");
807     if (PGOOpt) {
808       assert(PGOOpt->Action != PGOOptions::IRInstr &&
809              PGOOpt->Action != PGOOptions::SampleUse &&
810              "Cannot run CSProfileGen pass with ProfileGen or SampleUse "
811              " pass");
812       PGOOpt->CSProfileGenFile = CodeGenOpts.InstrProfileOutput.empty()
813                                      ? getDefaultProfileGenName()
814                                      : CodeGenOpts.InstrProfileOutput;
815       PGOOpt->CSAction = PGOOptions::CSIRInstr;
816     } else
817       PGOOpt =
818           PGOOptions("",
819                      CodeGenOpts.InstrProfileOutput.empty()
820                          ? getDefaultProfileGenName()
821                          : CodeGenOpts.InstrProfileOutput,
822                      "", /*MemoryProfile=*/"", nullptr, PGOOptions::NoAction,
823                      PGOOptions::CSIRInstr, CodeGenOpts.DebugInfoForProfiling);
824   }
825   if (TM)
826     TM->setPGOOption(PGOOpt);
827 
828   PipelineTuningOptions PTO;
829   PTO.LoopUnrolling = CodeGenOpts.UnrollLoops;
830   // For historical reasons, loop interleaving is set to mirror setting for loop
831   // unrolling.
832   PTO.LoopInterleaving = CodeGenOpts.UnrollLoops;
833   PTO.LoopVectorization = CodeGenOpts.VectorizeLoop;
834   PTO.SLPVectorization = CodeGenOpts.VectorizeSLP;
835   PTO.MergeFunctions = CodeGenOpts.MergeFunctions;
836   // Only enable CGProfilePass when using integrated assembler, since
837   // non-integrated assemblers don't recognize .cgprofile section.
838   PTO.CallGraphProfile = !CodeGenOpts.DisableIntegratedAS;
839   PTO.UnifiedLTO = CodeGenOpts.UnifiedLTO;
840 
841   LoopAnalysisManager LAM;
842   FunctionAnalysisManager FAM;
843   CGSCCAnalysisManager CGAM;
844   ModuleAnalysisManager MAM;
845 
846   bool DebugPassStructure = CodeGenOpts.DebugPass == "Structure";
847   PassInstrumentationCallbacks PIC;
848   PrintPassOptions PrintPassOpts;
849   PrintPassOpts.Indent = DebugPassStructure;
850   PrintPassOpts.SkipAnalyses = DebugPassStructure;
851   StandardInstrumentations SI(
852       TheModule->getContext(),
853       (CodeGenOpts.DebugPassManager || DebugPassStructure),
854       CodeGenOpts.VerifyEach, PrintPassOpts);
855   SI.registerCallbacks(PIC, &MAM);
856   PassBuilder PB(TM.get(), PTO, PGOOpt, &PIC);
857 
858   // Handle the assignment tracking feature options.
859   switch (CodeGenOpts.getAssignmentTrackingMode()) {
860   case CodeGenOptions::AssignmentTrackingOpts::Forced:
861     PB.registerPipelineStartEPCallback(
862         [&](ModulePassManager &MPM, OptimizationLevel Level) {
863           MPM.addPass(AssignmentTrackingPass());
864         });
865     break;
866   case CodeGenOptions::AssignmentTrackingOpts::Enabled:
867     // Disable assignment tracking in LTO builds for now as the performance
868     // cost is too high. Disable for LLDB tuning due to llvm.org/PR43126.
869     if (!CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.PrepareForLTO &&
870         CodeGenOpts.getDebuggerTuning() != llvm::DebuggerKind::LLDB) {
871       PB.registerPipelineStartEPCallback(
872           [&](ModulePassManager &MPM, OptimizationLevel Level) {
873             // Only use assignment tracking if optimisations are enabled.
874             if (Level != OptimizationLevel::O0)
875               MPM.addPass(AssignmentTrackingPass());
876           });
877     }
878     break;
879   case CodeGenOptions::AssignmentTrackingOpts::Disabled:
880     break;
881   }
882 
883   // Enable verify-debuginfo-preserve-each for new PM.
884   DebugifyEachInstrumentation Debugify;
885   DebugInfoPerPass DebugInfoBeforePass;
886   if (CodeGenOpts.EnableDIPreservationVerify) {
887     Debugify.setDebugifyMode(DebugifyMode::OriginalDebugInfo);
888     Debugify.setDebugInfoBeforePass(DebugInfoBeforePass);
889 
890     if (!CodeGenOpts.DIBugsReportFilePath.empty())
891       Debugify.setOrigDIVerifyBugsReportFilePath(
892           CodeGenOpts.DIBugsReportFilePath);
893     Debugify.registerCallbacks(PIC, MAM);
894   }
895   // Attempt to load pass plugins and register their callbacks with PB.
896   for (auto &PluginFN : CodeGenOpts.PassPlugins) {
897     auto PassPlugin = PassPlugin::Load(PluginFN);
898     if (PassPlugin) {
899       PassPlugin->registerPassBuilderCallbacks(PB);
900     } else {
901       Diags.Report(diag::err_fe_unable_to_load_plugin)
902           << PluginFN << toString(PassPlugin.takeError());
903     }
904   }
905 #define HANDLE_EXTENSION(Ext)                                                  \
906   get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
907 #include "llvm/Support/Extension.def"
908 
909   // Register the target library analysis directly and give it a customized
910   // preset TLI.
911   std::unique_ptr<TargetLibraryInfoImpl> TLII(
912       createTLII(TargetTriple, CodeGenOpts));
913   FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
914 
915   // Register all the basic analyses with the managers.
916   PB.registerModuleAnalyses(MAM);
917   PB.registerCGSCCAnalyses(CGAM);
918   PB.registerFunctionAnalyses(FAM);
919   PB.registerLoopAnalyses(LAM);
920   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
921 
922   ModulePassManager MPM;
923 
924   if (!CodeGenOpts.DisableLLVMPasses) {
925     // Map our optimization levels into one of the distinct levels used to
926     // configure the pipeline.
927     OptimizationLevel Level = mapToLevel(CodeGenOpts);
928 
929     bool IsThinLTO = CodeGenOpts.PrepareForThinLTO;
930     bool IsLTO = CodeGenOpts.PrepareForLTO;
931 
932     if (LangOpts.ObjCAutoRefCount) {
933       PB.registerPipelineStartEPCallback(
934           [](ModulePassManager &MPM, OptimizationLevel Level) {
935             if (Level != OptimizationLevel::O0)
936               MPM.addPass(
937                   createModuleToFunctionPassAdaptor(ObjCARCExpandPass()));
938           });
939       PB.registerPipelineEarlySimplificationEPCallback(
940           [](ModulePassManager &MPM, OptimizationLevel Level) {
941             if (Level != OptimizationLevel::O0)
942               MPM.addPass(ObjCARCAPElimPass());
943           });
944       PB.registerScalarOptimizerLateEPCallback(
945           [](FunctionPassManager &FPM, OptimizationLevel Level) {
946             if (Level != OptimizationLevel::O0)
947               FPM.addPass(ObjCARCOptPass());
948           });
949     }
950 
951     // If we reached here with a non-empty index file name, then the index
952     // file was empty and we are not performing ThinLTO backend compilation
953     // (used in testing in a distributed build environment).
954     bool IsThinLTOPostLink = !CodeGenOpts.ThinLTOIndexFile.empty();
955     // If so drop any the type test assume sequences inserted for whole program
956     // vtables so that codegen doesn't complain.
957     if (IsThinLTOPostLink)
958       PB.registerPipelineStartEPCallback(
959           [](ModulePassManager &MPM, OptimizationLevel Level) {
960             MPM.addPass(LowerTypeTestsPass(/*ExportSummary=*/nullptr,
961                                            /*ImportSummary=*/nullptr,
962                                            /*DropTypeTests=*/true));
963           });
964 
965     if (CodeGenOpts.InstrumentFunctions ||
966         CodeGenOpts.InstrumentFunctionEntryBare ||
967         CodeGenOpts.InstrumentFunctionsAfterInlining ||
968         CodeGenOpts.InstrumentForProfiling) {
969       PB.registerPipelineStartEPCallback(
970           [](ModulePassManager &MPM, OptimizationLevel Level) {
971             MPM.addPass(createModuleToFunctionPassAdaptor(
972                 EntryExitInstrumenterPass(/*PostInlining=*/false)));
973           });
974       PB.registerOptimizerLastEPCallback(
975           [](ModulePassManager &MPM, OptimizationLevel Level) {
976             MPM.addPass(createModuleToFunctionPassAdaptor(
977                 EntryExitInstrumenterPass(/*PostInlining=*/true)));
978           });
979     }
980 
981     // Register callbacks to schedule sanitizer passes at the appropriate part
982     // of the pipeline.
983     if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds))
984       PB.registerScalarOptimizerLateEPCallback(
985           [](FunctionPassManager &FPM, OptimizationLevel Level) {
986             FPM.addPass(BoundsCheckingPass());
987           });
988 
989     // Don't add sanitizers if we are here from ThinLTO PostLink. That already
990     // done on PreLink stage.
991     if (!IsThinLTOPostLink) {
992       addSanitizers(TargetTriple, CodeGenOpts, LangOpts, PB);
993       addKCFIPass(TargetTriple, LangOpts, PB);
994     }
995 
996     if (std::optional<GCOVOptions> Options =
997             getGCOVOptions(CodeGenOpts, LangOpts))
998       PB.registerPipelineStartEPCallback(
999           [Options](ModulePassManager &MPM, OptimizationLevel Level) {
1000             MPM.addPass(GCOVProfilerPass(*Options));
1001           });
1002     if (std::optional<InstrProfOptions> Options =
1003             getInstrProfOptions(CodeGenOpts, LangOpts))
1004       PB.registerPipelineStartEPCallback(
1005           [Options](ModulePassManager &MPM, OptimizationLevel Level) {
1006             MPM.addPass(InstrProfiling(*Options, false));
1007           });
1008 
1009     // TODO: Consider passing the MemoryProfileOutput to the pass builder via
1010     // the PGOOptions, and set this up there.
1011     if (!CodeGenOpts.MemoryProfileOutput.empty()) {
1012       PB.registerOptimizerLastEPCallback(
1013           [](ModulePassManager &MPM, OptimizationLevel Level) {
1014             MPM.addPass(createModuleToFunctionPassAdaptor(MemProfilerPass()));
1015             MPM.addPass(ModuleMemProfilerPass());
1016           });
1017     }
1018 
1019     bool IsThinOrUnifiedLTO = IsThinLTO || (IsLTO && CodeGenOpts.UnifiedLTO);
1020     if (CodeGenOpts.FatLTO) {
1021       MPM = PB.buildFatLTODefaultPipeline(Level, IsThinOrUnifiedLTO,
1022                                           IsThinOrUnifiedLTO ||
1023                                               shouldEmitRegularLTOSummary());
1024     } else if (IsThinOrUnifiedLTO) {
1025       MPM = PB.buildThinLTOPreLinkDefaultPipeline(Level);
1026     } else if (IsLTO) {
1027       MPM = PB.buildLTOPreLinkDefaultPipeline(Level);
1028     } else {
1029       MPM = PB.buildPerModuleDefaultPipeline(Level);
1030     }
1031   }
1032 
1033   // Add a verifier pass if requested. We don't have to do this if the action
1034   // requires code generation because there will already be a verifier pass in
1035   // the code-generation pipeline.
1036   if (!actionRequiresCodeGen(Action) && CodeGenOpts.VerifyModule)
1037     MPM.addPass(VerifierPass());
1038 
1039   if (Action == Backend_EmitBC || Action == Backend_EmitLL) {
1040     if (CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.DisableLLVMPasses) {
1041       if (!TheModule->getModuleFlag("EnableSplitLTOUnit"))
1042         TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
1043                                  CodeGenOpts.EnableSplitLTOUnit);
1044       if (Action == Backend_EmitBC) {
1045         if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
1046           ThinLinkOS = openOutputFile(CodeGenOpts.ThinLinkBitcodeFile);
1047           if (!ThinLinkOS)
1048             return;
1049         }
1050         if (CodeGenOpts.UnifiedLTO)
1051           TheModule->addModuleFlag(Module::Error, "UnifiedLTO", uint32_t(1));
1052         MPM.addPass(ThinLTOBitcodeWriterPass(
1053             *OS, ThinLinkOS ? &ThinLinkOS->os() : nullptr));
1054       } else {
1055         MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists,
1056                                     /*EmitLTOSummary=*/true));
1057       }
1058 
1059     } else {
1060       // Emit a module summary by default for Regular LTO except for ld64
1061       // targets
1062       bool EmitLTOSummary = shouldEmitRegularLTOSummary();
1063       if (EmitLTOSummary) {
1064         if (!TheModule->getModuleFlag("ThinLTO") && !CodeGenOpts.UnifiedLTO)
1065           TheModule->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
1066         if (!TheModule->getModuleFlag("EnableSplitLTOUnit"))
1067           TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
1068                                    uint32_t(1));
1069         if (CodeGenOpts.UnifiedLTO)
1070           TheModule->addModuleFlag(Module::Error, "UnifiedLTO", uint32_t(1));
1071       }
1072       if (Action == Backend_EmitBC)
1073         MPM.addPass(BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists,
1074                                       EmitLTOSummary));
1075       else
1076         MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists,
1077                                     EmitLTOSummary));
1078     }
1079   }
1080   if (CodeGenOpts.FatLTO) {
1081     // Set module flags, like EnableSplitLTOUnit and UnifiedLTO, since FatLTO
1082     // uses a different action than Backend_EmitBC or Backend_EmitLL.
1083     bool IsThinOrUnifiedLTO =
1084         CodeGenOpts.PrepareForThinLTO ||
1085         (CodeGenOpts.PrepareForLTO && CodeGenOpts.UnifiedLTO);
1086     if (!TheModule->getModuleFlag("ThinLTO"))
1087       TheModule->addModuleFlag(Module::Error, "ThinLTO",
1088                                uint32_t(IsThinOrUnifiedLTO));
1089     if (!TheModule->getModuleFlag("EnableSplitLTOUnit"))
1090       TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
1091                                uint32_t(CodeGenOpts.EnableSplitLTOUnit));
1092     if (CodeGenOpts.UnifiedLTO && !TheModule->getModuleFlag("UnifiedLTO"))
1093       TheModule->addModuleFlag(Module::Error, "UnifiedLTO", uint32_t(1));
1094   }
1095 
1096   // Now that we have all of the passes ready, run them.
1097   {
1098     PrettyStackTraceString CrashInfo("Optimizer");
1099     llvm::TimeTraceScope TimeScope("Optimizer");
1100     MPM.run(*TheModule, MAM);
1101   }
1102 }
1103 
1104 void EmitAssemblyHelper::RunCodegenPipeline(
1105     BackendAction Action, std::unique_ptr<raw_pwrite_stream> &OS,
1106     std::unique_ptr<llvm::ToolOutputFile> &DwoOS) {
1107   // We still use the legacy PM to run the codegen pipeline since the new PM
1108   // does not work with the codegen pipeline.
1109   // FIXME: make the new PM work with the codegen pipeline.
1110   legacy::PassManager CodeGenPasses;
1111 
1112   // Append any output we need to the pass manager.
1113   switch (Action) {
1114   case Backend_EmitAssembly:
1115   case Backend_EmitMCNull:
1116   case Backend_EmitObj:
1117     CodeGenPasses.add(
1118         createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
1119     if (!CodeGenOpts.SplitDwarfOutput.empty()) {
1120       DwoOS = openOutputFile(CodeGenOpts.SplitDwarfOutput);
1121       if (!DwoOS)
1122         return;
1123     }
1124     if (!AddEmitPasses(CodeGenPasses, Action, *OS,
1125                        DwoOS ? &DwoOS->os() : nullptr))
1126       // FIXME: Should we handle this error differently?
1127       return;
1128     break;
1129   default:
1130     return;
1131   }
1132 
1133   {
1134     PrettyStackTraceString CrashInfo("Code generation");
1135     llvm::TimeTraceScope TimeScope("CodeGenPasses");
1136     CodeGenPasses.run(*TheModule);
1137   }
1138 }
1139 
1140 void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
1141                                       std::unique_ptr<raw_pwrite_stream> OS) {
1142   TimeRegion Region(CodeGenOpts.TimePasses ? &CodeGenerationTime : nullptr);
1143   setCommandLineOpts(CodeGenOpts);
1144 
1145   bool RequiresCodeGen = actionRequiresCodeGen(Action);
1146   CreateTargetMachine(RequiresCodeGen);
1147 
1148   if (RequiresCodeGen && !TM)
1149     return;
1150   if (TM)
1151     TheModule->setDataLayout(TM->createDataLayout());
1152 
1153   // Before executing passes, print the final values of the LLVM options.
1154   cl::PrintOptionValues();
1155 
1156   std::unique_ptr<llvm::ToolOutputFile> ThinLinkOS, DwoOS;
1157   RunOptimizationPipeline(Action, OS, ThinLinkOS);
1158   RunCodegenPipeline(Action, OS, DwoOS);
1159 
1160   if (ThinLinkOS)
1161     ThinLinkOS->keep();
1162   if (DwoOS)
1163     DwoOS->keep();
1164 }
1165 
1166 static void runThinLTOBackend(
1167     DiagnosticsEngine &Diags, ModuleSummaryIndex *CombinedIndex, Module *M,
1168     const HeaderSearchOptions &HeaderOpts, const CodeGenOptions &CGOpts,
1169     const clang::TargetOptions &TOpts, const LangOptions &LOpts,
1170     std::unique_ptr<raw_pwrite_stream> OS, std::string SampleProfile,
1171     std::string ProfileRemapping, BackendAction Action) {
1172   StringMap<DenseMap<GlobalValue::GUID, GlobalValueSummary *>>
1173       ModuleToDefinedGVSummaries;
1174   CombinedIndex->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
1175 
1176   setCommandLineOpts(CGOpts);
1177 
1178   // We can simply import the values mentioned in the combined index, since
1179   // we should only invoke this using the individual indexes written out
1180   // via a WriteIndexesThinBackend.
1181   FunctionImporter::ImportMapTy ImportList;
1182   if (!lto::initImportList(*M, *CombinedIndex, ImportList))
1183     return;
1184 
1185   auto AddStream = [&](size_t Task, const Twine &ModuleName) {
1186     return std::make_unique<CachedFileStream>(std::move(OS),
1187                                               CGOpts.ObjectFilenameForDebug);
1188   };
1189   lto::Config Conf;
1190   if (CGOpts.SaveTempsFilePrefix != "") {
1191     if (Error E = Conf.addSaveTemps(CGOpts.SaveTempsFilePrefix + ".",
1192                                     /* UseInputModulePath */ false)) {
1193       handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
1194         errs() << "Error setting up ThinLTO save-temps: " << EIB.message()
1195                << '\n';
1196       });
1197     }
1198   }
1199   Conf.CPU = TOpts.CPU;
1200   Conf.CodeModel = getCodeModel(CGOpts);
1201   Conf.MAttrs = TOpts.Features;
1202   Conf.RelocModel = CGOpts.RelocationModel;
1203   std::optional<CodeGenOpt::Level> OptLevelOrNone =
1204       CodeGenOpt::getLevel(CGOpts.OptimizationLevel);
1205   assert(OptLevelOrNone && "Invalid optimization level!");
1206   Conf.CGOptLevel = *OptLevelOrNone;
1207   Conf.OptLevel = CGOpts.OptimizationLevel;
1208   initTargetOptions(Diags, Conf.Options, CGOpts, TOpts, LOpts, HeaderOpts);
1209   Conf.SampleProfile = std::move(SampleProfile);
1210   Conf.PTO.LoopUnrolling = CGOpts.UnrollLoops;
1211   // For historical reasons, loop interleaving is set to mirror setting for loop
1212   // unrolling.
1213   Conf.PTO.LoopInterleaving = CGOpts.UnrollLoops;
1214   Conf.PTO.LoopVectorization = CGOpts.VectorizeLoop;
1215   Conf.PTO.SLPVectorization = CGOpts.VectorizeSLP;
1216   // Only enable CGProfilePass when using integrated assembler, since
1217   // non-integrated assemblers don't recognize .cgprofile section.
1218   Conf.PTO.CallGraphProfile = !CGOpts.DisableIntegratedAS;
1219 
1220   // Context sensitive profile.
1221   if (CGOpts.hasProfileCSIRInstr()) {
1222     Conf.RunCSIRInstr = true;
1223     Conf.CSIRProfile = std::move(CGOpts.InstrProfileOutput);
1224   } else if (CGOpts.hasProfileCSIRUse()) {
1225     Conf.RunCSIRInstr = false;
1226     Conf.CSIRProfile = std::move(CGOpts.ProfileInstrumentUsePath);
1227   }
1228 
1229   Conf.ProfileRemapping = std::move(ProfileRemapping);
1230   Conf.DebugPassManager = CGOpts.DebugPassManager;
1231   Conf.VerifyEach = CGOpts.VerifyEach;
1232   Conf.RemarksWithHotness = CGOpts.DiagnosticsWithHotness;
1233   Conf.RemarksFilename = CGOpts.OptRecordFile;
1234   Conf.RemarksPasses = CGOpts.OptRecordPasses;
1235   Conf.RemarksFormat = CGOpts.OptRecordFormat;
1236   Conf.SplitDwarfFile = CGOpts.SplitDwarfFile;
1237   Conf.SplitDwarfOutput = CGOpts.SplitDwarfOutput;
1238   switch (Action) {
1239   case Backend_EmitNothing:
1240     Conf.PreCodeGenModuleHook = [](size_t Task, const Module &Mod) {
1241       return false;
1242     };
1243     break;
1244   case Backend_EmitLL:
1245     Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
1246       M->print(*OS, nullptr, CGOpts.EmitLLVMUseLists);
1247       return false;
1248     };
1249     break;
1250   case Backend_EmitBC:
1251     Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
1252       WriteBitcodeToFile(*M, *OS, CGOpts.EmitLLVMUseLists);
1253       return false;
1254     };
1255     break;
1256   default:
1257     Conf.CGFileType = getCodeGenFileType(Action);
1258     break;
1259   }
1260   if (Error E =
1261           thinBackend(Conf, -1, AddStream, *M, *CombinedIndex, ImportList,
1262                       ModuleToDefinedGVSummaries[M->getModuleIdentifier()],
1263                       /* ModuleMap */ nullptr, CGOpts.CmdArgs)) {
1264     handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
1265       errs() << "Error running ThinLTO backend: " << EIB.message() << '\n';
1266     });
1267   }
1268 }
1269 
1270 void clang::EmitBackendOutput(DiagnosticsEngine &Diags,
1271                               const HeaderSearchOptions &HeaderOpts,
1272                               const CodeGenOptions &CGOpts,
1273                               const clang::TargetOptions &TOpts,
1274                               const LangOptions &LOpts, StringRef TDesc,
1275                               Module *M, BackendAction Action,
1276                               IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
1277                               std::unique_ptr<raw_pwrite_stream> OS) {
1278 
1279   llvm::TimeTraceScope TimeScope("Backend");
1280 
1281   std::unique_ptr<llvm::Module> EmptyModule;
1282   if (!CGOpts.ThinLTOIndexFile.empty()) {
1283     // If we are performing a ThinLTO importing compile, load the function index
1284     // into memory and pass it into runThinLTOBackend, which will run the
1285     // function importer and invoke LTO passes.
1286     std::unique_ptr<ModuleSummaryIndex> CombinedIndex;
1287     if (Error E = llvm::getModuleSummaryIndexForFile(
1288                       CGOpts.ThinLTOIndexFile,
1289                       /*IgnoreEmptyThinLTOIndexFile*/ true)
1290                       .moveInto(CombinedIndex)) {
1291       logAllUnhandledErrors(std::move(E), errs(),
1292                             "Error loading index file '" +
1293                             CGOpts.ThinLTOIndexFile + "': ");
1294       return;
1295     }
1296 
1297     // A null CombinedIndex means we should skip ThinLTO compilation
1298     // (LLVM will optionally ignore empty index files, returning null instead
1299     // of an error).
1300     if (CombinedIndex) {
1301       if (!CombinedIndex->skipModuleByDistributedBackend()) {
1302         runThinLTOBackend(Diags, CombinedIndex.get(), M, HeaderOpts, CGOpts,
1303                           TOpts, LOpts, std::move(OS), CGOpts.SampleProfileFile,
1304                           CGOpts.ProfileRemappingFile, Action);
1305         return;
1306       }
1307       // Distributed indexing detected that nothing from the module is needed
1308       // for the final linking. So we can skip the compilation. We sill need to
1309       // output an empty object file to make sure that a linker does not fail
1310       // trying to read it. Also for some features, like CFI, we must skip
1311       // the compilation as CombinedIndex does not contain all required
1312       // information.
1313       EmptyModule = std::make_unique<llvm::Module>("empty", M->getContext());
1314       EmptyModule->setTargetTriple(M->getTargetTriple());
1315       M = EmptyModule.get();
1316     }
1317   }
1318 
1319   EmitAssemblyHelper AsmHelper(Diags, HeaderOpts, CGOpts, TOpts, LOpts, M, VFS);
1320   AsmHelper.EmitAssembly(Action, std::move(OS));
1321 
1322   // Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's
1323   // DataLayout.
1324   if (AsmHelper.TM) {
1325     std::string DLDesc = M->getDataLayout().getStringRepresentation();
1326     if (DLDesc != TDesc) {
1327       unsigned DiagID = Diags.getCustomDiagID(
1328           DiagnosticsEngine::Error, "backend data layout '%0' does not match "
1329                                     "expected target description '%1'");
1330       Diags.Report(DiagID) << DLDesc << TDesc;
1331     }
1332   }
1333 }
1334 
1335 // With -fembed-bitcode, save a copy of the llvm IR as data in the
1336 // __LLVM,__bitcode section.
1337 void clang::EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts,
1338                          llvm::MemoryBufferRef Buf) {
1339   if (CGOpts.getEmbedBitcode() == CodeGenOptions::Embed_Off)
1340     return;
1341   llvm::embedBitcodeInModule(
1342       *M, Buf, CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Marker,
1343       CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Bitcode,
1344       CGOpts.CmdArgs);
1345 }
1346 
1347 void clang::EmbedObject(llvm::Module *M, const CodeGenOptions &CGOpts,
1348                         DiagnosticsEngine &Diags) {
1349   if (CGOpts.OffloadObjects.empty())
1350     return;
1351 
1352   for (StringRef OffloadObject : CGOpts.OffloadObjects) {
1353     llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> ObjectOrErr =
1354         llvm::MemoryBuffer::getFileOrSTDIN(OffloadObject);
1355     if (std::error_code EC = ObjectOrErr.getError()) {
1356       auto DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1357                                           "could not open '%0' for embedding");
1358       Diags.Report(DiagID) << OffloadObject;
1359       return;
1360     }
1361 
1362     llvm::embedBufferInModule(*M, **ObjectOrErr, ".llvm.offloading",
1363                               Align(object::OffloadBinary::getAlignment()));
1364   }
1365 }
1366