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