xref: /freebsd/contrib/llvm-project/clang/lib/CodeGen/BackendUtil.cpp (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
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.EmulatedTLS = CodeGenOpts.EmulatedTLS;
405   Options.DebuggerTuning = CodeGenOpts.getDebuggerTuning();
406   Options.EmitStackSizeSection = CodeGenOpts.StackSizeSection;
407   Options.StackUsageOutput = CodeGenOpts.StackUsageOutput;
408   Options.EmitAddrsig = CodeGenOpts.Addrsig;
409   Options.ForceDwarfFrameSection = CodeGenOpts.ForceDwarfFrameSection;
410   Options.EmitCallSiteInfo = CodeGenOpts.EmitCallSiteInfo;
411   Options.EnableAIXExtendedAltivecABI = LangOpts.EnableAIXExtendedAltivecABI;
412   Options.XRayFunctionIndex = CodeGenOpts.XRayFunctionIndex;
413   Options.LoopAlignment = CodeGenOpts.LoopAlignment;
414   Options.DebugStrictDwarf = CodeGenOpts.DebugStrictDwarf;
415   Options.ObjectFilenameForDebug = CodeGenOpts.ObjectFilenameForDebug;
416   Options.Hotpatch = CodeGenOpts.HotPatch;
417   Options.JMCInstrument = CodeGenOpts.JMCInstrument;
418   Options.XCOFFReadOnlyPointers = CodeGenOpts.XCOFFReadOnlyPointers;
419 
420   switch (CodeGenOpts.getSwiftAsyncFramePointer()) {
421   case CodeGenOptions::SwiftAsyncFramePointerKind::Auto:
422     Options.SwiftAsyncFramePointer =
423         SwiftAsyncFramePointerMode::DeploymentBased;
424     break;
425 
426   case CodeGenOptions::SwiftAsyncFramePointerKind::Always:
427     Options.SwiftAsyncFramePointer = SwiftAsyncFramePointerMode::Always;
428     break;
429 
430   case CodeGenOptions::SwiftAsyncFramePointerKind::Never:
431     Options.SwiftAsyncFramePointer = SwiftAsyncFramePointerMode::Never;
432     break;
433   }
434 
435   Options.MCOptions.SplitDwarfFile = CodeGenOpts.SplitDwarfFile;
436   Options.MCOptions.EmitDwarfUnwind = CodeGenOpts.getEmitDwarfUnwind();
437   Options.MCOptions.EmitCompactUnwindNonCanonical =
438       CodeGenOpts.EmitCompactUnwindNonCanonical;
439   Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll;
440   Options.MCOptions.MCSaveTempLabels = CodeGenOpts.SaveTempLabels;
441   Options.MCOptions.MCUseDwarfDirectory =
442       CodeGenOpts.NoDwarfDirectoryAsm
443           ? llvm::MCTargetOptions::DisableDwarfDirectory
444           : llvm::MCTargetOptions::EnableDwarfDirectory;
445   Options.MCOptions.MCNoExecStack = CodeGenOpts.NoExecStack;
446   Options.MCOptions.MCIncrementalLinkerCompatible =
447       CodeGenOpts.IncrementalLinkerCompatible;
448   Options.MCOptions.MCFatalWarnings = CodeGenOpts.FatalWarnings;
449   Options.MCOptions.MCNoWarn = CodeGenOpts.NoWarn;
450   Options.MCOptions.AsmVerbose = CodeGenOpts.AsmVerbose;
451   Options.MCOptions.Dwarf64 = CodeGenOpts.Dwarf64;
452   Options.MCOptions.PreserveAsmComments = CodeGenOpts.PreserveAsmComments;
453   Options.MCOptions.ABIName = TargetOpts.ABI;
454   for (const auto &Entry : HSOpts.UserEntries)
455     if (!Entry.IsFramework &&
456         (Entry.Group == frontend::IncludeDirGroup::Quoted ||
457          Entry.Group == frontend::IncludeDirGroup::Angled ||
458          Entry.Group == frontend::IncludeDirGroup::System))
459       Options.MCOptions.IASSearchPaths.push_back(
460           Entry.IgnoreSysRoot ? Entry.Path : HSOpts.Sysroot + Entry.Path);
461   Options.MCOptions.Argv0 = CodeGenOpts.Argv0;
462   Options.MCOptions.CommandLineArgs = CodeGenOpts.CommandLineArgs;
463   Options.MCOptions.AsSecureLogFile = CodeGenOpts.AsSecureLogFile;
464   Options.MCOptions.PPCUseFullRegisterNames =
465       CodeGenOpts.PPCUseFullRegisterNames;
466   Options.MisExpect = CodeGenOpts.MisExpect;
467 
468   return true;
469 }
470 
471 static std::optional<GCOVOptions>
472 getGCOVOptions(const CodeGenOptions &CodeGenOpts, const LangOptions &LangOpts) {
473   if (CodeGenOpts.CoverageNotesFile.empty() &&
474       CodeGenOpts.CoverageDataFile.empty())
475     return std::nullopt;
476   // Not using 'GCOVOptions::getDefault' allows us to avoid exiting if
477   // LLVM's -default-gcov-version flag is set to something invalid.
478   GCOVOptions Options;
479   Options.EmitNotes = !CodeGenOpts.CoverageNotesFile.empty();
480   Options.EmitData = !CodeGenOpts.CoverageDataFile.empty();
481   llvm::copy(CodeGenOpts.CoverageVersion, std::begin(Options.Version));
482   Options.NoRedZone = CodeGenOpts.DisableRedZone;
483   Options.Filter = CodeGenOpts.ProfileFilterFiles;
484   Options.Exclude = CodeGenOpts.ProfileExcludeFiles;
485   Options.Atomic = CodeGenOpts.AtomicProfileUpdate;
486   return Options;
487 }
488 
489 static std::optional<InstrProfOptions>
490 getInstrProfOptions(const CodeGenOptions &CodeGenOpts,
491                     const LangOptions &LangOpts) {
492   if (!CodeGenOpts.hasProfileClangInstr())
493     return std::nullopt;
494   InstrProfOptions Options;
495   Options.NoRedZone = CodeGenOpts.DisableRedZone;
496   Options.InstrProfileOutput = CodeGenOpts.InstrProfileOutput;
497   Options.Atomic = CodeGenOpts.AtomicProfileUpdate;
498   return Options;
499 }
500 
501 static void setCommandLineOpts(const CodeGenOptions &CodeGenOpts) {
502   SmallVector<const char *, 16> BackendArgs;
503   BackendArgs.push_back("clang"); // Fake program name.
504   if (!CodeGenOpts.DebugPass.empty()) {
505     BackendArgs.push_back("-debug-pass");
506     BackendArgs.push_back(CodeGenOpts.DebugPass.c_str());
507   }
508   if (!CodeGenOpts.LimitFloatPrecision.empty()) {
509     BackendArgs.push_back("-limit-float-precision");
510     BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str());
511   }
512   // Check for the default "clang" invocation that won't set any cl::opt values.
513   // Skip trying to parse the command line invocation to avoid the issues
514   // described below.
515   if (BackendArgs.size() == 1)
516     return;
517   BackendArgs.push_back(nullptr);
518   // FIXME: The command line parser below is not thread-safe and shares a global
519   // state, so this call might crash or overwrite the options of another Clang
520   // instance in the same process.
521   llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
522                                     BackendArgs.data());
523 }
524 
525 void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
526   // Create the TargetMachine for generating code.
527   std::string Error;
528   std::string Triple = TheModule->getTargetTriple();
529   const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
530   if (!TheTarget) {
531     if (MustCreateTM)
532       Diags.Report(diag::err_fe_unable_to_create_target) << Error;
533     return;
534   }
535 
536   std::optional<llvm::CodeModel::Model> CM = getCodeModel(CodeGenOpts);
537   std::string FeaturesStr =
538       llvm::join(TargetOpts.Features.begin(), TargetOpts.Features.end(), ",");
539   llvm::Reloc::Model RM = CodeGenOpts.RelocationModel;
540   std::optional<CodeGenOptLevel> OptLevelOrNone =
541       CodeGenOpt::getLevel(CodeGenOpts.OptimizationLevel);
542   assert(OptLevelOrNone && "Invalid optimization level!");
543   CodeGenOptLevel OptLevel = *OptLevelOrNone;
544 
545   llvm::TargetOptions Options;
546   if (!initTargetOptions(Diags, Options, CodeGenOpts, TargetOpts, LangOpts,
547                          HSOpts))
548     return;
549   TM.reset(TheTarget->createTargetMachine(Triple, TargetOpts.CPU, FeaturesStr,
550                                           Options, RM, CM, OptLevel));
551   TM->setLargeDataThreshold(CodeGenOpts.LargeDataThreshold);
552 }
553 
554 bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager &CodeGenPasses,
555                                        BackendAction Action,
556                                        raw_pwrite_stream &OS,
557                                        raw_pwrite_stream *DwoOS) {
558   // Add LibraryInfo.
559   std::unique_ptr<TargetLibraryInfoImpl> TLII(
560       llvm::driver::createTLII(TargetTriple, CodeGenOpts.getVecLib()));
561   CodeGenPasses.add(new TargetLibraryInfoWrapperPass(*TLII));
562 
563   // Normal mode, emit a .s or .o file by running the code generator. Note,
564   // this also adds codegenerator level optimization passes.
565   CodeGenFileType CGFT = getCodeGenFileType(Action);
566 
567   // Add ObjC ARC final-cleanup optimizations. This is done as part of the
568   // "codegen" passes so that it isn't run multiple times when there is
569   // inlining happening.
570   if (CodeGenOpts.OptimizationLevel > 0)
571     CodeGenPasses.add(createObjCARCContractPass());
572 
573   if (TM->addPassesToEmitFile(CodeGenPasses, OS, DwoOS, CGFT,
574                               /*DisableVerify=*/!CodeGenOpts.VerifyModule)) {
575     Diags.Report(diag::err_fe_unable_to_interface_with_target);
576     return false;
577   }
578 
579   return true;
580 }
581 
582 static OptimizationLevel mapToLevel(const CodeGenOptions &Opts) {
583   switch (Opts.OptimizationLevel) {
584   default:
585     llvm_unreachable("Invalid optimization level!");
586 
587   case 0:
588     return OptimizationLevel::O0;
589 
590   case 1:
591     return OptimizationLevel::O1;
592 
593   case 2:
594     switch (Opts.OptimizeSize) {
595     default:
596       llvm_unreachable("Invalid optimization level for size!");
597 
598     case 0:
599       return OptimizationLevel::O2;
600 
601     case 1:
602       return OptimizationLevel::Os;
603 
604     case 2:
605       return OptimizationLevel::Oz;
606     }
607 
608   case 3:
609     return OptimizationLevel::O3;
610   }
611 }
612 
613 static void addKCFIPass(const Triple &TargetTriple, const LangOptions &LangOpts,
614                         PassBuilder &PB) {
615   // If the back-end supports KCFI operand bundle lowering, skip KCFIPass.
616   if (TargetTriple.getArch() == llvm::Triple::x86_64 ||
617       TargetTriple.isAArch64(64) || TargetTriple.isRISCV())
618     return;
619 
620   // Ensure we lower KCFI operand bundles with -O0.
621   PB.registerOptimizerLastEPCallback(
622       [&](ModulePassManager &MPM, OptimizationLevel Level) {
623         if (Level == OptimizationLevel::O0 &&
624             LangOpts.Sanitize.has(SanitizerKind::KCFI))
625           MPM.addPass(createModuleToFunctionPassAdaptor(KCFIPass()));
626       });
627 
628   // When optimizations are requested, run KCIFPass after InstCombine to
629   // avoid unnecessary checks.
630   PB.registerPeepholeEPCallback(
631       [&](FunctionPassManager &FPM, OptimizationLevel Level) {
632         if (Level != OptimizationLevel::O0 &&
633             LangOpts.Sanitize.has(SanitizerKind::KCFI))
634           FPM.addPass(KCFIPass());
635       });
636 }
637 
638 static void addSanitizers(const Triple &TargetTriple,
639                           const CodeGenOptions &CodeGenOpts,
640                           const LangOptions &LangOpts, PassBuilder &PB) {
641   auto SanitizersCallback = [&](ModulePassManager &MPM,
642                                 OptimizationLevel Level) {
643     if (CodeGenOpts.hasSanitizeCoverage()) {
644       auto SancovOpts = getSancovOptsFromCGOpts(CodeGenOpts);
645       MPM.addPass(SanitizerCoveragePass(
646           SancovOpts, CodeGenOpts.SanitizeCoverageAllowlistFiles,
647           CodeGenOpts.SanitizeCoverageIgnorelistFiles));
648     }
649 
650     if (CodeGenOpts.hasSanitizeBinaryMetadata()) {
651       MPM.addPass(SanitizerBinaryMetadataPass(
652           getSanitizerBinaryMetadataOptions(CodeGenOpts),
653           CodeGenOpts.SanitizeMetadataIgnorelistFiles));
654     }
655 
656     auto MSanPass = [&](SanitizerMask Mask, bool CompileKernel) {
657       if (LangOpts.Sanitize.has(Mask)) {
658         int TrackOrigins = CodeGenOpts.SanitizeMemoryTrackOrigins;
659         bool Recover = CodeGenOpts.SanitizeRecover.has(Mask);
660 
661         MemorySanitizerOptions options(TrackOrigins, Recover, CompileKernel,
662                                        CodeGenOpts.SanitizeMemoryParamRetval);
663         MPM.addPass(MemorySanitizerPass(options));
664         if (Level != OptimizationLevel::O0) {
665           // MemorySanitizer inserts complex instrumentation that mostly follows
666           // the logic of the original code, but operates on "shadow" values. It
667           // can benefit from re-running some general purpose optimization
668           // passes.
669           MPM.addPass(RequireAnalysisPass<GlobalsAA, llvm::Module>());
670           FunctionPassManager FPM;
671           FPM.addPass(EarlyCSEPass(true /* Enable mem-ssa. */));
672           FPM.addPass(InstCombinePass());
673           FPM.addPass(JumpThreadingPass());
674           FPM.addPass(GVNPass());
675           FPM.addPass(InstCombinePass());
676           MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
677         }
678       }
679     };
680     MSanPass(SanitizerKind::Memory, false);
681     MSanPass(SanitizerKind::KernelMemory, true);
682 
683     if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
684       MPM.addPass(ModuleThreadSanitizerPass());
685       MPM.addPass(createModuleToFunctionPassAdaptor(ThreadSanitizerPass()));
686     }
687 
688     auto ASanPass = [&](SanitizerMask Mask, bool CompileKernel) {
689       if (LangOpts.Sanitize.has(Mask)) {
690         bool UseGlobalGC = asanUseGlobalsGC(TargetTriple, CodeGenOpts);
691         bool UseOdrIndicator = CodeGenOpts.SanitizeAddressUseOdrIndicator;
692         llvm::AsanDtorKind DestructorKind =
693             CodeGenOpts.getSanitizeAddressDtor();
694         AddressSanitizerOptions Opts;
695         Opts.CompileKernel = CompileKernel;
696         Opts.Recover = CodeGenOpts.SanitizeRecover.has(Mask);
697         Opts.UseAfterScope = CodeGenOpts.SanitizeAddressUseAfterScope;
698         Opts.UseAfterReturn = CodeGenOpts.getSanitizeAddressUseAfterReturn();
699         MPM.addPass(AddressSanitizerPass(Opts, UseGlobalGC, UseOdrIndicator,
700                                          DestructorKind));
701       }
702     };
703     ASanPass(SanitizerKind::Address, false);
704     ASanPass(SanitizerKind::KernelAddress, true);
705 
706     auto HWASanPass = [&](SanitizerMask Mask, bool CompileKernel) {
707       if (LangOpts.Sanitize.has(Mask)) {
708         bool Recover = CodeGenOpts.SanitizeRecover.has(Mask);
709         MPM.addPass(HWAddressSanitizerPass(
710             {CompileKernel, Recover,
711              /*DisableOptimization=*/CodeGenOpts.OptimizationLevel == 0}));
712       }
713     };
714     HWASanPass(SanitizerKind::HWAddress, false);
715     HWASanPass(SanitizerKind::KernelHWAddress, true);
716 
717     if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) {
718       MPM.addPass(DataFlowSanitizerPass(LangOpts.NoSanitizeFiles));
719     }
720   };
721   if (ClSanitizeOnOptimizerEarlyEP) {
722     PB.registerOptimizerEarlyEPCallback(
723         [SanitizersCallback](ModulePassManager &MPM, OptimizationLevel Level) {
724           ModulePassManager NewMPM;
725           SanitizersCallback(NewMPM, Level);
726           if (!NewMPM.isEmpty()) {
727             // Sanitizers can abandon<GlobalsAA>.
728             NewMPM.addPass(RequireAnalysisPass<GlobalsAA, llvm::Module>());
729             MPM.addPass(std::move(NewMPM));
730           }
731         });
732   } else {
733     // LastEP does not need GlobalsAA.
734     PB.registerOptimizerLastEPCallback(SanitizersCallback);
735   }
736 }
737 
738 void EmitAssemblyHelper::RunOptimizationPipeline(
739     BackendAction Action, std::unique_ptr<raw_pwrite_stream> &OS,
740     std::unique_ptr<llvm::ToolOutputFile> &ThinLinkOS, BackendConsumer *BC) {
741   std::optional<PGOOptions> PGOOpt;
742 
743   if (CodeGenOpts.hasProfileIRInstr())
744     // -fprofile-generate.
745     PGOOpt = PGOOptions(
746         CodeGenOpts.InstrProfileOutput.empty() ? getDefaultProfileGenName()
747                                                : CodeGenOpts.InstrProfileOutput,
748         "", "", CodeGenOpts.MemoryProfileUsePath, nullptr, PGOOptions::IRInstr,
749         PGOOptions::NoCSAction, CodeGenOpts.DebugInfoForProfiling,
750         /*PseudoProbeForProfiling=*/false, CodeGenOpts.AtomicProfileUpdate);
751   else if (CodeGenOpts.hasProfileIRUse()) {
752     // -fprofile-use.
753     auto CSAction = CodeGenOpts.hasProfileCSIRUse() ? PGOOptions::CSIRUse
754                                                     : PGOOptions::NoCSAction;
755     PGOOpt = PGOOptions(
756         CodeGenOpts.ProfileInstrumentUsePath, "",
757         CodeGenOpts.ProfileRemappingFile, CodeGenOpts.MemoryProfileUsePath, VFS,
758         PGOOptions::IRUse, CSAction, CodeGenOpts.DebugInfoForProfiling);
759   } else if (!CodeGenOpts.SampleProfileFile.empty())
760     // -fprofile-sample-use
761     PGOOpt = PGOOptions(
762         CodeGenOpts.SampleProfileFile, "", CodeGenOpts.ProfileRemappingFile,
763         CodeGenOpts.MemoryProfileUsePath, VFS, PGOOptions::SampleUse,
764         PGOOptions::NoCSAction, CodeGenOpts.DebugInfoForProfiling,
765         CodeGenOpts.PseudoProbeForProfiling);
766   else if (!CodeGenOpts.MemoryProfileUsePath.empty())
767     // -fmemory-profile-use (without any of the above options)
768     PGOOpt = PGOOptions("", "", "", CodeGenOpts.MemoryProfileUsePath, VFS,
769                         PGOOptions::NoAction, PGOOptions::NoCSAction,
770                         CodeGenOpts.DebugInfoForProfiling);
771   else if (CodeGenOpts.PseudoProbeForProfiling)
772     // -fpseudo-probe-for-profiling
773     PGOOpt = PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr,
774                         PGOOptions::NoAction, PGOOptions::NoCSAction,
775                         CodeGenOpts.DebugInfoForProfiling, true);
776   else if (CodeGenOpts.DebugInfoForProfiling)
777     // -fdebug-info-for-profiling
778     PGOOpt = PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr,
779                         PGOOptions::NoAction, PGOOptions::NoCSAction, true);
780 
781   // Check to see if we want to generate a CS profile.
782   if (CodeGenOpts.hasProfileCSIRInstr()) {
783     assert(!CodeGenOpts.hasProfileCSIRUse() &&
784            "Cannot have both CSProfileUse pass and CSProfileGen pass at "
785            "the same time");
786     if (PGOOpt) {
787       assert(PGOOpt->Action != PGOOptions::IRInstr &&
788              PGOOpt->Action != PGOOptions::SampleUse &&
789              "Cannot run CSProfileGen pass with ProfileGen or SampleUse "
790              " pass");
791       PGOOpt->CSProfileGenFile = CodeGenOpts.InstrProfileOutput.empty()
792                                      ? getDefaultProfileGenName()
793                                      : CodeGenOpts.InstrProfileOutput;
794       PGOOpt->CSAction = PGOOptions::CSIRInstr;
795     } else
796       PGOOpt =
797           PGOOptions("",
798                      CodeGenOpts.InstrProfileOutput.empty()
799                          ? getDefaultProfileGenName()
800                          : CodeGenOpts.InstrProfileOutput,
801                      "", /*MemoryProfile=*/"", nullptr, PGOOptions::NoAction,
802                      PGOOptions::CSIRInstr, CodeGenOpts.DebugInfoForProfiling);
803   }
804   if (TM)
805     TM->setPGOOption(PGOOpt);
806 
807   PipelineTuningOptions PTO;
808   PTO.LoopUnrolling = CodeGenOpts.UnrollLoops;
809   // For historical reasons, loop interleaving is set to mirror setting for loop
810   // unrolling.
811   PTO.LoopInterleaving = CodeGenOpts.UnrollLoops;
812   PTO.LoopVectorization = CodeGenOpts.VectorizeLoop;
813   PTO.SLPVectorization = CodeGenOpts.VectorizeSLP;
814   PTO.MergeFunctions = CodeGenOpts.MergeFunctions;
815   // Only enable CGProfilePass when using integrated assembler, since
816   // non-integrated assemblers don't recognize .cgprofile section.
817   PTO.CallGraphProfile = !CodeGenOpts.DisableIntegratedAS;
818   PTO.UnifiedLTO = CodeGenOpts.UnifiedLTO;
819 
820   LoopAnalysisManager LAM;
821   FunctionAnalysisManager FAM;
822   CGSCCAnalysisManager CGAM;
823   ModuleAnalysisManager MAM;
824 
825   bool DebugPassStructure = CodeGenOpts.DebugPass == "Structure";
826   PassInstrumentationCallbacks PIC;
827   PrintPassOptions PrintPassOpts;
828   PrintPassOpts.Indent = DebugPassStructure;
829   PrintPassOpts.SkipAnalyses = DebugPassStructure;
830   StandardInstrumentations SI(
831       TheModule->getContext(),
832       (CodeGenOpts.DebugPassManager || DebugPassStructure),
833       CodeGenOpts.VerifyEach, PrintPassOpts);
834   SI.registerCallbacks(PIC, &MAM);
835   PassBuilder PB(TM.get(), PTO, PGOOpt, &PIC);
836 
837   // Handle the assignment tracking feature options.
838   switch (CodeGenOpts.getAssignmentTrackingMode()) {
839   case CodeGenOptions::AssignmentTrackingOpts::Forced:
840     PB.registerPipelineStartEPCallback(
841         [&](ModulePassManager &MPM, OptimizationLevel Level) {
842           MPM.addPass(AssignmentTrackingPass());
843         });
844     break;
845   case CodeGenOptions::AssignmentTrackingOpts::Enabled:
846     // Disable assignment tracking in LTO builds for now as the performance
847     // cost is too high. Disable for LLDB tuning due to llvm.org/PR43126.
848     if (!CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.PrepareForLTO &&
849         CodeGenOpts.getDebuggerTuning() != llvm::DebuggerKind::LLDB) {
850       PB.registerPipelineStartEPCallback(
851           [&](ModulePassManager &MPM, OptimizationLevel Level) {
852             // Only use assignment tracking if optimisations are enabled.
853             if (Level != OptimizationLevel::O0)
854               MPM.addPass(AssignmentTrackingPass());
855           });
856     }
857     break;
858   case CodeGenOptions::AssignmentTrackingOpts::Disabled:
859     break;
860   }
861 
862   // Enable verify-debuginfo-preserve-each for new PM.
863   DebugifyEachInstrumentation Debugify;
864   DebugInfoPerPass DebugInfoBeforePass;
865   if (CodeGenOpts.EnableDIPreservationVerify) {
866     Debugify.setDebugifyMode(DebugifyMode::OriginalDebugInfo);
867     Debugify.setDebugInfoBeforePass(DebugInfoBeforePass);
868 
869     if (!CodeGenOpts.DIBugsReportFilePath.empty())
870       Debugify.setOrigDIVerifyBugsReportFilePath(
871           CodeGenOpts.DIBugsReportFilePath);
872     Debugify.registerCallbacks(PIC, MAM);
873   }
874   // Attempt to load pass plugins and register their callbacks with PB.
875   for (auto &PluginFN : CodeGenOpts.PassPlugins) {
876     auto PassPlugin = PassPlugin::Load(PluginFN);
877     if (PassPlugin) {
878       PassPlugin->registerPassBuilderCallbacks(PB);
879     } else {
880       Diags.Report(diag::err_fe_unable_to_load_plugin)
881           << PluginFN << toString(PassPlugin.takeError());
882     }
883   }
884   for (const auto &PassCallback : CodeGenOpts.PassBuilderCallbacks)
885     PassCallback(PB);
886 #define HANDLE_EXTENSION(Ext)                                                  \
887   get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
888 #include "llvm/Support/Extension.def"
889 
890   // Register the target library analysis directly and give it a customized
891   // preset TLI.
892   std::unique_ptr<TargetLibraryInfoImpl> TLII(
893       llvm::driver::createTLII(TargetTriple, CodeGenOpts.getVecLib()));
894   FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
895 
896   // Register all the basic analyses with the managers.
897   PB.registerModuleAnalyses(MAM);
898   PB.registerCGSCCAnalyses(CGAM);
899   PB.registerFunctionAnalyses(FAM);
900   PB.registerLoopAnalyses(LAM);
901   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
902 
903   ModulePassManager MPM;
904   // Add a verifier pass, before any other passes, to catch CodeGen issues.
905   if (CodeGenOpts.VerifyModule)
906     MPM.addPass(VerifierPass());
907 
908   if (!CodeGenOpts.DisableLLVMPasses) {
909     // Map our optimization levels into one of the distinct levels used to
910     // configure the pipeline.
911     OptimizationLevel Level = mapToLevel(CodeGenOpts);
912 
913     const bool PrepareForThinLTO = CodeGenOpts.PrepareForThinLTO;
914     const bool PrepareForLTO = CodeGenOpts.PrepareForLTO;
915 
916     if (LangOpts.ObjCAutoRefCount) {
917       PB.registerPipelineStartEPCallback(
918           [](ModulePassManager &MPM, OptimizationLevel Level) {
919             if (Level != OptimizationLevel::O0)
920               MPM.addPass(
921                   createModuleToFunctionPassAdaptor(ObjCARCExpandPass()));
922           });
923       PB.registerPipelineEarlySimplificationEPCallback(
924           [](ModulePassManager &MPM, OptimizationLevel Level) {
925             if (Level != OptimizationLevel::O0)
926               MPM.addPass(ObjCARCAPElimPass());
927           });
928       PB.registerScalarOptimizerLateEPCallback(
929           [](FunctionPassManager &FPM, OptimizationLevel Level) {
930             if (Level != OptimizationLevel::O0)
931               FPM.addPass(ObjCARCOptPass());
932           });
933     }
934 
935     // If we reached here with a non-empty index file name, then the index
936     // file was empty and we are not performing ThinLTO backend compilation
937     // (used in testing in a distributed build environment).
938     bool IsThinLTOPostLink = !CodeGenOpts.ThinLTOIndexFile.empty();
939     // If so drop any the type test assume sequences inserted for whole program
940     // vtables so that codegen doesn't complain.
941     if (IsThinLTOPostLink)
942       PB.registerPipelineStartEPCallback(
943           [](ModulePassManager &MPM, OptimizationLevel Level) {
944             MPM.addPass(LowerTypeTestsPass(/*ExportSummary=*/nullptr,
945                                            /*ImportSummary=*/nullptr,
946                                            /*DropTypeTests=*/true));
947           });
948 
949     if (CodeGenOpts.InstrumentFunctions ||
950         CodeGenOpts.InstrumentFunctionEntryBare ||
951         CodeGenOpts.InstrumentFunctionsAfterInlining ||
952         CodeGenOpts.InstrumentForProfiling) {
953       PB.registerPipelineStartEPCallback(
954           [](ModulePassManager &MPM, OptimizationLevel Level) {
955             MPM.addPass(createModuleToFunctionPassAdaptor(
956                 EntryExitInstrumenterPass(/*PostInlining=*/false)));
957           });
958       PB.registerOptimizerLastEPCallback(
959           [](ModulePassManager &MPM, OptimizationLevel Level) {
960             MPM.addPass(createModuleToFunctionPassAdaptor(
961                 EntryExitInstrumenterPass(/*PostInlining=*/true)));
962           });
963     }
964 
965     // Register callbacks to schedule sanitizer passes at the appropriate part
966     // of the pipeline.
967     if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds))
968       PB.registerScalarOptimizerLateEPCallback(
969           [](FunctionPassManager &FPM, OptimizationLevel Level) {
970             FPM.addPass(BoundsCheckingPass());
971           });
972 
973     // Don't add sanitizers if we are here from ThinLTO PostLink. That already
974     // done on PreLink stage.
975     if (!IsThinLTOPostLink) {
976       addSanitizers(TargetTriple, CodeGenOpts, LangOpts, PB);
977       addKCFIPass(TargetTriple, LangOpts, PB);
978     }
979 
980     if (std::optional<GCOVOptions> Options =
981             getGCOVOptions(CodeGenOpts, LangOpts))
982       PB.registerPipelineStartEPCallback(
983           [Options](ModulePassManager &MPM, OptimizationLevel Level) {
984             MPM.addPass(GCOVProfilerPass(*Options));
985           });
986     if (std::optional<InstrProfOptions> Options =
987             getInstrProfOptions(CodeGenOpts, LangOpts))
988       PB.registerPipelineStartEPCallback(
989           [Options](ModulePassManager &MPM, OptimizationLevel Level) {
990             MPM.addPass(InstrProfilingLoweringPass(*Options, false));
991           });
992 
993     // TODO: Consider passing the MemoryProfileOutput to the pass builder via
994     // the PGOOptions, and set this up there.
995     if (!CodeGenOpts.MemoryProfileOutput.empty()) {
996       PB.registerOptimizerLastEPCallback(
997           [](ModulePassManager &MPM, OptimizationLevel Level) {
998             MPM.addPass(createModuleToFunctionPassAdaptor(MemProfilerPass()));
999             MPM.addPass(ModuleMemProfilerPass());
1000           });
1001     }
1002 
1003     if (CodeGenOpts.FatLTO) {
1004       assert(CodeGenOpts.UnifiedLTO && "FatLTO requires UnifiedLTO");
1005       MPM.addPass(PB.buildFatLTODefaultPipeline(Level));
1006     } else if (PrepareForThinLTO) {
1007       MPM.addPass(PB.buildThinLTOPreLinkDefaultPipeline(Level));
1008     } else if (PrepareForLTO) {
1009       MPM.addPass(PB.buildLTOPreLinkDefaultPipeline(Level));
1010     } else {
1011       MPM.addPass(PB.buildPerModuleDefaultPipeline(Level));
1012     }
1013   }
1014 
1015   // Re-link against any bitcodes supplied via the -mlink-builtin-bitcode option
1016   // Some optimizations may generate new function calls that would not have
1017   // been linked pre-optimization (i.e. fused sincos calls generated by
1018   // AMDGPULibCalls::fold_sincos.)
1019   if (ClRelinkBuiltinBitcodePostop)
1020     MPM.addPass(LinkInModulesPass(BC, false));
1021 
1022   // Add a verifier pass if requested. We don't have to do this if the action
1023   // requires code generation because there will already be a verifier pass in
1024   // the code-generation pipeline.
1025   // Since we already added a verifier pass above, this
1026   // might even not run the analysis, if previous passes caused no changes.
1027   if (!actionRequiresCodeGen(Action) && CodeGenOpts.VerifyModule)
1028     MPM.addPass(VerifierPass());
1029 
1030   if (Action == Backend_EmitBC || Action == Backend_EmitLL) {
1031     if (CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.DisableLLVMPasses) {
1032       if (!TheModule->getModuleFlag("EnableSplitLTOUnit"))
1033         TheModule->addModuleFlag(llvm::Module::Error, "EnableSplitLTOUnit",
1034                                  CodeGenOpts.EnableSplitLTOUnit);
1035       if (Action == Backend_EmitBC) {
1036         if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
1037           ThinLinkOS = openOutputFile(CodeGenOpts.ThinLinkBitcodeFile);
1038           if (!ThinLinkOS)
1039             return;
1040         }
1041         if (CodeGenOpts.UnifiedLTO)
1042           TheModule->addModuleFlag(llvm::Module::Error, "UnifiedLTO", uint32_t(1));
1043         MPM.addPass(ThinLTOBitcodeWriterPass(
1044             *OS, ThinLinkOS ? &ThinLinkOS->os() : nullptr));
1045       } else {
1046         MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists,
1047                                     /*EmitLTOSummary=*/true));
1048       }
1049     } else {
1050       // Emit a module summary by default for Regular LTO except for ld64
1051       // targets
1052       bool EmitLTOSummary = shouldEmitRegularLTOSummary();
1053       if (EmitLTOSummary) {
1054         if (!TheModule->getModuleFlag("ThinLTO") && !CodeGenOpts.UnifiedLTO)
1055           TheModule->addModuleFlag(llvm::Module::Error, "ThinLTO", uint32_t(0));
1056         if (!TheModule->getModuleFlag("EnableSplitLTOUnit"))
1057           TheModule->addModuleFlag(llvm::Module::Error, "EnableSplitLTOUnit",
1058                                    uint32_t(1));
1059         if (CodeGenOpts.UnifiedLTO)
1060           TheModule->addModuleFlag(llvm::Module::Error, "UnifiedLTO", uint32_t(1));
1061       }
1062       if (Action == Backend_EmitBC)
1063         MPM.addPass(BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists,
1064                                       EmitLTOSummary));
1065       else
1066         MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists,
1067                                     EmitLTOSummary));
1068     }
1069   }
1070   if (CodeGenOpts.FatLTO) {
1071     // Set the EnableSplitLTOUnit and UnifiedLTO module flags, since FatLTO
1072     // uses a different action than Backend_EmitBC or Backend_EmitLL.
1073     if (!TheModule->getModuleFlag("EnableSplitLTOUnit"))
1074       TheModule->addModuleFlag(llvm::Module::Error, "EnableSplitLTOUnit",
1075                                uint32_t(CodeGenOpts.EnableSplitLTOUnit));
1076     // FatLTO always means UnifiedLTO
1077     if (!TheModule->getModuleFlag("UnifiedLTO"))
1078       TheModule->addModuleFlag(llvm::Module::Error, "UnifiedLTO", uint32_t(1));
1079   }
1080 
1081   // Print a textual, '-passes=' compatible, representation of pipeline if
1082   // requested.
1083   if (PrintPipelinePasses) {
1084     MPM.printPipeline(outs(), [&PIC](StringRef ClassName) {
1085       auto PassName = PIC.getPassNameForClassName(ClassName);
1086       return PassName.empty() ? ClassName : PassName;
1087     });
1088     outs() << "\n";
1089     return;
1090   }
1091 
1092   if (LangOpts.HIPStdPar && !LangOpts.CUDAIsDevice &&
1093       LangOpts.HIPStdParInterposeAlloc)
1094     MPM.addPass(HipStdParAllocationInterpositionPass());
1095 
1096   // Now that we have all of the passes ready, run them.
1097   {
1098     PrettyStackTraceString CrashInfo("Optimizer");
1099     llvm::TimeTraceScope TimeScope("Optimizer");
1100     MPM.run(*TheModule, MAM);
1101   }
1102 }
1103 
1104 void EmitAssemblyHelper::RunCodegenPipeline(
1105     BackendAction Action, std::unique_ptr<raw_pwrite_stream> &OS,
1106     std::unique_ptr<llvm::ToolOutputFile> &DwoOS) {
1107   // We still use the legacy PM to run the codegen pipeline since the new PM
1108   // does not work with the codegen pipeline.
1109   // FIXME: make the new PM work with the codegen pipeline.
1110   legacy::PassManager CodeGenPasses;
1111 
1112   // Append any output we need to the pass manager.
1113   switch (Action) {
1114   case Backend_EmitAssembly:
1115   case Backend_EmitMCNull:
1116   case Backend_EmitObj:
1117     CodeGenPasses.add(
1118         createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
1119     if (!CodeGenOpts.SplitDwarfOutput.empty()) {
1120       DwoOS = openOutputFile(CodeGenOpts.SplitDwarfOutput);
1121       if (!DwoOS)
1122         return;
1123     }
1124     if (!AddEmitPasses(CodeGenPasses, Action, *OS,
1125                        DwoOS ? &DwoOS->os() : nullptr))
1126       // FIXME: Should we handle this error differently?
1127       return;
1128     break;
1129   default:
1130     return;
1131   }
1132 
1133   // If -print-pipeline-passes is requested, don't run the legacy pass manager.
1134   // FIXME: when codegen is switched to use the new pass manager, it should also
1135   // emit pass names here.
1136   if (PrintPipelinePasses) {
1137     return;
1138   }
1139 
1140   {
1141     PrettyStackTraceString CrashInfo("Code generation");
1142     llvm::TimeTraceScope TimeScope("CodeGenPasses");
1143     CodeGenPasses.run(*TheModule);
1144   }
1145 }
1146 
1147 void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
1148                                       std::unique_ptr<raw_pwrite_stream> OS,
1149                                       BackendConsumer *BC) {
1150   TimeRegion Region(CodeGenOpts.TimePasses ? &CodeGenerationTime : nullptr);
1151   setCommandLineOpts(CodeGenOpts);
1152 
1153   bool RequiresCodeGen = actionRequiresCodeGen(Action);
1154   CreateTargetMachine(RequiresCodeGen);
1155 
1156   if (RequiresCodeGen && !TM)
1157     return;
1158   if (TM)
1159     TheModule->setDataLayout(TM->createDataLayout());
1160 
1161   // Before executing passes, print the final values of the LLVM options.
1162   cl::PrintOptionValues();
1163 
1164   std::unique_ptr<llvm::ToolOutputFile> ThinLinkOS, DwoOS;
1165   RunOptimizationPipeline(Action, OS, ThinLinkOS, BC);
1166   RunCodegenPipeline(Action, OS, DwoOS);
1167 
1168   if (ThinLinkOS)
1169     ThinLinkOS->keep();
1170   if (DwoOS)
1171     DwoOS->keep();
1172 }
1173 
1174 static void runThinLTOBackend(
1175     DiagnosticsEngine &Diags, ModuleSummaryIndex *CombinedIndex,
1176     llvm::Module *M, const HeaderSearchOptions &HeaderOpts,
1177     const CodeGenOptions &CGOpts, const clang::TargetOptions &TOpts,
1178     const LangOptions &LOpts, std::unique_ptr<raw_pwrite_stream> OS,
1179     std::string SampleProfile, std::string ProfileRemapping,
1180     BackendAction Action) {
1181   DenseMap<StringRef, DenseMap<GlobalValue::GUID, GlobalValueSummary *>>
1182       ModuleToDefinedGVSummaries;
1183   CombinedIndex->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
1184 
1185   setCommandLineOpts(CGOpts);
1186 
1187   // We can simply import the values mentioned in the combined index, since
1188   // we should only invoke this using the individual indexes written out
1189   // via a WriteIndexesThinBackend.
1190   FunctionImporter::ImportMapTy ImportList;
1191   if (!lto::initImportList(*M, *CombinedIndex, ImportList))
1192     return;
1193 
1194   auto AddStream = [&](size_t Task, const Twine &ModuleName) {
1195     return std::make_unique<CachedFileStream>(std::move(OS),
1196                                               CGOpts.ObjectFilenameForDebug);
1197   };
1198   lto::Config Conf;
1199   if (CGOpts.SaveTempsFilePrefix != "") {
1200     if (Error E = Conf.addSaveTemps(CGOpts.SaveTempsFilePrefix + ".",
1201                                     /* UseInputModulePath */ false)) {
1202       handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
1203         errs() << "Error setting up ThinLTO save-temps: " << EIB.message()
1204                << '\n';
1205       });
1206     }
1207   }
1208   Conf.CPU = TOpts.CPU;
1209   Conf.CodeModel = getCodeModel(CGOpts);
1210   Conf.MAttrs = TOpts.Features;
1211   Conf.RelocModel = CGOpts.RelocationModel;
1212   std::optional<CodeGenOptLevel> OptLevelOrNone =
1213       CodeGenOpt::getLevel(CGOpts.OptimizationLevel);
1214   assert(OptLevelOrNone && "Invalid optimization level!");
1215   Conf.CGOptLevel = *OptLevelOrNone;
1216   Conf.OptLevel = CGOpts.OptimizationLevel;
1217   initTargetOptions(Diags, Conf.Options, CGOpts, TOpts, LOpts, HeaderOpts);
1218   Conf.SampleProfile = std::move(SampleProfile);
1219   Conf.PTO.LoopUnrolling = CGOpts.UnrollLoops;
1220   // For historical reasons, loop interleaving is set to mirror setting for loop
1221   // unrolling.
1222   Conf.PTO.LoopInterleaving = CGOpts.UnrollLoops;
1223   Conf.PTO.LoopVectorization = CGOpts.VectorizeLoop;
1224   Conf.PTO.SLPVectorization = CGOpts.VectorizeSLP;
1225   // Only enable CGProfilePass when using integrated assembler, since
1226   // non-integrated assemblers don't recognize .cgprofile section.
1227   Conf.PTO.CallGraphProfile = !CGOpts.DisableIntegratedAS;
1228 
1229   // Context sensitive profile.
1230   if (CGOpts.hasProfileCSIRInstr()) {
1231     Conf.RunCSIRInstr = true;
1232     Conf.CSIRProfile = std::move(CGOpts.InstrProfileOutput);
1233   } else if (CGOpts.hasProfileCSIRUse()) {
1234     Conf.RunCSIRInstr = false;
1235     Conf.CSIRProfile = std::move(CGOpts.ProfileInstrumentUsePath);
1236   }
1237 
1238   Conf.ProfileRemapping = std::move(ProfileRemapping);
1239   Conf.DebugPassManager = CGOpts.DebugPassManager;
1240   Conf.VerifyEach = CGOpts.VerifyEach;
1241   Conf.RemarksWithHotness = CGOpts.DiagnosticsWithHotness;
1242   Conf.RemarksFilename = CGOpts.OptRecordFile;
1243   Conf.RemarksPasses = CGOpts.OptRecordPasses;
1244   Conf.RemarksFormat = CGOpts.OptRecordFormat;
1245   Conf.SplitDwarfFile = CGOpts.SplitDwarfFile;
1246   Conf.SplitDwarfOutput = CGOpts.SplitDwarfOutput;
1247   switch (Action) {
1248   case Backend_EmitNothing:
1249     Conf.PreCodeGenModuleHook = [](size_t Task, const llvm::Module &Mod) {
1250       return false;
1251     };
1252     break;
1253   case Backend_EmitLL:
1254     Conf.PreCodeGenModuleHook = [&](size_t Task, const llvm::Module &Mod) {
1255       M->print(*OS, nullptr, CGOpts.EmitLLVMUseLists);
1256       return false;
1257     };
1258     break;
1259   case Backend_EmitBC:
1260     Conf.PreCodeGenModuleHook = [&](size_t Task, const llvm::Module &Mod) {
1261       WriteBitcodeToFile(*M, *OS, CGOpts.EmitLLVMUseLists);
1262       return false;
1263     };
1264     break;
1265   default:
1266     Conf.CGFileType = getCodeGenFileType(Action);
1267     break;
1268   }
1269   if (Error E =
1270           thinBackend(Conf, -1, AddStream, *M, *CombinedIndex, ImportList,
1271                       ModuleToDefinedGVSummaries[M->getModuleIdentifier()],
1272                       /* ModuleMap */ nullptr, CGOpts.CmdArgs)) {
1273     handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
1274       errs() << "Error running ThinLTO backend: " << EIB.message() << '\n';
1275     });
1276   }
1277 }
1278 
1279 void clang::EmitBackendOutput(
1280     DiagnosticsEngine &Diags, const HeaderSearchOptions &HeaderOpts,
1281     const CodeGenOptions &CGOpts, const clang::TargetOptions &TOpts,
1282     const LangOptions &LOpts, StringRef TDesc, llvm::Module *M,
1283     BackendAction Action, IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
1284     std::unique_ptr<raw_pwrite_stream> OS, BackendConsumer *BC) {
1285 
1286   llvm::TimeTraceScope TimeScope("Backend");
1287 
1288   std::unique_ptr<llvm::Module> EmptyModule;
1289   if (!CGOpts.ThinLTOIndexFile.empty()) {
1290     // If we are performing a ThinLTO importing compile, load the function index
1291     // into memory and pass it into runThinLTOBackend, which will run the
1292     // function importer and invoke LTO passes.
1293     std::unique_ptr<ModuleSummaryIndex> CombinedIndex;
1294     if (Error E = llvm::getModuleSummaryIndexForFile(
1295                       CGOpts.ThinLTOIndexFile,
1296                       /*IgnoreEmptyThinLTOIndexFile*/ true)
1297                       .moveInto(CombinedIndex)) {
1298       logAllUnhandledErrors(std::move(E), errs(),
1299                             "Error loading index file '" +
1300                             CGOpts.ThinLTOIndexFile + "': ");
1301       return;
1302     }
1303 
1304     // A null CombinedIndex means we should skip ThinLTO compilation
1305     // (LLVM will optionally ignore empty index files, returning null instead
1306     // of an error).
1307     if (CombinedIndex) {
1308       if (!CombinedIndex->skipModuleByDistributedBackend()) {
1309         runThinLTOBackend(Diags, CombinedIndex.get(), M, HeaderOpts, CGOpts,
1310                           TOpts, LOpts, std::move(OS), CGOpts.SampleProfileFile,
1311                           CGOpts.ProfileRemappingFile, Action);
1312         return;
1313       }
1314       // Distributed indexing detected that nothing from the module is needed
1315       // for the final linking. So we can skip the compilation. We sill need to
1316       // output an empty object file to make sure that a linker does not fail
1317       // trying to read it. Also for some features, like CFI, we must skip
1318       // the compilation as CombinedIndex does not contain all required
1319       // information.
1320       EmptyModule = std::make_unique<llvm::Module>("empty", M->getContext());
1321       EmptyModule->setTargetTriple(M->getTargetTriple());
1322       M = EmptyModule.get();
1323     }
1324   }
1325 
1326   EmitAssemblyHelper AsmHelper(Diags, HeaderOpts, CGOpts, TOpts, LOpts, M, VFS);
1327   AsmHelper.EmitAssembly(Action, std::move(OS), BC);
1328 
1329   // Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's
1330   // DataLayout.
1331   if (AsmHelper.TM) {
1332     std::string DLDesc = M->getDataLayout().getStringRepresentation();
1333     if (DLDesc != TDesc) {
1334       unsigned DiagID = Diags.getCustomDiagID(
1335           DiagnosticsEngine::Error, "backend data layout '%0' does not match "
1336                                     "expected target description '%1'");
1337       Diags.Report(DiagID) << DLDesc << TDesc;
1338     }
1339   }
1340 }
1341 
1342 // With -fembed-bitcode, save a copy of the llvm IR as data in the
1343 // __LLVM,__bitcode section.
1344 void clang::EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts,
1345                          llvm::MemoryBufferRef Buf) {
1346   if (CGOpts.getEmbedBitcode() == CodeGenOptions::Embed_Off)
1347     return;
1348   llvm::embedBitcodeInModule(
1349       *M, Buf, CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Marker,
1350       CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Bitcode,
1351       CGOpts.CmdArgs);
1352 }
1353 
1354 void clang::EmbedObject(llvm::Module *M, const CodeGenOptions &CGOpts,
1355                         DiagnosticsEngine &Diags) {
1356   if (CGOpts.OffloadObjects.empty())
1357     return;
1358 
1359   for (StringRef OffloadObject : CGOpts.OffloadObjects) {
1360     llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> ObjectOrErr =
1361         llvm::MemoryBuffer::getFileOrSTDIN(OffloadObject);
1362     if (ObjectOrErr.getError()) {
1363       auto DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1364                                           "could not open '%0' for embedding");
1365       Diags.Report(DiagID) << OffloadObject;
1366       return;
1367     }
1368 
1369     llvm::embedBufferInModule(*M, **ObjectOrErr, ".llvm.offloading",
1370                               Align(object::OffloadBinary::getAlignment()));
1371   }
1372 }
1373