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