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