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