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