xref: /freebsd/contrib/llvm-project/clang/lib/CodeGen/BackendUtil.cpp (revision 9bc300465e48e19d794d88d0c158a2adb92c7197)
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   /// Check whether we should emit a flag for UnifiedLTO.
190   /// The UnifiedLTO module flag should be set when UnifiedLTO is enabled for
191   /// ThinLTO or Full LTO with module summaries.
192   bool shouldEmitUnifiedLTOModueFlag() const {
193     return CodeGenOpts.UnifiedLTO &&
194            (CodeGenOpts.PrepareForThinLTO || shouldEmitRegularLTOSummary());
195   }
196 
197 public:
198   EmitAssemblyHelper(DiagnosticsEngine &_Diags,
199                      const HeaderSearchOptions &HeaderSearchOpts,
200                      const CodeGenOptions &CGOpts,
201                      const clang::TargetOptions &TOpts,
202                      const LangOptions &LOpts, llvm::Module *M,
203                      IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS)
204       : Diags(_Diags), HSOpts(HeaderSearchOpts), CodeGenOpts(CGOpts),
205         TargetOpts(TOpts), LangOpts(LOpts), TheModule(M), VFS(std::move(VFS)),
206         CodeGenerationTime("codegen", "Code Generation Time"),
207         TargetTriple(TheModule->getTargetTriple()) {}
208 
209   ~EmitAssemblyHelper() {
210     if (CodeGenOpts.DisableFree)
211       BuryPointer(std::move(TM));
212   }
213 
214   std::unique_ptr<TargetMachine> TM;
215 
216   // Emit output using the new pass manager for the optimization pipeline.
217   void EmitAssembly(BackendAction Action, std::unique_ptr<raw_pwrite_stream> OS,
218                     BackendConsumer *BC);
219 };
220 } // namespace
221 
222 static SanitizerCoverageOptions
223 getSancovOptsFromCGOpts(const CodeGenOptions &CGOpts) {
224   SanitizerCoverageOptions Opts;
225   Opts.CoverageType =
226       static_cast<SanitizerCoverageOptions::Type>(CGOpts.SanitizeCoverageType);
227   Opts.IndirectCalls = CGOpts.SanitizeCoverageIndirectCalls;
228   Opts.TraceBB = CGOpts.SanitizeCoverageTraceBB;
229   Opts.TraceCmp = CGOpts.SanitizeCoverageTraceCmp;
230   Opts.TraceDiv = CGOpts.SanitizeCoverageTraceDiv;
231   Opts.TraceGep = CGOpts.SanitizeCoverageTraceGep;
232   Opts.Use8bitCounters = CGOpts.SanitizeCoverage8bitCounters;
233   Opts.TracePC = CGOpts.SanitizeCoverageTracePC;
234   Opts.TracePCGuard = CGOpts.SanitizeCoverageTracePCGuard;
235   Opts.NoPrune = CGOpts.SanitizeCoverageNoPrune;
236   Opts.Inline8bitCounters = CGOpts.SanitizeCoverageInline8bitCounters;
237   Opts.InlineBoolFlag = CGOpts.SanitizeCoverageInlineBoolFlag;
238   Opts.PCTable = CGOpts.SanitizeCoveragePCTable;
239   Opts.StackDepth = CGOpts.SanitizeCoverageStackDepth;
240   Opts.TraceLoads = CGOpts.SanitizeCoverageTraceLoads;
241   Opts.TraceStores = CGOpts.SanitizeCoverageTraceStores;
242   Opts.CollectControlFlow = CGOpts.SanitizeCoverageControlFlow;
243   return Opts;
244 }
245 
246 static SanitizerBinaryMetadataOptions
247 getSanitizerBinaryMetadataOptions(const CodeGenOptions &CGOpts) {
248   SanitizerBinaryMetadataOptions Opts;
249   Opts.Covered = CGOpts.SanitizeBinaryMetadataCovered;
250   Opts.Atomics = CGOpts.SanitizeBinaryMetadataAtomics;
251   Opts.UAR = CGOpts.SanitizeBinaryMetadataUAR;
252   return Opts;
253 }
254 
255 // Check if ASan should use GC-friendly instrumentation for globals.
256 // First of all, there is no point if -fdata-sections is off (expect for MachO,
257 // where this is not a factor). Also, on ELF this feature requires an assembler
258 // extension that only works with -integrated-as at the moment.
259 static bool asanUseGlobalsGC(const Triple &T, const CodeGenOptions &CGOpts) {
260   if (!CGOpts.SanitizeAddressGlobalsDeadStripping)
261     return false;
262   switch (T.getObjectFormat()) {
263   case Triple::MachO:
264   case Triple::COFF:
265     return true;
266   case Triple::ELF:
267     return !CGOpts.DisableIntegratedAS;
268   case Triple::GOFF:
269     llvm::report_fatal_error("ASan not implemented for GOFF");
270   case Triple::XCOFF:
271     llvm::report_fatal_error("ASan not implemented for XCOFF.");
272   case Triple::Wasm:
273   case Triple::DXContainer:
274   case Triple::SPIRV:
275   case Triple::UnknownObjectFormat:
276     break;
277   }
278   return false;
279 }
280 
281 static std::optional<llvm::CodeModel::Model>
282 getCodeModel(const CodeGenOptions &CodeGenOpts) {
283   unsigned CodeModel = llvm::StringSwitch<unsigned>(CodeGenOpts.CodeModel)
284                            .Case("tiny", llvm::CodeModel::Tiny)
285                            .Case("small", llvm::CodeModel::Small)
286                            .Case("kernel", llvm::CodeModel::Kernel)
287                            .Case("medium", llvm::CodeModel::Medium)
288                            .Case("large", llvm::CodeModel::Large)
289                            .Case("default", ~1u)
290                            .Default(~0u);
291   assert(CodeModel != ~0u && "invalid code model!");
292   if (CodeModel == ~1u)
293     return std::nullopt;
294   return static_cast<llvm::CodeModel::Model>(CodeModel);
295 }
296 
297 static CodeGenFileType getCodeGenFileType(BackendAction Action) {
298   if (Action == Backend_EmitObj)
299     return CodeGenFileType::ObjectFile;
300   else if (Action == Backend_EmitMCNull)
301     return CodeGenFileType::Null;
302   else {
303     assert(Action == Backend_EmitAssembly && "Invalid action!");
304     return CodeGenFileType::AssemblyFile;
305   }
306 }
307 
308 static bool actionRequiresCodeGen(BackendAction Action) {
309   return Action != Backend_EmitNothing && Action != Backend_EmitBC &&
310          Action != Backend_EmitLL;
311 }
312 
313 static bool initTargetOptions(DiagnosticsEngine &Diags,
314                               llvm::TargetOptions &Options,
315                               const CodeGenOptions &CodeGenOpts,
316                               const clang::TargetOptions &TargetOpts,
317                               const LangOptions &LangOpts,
318                               const HeaderSearchOptions &HSOpts) {
319   switch (LangOpts.getThreadModel()) {
320   case LangOptions::ThreadModelKind::POSIX:
321     Options.ThreadModel = llvm::ThreadModel::POSIX;
322     break;
323   case LangOptions::ThreadModelKind::Single:
324     Options.ThreadModel = llvm::ThreadModel::Single;
325     break;
326   }
327 
328   // Set float ABI type.
329   assert((CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp" ||
330           CodeGenOpts.FloatABI == "hard" || CodeGenOpts.FloatABI.empty()) &&
331          "Invalid Floating Point ABI!");
332   Options.FloatABIType =
333       llvm::StringSwitch<llvm::FloatABI::ABIType>(CodeGenOpts.FloatABI)
334           .Case("soft", llvm::FloatABI::Soft)
335           .Case("softfp", llvm::FloatABI::Soft)
336           .Case("hard", llvm::FloatABI::Hard)
337           .Default(llvm::FloatABI::Default);
338 
339   // Set FP fusion mode.
340   switch (LangOpts.getDefaultFPContractMode()) {
341   case LangOptions::FPM_Off:
342     // Preserve any contraction performed by the front-end.  (Strict performs
343     // splitting of the muladd intrinsic in the backend.)
344     Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
345     break;
346   case LangOptions::FPM_On:
347   case LangOptions::FPM_FastHonorPragmas:
348     Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
349     break;
350   case LangOptions::FPM_Fast:
351     Options.AllowFPOpFusion = llvm::FPOpFusion::Fast;
352     break;
353   }
354 
355   Options.BinutilsVersion =
356       llvm::TargetMachine::parseBinutilsVersion(CodeGenOpts.BinutilsVersion);
357   Options.UseInitArray = CodeGenOpts.UseInitArray;
358   Options.DisableIntegratedAS = CodeGenOpts.DisableIntegratedAS;
359   Options.CompressDebugSections = CodeGenOpts.getCompressDebugSections();
360   Options.RelaxELFRelocations = CodeGenOpts.RelaxELFRelocations;
361 
362   // Set EABI version.
363   Options.EABIVersion = TargetOpts.EABIVersion;
364 
365   if (LangOpts.hasSjLjExceptions())
366     Options.ExceptionModel = llvm::ExceptionHandling::SjLj;
367   if (LangOpts.hasSEHExceptions())
368     Options.ExceptionModel = llvm::ExceptionHandling::WinEH;
369   if (LangOpts.hasDWARFExceptions())
370     Options.ExceptionModel = llvm::ExceptionHandling::DwarfCFI;
371   if (LangOpts.hasWasmExceptions())
372     Options.ExceptionModel = llvm::ExceptionHandling::Wasm;
373 
374   Options.NoInfsFPMath = LangOpts.NoHonorInfs;
375   Options.NoNaNsFPMath = LangOpts.NoHonorNaNs;
376   Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS;
377   Options.UnsafeFPMath = LangOpts.AllowFPReassoc && LangOpts.AllowRecip &&
378                          LangOpts.NoSignedZero && LangOpts.ApproxFunc &&
379                          (LangOpts.getDefaultFPContractMode() ==
380                               LangOptions::FPModeKind::FPM_Fast ||
381                           LangOpts.getDefaultFPContractMode() ==
382                               LangOptions::FPModeKind::FPM_FastHonorPragmas);
383   Options.ApproxFuncFPMath = LangOpts.ApproxFunc;
384 
385   Options.BBSections =
386       llvm::StringSwitch<llvm::BasicBlockSection>(CodeGenOpts.BBSections)
387           .Case("all", llvm::BasicBlockSection::All)
388           .Case("labels", llvm::BasicBlockSection::Labels)
389           .StartsWith("list=", llvm::BasicBlockSection::List)
390           .Case("none", llvm::BasicBlockSection::None)
391           .Default(llvm::BasicBlockSection::None);
392 
393   if (Options.BBSections == llvm::BasicBlockSection::List) {
394     ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
395         MemoryBuffer::getFile(CodeGenOpts.BBSections.substr(5));
396     if (!MBOrErr) {
397       Diags.Report(diag::err_fe_unable_to_load_basic_block_sections_file)
398           << MBOrErr.getError().message();
399       return false;
400     }
401     Options.BBSectionsFuncListBuf = std::move(*MBOrErr);
402   }
403 
404   Options.EnableMachineFunctionSplitter = CodeGenOpts.SplitMachineFunctions;
405   Options.FunctionSections = CodeGenOpts.FunctionSections;
406   Options.DataSections = CodeGenOpts.DataSections;
407   Options.IgnoreXCOFFVisibility = LangOpts.IgnoreXCOFFVisibility;
408   Options.UniqueSectionNames = CodeGenOpts.UniqueSectionNames;
409   Options.UniqueBasicBlockSectionNames =
410       CodeGenOpts.UniqueBasicBlockSectionNames;
411   Options.TLSSize = CodeGenOpts.TLSSize;
412   Options.EnableTLSDESC = CodeGenOpts.EnableTLSDESC;
413   Options.EmulatedTLS = CodeGenOpts.EmulatedTLS;
414   Options.DebuggerTuning = CodeGenOpts.getDebuggerTuning();
415   Options.EmitStackSizeSection = CodeGenOpts.StackSizeSection;
416   Options.StackUsageOutput = CodeGenOpts.StackUsageOutput;
417   Options.EmitAddrsig = CodeGenOpts.Addrsig;
418   Options.ForceDwarfFrameSection = CodeGenOpts.ForceDwarfFrameSection;
419   Options.EmitCallSiteInfo = CodeGenOpts.EmitCallSiteInfo;
420   Options.EnableAIXExtendedAltivecABI = LangOpts.EnableAIXExtendedAltivecABI;
421   Options.XRayFunctionIndex = CodeGenOpts.XRayFunctionIndex;
422   Options.LoopAlignment = CodeGenOpts.LoopAlignment;
423   Options.DebugStrictDwarf = CodeGenOpts.DebugStrictDwarf;
424   Options.ObjectFilenameForDebug = CodeGenOpts.ObjectFilenameForDebug;
425   Options.Hotpatch = CodeGenOpts.HotPatch;
426   Options.JMCInstrument = CodeGenOpts.JMCInstrument;
427   Options.XCOFFReadOnlyPointers = CodeGenOpts.XCOFFReadOnlyPointers;
428 
429   switch (CodeGenOpts.getSwiftAsyncFramePointer()) {
430   case CodeGenOptions::SwiftAsyncFramePointerKind::Auto:
431     Options.SwiftAsyncFramePointer =
432         SwiftAsyncFramePointerMode::DeploymentBased;
433     break;
434 
435   case CodeGenOptions::SwiftAsyncFramePointerKind::Always:
436     Options.SwiftAsyncFramePointer = SwiftAsyncFramePointerMode::Always;
437     break;
438 
439   case CodeGenOptions::SwiftAsyncFramePointerKind::Never:
440     Options.SwiftAsyncFramePointer = SwiftAsyncFramePointerMode::Never;
441     break;
442   }
443 
444   Options.MCOptions.SplitDwarfFile = CodeGenOpts.SplitDwarfFile;
445   Options.MCOptions.EmitDwarfUnwind = CodeGenOpts.getEmitDwarfUnwind();
446   Options.MCOptions.EmitCompactUnwindNonCanonical =
447       CodeGenOpts.EmitCompactUnwindNonCanonical;
448   Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll;
449   Options.MCOptions.MCSaveTempLabels = CodeGenOpts.SaveTempLabels;
450   Options.MCOptions.MCUseDwarfDirectory =
451       CodeGenOpts.NoDwarfDirectoryAsm
452           ? llvm::MCTargetOptions::DisableDwarfDirectory
453           : llvm::MCTargetOptions::EnableDwarfDirectory;
454   Options.MCOptions.MCNoExecStack = CodeGenOpts.NoExecStack;
455   Options.MCOptions.MCIncrementalLinkerCompatible =
456       CodeGenOpts.IncrementalLinkerCompatible;
457   Options.MCOptions.MCFatalWarnings = CodeGenOpts.FatalWarnings;
458   Options.MCOptions.MCNoWarn = CodeGenOpts.NoWarn;
459   Options.MCOptions.AsmVerbose = CodeGenOpts.AsmVerbose;
460   Options.MCOptions.Dwarf64 = CodeGenOpts.Dwarf64;
461   Options.MCOptions.PreserveAsmComments = CodeGenOpts.PreserveAsmComments;
462   Options.MCOptions.ABIName = TargetOpts.ABI;
463   for (const auto &Entry : HSOpts.UserEntries)
464     if (!Entry.IsFramework &&
465         (Entry.Group == frontend::IncludeDirGroup::Quoted ||
466          Entry.Group == frontend::IncludeDirGroup::Angled ||
467          Entry.Group == frontend::IncludeDirGroup::System))
468       Options.MCOptions.IASSearchPaths.push_back(
469           Entry.IgnoreSysRoot ? Entry.Path : HSOpts.Sysroot + Entry.Path);
470   Options.MCOptions.Argv0 = CodeGenOpts.Argv0;
471   Options.MCOptions.CommandLineArgs = CodeGenOpts.CommandLineArgs;
472   Options.MCOptions.AsSecureLogFile = CodeGenOpts.AsSecureLogFile;
473   Options.MCOptions.PPCUseFullRegisterNames =
474       CodeGenOpts.PPCUseFullRegisterNames;
475   Options.MisExpect = CodeGenOpts.MisExpect;
476 
477   return true;
478 }
479 
480 static std::optional<GCOVOptions>
481 getGCOVOptions(const CodeGenOptions &CodeGenOpts, const LangOptions &LangOpts) {
482   if (CodeGenOpts.CoverageNotesFile.empty() &&
483       CodeGenOpts.CoverageDataFile.empty())
484     return std::nullopt;
485   // Not using 'GCOVOptions::getDefault' allows us to avoid exiting if
486   // LLVM's -default-gcov-version flag is set to something invalid.
487   GCOVOptions Options;
488   Options.EmitNotes = !CodeGenOpts.CoverageNotesFile.empty();
489   Options.EmitData = !CodeGenOpts.CoverageDataFile.empty();
490   llvm::copy(CodeGenOpts.CoverageVersion, std::begin(Options.Version));
491   Options.NoRedZone = CodeGenOpts.DisableRedZone;
492   Options.Filter = CodeGenOpts.ProfileFilterFiles;
493   Options.Exclude = CodeGenOpts.ProfileExcludeFiles;
494   Options.Atomic = CodeGenOpts.AtomicProfileUpdate;
495   return Options;
496 }
497 
498 static std::optional<InstrProfOptions>
499 getInstrProfOptions(const CodeGenOptions &CodeGenOpts,
500                     const LangOptions &LangOpts) {
501   if (!CodeGenOpts.hasProfileClangInstr())
502     return std::nullopt;
503   InstrProfOptions Options;
504   Options.NoRedZone = CodeGenOpts.DisableRedZone;
505   Options.InstrProfileOutput = CodeGenOpts.InstrProfileOutput;
506   Options.Atomic = CodeGenOpts.AtomicProfileUpdate;
507   return Options;
508 }
509 
510 static void setCommandLineOpts(const CodeGenOptions &CodeGenOpts) {
511   SmallVector<const char *, 16> BackendArgs;
512   BackendArgs.push_back("clang"); // Fake program name.
513   if (!CodeGenOpts.DebugPass.empty()) {
514     BackendArgs.push_back("-debug-pass");
515     BackendArgs.push_back(CodeGenOpts.DebugPass.c_str());
516   }
517   if (!CodeGenOpts.LimitFloatPrecision.empty()) {
518     BackendArgs.push_back("-limit-float-precision");
519     BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str());
520   }
521   // Check for the default "clang" invocation that won't set any cl::opt values.
522   // Skip trying to parse the command line invocation to avoid the issues
523   // described below.
524   if (BackendArgs.size() == 1)
525     return;
526   BackendArgs.push_back(nullptr);
527   // FIXME: The command line parser below is not thread-safe and shares a global
528   // state, so this call might crash or overwrite the options of another Clang
529   // instance in the same process.
530   llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
531                                     BackendArgs.data());
532 }
533 
534 void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
535   // Create the TargetMachine for generating code.
536   std::string Error;
537   std::string Triple = TheModule->getTargetTriple();
538   const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
539   if (!TheTarget) {
540     if (MustCreateTM)
541       Diags.Report(diag::err_fe_unable_to_create_target) << Error;
542     return;
543   }
544 
545   std::optional<llvm::CodeModel::Model> CM = getCodeModel(CodeGenOpts);
546   std::string FeaturesStr =
547       llvm::join(TargetOpts.Features.begin(), TargetOpts.Features.end(), ",");
548   llvm::Reloc::Model RM = CodeGenOpts.RelocationModel;
549   std::optional<CodeGenOptLevel> OptLevelOrNone =
550       CodeGenOpt::getLevel(CodeGenOpts.OptimizationLevel);
551   assert(OptLevelOrNone && "Invalid optimization level!");
552   CodeGenOptLevel OptLevel = *OptLevelOrNone;
553 
554   llvm::TargetOptions Options;
555   if (!initTargetOptions(Diags, Options, CodeGenOpts, TargetOpts, LangOpts,
556                          HSOpts))
557     return;
558   TM.reset(TheTarget->createTargetMachine(Triple, TargetOpts.CPU, FeaturesStr,
559                                           Options, RM, CM, OptLevel));
560   TM->setLargeDataThreshold(CodeGenOpts.LargeDataThreshold);
561 }
562 
563 bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager &CodeGenPasses,
564                                        BackendAction Action,
565                                        raw_pwrite_stream &OS,
566                                        raw_pwrite_stream *DwoOS) {
567   // Add LibraryInfo.
568   std::unique_ptr<TargetLibraryInfoImpl> TLII(
569       llvm::driver::createTLII(TargetTriple, CodeGenOpts.getVecLib()));
570   CodeGenPasses.add(new TargetLibraryInfoWrapperPass(*TLII));
571 
572   // Normal mode, emit a .s or .o file by running the code generator. Note,
573   // this also adds codegenerator level optimization passes.
574   CodeGenFileType CGFT = getCodeGenFileType(Action);
575 
576   // Add ObjC ARC final-cleanup optimizations. This is done as part of the
577   // "codegen" passes so that it isn't run multiple times when there is
578   // inlining happening.
579   if (CodeGenOpts.OptimizationLevel > 0)
580     CodeGenPasses.add(createObjCARCContractPass());
581 
582   if (TM->addPassesToEmitFile(CodeGenPasses, OS, DwoOS, CGFT,
583                               /*DisableVerify=*/!CodeGenOpts.VerifyModule)) {
584     Diags.Report(diag::err_fe_unable_to_interface_with_target);
585     return false;
586   }
587 
588   return true;
589 }
590 
591 static OptimizationLevel mapToLevel(const CodeGenOptions &Opts) {
592   switch (Opts.OptimizationLevel) {
593   default:
594     llvm_unreachable("Invalid optimization level!");
595 
596   case 0:
597     return OptimizationLevel::O0;
598 
599   case 1:
600     return OptimizationLevel::O1;
601 
602   case 2:
603     switch (Opts.OptimizeSize) {
604     default:
605       llvm_unreachable("Invalid optimization level for size!");
606 
607     case 0:
608       return OptimizationLevel::O2;
609 
610     case 1:
611       return OptimizationLevel::Os;
612 
613     case 2:
614       return OptimizationLevel::Oz;
615     }
616 
617   case 3:
618     return OptimizationLevel::O3;
619   }
620 }
621 
622 static void addKCFIPass(const Triple &TargetTriple, const LangOptions &LangOpts,
623                         PassBuilder &PB) {
624   // If the back-end supports KCFI operand bundle lowering, skip KCFIPass.
625   if (TargetTriple.getArch() == llvm::Triple::x86_64 ||
626       TargetTriple.isAArch64(64) || TargetTriple.isRISCV())
627     return;
628 
629   // Ensure we lower KCFI operand bundles with -O0.
630   PB.registerOptimizerLastEPCallback(
631       [&](ModulePassManager &MPM, OptimizationLevel Level) {
632         if (Level == OptimizationLevel::O0 &&
633             LangOpts.Sanitize.has(SanitizerKind::KCFI))
634           MPM.addPass(createModuleToFunctionPassAdaptor(KCFIPass()));
635       });
636 
637   // When optimizations are requested, run KCIFPass after InstCombine to
638   // avoid unnecessary checks.
639   PB.registerPeepholeEPCallback(
640       [&](FunctionPassManager &FPM, OptimizationLevel Level) {
641         if (Level != OptimizationLevel::O0 &&
642             LangOpts.Sanitize.has(SanitizerKind::KCFI))
643           FPM.addPass(KCFIPass());
644       });
645 }
646 
647 static void addSanitizers(const Triple &TargetTriple,
648                           const CodeGenOptions &CodeGenOpts,
649                           const LangOptions &LangOpts, PassBuilder &PB) {
650   auto SanitizersCallback = [&](ModulePassManager &MPM,
651                                 OptimizationLevel Level) {
652     if (CodeGenOpts.hasSanitizeCoverage()) {
653       auto SancovOpts = getSancovOptsFromCGOpts(CodeGenOpts);
654       MPM.addPass(SanitizerCoveragePass(
655           SancovOpts, CodeGenOpts.SanitizeCoverageAllowlistFiles,
656           CodeGenOpts.SanitizeCoverageIgnorelistFiles));
657     }
658 
659     if (CodeGenOpts.hasSanitizeBinaryMetadata()) {
660       MPM.addPass(SanitizerBinaryMetadataPass(
661           getSanitizerBinaryMetadataOptions(CodeGenOpts),
662           CodeGenOpts.SanitizeMetadataIgnorelistFiles));
663     }
664 
665     auto MSanPass = [&](SanitizerMask Mask, bool CompileKernel) {
666       if (LangOpts.Sanitize.has(Mask)) {
667         int TrackOrigins = CodeGenOpts.SanitizeMemoryTrackOrigins;
668         bool Recover = CodeGenOpts.SanitizeRecover.has(Mask);
669 
670         MemorySanitizerOptions options(TrackOrigins, Recover, CompileKernel,
671                                        CodeGenOpts.SanitizeMemoryParamRetval);
672         MPM.addPass(MemorySanitizerPass(options));
673         if (Level != OptimizationLevel::O0) {
674           // MemorySanitizer inserts complex instrumentation that mostly follows
675           // the logic of the original code, but operates on "shadow" values. It
676           // can benefit from re-running some general purpose optimization
677           // passes.
678           MPM.addPass(RequireAnalysisPass<GlobalsAA, llvm::Module>());
679           FunctionPassManager FPM;
680           FPM.addPass(EarlyCSEPass(true /* Enable mem-ssa. */));
681           FPM.addPass(InstCombinePass());
682           FPM.addPass(JumpThreadingPass());
683           FPM.addPass(GVNPass());
684           FPM.addPass(InstCombinePass());
685           MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
686         }
687       }
688     };
689     MSanPass(SanitizerKind::Memory, false);
690     MSanPass(SanitizerKind::KernelMemory, true);
691 
692     if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
693       MPM.addPass(ModuleThreadSanitizerPass());
694       MPM.addPass(createModuleToFunctionPassAdaptor(ThreadSanitizerPass()));
695     }
696 
697     auto ASanPass = [&](SanitizerMask Mask, bool CompileKernel) {
698       if (LangOpts.Sanitize.has(Mask)) {
699         bool UseGlobalGC = asanUseGlobalsGC(TargetTriple, CodeGenOpts);
700         bool UseOdrIndicator = CodeGenOpts.SanitizeAddressUseOdrIndicator;
701         llvm::AsanDtorKind DestructorKind =
702             CodeGenOpts.getSanitizeAddressDtor();
703         AddressSanitizerOptions Opts;
704         Opts.CompileKernel = CompileKernel;
705         Opts.Recover = CodeGenOpts.SanitizeRecover.has(Mask);
706         Opts.UseAfterScope = CodeGenOpts.SanitizeAddressUseAfterScope;
707         Opts.UseAfterReturn = CodeGenOpts.getSanitizeAddressUseAfterReturn();
708         MPM.addPass(AddressSanitizerPass(Opts, UseGlobalGC, UseOdrIndicator,
709                                          DestructorKind));
710       }
711     };
712     ASanPass(SanitizerKind::Address, false);
713     ASanPass(SanitizerKind::KernelAddress, true);
714 
715     auto HWASanPass = [&](SanitizerMask Mask, bool CompileKernel) {
716       if (LangOpts.Sanitize.has(Mask)) {
717         bool Recover = CodeGenOpts.SanitizeRecover.has(Mask);
718         MPM.addPass(HWAddressSanitizerPass(
719             {CompileKernel, Recover,
720              /*DisableOptimization=*/CodeGenOpts.OptimizationLevel == 0}));
721       }
722     };
723     HWASanPass(SanitizerKind::HWAddress, false);
724     HWASanPass(SanitizerKind::KernelHWAddress, true);
725 
726     if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) {
727       MPM.addPass(DataFlowSanitizerPass(LangOpts.NoSanitizeFiles));
728     }
729   };
730   if (ClSanitizeOnOptimizerEarlyEP) {
731     PB.registerOptimizerEarlyEPCallback(
732         [SanitizersCallback](ModulePassManager &MPM, OptimizationLevel Level) {
733           ModulePassManager NewMPM;
734           SanitizersCallback(NewMPM, Level);
735           if (!NewMPM.isEmpty()) {
736             // Sanitizers can abandon<GlobalsAA>.
737             NewMPM.addPass(RequireAnalysisPass<GlobalsAA, llvm::Module>());
738             MPM.addPass(std::move(NewMPM));
739           }
740         });
741   } else {
742     // LastEP does not need GlobalsAA.
743     PB.registerOptimizerLastEPCallback(SanitizersCallback);
744   }
745 }
746 
747 void EmitAssemblyHelper::RunOptimizationPipeline(
748     BackendAction Action, std::unique_ptr<raw_pwrite_stream> &OS,
749     std::unique_ptr<llvm::ToolOutputFile> &ThinLinkOS, BackendConsumer *BC) {
750   std::optional<PGOOptions> PGOOpt;
751 
752   if (CodeGenOpts.hasProfileIRInstr())
753     // -fprofile-generate.
754     PGOOpt = PGOOptions(
755         CodeGenOpts.InstrProfileOutput.empty() ? getDefaultProfileGenName()
756                                                : CodeGenOpts.InstrProfileOutput,
757         "", "", CodeGenOpts.MemoryProfileUsePath, nullptr, PGOOptions::IRInstr,
758         PGOOptions::NoCSAction, CodeGenOpts.DebugInfoForProfiling,
759         /*PseudoProbeForProfiling=*/false, CodeGenOpts.AtomicProfileUpdate);
760   else if (CodeGenOpts.hasProfileIRUse()) {
761     // -fprofile-use.
762     auto CSAction = CodeGenOpts.hasProfileCSIRUse() ? PGOOptions::CSIRUse
763                                                     : PGOOptions::NoCSAction;
764     PGOOpt = PGOOptions(
765         CodeGenOpts.ProfileInstrumentUsePath, "",
766         CodeGenOpts.ProfileRemappingFile, CodeGenOpts.MemoryProfileUsePath, VFS,
767         PGOOptions::IRUse, CSAction, CodeGenOpts.DebugInfoForProfiling);
768   } else if (!CodeGenOpts.SampleProfileFile.empty())
769     // -fprofile-sample-use
770     PGOOpt = PGOOptions(
771         CodeGenOpts.SampleProfileFile, "", CodeGenOpts.ProfileRemappingFile,
772         CodeGenOpts.MemoryProfileUsePath, VFS, PGOOptions::SampleUse,
773         PGOOptions::NoCSAction, CodeGenOpts.DebugInfoForProfiling,
774         CodeGenOpts.PseudoProbeForProfiling);
775   else if (!CodeGenOpts.MemoryProfileUsePath.empty())
776     // -fmemory-profile-use (without any of the above options)
777     PGOOpt = PGOOptions("", "", "", CodeGenOpts.MemoryProfileUsePath, VFS,
778                         PGOOptions::NoAction, PGOOptions::NoCSAction,
779                         CodeGenOpts.DebugInfoForProfiling);
780   else if (CodeGenOpts.PseudoProbeForProfiling)
781     // -fpseudo-probe-for-profiling
782     PGOOpt = PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr,
783                         PGOOptions::NoAction, PGOOptions::NoCSAction,
784                         CodeGenOpts.DebugInfoForProfiling, true);
785   else if (CodeGenOpts.DebugInfoForProfiling)
786     // -fdebug-info-for-profiling
787     PGOOpt = PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr,
788                         PGOOptions::NoAction, PGOOptions::NoCSAction, true);
789 
790   // Check to see if we want to generate a CS profile.
791   if (CodeGenOpts.hasProfileCSIRInstr()) {
792     assert(!CodeGenOpts.hasProfileCSIRUse() &&
793            "Cannot have both CSProfileUse pass and CSProfileGen pass at "
794            "the same time");
795     if (PGOOpt) {
796       assert(PGOOpt->Action != PGOOptions::IRInstr &&
797              PGOOpt->Action != PGOOptions::SampleUse &&
798              "Cannot run CSProfileGen pass with ProfileGen or SampleUse "
799              " pass");
800       PGOOpt->CSProfileGenFile = CodeGenOpts.InstrProfileOutput.empty()
801                                      ? getDefaultProfileGenName()
802                                      : CodeGenOpts.InstrProfileOutput;
803       PGOOpt->CSAction = PGOOptions::CSIRInstr;
804     } else
805       PGOOpt =
806           PGOOptions("",
807                      CodeGenOpts.InstrProfileOutput.empty()
808                          ? getDefaultProfileGenName()
809                          : CodeGenOpts.InstrProfileOutput,
810                      "", /*MemoryProfile=*/"", nullptr, PGOOptions::NoAction,
811                      PGOOptions::CSIRInstr, CodeGenOpts.DebugInfoForProfiling);
812   }
813   if (TM)
814     TM->setPGOOption(PGOOpt);
815 
816   PipelineTuningOptions PTO;
817   PTO.LoopUnrolling = CodeGenOpts.UnrollLoops;
818   // For historical reasons, loop interleaving is set to mirror setting for loop
819   // unrolling.
820   PTO.LoopInterleaving = CodeGenOpts.UnrollLoops;
821   PTO.LoopVectorization = CodeGenOpts.VectorizeLoop;
822   PTO.SLPVectorization = CodeGenOpts.VectorizeSLP;
823   PTO.MergeFunctions = CodeGenOpts.MergeFunctions;
824   // Only enable CGProfilePass when using integrated assembler, since
825   // non-integrated assemblers don't recognize .cgprofile section.
826   PTO.CallGraphProfile = !CodeGenOpts.DisableIntegratedAS;
827   PTO.UnifiedLTO = CodeGenOpts.UnifiedLTO;
828 
829   LoopAnalysisManager LAM;
830   FunctionAnalysisManager FAM;
831   CGSCCAnalysisManager CGAM;
832   ModuleAnalysisManager MAM;
833 
834   bool DebugPassStructure = CodeGenOpts.DebugPass == "Structure";
835   PassInstrumentationCallbacks PIC;
836   PrintPassOptions PrintPassOpts;
837   PrintPassOpts.Indent = DebugPassStructure;
838   PrintPassOpts.SkipAnalyses = DebugPassStructure;
839   StandardInstrumentations SI(
840       TheModule->getContext(),
841       (CodeGenOpts.DebugPassManager || DebugPassStructure),
842       CodeGenOpts.VerifyEach, PrintPassOpts);
843   SI.registerCallbacks(PIC, &MAM);
844   PassBuilder PB(TM.get(), PTO, PGOOpt, &PIC);
845 
846   // Handle the assignment tracking feature options.
847   switch (CodeGenOpts.getAssignmentTrackingMode()) {
848   case CodeGenOptions::AssignmentTrackingOpts::Forced:
849     PB.registerPipelineStartEPCallback(
850         [&](ModulePassManager &MPM, OptimizationLevel Level) {
851           MPM.addPass(AssignmentTrackingPass());
852         });
853     break;
854   case CodeGenOptions::AssignmentTrackingOpts::Enabled:
855     // Disable assignment tracking in LTO builds for now as the performance
856     // cost is too high. Disable for LLDB tuning due to llvm.org/PR43126.
857     if (!CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.PrepareForLTO &&
858         CodeGenOpts.getDebuggerTuning() != llvm::DebuggerKind::LLDB) {
859       PB.registerPipelineStartEPCallback(
860           [&](ModulePassManager &MPM, OptimizationLevel Level) {
861             // Only use assignment tracking if optimisations are enabled.
862             if (Level != OptimizationLevel::O0)
863               MPM.addPass(AssignmentTrackingPass());
864           });
865     }
866     break;
867   case CodeGenOptions::AssignmentTrackingOpts::Disabled:
868     break;
869   }
870 
871   // Enable verify-debuginfo-preserve-each for new PM.
872   DebugifyEachInstrumentation Debugify;
873   DebugInfoPerPass DebugInfoBeforePass;
874   if (CodeGenOpts.EnableDIPreservationVerify) {
875     Debugify.setDebugifyMode(DebugifyMode::OriginalDebugInfo);
876     Debugify.setDebugInfoBeforePass(DebugInfoBeforePass);
877 
878     if (!CodeGenOpts.DIBugsReportFilePath.empty())
879       Debugify.setOrigDIVerifyBugsReportFilePath(
880           CodeGenOpts.DIBugsReportFilePath);
881     Debugify.registerCallbacks(PIC, MAM);
882   }
883   // Attempt to load pass plugins and register their callbacks with PB.
884   for (auto &PluginFN : CodeGenOpts.PassPlugins) {
885     auto PassPlugin = PassPlugin::Load(PluginFN);
886     if (PassPlugin) {
887       PassPlugin->registerPassBuilderCallbacks(PB);
888     } else {
889       Diags.Report(diag::err_fe_unable_to_load_plugin)
890           << PluginFN << toString(PassPlugin.takeError());
891     }
892   }
893   for (const auto &PassCallback : CodeGenOpts.PassBuilderCallbacks)
894     PassCallback(PB);
895 #define HANDLE_EXTENSION(Ext)                                                  \
896   get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
897 #include "llvm/Support/Extension.def"
898 
899   // Register the target library analysis directly and give it a customized
900   // preset TLI.
901   std::unique_ptr<TargetLibraryInfoImpl> TLII(
902       llvm::driver::createTLII(TargetTriple, CodeGenOpts.getVecLib()));
903   FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
904 
905   // Register all the basic analyses with the managers.
906   PB.registerModuleAnalyses(MAM);
907   PB.registerCGSCCAnalyses(CGAM);
908   PB.registerFunctionAnalyses(FAM);
909   PB.registerLoopAnalyses(LAM);
910   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
911 
912   ModulePassManager MPM;
913   // Add a verifier pass, before any other passes, to catch CodeGen issues.
914   if (CodeGenOpts.VerifyModule)
915     MPM.addPass(VerifierPass());
916 
917   if (!CodeGenOpts.DisableLLVMPasses) {
918     // Map our optimization levels into one of the distinct levels used to
919     // configure the pipeline.
920     OptimizationLevel Level = mapToLevel(CodeGenOpts);
921 
922     const bool PrepareForThinLTO = CodeGenOpts.PrepareForThinLTO;
923     const bool PrepareForLTO = CodeGenOpts.PrepareForLTO;
924 
925     if (LangOpts.ObjCAutoRefCount) {
926       PB.registerPipelineStartEPCallback(
927           [](ModulePassManager &MPM, OptimizationLevel Level) {
928             if (Level != OptimizationLevel::O0)
929               MPM.addPass(
930                   createModuleToFunctionPassAdaptor(ObjCARCExpandPass()));
931           });
932       PB.registerPipelineEarlySimplificationEPCallback(
933           [](ModulePassManager &MPM, OptimizationLevel Level) {
934             if (Level != OptimizationLevel::O0)
935               MPM.addPass(ObjCARCAPElimPass());
936           });
937       PB.registerScalarOptimizerLateEPCallback(
938           [](FunctionPassManager &FPM, OptimizationLevel Level) {
939             if (Level != OptimizationLevel::O0)
940               FPM.addPass(ObjCARCOptPass());
941           });
942     }
943 
944     // If we reached here with a non-empty index file name, then the index
945     // file was empty and we are not performing ThinLTO backend compilation
946     // (used in testing in a distributed build environment).
947     bool IsThinLTOPostLink = !CodeGenOpts.ThinLTOIndexFile.empty();
948     // If so drop any the type test assume sequences inserted for whole program
949     // vtables so that codegen doesn't complain.
950     if (IsThinLTOPostLink)
951       PB.registerPipelineStartEPCallback(
952           [](ModulePassManager &MPM, OptimizationLevel Level) {
953             MPM.addPass(LowerTypeTestsPass(/*ExportSummary=*/nullptr,
954                                            /*ImportSummary=*/nullptr,
955                                            /*DropTypeTests=*/true));
956           });
957 
958     if (CodeGenOpts.InstrumentFunctions ||
959         CodeGenOpts.InstrumentFunctionEntryBare ||
960         CodeGenOpts.InstrumentFunctionsAfterInlining ||
961         CodeGenOpts.InstrumentForProfiling) {
962       PB.registerPipelineStartEPCallback(
963           [](ModulePassManager &MPM, OptimizationLevel Level) {
964             MPM.addPass(createModuleToFunctionPassAdaptor(
965                 EntryExitInstrumenterPass(/*PostInlining=*/false)));
966           });
967       PB.registerOptimizerLastEPCallback(
968           [](ModulePassManager &MPM, OptimizationLevel Level) {
969             MPM.addPass(createModuleToFunctionPassAdaptor(
970                 EntryExitInstrumenterPass(/*PostInlining=*/true)));
971           });
972     }
973 
974     // Register callbacks to schedule sanitizer passes at the appropriate part
975     // of the pipeline.
976     if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds))
977       PB.registerScalarOptimizerLateEPCallback(
978           [](FunctionPassManager &FPM, OptimizationLevel Level) {
979             FPM.addPass(BoundsCheckingPass());
980           });
981 
982     // Don't add sanitizers if we are here from ThinLTO PostLink. That already
983     // done on PreLink stage.
984     if (!IsThinLTOPostLink) {
985       addSanitizers(TargetTriple, CodeGenOpts, LangOpts, PB);
986       addKCFIPass(TargetTriple, LangOpts, PB);
987     }
988 
989     if (std::optional<GCOVOptions> Options =
990             getGCOVOptions(CodeGenOpts, LangOpts))
991       PB.registerPipelineStartEPCallback(
992           [Options](ModulePassManager &MPM, OptimizationLevel Level) {
993             MPM.addPass(GCOVProfilerPass(*Options));
994           });
995     if (std::optional<InstrProfOptions> Options =
996             getInstrProfOptions(CodeGenOpts, LangOpts))
997       PB.registerPipelineStartEPCallback(
998           [Options](ModulePassManager &MPM, OptimizationLevel Level) {
999             MPM.addPass(InstrProfilingLoweringPass(*Options, false));
1000           });
1001 
1002     // TODO: Consider passing the MemoryProfileOutput to the pass builder via
1003     // the PGOOptions, and set this up there.
1004     if (!CodeGenOpts.MemoryProfileOutput.empty()) {
1005       PB.registerOptimizerLastEPCallback(
1006           [](ModulePassManager &MPM, OptimizationLevel Level) {
1007             MPM.addPass(createModuleToFunctionPassAdaptor(MemProfilerPass()));
1008             MPM.addPass(ModuleMemProfilerPass());
1009           });
1010     }
1011 
1012     if (CodeGenOpts.FatLTO) {
1013       MPM.addPass(PB.buildFatLTODefaultPipeline(
1014           Level, PrepareForThinLTO,
1015           PrepareForThinLTO || shouldEmitRegularLTOSummary()));
1016     } else if (PrepareForThinLTO) {
1017       MPM.addPass(PB.buildThinLTOPreLinkDefaultPipeline(Level));
1018     } else if (PrepareForLTO) {
1019       MPM.addPass(PB.buildLTOPreLinkDefaultPipeline(Level));
1020     } else {
1021       MPM.addPass(PB.buildPerModuleDefaultPipeline(Level));
1022     }
1023   }
1024 
1025   // Re-link against any bitcodes supplied via the -mlink-builtin-bitcode option
1026   // Some optimizations may generate new function calls that would not have
1027   // been linked pre-optimization (i.e. fused sincos calls generated by
1028   // AMDGPULibCalls::fold_sincos.)
1029   if (ClRelinkBuiltinBitcodePostop)
1030     MPM.addPass(LinkInModulesPass(BC, false));
1031 
1032   // Add a verifier pass if requested. We don't have to do this if the action
1033   // requires code generation because there will already be a verifier pass in
1034   // the code-generation pipeline.
1035   // Since we already added a verifier pass above, this
1036   // might even not run the analysis, if previous passes caused no changes.
1037   if (!actionRequiresCodeGen(Action) && CodeGenOpts.VerifyModule)
1038     MPM.addPass(VerifierPass());
1039 
1040   if (Action == Backend_EmitBC || Action == Backend_EmitLL ||
1041       CodeGenOpts.FatLTO) {
1042     if (CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.DisableLLVMPasses) {
1043       if (!TheModule->getModuleFlag("EnableSplitLTOUnit"))
1044         TheModule->addModuleFlag(llvm::Module::Error, "EnableSplitLTOUnit",
1045                                  CodeGenOpts.EnableSplitLTOUnit);
1046       if (Action == Backend_EmitBC) {
1047         if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
1048           ThinLinkOS = openOutputFile(CodeGenOpts.ThinLinkBitcodeFile);
1049           if (!ThinLinkOS)
1050             return;
1051         }
1052         MPM.addPass(ThinLTOBitcodeWriterPass(
1053             *OS, ThinLinkOS ? &ThinLinkOS->os() : nullptr));
1054       } else if (Action == Backend_EmitLL) {
1055         MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists,
1056                                     /*EmitLTOSummary=*/true));
1057       }
1058     } else {
1059       // Emit a module summary by default for Regular LTO except for ld64
1060       // targets
1061       bool EmitLTOSummary = shouldEmitRegularLTOSummary();
1062       if (EmitLTOSummary) {
1063         if (!TheModule->getModuleFlag("ThinLTO") && !CodeGenOpts.UnifiedLTO)
1064           TheModule->addModuleFlag(llvm::Module::Error, "ThinLTO", uint32_t(0));
1065         if (!TheModule->getModuleFlag("EnableSplitLTOUnit"))
1066           TheModule->addModuleFlag(llvm::Module::Error, "EnableSplitLTOUnit",
1067                                    uint32_t(1));
1068       }
1069       if (Action == Backend_EmitBC) {
1070         MPM.addPass(BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists,
1071                                       EmitLTOSummary));
1072       } else if (Action == Backend_EmitLL) {
1073         MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists,
1074                                     EmitLTOSummary));
1075       }
1076     }
1077 
1078     if (shouldEmitUnifiedLTOModueFlag())
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