1 //===--- BackendUtil.cpp - LLVM Backend Utilities -------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "clang/CodeGen/BackendUtil.h"
10 #include "BackendConsumer.h"
11 #include "LinkInModulesPass.h"
12 #include "clang/Basic/CodeGenOptions.h"
13 #include "clang/Basic/Diagnostic.h"
14 #include "clang/Basic/LangOptions.h"
15 #include "clang/Basic/TargetOptions.h"
16 #include "clang/Frontend/FrontendDiagnostic.h"
17 #include "clang/Frontend/Utils.h"
18 #include "clang/Lex/HeaderSearchOptions.h"
19 #include "llvm/ADT/SmallSet.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/ADT/StringSwitch.h"
22 #include "llvm/Analysis/AliasAnalysis.h"
23 #include "llvm/Analysis/GlobalsModRef.h"
24 #include "llvm/Analysis/TargetLibraryInfo.h"
25 #include "llvm/Analysis/TargetTransformInfo.h"
26 #include "llvm/Bitcode/BitcodeReader.h"
27 #include "llvm/Bitcode/BitcodeWriter.h"
28 #include "llvm/Bitcode/BitcodeWriterPass.h"
29 #include "llvm/CodeGen/RegAllocRegistry.h"
30 #include "llvm/CodeGen/SchedulerRegistry.h"
31 #include "llvm/CodeGen/TargetSubtargetInfo.h"
32 #include "llvm/Frontend/Driver/CodeGenOptions.h"
33 #include "llvm/IR/DataLayout.h"
34 #include "llvm/IR/DebugInfo.h"
35 #include "llvm/IR/LegacyPassManager.h"
36 #include "llvm/IR/Module.h"
37 #include "llvm/IR/ModuleSummaryIndex.h"
38 #include "llvm/IR/PassManager.h"
39 #include "llvm/IR/Verifier.h"
40 #include "llvm/IRPrinter/IRPrintingPasses.h"
41 #include "llvm/LTO/LTOBackend.h"
42 #include "llvm/MC/MCAsmInfo.h"
43 #include "llvm/MC/TargetRegistry.h"
44 #include "llvm/Object/OffloadBinary.h"
45 #include "llvm/Passes/PassBuilder.h"
46 #include "llvm/Passes/PassPlugin.h"
47 #include "llvm/Passes/StandardInstrumentations.h"
48 #include "llvm/ProfileData/InstrProfCorrelator.h"
49 #include "llvm/Support/BuryPointer.h"
50 #include "llvm/Support/CommandLine.h"
51 #include "llvm/Support/MemoryBuffer.h"
52 #include "llvm/Support/PrettyStackTrace.h"
53 #include "llvm/Support/TimeProfiler.h"
54 #include "llvm/Support/Timer.h"
55 #include "llvm/Support/ToolOutputFile.h"
56 #include "llvm/Support/VirtualFileSystem.h"
57 #include "llvm/Support/raw_ostream.h"
58 #include "llvm/Target/TargetMachine.h"
59 #include "llvm/Target/TargetOptions.h"
60 #include "llvm/TargetParser/SubtargetFeature.h"
61 #include "llvm/TargetParser/Triple.h"
62 #include "llvm/Transforms/HipStdPar/HipStdPar.h"
63 #include "llvm/Transforms/IPO/EmbedBitcodePass.h"
64 #include "llvm/Transforms/IPO/LowerTypeTests.h"
65 #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
66 #include "llvm/Transforms/InstCombine/InstCombine.h"
67 #include "llvm/Transforms/Instrumentation.h"
68 #include "llvm/Transforms/Instrumentation/AddressSanitizer.h"
69 #include "llvm/Transforms/Instrumentation/AddressSanitizerOptions.h"
70 #include "llvm/Transforms/Instrumentation/BoundsChecking.h"
71 #include "llvm/Transforms/Instrumentation/DataFlowSanitizer.h"
72 #include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
73 #include "llvm/Transforms/Instrumentation/HWAddressSanitizer.h"
74 #include "llvm/Transforms/Instrumentation/InstrProfiling.h"
75 #include "llvm/Transforms/Instrumentation/KCFI.h"
76 #include "llvm/Transforms/Instrumentation/LowerAllowCheckPass.h"
77 #include "llvm/Transforms/Instrumentation/MemProfiler.h"
78 #include "llvm/Transforms/Instrumentation/MemorySanitizer.h"
79 #include "llvm/Transforms/Instrumentation/NumericalStabilitySanitizer.h"
80 #include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
81 #include "llvm/Transforms/Instrumentation/RealtimeSanitizer.h"
82 #include "llvm/Transforms/Instrumentation/SanitizerBinaryMetadata.h"
83 #include "llvm/Transforms/Instrumentation/SanitizerCoverage.h"
84 #include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
85 #include "llvm/Transforms/ObjCARC.h"
86 #include "llvm/Transforms/Scalar/EarlyCSE.h"
87 #include "llvm/Transforms/Scalar/GVN.h"
88 #include "llvm/Transforms/Scalar/JumpThreading.h"
89 #include "llvm/Transforms/Utils/Debugify.h"
90 #include "llvm/Transforms/Utils/ModuleUtils.h"
93 using namespace clang
;
96 #define HANDLE_EXTENSION(Ext) \
97 llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
98 #include "llvm/Support/Extension.def"
101 extern cl::opt
<bool> PrintPipelinePasses
;
103 // Experiment to move sanitizers earlier.
104 static cl::opt
<bool> ClSanitizeOnOptimizerEarlyEP(
105 "sanitizer-early-opt-ep", cl::Optional
,
106 cl::desc("Insert sanitizers on OptimizerEarlyEP."));
108 // Experiment to mark cold functions as optsize/minsize/optnone.
109 // TODO: remove once this is exposed as a proper driver flag.
110 static cl::opt
<PGOOptions::ColdFuncOpt
> ClPGOColdFuncAttr(
111 "pgo-cold-func-opt", cl::init(PGOOptions::ColdFuncOpt::Default
), cl::Hidden
,
113 "Function attribute to apply to cold functions as determined by PGO"),
114 cl::values(clEnumValN(PGOOptions::ColdFuncOpt::Default
, "default",
115 "Default (no attribute)"),
116 clEnumValN(PGOOptions::ColdFuncOpt::OptSize
, "optsize",
117 "Mark cold functions with optsize."),
118 clEnumValN(PGOOptions::ColdFuncOpt::MinSize
, "minsize",
119 "Mark cold functions with minsize."),
120 clEnumValN(PGOOptions::ColdFuncOpt::OptNone
, "optnone",
121 "Mark cold functions with optnone.")));
123 extern cl::opt
<InstrProfCorrelator::ProfCorrelatorKind
> ProfileCorrelate
;
128 // Default filename used for profile generation.
129 std::string
getDefaultProfileGenName() {
130 return DebugInfoCorrelate
|| ProfileCorrelate
!= InstrProfCorrelator::NONE
131 ? "default_%m.proflite"
132 : "default_%m.profraw";
135 class EmitAssemblyHelper
{
136 DiagnosticsEngine
&Diags
;
137 const HeaderSearchOptions
&HSOpts
;
138 const CodeGenOptions
&CodeGenOpts
;
139 const clang::TargetOptions
&TargetOpts
;
140 const LangOptions
&LangOpts
;
141 llvm::Module
*TheModule
;
142 IntrusiveRefCntPtr
<llvm::vfs::FileSystem
> VFS
;
144 Timer CodeGenerationTime
;
146 std::unique_ptr
<raw_pwrite_stream
> OS
;
150 TargetIRAnalysis
getTargetIRAnalysis() const {
152 return TM
->getTargetIRAnalysis();
154 return TargetIRAnalysis();
157 /// Generates the TargetMachine.
158 /// Leaves TM unchanged if it is unable to create the target machine.
159 /// Some of our clang tests specify triples which are not built
160 /// into clang. This is okay because these tests check the generated
161 /// IR, and they require DataLayout which depends on the triple.
162 /// In this case, we allow this method to fail and not report an error.
163 /// When MustCreateTM is used, we print an error if we are unable to load
164 /// the requested target.
165 void CreateTargetMachine(bool MustCreateTM
);
167 /// Add passes necessary to emit assembly or LLVM IR.
169 /// \return True on success.
170 bool AddEmitPasses(legacy::PassManager
&CodeGenPasses
, BackendAction Action
,
171 raw_pwrite_stream
&OS
, raw_pwrite_stream
*DwoOS
);
173 std::unique_ptr
<llvm::ToolOutputFile
> openOutputFile(StringRef Path
) {
175 auto F
= std::make_unique
<llvm::ToolOutputFile
>(Path
, EC
,
176 llvm::sys::fs::OF_None
);
178 Diags
.Report(diag::err_fe_unable_to_open_output
) << Path
<< EC
.message();
184 void RunOptimizationPipeline(
185 BackendAction Action
, std::unique_ptr
<raw_pwrite_stream
> &OS
,
186 std::unique_ptr
<llvm::ToolOutputFile
> &ThinLinkOS
, BackendConsumer
*BC
);
187 void RunCodegenPipeline(BackendAction Action
,
188 std::unique_ptr
<raw_pwrite_stream
> &OS
,
189 std::unique_ptr
<llvm::ToolOutputFile
> &DwoOS
);
191 /// Check whether we should emit a module summary for regular LTO.
192 /// The module summary should be emitted by default for regular LTO
193 /// except for ld64 targets.
195 /// \return True if the module summary should be emitted.
196 bool shouldEmitRegularLTOSummary() const {
197 return CodeGenOpts
.PrepareForLTO
&& !CodeGenOpts
.DisableLLVMPasses
&&
198 TargetTriple
.getVendor() != llvm::Triple::Apple
;
201 /// Check whether we should emit a flag for UnifiedLTO.
202 /// The UnifiedLTO module flag should be set when UnifiedLTO is enabled for
203 /// ThinLTO or Full LTO with module summaries.
204 bool shouldEmitUnifiedLTOModueFlag() const {
205 return CodeGenOpts
.UnifiedLTO
&&
206 (CodeGenOpts
.PrepareForThinLTO
|| shouldEmitRegularLTOSummary());
210 EmitAssemblyHelper(DiagnosticsEngine
&_Diags
,
211 const HeaderSearchOptions
&HeaderSearchOpts
,
212 const CodeGenOptions
&CGOpts
,
213 const clang::TargetOptions
&TOpts
,
214 const LangOptions
&LOpts
, llvm::Module
*M
,
215 IntrusiveRefCntPtr
<llvm::vfs::FileSystem
> VFS
)
216 : Diags(_Diags
), HSOpts(HeaderSearchOpts
), CodeGenOpts(CGOpts
),
217 TargetOpts(TOpts
), LangOpts(LOpts
), TheModule(M
), VFS(std::move(VFS
)),
218 CodeGenerationTime("codegen", "Code Generation Time"),
219 TargetTriple(TheModule
->getTargetTriple()) {}
221 ~EmitAssemblyHelper() {
222 if (CodeGenOpts
.DisableFree
)
223 BuryPointer(std::move(TM
));
226 std::unique_ptr
<TargetMachine
> TM
;
228 // Emit output using the new pass manager for the optimization pipeline.
229 void EmitAssembly(BackendAction Action
, std::unique_ptr
<raw_pwrite_stream
> OS
,
230 BackendConsumer
*BC
);
234 static SanitizerCoverageOptions
235 getSancovOptsFromCGOpts(const CodeGenOptions
&CGOpts
) {
236 SanitizerCoverageOptions Opts
;
238 static_cast<SanitizerCoverageOptions::Type
>(CGOpts
.SanitizeCoverageType
);
239 Opts
.IndirectCalls
= CGOpts
.SanitizeCoverageIndirectCalls
;
240 Opts
.TraceBB
= CGOpts
.SanitizeCoverageTraceBB
;
241 Opts
.TraceCmp
= CGOpts
.SanitizeCoverageTraceCmp
;
242 Opts
.TraceDiv
= CGOpts
.SanitizeCoverageTraceDiv
;
243 Opts
.TraceGep
= CGOpts
.SanitizeCoverageTraceGep
;
244 Opts
.Use8bitCounters
= CGOpts
.SanitizeCoverage8bitCounters
;
245 Opts
.TracePC
= CGOpts
.SanitizeCoverageTracePC
;
246 Opts
.TracePCGuard
= CGOpts
.SanitizeCoverageTracePCGuard
;
247 Opts
.NoPrune
= CGOpts
.SanitizeCoverageNoPrune
;
248 Opts
.Inline8bitCounters
= CGOpts
.SanitizeCoverageInline8bitCounters
;
249 Opts
.InlineBoolFlag
= CGOpts
.SanitizeCoverageInlineBoolFlag
;
250 Opts
.PCTable
= CGOpts
.SanitizeCoveragePCTable
;
251 Opts
.StackDepth
= CGOpts
.SanitizeCoverageStackDepth
;
252 Opts
.TraceLoads
= CGOpts
.SanitizeCoverageTraceLoads
;
253 Opts
.TraceStores
= CGOpts
.SanitizeCoverageTraceStores
;
254 Opts
.CollectControlFlow
= CGOpts
.SanitizeCoverageControlFlow
;
258 static SanitizerBinaryMetadataOptions
259 getSanitizerBinaryMetadataOptions(const CodeGenOptions
&CGOpts
) {
260 SanitizerBinaryMetadataOptions Opts
;
261 Opts
.Covered
= CGOpts
.SanitizeBinaryMetadataCovered
;
262 Opts
.Atomics
= CGOpts
.SanitizeBinaryMetadataAtomics
;
263 Opts
.UAR
= CGOpts
.SanitizeBinaryMetadataUAR
;
267 // Check if ASan should use GC-friendly instrumentation for globals.
268 // First of all, there is no point if -fdata-sections is off (expect for MachO,
269 // where this is not a factor). Also, on ELF this feature requires an assembler
270 // extension that only works with -integrated-as at the moment.
271 static bool asanUseGlobalsGC(const Triple
&T
, const CodeGenOptions
&CGOpts
) {
272 if (!CGOpts
.SanitizeAddressGlobalsDeadStripping
)
274 switch (T
.getObjectFormat()) {
279 return !CGOpts
.DisableIntegratedAS
;
281 llvm::report_fatal_error("ASan not implemented for GOFF");
283 llvm::report_fatal_error("ASan not implemented for XCOFF.");
285 case Triple::DXContainer
:
287 case Triple::UnknownObjectFormat
:
293 static std::optional
<llvm::CodeModel::Model
>
294 getCodeModel(const CodeGenOptions
&CodeGenOpts
) {
295 unsigned CodeModel
= llvm::StringSwitch
<unsigned>(CodeGenOpts
.CodeModel
)
296 .Case("tiny", llvm::CodeModel::Tiny
)
297 .Case("small", llvm::CodeModel::Small
)
298 .Case("kernel", llvm::CodeModel::Kernel
)
299 .Case("medium", llvm::CodeModel::Medium
)
300 .Case("large", llvm::CodeModel::Large
)
301 .Case("default", ~1u)
303 assert(CodeModel
!= ~0u && "invalid code model!");
304 if (CodeModel
== ~1u)
306 return static_cast<llvm::CodeModel::Model
>(CodeModel
);
309 static CodeGenFileType
getCodeGenFileType(BackendAction Action
) {
310 if (Action
== Backend_EmitObj
)
311 return CodeGenFileType::ObjectFile
;
312 else if (Action
== Backend_EmitMCNull
)
313 return CodeGenFileType::Null
;
315 assert(Action
== Backend_EmitAssembly
&& "Invalid action!");
316 return CodeGenFileType::AssemblyFile
;
320 static bool actionRequiresCodeGen(BackendAction Action
) {
321 return Action
!= Backend_EmitNothing
&& Action
!= Backend_EmitBC
&&
322 Action
!= Backend_EmitLL
;
325 static bool initTargetOptions(DiagnosticsEngine
&Diags
,
326 llvm::TargetOptions
&Options
,
327 const CodeGenOptions
&CodeGenOpts
,
328 const clang::TargetOptions
&TargetOpts
,
329 const LangOptions
&LangOpts
,
330 const HeaderSearchOptions
&HSOpts
) {
331 switch (LangOpts
.getThreadModel()) {
332 case LangOptions::ThreadModelKind::POSIX
:
333 Options
.ThreadModel
= llvm::ThreadModel::POSIX
;
335 case LangOptions::ThreadModelKind::Single
:
336 Options
.ThreadModel
= llvm::ThreadModel::Single
;
340 // Set float ABI type.
341 assert((CodeGenOpts
.FloatABI
== "soft" || CodeGenOpts
.FloatABI
== "softfp" ||
342 CodeGenOpts
.FloatABI
== "hard" || CodeGenOpts
.FloatABI
.empty()) &&
343 "Invalid Floating Point ABI!");
344 Options
.FloatABIType
=
345 llvm::StringSwitch
<llvm::FloatABI::ABIType
>(CodeGenOpts
.FloatABI
)
346 .Case("soft", llvm::FloatABI::Soft
)
347 .Case("softfp", llvm::FloatABI::Soft
)
348 .Case("hard", llvm::FloatABI::Hard
)
349 .Default(llvm::FloatABI::Default
);
351 // Set FP fusion mode.
352 switch (LangOpts
.getDefaultFPContractMode()) {
353 case LangOptions::FPM_Off
:
354 // Preserve any contraction performed by the front-end. (Strict performs
355 // splitting of the muladd intrinsic in the backend.)
356 Options
.AllowFPOpFusion
= llvm::FPOpFusion::Standard
;
358 case LangOptions::FPM_On
:
359 case LangOptions::FPM_FastHonorPragmas
:
360 Options
.AllowFPOpFusion
= llvm::FPOpFusion::Standard
;
362 case LangOptions::FPM_Fast
:
363 Options
.AllowFPOpFusion
= llvm::FPOpFusion::Fast
;
367 Options
.BinutilsVersion
=
368 llvm::TargetMachine::parseBinutilsVersion(CodeGenOpts
.BinutilsVersion
);
369 Options
.UseInitArray
= CodeGenOpts
.UseInitArray
;
370 Options
.DisableIntegratedAS
= CodeGenOpts
.DisableIntegratedAS
;
373 Options
.EABIVersion
= TargetOpts
.EABIVersion
;
375 if (LangOpts
.hasSjLjExceptions())
376 Options
.ExceptionModel
= llvm::ExceptionHandling::SjLj
;
377 if (LangOpts
.hasSEHExceptions())
378 Options
.ExceptionModel
= llvm::ExceptionHandling::WinEH
;
379 if (LangOpts
.hasDWARFExceptions())
380 Options
.ExceptionModel
= llvm::ExceptionHandling::DwarfCFI
;
381 if (LangOpts
.hasWasmExceptions())
382 Options
.ExceptionModel
= llvm::ExceptionHandling::Wasm
;
384 Options
.NoInfsFPMath
= LangOpts
.NoHonorInfs
;
385 Options
.NoNaNsFPMath
= LangOpts
.NoHonorNaNs
;
386 Options
.NoZerosInBSS
= CodeGenOpts
.NoZeroInitializedInBSS
;
387 Options
.UnsafeFPMath
= LangOpts
.AllowFPReassoc
&& LangOpts
.AllowRecip
&&
388 LangOpts
.NoSignedZero
&& LangOpts
.ApproxFunc
&&
389 (LangOpts
.getDefaultFPContractMode() ==
390 LangOptions::FPModeKind::FPM_Fast
||
391 LangOpts
.getDefaultFPContractMode() ==
392 LangOptions::FPModeKind::FPM_FastHonorPragmas
);
393 Options
.ApproxFuncFPMath
= LangOpts
.ApproxFunc
;
395 Options
.BBAddrMap
= CodeGenOpts
.BBAddrMap
;
397 llvm::StringSwitch
<llvm::BasicBlockSection
>(CodeGenOpts
.BBSections
)
398 .Case("all", llvm::BasicBlockSection::All
)
399 .Case("labels", llvm::BasicBlockSection::Labels
)
400 .StartsWith("list=", llvm::BasicBlockSection::List
)
401 .Case("none", llvm::BasicBlockSection::None
)
402 .Default(llvm::BasicBlockSection::None
);
404 if (Options
.BBSections
== llvm::BasicBlockSection::List
) {
405 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> MBOrErr
=
406 MemoryBuffer::getFile(CodeGenOpts
.BBSections
.substr(5));
408 Diags
.Report(diag::err_fe_unable_to_load_basic_block_sections_file
)
409 << MBOrErr
.getError().message();
412 Options
.BBSectionsFuncListBuf
= std::move(*MBOrErr
);
415 Options
.EnableMachineFunctionSplitter
= CodeGenOpts
.SplitMachineFunctions
;
416 Options
.FunctionSections
= CodeGenOpts
.FunctionSections
;
417 Options
.DataSections
= CodeGenOpts
.DataSections
;
418 Options
.IgnoreXCOFFVisibility
= LangOpts
.IgnoreXCOFFVisibility
;
419 Options
.UniqueSectionNames
= CodeGenOpts
.UniqueSectionNames
;
420 Options
.UniqueBasicBlockSectionNames
=
421 CodeGenOpts
.UniqueBasicBlockSectionNames
;
422 Options
.SeparateNamedSections
= CodeGenOpts
.SeparateNamedSections
;
423 Options
.TLSSize
= CodeGenOpts
.TLSSize
;
424 Options
.EnableTLSDESC
= CodeGenOpts
.EnableTLSDESC
;
425 Options
.EmulatedTLS
= CodeGenOpts
.EmulatedTLS
;
426 Options
.DebuggerTuning
= CodeGenOpts
.getDebuggerTuning();
427 Options
.EmitStackSizeSection
= CodeGenOpts
.StackSizeSection
;
428 Options
.StackUsageOutput
= CodeGenOpts
.StackUsageOutput
;
429 Options
.EmitAddrsig
= CodeGenOpts
.Addrsig
;
430 Options
.ForceDwarfFrameSection
= CodeGenOpts
.ForceDwarfFrameSection
;
431 Options
.EmitCallSiteInfo
= CodeGenOpts
.EmitCallSiteInfo
;
432 Options
.EnableAIXExtendedAltivecABI
= LangOpts
.EnableAIXExtendedAltivecABI
;
433 Options
.XRayFunctionIndex
= CodeGenOpts
.XRayFunctionIndex
;
434 Options
.LoopAlignment
= CodeGenOpts
.LoopAlignment
;
435 Options
.DebugStrictDwarf
= CodeGenOpts
.DebugStrictDwarf
;
436 Options
.ObjectFilenameForDebug
= CodeGenOpts
.ObjectFilenameForDebug
;
437 Options
.Hotpatch
= CodeGenOpts
.HotPatch
;
438 Options
.JMCInstrument
= CodeGenOpts
.JMCInstrument
;
439 Options
.XCOFFReadOnlyPointers
= CodeGenOpts
.XCOFFReadOnlyPointers
;
441 switch (CodeGenOpts
.getSwiftAsyncFramePointer()) {
442 case CodeGenOptions::SwiftAsyncFramePointerKind::Auto
:
443 Options
.SwiftAsyncFramePointer
=
444 SwiftAsyncFramePointerMode::DeploymentBased
;
447 case CodeGenOptions::SwiftAsyncFramePointerKind::Always
:
448 Options
.SwiftAsyncFramePointer
= SwiftAsyncFramePointerMode::Always
;
451 case CodeGenOptions::SwiftAsyncFramePointerKind::Never
:
452 Options
.SwiftAsyncFramePointer
= SwiftAsyncFramePointerMode::Never
;
456 Options
.MCOptions
.SplitDwarfFile
= CodeGenOpts
.SplitDwarfFile
;
457 Options
.MCOptions
.EmitDwarfUnwind
= CodeGenOpts
.getEmitDwarfUnwind();
458 Options
.MCOptions
.EmitCompactUnwindNonCanonical
=
459 CodeGenOpts
.EmitCompactUnwindNonCanonical
;
460 Options
.MCOptions
.MCRelaxAll
= CodeGenOpts
.RelaxAll
;
461 Options
.MCOptions
.MCSaveTempLabels
= CodeGenOpts
.SaveTempLabels
;
462 Options
.MCOptions
.MCUseDwarfDirectory
=
463 CodeGenOpts
.NoDwarfDirectoryAsm
464 ? llvm::MCTargetOptions::DisableDwarfDirectory
465 : llvm::MCTargetOptions::EnableDwarfDirectory
;
466 Options
.MCOptions
.MCNoExecStack
= CodeGenOpts
.NoExecStack
;
467 Options
.MCOptions
.MCIncrementalLinkerCompatible
=
468 CodeGenOpts
.IncrementalLinkerCompatible
;
469 Options
.MCOptions
.MCFatalWarnings
= CodeGenOpts
.FatalWarnings
;
470 Options
.MCOptions
.MCNoWarn
= CodeGenOpts
.NoWarn
;
471 Options
.MCOptions
.AsmVerbose
= CodeGenOpts
.AsmVerbose
;
472 Options
.MCOptions
.Dwarf64
= CodeGenOpts
.Dwarf64
;
473 Options
.MCOptions
.PreserveAsmComments
= CodeGenOpts
.PreserveAsmComments
;
474 Options
.MCOptions
.Crel
= CodeGenOpts
.Crel
;
475 Options
.MCOptions
.ImplicitMapSyms
= CodeGenOpts
.ImplicitMapSyms
;
476 Options
.MCOptions
.X86RelaxRelocations
= CodeGenOpts
.X86RelaxRelocations
;
477 Options
.MCOptions
.CompressDebugSections
=
478 CodeGenOpts
.getCompressDebugSections();
479 Options
.MCOptions
.ABIName
= TargetOpts
.ABI
;
480 for (const auto &Entry
: HSOpts
.UserEntries
)
481 if (!Entry
.IsFramework
&&
482 (Entry
.Group
== frontend::IncludeDirGroup::Quoted
||
483 Entry
.Group
== frontend::IncludeDirGroup::Angled
||
484 Entry
.Group
== frontend::IncludeDirGroup::System
))
485 Options
.MCOptions
.IASSearchPaths
.push_back(
486 Entry
.IgnoreSysRoot
? Entry
.Path
: HSOpts
.Sysroot
+ Entry
.Path
);
487 Options
.MCOptions
.Argv0
= CodeGenOpts
.Argv0
;
488 Options
.MCOptions
.CommandLineArgs
= CodeGenOpts
.CommandLineArgs
;
489 Options
.MCOptions
.AsSecureLogFile
= CodeGenOpts
.AsSecureLogFile
;
490 Options
.MCOptions
.PPCUseFullRegisterNames
=
491 CodeGenOpts
.PPCUseFullRegisterNames
;
492 Options
.MisExpect
= CodeGenOpts
.MisExpect
;
497 static std::optional
<GCOVOptions
>
498 getGCOVOptions(const CodeGenOptions
&CodeGenOpts
, const LangOptions
&LangOpts
) {
499 if (CodeGenOpts
.CoverageNotesFile
.empty() &&
500 CodeGenOpts
.CoverageDataFile
.empty())
502 // Not using 'GCOVOptions::getDefault' allows us to avoid exiting if
503 // LLVM's -default-gcov-version flag is set to something invalid.
505 Options
.EmitNotes
= !CodeGenOpts
.CoverageNotesFile
.empty();
506 Options
.EmitData
= !CodeGenOpts
.CoverageDataFile
.empty();
507 llvm::copy(CodeGenOpts
.CoverageVersion
, std::begin(Options
.Version
));
508 Options
.NoRedZone
= CodeGenOpts
.DisableRedZone
;
509 Options
.Filter
= CodeGenOpts
.ProfileFilterFiles
;
510 Options
.Exclude
= CodeGenOpts
.ProfileExcludeFiles
;
511 Options
.Atomic
= CodeGenOpts
.AtomicProfileUpdate
;
515 static std::optional
<InstrProfOptions
>
516 getInstrProfOptions(const CodeGenOptions
&CodeGenOpts
,
517 const LangOptions
&LangOpts
) {
518 if (!CodeGenOpts
.hasProfileClangInstr())
520 InstrProfOptions Options
;
521 Options
.NoRedZone
= CodeGenOpts
.DisableRedZone
;
522 Options
.InstrProfileOutput
= CodeGenOpts
.InstrProfileOutput
;
523 Options
.Atomic
= CodeGenOpts
.AtomicProfileUpdate
;
527 static void setCommandLineOpts(const CodeGenOptions
&CodeGenOpts
) {
528 SmallVector
<const char *, 16> BackendArgs
;
529 BackendArgs
.push_back("clang"); // Fake program name.
530 if (!CodeGenOpts
.DebugPass
.empty()) {
531 BackendArgs
.push_back("-debug-pass");
532 BackendArgs
.push_back(CodeGenOpts
.DebugPass
.c_str());
534 if (!CodeGenOpts
.LimitFloatPrecision
.empty()) {
535 BackendArgs
.push_back("-limit-float-precision");
536 BackendArgs
.push_back(CodeGenOpts
.LimitFloatPrecision
.c_str());
538 // Check for the default "clang" invocation that won't set any cl::opt values.
539 // Skip trying to parse the command line invocation to avoid the issues
541 if (BackendArgs
.size() == 1)
543 BackendArgs
.push_back(nullptr);
544 // FIXME: The command line parser below is not thread-safe and shares a global
545 // state, so this call might crash or overwrite the options of another Clang
546 // instance in the same process.
547 llvm::cl::ParseCommandLineOptions(BackendArgs
.size() - 1,
551 void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM
) {
552 // Create the TargetMachine for generating code.
554 std::string Triple
= TheModule
->getTargetTriple();
555 const llvm::Target
*TheTarget
= TargetRegistry::lookupTarget(Triple
, Error
);
558 Diags
.Report(diag::err_fe_unable_to_create_target
) << Error
;
562 std::optional
<llvm::CodeModel::Model
> CM
= getCodeModel(CodeGenOpts
);
563 std::string FeaturesStr
=
564 llvm::join(TargetOpts
.Features
.begin(), TargetOpts
.Features
.end(), ",");
565 llvm::Reloc::Model RM
= CodeGenOpts
.RelocationModel
;
566 std::optional
<CodeGenOptLevel
> OptLevelOrNone
=
567 CodeGenOpt::getLevel(CodeGenOpts
.OptimizationLevel
);
568 assert(OptLevelOrNone
&& "Invalid optimization level!");
569 CodeGenOptLevel OptLevel
= *OptLevelOrNone
;
571 llvm::TargetOptions Options
;
572 if (!initTargetOptions(Diags
, Options
, CodeGenOpts
, TargetOpts
, LangOpts
,
575 TM
.reset(TheTarget
->createTargetMachine(Triple
, TargetOpts
.CPU
, FeaturesStr
,
576 Options
, RM
, CM
, OptLevel
));
577 TM
->setLargeDataThreshold(CodeGenOpts
.LargeDataThreshold
);
580 bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager
&CodeGenPasses
,
581 BackendAction Action
,
582 raw_pwrite_stream
&OS
,
583 raw_pwrite_stream
*DwoOS
) {
585 std::unique_ptr
<TargetLibraryInfoImpl
> TLII(
586 llvm::driver::createTLII(TargetTriple
, CodeGenOpts
.getVecLib()));
587 CodeGenPasses
.add(new TargetLibraryInfoWrapperPass(*TLII
));
589 // Normal mode, emit a .s or .o file by running the code generator. Note,
590 // this also adds codegenerator level optimization passes.
591 CodeGenFileType CGFT
= getCodeGenFileType(Action
);
593 if (TM
->addPassesToEmitFile(CodeGenPasses
, OS
, DwoOS
, CGFT
,
594 /*DisableVerify=*/!CodeGenOpts
.VerifyModule
)) {
595 Diags
.Report(diag::err_fe_unable_to_interface_with_target
);
602 static OptimizationLevel
mapToLevel(const CodeGenOptions
&Opts
) {
603 switch (Opts
.OptimizationLevel
) {
605 llvm_unreachable("Invalid optimization level!");
608 return OptimizationLevel::O0
;
611 return OptimizationLevel::O1
;
614 switch (Opts
.OptimizeSize
) {
616 llvm_unreachable("Invalid optimization level for size!");
619 return OptimizationLevel::O2
;
622 return OptimizationLevel::Os
;
625 return OptimizationLevel::Oz
;
629 return OptimizationLevel::O3
;
633 static void addKCFIPass(const Triple
&TargetTriple
, const LangOptions
&LangOpts
,
635 // If the back-end supports KCFI operand bundle lowering, skip KCFIPass.
636 if (TargetTriple
.getArch() == llvm::Triple::x86_64
||
637 TargetTriple
.isAArch64(64) || TargetTriple
.isRISCV())
640 // Ensure we lower KCFI operand bundles with -O0.
641 PB
.registerOptimizerLastEPCallback(
642 [&](ModulePassManager
&MPM
, OptimizationLevel Level
) {
643 if (Level
== OptimizationLevel::O0
&&
644 LangOpts
.Sanitize
.has(SanitizerKind::KCFI
))
645 MPM
.addPass(createModuleToFunctionPassAdaptor(KCFIPass()));
648 // When optimizations are requested, run KCIFPass after InstCombine to
649 // avoid unnecessary checks.
650 PB
.registerPeepholeEPCallback(
651 [&](FunctionPassManager
&FPM
, OptimizationLevel Level
) {
652 if (Level
!= OptimizationLevel::O0
&&
653 LangOpts
.Sanitize
.has(SanitizerKind::KCFI
))
654 FPM
.addPass(KCFIPass());
658 static void addSanitizers(const Triple
&TargetTriple
,
659 const CodeGenOptions
&CodeGenOpts
,
660 const LangOptions
&LangOpts
, PassBuilder
&PB
) {
661 auto SanitizersCallback
= [&](ModulePassManager
&MPM
,
662 OptimizationLevel Level
) {
663 if (CodeGenOpts
.hasSanitizeCoverage()) {
664 auto SancovOpts
= getSancovOptsFromCGOpts(CodeGenOpts
);
665 MPM
.addPass(SanitizerCoveragePass(
666 SancovOpts
, CodeGenOpts
.SanitizeCoverageAllowlistFiles
,
667 CodeGenOpts
.SanitizeCoverageIgnorelistFiles
));
670 if (CodeGenOpts
.hasSanitizeBinaryMetadata()) {
671 MPM
.addPass(SanitizerBinaryMetadataPass(
672 getSanitizerBinaryMetadataOptions(CodeGenOpts
),
673 CodeGenOpts
.SanitizeMetadataIgnorelistFiles
));
676 auto MSanPass
= [&](SanitizerMask Mask
, bool CompileKernel
) {
677 if (LangOpts
.Sanitize
.has(Mask
)) {
678 int TrackOrigins
= CodeGenOpts
.SanitizeMemoryTrackOrigins
;
679 bool Recover
= CodeGenOpts
.SanitizeRecover
.has(Mask
);
681 MemorySanitizerOptions
options(TrackOrigins
, Recover
, CompileKernel
,
682 CodeGenOpts
.SanitizeMemoryParamRetval
);
683 MPM
.addPass(MemorySanitizerPass(options
));
684 if (Level
!= OptimizationLevel::O0
) {
685 // MemorySanitizer inserts complex instrumentation that mostly follows
686 // the logic of the original code, but operates on "shadow" values. It
687 // can benefit from re-running some general purpose optimization
689 MPM
.addPass(RequireAnalysisPass
<GlobalsAA
, llvm::Module
>());
690 FunctionPassManager FPM
;
691 FPM
.addPass(EarlyCSEPass(true /* Enable mem-ssa. */));
692 FPM
.addPass(InstCombinePass());
693 FPM
.addPass(JumpThreadingPass());
694 FPM
.addPass(GVNPass());
695 FPM
.addPass(InstCombinePass());
696 MPM
.addPass(createModuleToFunctionPassAdaptor(std::move(FPM
)));
700 MSanPass(SanitizerKind::Memory
, false);
701 MSanPass(SanitizerKind::KernelMemory
, true);
703 if (LangOpts
.Sanitize
.has(SanitizerKind::Thread
)) {
704 MPM
.addPass(ModuleThreadSanitizerPass());
705 MPM
.addPass(createModuleToFunctionPassAdaptor(ThreadSanitizerPass()));
708 if (LangOpts
.Sanitize
.has(SanitizerKind::NumericalStability
))
709 MPM
.addPass(NumericalStabilitySanitizerPass());
711 auto ASanPass
= [&](SanitizerMask Mask
, bool CompileKernel
) {
712 if (LangOpts
.Sanitize
.has(Mask
)) {
713 bool UseGlobalGC
= asanUseGlobalsGC(TargetTriple
, CodeGenOpts
);
714 bool UseOdrIndicator
= CodeGenOpts
.SanitizeAddressUseOdrIndicator
;
715 llvm::AsanDtorKind DestructorKind
=
716 CodeGenOpts
.getSanitizeAddressDtor();
717 AddressSanitizerOptions Opts
;
718 Opts
.CompileKernel
= CompileKernel
;
719 Opts
.Recover
= CodeGenOpts
.SanitizeRecover
.has(Mask
);
720 Opts
.UseAfterScope
= CodeGenOpts
.SanitizeAddressUseAfterScope
;
721 Opts
.UseAfterReturn
= CodeGenOpts
.getSanitizeAddressUseAfterReturn();
722 MPM
.addPass(AddressSanitizerPass(Opts
, UseGlobalGC
, UseOdrIndicator
,
726 ASanPass(SanitizerKind::Address
, false);
727 ASanPass(SanitizerKind::KernelAddress
, true);
729 auto HWASanPass
= [&](SanitizerMask Mask
, bool CompileKernel
) {
730 if (LangOpts
.Sanitize
.has(Mask
)) {
731 bool Recover
= CodeGenOpts
.SanitizeRecover
.has(Mask
);
732 MPM
.addPass(HWAddressSanitizerPass(
733 {CompileKernel
, Recover
,
734 /*DisableOptimization=*/CodeGenOpts
.OptimizationLevel
== 0}));
737 HWASanPass(SanitizerKind::HWAddress
, false);
738 HWASanPass(SanitizerKind::KernelHWAddress
, true);
740 if (LangOpts
.Sanitize
.has(SanitizerKind::DataFlow
)) {
741 MPM
.addPass(DataFlowSanitizerPass(LangOpts
.NoSanitizeFiles
));
744 if (ClSanitizeOnOptimizerEarlyEP
) {
745 PB
.registerOptimizerEarlyEPCallback(
746 [SanitizersCallback
](ModulePassManager
&MPM
, OptimizationLevel Level
) {
747 ModulePassManager NewMPM
;
748 SanitizersCallback(NewMPM
, Level
);
749 if (!NewMPM
.isEmpty()) {
750 // Sanitizers can abandon<GlobalsAA>.
751 NewMPM
.addPass(RequireAnalysisPass
<GlobalsAA
, llvm::Module
>());
752 MPM
.addPass(std::move(NewMPM
));
756 // LastEP does not need GlobalsAA.
757 PB
.registerOptimizerLastEPCallback(SanitizersCallback
);
760 if (LowerAllowCheckPass::IsRequested()) {
761 // We can optimize after inliner, and PGO profile matching. The hook below
762 // is called at the end `buildFunctionSimplificationPipeline`, which called
763 // from `buildInlinerPipeline`, which called after profile matching.
764 PB
.registerScalarOptimizerLateEPCallback(
765 [](FunctionPassManager
&FPM
, OptimizationLevel Level
) {
766 FPM
.addPass(LowerAllowCheckPass());
771 void EmitAssemblyHelper::RunOptimizationPipeline(
772 BackendAction Action
, std::unique_ptr
<raw_pwrite_stream
> &OS
,
773 std::unique_ptr
<llvm::ToolOutputFile
> &ThinLinkOS
, BackendConsumer
*BC
) {
774 std::optional
<PGOOptions
> PGOOpt
;
776 if (CodeGenOpts
.hasProfileIRInstr())
777 // -fprofile-generate.
779 CodeGenOpts
.InstrProfileOutput
.empty() ? getDefaultProfileGenName()
780 : CodeGenOpts
.InstrProfileOutput
,
781 "", "", CodeGenOpts
.MemoryProfileUsePath
, nullptr, PGOOptions::IRInstr
,
782 PGOOptions::NoCSAction
, ClPGOColdFuncAttr
,
783 CodeGenOpts
.DebugInfoForProfiling
,
784 /*PseudoProbeForProfiling=*/false, CodeGenOpts
.AtomicProfileUpdate
);
785 else if (CodeGenOpts
.hasProfileIRUse()) {
787 auto CSAction
= CodeGenOpts
.hasProfileCSIRUse() ? PGOOptions::CSIRUse
788 : PGOOptions::NoCSAction
;
789 PGOOpt
= PGOOptions(CodeGenOpts
.ProfileInstrumentUsePath
, "",
790 CodeGenOpts
.ProfileRemappingFile
,
791 CodeGenOpts
.MemoryProfileUsePath
, VFS
,
792 PGOOptions::IRUse
, CSAction
, ClPGOColdFuncAttr
,
793 CodeGenOpts
.DebugInfoForProfiling
);
794 } else if (!CodeGenOpts
.SampleProfileFile
.empty())
795 // -fprofile-sample-use
797 CodeGenOpts
.SampleProfileFile
, "", CodeGenOpts
.ProfileRemappingFile
,
798 CodeGenOpts
.MemoryProfileUsePath
, VFS
, PGOOptions::SampleUse
,
799 PGOOptions::NoCSAction
, ClPGOColdFuncAttr
,
800 CodeGenOpts
.DebugInfoForProfiling
, CodeGenOpts
.PseudoProbeForProfiling
);
801 else if (!CodeGenOpts
.MemoryProfileUsePath
.empty())
802 // -fmemory-profile-use (without any of the above options)
803 PGOOpt
= PGOOptions("", "", "", CodeGenOpts
.MemoryProfileUsePath
, VFS
,
804 PGOOptions::NoAction
, PGOOptions::NoCSAction
,
805 ClPGOColdFuncAttr
, CodeGenOpts
.DebugInfoForProfiling
);
806 else if (CodeGenOpts
.PseudoProbeForProfiling
)
807 // -fpseudo-probe-for-profiling
809 PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr,
810 PGOOptions::NoAction
, PGOOptions::NoCSAction
,
811 ClPGOColdFuncAttr
, CodeGenOpts
.DebugInfoForProfiling
, true);
812 else if (CodeGenOpts
.DebugInfoForProfiling
)
813 // -fdebug-info-for-profiling
814 PGOOpt
= PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr,
815 PGOOptions::NoAction
, PGOOptions::NoCSAction
,
816 ClPGOColdFuncAttr
, true);
818 // Check to see if we want to generate a CS profile.
819 if (CodeGenOpts
.hasProfileCSIRInstr()) {
820 assert(!CodeGenOpts
.hasProfileCSIRUse() &&
821 "Cannot have both CSProfileUse pass and CSProfileGen pass at "
824 assert(PGOOpt
->Action
!= PGOOptions::IRInstr
&&
825 PGOOpt
->Action
!= PGOOptions::SampleUse
&&
826 "Cannot run CSProfileGen pass with ProfileGen or SampleUse "
828 PGOOpt
->CSProfileGenFile
= CodeGenOpts
.InstrProfileOutput
.empty()
829 ? getDefaultProfileGenName()
830 : CodeGenOpts
.InstrProfileOutput
;
831 PGOOpt
->CSAction
= PGOOptions::CSIRInstr
;
833 PGOOpt
= PGOOptions("",
834 CodeGenOpts
.InstrProfileOutput
.empty()
835 ? getDefaultProfileGenName()
836 : CodeGenOpts
.InstrProfileOutput
,
837 "", /*MemoryProfile=*/"", nullptr,
838 PGOOptions::NoAction
, PGOOptions::CSIRInstr
,
839 ClPGOColdFuncAttr
, CodeGenOpts
.DebugInfoForProfiling
);
842 TM
->setPGOOption(PGOOpt
);
844 PipelineTuningOptions PTO
;
845 PTO
.LoopUnrolling
= CodeGenOpts
.UnrollLoops
;
846 // For historical reasons, loop interleaving is set to mirror setting for loop
848 PTO
.LoopInterleaving
= CodeGenOpts
.UnrollLoops
;
849 PTO
.LoopVectorization
= CodeGenOpts
.VectorizeLoop
;
850 PTO
.SLPVectorization
= CodeGenOpts
.VectorizeSLP
;
851 PTO
.MergeFunctions
= CodeGenOpts
.MergeFunctions
;
852 // Only enable CGProfilePass when using integrated assembler, since
853 // non-integrated assemblers don't recognize .cgprofile section.
854 PTO
.CallGraphProfile
= !CodeGenOpts
.DisableIntegratedAS
;
855 PTO
.UnifiedLTO
= CodeGenOpts
.UnifiedLTO
;
857 LoopAnalysisManager LAM
;
858 FunctionAnalysisManager FAM
;
859 CGSCCAnalysisManager CGAM
;
860 ModuleAnalysisManager MAM
;
862 bool DebugPassStructure
= CodeGenOpts
.DebugPass
== "Structure";
863 PassInstrumentationCallbacks PIC
;
864 PrintPassOptions PrintPassOpts
;
865 PrintPassOpts
.Indent
= DebugPassStructure
;
866 PrintPassOpts
.SkipAnalyses
= DebugPassStructure
;
867 StandardInstrumentations
SI(
868 TheModule
->getContext(),
869 (CodeGenOpts
.DebugPassManager
|| DebugPassStructure
),
870 CodeGenOpts
.VerifyEach
, PrintPassOpts
);
871 SI
.registerCallbacks(PIC
, &MAM
);
872 PassBuilder
PB(TM
.get(), PTO
, PGOOpt
, &PIC
);
874 // Handle the assignment tracking feature options.
875 switch (CodeGenOpts
.getAssignmentTrackingMode()) {
876 case CodeGenOptions::AssignmentTrackingOpts::Forced
:
877 PB
.registerPipelineStartEPCallback(
878 [&](ModulePassManager
&MPM
, OptimizationLevel Level
) {
879 MPM
.addPass(AssignmentTrackingPass());
882 case CodeGenOptions::AssignmentTrackingOpts::Enabled
:
883 // Disable assignment tracking in LTO builds for now as the performance
884 // cost is too high. Disable for LLDB tuning due to llvm.org/PR43126.
885 if (!CodeGenOpts
.PrepareForThinLTO
&& !CodeGenOpts
.PrepareForLTO
&&
886 CodeGenOpts
.getDebuggerTuning() != llvm::DebuggerKind::LLDB
) {
887 PB
.registerPipelineStartEPCallback(
888 [&](ModulePassManager
&MPM
, OptimizationLevel Level
) {
889 // Only use assignment tracking if optimisations are enabled.
890 if (Level
!= OptimizationLevel::O0
)
891 MPM
.addPass(AssignmentTrackingPass());
895 case CodeGenOptions::AssignmentTrackingOpts::Disabled
:
899 // Enable verify-debuginfo-preserve-each for new PM.
900 DebugifyEachInstrumentation Debugify
;
901 DebugInfoPerPass DebugInfoBeforePass
;
902 if (CodeGenOpts
.EnableDIPreservationVerify
) {
903 Debugify
.setDebugifyMode(DebugifyMode::OriginalDebugInfo
);
904 Debugify
.setDebugInfoBeforePass(DebugInfoBeforePass
);
906 if (!CodeGenOpts
.DIBugsReportFilePath
.empty())
907 Debugify
.setOrigDIVerifyBugsReportFilePath(
908 CodeGenOpts
.DIBugsReportFilePath
);
909 Debugify
.registerCallbacks(PIC
, MAM
);
911 // Attempt to load pass plugins and register their callbacks with PB.
912 for (auto &PluginFN
: CodeGenOpts
.PassPlugins
) {
913 auto PassPlugin
= PassPlugin::Load(PluginFN
);
915 PassPlugin
->registerPassBuilderCallbacks(PB
);
917 Diags
.Report(diag::err_fe_unable_to_load_plugin
)
918 << PluginFN
<< toString(PassPlugin
.takeError());
921 for (const auto &PassCallback
: CodeGenOpts
.PassBuilderCallbacks
)
923 #define HANDLE_EXTENSION(Ext) \
924 get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
925 #include "llvm/Support/Extension.def"
927 // Register the target library analysis directly and give it a customized
929 std::unique_ptr
<TargetLibraryInfoImpl
> TLII(
930 llvm::driver::createTLII(TargetTriple
, CodeGenOpts
.getVecLib()));
931 FAM
.registerPass([&] { return TargetLibraryAnalysis(*TLII
); });
933 // Register all the basic analyses with the managers.
934 PB
.registerModuleAnalyses(MAM
);
935 PB
.registerCGSCCAnalyses(CGAM
);
936 PB
.registerFunctionAnalyses(FAM
);
937 PB
.registerLoopAnalyses(LAM
);
938 PB
.crossRegisterProxies(LAM
, FAM
, CGAM
, MAM
);
940 ModulePassManager MPM
;
941 // Add a verifier pass, before any other passes, to catch CodeGen issues.
942 if (CodeGenOpts
.VerifyModule
)
943 MPM
.addPass(VerifierPass());
945 if (!CodeGenOpts
.DisableLLVMPasses
) {
946 // Map our optimization levels into one of the distinct levels used to
947 // configure the pipeline.
948 OptimizationLevel Level
= mapToLevel(CodeGenOpts
);
950 const bool PrepareForThinLTO
= CodeGenOpts
.PrepareForThinLTO
;
951 const bool PrepareForLTO
= CodeGenOpts
.PrepareForLTO
;
953 if (LangOpts
.ObjCAutoRefCount
) {
954 PB
.registerPipelineStartEPCallback(
955 [](ModulePassManager
&MPM
, OptimizationLevel Level
) {
956 if (Level
!= OptimizationLevel::O0
)
958 createModuleToFunctionPassAdaptor(ObjCARCExpandPass()));
960 PB
.registerPipelineEarlySimplificationEPCallback(
961 [](ModulePassManager
&MPM
, OptimizationLevel Level
) {
962 if (Level
!= OptimizationLevel::O0
)
963 MPM
.addPass(ObjCARCAPElimPass());
965 PB
.registerScalarOptimizerLateEPCallback(
966 [](FunctionPassManager
&FPM
, OptimizationLevel Level
) {
967 if (Level
!= OptimizationLevel::O0
)
968 FPM
.addPass(ObjCARCOptPass());
972 // If we reached here with a non-empty index file name, then the index
973 // file was empty and we are not performing ThinLTO backend compilation
974 // (used in testing in a distributed build environment).
975 bool IsThinLTOPostLink
= !CodeGenOpts
.ThinLTOIndexFile
.empty();
976 // If so drop any the type test assume sequences inserted for whole program
977 // vtables so that codegen doesn't complain.
978 if (IsThinLTOPostLink
)
979 PB
.registerPipelineStartEPCallback(
980 [](ModulePassManager
&MPM
, OptimizationLevel Level
) {
981 MPM
.addPass(LowerTypeTestsPass(/*ExportSummary=*/nullptr,
982 /*ImportSummary=*/nullptr,
983 /*DropTypeTests=*/true));
986 // Register callbacks to schedule sanitizer passes at the appropriate part
988 if (LangOpts
.Sanitize
.has(SanitizerKind::LocalBounds
))
989 PB
.registerScalarOptimizerLateEPCallback(
990 [](FunctionPassManager
&FPM
, OptimizationLevel Level
) {
991 FPM
.addPass(BoundsCheckingPass());
994 if (LangOpts
.Sanitize
.has(SanitizerKind::Realtime
))
995 PB
.registerScalarOptimizerLateEPCallback(
996 [](FunctionPassManager
&FPM
, OptimizationLevel Level
) {
997 RealtimeSanitizerOptions Opts
;
998 FPM
.addPass(RealtimeSanitizerPass(Opts
));
1001 // Don't add sanitizers if we are here from ThinLTO PostLink. That already
1002 // done on PreLink stage.
1003 if (!IsThinLTOPostLink
) {
1004 addSanitizers(TargetTriple
, CodeGenOpts
, LangOpts
, PB
);
1005 addKCFIPass(TargetTriple
, LangOpts
, PB
);
1008 if (std::optional
<GCOVOptions
> Options
=
1009 getGCOVOptions(CodeGenOpts
, LangOpts
))
1010 PB
.registerPipelineStartEPCallback(
1011 [Options
](ModulePassManager
&MPM
, OptimizationLevel Level
) {
1012 MPM
.addPass(GCOVProfilerPass(*Options
));
1014 if (std::optional
<InstrProfOptions
> Options
=
1015 getInstrProfOptions(CodeGenOpts
, LangOpts
))
1016 PB
.registerPipelineStartEPCallback(
1017 [Options
](ModulePassManager
&MPM
, OptimizationLevel Level
) {
1018 MPM
.addPass(InstrProfilingLoweringPass(*Options
, false));
1021 // TODO: Consider passing the MemoryProfileOutput to the pass builder via
1022 // the PGOOptions, and set this up there.
1023 if (!CodeGenOpts
.MemoryProfileOutput
.empty()) {
1024 PB
.registerOptimizerLastEPCallback(
1025 [](ModulePassManager
&MPM
, OptimizationLevel Level
) {
1026 MPM
.addPass(createModuleToFunctionPassAdaptor(MemProfilerPass()));
1027 MPM
.addPass(ModuleMemProfilerPass());
1031 if (CodeGenOpts
.FatLTO
) {
1032 MPM
.addPass(PB
.buildFatLTODefaultPipeline(
1033 Level
, PrepareForThinLTO
,
1034 PrepareForThinLTO
|| shouldEmitRegularLTOSummary()));
1035 } else if (PrepareForThinLTO
) {
1036 MPM
.addPass(PB
.buildThinLTOPreLinkDefaultPipeline(Level
));
1037 } else if (PrepareForLTO
) {
1038 MPM
.addPass(PB
.buildLTOPreLinkDefaultPipeline(Level
));
1040 MPM
.addPass(PB
.buildPerModuleDefaultPipeline(Level
));
1044 // Link against bitcodes supplied via the -mlink-builtin-bitcode option
1045 if (CodeGenOpts
.LinkBitcodePostopt
)
1046 MPM
.addPass(LinkInModulesPass(BC
));
1048 // Add a verifier pass if requested. We don't have to do this if the action
1049 // requires code generation because there will already be a verifier pass in
1050 // the code-generation pipeline.
1051 // Since we already added a verifier pass above, this
1052 // might even not run the analysis, if previous passes caused no changes.
1053 if (!actionRequiresCodeGen(Action
) && CodeGenOpts
.VerifyModule
)
1054 MPM
.addPass(VerifierPass());
1056 if (Action
== Backend_EmitBC
|| Action
== Backend_EmitLL
||
1057 CodeGenOpts
.FatLTO
) {
1058 if (CodeGenOpts
.PrepareForThinLTO
&& !CodeGenOpts
.DisableLLVMPasses
) {
1059 if (!TheModule
->getModuleFlag("EnableSplitLTOUnit"))
1060 TheModule
->addModuleFlag(llvm::Module::Error
, "EnableSplitLTOUnit",
1061 CodeGenOpts
.EnableSplitLTOUnit
);
1062 if (Action
== Backend_EmitBC
) {
1063 if (!CodeGenOpts
.ThinLinkBitcodeFile
.empty()) {
1064 ThinLinkOS
= openOutputFile(CodeGenOpts
.ThinLinkBitcodeFile
);
1068 MPM
.addPass(ThinLTOBitcodeWriterPass(
1069 *OS
, ThinLinkOS
? &ThinLinkOS
->os() : nullptr));
1070 } else if (Action
== Backend_EmitLL
) {
1071 MPM
.addPass(PrintModulePass(*OS
, "", CodeGenOpts
.EmitLLVMUseLists
,
1072 /*EmitLTOSummary=*/true));
1075 // Emit a module summary by default for Regular LTO except for ld64
1077 bool EmitLTOSummary
= shouldEmitRegularLTOSummary();
1078 if (EmitLTOSummary
) {
1079 if (!TheModule
->getModuleFlag("ThinLTO") && !CodeGenOpts
.UnifiedLTO
)
1080 TheModule
->addModuleFlag(llvm::Module::Error
, "ThinLTO", uint32_t(0));
1081 if (!TheModule
->getModuleFlag("EnableSplitLTOUnit"))
1082 TheModule
->addModuleFlag(llvm::Module::Error
, "EnableSplitLTOUnit",
1085 if (Action
== Backend_EmitBC
) {
1086 MPM
.addPass(BitcodeWriterPass(*OS
, CodeGenOpts
.EmitLLVMUseLists
,
1088 } else if (Action
== Backend_EmitLL
) {
1089 MPM
.addPass(PrintModulePass(*OS
, "", CodeGenOpts
.EmitLLVMUseLists
,
1094 if (shouldEmitUnifiedLTOModueFlag())
1095 TheModule
->addModuleFlag(llvm::Module::Error
, "UnifiedLTO", uint32_t(1));
1098 // FIXME: This should eventually be replaced by a first-class driver option.
1099 // This should be done for both clang and flang simultaneously.
1100 // Print a textual, '-passes=' compatible, representation of pipeline if
1102 if (PrintPipelinePasses
) {
1103 MPM
.printPipeline(outs(), [&PIC
](StringRef ClassName
) {
1104 auto PassName
= PIC
.getPassNameForClassName(ClassName
);
1105 return PassName
.empty() ? ClassName
: PassName
;
1111 if (LangOpts
.HIPStdPar
&& !LangOpts
.CUDAIsDevice
&&
1112 LangOpts
.HIPStdParInterposeAlloc
)
1113 MPM
.addPass(HipStdParAllocationInterpositionPass());
1115 // Now that we have all of the passes ready, run them.
1117 PrettyStackTraceString
CrashInfo("Optimizer");
1118 llvm::TimeTraceScope
TimeScope("Optimizer");
1119 MPM
.run(*TheModule
, MAM
);
1123 void EmitAssemblyHelper::RunCodegenPipeline(
1124 BackendAction Action
, std::unique_ptr
<raw_pwrite_stream
> &OS
,
1125 std::unique_ptr
<llvm::ToolOutputFile
> &DwoOS
) {
1126 // We still use the legacy PM to run the codegen pipeline since the new PM
1127 // does not work with the codegen pipeline.
1128 // FIXME: make the new PM work with the codegen pipeline.
1129 legacy::PassManager CodeGenPasses
;
1131 // Append any output we need to the pass manager.
1133 case Backend_EmitAssembly
:
1134 case Backend_EmitMCNull
:
1135 case Backend_EmitObj
:
1137 createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
1138 if (!CodeGenOpts
.SplitDwarfOutput
.empty()) {
1139 DwoOS
= openOutputFile(CodeGenOpts
.SplitDwarfOutput
);
1143 if (!AddEmitPasses(CodeGenPasses
, Action
, *OS
,
1144 DwoOS
? &DwoOS
->os() : nullptr))
1145 // FIXME: Should we handle this error differently?
1152 // If -print-pipeline-passes is requested, don't run the legacy pass manager.
1153 // FIXME: when codegen is switched to use the new pass manager, it should also
1154 // emit pass names here.
1155 if (PrintPipelinePasses
) {
1160 PrettyStackTraceString
CrashInfo("Code generation");
1161 llvm::TimeTraceScope
TimeScope("CodeGenPasses");
1162 CodeGenPasses
.run(*TheModule
);
1166 void EmitAssemblyHelper::EmitAssembly(BackendAction Action
,
1167 std::unique_ptr
<raw_pwrite_stream
> OS
,
1168 BackendConsumer
*BC
) {
1169 TimeRegion
Region(CodeGenOpts
.TimePasses
? &CodeGenerationTime
: nullptr);
1170 setCommandLineOpts(CodeGenOpts
);
1172 bool RequiresCodeGen
= actionRequiresCodeGen(Action
);
1173 CreateTargetMachine(RequiresCodeGen
);
1175 if (RequiresCodeGen
&& !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
, BC
);
1185 RunCodegenPipeline(Action
, OS
, DwoOS
);
1193 static void runThinLTOBackend(
1194 DiagnosticsEngine
&Diags
, ModuleSummaryIndex
*CombinedIndex
,
1195 llvm::Module
*M
, const HeaderSearchOptions
&HeaderOpts
,
1196 const CodeGenOptions
&CGOpts
, const clang::TargetOptions
&TOpts
,
1197 const LangOptions
&LOpts
, std::unique_ptr
<raw_pwrite_stream
> OS
,
1198 std::string SampleProfile
, std::string ProfileRemapping
,
1199 BackendAction Action
) {
1200 DenseMap
<StringRef
, DenseMap
<GlobalValue::GUID
, GlobalValueSummary
*>>
1201 ModuleToDefinedGVSummaries
;
1202 CombinedIndex
->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries
);
1204 setCommandLineOpts(CGOpts
);
1206 // We can simply import the values mentioned in the combined index, since
1207 // we should only invoke this using the individual indexes written out
1208 // via a WriteIndexesThinBackend.
1209 FunctionImporter::ImportIDTable ImportIDs
;
1210 FunctionImporter::ImportMapTy
ImportList(ImportIDs
);
1211 if (!lto::initImportList(*M
, *CombinedIndex
, ImportList
))
1214 auto AddStream
= [&](size_t Task
, const Twine
&ModuleName
) {
1215 return std::make_unique
<CachedFileStream
>(std::move(OS
),
1216 CGOpts
.ObjectFilenameForDebug
);
1219 if (CGOpts
.SaveTempsFilePrefix
!= "") {
1220 if (Error E
= Conf
.addSaveTemps(CGOpts
.SaveTempsFilePrefix
+ ".",
1221 /* UseInputModulePath */ false)) {
1222 handleAllErrors(std::move(E
), [&](ErrorInfoBase
&EIB
) {
1223 errs() << "Error setting up ThinLTO save-temps: " << EIB
.message()
1228 Conf
.CPU
= TOpts
.CPU
;
1229 Conf
.CodeModel
= getCodeModel(CGOpts
);
1230 Conf
.MAttrs
= TOpts
.Features
;
1231 Conf
.RelocModel
= CGOpts
.RelocationModel
;
1232 std::optional
<CodeGenOptLevel
> OptLevelOrNone
=
1233 CodeGenOpt::getLevel(CGOpts
.OptimizationLevel
);
1234 assert(OptLevelOrNone
&& "Invalid optimization level!");
1235 Conf
.CGOptLevel
= *OptLevelOrNone
;
1236 Conf
.OptLevel
= CGOpts
.OptimizationLevel
;
1237 initTargetOptions(Diags
, Conf
.Options
, CGOpts
, TOpts
, LOpts
, HeaderOpts
);
1238 Conf
.SampleProfile
= std::move(SampleProfile
);
1239 Conf
.PTO
.LoopUnrolling
= CGOpts
.UnrollLoops
;
1240 // For historical reasons, loop interleaving is set to mirror setting for loop
1242 Conf
.PTO
.LoopInterleaving
= CGOpts
.UnrollLoops
;
1243 Conf
.PTO
.LoopVectorization
= CGOpts
.VectorizeLoop
;
1244 Conf
.PTO
.SLPVectorization
= CGOpts
.VectorizeSLP
;
1245 // Only enable CGProfilePass when using integrated assembler, since
1246 // non-integrated assemblers don't recognize .cgprofile section.
1247 Conf
.PTO
.CallGraphProfile
= !CGOpts
.DisableIntegratedAS
;
1249 // Context sensitive profile.
1250 if (CGOpts
.hasProfileCSIRInstr()) {
1251 Conf
.RunCSIRInstr
= true;
1252 Conf
.CSIRProfile
= std::move(CGOpts
.InstrProfileOutput
);
1253 } else if (CGOpts
.hasProfileCSIRUse()) {
1254 Conf
.RunCSIRInstr
= false;
1255 Conf
.CSIRProfile
= std::move(CGOpts
.ProfileInstrumentUsePath
);
1258 Conf
.ProfileRemapping
= std::move(ProfileRemapping
);
1259 Conf
.DebugPassManager
= CGOpts
.DebugPassManager
;
1260 Conf
.VerifyEach
= CGOpts
.VerifyEach
;
1261 Conf
.RemarksWithHotness
= CGOpts
.DiagnosticsWithHotness
;
1262 Conf
.RemarksFilename
= CGOpts
.OptRecordFile
;
1263 Conf
.RemarksPasses
= CGOpts
.OptRecordPasses
;
1264 Conf
.RemarksFormat
= CGOpts
.OptRecordFormat
;
1265 Conf
.SplitDwarfFile
= CGOpts
.SplitDwarfFile
;
1266 Conf
.SplitDwarfOutput
= CGOpts
.SplitDwarfOutput
;
1268 case Backend_EmitNothing
:
1269 Conf
.PreCodeGenModuleHook
= [](size_t Task
, const llvm::Module
&Mod
) {
1273 case Backend_EmitLL
:
1274 Conf
.PreCodeGenModuleHook
= [&](size_t Task
, const llvm::Module
&Mod
) {
1275 M
->print(*OS
, nullptr, CGOpts
.EmitLLVMUseLists
);
1279 case Backend_EmitBC
:
1280 Conf
.PreCodeGenModuleHook
= [&](size_t Task
, const llvm::Module
&Mod
) {
1281 WriteBitcodeToFile(*M
, *OS
, CGOpts
.EmitLLVMUseLists
);
1286 Conf
.CGFileType
= getCodeGenFileType(Action
);
1290 thinBackend(Conf
, -1, AddStream
, *M
, *CombinedIndex
, ImportList
,
1291 ModuleToDefinedGVSummaries
[M
->getModuleIdentifier()],
1292 /* ModuleMap */ nullptr, CGOpts
.CmdArgs
)) {
1293 handleAllErrors(std::move(E
), [&](ErrorInfoBase
&EIB
) {
1294 errs() << "Error running ThinLTO backend: " << EIB
.message() << '\n';
1299 void clang::EmitBackendOutput(
1300 DiagnosticsEngine
&Diags
, const HeaderSearchOptions
&HeaderOpts
,
1301 const CodeGenOptions
&CGOpts
, const clang::TargetOptions
&TOpts
,
1302 const LangOptions
&LOpts
, StringRef TDesc
, llvm::Module
*M
,
1303 BackendAction Action
, IntrusiveRefCntPtr
<llvm::vfs::FileSystem
> VFS
,
1304 std::unique_ptr
<raw_pwrite_stream
> OS
, BackendConsumer
*BC
) {
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
+ "': ");
1324 // A null CombinedIndex means we should skip ThinLTO compilation
1325 // (LLVM will optionally ignore empty index files, returning null instead
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
);
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
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
), BC
);
1349 // Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's
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
)
1368 llvm::embedBitcodeInModule(
1369 *M
, Buf
, CGOpts
.getEmbedBitcode() != CodeGenOptions::Embed_Marker
,
1370 CGOpts
.getEmbedBitcode() != CodeGenOptions::Embed_Bitcode
,
1374 void clang::EmbedObject(llvm::Module
*M
, const CodeGenOptions
&CGOpts
,
1375 DiagnosticsEngine
&Diags
) {
1376 if (CGOpts
.OffloadObjects
.empty())
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
;
1389 llvm::embedBufferInModule(*M
, **ObjectOrErr
, ".llvm.offloading",
1390 Align(object::OffloadBinary::getAlignment()));