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