19#include "llvm/ADT/ScopeExit.h"
20#include "llvm/ADT/StringExtras.h"
21#include "llvm/ADT/StringSwitch.h"
22#include "llvm/Analysis/GlobalsModRef.h"
23#include "llvm/Analysis/TargetLibraryInfo.h"
24#include "llvm/Analysis/TargetTransformInfo.h"
25#include "llvm/Bitcode/BitcodeReader.h"
26#include "llvm/Bitcode/BitcodeWriter.h"
27#include "llvm/Bitcode/BitcodeWriterPass.h"
28#include "llvm/CodeGen/TargetSubtargetInfo.h"
29#include "llvm/Frontend/Driver/CodeGenOptions.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/TargetRegistry.h"
40#include "llvm/Object/OffloadBinary.h"
41#include "llvm/Passes/PassBuilder.h"
42#include "llvm/Passes/PassPlugin.h"
43#include "llvm/Passes/StandardInstrumentations.h"
44#include "llvm/ProfileData/InstrProfCorrelator.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/Program.h"
50#include "llvm/Support/TimeProfiler.h"
51#include "llvm/Support/Timer.h"
52#include "llvm/Support/ToolOutputFile.h"
53#include "llvm/Support/VirtualFileSystem.h"
54#include "llvm/Support/raw_ostream.h"
55#include "llvm/Target/TargetMachine.h"
56#include "llvm/Target/TargetOptions.h"
57#include "llvm/TargetParser/SubtargetFeature.h"
58#include "llvm/TargetParser/Triple.h"
59#include "llvm/Transforms/HipStdPar/HipStdPar.h"
60#include "llvm/Transforms/IPO/EmbedBitcodePass.h"
61#include "llvm/Transforms/IPO/LowerTypeTests.h"
62#include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
63#include "llvm/Transforms/InstCombine/InstCombine.h"
64#include "llvm/Transforms/Instrumentation/AddressSanitizer.h"
65#include "llvm/Transforms/Instrumentation/AddressSanitizerOptions.h"
66#include "llvm/Transforms/Instrumentation/BoundsChecking.h"
67#include "llvm/Transforms/Instrumentation/DataFlowSanitizer.h"
68#include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
69#include "llvm/Transforms/Instrumentation/HWAddressSanitizer.h"
70#include "llvm/Transforms/Instrumentation/InstrProfiling.h"
71#include "llvm/Transforms/Instrumentation/KCFI.h"
72#include "llvm/Transforms/Instrumentation/LowerAllowCheckPass.h"
73#include "llvm/Transforms/Instrumentation/MemProfiler.h"
74#include "llvm/Transforms/Instrumentation/MemorySanitizer.h"
75#include "llvm/Transforms/Instrumentation/NumericalStabilitySanitizer.h"
76#include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
77#include "llvm/Transforms/Instrumentation/RealtimeSanitizer.h"
78#include "llvm/Transforms/Instrumentation/SanitizerBinaryMetadata.h"
79#include "llvm/Transforms/Instrumentation/SanitizerCoverage.h"
80#include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
81#include "llvm/Transforms/Instrumentation/TypeSanitizer.h"
82#include "llvm/Transforms/ObjCARC.h"
83#include "llvm/Transforms/Scalar/EarlyCSE.h"
84#include "llvm/Transforms/Scalar/GVN.h"
85#include "llvm/Transforms/Scalar/JumpThreading.h"
86#include "llvm/Transforms/Utils/Debugify.h"
87#include "llvm/Transforms/Utils/ModuleUtils.h"
94#define HANDLE_EXTENSION(Ext) \
95 llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
96#include "llvm/Support/Extension.def"
103 "sanitizer-early-opt-ep", cl::Optional,
104 cl::desc(
"Insert sanitizers on OptimizerEarlyEP."));
109 "pgo-cold-func-opt", cl::init(PGOOptions::ColdFuncOpt::Default), cl::Hidden,
111 "Function attribute to apply to cold functions as determined by PGO"),
112 cl::values(clEnumValN(PGOOptions::ColdFuncOpt::Default,
"default",
113 "Default (no attribute)"),
114 clEnumValN(PGOOptions::ColdFuncOpt::OptSize,
"optsize",
115 "Mark cold functions with optsize."),
116 clEnumValN(PGOOptions::ColdFuncOpt::MinSize,
"minsize",
117 "Mark cold functions with minsize."),
118 clEnumValN(PGOOptions::ColdFuncOpt::OptNone,
"optnone",
119 "Mark cold functions with optnone.")));
130std::string getDefaultProfileGenName() {
132 ?
"default_%m.proflite"
133 :
"default_%m.profraw";
136class EmitAssemblyHelper {
142 llvm::Module *TheModule;
145 std::unique_ptr<raw_pwrite_stream> OS;
149 TargetIRAnalysis getTargetIRAnalysis()
const {
151 return TM->getTargetIRAnalysis();
153 return TargetIRAnalysis();
164 void CreateTargetMachine(
bool MustCreateTM);
169 bool AddEmitPasses(legacy::PassManager &CodeGenPasses,
BackendAction Action,
170 raw_pwrite_stream &OS, raw_pwrite_stream *DwoOS);
172 std::unique_ptr<llvm::ToolOutputFile> openOutputFile(StringRef
Path) {
174 auto F = std::make_unique<llvm::ToolOutputFile>(
Path, EC,
175 llvm::sys::fs::OF_None);
177 Diags.
Report(diag::err_fe_unable_to_open_output) <<
Path << EC.message();
183 void RunOptimizationPipeline(
184 BackendAction Action, std::unique_ptr<raw_pwrite_stream> &OS,
185 std::unique_ptr<llvm::ToolOutputFile> &ThinLinkOS,
BackendConsumer *BC);
187 std::unique_ptr<raw_pwrite_stream> &OS,
188 std::unique_ptr<llvm::ToolOutputFile> &DwoOS);
195 bool shouldEmitRegularLTOSummary()
const {
196 return CodeGenOpts.PrepareForLTO && !CodeGenOpts.DisableLLVMPasses &&
197 TargetTriple.getVendor() != llvm::Triple::Apple;
203 bool shouldEmitUnifiedLTOModueFlag()
const {
204 return CodeGenOpts.UnifiedLTO &&
205 (CodeGenOpts.PrepareForThinLTO || shouldEmitRegularLTOSummary());
212 : CI(CI), Diags(CI.getDiagnostics()), CodeGenOpts(CGOpts),
213 TargetOpts(CI.getTargetOpts()), LangOpts(CI.getLangOpts()),
215 TargetTriple(TheModule->getTargetTriple()) {}
217 ~EmitAssemblyHelper() {
218 if (CodeGenOpts.DisableFree)
219 BuryPointer(std::move(TM));
222 std::unique_ptr<TargetMachine> TM;
225 void emitAssembly(
BackendAction Action, std::unique_ptr<raw_pwrite_stream> OS,
230static SanitizerCoverageOptions
232 SanitizerCoverageOptions Opts;
234 static_cast<SanitizerCoverageOptions::Type
>(CGOpts.SanitizeCoverageType);
235 Opts.IndirectCalls = CGOpts.SanitizeCoverageIndirectCalls;
236 Opts.TraceBB = CGOpts.SanitizeCoverageTraceBB;
237 Opts.TraceCmp = CGOpts.SanitizeCoverageTraceCmp;
238 Opts.TraceDiv = CGOpts.SanitizeCoverageTraceDiv;
239 Opts.TraceGep = CGOpts.SanitizeCoverageTraceGep;
240 Opts.Use8bitCounters = CGOpts.SanitizeCoverage8bitCounters;
241 Opts.TracePC = CGOpts.SanitizeCoverageTracePC;
242 Opts.TracePCGuard = CGOpts.SanitizeCoverageTracePCGuard;
243 Opts.NoPrune = CGOpts.SanitizeCoverageNoPrune;
244 Opts.Inline8bitCounters = CGOpts.SanitizeCoverageInline8bitCounters;
245 Opts.InlineBoolFlag = CGOpts.SanitizeCoverageInlineBoolFlag;
246 Opts.PCTable = CGOpts.SanitizeCoveragePCTable;
247 Opts.StackDepth = CGOpts.SanitizeCoverageStackDepth;
248 Opts.TraceLoads = CGOpts.SanitizeCoverageTraceLoads;
249 Opts.TraceStores = CGOpts.SanitizeCoverageTraceStores;
250 Opts.CollectControlFlow = CGOpts.SanitizeCoverageControlFlow;
254static SanitizerBinaryMetadataOptions
256 SanitizerBinaryMetadataOptions Opts;
257 Opts.Covered = CGOpts.SanitizeBinaryMetadataCovered;
258 Opts.Atomics = CGOpts.SanitizeBinaryMetadataAtomics;
259 Opts.UAR = CGOpts.SanitizeBinaryMetadataUAR;
268 if (!CGOpts.SanitizeAddressGlobalsDeadStripping)
270 switch (
T.getObjectFormat()) {
275 return !CGOpts.DisableIntegratedAS;
277 llvm::report_fatal_error(
"ASan not implemented for GOFF");
279 llvm::report_fatal_error(
"ASan not implemented for XCOFF.");
281 case Triple::DXContainer:
283 case Triple::UnknownObjectFormat:
289static std::optional<llvm::CodeModel::Model>
291 unsigned CodeModel = llvm::StringSwitch<unsigned>(CodeGenOpts.
CodeModel)
292 .Case(
"tiny", llvm::CodeModel::Tiny)
293 .Case(
"small", llvm::CodeModel::Small)
294 .Case(
"kernel", llvm::CodeModel::Kernel)
295 .Case(
"medium", llvm::CodeModel::Medium)
296 .Case(
"large", llvm::CodeModel::Large)
297 .Case(
"default", ~1u)
299 assert(CodeModel != ~0u &&
"invalid code model!");
300 if (CodeModel == ~1u)
302 return static_cast<llvm::CodeModel::Model
>(CodeModel);
307 return CodeGenFileType::ObjectFile;
309 return CodeGenFileType::Null;
312 return CodeGenFileType::AssemblyFile;
322 StringRef MainFilename) {
324 return std::string{};
326 std::string FlatCmdLine;
327 raw_string_ostream OS(FlatCmdLine);
328 bool PrintedOneArg =
false;
329 if (!StringRef(Args[0]).
contains(
"-cc1")) {
330 llvm::sys::printArg(OS,
"-cc1",
true);
331 PrintedOneArg =
true;
333 for (
unsigned i = 0; i < Args.size(); i++) {
334 StringRef Arg = Args[i];
337 if (Arg ==
"-main-file-name" || Arg ==
"-o") {
341 if (Arg.starts_with(
"-object-file-name") || Arg == MainFilename)
344 if (Arg.starts_with(
"-fmessage-length"))
348 llvm::sys::printArg(OS, Arg,
true);
349 PrintedOneArg =
true;
356 llvm::TargetOptions &Options) {
361 switch (LangOpts.getThreadModel()) {
362 case LangOptions::ThreadModelKind::POSIX:
363 Options.ThreadModel = llvm::ThreadModel::POSIX;
365 case LangOptions::ThreadModelKind::Single:
366 Options.ThreadModel = llvm::ThreadModel::Single;
371 assert((CodeGenOpts.
FloatABI ==
"soft" || CodeGenOpts.
FloatABI ==
"softfp" ||
373 "Invalid Floating Point ABI!");
374 Options.FloatABIType =
375 llvm::StringSwitch<llvm::FloatABI::ABIType>(CodeGenOpts.
FloatABI)
376 .Case(
"soft", llvm::FloatABI::Soft)
377 .Case(
"softfp", llvm::FloatABI::Soft)
378 .Case(
"hard", llvm::FloatABI::Hard)
379 .Default(llvm::FloatABI::Default);
382 switch (LangOpts.getDefaultFPContractMode()) {
383 case LangOptions::FPM_Off:
386 Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
388 case LangOptions::FPM_On:
389 case LangOptions::FPM_FastHonorPragmas:
390 Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
392 case LangOptions::FPM_Fast:
393 Options.AllowFPOpFusion = llvm::FPOpFusion::Fast;
397 Options.BinutilsVersion =
398 llvm::TargetMachine::parseBinutilsVersion(CodeGenOpts.
BinutilsVersion);
399 Options.UseInitArray = CodeGenOpts.UseInitArray;
400 Options.DisableIntegratedAS = CodeGenOpts.DisableIntegratedAS;
406 Options.ExceptionModel = llvm::ExceptionHandling::SjLj;
408 Options.ExceptionModel = llvm::ExceptionHandling::WinEH;
410 Options.ExceptionModel = llvm::ExceptionHandling::DwarfCFI;
412 Options.ExceptionModel = llvm::ExceptionHandling::Wasm;
414 Options.NoInfsFPMath = LangOpts.NoHonorInfs;
415 Options.NoNaNsFPMath = LangOpts.NoHonorNaNs;
416 Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS;
417 Options.UnsafeFPMath = LangOpts.AllowFPReassoc && LangOpts.AllowRecip &&
418 LangOpts.NoSignedZero && LangOpts.ApproxFunc &&
419 (LangOpts.getDefaultFPContractMode() ==
420 LangOptions::FPModeKind::FPM_Fast ||
421 LangOpts.getDefaultFPContractMode() ==
422 LangOptions::FPModeKind::FPM_FastHonorPragmas);
423 Options.ApproxFuncFPMath = LangOpts.ApproxFunc;
425 Options.BBAddrMap = CodeGenOpts.BBAddrMap;
427 llvm::StringSwitch<llvm::BasicBlockSection>(CodeGenOpts.
BBSections)
428 .Case(
"all", llvm::BasicBlockSection::All)
429 .StartsWith(
"list=", llvm::BasicBlockSection::List)
430 .Case(
"none", llvm::BasicBlockSection::None)
431 .Default(llvm::BasicBlockSection::None);
433 if (Options.BBSections == llvm::BasicBlockSection::List) {
434 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
435 MemoryBuffer::getFile(CodeGenOpts.
BBSections.substr(5));
437 Diags.
Report(diag::err_fe_unable_to_load_basic_block_sections_file)
438 << MBOrErr.getError().message();
441 Options.BBSectionsFuncListBuf = std::move(*MBOrErr);
444 Options.EnableMachineFunctionSplitter = CodeGenOpts.SplitMachineFunctions;
445 Options.FunctionSections = CodeGenOpts.FunctionSections;
446 Options.DataSections = CodeGenOpts.DataSections;
447 Options.IgnoreXCOFFVisibility = LangOpts.IgnoreXCOFFVisibility;
448 Options.UniqueSectionNames = CodeGenOpts.UniqueSectionNames;
449 Options.UniqueBasicBlockSectionNames =
450 CodeGenOpts.UniqueBasicBlockSectionNames;
451 Options.SeparateNamedSections = CodeGenOpts.SeparateNamedSections;
452 Options.TLSSize = CodeGenOpts.TLSSize;
453 Options.EnableTLSDESC = CodeGenOpts.EnableTLSDESC;
454 Options.EmulatedTLS = CodeGenOpts.EmulatedTLS;
455 Options.DebuggerTuning = CodeGenOpts.getDebuggerTuning();
456 Options.EmitStackSizeSection = CodeGenOpts.StackSizeSection;
458 Options.EmitAddrsig = CodeGenOpts.Addrsig;
459 Options.ForceDwarfFrameSection = CodeGenOpts.ForceDwarfFrameSection;
460 Options.EmitCallSiteInfo = CodeGenOpts.EmitCallSiteInfo;
461 Options.EnableAIXExtendedAltivecABI = LangOpts.EnableAIXExtendedAltivecABI;
462 Options.XRayFunctionIndex = CodeGenOpts.XRayFunctionIndex;
463 Options.LoopAlignment = CodeGenOpts.LoopAlignment;
464 Options.DebugStrictDwarf = CodeGenOpts.DebugStrictDwarf;
466 Options.Hotpatch = CodeGenOpts.HotPatch;
467 Options.JMCInstrument = CodeGenOpts.JMCInstrument;
468 Options.XCOFFReadOnlyPointers = CodeGenOpts.XCOFFReadOnlyPointers;
470 switch (CodeGenOpts.getSwiftAsyncFramePointer()) {
471 case CodeGenOptions::SwiftAsyncFramePointerKind::Auto:
472 Options.SwiftAsyncFramePointer =
473 SwiftAsyncFramePointerMode::DeploymentBased;
476 case CodeGenOptions::SwiftAsyncFramePointerKind::Always:
477 Options.SwiftAsyncFramePointer = SwiftAsyncFramePointerMode::Always;
480 case CodeGenOptions::SwiftAsyncFramePointerKind::Never:
481 Options.SwiftAsyncFramePointer = SwiftAsyncFramePointerMode::Never;
486 Options.MCOptions.EmitDwarfUnwind = CodeGenOpts.getEmitDwarfUnwind();
487 Options.MCOptions.EmitCompactUnwindNonCanonical =
488 CodeGenOpts.EmitCompactUnwindNonCanonical;
489 Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll;
490 Options.MCOptions.MCSaveTempLabels = CodeGenOpts.SaveTempLabels;
491 Options.MCOptions.MCUseDwarfDirectory =
492 CodeGenOpts.NoDwarfDirectoryAsm
493 ? llvm::MCTargetOptions::DisableDwarfDirectory
494 : llvm::MCTargetOptions::EnableDwarfDirectory;
495 Options.MCOptions.MCNoExecStack = CodeGenOpts.NoExecStack;
496 Options.MCOptions.MCIncrementalLinkerCompatible =
497 CodeGenOpts.IncrementalLinkerCompatible;
498 Options.MCOptions.MCFatalWarnings = CodeGenOpts.FatalWarnings;
499 Options.MCOptions.MCNoWarn = CodeGenOpts.NoWarn;
500 Options.MCOptions.AsmVerbose = CodeGenOpts.AsmVerbose;
501 Options.MCOptions.Dwarf64 = CodeGenOpts.Dwarf64;
502 Options.MCOptions.PreserveAsmComments = CodeGenOpts.PreserveAsmComments;
503 Options.MCOptions.Crel = CodeGenOpts.Crel;
504 Options.MCOptions.ImplicitMapSyms = CodeGenOpts.ImplicitMapSyms;
505 Options.MCOptions.X86RelaxRelocations = CodeGenOpts.X86RelaxRelocations;
506 Options.MCOptions.CompressDebugSections =
507 CodeGenOpts.getCompressDebugSections();
508 if (CodeGenOpts.OutputAsmVariant != 3)
509 Options.MCOptions.OutputAsmVariant = CodeGenOpts.OutputAsmVariant;
510 Options.MCOptions.ABIName = TargetOpts.
ABI;
511 for (
const auto &Entry : HSOpts.UserEntries)
512 if (!Entry.IsFramework &&
513 (Entry.Group == frontend::IncludeDirGroup::Quoted ||
514 Entry.Group == frontend::IncludeDirGroup::Angled ||
515 Entry.Group == frontend::IncludeDirGroup::System))
516 Options.MCOptions.IASSearchPaths.push_back(
517 Entry.IgnoreSysRoot ? Entry.Path : HSOpts.Sysroot + Entry.Path);
518 Options.MCOptions.Argv0 = CodeGenOpts.
Argv0 ? CodeGenOpts.
Argv0 :
"";
522 Options.MCOptions.PPCUseFullRegisterNames =
523 CodeGenOpts.PPCUseFullRegisterNames;
524 Options.MisExpect = CodeGenOpts.MisExpect;
529static std::optional<GCOVOptions>
540 Options.NoRedZone = CodeGenOpts.DisableRedZone;
543 Options.Atomic = CodeGenOpts.AtomicProfileUpdate;
547static std::optional<InstrProfOptions>
552 InstrProfOptions Options;
553 Options.NoRedZone = CodeGenOpts.DisableRedZone;
555 Options.Atomic = CodeGenOpts.AtomicProfileUpdate;
561 BackendArgs.push_back(
"clang");
563 BackendArgs.push_back(
"-debug-pass");
564 BackendArgs.push_back(CodeGenOpts.
DebugPass.c_str());
567 BackendArgs.push_back(
"-limit-float-precision");
573 if (BackendArgs.size() == 1)
575 BackendArgs.push_back(
nullptr);
579 llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
583void EmitAssemblyHelper::CreateTargetMachine(
bool MustCreateTM) {
586 std::string Triple = TheModule->getTargetTriple();
587 const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
590 Diags.
Report(diag::err_fe_unable_to_create_target) <<
Error;
594 std::optional<llvm::CodeModel::Model> CM =
getCodeModel(CodeGenOpts);
595 std::string FeaturesStr =
598 std::optional<CodeGenOptLevel> OptLevelOrNone =
599 CodeGenOpt::getLevel(CodeGenOpts.OptimizationLevel);
600 assert(OptLevelOrNone &&
"Invalid optimization level!");
601 CodeGenOptLevel OptLevel = *OptLevelOrNone;
603 llvm::TargetOptions Options;
606 TM.reset(TheTarget->createTargetMachine(Triple, TargetOpts.
CPU, FeaturesStr,
607 Options, RM, CM, OptLevel));
611bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager &CodeGenPasses,
613 raw_pwrite_stream &OS,
614 raw_pwrite_stream *DwoOS) {
616 std::unique_ptr<TargetLibraryInfoImpl> TLII(
617 llvm::driver::createTLII(TargetTriple, CodeGenOpts.getVecLib()));
618 CodeGenPasses.add(
new TargetLibraryInfoWrapperPass(*TLII));
624 if (TM->addPassesToEmitFile(CodeGenPasses, OS, DwoOS, CGFT,
625 !CodeGenOpts.VerifyModule)) {
626 Diags.
Report(diag::err_fe_unable_to_interface_with_target);
634 switch (Opts.OptimizationLevel) {
636 llvm_unreachable(
"Invalid optimization level!");
639 return OptimizationLevel::O0;
642 return OptimizationLevel::O1;
645 switch (Opts.OptimizeSize) {
647 llvm_unreachable(
"Invalid optimization level for size!");
650 return OptimizationLevel::O2;
653 return OptimizationLevel::Os;
656 return OptimizationLevel::Oz;
660 return OptimizationLevel::O3;
667 if (TargetTriple.getArch() == llvm::Triple::x86_64 ||
668 TargetTriple.isAArch64(64) || TargetTriple.isRISCV())
672 PB.registerOptimizerLastEPCallback(
673 [&](ModulePassManager &MPM, OptimizationLevel Level, ThinOrFullLTOPhase) {
674 if (Level == OptimizationLevel::O0 &&
676 MPM.addPass(createModuleToFunctionPassAdaptor(KCFIPass()));
681 PB.registerPeepholeEPCallback(
682 [&](FunctionPassManager &FPM, OptimizationLevel Level) {
683 if (Level != OptimizationLevel::O0 &&
685 FPM.addPass(KCFIPass());
692 auto SanitizersCallback = [&](ModulePassManager &MPM, OptimizationLevel Level,
693 ThinOrFullLTOPhase) {
696 MPM.addPass(SanitizerCoveragePass(
702 MPM.addPass(SanitizerBinaryMetadataPass(
707 auto MSanPass = [&](
SanitizerMask Mask,
bool CompileKernel) {
709 int TrackOrigins = CodeGenOpts.SanitizeMemoryTrackOrigins;
712 MemorySanitizerOptions options(TrackOrigins, Recover, CompileKernel,
713 CodeGenOpts.SanitizeMemoryParamRetval);
714 MPM.addPass(MemorySanitizerPass(options));
715 if (Level != OptimizationLevel::O0) {
720 MPM.addPass(RequireAnalysisPass<GlobalsAA, llvm::Module>());
721 FunctionPassManager FPM;
722 FPM.addPass(EarlyCSEPass(
true ));
723 FPM.addPass(InstCombinePass());
724 FPM.addPass(JumpThreadingPass());
725 FPM.addPass(GVNPass());
726 FPM.addPass(InstCombinePass());
727 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
731 MSanPass(SanitizerKind::Memory,
false);
732 MSanPass(SanitizerKind::KernelMemory,
true);
734 if (LangOpts.
Sanitize.
has(SanitizerKind::Thread)) {
735 MPM.addPass(ModuleThreadSanitizerPass());
736 MPM.addPass(createModuleToFunctionPassAdaptor(ThreadSanitizerPass()));
740 MPM.addPass(TypeSanitizerPass());
742 if (LangOpts.
Sanitize.
has(SanitizerKind::NumericalStability))
743 MPM.addPass(NumericalStabilitySanitizerPass());
745 if (LangOpts.
Sanitize.
has(SanitizerKind::Realtime))
746 MPM.addPass(RealtimeSanitizerPass());
748 auto ASanPass = [&](
SanitizerMask Mask,
bool CompileKernel) {
751 bool UseOdrIndicator = CodeGenOpts.SanitizeAddressUseOdrIndicator;
752 llvm::AsanDtorKind DestructorKind =
753 CodeGenOpts.getSanitizeAddressDtor();
754 AddressSanitizerOptions Opts;
755 Opts.CompileKernel = CompileKernel;
757 Opts.UseAfterScope = CodeGenOpts.SanitizeAddressUseAfterScope;
758 Opts.UseAfterReturn = CodeGenOpts.getSanitizeAddressUseAfterReturn();
759 MPM.addPass(AddressSanitizerPass(Opts, UseGlobalGC, UseOdrIndicator,
763 ASanPass(SanitizerKind::Address,
false);
764 ASanPass(SanitizerKind::KernelAddress,
true);
766 auto HWASanPass = [&](
SanitizerMask Mask,
bool CompileKernel) {
769 MPM.addPass(HWAddressSanitizerPass(
770 {CompileKernel, Recover,
771 CodeGenOpts.OptimizationLevel == 0}));
774 HWASanPass(SanitizerKind::HWAddress,
false);
775 HWASanPass(SanitizerKind::KernelHWAddress,
true);
777 if (LangOpts.
Sanitize.
has(SanitizerKind::DataFlow)) {
782 PB.registerOptimizerEarlyEPCallback(
783 [SanitizersCallback](ModulePassManager &MPM, OptimizationLevel Level,
784 ThinOrFullLTOPhase Phase) {
785 ModulePassManager NewMPM;
786 SanitizersCallback(NewMPM, Level, Phase);
787 if (!NewMPM.isEmpty()) {
789 NewMPM.addPass(RequireAnalysisPass<GlobalsAA, llvm::Module>());
790 MPM.addPass(std::move(NewMPM));
795 PB.registerOptimizerLastEPCallback(SanitizersCallback);
798 if (LowerAllowCheckPass::IsRequested()) {
800 PB.registerOptimizerEarlyEPCallback([&](ModulePassManager &MPM,
801 OptimizationLevel Level,
802 ThinOrFullLTOPhase Phase) {
803 LowerAllowCheckPass::Options Opts;
804 MPM.addPass(createModuleToFunctionPassAdaptor(LowerAllowCheckPass(Opts)));
809void EmitAssemblyHelper::RunOptimizationPipeline(
810 BackendAction Action, std::unique_ptr<raw_pwrite_stream> &OS,
811 std::unique_ptr<llvm::ToolOutputFile> &ThinLinkOS,
BackendConsumer *BC) {
812 std::optional<PGOOptions> PGOOpt;
821 CodeGenOpts.DebugInfoForProfiling,
822 false, CodeGenOpts.AtomicProfileUpdate);
826 : PGOOptions::NoCSAction;
831 CodeGenOpts.DebugInfoForProfiling);
838 CodeGenOpts.DebugInfoForProfiling, CodeGenOpts.PseudoProbeForProfiling);
842 PGOOptions::NoAction, PGOOptions::NoCSAction,
844 else if (CodeGenOpts.PseudoProbeForProfiling)
847 PGOOptions(
"",
"",
"",
"",
nullptr,
848 PGOOptions::NoAction, PGOOptions::NoCSAction,
850 else if (CodeGenOpts.DebugInfoForProfiling)
852 PGOOpt = PGOOptions(
"",
"",
"",
"",
nullptr,
853 PGOOptions::NoAction, PGOOptions::NoCSAction,
859 "Cannot have both CSProfileUse pass and CSProfileGen pass at "
862 assert(PGOOpt->Action != PGOOptions::IRInstr &&
863 PGOOpt->Action != PGOOptions::SampleUse &&
864 "Cannot run CSProfileGen pass with ProfileGen or SampleUse "
867 ? getDefaultProfileGenName()
868 : CodeGenOpts.InstrProfileOutput;
869 PGOOpt->CSAction = PGOOptions::CSIRInstr;
871 PGOOpt = PGOOptions(
"",
873 ? getDefaultProfileGenName()
876 PGOOptions::NoAction, PGOOptions::CSIRInstr,
880 TM->setPGOOption(PGOOpt);
882 PipelineTuningOptions PTO;
883 PTO.LoopUnrolling = CodeGenOpts.UnrollLoops;
886 PTO.LoopInterleaving = CodeGenOpts.UnrollLoops;
887 PTO.LoopVectorization = CodeGenOpts.VectorizeLoop;
888 PTO.SLPVectorization = CodeGenOpts.VectorizeSLP;
889 PTO.MergeFunctions = CodeGenOpts.MergeFunctions;
892 PTO.CallGraphProfile = !CodeGenOpts.DisableIntegratedAS;
893 PTO.UnifiedLTO = CodeGenOpts.UnifiedLTO;
895 LoopAnalysisManager LAM;
896 FunctionAnalysisManager FAM;
897 CGSCCAnalysisManager CGAM;
898 ModuleAnalysisManager MAM;
900 bool DebugPassStructure = CodeGenOpts.
DebugPass ==
"Structure";
901 PassInstrumentationCallbacks PIC;
902 PrintPassOptions PrintPassOpts;
903 PrintPassOpts.Indent = DebugPassStructure;
904 PrintPassOpts.SkipAnalyses = DebugPassStructure;
905 StandardInstrumentations SI(
906 TheModule->getContext(),
907 (CodeGenOpts.DebugPassManager || DebugPassStructure),
908 CodeGenOpts.VerifyEach, PrintPassOpts);
909 SI.registerCallbacks(PIC, &MAM);
910 PassBuilder PB(TM.get(), PTO, PGOOpt, &PIC);
913 switch (CodeGenOpts.getAssignmentTrackingMode()) {
914 case CodeGenOptions::AssignmentTrackingOpts::Forced:
915 PB.registerPipelineStartEPCallback(
916 [&](ModulePassManager &MPM, OptimizationLevel Level) {
917 MPM.addPass(AssignmentTrackingPass());
920 case CodeGenOptions::AssignmentTrackingOpts::Enabled:
923 if (!CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.PrepareForLTO &&
924 CodeGenOpts.getDebuggerTuning() != llvm::DebuggerKind::LLDB) {
925 PB.registerPipelineStartEPCallback(
926 [&](ModulePassManager &MPM, OptimizationLevel Level) {
928 if (Level != OptimizationLevel::O0)
929 MPM.addPass(AssignmentTrackingPass());
933 case CodeGenOptions::AssignmentTrackingOpts::Disabled:
938 DebugifyEachInstrumentation Debugify;
939 DebugInfoPerPass DebugInfoBeforePass;
940 if (CodeGenOpts.EnableDIPreservationVerify) {
941 Debugify.setDebugifyMode(DebugifyMode::OriginalDebugInfo);
942 Debugify.setDebugInfoBeforePass(DebugInfoBeforePass);
945 Debugify.setOrigDIVerifyBugsReportFilePath(
947 Debugify.registerCallbacks(PIC, MAM);
951 auto PassPlugin = PassPlugin::Load(PluginFN);
953 PassPlugin->registerPassBuilderCallbacks(PB);
955 Diags.
Report(diag::err_fe_unable_to_load_plugin)
956 << PluginFN <<
toString(PassPlugin.takeError());
961#define HANDLE_EXTENSION(Ext) \
962 get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
963#include "llvm/Support/Extension.def"
967 std::unique_ptr<TargetLibraryInfoImpl> TLII(
968 llvm::driver::createTLII(TargetTriple, CodeGenOpts.getVecLib()));
969 FAM.registerPass([&] {
return TargetLibraryAnalysis(*TLII); });
972 PB.registerModuleAnalyses(MAM);
973 PB.registerCGSCCAnalyses(CGAM);
974 PB.registerFunctionAnalyses(FAM);
975 PB.registerLoopAnalyses(LAM);
976 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
978 ModulePassManager MPM;
980 if (CodeGenOpts.VerifyModule)
981 MPM.addPass(VerifierPass());
983 if (!CodeGenOpts.DisableLLVMPasses) {
988 const bool PrepareForThinLTO = CodeGenOpts.PrepareForThinLTO;
989 const bool PrepareForLTO = CodeGenOpts.PrepareForLTO;
991 if (LangOpts.ObjCAutoRefCount) {
992 PB.registerPipelineStartEPCallback(
993 [](ModulePassManager &MPM, OptimizationLevel Level) {
994 if (Level != OptimizationLevel::O0)
996 createModuleToFunctionPassAdaptor(ObjCARCExpandPass()));
998 PB.registerPipelineEarlySimplificationEPCallback(
999 [](ModulePassManager &MPM, OptimizationLevel Level,
1000 ThinOrFullLTOPhase) {
1001 if (Level != OptimizationLevel::O0)
1002 MPM.addPass(ObjCARCAPElimPass());
1004 PB.registerScalarOptimizerLateEPCallback(
1005 [](FunctionPassManager &FPM, OptimizationLevel Level) {
1006 if (Level != OptimizationLevel::O0)
1007 FPM.addPass(ObjCARCOptPass());
1017 if (IsThinLTOPostLink)
1018 PB.registerPipelineStartEPCallback(
1019 [](ModulePassManager &MPM, OptimizationLevel Level) {
1020 MPM.addPass(LowerTypeTestsPass(
1023 lowertypetests::DropTestKind::Assume));
1028 if (LangOpts.
Sanitize.
has(SanitizerKind::LocalBounds))
1029 PB.registerScalarOptimizerLateEPCallback([
this](FunctionPassManager &FPM,
1030 OptimizationLevel Level) {
1031 BoundsCheckingPass::Options Options;
1034 static_assert(SanitizerKind::SO_LocalBounds <=
1035 std::numeric_limits<
1036 decltype(Options.GuardKind)::value_type>::max(),
1037 "Update type of llvm.allow.ubsan.check to represent "
1038 "SanitizerKind::SO_LocalBounds.");
1039 Options.GuardKind = SanitizerKind::SO_LocalBounds;
1046 CodeGenOpts.SanitizeMinimalRuntime),
1048 CodeGenOpts.SanitizeRecover.has(SanitizerKind::LocalBounds),
1051 FPM.addPass(BoundsCheckingPass(Options));
1056 if (!IsThinLTOPostLink) {
1061 if (std::optional<GCOVOptions> Options =
1063 PB.registerPipelineStartEPCallback(
1064 [Options](ModulePassManager &MPM, OptimizationLevel Level) {
1065 MPM.addPass(GCOVProfilerPass(*Options));
1067 if (std::optional<InstrProfOptions> Options =
1069 PB.registerPipelineStartEPCallback(
1070 [Options](ModulePassManager &MPM, OptimizationLevel Level) {
1071 MPM.addPass(InstrProfilingLoweringPass(*Options,
false));
1077 PB.registerOptimizerLastEPCallback([](ModulePassManager &MPM,
1078 OptimizationLevel Level,
1079 ThinOrFullLTOPhase) {
1080 MPM.addPass(createModuleToFunctionPassAdaptor(MemProfilerPass()));
1081 MPM.addPass(ModuleMemProfilerPass());
1085 if (CodeGenOpts.FatLTO) {
1086 MPM.addPass(PB.buildFatLTODefaultPipeline(
1087 Level, PrepareForThinLTO,
1088 PrepareForThinLTO || shouldEmitRegularLTOSummary()));
1089 }
else if (PrepareForThinLTO) {
1090 MPM.addPass(PB.buildThinLTOPreLinkDefaultPipeline(Level));
1091 }
else if (PrepareForLTO) {
1092 MPM.addPass(PB.buildLTOPreLinkDefaultPipeline(Level));
1094 MPM.addPass(PB.buildPerModuleDefaultPipeline(Level));
1099 if (CodeGenOpts.LinkBitcodePostopt)
1108 MPM.addPass(VerifierPass());
1111 CodeGenOpts.FatLTO) {
1112 if (CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.DisableLLVMPasses) {
1113 if (!TheModule->getModuleFlag(
"EnableSplitLTOUnit"))
1114 TheModule->addModuleFlag(llvm::Module::Error,
"EnableSplitLTOUnit",
1115 CodeGenOpts.EnableSplitLTOUnit);
1122 MPM.addPass(ThinLTOBitcodeWriterPass(
1123 *OS, ThinLinkOS ? &ThinLinkOS->os() :
nullptr));
1125 MPM.addPass(PrintModulePass(*OS,
"", CodeGenOpts.EmitLLVMUseLists,
1131 bool EmitLTOSummary = shouldEmitRegularLTOSummary();
1132 if (EmitLTOSummary) {
1133 if (!TheModule->getModuleFlag(
"ThinLTO") && !CodeGenOpts.UnifiedLTO)
1134 TheModule->addModuleFlag(llvm::Module::Error,
"ThinLTO",
uint32_t(0));
1135 if (!TheModule->getModuleFlag(
"EnableSplitLTOUnit"))
1136 TheModule->addModuleFlag(llvm::Module::Error,
"EnableSplitLTOUnit",
1140 MPM.addPass(BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists,
1143 MPM.addPass(PrintModulePass(*OS,
"", CodeGenOpts.EmitLLVMUseLists,
1148 if (shouldEmitUnifiedLTOModueFlag())
1149 TheModule->addModuleFlag(llvm::Module::Error,
"UnifiedLTO",
uint32_t(1));
1157 MPM.printPipeline(outs(), [&PIC](StringRef ClassName) {
1158 auto PassName = PIC.getPassNameForClassName(ClassName);
1159 return PassName.empty() ? ClassName : PassName;
1165 if (LangOpts.HIPStdPar && !LangOpts.CUDAIsDevice &&
1166 LangOpts.HIPStdParInterposeAlloc)
1167 MPM.addPass(HipStdParAllocationInterpositionPass());
1171 PrettyStackTraceString CrashInfo(
"Optimizer");
1172 llvm::TimeTraceScope TimeScope(
"Optimizer");
1178 MPM.run(*TheModule, MAM);
1184void EmitAssemblyHelper::RunCodegenPipeline(
1185 BackendAction Action, std::unique_ptr<raw_pwrite_stream> &OS,
1186 std::unique_ptr<llvm::ToolOutputFile> &DwoOS) {
1190 legacy::PassManager CodeGenPasses;
1198 createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
1204 if (!AddEmitPasses(CodeGenPasses, Action, *OS,
1205 DwoOS ? &DwoOS->os() :
nullptr))
1221 PrettyStackTraceString CrashInfo(
"Code generation");
1222 llvm::TimeTraceScope TimeScope(
"CodeGenPasses");
1225 timer.init(
"codegen",
"Machine code generation", CI.
getTimerGroup());
1228 CodeGenPasses.run(*TheModule);
1235 std::unique_ptr<raw_pwrite_stream> OS,
1240 CreateTargetMachine(RequiresCodeGen);
1242 if (RequiresCodeGen && !TM)
1245 TheModule->setDataLayout(TM->createDataLayout());
1248 cl::PrintOptionValues();
1250 std::unique_ptr<llvm::ToolOutputFile> ThinLinkOS, DwoOS;
1251 RunOptimizationPipeline(Action, OS, ThinLinkOS, BC);
1252 RunCodegenPipeline(Action, OS, DwoOS);
1262 llvm::Module *M, std::unique_ptr<raw_pwrite_stream> OS,
1263 std::string SampleProfile, std::string ProfileRemapping,
1268 DenseMap<StringRef, DenseMap<GlobalValue::GUID, GlobalValueSummary *>>
1269 ModuleToDefinedGVSummaries;
1270 CombinedIndex->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
1277 FunctionImporter::ImportIDTable ImportIDs;
1278 FunctionImporter::ImportMapTy ImportList(ImportIDs);
1279 if (!lto::initImportList(*M, *CombinedIndex, ImportList))
1282 auto AddStream = [&](
size_t Task,
const Twine &ModuleName) {
1283 return std::make_unique<CachedFileStream>(std::move(OS),
1284 CGOpts.ObjectFilenameForDebug);
1287 if (CGOpts.SaveTempsFilePrefix !=
"") {
1288 if (Error
E = Conf.addSaveTemps(CGOpts.SaveTempsFilePrefix +
".",
1290 handleAllErrors(std::move(
E), [&](ErrorInfoBase &EIB) {
1291 errs() <<
"Error setting up ThinLTO save-temps: " << EIB.message()
1296 Conf.CPU = TOpts.CPU;
1298 Conf.MAttrs = TOpts.Features;
1299 Conf.RelocModel = CGOpts.RelocationModel;
1300 std::optional<CodeGenOptLevel> OptLevelOrNone =
1301 CodeGenOpt::getLevel(CGOpts.OptimizationLevel);
1302 assert(OptLevelOrNone &&
"Invalid optimization level!");
1303 Conf.CGOptLevel = *OptLevelOrNone;
1304 Conf.OptLevel = CGOpts.OptimizationLevel;
1306 Conf.SampleProfile = std::move(SampleProfile);
1307 Conf.PTO.LoopUnrolling = CGOpts.UnrollLoops;
1310 Conf.PTO.LoopInterleaving = CGOpts.UnrollLoops;
1311 Conf.PTO.LoopVectorization = CGOpts.VectorizeLoop;
1312 Conf.PTO.SLPVectorization = CGOpts.VectorizeSLP;
1315 Conf.PTO.CallGraphProfile = !CGOpts.DisableIntegratedAS;
1318 if (CGOpts.hasProfileCSIRInstr()) {
1319 Conf.RunCSIRInstr =
true;
1320 Conf.CSIRProfile = std::move(CGOpts.InstrProfileOutput);
1321 }
else if (CGOpts.hasProfileCSIRUse()) {
1322 Conf.RunCSIRInstr =
false;
1323 Conf.CSIRProfile = std::move(CGOpts.ProfileInstrumentUsePath);
1326 Conf.ProfileRemapping = std::move(ProfileRemapping);
1327 Conf.DebugPassManager = CGOpts.DebugPassManager;
1328 Conf.VerifyEach = CGOpts.VerifyEach;
1329 Conf.RemarksWithHotness = CGOpts.DiagnosticsWithHotness;
1330 Conf.RemarksFilename = CGOpts.OptRecordFile;
1331 Conf.RemarksPasses = CGOpts.OptRecordPasses;
1332 Conf.RemarksFormat = CGOpts.OptRecordFormat;
1333 Conf.SplitDwarfFile = CGOpts.SplitDwarfFile;
1334 Conf.SplitDwarfOutput = CGOpts.SplitDwarfOutput;
1337 Conf.PreCodeGenModuleHook = [](
size_t Task,
const llvm::Module &Mod) {
1342 Conf.PreCodeGenModuleHook = [&](
size_t Task,
const llvm::Module &Mod) {
1343 M->print(*OS,
nullptr, CGOpts.EmitLLVMUseLists);
1348 Conf.PreCodeGenModuleHook = [&](
size_t Task,
const llvm::Module &Mod) {
1349 WriteBitcodeToFile(*M, *OS, CGOpts.EmitLLVMUseLists);
1358 thinBackend(Conf, -1, AddStream, *M, *CombinedIndex, ImportList,
1359 ModuleToDefinedGVSummaries[M->getModuleIdentifier()],
1360 nullptr, Conf.CodeGenOnly,
1361 nullptr, CGOpts.CmdArgs)) {
1362 handleAllErrors(std::move(
E), [&](ErrorInfoBase &EIB) {
1363 errs() <<
"Error running ThinLTO backend: " << EIB.message() <<
'\n';
1369 StringRef TDesc, llvm::Module *M,
1372 std::unique_ptr<raw_pwrite_stream> OS,
1374 llvm::TimeTraceScope TimeScope(
"Backend");
1377 std::unique_ptr<llvm::Module> EmptyModule;
1382 std::unique_ptr<ModuleSummaryIndex> CombinedIndex;
1383 if (Error
E = llvm::getModuleSummaryIndexForFile(
1386 .moveInto(CombinedIndex)) {
1387 logAllUnhandledErrors(std::move(
E), errs(),
1388 "Error loading index file '" +
1396 if (CombinedIndex) {
1397 if (!CombinedIndex->skipModuleByDistributedBackend()) {
1409 EmptyModule = std::make_unique<llvm::Module>(
"empty", M->getContext());
1410 EmptyModule->setTargetTriple(M->getTargetTriple());
1411 M = EmptyModule.get();
1415 EmitAssemblyHelper AsmHelper(CI, CGOpts, M, VFS);
1416 AsmHelper.emitAssembly(Action, std::move(OS), BC);
1421 std::string DLDesc = M->getDataLayout().getStringRepresentation();
1422 if (DLDesc != TDesc) {
1425 "expected target description '%1'");
1426 Diags.
Report(DiagID) << DLDesc << TDesc;
1434 llvm::MemoryBufferRef Buf) {
1437 llvm::embedBitcodeInModule(
1449 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> ObjectOrErr =
1450 llvm::MemoryBuffer::getFileOrSTDIN(OffloadObject);
1451 if (ObjectOrErr.getError()) {
1453 "could not open '%0' for embedding");
1454 Diags.
Report(DiagID) << OffloadObject;
1458 llvm::embedBufferInModule(*M, **ObjectOrErr,
".llvm.offloading",
1459 Align(object::OffloadBinary::getAlignment()));
static bool actionRequiresCodeGen(BackendAction Action)
static void addSanitizers(const Triple &TargetTriple, const CodeGenOptions &CodeGenOpts, const LangOptions &LangOpts, PassBuilder &PB)
static std::optional< llvm::CodeModel::Model > getCodeModel(const CodeGenOptions &CodeGenOpts)
static void runThinLTOBackend(CompilerInstance &CI, ModuleSummaryIndex *CombinedIndex, llvm::Module *M, std::unique_ptr< raw_pwrite_stream > OS, std::string SampleProfile, std::string ProfileRemapping, BackendAction Action)
static SanitizerBinaryMetadataOptions getSanitizerBinaryMetadataOptions(const CodeGenOptions &CGOpts)
static std::optional< GCOVOptions > getGCOVOptions(const CodeGenOptions &CodeGenOpts, const LangOptions &LangOpts)
static bool initTargetOptions(const CompilerInstance &CI, DiagnosticsEngine &Diags, llvm::TargetOptions &Options)
static void addKCFIPass(const Triple &TargetTriple, const LangOptions &LangOpts, PassBuilder &PB)
static SanitizerCoverageOptions getSancovOptsFromCGOpts(const CodeGenOptions &CGOpts)
static OptimizationLevel mapToLevel(const CodeGenOptions &Opts)
static std::optional< InstrProfOptions > getInstrProfOptions(const CodeGenOptions &CodeGenOpts, const LangOptions &LangOpts)
static bool asanUseGlobalsGC(const Triple &T, const CodeGenOptions &CGOpts)
static void setCommandLineOpts(const CodeGenOptions &CodeGenOpts)
static CodeGenFileType getCodeGenFileType(BackendAction Action)
static std::string flattenClangCommandLine(ArrayRef< std::string > Args, StringRef MainFilename)
Defines the Diagnostic-related interfaces.
Defines the clang::LangOptions interface.
This file provides a pass to link in Modules from a provided BackendConsumer.
static std::string toString(const clang::SanitizerSet &Sanitizers)
Produce a string containing comma-separated names of sanitizers in Sanitizers set.
static bool contains(const std::set< tok::TokenKind > &Terminators, const Token &Tok)
Defines the clang::TargetOptions class.
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
SanitizerSet SanitizeMergeHandlers
Set of sanitizer checks that can merge handlers (smaller code size at the expense of debuggability).
std::string InstrProfileOutput
Name of the profile file to use as output for -fprofile-instr-generate, -fprofile-generate,...
std::string BinutilsVersion
bool hasProfileIRUse() const
Check if IR level profile use is on.
char CoverageVersion[4]
The version string to put into coverage files.
std::string FloatABI
The ABI to use for passing floating point arguments.
std::string ThinLinkBitcodeFile
Name of a file that can optionally be written with minimized bitcode to be used as input for the Thin...
bool hasProfileCSIRInstr() const
Check if CS IR level profile instrumentation is on.
std::string DebugPass
Enable additional debugging information.
llvm::Reloc::Model RelocationModel
The name of the relocation model to use.
std::string CoverageNotesFile
The filename with path we use for coverage notes files.
std::string ProfileInstrumentUsePath
Name of the profile file to use as input for -fprofile-instr-use.
std::string SampleProfileFile
Name of the profile file to use with -fprofile-sample-use.
uint64_t LargeDataThreshold
The code model-specific large data threshold to use (-mlarge-data-threshold).
std::string MemoryProfileOutput
Name of the profile file to use as output for with -fmemory-profile.
std::vector< std::function< void(llvm::PassBuilder &)> > PassBuilderCallbacks
List of pass builder callbacks.
std::string LimitFloatPrecision
The float precision limit to use, if non-empty.
std::string CodeModel
The code model to use (-mcmodel).
std::string CoverageDataFile
The filename with path we use for coverage data files.
std::vector< std::string > PassPlugins
List of dynamic shared object files to be loaded as pass plugins.
bool hasProfileClangInstr() const
Check if Clang profile instrumenation is on.
std::string StackUsageOutput
Name of the stack usage file (i.e., .su file) if user passes -fstack-usage.
std::vector< std::string > SanitizeCoverageAllowlistFiles
Path to allowlist file specifying which objects (files, functions) should exclusively be instrumented...
std::vector< std::string > SanitizeCoverageIgnorelistFiles
Path to ignorelist file specifying which objects (files, functions) listed for instrumentation by san...
bool hasSanitizeCoverage() const
std::string MainFileName
The user provided name for the "main file", if non-empty.
bool hasProfileIRInstr() const
Check if IR level profile instrumentation is on.
bool hasProfileCSIRUse() const
Check if CSIR profile use is on.
SanitizerSet SanitizeTrap
Set of sanitizer checks that trap rather than diagnose.
std::vector< std::string > SanitizeMetadataIgnorelistFiles
Path to ignorelist file specifying which objects (files, functions) listed for instrumentation by san...
SanitizerSet SanitizeRecover
Set of sanitizer checks that are non-fatal (i.e.
std::string ProfileExcludeFiles
Regexes separated by a semi-colon to filter the files to not instrument.
std::string AsSecureLogFile
The name of a file to use with .secure_log_unique directives.
std::string ProfileRemappingFile
Name of the profile remapping file to apply to the profile data supplied by -fprofile-sample-use or -...
bool hasSanitizeBinaryMetadata() const
std::string ThinLTOIndexFile
Name of the function summary index file to use for ThinLTO function importing.
const char * Argv0
Executable and command-line used to create a given CompilerInvocation.
SanitizerMaskCutoffs SanitizeSkipHotCutoffs
Set of thresholds in a range [0.0, 1.0]: the top hottest code responsible for the given fraction of P...
std::string SplitDwarfFile
The name for the split debug info file used for the DW_AT_[GNU_]dwo_name attribute in the skeleton CU...
std::vector< uint8_t > CmdArgs
List of backend command-line options for -fembed-bitcode.
std::vector< std::string > CommandLineArgs
std::string MemoryProfileUsePath
Name of the profile file to use as input for -fmemory-profile-use.
std::vector< std::string > OffloadObjects
List of filenames passed in using the -fembed-offload-object option.
std::string ProfileFilterFiles
Regexes separated by a semi-colon to filter the files to instrument.
std::string ObjectFilenameForDebug
Output filename used in the COFF debug information.
std::string SplitDwarfOutput
Output filename for the split debug info, not used in the skeleton CU.
std::string DIBugsReportFilePath
The file to use for dumping bug report by Debugify for original debug info.
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
llvm::TimerGroup & getTimerGroup() const
llvm::Timer & getFrontendTimer() const
TargetOptions & getTargetOpts()
HeaderSearchOptions & getHeaderSearchOpts()
LangOptions & getLangOpts()
CodeGenOptions & getCodeGenOpts()
Concrete class used by the front-end to report problems and issues.
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
unsigned getCustomDiagID(Level L, const char(&FormatString)[N])
Return an ID for a diagnostic with the specified format string and level.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
bool hasWasmExceptions() const
bool hasSjLjExceptions() const
SanitizerSet Sanitize
Set of enabled sanitizers.
bool hasDWARFExceptions() const
bool hasSEHExceptions() const
std::vector< std::string > NoSanitizeFiles
Paths to files specifying which objects (files, functions, variables) should not be instrumented.
Options for controlling the target.
std::vector< std::string > Features
The list of target specific features to enable or disable – this should be a list of strings starting...
std::string ABI
If given, the name of the target ABI to use.
std::string CPU
If given, the name of the target CPU to generate code for.
llvm::EABI EABIVersion
The EABI version to use.
Create and return a pass that links in Moduels from a provided BackendConsumer to a given primary Mod...
The JSON file list parser is used to communicate input to InstallAPI.
if(T->getSizeExpr()) TRY_TO(TraverseStmt(const_cast< Expr * >(T -> getSizeExpr())))
void EmbedObject(llvm::Module *M, const CodeGenOptions &CGOpts, DiagnosticsEngine &Diags)
llvm::cl::opt< bool > ClSanitizeGuardChecks
void emitBackendOutput(CompilerInstance &CI, CodeGenOptions &CGOpts, StringRef TDesc, llvm::Module *M, BackendAction Action, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS, std::unique_ptr< raw_pwrite_stream > OS, BackendConsumer *BC=nullptr)
void EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts, llvm::MemoryBufferRef Buf)
@ Backend_EmitAssembly
Emit native assembly files.
@ Backend_EmitLL
Emit human-readable LLVM assembly.
@ Backend_EmitBC
Emit LLVM bitcode files.
@ Backend_EmitObj
Emit native object files.
@ Backend_EmitMCNull
Run CodeGen, but don't emit anything.
@ Backend_EmitNothing
Don't emit anything (benchmarking mode)
const FunctionProtoType * T
Diagnostic wrappers for TextAPI types for error reporting.
cl::opt< bool > PrintPipelinePasses
cl::opt< InstrProfCorrelator::ProfCorrelatorKind > ProfileCorrelate
static cl::opt< PGOOptions::ColdFuncOpt > ClPGOColdFuncAttr("pgo-cold-func-opt", cl::init(PGOOptions::ColdFuncOpt::Default), cl::Hidden, cl::desc("Function attribute to apply to cold functions as determined by PGO"), cl::values(clEnumValN(PGOOptions::ColdFuncOpt::Default, "default", "Default (no attribute)"), clEnumValN(PGOOptions::ColdFuncOpt::OptSize, "optsize", "Mark cold functions with optsize."), clEnumValN(PGOOptions::ColdFuncOpt::MinSize, "minsize", "Mark cold functions with minsize."), clEnumValN(PGOOptions::ColdFuncOpt::OptNone, "optnone", "Mark cold functions with optnone.")))
static cl::opt< bool > ClSanitizeOnOptimizerEarlyEP("sanitizer-early-opt-ep", cl::Optional, cl::desc("Insert sanitizers on OptimizerEarlyEP."))
bool has(SanitizerMask K) const
Check if a certain (single) sanitizer is enabled.