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/Passes/PassBuilder.h" 42 #include "llvm/Passes/PassPlugin.h" 43 #include "llvm/Passes/StandardInstrumentations.h" 44 #include "llvm/Support/BuryPointer.h" 45 #include "llvm/Support/CommandLine.h" 46 #include "llvm/Support/MemoryBuffer.h" 47 #include "llvm/Support/PrettyStackTrace.h" 48 #include "llvm/Support/TargetRegistry.h" 49 #include "llvm/Support/TimeProfiler.h" 50 #include "llvm/Support/Timer.h" 51 #include "llvm/Support/ToolOutputFile.h" 52 #include "llvm/Support/raw_ostream.h" 53 #include "llvm/Target/TargetMachine.h" 54 #include "llvm/Target/TargetOptions.h" 55 #include "llvm/Transforms/Coroutines.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/PassManagerBuilder.h" 64 #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h" 65 #include "llvm/Transforms/InstCombine/InstCombine.h" 66 #include "llvm/Transforms/Instrumentation.h" 67 #include "llvm/Transforms/Instrumentation/AddressSanitizer.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/EntryExitInstrumenter.h" 85 #include "llvm/Transforms/Utils/NameAnonGlobals.h" 86 #include "llvm/Transforms/Utils/SymbolRewriter.h" 87 #include "llvm/Transforms/Utils/UniqueInternalLinkageNames.h" 88 #include <memory> 89 using namespace clang; 90 using namespace llvm; 91 92 #define HANDLE_EXTENSION(Ext) \ 93 llvm::PassPluginLibraryInfo get##Ext##PluginInfo(); 94 #include "llvm/Support/Extension.def" 95 96 namespace { 97 98 // Default filename used for profile generation. 99 static constexpr StringLiteral DefaultProfileGenName = "default_%m.profraw"; 100 101 class EmitAssemblyHelper { 102 DiagnosticsEngine &Diags; 103 const HeaderSearchOptions &HSOpts; 104 const CodeGenOptions &CodeGenOpts; 105 const clang::TargetOptions &TargetOpts; 106 const LangOptions &LangOpts; 107 Module *TheModule; 108 109 Timer CodeGenerationTime; 110 111 std::unique_ptr<raw_pwrite_stream> OS; 112 113 TargetIRAnalysis getTargetIRAnalysis() const { 114 if (TM) 115 return TM->getTargetIRAnalysis(); 116 117 return TargetIRAnalysis(); 118 } 119 120 void CreatePasses(legacy::PassManager &MPM, legacy::FunctionPassManager &FPM); 121 122 /// Generates the TargetMachine. 123 /// Leaves TM unchanged if it is unable to create the target machine. 124 /// Some of our clang tests specify triples which are not built 125 /// into clang. This is okay because these tests check the generated 126 /// IR, and they require DataLayout which depends on the triple. 127 /// In this case, we allow this method to fail and not report an error. 128 /// When MustCreateTM is used, we print an error if we are unable to load 129 /// the requested target. 130 void CreateTargetMachine(bool MustCreateTM); 131 132 /// Add passes necessary to emit assembly or LLVM IR. 133 /// 134 /// \return True on success. 135 bool AddEmitPasses(legacy::PassManager &CodeGenPasses, BackendAction Action, 136 raw_pwrite_stream &OS, raw_pwrite_stream *DwoOS); 137 138 std::unique_ptr<llvm::ToolOutputFile> openOutputFile(StringRef Path) { 139 std::error_code EC; 140 auto F = std::make_unique<llvm::ToolOutputFile>(Path, EC, 141 llvm::sys::fs::OF_None); 142 if (EC) { 143 Diags.Report(diag::err_fe_unable_to_open_output) << Path << EC.message(); 144 F.reset(); 145 } 146 return F; 147 } 148 149 public: 150 EmitAssemblyHelper(DiagnosticsEngine &_Diags, 151 const HeaderSearchOptions &HeaderSearchOpts, 152 const CodeGenOptions &CGOpts, 153 const clang::TargetOptions &TOpts, 154 const LangOptions &LOpts, Module *M) 155 : Diags(_Diags), HSOpts(HeaderSearchOpts), CodeGenOpts(CGOpts), 156 TargetOpts(TOpts), LangOpts(LOpts), TheModule(M), 157 CodeGenerationTime("codegen", "Code Generation Time") {} 158 159 ~EmitAssemblyHelper() { 160 if (CodeGenOpts.DisableFree) 161 BuryPointer(std::move(TM)); 162 } 163 164 std::unique_ptr<TargetMachine> TM; 165 166 void EmitAssembly(BackendAction Action, 167 std::unique_ptr<raw_pwrite_stream> OS); 168 169 void EmitAssemblyWithNewPassManager(BackendAction Action, 170 std::unique_ptr<raw_pwrite_stream> OS); 171 }; 172 173 // We need this wrapper to access LangOpts and CGOpts from extension functions 174 // that we add to the PassManagerBuilder. 175 class PassManagerBuilderWrapper : public PassManagerBuilder { 176 public: 177 PassManagerBuilderWrapper(const Triple &TargetTriple, 178 const CodeGenOptions &CGOpts, 179 const LangOptions &LangOpts) 180 : PassManagerBuilder(), TargetTriple(TargetTriple), CGOpts(CGOpts), 181 LangOpts(LangOpts) {} 182 const Triple &getTargetTriple() const { return TargetTriple; } 183 const CodeGenOptions &getCGOpts() const { return CGOpts; } 184 const LangOptions &getLangOpts() const { return LangOpts; } 185 186 private: 187 const Triple &TargetTriple; 188 const CodeGenOptions &CGOpts; 189 const LangOptions &LangOpts; 190 }; 191 } 192 193 static void addObjCARCAPElimPass(const PassManagerBuilder &Builder, PassManagerBase &PM) { 194 if (Builder.OptLevel > 0) 195 PM.add(createObjCARCAPElimPass()); 196 } 197 198 static void addObjCARCExpandPass(const PassManagerBuilder &Builder, PassManagerBase &PM) { 199 if (Builder.OptLevel > 0) 200 PM.add(createObjCARCExpandPass()); 201 } 202 203 static void addObjCARCOptPass(const PassManagerBuilder &Builder, PassManagerBase &PM) { 204 if (Builder.OptLevel > 0) 205 PM.add(createObjCARCOptPass()); 206 } 207 208 static void addAddDiscriminatorsPass(const PassManagerBuilder &Builder, 209 legacy::PassManagerBase &PM) { 210 PM.add(createAddDiscriminatorsPass()); 211 } 212 213 static void addBoundsCheckingPass(const PassManagerBuilder &Builder, 214 legacy::PassManagerBase &PM) { 215 PM.add(createBoundsCheckingLegacyPass()); 216 } 217 218 static SanitizerCoverageOptions 219 getSancovOptsFromCGOpts(const CodeGenOptions &CGOpts) { 220 SanitizerCoverageOptions Opts; 221 Opts.CoverageType = 222 static_cast<SanitizerCoverageOptions::Type>(CGOpts.SanitizeCoverageType); 223 Opts.IndirectCalls = CGOpts.SanitizeCoverageIndirectCalls; 224 Opts.TraceBB = CGOpts.SanitizeCoverageTraceBB; 225 Opts.TraceCmp = CGOpts.SanitizeCoverageTraceCmp; 226 Opts.TraceDiv = CGOpts.SanitizeCoverageTraceDiv; 227 Opts.TraceGep = CGOpts.SanitizeCoverageTraceGep; 228 Opts.Use8bitCounters = CGOpts.SanitizeCoverage8bitCounters; 229 Opts.TracePC = CGOpts.SanitizeCoverageTracePC; 230 Opts.TracePCGuard = CGOpts.SanitizeCoverageTracePCGuard; 231 Opts.NoPrune = CGOpts.SanitizeCoverageNoPrune; 232 Opts.Inline8bitCounters = CGOpts.SanitizeCoverageInline8bitCounters; 233 Opts.InlineBoolFlag = CGOpts.SanitizeCoverageInlineBoolFlag; 234 Opts.PCTable = CGOpts.SanitizeCoveragePCTable; 235 Opts.StackDepth = CGOpts.SanitizeCoverageStackDepth; 236 return Opts; 237 } 238 239 static void addSanitizerCoveragePass(const PassManagerBuilder &Builder, 240 legacy::PassManagerBase &PM) { 241 const PassManagerBuilderWrapper &BuilderWrapper = 242 static_cast<const PassManagerBuilderWrapper &>(Builder); 243 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts(); 244 auto Opts = getSancovOptsFromCGOpts(CGOpts); 245 PM.add(createModuleSanitizerCoverageLegacyPassPass( 246 Opts, CGOpts.SanitizeCoverageAllowlistFiles, 247 CGOpts.SanitizeCoverageBlocklistFiles)); 248 } 249 250 // Check if ASan should use GC-friendly instrumentation for globals. 251 // First of all, there is no point if -fdata-sections is off (expect for MachO, 252 // where this is not a factor). Also, on ELF this feature requires an assembler 253 // extension that only works with -integrated-as at the moment. 254 static bool asanUseGlobalsGC(const Triple &T, const CodeGenOptions &CGOpts) { 255 if (!CGOpts.SanitizeAddressGlobalsDeadStripping) 256 return false; 257 switch (T.getObjectFormat()) { 258 case Triple::MachO: 259 case Triple::COFF: 260 return true; 261 case Triple::ELF: 262 return CGOpts.DataSections && !CGOpts.DisableIntegratedAS; 263 case Triple::GOFF: 264 llvm::report_fatal_error("ASan not implemented for GOFF"); 265 case Triple::XCOFF: 266 llvm::report_fatal_error("ASan not implemented for XCOFF."); 267 case Triple::Wasm: 268 case Triple::UnknownObjectFormat: 269 break; 270 } 271 return false; 272 } 273 274 static void addMemProfilerPasses(const PassManagerBuilder &Builder, 275 legacy::PassManagerBase &PM) { 276 PM.add(createMemProfilerFunctionPass()); 277 PM.add(createModuleMemProfilerLegacyPassPass()); 278 } 279 280 static void addAddressSanitizerPasses(const PassManagerBuilder &Builder, 281 legacy::PassManagerBase &PM) { 282 const PassManagerBuilderWrapper &BuilderWrapper = 283 static_cast<const PassManagerBuilderWrapper&>(Builder); 284 const Triple &T = BuilderWrapper.getTargetTriple(); 285 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts(); 286 bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Address); 287 bool UseAfterScope = CGOpts.SanitizeAddressUseAfterScope; 288 bool UseOdrIndicator = CGOpts.SanitizeAddressUseOdrIndicator; 289 bool UseGlobalsGC = asanUseGlobalsGC(T, CGOpts); 290 PM.add(createAddressSanitizerFunctionPass(/*CompileKernel*/ false, Recover, 291 UseAfterScope)); 292 PM.add(createModuleAddressSanitizerLegacyPassPass( 293 /*CompileKernel*/ false, Recover, UseGlobalsGC, UseOdrIndicator)); 294 } 295 296 static void addKernelAddressSanitizerPasses(const PassManagerBuilder &Builder, 297 legacy::PassManagerBase &PM) { 298 PM.add(createAddressSanitizerFunctionPass( 299 /*CompileKernel*/ true, /*Recover*/ true, /*UseAfterScope*/ false)); 300 PM.add(createModuleAddressSanitizerLegacyPassPass( 301 /*CompileKernel*/ true, /*Recover*/ true, /*UseGlobalsGC*/ true, 302 /*UseOdrIndicator*/ false)); 303 } 304 305 static void addHWAddressSanitizerPasses(const PassManagerBuilder &Builder, 306 legacy::PassManagerBase &PM) { 307 const PassManagerBuilderWrapper &BuilderWrapper = 308 static_cast<const PassManagerBuilderWrapper &>(Builder); 309 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts(); 310 bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::HWAddress); 311 PM.add( 312 createHWAddressSanitizerLegacyPassPass(/*CompileKernel*/ false, Recover)); 313 } 314 315 static void addKernelHWAddressSanitizerPasses(const PassManagerBuilder &Builder, 316 legacy::PassManagerBase &PM) { 317 PM.add(createHWAddressSanitizerLegacyPassPass( 318 /*CompileKernel*/ true, /*Recover*/ true)); 319 } 320 321 static void addGeneralOptsForMemorySanitizer(const PassManagerBuilder &Builder, 322 legacy::PassManagerBase &PM, 323 bool CompileKernel) { 324 const PassManagerBuilderWrapper &BuilderWrapper = 325 static_cast<const PassManagerBuilderWrapper&>(Builder); 326 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts(); 327 int TrackOrigins = CGOpts.SanitizeMemoryTrackOrigins; 328 bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Memory); 329 PM.add(createMemorySanitizerLegacyPassPass( 330 MemorySanitizerOptions{TrackOrigins, Recover, CompileKernel})); 331 332 // MemorySanitizer inserts complex instrumentation that mostly follows 333 // the logic of the original code, but operates on "shadow" values. 334 // It can benefit from re-running some general purpose optimization passes. 335 if (Builder.OptLevel > 0) { 336 PM.add(createEarlyCSEPass()); 337 PM.add(createReassociatePass()); 338 PM.add(createLICMPass()); 339 PM.add(createGVNPass()); 340 PM.add(createInstructionCombiningPass()); 341 PM.add(createDeadStoreEliminationPass()); 342 } 343 } 344 345 static void addMemorySanitizerPass(const PassManagerBuilder &Builder, 346 legacy::PassManagerBase &PM) { 347 addGeneralOptsForMemorySanitizer(Builder, PM, /*CompileKernel*/ false); 348 } 349 350 static void addKernelMemorySanitizerPass(const PassManagerBuilder &Builder, 351 legacy::PassManagerBase &PM) { 352 addGeneralOptsForMemorySanitizer(Builder, PM, /*CompileKernel*/ true); 353 } 354 355 static void addThreadSanitizerPass(const PassManagerBuilder &Builder, 356 legacy::PassManagerBase &PM) { 357 PM.add(createThreadSanitizerLegacyPassPass()); 358 } 359 360 static void addDataFlowSanitizerPass(const PassManagerBuilder &Builder, 361 legacy::PassManagerBase &PM) { 362 const PassManagerBuilderWrapper &BuilderWrapper = 363 static_cast<const PassManagerBuilderWrapper&>(Builder); 364 const LangOptions &LangOpts = BuilderWrapper.getLangOpts(); 365 PM.add(createDataFlowSanitizerLegacyPassPass(LangOpts.NoSanitizeFiles)); 366 } 367 368 static TargetLibraryInfoImpl *createTLII(llvm::Triple &TargetTriple, 369 const CodeGenOptions &CodeGenOpts) { 370 TargetLibraryInfoImpl *TLII = new TargetLibraryInfoImpl(TargetTriple); 371 372 switch (CodeGenOpts.getVecLib()) { 373 case CodeGenOptions::Accelerate: 374 TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::Accelerate); 375 break; 376 case CodeGenOptions::LIBMVEC: 377 switch(TargetTriple.getArch()) { 378 default: 379 break; 380 case llvm::Triple::x86_64: 381 TLII->addVectorizableFunctionsFromVecLib 382 (TargetLibraryInfoImpl::LIBMVEC_X86); 383 break; 384 } 385 break; 386 case CodeGenOptions::MASSV: 387 TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::MASSV); 388 break; 389 case CodeGenOptions::SVML: 390 TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::SVML); 391 break; 392 default: 393 break; 394 } 395 return TLII; 396 } 397 398 static void addSymbolRewriterPass(const CodeGenOptions &Opts, 399 legacy::PassManager *MPM) { 400 llvm::SymbolRewriter::RewriteDescriptorList DL; 401 402 llvm::SymbolRewriter::RewriteMapParser MapParser; 403 for (const auto &MapFile : Opts.RewriteMapFiles) 404 MapParser.parse(MapFile, &DL); 405 406 MPM->add(createRewriteSymbolsPass(DL)); 407 } 408 409 static CodeGenOpt::Level getCGOptLevel(const CodeGenOptions &CodeGenOpts) { 410 switch (CodeGenOpts.OptimizationLevel) { 411 default: 412 llvm_unreachable("Invalid optimization level!"); 413 case 0: 414 return CodeGenOpt::None; 415 case 1: 416 return CodeGenOpt::Less; 417 case 2: 418 return CodeGenOpt::Default; // O2/Os/Oz 419 case 3: 420 return CodeGenOpt::Aggressive; 421 } 422 } 423 424 static Optional<llvm::CodeModel::Model> 425 getCodeModel(const CodeGenOptions &CodeGenOpts) { 426 unsigned CodeModel = llvm::StringSwitch<unsigned>(CodeGenOpts.CodeModel) 427 .Case("tiny", llvm::CodeModel::Tiny) 428 .Case("small", llvm::CodeModel::Small) 429 .Case("kernel", llvm::CodeModel::Kernel) 430 .Case("medium", llvm::CodeModel::Medium) 431 .Case("large", llvm::CodeModel::Large) 432 .Case("default", ~1u) 433 .Default(~0u); 434 assert(CodeModel != ~0u && "invalid code model!"); 435 if (CodeModel == ~1u) 436 return None; 437 return static_cast<llvm::CodeModel::Model>(CodeModel); 438 } 439 440 static CodeGenFileType getCodeGenFileType(BackendAction Action) { 441 if (Action == Backend_EmitObj) 442 return CGFT_ObjectFile; 443 else if (Action == Backend_EmitMCNull) 444 return CGFT_Null; 445 else { 446 assert(Action == Backend_EmitAssembly && "Invalid action!"); 447 return CGFT_AssemblyFile; 448 } 449 } 450 451 static bool initTargetOptions(DiagnosticsEngine &Diags, 452 llvm::TargetOptions &Options, 453 const CodeGenOptions &CodeGenOpts, 454 const clang::TargetOptions &TargetOpts, 455 const LangOptions &LangOpts, 456 const HeaderSearchOptions &HSOpts) { 457 switch (LangOpts.getThreadModel()) { 458 case LangOptions::ThreadModelKind::POSIX: 459 Options.ThreadModel = llvm::ThreadModel::POSIX; 460 break; 461 case LangOptions::ThreadModelKind::Single: 462 Options.ThreadModel = llvm::ThreadModel::Single; 463 break; 464 } 465 466 // Set float ABI type. 467 assert((CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp" || 468 CodeGenOpts.FloatABI == "hard" || CodeGenOpts.FloatABI.empty()) && 469 "Invalid Floating Point ABI!"); 470 Options.FloatABIType = 471 llvm::StringSwitch<llvm::FloatABI::ABIType>(CodeGenOpts.FloatABI) 472 .Case("soft", llvm::FloatABI::Soft) 473 .Case("softfp", llvm::FloatABI::Soft) 474 .Case("hard", llvm::FloatABI::Hard) 475 .Default(llvm::FloatABI::Default); 476 477 // Set FP fusion mode. 478 switch (LangOpts.getDefaultFPContractMode()) { 479 case LangOptions::FPM_Off: 480 // Preserve any contraction performed by the front-end. (Strict performs 481 // splitting of the muladd intrinsic in the backend.) 482 Options.AllowFPOpFusion = llvm::FPOpFusion::Standard; 483 break; 484 case LangOptions::FPM_On: 485 case LangOptions::FPM_FastHonorPragmas: 486 Options.AllowFPOpFusion = llvm::FPOpFusion::Standard; 487 break; 488 case LangOptions::FPM_Fast: 489 Options.AllowFPOpFusion = llvm::FPOpFusion::Fast; 490 break; 491 } 492 493 Options.BinutilsVersion = 494 llvm::TargetMachine::parseBinutilsVersion(CodeGenOpts.BinutilsVersion); 495 Options.UseInitArray = CodeGenOpts.UseInitArray; 496 Options.DisableIntegratedAS = CodeGenOpts.DisableIntegratedAS; 497 Options.CompressDebugSections = CodeGenOpts.getCompressDebugSections(); 498 Options.RelaxELFRelocations = CodeGenOpts.RelaxELFRelocations; 499 500 // Set EABI version. 501 Options.EABIVersion = TargetOpts.EABIVersion; 502 503 if (LangOpts.hasSjLjExceptions()) 504 Options.ExceptionModel = llvm::ExceptionHandling::SjLj; 505 if (LangOpts.hasSEHExceptions()) 506 Options.ExceptionModel = llvm::ExceptionHandling::WinEH; 507 if (LangOpts.hasDWARFExceptions()) 508 Options.ExceptionModel = llvm::ExceptionHandling::DwarfCFI; 509 if (LangOpts.hasWasmExceptions()) 510 Options.ExceptionModel = llvm::ExceptionHandling::Wasm; 511 512 Options.NoInfsFPMath = LangOpts.NoHonorInfs; 513 Options.NoNaNsFPMath = LangOpts.NoHonorNaNs; 514 Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS; 515 Options.UnsafeFPMath = LangOpts.UnsafeFPMath; 516 Options.StackAlignmentOverride = CodeGenOpts.StackAlignment; 517 518 Options.BBSections = 519 llvm::StringSwitch<llvm::BasicBlockSection>(CodeGenOpts.BBSections) 520 .Case("all", llvm::BasicBlockSection::All) 521 .Case("labels", llvm::BasicBlockSection::Labels) 522 .StartsWith("list=", llvm::BasicBlockSection::List) 523 .Case("none", llvm::BasicBlockSection::None) 524 .Default(llvm::BasicBlockSection::None); 525 526 if (Options.BBSections == llvm::BasicBlockSection::List) { 527 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = 528 MemoryBuffer::getFile(CodeGenOpts.BBSections.substr(5)); 529 if (!MBOrErr) { 530 Diags.Report(diag::err_fe_unable_to_load_basic_block_sections_file) 531 << MBOrErr.getError().message(); 532 return false; 533 } 534 Options.BBSectionsFuncListBuf = std::move(*MBOrErr); 535 } 536 537 Options.EnableMachineFunctionSplitter = CodeGenOpts.SplitMachineFunctions; 538 Options.FunctionSections = CodeGenOpts.FunctionSections; 539 Options.DataSections = CodeGenOpts.DataSections; 540 Options.IgnoreXCOFFVisibility = CodeGenOpts.IgnoreXCOFFVisibility; 541 Options.UniqueSectionNames = CodeGenOpts.UniqueSectionNames; 542 Options.UniqueBasicBlockSectionNames = 543 CodeGenOpts.UniqueBasicBlockSectionNames; 544 Options.StackProtectorGuard = 545 llvm::StringSwitch<llvm::StackProtectorGuards>(CodeGenOpts 546 .StackProtectorGuard) 547 .Case("tls", llvm::StackProtectorGuards::TLS) 548 .Case("global", llvm::StackProtectorGuards::Global) 549 .Default(llvm::StackProtectorGuards::None); 550 Options.StackProtectorGuardOffset = CodeGenOpts.StackProtectorGuardOffset; 551 Options.StackProtectorGuardReg = CodeGenOpts.StackProtectorGuardReg; 552 Options.TLSSize = CodeGenOpts.TLSSize; 553 Options.EmulatedTLS = CodeGenOpts.EmulatedTLS; 554 Options.ExplicitEmulatedTLS = CodeGenOpts.ExplicitEmulatedTLS; 555 Options.DebuggerTuning = CodeGenOpts.getDebuggerTuning(); 556 Options.EmitStackSizeSection = CodeGenOpts.StackSizeSection; 557 Options.EmitAddrsig = CodeGenOpts.Addrsig; 558 Options.ForceDwarfFrameSection = CodeGenOpts.ForceDwarfFrameSection; 559 Options.EmitCallSiteInfo = CodeGenOpts.EmitCallSiteInfo; 560 Options.EnableAIXExtendedAltivecABI = CodeGenOpts.EnableAIXExtendedAltivecABI; 561 Options.PseudoProbeForProfiling = CodeGenOpts.PseudoProbeForProfiling; 562 Options.ValueTrackingVariableLocations = 563 CodeGenOpts.ValueTrackingVariableLocations; 564 Options.XRayOmitFunctionIndex = CodeGenOpts.XRayOmitFunctionIndex; 565 566 Options.MCOptions.SplitDwarfFile = CodeGenOpts.SplitDwarfFile; 567 Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll; 568 Options.MCOptions.MCSaveTempLabels = CodeGenOpts.SaveTempLabels; 569 Options.MCOptions.MCUseDwarfDirectory = !CodeGenOpts.NoDwarfDirectoryAsm; 570 Options.MCOptions.MCNoExecStack = CodeGenOpts.NoExecStack; 571 Options.MCOptions.MCIncrementalLinkerCompatible = 572 CodeGenOpts.IncrementalLinkerCompatible; 573 Options.MCOptions.MCFatalWarnings = CodeGenOpts.FatalWarnings; 574 Options.MCOptions.MCNoWarn = CodeGenOpts.NoWarn; 575 Options.MCOptions.AsmVerbose = CodeGenOpts.AsmVerbose; 576 Options.MCOptions.Dwarf64 = CodeGenOpts.Dwarf64; 577 Options.MCOptions.PreserveAsmComments = CodeGenOpts.PreserveAsmComments; 578 Options.MCOptions.ABIName = TargetOpts.ABI; 579 for (const auto &Entry : HSOpts.UserEntries) 580 if (!Entry.IsFramework && 581 (Entry.Group == frontend::IncludeDirGroup::Quoted || 582 Entry.Group == frontend::IncludeDirGroup::Angled || 583 Entry.Group == frontend::IncludeDirGroup::System)) 584 Options.MCOptions.IASSearchPaths.push_back( 585 Entry.IgnoreSysRoot ? Entry.Path : HSOpts.Sysroot + Entry.Path); 586 Options.MCOptions.Argv0 = CodeGenOpts.Argv0; 587 Options.MCOptions.CommandLineArgs = CodeGenOpts.CommandLineArgs; 588 589 return true; 590 } 591 592 static Optional<GCOVOptions> getGCOVOptions(const CodeGenOptions &CodeGenOpts, 593 const LangOptions &LangOpts) { 594 if (!CodeGenOpts.EmitGcovArcs && !CodeGenOpts.EmitGcovNotes) 595 return None; 596 // Not using 'GCOVOptions::getDefault' allows us to avoid exiting if 597 // LLVM's -default-gcov-version flag is set to something invalid. 598 GCOVOptions Options; 599 Options.EmitNotes = CodeGenOpts.EmitGcovNotes; 600 Options.EmitData = CodeGenOpts.EmitGcovArcs; 601 llvm::copy(CodeGenOpts.CoverageVersion, std::begin(Options.Version)); 602 Options.NoRedZone = CodeGenOpts.DisableRedZone; 603 Options.Filter = CodeGenOpts.ProfileFilterFiles; 604 Options.Exclude = CodeGenOpts.ProfileExcludeFiles; 605 Options.Atomic = CodeGenOpts.AtomicProfileUpdate; 606 return Options; 607 } 608 609 static Optional<InstrProfOptions> 610 getInstrProfOptions(const CodeGenOptions &CodeGenOpts, 611 const LangOptions &LangOpts) { 612 if (!CodeGenOpts.hasProfileClangInstr()) 613 return None; 614 InstrProfOptions Options; 615 Options.NoRedZone = CodeGenOpts.DisableRedZone; 616 Options.InstrProfileOutput = CodeGenOpts.InstrProfileOutput; 617 Options.Atomic = CodeGenOpts.AtomicProfileUpdate; 618 return Options; 619 } 620 621 void EmitAssemblyHelper::CreatePasses(legacy::PassManager &MPM, 622 legacy::FunctionPassManager &FPM) { 623 // Handle disabling of all LLVM passes, where we want to preserve the 624 // internal module before any optimization. 625 if (CodeGenOpts.DisableLLVMPasses) 626 return; 627 628 // Figure out TargetLibraryInfo. This needs to be added to MPM and FPM 629 // manually (and not via PMBuilder), since some passes (eg. InstrProfiling) 630 // are inserted before PMBuilder ones - they'd get the default-constructed 631 // TLI with an unknown target otherwise. 632 Triple TargetTriple(TheModule->getTargetTriple()); 633 std::unique_ptr<TargetLibraryInfoImpl> TLII( 634 createTLII(TargetTriple, CodeGenOpts)); 635 636 // If we reached here with a non-empty index file name, then the index file 637 // was empty and we are not performing ThinLTO backend compilation (used in 638 // testing in a distributed build environment). Drop any the type test 639 // assume sequences inserted for whole program vtables so that codegen doesn't 640 // complain. 641 if (!CodeGenOpts.ThinLTOIndexFile.empty()) 642 MPM.add(createLowerTypeTestsPass(/*ExportSummary=*/nullptr, 643 /*ImportSummary=*/nullptr, 644 /*DropTypeTests=*/true)); 645 646 PassManagerBuilderWrapper PMBuilder(TargetTriple, CodeGenOpts, LangOpts); 647 648 // At O0 and O1 we only run the always inliner which is more efficient. At 649 // higher optimization levels we run the normal inliner. 650 if (CodeGenOpts.OptimizationLevel <= 1) { 651 bool InsertLifetimeIntrinsics = ((CodeGenOpts.OptimizationLevel != 0 && 652 !CodeGenOpts.DisableLifetimeMarkers) || 653 LangOpts.Coroutines); 654 PMBuilder.Inliner = createAlwaysInlinerLegacyPass(InsertLifetimeIntrinsics); 655 } else { 656 // We do not want to inline hot callsites for SamplePGO module-summary build 657 // because profile annotation will happen again in ThinLTO backend, and we 658 // want the IR of the hot path to match the profile. 659 PMBuilder.Inliner = createFunctionInliningPass( 660 CodeGenOpts.OptimizationLevel, CodeGenOpts.OptimizeSize, 661 (!CodeGenOpts.SampleProfileFile.empty() && 662 CodeGenOpts.PrepareForThinLTO)); 663 } 664 665 PMBuilder.OptLevel = CodeGenOpts.OptimizationLevel; 666 PMBuilder.SizeLevel = CodeGenOpts.OptimizeSize; 667 PMBuilder.SLPVectorize = CodeGenOpts.VectorizeSLP; 668 PMBuilder.LoopVectorize = CodeGenOpts.VectorizeLoop; 669 // Only enable CGProfilePass when using integrated assembler, since 670 // non-integrated assemblers don't recognize .cgprofile section. 671 PMBuilder.CallGraphProfile = !CodeGenOpts.DisableIntegratedAS; 672 673 PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops; 674 // Loop interleaving in the loop vectorizer has historically been set to be 675 // enabled when loop unrolling is enabled. 676 PMBuilder.LoopsInterleaved = CodeGenOpts.UnrollLoops; 677 PMBuilder.MergeFunctions = CodeGenOpts.MergeFunctions; 678 PMBuilder.PrepareForThinLTO = CodeGenOpts.PrepareForThinLTO; 679 PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO; 680 PMBuilder.RerollLoops = CodeGenOpts.RerollLoops; 681 682 MPM.add(new TargetLibraryInfoWrapperPass(*TLII)); 683 684 if (TM) 685 TM->adjustPassManager(PMBuilder); 686 687 if (CodeGenOpts.DebugInfoForProfiling || 688 !CodeGenOpts.SampleProfileFile.empty()) 689 PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible, 690 addAddDiscriminatorsPass); 691 692 // In ObjC ARC mode, add the main ARC optimization passes. 693 if (LangOpts.ObjCAutoRefCount) { 694 PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible, 695 addObjCARCExpandPass); 696 PMBuilder.addExtension(PassManagerBuilder::EP_ModuleOptimizerEarly, 697 addObjCARCAPElimPass); 698 PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate, 699 addObjCARCOptPass); 700 } 701 702 if (LangOpts.Coroutines) 703 addCoroutinePassesToExtensionPoints(PMBuilder); 704 705 if (!CodeGenOpts.MemoryProfileOutput.empty()) { 706 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 707 addMemProfilerPasses); 708 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 709 addMemProfilerPasses); 710 } 711 712 if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds)) { 713 PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate, 714 addBoundsCheckingPass); 715 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 716 addBoundsCheckingPass); 717 } 718 719 if (CodeGenOpts.SanitizeCoverageType || 720 CodeGenOpts.SanitizeCoverageIndirectCalls || 721 CodeGenOpts.SanitizeCoverageTraceCmp) { 722 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 723 addSanitizerCoveragePass); 724 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 725 addSanitizerCoveragePass); 726 } 727 728 if (LangOpts.Sanitize.has(SanitizerKind::Address)) { 729 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 730 addAddressSanitizerPasses); 731 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 732 addAddressSanitizerPasses); 733 } 734 735 if (LangOpts.Sanitize.has(SanitizerKind::KernelAddress)) { 736 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 737 addKernelAddressSanitizerPasses); 738 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 739 addKernelAddressSanitizerPasses); 740 } 741 742 if (LangOpts.Sanitize.has(SanitizerKind::HWAddress)) { 743 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 744 addHWAddressSanitizerPasses); 745 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 746 addHWAddressSanitizerPasses); 747 } 748 749 if (LangOpts.Sanitize.has(SanitizerKind::KernelHWAddress)) { 750 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 751 addKernelHWAddressSanitizerPasses); 752 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 753 addKernelHWAddressSanitizerPasses); 754 } 755 756 if (LangOpts.Sanitize.has(SanitizerKind::Memory)) { 757 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 758 addMemorySanitizerPass); 759 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 760 addMemorySanitizerPass); 761 } 762 763 if (LangOpts.Sanitize.has(SanitizerKind::KernelMemory)) { 764 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 765 addKernelMemorySanitizerPass); 766 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 767 addKernelMemorySanitizerPass); 768 } 769 770 if (LangOpts.Sanitize.has(SanitizerKind::Thread)) { 771 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 772 addThreadSanitizerPass); 773 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 774 addThreadSanitizerPass); 775 } 776 777 if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) { 778 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 779 addDataFlowSanitizerPass); 780 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 781 addDataFlowSanitizerPass); 782 } 783 784 // Set up the per-function pass manager. 785 FPM.add(new TargetLibraryInfoWrapperPass(*TLII)); 786 if (CodeGenOpts.VerifyModule) 787 FPM.add(createVerifierPass()); 788 789 // Set up the per-module pass manager. 790 if (!CodeGenOpts.RewriteMapFiles.empty()) 791 addSymbolRewriterPass(CodeGenOpts, &MPM); 792 793 // Add UniqueInternalLinkageNames Pass which renames internal linkage symbols 794 // with unique names. 795 if (CodeGenOpts.UniqueInternalLinkageNames) { 796 MPM.add(createUniqueInternalLinkageNamesPass()); 797 } 798 799 if (Optional<GCOVOptions> Options = getGCOVOptions(CodeGenOpts, LangOpts)) { 800 MPM.add(createGCOVProfilerPass(*Options)); 801 if (CodeGenOpts.getDebugInfo() == codegenoptions::NoDebugInfo) 802 MPM.add(createStripSymbolsPass(true)); 803 } 804 805 if (Optional<InstrProfOptions> Options = 806 getInstrProfOptions(CodeGenOpts, LangOpts)) 807 MPM.add(createInstrProfilingLegacyPass(*Options, false)); 808 809 bool hasIRInstr = false; 810 if (CodeGenOpts.hasProfileIRInstr()) { 811 PMBuilder.EnablePGOInstrGen = true; 812 hasIRInstr = true; 813 } 814 if (CodeGenOpts.hasProfileCSIRInstr()) { 815 assert(!CodeGenOpts.hasProfileCSIRUse() && 816 "Cannot have both CSProfileUse pass and CSProfileGen pass at the " 817 "same time"); 818 assert(!hasIRInstr && 819 "Cannot have both ProfileGen pass and CSProfileGen pass at the " 820 "same time"); 821 PMBuilder.EnablePGOCSInstrGen = true; 822 hasIRInstr = true; 823 } 824 if (hasIRInstr) { 825 if (!CodeGenOpts.InstrProfileOutput.empty()) 826 PMBuilder.PGOInstrGen = CodeGenOpts.InstrProfileOutput; 827 else 828 PMBuilder.PGOInstrGen = std::string(DefaultProfileGenName); 829 } 830 if (CodeGenOpts.hasProfileIRUse()) { 831 PMBuilder.PGOInstrUse = CodeGenOpts.ProfileInstrumentUsePath; 832 PMBuilder.EnablePGOCSInstrUse = CodeGenOpts.hasProfileCSIRUse(); 833 } 834 835 if (!CodeGenOpts.SampleProfileFile.empty()) 836 PMBuilder.PGOSampleUse = CodeGenOpts.SampleProfileFile; 837 838 PMBuilder.populateFunctionPassManager(FPM); 839 PMBuilder.populateModulePassManager(MPM); 840 } 841 842 static void setCommandLineOpts(const CodeGenOptions &CodeGenOpts) { 843 SmallVector<const char *, 16> BackendArgs; 844 BackendArgs.push_back("clang"); // Fake program name. 845 if (!CodeGenOpts.DebugPass.empty()) { 846 BackendArgs.push_back("-debug-pass"); 847 BackendArgs.push_back(CodeGenOpts.DebugPass.c_str()); 848 } 849 if (!CodeGenOpts.LimitFloatPrecision.empty()) { 850 BackendArgs.push_back("-limit-float-precision"); 851 BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str()); 852 } 853 BackendArgs.push_back(nullptr); 854 llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1, 855 BackendArgs.data()); 856 } 857 858 void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) { 859 // Create the TargetMachine for generating code. 860 std::string Error; 861 std::string Triple = TheModule->getTargetTriple(); 862 const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error); 863 if (!TheTarget) { 864 if (MustCreateTM) 865 Diags.Report(diag::err_fe_unable_to_create_target) << Error; 866 return; 867 } 868 869 Optional<llvm::CodeModel::Model> CM = getCodeModel(CodeGenOpts); 870 std::string FeaturesStr = 871 llvm::join(TargetOpts.Features.begin(), TargetOpts.Features.end(), ","); 872 llvm::Reloc::Model RM = CodeGenOpts.RelocationModel; 873 CodeGenOpt::Level OptLevel = getCGOptLevel(CodeGenOpts); 874 875 llvm::TargetOptions Options; 876 if (!initTargetOptions(Diags, Options, CodeGenOpts, TargetOpts, LangOpts, 877 HSOpts)) 878 return; 879 TM.reset(TheTarget->createTargetMachine(Triple, TargetOpts.CPU, FeaturesStr, 880 Options, RM, CM, OptLevel)); 881 } 882 883 bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager &CodeGenPasses, 884 BackendAction Action, 885 raw_pwrite_stream &OS, 886 raw_pwrite_stream *DwoOS) { 887 // Add LibraryInfo. 888 llvm::Triple TargetTriple(TheModule->getTargetTriple()); 889 std::unique_ptr<TargetLibraryInfoImpl> TLII( 890 createTLII(TargetTriple, CodeGenOpts)); 891 CodeGenPasses.add(new TargetLibraryInfoWrapperPass(*TLII)); 892 893 // Normal mode, emit a .s or .o file by running the code generator. Note, 894 // this also adds codegenerator level optimization passes. 895 CodeGenFileType CGFT = getCodeGenFileType(Action); 896 897 // Add ObjC ARC final-cleanup optimizations. This is done as part of the 898 // "codegen" passes so that it isn't run multiple times when there is 899 // inlining happening. 900 if (CodeGenOpts.OptimizationLevel > 0) 901 CodeGenPasses.add(createObjCARCContractPass()); 902 903 if (TM->addPassesToEmitFile(CodeGenPasses, OS, DwoOS, CGFT, 904 /*DisableVerify=*/!CodeGenOpts.VerifyModule)) { 905 Diags.Report(diag::err_fe_unable_to_interface_with_target); 906 return false; 907 } 908 909 return true; 910 } 911 912 void EmitAssemblyHelper::EmitAssembly(BackendAction Action, 913 std::unique_ptr<raw_pwrite_stream> OS) { 914 TimeRegion Region(CodeGenOpts.TimePasses ? &CodeGenerationTime : nullptr); 915 916 setCommandLineOpts(CodeGenOpts); 917 918 bool UsesCodeGen = (Action != Backend_EmitNothing && 919 Action != Backend_EmitBC && 920 Action != Backend_EmitLL); 921 CreateTargetMachine(UsesCodeGen); 922 923 if (UsesCodeGen && !TM) 924 return; 925 if (TM) 926 TheModule->setDataLayout(TM->createDataLayout()); 927 928 legacy::PassManager PerModulePasses; 929 PerModulePasses.add( 930 createTargetTransformInfoWrapperPass(getTargetIRAnalysis())); 931 932 legacy::FunctionPassManager PerFunctionPasses(TheModule); 933 PerFunctionPasses.add( 934 createTargetTransformInfoWrapperPass(getTargetIRAnalysis())); 935 936 CreatePasses(PerModulePasses, PerFunctionPasses); 937 938 legacy::PassManager CodeGenPasses; 939 CodeGenPasses.add( 940 createTargetTransformInfoWrapperPass(getTargetIRAnalysis())); 941 942 std::unique_ptr<llvm::ToolOutputFile> ThinLinkOS, DwoOS; 943 944 switch (Action) { 945 case Backend_EmitNothing: 946 break; 947 948 case Backend_EmitBC: 949 if (CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.DisableLLVMPasses) { 950 if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) { 951 ThinLinkOS = openOutputFile(CodeGenOpts.ThinLinkBitcodeFile); 952 if (!ThinLinkOS) 953 return; 954 } 955 TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit", 956 CodeGenOpts.EnableSplitLTOUnit); 957 PerModulePasses.add(createWriteThinLTOBitcodePass( 958 *OS, ThinLinkOS ? &ThinLinkOS->os() : nullptr)); 959 } else { 960 // Emit a module summary by default for Regular LTO except for ld64 961 // targets 962 bool EmitLTOSummary = 963 (CodeGenOpts.PrepareForLTO && 964 !CodeGenOpts.DisableLLVMPasses && 965 llvm::Triple(TheModule->getTargetTriple()).getVendor() != 966 llvm::Triple::Apple); 967 if (EmitLTOSummary) { 968 if (!TheModule->getModuleFlag("ThinLTO")) 969 TheModule->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0)); 970 TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit", 971 uint32_t(1)); 972 } 973 974 PerModulePasses.add(createBitcodeWriterPass( 975 *OS, CodeGenOpts.EmitLLVMUseLists, EmitLTOSummary)); 976 } 977 break; 978 979 case Backend_EmitLL: 980 PerModulePasses.add( 981 createPrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists)); 982 break; 983 984 default: 985 if (!CodeGenOpts.SplitDwarfOutput.empty()) { 986 DwoOS = openOutputFile(CodeGenOpts.SplitDwarfOutput); 987 if (!DwoOS) 988 return; 989 } 990 if (!AddEmitPasses(CodeGenPasses, Action, *OS, 991 DwoOS ? &DwoOS->os() : nullptr)) 992 return; 993 } 994 995 // Before executing passes, print the final values of the LLVM options. 996 cl::PrintOptionValues(); 997 998 // Run passes. For now we do all passes at once, but eventually we 999 // would like to have the option of streaming code generation. 1000 1001 { 1002 PrettyStackTraceString CrashInfo("Per-function optimization"); 1003 llvm::TimeTraceScope TimeScope("PerFunctionPasses"); 1004 1005 PerFunctionPasses.doInitialization(); 1006 for (Function &F : *TheModule) 1007 if (!F.isDeclaration()) 1008 PerFunctionPasses.run(F); 1009 PerFunctionPasses.doFinalization(); 1010 } 1011 1012 { 1013 PrettyStackTraceString CrashInfo("Per-module optimization passes"); 1014 llvm::TimeTraceScope TimeScope("PerModulePasses"); 1015 PerModulePasses.run(*TheModule); 1016 } 1017 1018 { 1019 PrettyStackTraceString CrashInfo("Code generation"); 1020 llvm::TimeTraceScope TimeScope("CodeGenPasses"); 1021 CodeGenPasses.run(*TheModule); 1022 } 1023 1024 if (ThinLinkOS) 1025 ThinLinkOS->keep(); 1026 if (DwoOS) 1027 DwoOS->keep(); 1028 } 1029 1030 static PassBuilder::OptimizationLevel mapToLevel(const CodeGenOptions &Opts) { 1031 switch (Opts.OptimizationLevel) { 1032 default: 1033 llvm_unreachable("Invalid optimization level!"); 1034 1035 case 0: 1036 return PassBuilder::OptimizationLevel::O0; 1037 1038 case 1: 1039 return PassBuilder::OptimizationLevel::O1; 1040 1041 case 2: 1042 switch (Opts.OptimizeSize) { 1043 default: 1044 llvm_unreachable("Invalid optimization level for size!"); 1045 1046 case 0: 1047 return PassBuilder::OptimizationLevel::O2; 1048 1049 case 1: 1050 return PassBuilder::OptimizationLevel::Os; 1051 1052 case 2: 1053 return PassBuilder::OptimizationLevel::Oz; 1054 } 1055 1056 case 3: 1057 return PassBuilder::OptimizationLevel::O3; 1058 } 1059 } 1060 1061 static void addSanitizers(const Triple &TargetTriple, 1062 const CodeGenOptions &CodeGenOpts, 1063 const LangOptions &LangOpts, PassBuilder &PB) { 1064 PB.registerOptimizerLastEPCallback([&](ModulePassManager &MPM, 1065 PassBuilder::OptimizationLevel Level) { 1066 if (CodeGenOpts.SanitizeCoverageType || 1067 CodeGenOpts.SanitizeCoverageIndirectCalls || 1068 CodeGenOpts.SanitizeCoverageTraceCmp) { 1069 auto SancovOpts = getSancovOptsFromCGOpts(CodeGenOpts); 1070 MPM.addPass(ModuleSanitizerCoveragePass( 1071 SancovOpts, CodeGenOpts.SanitizeCoverageAllowlistFiles, 1072 CodeGenOpts.SanitizeCoverageBlocklistFiles)); 1073 } 1074 1075 auto MSanPass = [&](SanitizerMask Mask, bool CompileKernel) { 1076 if (LangOpts.Sanitize.has(Mask)) { 1077 int TrackOrigins = CodeGenOpts.SanitizeMemoryTrackOrigins; 1078 bool Recover = CodeGenOpts.SanitizeRecover.has(Mask); 1079 1080 MPM.addPass( 1081 MemorySanitizerPass({TrackOrigins, Recover, CompileKernel})); 1082 FunctionPassManager FPM(CodeGenOpts.DebugPassManager); 1083 FPM.addPass( 1084 MemorySanitizerPass({TrackOrigins, Recover, CompileKernel})); 1085 if (Level != PassBuilder::OptimizationLevel::O0) { 1086 // MemorySanitizer inserts complex instrumentation that mostly 1087 // follows the logic of the original code, but operates on 1088 // "shadow" values. It can benefit from re-running some 1089 // general purpose optimization passes. 1090 FPM.addPass(EarlyCSEPass()); 1091 // TODO: Consider add more passes like in 1092 // addGeneralOptsForMemorySanitizer. EarlyCSEPass makes visible 1093 // difference on size. It's not clear if the rest is still 1094 // usefull. InstCombinePass breakes 1095 // compiler-rt/test/msan/select_origin.cpp. 1096 } 1097 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); 1098 } 1099 }; 1100 MSanPass(SanitizerKind::Memory, false); 1101 MSanPass(SanitizerKind::KernelMemory, true); 1102 1103 if (LangOpts.Sanitize.has(SanitizerKind::Thread)) { 1104 MPM.addPass(ThreadSanitizerPass()); 1105 MPM.addPass(createModuleToFunctionPassAdaptor(ThreadSanitizerPass())); 1106 } 1107 1108 auto ASanPass = [&](SanitizerMask Mask, bool CompileKernel) { 1109 if (LangOpts.Sanitize.has(Mask)) { 1110 bool Recover = CodeGenOpts.SanitizeRecover.has(Mask); 1111 bool UseAfterScope = CodeGenOpts.SanitizeAddressUseAfterScope; 1112 bool ModuleUseAfterScope = asanUseGlobalsGC(TargetTriple, CodeGenOpts); 1113 bool UseOdrIndicator = CodeGenOpts.SanitizeAddressUseOdrIndicator; 1114 MPM.addPass(RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>()); 1115 MPM.addPass(ModuleAddressSanitizerPass( 1116 CompileKernel, Recover, ModuleUseAfterScope, UseOdrIndicator)); 1117 MPM.addPass(createModuleToFunctionPassAdaptor( 1118 AddressSanitizerPass(CompileKernel, Recover, UseAfterScope))); 1119 } 1120 }; 1121 ASanPass(SanitizerKind::Address, false); 1122 ASanPass(SanitizerKind::KernelAddress, true); 1123 1124 auto HWASanPass = [&](SanitizerMask Mask, bool CompileKernel) { 1125 if (LangOpts.Sanitize.has(Mask)) { 1126 bool Recover = CodeGenOpts.SanitizeRecover.has(Mask); 1127 MPM.addPass(HWAddressSanitizerPass(CompileKernel, Recover)); 1128 } 1129 }; 1130 HWASanPass(SanitizerKind::HWAddress, false); 1131 HWASanPass(SanitizerKind::KernelHWAddress, true); 1132 1133 if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) { 1134 MPM.addPass(DataFlowSanitizerPass(LangOpts.NoSanitizeFiles)); 1135 } 1136 }); 1137 } 1138 1139 /// A clean version of `EmitAssembly` that uses the new pass manager. 1140 /// 1141 /// Not all features are currently supported in this system, but where 1142 /// necessary it falls back to the legacy pass manager to at least provide 1143 /// basic functionality. 1144 /// 1145 /// This API is planned to have its functionality finished and then to replace 1146 /// `EmitAssembly` at some point in the future when the default switches. 1147 void EmitAssemblyHelper::EmitAssemblyWithNewPassManager( 1148 BackendAction Action, std::unique_ptr<raw_pwrite_stream> OS) { 1149 TimeRegion Region(CodeGenOpts.TimePasses ? &CodeGenerationTime : nullptr); 1150 setCommandLineOpts(CodeGenOpts); 1151 1152 bool RequiresCodeGen = (Action != Backend_EmitNothing && 1153 Action != Backend_EmitBC && 1154 Action != Backend_EmitLL); 1155 CreateTargetMachine(RequiresCodeGen); 1156 1157 if (RequiresCodeGen && !TM) 1158 return; 1159 if (TM) 1160 TheModule->setDataLayout(TM->createDataLayout()); 1161 1162 Optional<PGOOptions> PGOOpt; 1163 1164 if (CodeGenOpts.hasProfileIRInstr()) 1165 // -fprofile-generate. 1166 PGOOpt = PGOOptions(CodeGenOpts.InstrProfileOutput.empty() 1167 ? std::string(DefaultProfileGenName) 1168 : CodeGenOpts.InstrProfileOutput, 1169 "", "", PGOOptions::IRInstr, PGOOptions::NoCSAction, 1170 CodeGenOpts.DebugInfoForProfiling); 1171 else if (CodeGenOpts.hasProfileIRUse()) { 1172 // -fprofile-use. 1173 auto CSAction = CodeGenOpts.hasProfileCSIRUse() ? PGOOptions::CSIRUse 1174 : PGOOptions::NoCSAction; 1175 PGOOpt = PGOOptions(CodeGenOpts.ProfileInstrumentUsePath, "", 1176 CodeGenOpts.ProfileRemappingFile, PGOOptions::IRUse, 1177 CSAction, CodeGenOpts.DebugInfoForProfiling); 1178 } else if (!CodeGenOpts.SampleProfileFile.empty()) 1179 // -fprofile-sample-use 1180 PGOOpt = PGOOptions( 1181 CodeGenOpts.SampleProfileFile, "", CodeGenOpts.ProfileRemappingFile, 1182 PGOOptions::SampleUse, PGOOptions::NoCSAction, 1183 CodeGenOpts.DebugInfoForProfiling, CodeGenOpts.PseudoProbeForProfiling); 1184 else if (CodeGenOpts.PseudoProbeForProfiling) 1185 // -fpseudo-probe-for-profiling 1186 PGOOpt = 1187 PGOOptions("", "", "", PGOOptions::NoAction, PGOOptions::NoCSAction, 1188 CodeGenOpts.DebugInfoForProfiling, true); 1189 else if (CodeGenOpts.DebugInfoForProfiling) 1190 // -fdebug-info-for-profiling 1191 PGOOpt = PGOOptions("", "", "", PGOOptions::NoAction, 1192 PGOOptions::NoCSAction, true); 1193 1194 // Check to see if we want to generate a CS profile. 1195 if (CodeGenOpts.hasProfileCSIRInstr()) { 1196 assert(!CodeGenOpts.hasProfileCSIRUse() && 1197 "Cannot have both CSProfileUse pass and CSProfileGen pass at " 1198 "the same time"); 1199 if (PGOOpt.hasValue()) { 1200 assert(PGOOpt->Action != PGOOptions::IRInstr && 1201 PGOOpt->Action != PGOOptions::SampleUse && 1202 "Cannot run CSProfileGen pass with ProfileGen or SampleUse " 1203 " pass"); 1204 PGOOpt->CSProfileGenFile = CodeGenOpts.InstrProfileOutput.empty() 1205 ? std::string(DefaultProfileGenName) 1206 : CodeGenOpts.InstrProfileOutput; 1207 PGOOpt->CSAction = PGOOptions::CSIRInstr; 1208 } else 1209 PGOOpt = PGOOptions("", 1210 CodeGenOpts.InstrProfileOutput.empty() 1211 ? std::string(DefaultProfileGenName) 1212 : CodeGenOpts.InstrProfileOutput, 1213 "", PGOOptions::NoAction, PGOOptions::CSIRInstr, 1214 CodeGenOpts.DebugInfoForProfiling); 1215 } 1216 1217 PipelineTuningOptions PTO; 1218 PTO.LoopUnrolling = CodeGenOpts.UnrollLoops; 1219 // For historical reasons, loop interleaving is set to mirror setting for loop 1220 // unrolling. 1221 PTO.LoopInterleaving = CodeGenOpts.UnrollLoops; 1222 PTO.LoopVectorization = CodeGenOpts.VectorizeLoop; 1223 PTO.SLPVectorization = CodeGenOpts.VectorizeSLP; 1224 PTO.MergeFunctions = CodeGenOpts.MergeFunctions; 1225 // Only enable CGProfilePass when using integrated assembler, since 1226 // non-integrated assemblers don't recognize .cgprofile section. 1227 PTO.CallGraphProfile = !CodeGenOpts.DisableIntegratedAS; 1228 PTO.Coroutines = LangOpts.Coroutines; 1229 PTO.UniqueLinkageNames = CodeGenOpts.UniqueInternalLinkageNames; 1230 1231 PassInstrumentationCallbacks PIC; 1232 StandardInstrumentations SI(CodeGenOpts.DebugPassManager); 1233 SI.registerCallbacks(PIC); 1234 PassBuilder PB(CodeGenOpts.DebugPassManager, TM.get(), PTO, PGOOpt, &PIC); 1235 1236 // Attempt to load pass plugins and register their callbacks with PB. 1237 for (auto &PluginFN : CodeGenOpts.PassPlugins) { 1238 auto PassPlugin = PassPlugin::Load(PluginFN); 1239 if (PassPlugin) { 1240 PassPlugin->registerPassBuilderCallbacks(PB); 1241 } else { 1242 Diags.Report(diag::err_fe_unable_to_load_plugin) 1243 << PluginFN << toString(PassPlugin.takeError()); 1244 } 1245 } 1246 #define HANDLE_EXTENSION(Ext) \ 1247 get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB); 1248 #include "llvm/Support/Extension.def" 1249 1250 LoopAnalysisManager LAM(CodeGenOpts.DebugPassManager); 1251 FunctionAnalysisManager FAM(CodeGenOpts.DebugPassManager); 1252 CGSCCAnalysisManager CGAM(CodeGenOpts.DebugPassManager); 1253 ModuleAnalysisManager MAM(CodeGenOpts.DebugPassManager); 1254 1255 // Register the AA manager first so that our version is the one used. 1256 FAM.registerPass([&] { return PB.buildDefaultAAPipeline(); }); 1257 1258 // Register the target library analysis directly and give it a customized 1259 // preset TLI. 1260 Triple TargetTriple(TheModule->getTargetTriple()); 1261 std::unique_ptr<TargetLibraryInfoImpl> TLII( 1262 createTLII(TargetTriple, CodeGenOpts)); 1263 FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); }); 1264 1265 // Register all the basic analyses with the managers. 1266 PB.registerModuleAnalyses(MAM); 1267 PB.registerCGSCCAnalyses(CGAM); 1268 PB.registerFunctionAnalyses(FAM); 1269 PB.registerLoopAnalyses(LAM); 1270 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); 1271 1272 ModulePassManager MPM(CodeGenOpts.DebugPassManager); 1273 1274 if (!CodeGenOpts.DisableLLVMPasses) { 1275 // Map our optimization levels into one of the distinct levels used to 1276 // configure the pipeline. 1277 PassBuilder::OptimizationLevel Level = mapToLevel(CodeGenOpts); 1278 1279 bool IsThinLTO = CodeGenOpts.PrepareForThinLTO; 1280 bool IsLTO = CodeGenOpts.PrepareForLTO; 1281 1282 if (LangOpts.ObjCAutoRefCount) { 1283 PB.registerPipelineStartEPCallback( 1284 [](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) { 1285 if (Level != PassBuilder::OptimizationLevel::O0) 1286 MPM.addPass( 1287 createModuleToFunctionPassAdaptor(ObjCARCExpandPass())); 1288 }); 1289 PB.registerPipelineEarlySimplificationEPCallback( 1290 [](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) { 1291 if (Level != PassBuilder::OptimizationLevel::O0) 1292 MPM.addPass(ObjCARCAPElimPass()); 1293 }); 1294 PB.registerScalarOptimizerLateEPCallback( 1295 [](FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) { 1296 if (Level != PassBuilder::OptimizationLevel::O0) 1297 FPM.addPass(ObjCARCOptPass()); 1298 }); 1299 } 1300 1301 // If we reached here with a non-empty index file name, then the index 1302 // file was empty and we are not performing ThinLTO backend compilation 1303 // (used in testing in a distributed build environment). Drop any the type 1304 // test assume sequences inserted for whole program vtables so that 1305 // codegen doesn't complain. 1306 if (!CodeGenOpts.ThinLTOIndexFile.empty()) 1307 PB.registerPipelineStartEPCallback( 1308 [](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) { 1309 MPM.addPass(LowerTypeTestsPass(/*ExportSummary=*/nullptr, 1310 /*ImportSummary=*/nullptr, 1311 /*DropTypeTests=*/true)); 1312 }); 1313 1314 if (Level != PassBuilder::OptimizationLevel::O0) { 1315 PB.registerPipelineStartEPCallback( 1316 [](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) { 1317 MPM.addPass(createModuleToFunctionPassAdaptor( 1318 EntryExitInstrumenterPass(/*PostInlining=*/false))); 1319 }); 1320 } 1321 1322 // Register callbacks to schedule sanitizer passes at the appropriate part 1323 // of the pipeline. 1324 if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds)) 1325 PB.registerScalarOptimizerLateEPCallback( 1326 [](FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) { 1327 FPM.addPass(BoundsCheckingPass()); 1328 }); 1329 1330 addSanitizers(TargetTriple, CodeGenOpts, LangOpts, PB); 1331 1332 if (Optional<GCOVOptions> Options = getGCOVOptions(CodeGenOpts, LangOpts)) 1333 PB.registerPipelineStartEPCallback( 1334 [Options](ModulePassManager &MPM, 1335 PassBuilder::OptimizationLevel Level) { 1336 MPM.addPass(GCOVProfilerPass(*Options)); 1337 }); 1338 if (Optional<InstrProfOptions> Options = 1339 getInstrProfOptions(CodeGenOpts, LangOpts)) 1340 PB.registerPipelineStartEPCallback( 1341 [Options](ModulePassManager &MPM, 1342 PassBuilder::OptimizationLevel Level) { 1343 MPM.addPass(InstrProfiling(*Options, false)); 1344 }); 1345 1346 if (CodeGenOpts.OptimizationLevel == 0) { 1347 MPM = PB.buildO0DefaultPipeline(Level, IsLTO || IsThinLTO); 1348 } else if (IsThinLTO) { 1349 MPM = PB.buildThinLTOPreLinkDefaultPipeline(Level); 1350 } else if (IsLTO) { 1351 MPM = PB.buildLTOPreLinkDefaultPipeline(Level); 1352 } else { 1353 MPM = PB.buildPerModuleDefaultPipeline(Level); 1354 } 1355 1356 if (!CodeGenOpts.MemoryProfileOutput.empty()) { 1357 MPM.addPass(createModuleToFunctionPassAdaptor(MemProfilerPass())); 1358 MPM.addPass(ModuleMemProfilerPass()); 1359 } 1360 } 1361 1362 // FIXME: We still use the legacy pass manager to do code generation. We 1363 // create that pass manager here and use it as needed below. 1364 legacy::PassManager CodeGenPasses; 1365 bool NeedCodeGen = false; 1366 std::unique_ptr<llvm::ToolOutputFile> ThinLinkOS, DwoOS; 1367 1368 // Append any output we need to the pass manager. 1369 switch (Action) { 1370 case Backend_EmitNothing: 1371 break; 1372 1373 case Backend_EmitBC: 1374 if (CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.DisableLLVMPasses) { 1375 if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) { 1376 ThinLinkOS = openOutputFile(CodeGenOpts.ThinLinkBitcodeFile); 1377 if (!ThinLinkOS) 1378 return; 1379 } 1380 TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit", 1381 CodeGenOpts.EnableSplitLTOUnit); 1382 MPM.addPass(ThinLTOBitcodeWriterPass(*OS, ThinLinkOS ? &ThinLinkOS->os() 1383 : nullptr)); 1384 } else { 1385 // Emit a module summary by default for Regular LTO except for ld64 1386 // targets 1387 bool EmitLTOSummary = 1388 (CodeGenOpts.PrepareForLTO && 1389 !CodeGenOpts.DisableLLVMPasses && 1390 llvm::Triple(TheModule->getTargetTriple()).getVendor() != 1391 llvm::Triple::Apple); 1392 if (EmitLTOSummary) { 1393 if (!TheModule->getModuleFlag("ThinLTO")) 1394 TheModule->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0)); 1395 TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit", 1396 uint32_t(1)); 1397 } 1398 MPM.addPass( 1399 BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists, EmitLTOSummary)); 1400 } 1401 break; 1402 1403 case Backend_EmitLL: 1404 MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists)); 1405 break; 1406 1407 case Backend_EmitAssembly: 1408 case Backend_EmitMCNull: 1409 case Backend_EmitObj: 1410 NeedCodeGen = true; 1411 CodeGenPasses.add( 1412 createTargetTransformInfoWrapperPass(getTargetIRAnalysis())); 1413 if (!CodeGenOpts.SplitDwarfOutput.empty()) { 1414 DwoOS = openOutputFile(CodeGenOpts.SplitDwarfOutput); 1415 if (!DwoOS) 1416 return; 1417 } 1418 if (!AddEmitPasses(CodeGenPasses, Action, *OS, 1419 DwoOS ? &DwoOS->os() : nullptr)) 1420 // FIXME: Should we handle this error differently? 1421 return; 1422 break; 1423 } 1424 1425 // Before executing passes, print the final values of the LLVM options. 1426 cl::PrintOptionValues(); 1427 1428 // Now that we have all of the passes ready, run them. 1429 { 1430 PrettyStackTraceString CrashInfo("Optimizer"); 1431 MPM.run(*TheModule, MAM); 1432 } 1433 1434 // Now if needed, run the legacy PM for codegen. 1435 if (NeedCodeGen) { 1436 PrettyStackTraceString CrashInfo("Code generation"); 1437 CodeGenPasses.run(*TheModule); 1438 } 1439 1440 if (ThinLinkOS) 1441 ThinLinkOS->keep(); 1442 if (DwoOS) 1443 DwoOS->keep(); 1444 } 1445 1446 static void runThinLTOBackend( 1447 DiagnosticsEngine &Diags, ModuleSummaryIndex *CombinedIndex, Module *M, 1448 const HeaderSearchOptions &HeaderOpts, const CodeGenOptions &CGOpts, 1449 const clang::TargetOptions &TOpts, const LangOptions &LOpts, 1450 std::unique_ptr<raw_pwrite_stream> OS, std::string SampleProfile, 1451 std::string ProfileRemapping, BackendAction Action) { 1452 StringMap<DenseMap<GlobalValue::GUID, GlobalValueSummary *>> 1453 ModuleToDefinedGVSummaries; 1454 CombinedIndex->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); 1455 1456 setCommandLineOpts(CGOpts); 1457 1458 // We can simply import the values mentioned in the combined index, since 1459 // we should only invoke this using the individual indexes written out 1460 // via a WriteIndexesThinBackend. 1461 FunctionImporter::ImportMapTy ImportList; 1462 std::vector<std::unique_ptr<llvm::MemoryBuffer>> OwnedImports; 1463 MapVector<llvm::StringRef, llvm::BitcodeModule> ModuleMap; 1464 if (!lto::loadReferencedModules(*M, *CombinedIndex, ImportList, ModuleMap, 1465 OwnedImports)) 1466 return; 1467 1468 auto AddStream = [&](size_t Task) { 1469 return std::make_unique<lto::NativeObjectStream>(std::move(OS)); 1470 }; 1471 lto::Config Conf; 1472 if (CGOpts.SaveTempsFilePrefix != "") { 1473 if (Error E = Conf.addSaveTemps(CGOpts.SaveTempsFilePrefix + ".", 1474 /* UseInputModulePath */ false)) { 1475 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) { 1476 errs() << "Error setting up ThinLTO save-temps: " << EIB.message() 1477 << '\n'; 1478 }); 1479 } 1480 } 1481 Conf.CPU = TOpts.CPU; 1482 Conf.CodeModel = getCodeModel(CGOpts); 1483 Conf.MAttrs = TOpts.Features; 1484 Conf.RelocModel = CGOpts.RelocationModel; 1485 Conf.CGOptLevel = getCGOptLevel(CGOpts); 1486 Conf.OptLevel = CGOpts.OptimizationLevel; 1487 initTargetOptions(Diags, Conf.Options, CGOpts, TOpts, LOpts, HeaderOpts); 1488 Conf.SampleProfile = std::move(SampleProfile); 1489 Conf.PTO.LoopUnrolling = CGOpts.UnrollLoops; 1490 // For historical reasons, loop interleaving is set to mirror setting for loop 1491 // unrolling. 1492 Conf.PTO.LoopInterleaving = CGOpts.UnrollLoops; 1493 Conf.PTO.LoopVectorization = CGOpts.VectorizeLoop; 1494 Conf.PTO.SLPVectorization = CGOpts.VectorizeSLP; 1495 // Only enable CGProfilePass when using integrated assembler, since 1496 // non-integrated assemblers don't recognize .cgprofile section. 1497 Conf.PTO.CallGraphProfile = !CGOpts.DisableIntegratedAS; 1498 1499 // Context sensitive profile. 1500 if (CGOpts.hasProfileCSIRInstr()) { 1501 Conf.RunCSIRInstr = true; 1502 Conf.CSIRProfile = std::move(CGOpts.InstrProfileOutput); 1503 } else if (CGOpts.hasProfileCSIRUse()) { 1504 Conf.RunCSIRInstr = false; 1505 Conf.CSIRProfile = std::move(CGOpts.ProfileInstrumentUsePath); 1506 } 1507 1508 Conf.ProfileRemapping = std::move(ProfileRemapping); 1509 Conf.UseNewPM = !CGOpts.LegacyPassManager; 1510 Conf.DebugPassManager = CGOpts.DebugPassManager; 1511 Conf.RemarksWithHotness = CGOpts.DiagnosticsWithHotness; 1512 Conf.RemarksFilename = CGOpts.OptRecordFile; 1513 Conf.RemarksPasses = CGOpts.OptRecordPasses; 1514 Conf.RemarksFormat = CGOpts.OptRecordFormat; 1515 Conf.SplitDwarfFile = CGOpts.SplitDwarfFile; 1516 Conf.SplitDwarfOutput = CGOpts.SplitDwarfOutput; 1517 switch (Action) { 1518 case Backend_EmitNothing: 1519 Conf.PreCodeGenModuleHook = [](size_t Task, const Module &Mod) { 1520 return false; 1521 }; 1522 break; 1523 case Backend_EmitLL: 1524 Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) { 1525 M->print(*OS, nullptr, CGOpts.EmitLLVMUseLists); 1526 return false; 1527 }; 1528 break; 1529 case Backend_EmitBC: 1530 Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) { 1531 WriteBitcodeToFile(*M, *OS, CGOpts.EmitLLVMUseLists); 1532 return false; 1533 }; 1534 break; 1535 default: 1536 Conf.CGFileType = getCodeGenFileType(Action); 1537 break; 1538 } 1539 if (Error E = 1540 thinBackend(Conf, -1, AddStream, *M, *CombinedIndex, ImportList, 1541 ModuleToDefinedGVSummaries[M->getModuleIdentifier()], 1542 ModuleMap, CGOpts.CmdArgs)) { 1543 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) { 1544 errs() << "Error running ThinLTO backend: " << EIB.message() << '\n'; 1545 }); 1546 } 1547 } 1548 1549 void clang::EmitBackendOutput(DiagnosticsEngine &Diags, 1550 const HeaderSearchOptions &HeaderOpts, 1551 const CodeGenOptions &CGOpts, 1552 const clang::TargetOptions &TOpts, 1553 const LangOptions &LOpts, 1554 const llvm::DataLayout &TDesc, Module *M, 1555 BackendAction Action, 1556 std::unique_ptr<raw_pwrite_stream> OS) { 1557 1558 llvm::TimeTraceScope TimeScope("Backend"); 1559 1560 std::unique_ptr<llvm::Module> EmptyModule; 1561 if (!CGOpts.ThinLTOIndexFile.empty()) { 1562 // If we are performing a ThinLTO importing compile, load the function index 1563 // into memory and pass it into runThinLTOBackend, which will run the 1564 // function importer and invoke LTO passes. 1565 Expected<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr = 1566 llvm::getModuleSummaryIndexForFile(CGOpts.ThinLTOIndexFile, 1567 /*IgnoreEmptyThinLTOIndexFile*/true); 1568 if (!IndexOrErr) { 1569 logAllUnhandledErrors(IndexOrErr.takeError(), errs(), 1570 "Error loading index file '" + 1571 CGOpts.ThinLTOIndexFile + "': "); 1572 return; 1573 } 1574 std::unique_ptr<ModuleSummaryIndex> CombinedIndex = std::move(*IndexOrErr); 1575 // A null CombinedIndex means we should skip ThinLTO compilation 1576 // (LLVM will optionally ignore empty index files, returning null instead 1577 // of an error). 1578 if (CombinedIndex) { 1579 if (!CombinedIndex->skipModuleByDistributedBackend()) { 1580 runThinLTOBackend(Diags, CombinedIndex.get(), M, HeaderOpts, CGOpts, 1581 TOpts, LOpts, std::move(OS), CGOpts.SampleProfileFile, 1582 CGOpts.ProfileRemappingFile, Action); 1583 return; 1584 } 1585 // Distributed indexing detected that nothing from the module is needed 1586 // for the final linking. So we can skip the compilation. We sill need to 1587 // output an empty object file to make sure that a linker does not fail 1588 // trying to read it. Also for some features, like CFI, we must skip 1589 // the compilation as CombinedIndex does not contain all required 1590 // information. 1591 EmptyModule = std::make_unique<llvm::Module>("empty", M->getContext()); 1592 EmptyModule->setTargetTriple(M->getTargetTriple()); 1593 M = EmptyModule.get(); 1594 } 1595 } 1596 1597 EmitAssemblyHelper AsmHelper(Diags, HeaderOpts, CGOpts, TOpts, LOpts, M); 1598 1599 if (!CGOpts.LegacyPassManager) 1600 AsmHelper.EmitAssemblyWithNewPassManager(Action, std::move(OS)); 1601 else 1602 AsmHelper.EmitAssembly(Action, std::move(OS)); 1603 1604 // Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's 1605 // DataLayout. 1606 if (AsmHelper.TM) { 1607 std::string DLDesc = M->getDataLayout().getStringRepresentation(); 1608 if (DLDesc != TDesc.getStringRepresentation()) { 1609 unsigned DiagID = Diags.getCustomDiagID( 1610 DiagnosticsEngine::Error, "backend data layout '%0' does not match " 1611 "expected target description '%1'"); 1612 Diags.Report(DiagID) << DLDesc << TDesc.getStringRepresentation(); 1613 } 1614 } 1615 } 1616 1617 // With -fembed-bitcode, save a copy of the llvm IR as data in the 1618 // __LLVM,__bitcode section. 1619 void clang::EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts, 1620 llvm::MemoryBufferRef Buf) { 1621 if (CGOpts.getEmbedBitcode() == CodeGenOptions::Embed_Off) 1622 return; 1623 llvm::EmbedBitcodeInModule( 1624 *M, Buf, CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Marker, 1625 CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Bitcode, 1626 CGOpts.CmdArgs); 1627 } 1628