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