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