1 //===--- BackendUtil.cpp - LLVM Backend Utilities -------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "clang/CodeGen/BackendUtil.h" 11 #include "clang/Basic/Diagnostic.h" 12 #include "clang/Basic/LangOptions.h" 13 #include "clang/Basic/TargetOptions.h" 14 #include "clang/Frontend/CodeGenOptions.h" 15 #include "clang/Frontend/FrontendDiagnostic.h" 16 #include "clang/Frontend/Utils.h" 17 #include "clang/Lex/HeaderSearchOptions.h" 18 #include "llvm/ADT/SmallSet.h" 19 #include "llvm/ADT/StringExtras.h" 20 #include "llvm/ADT/StringSwitch.h" 21 #include "llvm/ADT/Triple.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/IR/DataLayout.h" 30 #include "llvm/IR/IRPrintingPasses.h" 31 #include "llvm/IR/LegacyPassManager.h" 32 #include "llvm/IR/Module.h" 33 #include "llvm/IR/ModuleSummaryIndex.h" 34 #include "llvm/IR/Verifier.h" 35 #include "llvm/LTO/LTOBackend.h" 36 #include "llvm/MC/MCAsmInfo.h" 37 #include "llvm/MC/SubtargetFeature.h" 38 #include "llvm/Passes/PassBuilder.h" 39 #include "llvm/Support/CommandLine.h" 40 #include "llvm/Support/MemoryBuffer.h" 41 #include "llvm/Support/PrettyStackTrace.h" 42 #include "llvm/Support/TargetRegistry.h" 43 #include "llvm/Support/Timer.h" 44 #include "llvm/Support/raw_ostream.h" 45 #include "llvm/Target/TargetMachine.h" 46 #include "llvm/Target/TargetOptions.h" 47 #include "llvm/Target/TargetSubtargetInfo.h" 48 #include "llvm/Transforms/Coroutines.h" 49 #include "llvm/Transforms/IPO.h" 50 #include "llvm/Transforms/IPO/AlwaysInliner.h" 51 #include "llvm/Transforms/IPO/PassManagerBuilder.h" 52 #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h" 53 #include "llvm/Transforms/Instrumentation.h" 54 #include "llvm/Transforms/ObjCARC.h" 55 #include "llvm/Transforms/Scalar.h" 56 #include "llvm/Transforms/Scalar/GVN.h" 57 #include "llvm/Transforms/Utils/NameAnonGlobals.h" 58 #include "llvm/Transforms/Utils/SymbolRewriter.h" 59 #include <memory> 60 using namespace clang; 61 using namespace llvm; 62 63 namespace { 64 65 // Default filename used for profile generation. 66 static constexpr StringLiteral DefaultProfileGenName = "default_%m.profraw"; 67 68 class EmitAssemblyHelper { 69 DiagnosticsEngine &Diags; 70 const HeaderSearchOptions &HSOpts; 71 const CodeGenOptions &CodeGenOpts; 72 const clang::TargetOptions &TargetOpts; 73 const LangOptions &LangOpts; 74 Module *TheModule; 75 76 Timer CodeGenerationTime; 77 78 std::unique_ptr<raw_pwrite_stream> OS; 79 80 TargetIRAnalysis getTargetIRAnalysis() const { 81 if (TM) 82 return TM->getTargetIRAnalysis(); 83 84 return TargetIRAnalysis(); 85 } 86 87 void CreatePasses(legacy::PassManager &MPM, legacy::FunctionPassManager &FPM); 88 89 /// Generates the TargetMachine. 90 /// Leaves TM unchanged if it is unable to create the target machine. 91 /// Some of our clang tests specify triples which are not built 92 /// into clang. This is okay because these tests check the generated 93 /// IR, and they require DataLayout which depends on the triple. 94 /// In this case, we allow this method to fail and not report an error. 95 /// When MustCreateTM is used, we print an error if we are unable to load 96 /// the requested target. 97 void CreateTargetMachine(bool MustCreateTM); 98 99 /// Add passes necessary to emit assembly or LLVM IR. 100 /// 101 /// \return True on success. 102 bool AddEmitPasses(legacy::PassManager &CodeGenPasses, BackendAction Action, 103 raw_pwrite_stream &OS); 104 105 public: 106 EmitAssemblyHelper(DiagnosticsEngine &_Diags, 107 const HeaderSearchOptions &HeaderSearchOpts, 108 const CodeGenOptions &CGOpts, 109 const clang::TargetOptions &TOpts, 110 const LangOptions &LOpts, Module *M) 111 : Diags(_Diags), HSOpts(HeaderSearchOpts), CodeGenOpts(CGOpts), 112 TargetOpts(TOpts), LangOpts(LOpts), TheModule(M), 113 CodeGenerationTime("codegen", "Code Generation Time") {} 114 115 ~EmitAssemblyHelper() { 116 if (CodeGenOpts.DisableFree) 117 BuryPointer(std::move(TM)); 118 } 119 120 std::unique_ptr<TargetMachine> TM; 121 122 void EmitAssembly(BackendAction Action, 123 std::unique_ptr<raw_pwrite_stream> OS); 124 125 void EmitAssemblyWithNewPassManager(BackendAction Action, 126 std::unique_ptr<raw_pwrite_stream> OS); 127 }; 128 129 // We need this wrapper to access LangOpts and CGOpts from extension functions 130 // that we add to the PassManagerBuilder. 131 class PassManagerBuilderWrapper : public PassManagerBuilder { 132 public: 133 PassManagerBuilderWrapper(const Triple &TargetTriple, 134 const CodeGenOptions &CGOpts, 135 const LangOptions &LangOpts) 136 : PassManagerBuilder(), TargetTriple(TargetTriple), CGOpts(CGOpts), 137 LangOpts(LangOpts) {} 138 const Triple &getTargetTriple() const { return TargetTriple; } 139 const CodeGenOptions &getCGOpts() const { return CGOpts; } 140 const LangOptions &getLangOpts() const { return LangOpts; } 141 142 private: 143 const Triple &TargetTriple; 144 const CodeGenOptions &CGOpts; 145 const LangOptions &LangOpts; 146 }; 147 } 148 149 static void addObjCARCAPElimPass(const PassManagerBuilder &Builder, PassManagerBase &PM) { 150 if (Builder.OptLevel > 0) 151 PM.add(createObjCARCAPElimPass()); 152 } 153 154 static void addObjCARCExpandPass(const PassManagerBuilder &Builder, PassManagerBase &PM) { 155 if (Builder.OptLevel > 0) 156 PM.add(createObjCARCExpandPass()); 157 } 158 159 static void addObjCARCOptPass(const PassManagerBuilder &Builder, PassManagerBase &PM) { 160 if (Builder.OptLevel > 0) 161 PM.add(createObjCARCOptPass()); 162 } 163 164 static void addAddDiscriminatorsPass(const PassManagerBuilder &Builder, 165 legacy::PassManagerBase &PM) { 166 PM.add(createAddDiscriminatorsPass()); 167 } 168 169 static void addBoundsCheckingPass(const PassManagerBuilder &Builder, 170 legacy::PassManagerBase &PM) { 171 PM.add(createBoundsCheckingPass()); 172 } 173 174 static void addSanitizerCoveragePass(const PassManagerBuilder &Builder, 175 legacy::PassManagerBase &PM) { 176 const PassManagerBuilderWrapper &BuilderWrapper = 177 static_cast<const PassManagerBuilderWrapper&>(Builder); 178 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts(); 179 SanitizerCoverageOptions Opts; 180 Opts.CoverageType = 181 static_cast<SanitizerCoverageOptions::Type>(CGOpts.SanitizeCoverageType); 182 Opts.IndirectCalls = CGOpts.SanitizeCoverageIndirectCalls; 183 Opts.TraceBB = CGOpts.SanitizeCoverageTraceBB; 184 Opts.TraceCmp = CGOpts.SanitizeCoverageTraceCmp; 185 Opts.TraceDiv = CGOpts.SanitizeCoverageTraceDiv; 186 Opts.TraceGep = CGOpts.SanitizeCoverageTraceGep; 187 Opts.Use8bitCounters = CGOpts.SanitizeCoverage8bitCounters; 188 Opts.TracePC = CGOpts.SanitizeCoverageTracePC; 189 Opts.TracePCGuard = CGOpts.SanitizeCoverageTracePCGuard; 190 Opts.NoPrune = CGOpts.SanitizeCoverageNoPrune; 191 Opts.Inline8bitCounters = CGOpts.SanitizeCoverageInline8bitCounters; 192 PM.add(createSanitizerCoverageModulePass(Opts)); 193 } 194 195 // Check if ASan should use GC-friendly instrumentation for globals. 196 // First of all, there is no point if -fdata-sections is off (expect for MachO, 197 // where this is not a factor). Also, on ELF this feature requires an assembler 198 // extension that only works with -integrated-as at the moment. 199 static bool asanUseGlobalsGC(const Triple &T, const CodeGenOptions &CGOpts) { 200 if (!CGOpts.SanitizeAddressGlobalsDeadStripping) 201 return false; 202 switch (T.getObjectFormat()) { 203 case Triple::MachO: 204 case Triple::COFF: 205 return true; 206 case Triple::ELF: 207 return CGOpts.DataSections && !CGOpts.DisableIntegratedAS; 208 default: 209 return false; 210 } 211 } 212 213 static void addAddressSanitizerPasses(const PassManagerBuilder &Builder, 214 legacy::PassManagerBase &PM) { 215 const PassManagerBuilderWrapper &BuilderWrapper = 216 static_cast<const PassManagerBuilderWrapper&>(Builder); 217 const Triple &T = BuilderWrapper.getTargetTriple(); 218 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts(); 219 bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Address); 220 bool UseAfterScope = CGOpts.SanitizeAddressUseAfterScope; 221 bool UseGlobalsGC = asanUseGlobalsGC(T, CGOpts); 222 PM.add(createAddressSanitizerFunctionPass(/*CompileKernel*/ false, Recover, 223 UseAfterScope)); 224 PM.add(createAddressSanitizerModulePass(/*CompileKernel*/ false, Recover, 225 UseGlobalsGC)); 226 } 227 228 static void addKernelAddressSanitizerPasses(const PassManagerBuilder &Builder, 229 legacy::PassManagerBase &PM) { 230 PM.add(createAddressSanitizerFunctionPass( 231 /*CompileKernel*/ true, 232 /*Recover*/ true, /*UseAfterScope*/ false)); 233 PM.add(createAddressSanitizerModulePass(/*CompileKernel*/true, 234 /*Recover*/true)); 235 } 236 237 static void addMemorySanitizerPass(const PassManagerBuilder &Builder, 238 legacy::PassManagerBase &PM) { 239 const PassManagerBuilderWrapper &BuilderWrapper = 240 static_cast<const PassManagerBuilderWrapper&>(Builder); 241 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts(); 242 int TrackOrigins = CGOpts.SanitizeMemoryTrackOrigins; 243 bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Memory); 244 PM.add(createMemorySanitizerPass(TrackOrigins, Recover)); 245 246 // MemorySanitizer inserts complex instrumentation that mostly follows 247 // the logic of the original code, but operates on "shadow" values. 248 // It can benefit from re-running some general purpose optimization passes. 249 if (Builder.OptLevel > 0) { 250 PM.add(createEarlyCSEPass()); 251 PM.add(createReassociatePass()); 252 PM.add(createLICMPass()); 253 PM.add(createGVNPass()); 254 PM.add(createInstructionCombiningPass()); 255 PM.add(createDeadStoreEliminationPass()); 256 } 257 } 258 259 static void addThreadSanitizerPass(const PassManagerBuilder &Builder, 260 legacy::PassManagerBase &PM) { 261 PM.add(createThreadSanitizerPass()); 262 } 263 264 static void addDataFlowSanitizerPass(const PassManagerBuilder &Builder, 265 legacy::PassManagerBase &PM) { 266 const PassManagerBuilderWrapper &BuilderWrapper = 267 static_cast<const PassManagerBuilderWrapper&>(Builder); 268 const LangOptions &LangOpts = BuilderWrapper.getLangOpts(); 269 PM.add(createDataFlowSanitizerPass(LangOpts.SanitizerBlacklistFiles)); 270 } 271 272 static void addEfficiencySanitizerPass(const PassManagerBuilder &Builder, 273 legacy::PassManagerBase &PM) { 274 const PassManagerBuilderWrapper &BuilderWrapper = 275 static_cast<const PassManagerBuilderWrapper&>(Builder); 276 const LangOptions &LangOpts = BuilderWrapper.getLangOpts(); 277 EfficiencySanitizerOptions Opts; 278 if (LangOpts.Sanitize.has(SanitizerKind::EfficiencyCacheFrag)) 279 Opts.ToolType = EfficiencySanitizerOptions::ESAN_CacheFrag; 280 else if (LangOpts.Sanitize.has(SanitizerKind::EfficiencyWorkingSet)) 281 Opts.ToolType = EfficiencySanitizerOptions::ESAN_WorkingSet; 282 PM.add(createEfficiencySanitizerPass(Opts)); 283 } 284 285 static TargetLibraryInfoImpl *createTLII(llvm::Triple &TargetTriple, 286 const CodeGenOptions &CodeGenOpts) { 287 TargetLibraryInfoImpl *TLII = new TargetLibraryInfoImpl(TargetTriple); 288 if (!CodeGenOpts.SimplifyLibCalls) 289 TLII->disableAllFunctions(); 290 else { 291 // Disable individual libc/libm calls in TargetLibraryInfo. 292 LibFunc F; 293 for (auto &FuncName : CodeGenOpts.getNoBuiltinFuncs()) 294 if (TLII->getLibFunc(FuncName, F)) 295 TLII->setUnavailable(F); 296 } 297 298 switch (CodeGenOpts.getVecLib()) { 299 case CodeGenOptions::Accelerate: 300 TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::Accelerate); 301 break; 302 case CodeGenOptions::SVML: 303 TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::SVML); 304 break; 305 default: 306 break; 307 } 308 return TLII; 309 } 310 311 static void addSymbolRewriterPass(const CodeGenOptions &Opts, 312 legacy::PassManager *MPM) { 313 llvm::SymbolRewriter::RewriteDescriptorList DL; 314 315 llvm::SymbolRewriter::RewriteMapParser MapParser; 316 for (const auto &MapFile : Opts.RewriteMapFiles) 317 MapParser.parse(MapFile, &DL); 318 319 MPM->add(createRewriteSymbolsPass(DL)); 320 } 321 322 static CodeGenOpt::Level getCGOptLevel(const CodeGenOptions &CodeGenOpts) { 323 switch (CodeGenOpts.OptimizationLevel) { 324 default: 325 llvm_unreachable("Invalid optimization level!"); 326 case 0: 327 return CodeGenOpt::None; 328 case 1: 329 return CodeGenOpt::Less; 330 case 2: 331 return CodeGenOpt::Default; // O2/Os/Oz 332 case 3: 333 return CodeGenOpt::Aggressive; 334 } 335 } 336 337 static llvm::CodeModel::Model getCodeModel(const CodeGenOptions &CodeGenOpts) { 338 unsigned CodeModel = 339 llvm::StringSwitch<unsigned>(CodeGenOpts.CodeModel) 340 .Case("small", llvm::CodeModel::Small) 341 .Case("kernel", llvm::CodeModel::Kernel) 342 .Case("medium", llvm::CodeModel::Medium) 343 .Case("large", llvm::CodeModel::Large) 344 .Case("default", llvm::CodeModel::Default) 345 .Default(~0u); 346 assert(CodeModel != ~0u && "invalid code model!"); 347 return static_cast<llvm::CodeModel::Model>(CodeModel); 348 } 349 350 static llvm::Reloc::Model getRelocModel(const CodeGenOptions &CodeGenOpts) { 351 // Keep this synced with the equivalent code in 352 // lib/Frontend/CompilerInvocation.cpp 353 llvm::Optional<llvm::Reloc::Model> RM; 354 RM = llvm::StringSwitch<llvm::Reloc::Model>(CodeGenOpts.RelocationModel) 355 .Case("static", llvm::Reloc::Static) 356 .Case("pic", llvm::Reloc::PIC_) 357 .Case("ropi", llvm::Reloc::ROPI) 358 .Case("rwpi", llvm::Reloc::RWPI) 359 .Case("ropi-rwpi", llvm::Reloc::ROPI_RWPI) 360 .Case("dynamic-no-pic", llvm::Reloc::DynamicNoPIC); 361 assert(RM.hasValue() && "invalid PIC model!"); 362 return *RM; 363 } 364 365 static TargetMachine::CodeGenFileType getCodeGenFileType(BackendAction Action) { 366 if (Action == Backend_EmitObj) 367 return TargetMachine::CGFT_ObjectFile; 368 else if (Action == Backend_EmitMCNull) 369 return TargetMachine::CGFT_Null; 370 else { 371 assert(Action == Backend_EmitAssembly && "Invalid action!"); 372 return TargetMachine::CGFT_AssemblyFile; 373 } 374 } 375 376 static void initTargetOptions(llvm::TargetOptions &Options, 377 const CodeGenOptions &CodeGenOpts, 378 const clang::TargetOptions &TargetOpts, 379 const LangOptions &LangOpts, 380 const HeaderSearchOptions &HSOpts) { 381 Options.ThreadModel = 382 llvm::StringSwitch<llvm::ThreadModel::Model>(CodeGenOpts.ThreadModel) 383 .Case("posix", llvm::ThreadModel::POSIX) 384 .Case("single", llvm::ThreadModel::Single); 385 386 // Set float ABI type. 387 assert((CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp" || 388 CodeGenOpts.FloatABI == "hard" || CodeGenOpts.FloatABI.empty()) && 389 "Invalid Floating Point ABI!"); 390 Options.FloatABIType = 391 llvm::StringSwitch<llvm::FloatABI::ABIType>(CodeGenOpts.FloatABI) 392 .Case("soft", llvm::FloatABI::Soft) 393 .Case("softfp", llvm::FloatABI::Soft) 394 .Case("hard", llvm::FloatABI::Hard) 395 .Default(llvm::FloatABI::Default); 396 397 // Set FP fusion mode. 398 switch (LangOpts.getDefaultFPContractMode()) { 399 case LangOptions::FPC_Off: 400 // Preserve any contraction performed by the front-end. (Strict performs 401 // splitting of the muladd instrinsic in the backend.) 402 Options.AllowFPOpFusion = llvm::FPOpFusion::Standard; 403 break; 404 case LangOptions::FPC_On: 405 Options.AllowFPOpFusion = llvm::FPOpFusion::Standard; 406 break; 407 case LangOptions::FPC_Fast: 408 Options.AllowFPOpFusion = llvm::FPOpFusion::Fast; 409 break; 410 } 411 412 Options.UseInitArray = CodeGenOpts.UseInitArray; 413 Options.DisableIntegratedAS = CodeGenOpts.DisableIntegratedAS; 414 Options.CompressDebugSections = CodeGenOpts.getCompressDebugSections(); 415 Options.RelaxELFRelocations = CodeGenOpts.RelaxELFRelocations; 416 417 // Set EABI version. 418 Options.EABIVersion = TargetOpts.EABIVersion; 419 420 if (LangOpts.SjLjExceptions) 421 Options.ExceptionModel = llvm::ExceptionHandling::SjLj; 422 423 Options.NoInfsFPMath = CodeGenOpts.NoInfsFPMath; 424 Options.NoNaNsFPMath = CodeGenOpts.NoNaNsFPMath; 425 Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS; 426 Options.UnsafeFPMath = CodeGenOpts.UnsafeFPMath; 427 Options.StackAlignmentOverride = CodeGenOpts.StackAlignment; 428 Options.FunctionSections = CodeGenOpts.FunctionSections; 429 Options.DataSections = CodeGenOpts.DataSections; 430 Options.UniqueSectionNames = CodeGenOpts.UniqueSectionNames; 431 Options.EmulatedTLS = CodeGenOpts.EmulatedTLS; 432 Options.DebuggerTuning = CodeGenOpts.getDebuggerTuning(); 433 434 if (CodeGenOpts.EnableSplitDwarf) 435 Options.MCOptions.SplitDwarfFile = CodeGenOpts.SplitDwarfFile; 436 Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll; 437 Options.MCOptions.MCSaveTempLabels = CodeGenOpts.SaveTempLabels; 438 Options.MCOptions.MCUseDwarfDirectory = !CodeGenOpts.NoDwarfDirectoryAsm; 439 Options.MCOptions.MCNoExecStack = CodeGenOpts.NoExecStack; 440 Options.MCOptions.MCIncrementalLinkerCompatible = 441 CodeGenOpts.IncrementalLinkerCompatible; 442 Options.MCOptions.MCPIECopyRelocations = CodeGenOpts.PIECopyRelocations; 443 Options.MCOptions.MCFatalWarnings = CodeGenOpts.FatalWarnings; 444 Options.MCOptions.AsmVerbose = CodeGenOpts.AsmVerbose; 445 Options.MCOptions.PreserveAsmComments = CodeGenOpts.PreserveAsmComments; 446 Options.MCOptions.ABIName = TargetOpts.ABI; 447 for (const auto &Entry : HSOpts.UserEntries) 448 if (!Entry.IsFramework && 449 (Entry.Group == frontend::IncludeDirGroup::Quoted || 450 Entry.Group == frontend::IncludeDirGroup::Angled || 451 Entry.Group == frontend::IncludeDirGroup::System)) 452 Options.MCOptions.IASSearchPaths.push_back( 453 Entry.IgnoreSysRoot ? Entry.Path : HSOpts.Sysroot + Entry.Path); 454 } 455 456 void EmitAssemblyHelper::CreatePasses(legacy::PassManager &MPM, 457 legacy::FunctionPassManager &FPM) { 458 // Handle disabling of all LLVM passes, where we want to preserve the 459 // internal module before any optimization. 460 if (CodeGenOpts.DisableLLVMPasses) 461 return; 462 463 // Figure out TargetLibraryInfo. This needs to be added to MPM and FPM 464 // manually (and not via PMBuilder), since some passes (eg. InstrProfiling) 465 // are inserted before PMBuilder ones - they'd get the default-constructed 466 // TLI with an unknown target otherwise. 467 Triple TargetTriple(TheModule->getTargetTriple()); 468 std::unique_ptr<TargetLibraryInfoImpl> TLII( 469 createTLII(TargetTriple, CodeGenOpts)); 470 471 PassManagerBuilderWrapper PMBuilder(TargetTriple, CodeGenOpts, LangOpts); 472 473 // At O0 and O1 we only run the always inliner which is more efficient. At 474 // higher optimization levels we run the normal inliner. 475 if (CodeGenOpts.OptimizationLevel <= 1) { 476 bool InsertLifetimeIntrinsics = (CodeGenOpts.OptimizationLevel != 0 && 477 !CodeGenOpts.DisableLifetimeMarkers); 478 PMBuilder.Inliner = createAlwaysInlinerLegacyPass(InsertLifetimeIntrinsics); 479 } else { 480 // We do not want to inline hot callsites for SamplePGO module-summary build 481 // because profile annotation will happen again in ThinLTO backend, and we 482 // want the IR of the hot path to match the profile. 483 PMBuilder.Inliner = createFunctionInliningPass( 484 CodeGenOpts.OptimizationLevel, CodeGenOpts.OptimizeSize, 485 (!CodeGenOpts.SampleProfileFile.empty() && 486 CodeGenOpts.EmitSummaryIndex)); 487 } 488 489 PMBuilder.OptLevel = CodeGenOpts.OptimizationLevel; 490 PMBuilder.SizeLevel = CodeGenOpts.OptimizeSize; 491 PMBuilder.SLPVectorize = CodeGenOpts.VectorizeSLP; 492 PMBuilder.LoopVectorize = CodeGenOpts.VectorizeLoop; 493 494 PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops; 495 PMBuilder.MergeFunctions = CodeGenOpts.MergeFunctions; 496 PMBuilder.PrepareForThinLTO = CodeGenOpts.EmitSummaryIndex; 497 PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO; 498 PMBuilder.RerollLoops = CodeGenOpts.RerollLoops; 499 500 MPM.add(new TargetLibraryInfoWrapperPass(*TLII)); 501 502 if (TM) 503 TM->adjustPassManager(PMBuilder); 504 505 if (CodeGenOpts.DebugInfoForProfiling || 506 !CodeGenOpts.SampleProfileFile.empty()) 507 PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible, 508 addAddDiscriminatorsPass); 509 510 // In ObjC ARC mode, add the main ARC optimization passes. 511 if (LangOpts.ObjCAutoRefCount) { 512 PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible, 513 addObjCARCExpandPass); 514 PMBuilder.addExtension(PassManagerBuilder::EP_ModuleOptimizerEarly, 515 addObjCARCAPElimPass); 516 PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate, 517 addObjCARCOptPass); 518 } 519 520 if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds)) { 521 PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate, 522 addBoundsCheckingPass); 523 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 524 addBoundsCheckingPass); 525 } 526 527 if (CodeGenOpts.SanitizeCoverageType || 528 CodeGenOpts.SanitizeCoverageIndirectCalls || 529 CodeGenOpts.SanitizeCoverageTraceCmp) { 530 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 531 addSanitizerCoveragePass); 532 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 533 addSanitizerCoveragePass); 534 } 535 536 if (LangOpts.Sanitize.has(SanitizerKind::Address)) { 537 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 538 addAddressSanitizerPasses); 539 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 540 addAddressSanitizerPasses); 541 } 542 543 if (LangOpts.Sanitize.has(SanitizerKind::KernelAddress)) { 544 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 545 addKernelAddressSanitizerPasses); 546 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 547 addKernelAddressSanitizerPasses); 548 } 549 550 if (LangOpts.Sanitize.has(SanitizerKind::Memory)) { 551 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 552 addMemorySanitizerPass); 553 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 554 addMemorySanitizerPass); 555 } 556 557 if (LangOpts.Sanitize.has(SanitizerKind::Thread)) { 558 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 559 addThreadSanitizerPass); 560 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 561 addThreadSanitizerPass); 562 } 563 564 if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) { 565 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 566 addDataFlowSanitizerPass); 567 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 568 addDataFlowSanitizerPass); 569 } 570 571 if (LangOpts.CoroutinesTS) 572 addCoroutinePassesToExtensionPoints(PMBuilder); 573 574 if (LangOpts.Sanitize.hasOneOf(SanitizerKind::Efficiency)) { 575 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 576 addEfficiencySanitizerPass); 577 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 578 addEfficiencySanitizerPass); 579 } 580 581 // Set up the per-function pass manager. 582 FPM.add(new TargetLibraryInfoWrapperPass(*TLII)); 583 if (CodeGenOpts.VerifyModule) 584 FPM.add(createVerifierPass()); 585 586 // Set up the per-module pass manager. 587 if (!CodeGenOpts.RewriteMapFiles.empty()) 588 addSymbolRewriterPass(CodeGenOpts, &MPM); 589 590 if (!CodeGenOpts.DisableGCov && 591 (CodeGenOpts.EmitGcovArcs || CodeGenOpts.EmitGcovNotes)) { 592 // Not using 'GCOVOptions::getDefault' allows us to avoid exiting if 593 // LLVM's -default-gcov-version flag is set to something invalid. 594 GCOVOptions Options; 595 Options.EmitNotes = CodeGenOpts.EmitGcovNotes; 596 Options.EmitData = CodeGenOpts.EmitGcovArcs; 597 memcpy(Options.Version, CodeGenOpts.CoverageVersion, 4); 598 Options.UseCfgChecksum = CodeGenOpts.CoverageExtraChecksum; 599 Options.NoRedZone = CodeGenOpts.DisableRedZone; 600 Options.FunctionNamesInData = 601 !CodeGenOpts.CoverageNoFunctionNamesInData; 602 Options.ExitBlockBeforeBody = CodeGenOpts.CoverageExitBlockBeforeBody; 603 MPM.add(createGCOVProfilerPass(Options)); 604 if (CodeGenOpts.getDebugInfo() == codegenoptions::NoDebugInfo) 605 MPM.add(createStripSymbolsPass(true)); 606 } 607 608 if (CodeGenOpts.hasProfileClangInstr()) { 609 InstrProfOptions Options; 610 Options.NoRedZone = CodeGenOpts.DisableRedZone; 611 Options.InstrProfileOutput = CodeGenOpts.InstrProfileOutput; 612 MPM.add(createInstrProfilingLegacyPass(Options)); 613 } 614 if (CodeGenOpts.hasProfileIRInstr()) { 615 PMBuilder.EnablePGOInstrGen = true; 616 if (!CodeGenOpts.InstrProfileOutput.empty()) 617 PMBuilder.PGOInstrGen = CodeGenOpts.InstrProfileOutput; 618 else 619 PMBuilder.PGOInstrGen = DefaultProfileGenName; 620 } 621 if (CodeGenOpts.hasProfileIRUse()) 622 PMBuilder.PGOInstrUse = CodeGenOpts.ProfileInstrumentUsePath; 623 624 if (!CodeGenOpts.SampleProfileFile.empty()) 625 PMBuilder.PGOSampleUse = CodeGenOpts.SampleProfileFile; 626 627 PMBuilder.populateFunctionPassManager(FPM); 628 PMBuilder.populateModulePassManager(MPM); 629 } 630 631 static void setCommandLineOpts(const CodeGenOptions &CodeGenOpts) { 632 SmallVector<const char *, 16> BackendArgs; 633 BackendArgs.push_back("clang"); // Fake program name. 634 if (!CodeGenOpts.DebugPass.empty()) { 635 BackendArgs.push_back("-debug-pass"); 636 BackendArgs.push_back(CodeGenOpts.DebugPass.c_str()); 637 } 638 if (!CodeGenOpts.LimitFloatPrecision.empty()) { 639 BackendArgs.push_back("-limit-float-precision"); 640 BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str()); 641 } 642 for (const std::string &BackendOption : CodeGenOpts.BackendOptions) 643 BackendArgs.push_back(BackendOption.c_str()); 644 BackendArgs.push_back(nullptr); 645 llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1, 646 BackendArgs.data()); 647 } 648 649 void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) { 650 // Create the TargetMachine for generating code. 651 std::string Error; 652 std::string Triple = TheModule->getTargetTriple(); 653 const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error); 654 if (!TheTarget) { 655 if (MustCreateTM) 656 Diags.Report(diag::err_fe_unable_to_create_target) << Error; 657 return; 658 } 659 660 llvm::CodeModel::Model CM = getCodeModel(CodeGenOpts); 661 std::string FeaturesStr = 662 llvm::join(TargetOpts.Features.begin(), TargetOpts.Features.end(), ","); 663 llvm::Reloc::Model RM = getRelocModel(CodeGenOpts); 664 CodeGenOpt::Level OptLevel = getCGOptLevel(CodeGenOpts); 665 666 llvm::TargetOptions Options; 667 initTargetOptions(Options, CodeGenOpts, TargetOpts, LangOpts, HSOpts); 668 TM.reset(TheTarget->createTargetMachine(Triple, TargetOpts.CPU, FeaturesStr, 669 Options, RM, CM, OptLevel)); 670 } 671 672 bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager &CodeGenPasses, 673 BackendAction Action, 674 raw_pwrite_stream &OS) { 675 // Add LibraryInfo. 676 llvm::Triple TargetTriple(TheModule->getTargetTriple()); 677 std::unique_ptr<TargetLibraryInfoImpl> TLII( 678 createTLII(TargetTriple, CodeGenOpts)); 679 CodeGenPasses.add(new TargetLibraryInfoWrapperPass(*TLII)); 680 681 // Normal mode, emit a .s or .o file by running the code generator. Note, 682 // this also adds codegenerator level optimization passes. 683 TargetMachine::CodeGenFileType CGFT = getCodeGenFileType(Action); 684 685 // Add ObjC ARC final-cleanup optimizations. This is done as part of the 686 // "codegen" passes so that it isn't run multiple times when there is 687 // inlining happening. 688 if (CodeGenOpts.OptimizationLevel > 0) 689 CodeGenPasses.add(createObjCARCContractPass()); 690 691 if (TM->addPassesToEmitFile(CodeGenPasses, OS, CGFT, 692 /*DisableVerify=*/!CodeGenOpts.VerifyModule)) { 693 Diags.Report(diag::err_fe_unable_to_interface_with_target); 694 return false; 695 } 696 697 return true; 698 } 699 700 void EmitAssemblyHelper::EmitAssembly(BackendAction Action, 701 std::unique_ptr<raw_pwrite_stream> OS) { 702 TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : nullptr); 703 704 setCommandLineOpts(CodeGenOpts); 705 706 bool UsesCodeGen = (Action != Backend_EmitNothing && 707 Action != Backend_EmitBC && 708 Action != Backend_EmitLL); 709 CreateTargetMachine(UsesCodeGen); 710 711 if (UsesCodeGen && !TM) 712 return; 713 if (TM) 714 TheModule->setDataLayout(TM->createDataLayout()); 715 716 legacy::PassManager PerModulePasses; 717 PerModulePasses.add( 718 createTargetTransformInfoWrapperPass(getTargetIRAnalysis())); 719 720 legacy::FunctionPassManager PerFunctionPasses(TheModule); 721 PerFunctionPasses.add( 722 createTargetTransformInfoWrapperPass(getTargetIRAnalysis())); 723 724 CreatePasses(PerModulePasses, PerFunctionPasses); 725 726 legacy::PassManager CodeGenPasses; 727 CodeGenPasses.add( 728 createTargetTransformInfoWrapperPass(getTargetIRAnalysis())); 729 730 std::unique_ptr<raw_fd_ostream> ThinLinkOS; 731 732 switch (Action) { 733 case Backend_EmitNothing: 734 break; 735 736 case Backend_EmitBC: 737 if (CodeGenOpts.EmitSummaryIndex) { 738 if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) { 739 std::error_code EC; 740 ThinLinkOS.reset(new llvm::raw_fd_ostream( 741 CodeGenOpts.ThinLinkBitcodeFile, EC, 742 llvm::sys::fs::F_None)); 743 if (EC) { 744 Diags.Report(diag::err_fe_unable_to_open_output) << CodeGenOpts.ThinLinkBitcodeFile 745 << EC.message(); 746 return; 747 } 748 } 749 PerModulePasses.add( 750 createWriteThinLTOBitcodePass(*OS, ThinLinkOS.get())); 751 } 752 else 753 PerModulePasses.add( 754 createBitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists)); 755 break; 756 757 case Backend_EmitLL: 758 PerModulePasses.add( 759 createPrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists)); 760 break; 761 762 default: 763 if (!AddEmitPasses(CodeGenPasses, Action, *OS)) 764 return; 765 } 766 767 // Before executing passes, print the final values of the LLVM options. 768 cl::PrintOptionValues(); 769 770 // Run passes. For now we do all passes at once, but eventually we 771 // would like to have the option of streaming code generation. 772 773 { 774 PrettyStackTraceString CrashInfo("Per-function optimization"); 775 776 PerFunctionPasses.doInitialization(); 777 for (Function &F : *TheModule) 778 if (!F.isDeclaration()) 779 PerFunctionPasses.run(F); 780 PerFunctionPasses.doFinalization(); 781 } 782 783 { 784 PrettyStackTraceString CrashInfo("Per-module optimization passes"); 785 PerModulePasses.run(*TheModule); 786 } 787 788 { 789 PrettyStackTraceString CrashInfo("Code generation"); 790 CodeGenPasses.run(*TheModule); 791 } 792 } 793 794 static PassBuilder::OptimizationLevel mapToLevel(const CodeGenOptions &Opts) { 795 switch (Opts.OptimizationLevel) { 796 default: 797 llvm_unreachable("Invalid optimization level!"); 798 799 case 1: 800 return PassBuilder::O1; 801 802 case 2: 803 switch (Opts.OptimizeSize) { 804 default: 805 llvm_unreachable("Invalide optimization level for size!"); 806 807 case 0: 808 return PassBuilder::O2; 809 810 case 1: 811 return PassBuilder::Os; 812 813 case 2: 814 return PassBuilder::Oz; 815 } 816 817 case 3: 818 return PassBuilder::O3; 819 } 820 } 821 822 /// A clean version of `EmitAssembly` that uses the new pass manager. 823 /// 824 /// Not all features are currently supported in this system, but where 825 /// necessary it falls back to the legacy pass manager to at least provide 826 /// basic functionality. 827 /// 828 /// This API is planned to have its functionality finished and then to replace 829 /// `EmitAssembly` at some point in the future when the default switches. 830 void EmitAssemblyHelper::EmitAssemblyWithNewPassManager( 831 BackendAction Action, std::unique_ptr<raw_pwrite_stream> OS) { 832 TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : nullptr); 833 setCommandLineOpts(CodeGenOpts); 834 835 // The new pass manager always makes a target machine available to passes 836 // during construction. 837 CreateTargetMachine(/*MustCreateTM*/ true); 838 if (!TM) 839 // This will already be diagnosed, just bail. 840 return; 841 TheModule->setDataLayout(TM->createDataLayout()); 842 843 PGOOptions PGOOpt; 844 845 // -fprofile-generate. 846 PGOOpt.RunProfileGen = CodeGenOpts.hasProfileIRInstr(); 847 if (PGOOpt.RunProfileGen) 848 PGOOpt.ProfileGenFile = CodeGenOpts.InstrProfileOutput.empty() ? 849 DefaultProfileGenName : CodeGenOpts.InstrProfileOutput; 850 851 // -fprofile-use. 852 if (CodeGenOpts.hasProfileIRUse()) 853 PGOOpt.ProfileUseFile = CodeGenOpts.ProfileInstrumentUsePath; 854 855 if (!CodeGenOpts.SampleProfileFile.empty()) 856 PGOOpt.SampleProfileFile = CodeGenOpts.SampleProfileFile; 857 858 // Only pass a PGO options struct if -fprofile-generate or 859 // -fprofile-use were passed on the cmdline. 860 PassBuilder PB(TM.get(), 861 (PGOOpt.RunProfileGen || 862 !PGOOpt.ProfileUseFile.empty() || 863 !PGOOpt.SampleProfileFile.empty()) ? 864 Optional<PGOOptions>(PGOOpt) : None); 865 866 LoopAnalysisManager LAM; 867 FunctionAnalysisManager FAM; 868 CGSCCAnalysisManager CGAM; 869 ModuleAnalysisManager MAM; 870 871 // Register the AA manager first so that our version is the one used. 872 FAM.registerPass([&] { return PB.buildDefaultAAPipeline(); }); 873 874 // Register all the basic analyses with the managers. 875 PB.registerModuleAnalyses(MAM); 876 PB.registerCGSCCAnalyses(CGAM); 877 PB.registerFunctionAnalyses(FAM); 878 PB.registerLoopAnalyses(LAM); 879 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); 880 881 ModulePassManager MPM(CodeGenOpts.DebugPassManager); 882 883 if (!CodeGenOpts.DisableLLVMPasses) { 884 bool IsThinLTO = CodeGenOpts.EmitSummaryIndex; 885 bool IsLTO = CodeGenOpts.PrepareForLTO; 886 887 if (CodeGenOpts.OptimizationLevel == 0) { 888 // Build a minimal pipeline based on the semantics required by Clang, 889 // which is just that always inlining occurs. 890 MPM.addPass(AlwaysInlinerPass()); 891 if (IsThinLTO) 892 MPM.addPass(NameAnonGlobalPass()); 893 } else { 894 // Map our optimization levels into one of the distinct levels used to 895 // configure the pipeline. 896 PassBuilder::OptimizationLevel Level = mapToLevel(CodeGenOpts); 897 898 if (IsThinLTO) { 899 MPM = PB.buildThinLTOPreLinkDefaultPipeline( 900 Level, CodeGenOpts.DebugPassManager); 901 MPM.addPass(NameAnonGlobalPass()); 902 } else if (IsLTO) { 903 MPM = PB.buildLTOPreLinkDefaultPipeline(Level, 904 CodeGenOpts.DebugPassManager); 905 } else { 906 MPM = PB.buildPerModuleDefaultPipeline(Level, 907 CodeGenOpts.DebugPassManager); 908 } 909 } 910 } 911 912 // FIXME: We still use the legacy pass manager to do code generation. We 913 // create that pass manager here and use it as needed below. 914 legacy::PassManager CodeGenPasses; 915 bool NeedCodeGen = false; 916 Optional<raw_fd_ostream> ThinLinkOS; 917 918 // Append any output we need to the pass manager. 919 switch (Action) { 920 case Backend_EmitNothing: 921 break; 922 923 case Backend_EmitBC: 924 if (CodeGenOpts.EmitSummaryIndex) { 925 if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) { 926 std::error_code EC; 927 ThinLinkOS.emplace(CodeGenOpts.ThinLinkBitcodeFile, EC, 928 llvm::sys::fs::F_None); 929 if (EC) { 930 Diags.Report(diag::err_fe_unable_to_open_output) 931 << CodeGenOpts.ThinLinkBitcodeFile << EC.message(); 932 return; 933 } 934 } 935 MPM.addPass( 936 ThinLTOBitcodeWriterPass(*OS, ThinLinkOS ? &*ThinLinkOS : nullptr)); 937 } else { 938 MPM.addPass(BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists, 939 CodeGenOpts.EmitSummaryIndex, 940 CodeGenOpts.EmitSummaryIndex)); 941 } 942 break; 943 944 case Backend_EmitLL: 945 MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists)); 946 break; 947 948 case Backend_EmitAssembly: 949 case Backend_EmitMCNull: 950 case Backend_EmitObj: 951 NeedCodeGen = true; 952 CodeGenPasses.add( 953 createTargetTransformInfoWrapperPass(getTargetIRAnalysis())); 954 if (!AddEmitPasses(CodeGenPasses, Action, *OS)) 955 // FIXME: Should we handle this error differently? 956 return; 957 break; 958 } 959 960 // Before executing passes, print the final values of the LLVM options. 961 cl::PrintOptionValues(); 962 963 // Now that we have all of the passes ready, run them. 964 { 965 PrettyStackTraceString CrashInfo("Optimizer"); 966 MPM.run(*TheModule, MAM); 967 } 968 969 // Now if needed, run the legacy PM for codegen. 970 if (NeedCodeGen) { 971 PrettyStackTraceString CrashInfo("Code generation"); 972 CodeGenPasses.run(*TheModule); 973 } 974 } 975 976 Expected<BitcodeModule> clang::FindThinLTOModule(MemoryBufferRef MBRef) { 977 Expected<std::vector<BitcodeModule>> BMsOrErr = getBitcodeModuleList(MBRef); 978 if (!BMsOrErr) 979 return BMsOrErr.takeError(); 980 981 // The bitcode file may contain multiple modules, we want the one that is 982 // marked as being the ThinLTO module. 983 for (BitcodeModule &BM : *BMsOrErr) { 984 Expected<BitcodeLTOInfo> LTOInfo = BM.getLTOInfo(); 985 if (LTOInfo && LTOInfo->IsThinLTO) 986 return BM; 987 } 988 989 return make_error<StringError>("Could not find module summary", 990 inconvertibleErrorCode()); 991 } 992 993 static void runThinLTOBackend(ModuleSummaryIndex *CombinedIndex, Module *M, 994 const HeaderSearchOptions &HeaderOpts, 995 const CodeGenOptions &CGOpts, 996 const clang::TargetOptions &TOpts, 997 const LangOptions &LOpts, 998 std::unique_ptr<raw_pwrite_stream> OS, 999 std::string SampleProfile, 1000 BackendAction Action) { 1001 StringMap<DenseMap<GlobalValue::GUID, GlobalValueSummary *>> 1002 ModuleToDefinedGVSummaries; 1003 CombinedIndex->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); 1004 1005 setCommandLineOpts(CGOpts); 1006 1007 // We can simply import the values mentioned in the combined index, since 1008 // we should only invoke this using the individual indexes written out 1009 // via a WriteIndexesThinBackend. 1010 FunctionImporter::ImportMapTy ImportList; 1011 for (auto &GlobalList : *CombinedIndex) { 1012 // Ignore entries for undefined references. 1013 if (GlobalList.second.SummaryList.empty()) 1014 continue; 1015 1016 auto GUID = GlobalList.first; 1017 assert(GlobalList.second.SummaryList.size() == 1 && 1018 "Expected individual combined index to have one summary per GUID"); 1019 auto &Summary = GlobalList.second.SummaryList[0]; 1020 // Skip the summaries for the importing module. These are included to 1021 // e.g. record required linkage changes. 1022 if (Summary->modulePath() == M->getModuleIdentifier()) 1023 continue; 1024 // Doesn't matter what value we plug in to the map, just needs an entry 1025 // to provoke importing by thinBackend. 1026 ImportList[Summary->modulePath()][GUID] = 1; 1027 } 1028 1029 std::vector<std::unique_ptr<llvm::MemoryBuffer>> OwnedImports; 1030 MapVector<llvm::StringRef, llvm::BitcodeModule> ModuleMap; 1031 1032 for (auto &I : ImportList) { 1033 ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MBOrErr = 1034 llvm::MemoryBuffer::getFile(I.first()); 1035 if (!MBOrErr) { 1036 errs() << "Error loading imported file '" << I.first() 1037 << "': " << MBOrErr.getError().message() << "\n"; 1038 return; 1039 } 1040 1041 Expected<BitcodeModule> BMOrErr = FindThinLTOModule(**MBOrErr); 1042 if (!BMOrErr) { 1043 handleAllErrors(BMOrErr.takeError(), [&](ErrorInfoBase &EIB) { 1044 errs() << "Error loading imported file '" << I.first() 1045 << "': " << EIB.message() << '\n'; 1046 }); 1047 return; 1048 } 1049 ModuleMap.insert({I.first(), *BMOrErr}); 1050 1051 OwnedImports.push_back(std::move(*MBOrErr)); 1052 } 1053 auto AddStream = [&](size_t Task) { 1054 return llvm::make_unique<lto::NativeObjectStream>(std::move(OS)); 1055 }; 1056 lto::Config Conf; 1057 Conf.CPU = TOpts.CPU; 1058 Conf.CodeModel = getCodeModel(CGOpts); 1059 Conf.MAttrs = TOpts.Features; 1060 Conf.RelocModel = getRelocModel(CGOpts); 1061 Conf.CGOptLevel = getCGOptLevel(CGOpts); 1062 initTargetOptions(Conf.Options, CGOpts, TOpts, LOpts, HeaderOpts); 1063 Conf.SampleProfile = std::move(SampleProfile); 1064 Conf.UseNewPM = CGOpts.ExperimentalNewPassManager; 1065 switch (Action) { 1066 case Backend_EmitNothing: 1067 Conf.PreCodeGenModuleHook = [](size_t Task, const Module &Mod) { 1068 return false; 1069 }; 1070 break; 1071 case Backend_EmitLL: 1072 Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) { 1073 M->print(*OS, nullptr, CGOpts.EmitLLVMUseLists); 1074 return false; 1075 }; 1076 break; 1077 case Backend_EmitBC: 1078 Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) { 1079 WriteBitcodeToFile(M, *OS, CGOpts.EmitLLVMUseLists); 1080 return false; 1081 }; 1082 break; 1083 default: 1084 Conf.CGFileType = getCodeGenFileType(Action); 1085 break; 1086 } 1087 if (Error E = thinBackend( 1088 Conf, 0, AddStream, *M, *CombinedIndex, ImportList, 1089 ModuleToDefinedGVSummaries[M->getModuleIdentifier()], ModuleMap)) { 1090 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) { 1091 errs() << "Error running ThinLTO backend: " << EIB.message() << '\n'; 1092 }); 1093 } 1094 } 1095 1096 void clang::EmitBackendOutput(DiagnosticsEngine &Diags, 1097 const HeaderSearchOptions &HeaderOpts, 1098 const CodeGenOptions &CGOpts, 1099 const clang::TargetOptions &TOpts, 1100 const LangOptions &LOpts, 1101 const llvm::DataLayout &TDesc, Module *M, 1102 BackendAction Action, 1103 std::unique_ptr<raw_pwrite_stream> OS) { 1104 if (!CGOpts.ThinLTOIndexFile.empty()) { 1105 // If we are performing a ThinLTO importing compile, load the function index 1106 // into memory and pass it into runThinLTOBackend, which will run the 1107 // function importer and invoke LTO passes. 1108 Expected<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr = 1109 llvm::getModuleSummaryIndexForFile(CGOpts.ThinLTOIndexFile, 1110 /*IgnoreEmptyThinLTOIndexFile*/true); 1111 if (!IndexOrErr) { 1112 logAllUnhandledErrors(IndexOrErr.takeError(), errs(), 1113 "Error loading index file '" + 1114 CGOpts.ThinLTOIndexFile + "': "); 1115 return; 1116 } 1117 std::unique_ptr<ModuleSummaryIndex> CombinedIndex = std::move(*IndexOrErr); 1118 // A null CombinedIndex means we should skip ThinLTO compilation 1119 // (LLVM will optionally ignore empty index files, returning null instead 1120 // of an error). 1121 bool DoThinLTOBackend = CombinedIndex != nullptr; 1122 if (DoThinLTOBackend) { 1123 runThinLTOBackend(CombinedIndex.get(), M, HeaderOpts, CGOpts, TOpts, 1124 LOpts, std::move(OS), CGOpts.SampleProfileFile, Action); 1125 return; 1126 } 1127 } 1128 1129 EmitAssemblyHelper AsmHelper(Diags, HeaderOpts, CGOpts, TOpts, LOpts, M); 1130 1131 if (CGOpts.ExperimentalNewPassManager) 1132 AsmHelper.EmitAssemblyWithNewPassManager(Action, std::move(OS)); 1133 else 1134 AsmHelper.EmitAssembly(Action, std::move(OS)); 1135 1136 // Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's 1137 // DataLayout. 1138 if (AsmHelper.TM) { 1139 std::string DLDesc = M->getDataLayout().getStringRepresentation(); 1140 if (DLDesc != TDesc.getStringRepresentation()) { 1141 unsigned DiagID = Diags.getCustomDiagID( 1142 DiagnosticsEngine::Error, "backend data layout '%0' does not match " 1143 "expected target description '%1'"); 1144 Diags.Report(DiagID) << DLDesc << TDesc.getStringRepresentation(); 1145 } 1146 } 1147 } 1148 1149 static const char* getSectionNameForBitcode(const Triple &T) { 1150 switch (T.getObjectFormat()) { 1151 case Triple::MachO: 1152 return "__LLVM,__bitcode"; 1153 case Triple::COFF: 1154 case Triple::ELF: 1155 case Triple::Wasm: 1156 case Triple::UnknownObjectFormat: 1157 return ".llvmbc"; 1158 } 1159 llvm_unreachable("Unimplemented ObjectFormatType"); 1160 } 1161 1162 static const char* getSectionNameForCommandline(const Triple &T) { 1163 switch (T.getObjectFormat()) { 1164 case Triple::MachO: 1165 return "__LLVM,__cmdline"; 1166 case Triple::COFF: 1167 case Triple::ELF: 1168 case Triple::Wasm: 1169 case Triple::UnknownObjectFormat: 1170 return ".llvmcmd"; 1171 } 1172 llvm_unreachable("Unimplemented ObjectFormatType"); 1173 } 1174 1175 // With -fembed-bitcode, save a copy of the llvm IR as data in the 1176 // __LLVM,__bitcode section. 1177 void clang::EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts, 1178 llvm::MemoryBufferRef Buf) { 1179 if (CGOpts.getEmbedBitcode() == CodeGenOptions::Embed_Off) 1180 return; 1181 1182 // Save llvm.compiler.used and remote it. 1183 SmallVector<Constant*, 2> UsedArray; 1184 SmallSet<GlobalValue*, 4> UsedGlobals; 1185 Type *UsedElementType = Type::getInt8Ty(M->getContext())->getPointerTo(0); 1186 GlobalVariable *Used = collectUsedGlobalVariables(*M, UsedGlobals, true); 1187 for (auto *GV : UsedGlobals) { 1188 if (GV->getName() != "llvm.embedded.module" && 1189 GV->getName() != "llvm.cmdline") 1190 UsedArray.push_back( 1191 ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType)); 1192 } 1193 if (Used) 1194 Used->eraseFromParent(); 1195 1196 // Embed the bitcode for the llvm module. 1197 std::string Data; 1198 ArrayRef<uint8_t> ModuleData; 1199 Triple T(M->getTargetTriple()); 1200 // Create a constant that contains the bitcode. 1201 // In case of embedding a marker, ignore the input Buf and use the empty 1202 // ArrayRef. It is also legal to create a bitcode marker even Buf is empty. 1203 if (CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Marker) { 1204 if (!isBitcode((const unsigned char *)Buf.getBufferStart(), 1205 (const unsigned char *)Buf.getBufferEnd())) { 1206 // If the input is LLVM Assembly, bitcode is produced by serializing 1207 // the module. Use-lists order need to be perserved in this case. 1208 llvm::raw_string_ostream OS(Data); 1209 llvm::WriteBitcodeToFile(M, OS, /* ShouldPreserveUseListOrder */ true); 1210 ModuleData = 1211 ArrayRef<uint8_t>((const uint8_t *)OS.str().data(), OS.str().size()); 1212 } else 1213 // If the input is LLVM bitcode, write the input byte stream directly. 1214 ModuleData = ArrayRef<uint8_t>((const uint8_t *)Buf.getBufferStart(), 1215 Buf.getBufferSize()); 1216 } 1217 llvm::Constant *ModuleConstant = 1218 llvm::ConstantDataArray::get(M->getContext(), ModuleData); 1219 llvm::GlobalVariable *GV = new llvm::GlobalVariable( 1220 *M, ModuleConstant->getType(), true, llvm::GlobalValue::PrivateLinkage, 1221 ModuleConstant); 1222 GV->setSection(getSectionNameForBitcode(T)); 1223 UsedArray.push_back( 1224 ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType)); 1225 if (llvm::GlobalVariable *Old = 1226 M->getGlobalVariable("llvm.embedded.module", true)) { 1227 assert(Old->hasOneUse() && 1228 "llvm.embedded.module can only be used once in llvm.compiler.used"); 1229 GV->takeName(Old); 1230 Old->eraseFromParent(); 1231 } else { 1232 GV->setName("llvm.embedded.module"); 1233 } 1234 1235 // Skip if only bitcode needs to be embedded. 1236 if (CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Bitcode) { 1237 // Embed command-line options. 1238 ArrayRef<uint8_t> CmdData(const_cast<uint8_t *>(CGOpts.CmdArgs.data()), 1239 CGOpts.CmdArgs.size()); 1240 llvm::Constant *CmdConstant = 1241 llvm::ConstantDataArray::get(M->getContext(), CmdData); 1242 GV = new llvm::GlobalVariable(*M, CmdConstant->getType(), true, 1243 llvm::GlobalValue::PrivateLinkage, 1244 CmdConstant); 1245 GV->setSection(getSectionNameForCommandline(T)); 1246 UsedArray.push_back( 1247 ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType)); 1248 if (llvm::GlobalVariable *Old = 1249 M->getGlobalVariable("llvm.cmdline", true)) { 1250 assert(Old->hasOneUse() && 1251 "llvm.cmdline can only be used once in llvm.compiler.used"); 1252 GV->takeName(Old); 1253 Old->eraseFromParent(); 1254 } else { 1255 GV->setName("llvm.cmdline"); 1256 } 1257 } 1258 1259 if (UsedArray.empty()) 1260 return; 1261 1262 // Recreate llvm.compiler.used. 1263 ArrayType *ATy = ArrayType::get(UsedElementType, UsedArray.size()); 1264 auto *NewUsed = new GlobalVariable( 1265 *M, ATy, false, llvm::GlobalValue::AppendingLinkage, 1266 llvm::ConstantArray::get(ATy, UsedArray), "llvm.compiler.used"); 1267 NewUsed->setSection("llvm.metadata"); 1268 } 1269