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