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