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