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