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