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