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