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

source code of clang/lib/CodeGen/BackendUtil.cpp