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

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