1 //===--- BackendUtil.cpp - LLVM Backend Utilities -------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "clang/CodeGen/BackendUtil.h" 10 #include "BackendConsumer.h" 11 #include "LinkInModulesPass.h" 12 #include "clang/Basic/CodeGenOptions.h" 13 #include "clang/Basic/Diagnostic.h" 14 #include "clang/Basic/LangOptions.h" 15 #include "clang/Basic/TargetOptions.h" 16 #include "clang/Frontend/FrontendDiagnostic.h" 17 #include "clang/Frontend/Utils.h" 18 #include "clang/Lex/HeaderSearchOptions.h" 19 #include "llvm/ADT/ScopeExit.h" 20 #include "llvm/ADT/StringExtras.h" 21 #include "llvm/ADT/StringSwitch.h" 22 #include "llvm/Analysis/GlobalsModRef.h" 23 #include "llvm/Analysis/TargetLibraryInfo.h" 24 #include "llvm/Analysis/TargetTransformInfo.h" 25 #include "llvm/Bitcode/BitcodeReader.h" 26 #include "llvm/Bitcode/BitcodeWriter.h" 27 #include "llvm/Bitcode/BitcodeWriterPass.h" 28 #include "llvm/CodeGen/TargetSubtargetInfo.h" 29 #include "llvm/Frontend/Driver/CodeGenOptions.h" 30 #include "llvm/IR/DataLayout.h" 31 #include "llvm/IR/DebugInfo.h" 32 #include "llvm/IR/LegacyPassManager.h" 33 #include "llvm/IR/Module.h" 34 #include "llvm/IR/ModuleSummaryIndex.h" 35 #include "llvm/IR/PassManager.h" 36 #include "llvm/IR/Verifier.h" 37 #include "llvm/IRPrinter/IRPrintingPasses.h" 38 #include "llvm/LTO/LTOBackend.h" 39 #include "llvm/MC/TargetRegistry.h" 40 #include "llvm/Object/OffloadBinary.h" 41 #include "llvm/Passes/PassBuilder.h" 42 #include "llvm/Passes/PassPlugin.h" 43 #include "llvm/Passes/StandardInstrumentations.h" 44 #include "llvm/ProfileData/InstrProfCorrelator.h" 45 #include "llvm/Support/BuryPointer.h" 46 #include "llvm/Support/CommandLine.h" 47 #include "llvm/Support/MemoryBuffer.h" 48 #include "llvm/Support/PrettyStackTrace.h" 49 #include "llvm/Support/Program.h" 50 #include "llvm/Support/TimeProfiler.h" 51 #include "llvm/Support/Timer.h" 52 #include "llvm/Support/ToolOutputFile.h" 53 #include "llvm/Support/VirtualFileSystem.h" 54 #include "llvm/Support/raw_ostream.h" 55 #include "llvm/Target/TargetMachine.h" 56 #include "llvm/Target/TargetOptions.h" 57 #include "llvm/TargetParser/SubtargetFeature.h" 58 #include "llvm/TargetParser/Triple.h" 59 #include "llvm/Transforms/HipStdPar/HipStdPar.h" 60 #include "llvm/Transforms/IPO/EmbedBitcodePass.h" 61 #include "llvm/Transforms/IPO/LowerTypeTests.h" 62 #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h" 63 #include "llvm/Transforms/InstCombine/InstCombine.h" 64 #include "llvm/Transforms/Instrumentation/AddressSanitizer.h" 65 #include "llvm/Transforms/Instrumentation/AddressSanitizerOptions.h" 66 #include "llvm/Transforms/Instrumentation/BoundsChecking.h" 67 #include "llvm/Transforms/Instrumentation/DataFlowSanitizer.h" 68 #include "llvm/Transforms/Instrumentation/GCOVProfiler.h" 69 #include "llvm/Transforms/Instrumentation/HWAddressSanitizer.h" 70 #include "llvm/Transforms/Instrumentation/InstrProfiling.h" 71 #include "llvm/Transforms/Instrumentation/KCFI.h" 72 #include "llvm/Transforms/Instrumentation/LowerAllowCheckPass.h" 73 #include "llvm/Transforms/Instrumentation/MemProfiler.h" 74 #include "llvm/Transforms/Instrumentation/MemorySanitizer.h" 75 #include "llvm/Transforms/Instrumentation/NumericalStabilitySanitizer.h" 76 #include "llvm/Transforms/Instrumentation/PGOInstrumentation.h" 77 #include "llvm/Transforms/Instrumentation/RealtimeSanitizer.h" 78 #include "llvm/Transforms/Instrumentation/SanitizerBinaryMetadata.h" 79 #include "llvm/Transforms/Instrumentation/SanitizerCoverage.h" 80 #include "llvm/Transforms/Instrumentation/ThreadSanitizer.h" 81 #include "llvm/Transforms/Instrumentation/TypeSanitizer.h" 82 #include "llvm/Transforms/ObjCARC.h" 83 #include "llvm/Transforms/Scalar/EarlyCSE.h" 84 #include "llvm/Transforms/Scalar/GVN.h" 85 #include "llvm/Transforms/Scalar/JumpThreading.h" 86 #include "llvm/Transforms/Utils/Debugify.h" 87 #include "llvm/Transforms/Utils/ModuleUtils.h" 88 #include <limits> 89 #include <memory> 90 #include <optional> 91 using namespace clang; 92 using namespace llvm; 93 94 #define HANDLE_EXTENSION(Ext) \ 95 llvm::PassPluginLibraryInfo get##Ext##PluginInfo(); 96 #include "llvm/Support/Extension.def" 97 98 namespace llvm { 99 extern cl::opt<bool> PrintPipelinePasses; 100 101 // Experiment to move sanitizers earlier. 102 static cl::opt<bool> ClSanitizeOnOptimizerEarlyEP( 103 "sanitizer-early-opt-ep", cl::Optional, 104 cl::desc("Insert sanitizers on OptimizerEarlyEP.")); 105 106 // Experiment to mark cold functions as optsize/minsize/optnone. 107 // TODO: remove once this is exposed as a proper driver flag. 108 static cl::opt<PGOOptions::ColdFuncOpt> ClPGOColdFuncAttr( 109 "pgo-cold-func-opt", cl::init(PGOOptions::ColdFuncOpt::Default), cl::Hidden, 110 cl::desc( 111 "Function attribute to apply to cold functions as determined by PGO"), 112 cl::values(clEnumValN(PGOOptions::ColdFuncOpt::Default, "default", 113 "Default (no attribute)"), 114 clEnumValN(PGOOptions::ColdFuncOpt::OptSize, "optsize", 115 "Mark cold functions with optsize."), 116 clEnumValN(PGOOptions::ColdFuncOpt::MinSize, "minsize", 117 "Mark cold functions with minsize."), 118 clEnumValN(PGOOptions::ColdFuncOpt::OptNone, "optnone", 119 "Mark cold functions with optnone."))); 120 121 extern cl::opt<InstrProfCorrelator::ProfCorrelatorKind> ProfileCorrelate; 122 } // namespace llvm 123 namespace clang { 124 extern llvm::cl::opt<bool> ClSanitizeGuardChecks; 125 } 126 127 namespace { 128 129 // Default filename used for profile generation. 130 std::string getDefaultProfileGenName() { 131 return DebugInfoCorrelate || ProfileCorrelate != InstrProfCorrelator::NONE 132 ? "default_%m.proflite" 133 : "default_%m.profraw"; 134 } 135 136 class EmitAssemblyHelper { 137 CompilerInstance &CI; 138 DiagnosticsEngine &Diags; 139 const CodeGenOptions &CodeGenOpts; 140 const clang::TargetOptions &TargetOpts; 141 const LangOptions &LangOpts; 142 llvm::Module *TheModule; 143 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS; 144 145 std::unique_ptr<raw_pwrite_stream> OS; 146 147 Triple TargetTriple; 148 149 TargetIRAnalysis getTargetIRAnalysis() const { 150 if (TM) 151 return TM->getTargetIRAnalysis(); 152 153 return TargetIRAnalysis(); 154 } 155 156 /// Generates the TargetMachine. 157 /// Leaves TM unchanged if it is unable to create the target machine. 158 /// Some of our clang tests specify triples which are not built 159 /// into clang. This is okay because these tests check the generated 160 /// IR, and they require DataLayout which depends on the triple. 161 /// In this case, we allow this method to fail and not report an error. 162 /// When MustCreateTM is used, we print an error if we are unable to load 163 /// the requested target. 164 void CreateTargetMachine(bool MustCreateTM); 165 166 /// Add passes necessary to emit assembly or LLVM IR. 167 /// 168 /// \return True on success. 169 bool AddEmitPasses(legacy::PassManager &CodeGenPasses, BackendAction Action, 170 raw_pwrite_stream &OS, raw_pwrite_stream *DwoOS); 171 172 std::unique_ptr<llvm::ToolOutputFile> openOutputFile(StringRef Path) { 173 std::error_code EC; 174 auto F = std::make_unique<llvm::ToolOutputFile>(Path, EC, 175 llvm::sys::fs::OF_None); 176 if (EC) { 177 Diags.Report(diag::err_fe_unable_to_open_output) << Path << EC.message(); 178 F.reset(); 179 } 180 return F; 181 } 182 183 void RunOptimizationPipeline( 184 BackendAction Action, std::unique_ptr<raw_pwrite_stream> &OS, 185 std::unique_ptr<llvm::ToolOutputFile> &ThinLinkOS, BackendConsumer *BC); 186 void RunCodegenPipeline(BackendAction Action, 187 std::unique_ptr<raw_pwrite_stream> &OS, 188 std::unique_ptr<llvm::ToolOutputFile> &DwoOS); 189 190 /// Check whether we should emit a module summary for regular LTO. 191 /// The module summary should be emitted by default for regular LTO 192 /// except for ld64 targets. 193 /// 194 /// \return True if the module summary should be emitted. 195 bool shouldEmitRegularLTOSummary() const { 196 return CodeGenOpts.PrepareForLTO && !CodeGenOpts.DisableLLVMPasses && 197 TargetTriple.getVendor() != llvm::Triple::Apple; 198 } 199 200 /// Check whether we should emit a flag for UnifiedLTO. 201 /// The UnifiedLTO module flag should be set when UnifiedLTO is enabled for 202 /// ThinLTO or Full LTO with module summaries. 203 bool shouldEmitUnifiedLTOModueFlag() const { 204 return CodeGenOpts.UnifiedLTO && 205 (CodeGenOpts.PrepareForThinLTO || shouldEmitRegularLTOSummary()); 206 } 207 208 public: 209 EmitAssemblyHelper(CompilerInstance &CI, CodeGenOptions &CGOpts, 210 llvm::Module *M, 211 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) 212 : CI(CI), Diags(CI.getDiagnostics()), CodeGenOpts(CGOpts), 213 TargetOpts(CI.getTargetOpts()), LangOpts(CI.getLangOpts()), 214 TheModule(M), VFS(std::move(VFS)), 215 TargetTriple(TheModule->getTargetTriple()) {} 216 217 ~EmitAssemblyHelper() { 218 if (CodeGenOpts.DisableFree) 219 BuryPointer(std::move(TM)); 220 } 221 222 std::unique_ptr<TargetMachine> TM; 223 224 // Emit output using the new pass manager for the optimization pipeline. 225 void emitAssembly(BackendAction Action, std::unique_ptr<raw_pwrite_stream> OS, 226 BackendConsumer *BC); 227 }; 228 } // namespace 229 230 static SanitizerCoverageOptions 231 getSancovOptsFromCGOpts(const CodeGenOptions &CGOpts) { 232 SanitizerCoverageOptions Opts; 233 Opts.CoverageType = 234 static_cast<SanitizerCoverageOptions::Type>(CGOpts.SanitizeCoverageType); 235 Opts.IndirectCalls = CGOpts.SanitizeCoverageIndirectCalls; 236 Opts.TraceBB = CGOpts.SanitizeCoverageTraceBB; 237 Opts.TraceCmp = CGOpts.SanitizeCoverageTraceCmp; 238 Opts.TraceDiv = CGOpts.SanitizeCoverageTraceDiv; 239 Opts.TraceGep = CGOpts.SanitizeCoverageTraceGep; 240 Opts.Use8bitCounters = CGOpts.SanitizeCoverage8bitCounters; 241 Opts.TracePC = CGOpts.SanitizeCoverageTracePC; 242 Opts.TracePCGuard = CGOpts.SanitizeCoverageTracePCGuard; 243 Opts.NoPrune = CGOpts.SanitizeCoverageNoPrune; 244 Opts.Inline8bitCounters = CGOpts.SanitizeCoverageInline8bitCounters; 245 Opts.InlineBoolFlag = CGOpts.SanitizeCoverageInlineBoolFlag; 246 Opts.PCTable = CGOpts.SanitizeCoveragePCTable; 247 Opts.StackDepth = CGOpts.SanitizeCoverageStackDepth; 248 Opts.TraceLoads = CGOpts.SanitizeCoverageTraceLoads; 249 Opts.TraceStores = CGOpts.SanitizeCoverageTraceStores; 250 Opts.CollectControlFlow = CGOpts.SanitizeCoverageControlFlow; 251 return Opts; 252 } 253 254 static SanitizerBinaryMetadataOptions 255 getSanitizerBinaryMetadataOptions(const CodeGenOptions &CGOpts) { 256 SanitizerBinaryMetadataOptions Opts; 257 Opts.Covered = CGOpts.SanitizeBinaryMetadataCovered; 258 Opts.Atomics = CGOpts.SanitizeBinaryMetadataAtomics; 259 Opts.UAR = CGOpts.SanitizeBinaryMetadataUAR; 260 return Opts; 261 } 262 263 // Check if ASan should use GC-friendly instrumentation for globals. 264 // First of all, there is no point if -fdata-sections is off (expect for MachO, 265 // where this is not a factor). Also, on ELF this feature requires an assembler 266 // extension that only works with -integrated-as at the moment. 267 static bool asanUseGlobalsGC(const Triple &T, const CodeGenOptions &CGOpts) { 268 if (!CGOpts.SanitizeAddressGlobalsDeadStripping) 269 return false; 270 switch (T.getObjectFormat()) { 271 case Triple::MachO: 272 case Triple::COFF: 273 return true; 274 case Triple::ELF: 275 return !CGOpts.DisableIntegratedAS; 276 case Triple::GOFF: 277 llvm::report_fatal_error("ASan not implemented for GOFF"); 278 case Triple::XCOFF: 279 llvm::report_fatal_error("ASan not implemented for XCOFF."); 280 case Triple::Wasm: 281 case Triple::DXContainer: 282 case Triple::SPIRV: 283 case Triple::UnknownObjectFormat: 284 break; 285 } 286 return false; 287 } 288 289 static std::optional<llvm::CodeModel::Model> 290 getCodeModel(const CodeGenOptions &CodeGenOpts) { 291 unsigned CodeModel = llvm::StringSwitch<unsigned>(CodeGenOpts.CodeModel) 292 .Case("tiny", llvm::CodeModel::Tiny) 293 .Case("small", llvm::CodeModel::Small) 294 .Case("kernel", llvm::CodeModel::Kernel) 295 .Case("medium", llvm::CodeModel::Medium) 296 .Case("large", llvm::CodeModel::Large) 297 .Case("default", ~1u) 298 .Default(~0u); 299 assert(CodeModel != ~0u && "invalid code model!"); 300 if (CodeModel == ~1u) 301 return std::nullopt; 302 return static_cast<llvm::CodeModel::Model>(CodeModel); 303 } 304 305 static CodeGenFileType getCodeGenFileType(BackendAction Action) { 306 if (Action == Backend_EmitObj) 307 return CodeGenFileType::ObjectFile; 308 else if (Action == Backend_EmitMCNull) 309 return CodeGenFileType::Null; 310 else { 311 assert(Action == Backend_EmitAssembly && "Invalid action!"); 312 return CodeGenFileType::AssemblyFile; 313 } 314 } 315 316 static bool actionRequiresCodeGen(BackendAction Action) { 317 return Action != Backend_EmitNothing && Action != Backend_EmitBC && 318 Action != Backend_EmitLL; 319 } 320 321 static std::string flattenClangCommandLine(ArrayRef<std::string> Args, 322 StringRef MainFilename) { 323 if (Args.empty()) 324 return std::string{}; 325 326 std::string FlatCmdLine; 327 raw_string_ostream OS(FlatCmdLine); 328 bool PrintedOneArg = false; 329 if (!StringRef(Args[0]).contains("-cc1")) { 330 llvm::sys::printArg(OS, "-cc1", /*Quote=*/true); 331 PrintedOneArg = true; 332 } 333 for (unsigned i = 0; i < Args.size(); i++) { 334 StringRef Arg = Args[i]; 335 if (Arg.empty()) 336 continue; 337 if (Arg == "-main-file-name" || Arg == "-o") { 338 i++; // Skip this argument and next one. 339 continue; 340 } 341 if (Arg.starts_with("-object-file-name") || Arg == MainFilename) 342 continue; 343 // Skip fmessage-length for reproducibility. 344 if (Arg.starts_with("-fmessage-length")) 345 continue; 346 if (PrintedOneArg) 347 OS << " "; 348 llvm::sys::printArg(OS, Arg, /*Quote=*/true); 349 PrintedOneArg = true; 350 } 351 return FlatCmdLine; 352 } 353 354 static bool initTargetOptions(const CompilerInstance &CI, 355 DiagnosticsEngine &Diags, 356 llvm::TargetOptions &Options) { 357 const auto &CodeGenOpts = CI.getCodeGenOpts(); 358 const auto &TargetOpts = CI.getTargetOpts(); 359 const auto &LangOpts = CI.getLangOpts(); 360 const auto &HSOpts = CI.getHeaderSearchOpts(); 361 switch (LangOpts.getThreadModel()) { 362 case LangOptions::ThreadModelKind::POSIX: 363 Options.ThreadModel = llvm::ThreadModel::POSIX; 364 break; 365 case LangOptions::ThreadModelKind::Single: 366 Options.ThreadModel = llvm::ThreadModel::Single; 367 break; 368 } 369 370 // Set float ABI type. 371 assert((CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp" || 372 CodeGenOpts.FloatABI == "hard" || CodeGenOpts.FloatABI.empty()) && 373 "Invalid Floating Point ABI!"); 374 Options.FloatABIType = 375 llvm::StringSwitch<llvm::FloatABI::ABIType>(CodeGenOpts.FloatABI) 376 .Case("soft", llvm::FloatABI::Soft) 377 .Case("softfp", llvm::FloatABI::Soft) 378 .Case("hard", llvm::FloatABI::Hard) 379 .Default(llvm::FloatABI::Default); 380 381 // Set FP fusion mode. 382 switch (LangOpts.getDefaultFPContractMode()) { 383 case LangOptions::FPM_Off: 384 // Preserve any contraction performed by the front-end. (Strict performs 385 // splitting of the muladd intrinsic in the backend.) 386 Options.AllowFPOpFusion = llvm::FPOpFusion::Standard; 387 break; 388 case LangOptions::FPM_On: 389 case LangOptions::FPM_FastHonorPragmas: 390 Options.AllowFPOpFusion = llvm::FPOpFusion::Standard; 391 break; 392 case LangOptions::FPM_Fast: 393 Options.AllowFPOpFusion = llvm::FPOpFusion::Fast; 394 break; 395 } 396 397 Options.BinutilsVersion = 398 llvm::TargetMachine::parseBinutilsVersion(CodeGenOpts.BinutilsVersion); 399 Options.UseInitArray = CodeGenOpts.UseInitArray; 400 Options.DisableIntegratedAS = CodeGenOpts.DisableIntegratedAS; 401 402 // Set EABI version. 403 Options.EABIVersion = TargetOpts.EABIVersion; 404 405 if (LangOpts.hasSjLjExceptions()) 406 Options.ExceptionModel = llvm::ExceptionHandling::SjLj; 407 if (LangOpts.hasSEHExceptions()) 408 Options.ExceptionModel = llvm::ExceptionHandling::WinEH; 409 if (LangOpts.hasDWARFExceptions()) 410 Options.ExceptionModel = llvm::ExceptionHandling::DwarfCFI; 411 if (LangOpts.hasWasmExceptions()) 412 Options.ExceptionModel = llvm::ExceptionHandling::Wasm; 413 414 Options.NoInfsFPMath = LangOpts.NoHonorInfs; 415 Options.NoNaNsFPMath = LangOpts.NoHonorNaNs; 416 Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS; 417 Options.UnsafeFPMath = LangOpts.AllowFPReassoc && LangOpts.AllowRecip && 418 LangOpts.NoSignedZero && LangOpts.ApproxFunc && 419 (LangOpts.getDefaultFPContractMode() == 420 LangOptions::FPModeKind::FPM_Fast || 421 LangOpts.getDefaultFPContractMode() == 422 LangOptions::FPModeKind::FPM_FastHonorPragmas); 423 Options.ApproxFuncFPMath = LangOpts.ApproxFunc; 424 425 Options.BBAddrMap = CodeGenOpts.BBAddrMap; 426 Options.BBSections = 427 llvm::StringSwitch<llvm::BasicBlockSection>(CodeGenOpts.BBSections) 428 .Case("all", llvm::BasicBlockSection::All) 429 .StartsWith("list=", llvm::BasicBlockSection::List) 430 .Case("none", llvm::BasicBlockSection::None) 431 .Default(llvm::BasicBlockSection::None); 432 433 if (Options.BBSections == llvm::BasicBlockSection::List) { 434 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = 435 MemoryBuffer::getFile(CodeGenOpts.BBSections.substr(5)); 436 if (!MBOrErr) { 437 Diags.Report(diag::err_fe_unable_to_load_basic_block_sections_file) 438 << MBOrErr.getError().message(); 439 return false; 440 } 441 Options.BBSectionsFuncListBuf = std::move(*MBOrErr); 442 } 443 444 Options.EnableMachineFunctionSplitter = CodeGenOpts.SplitMachineFunctions; 445 Options.FunctionSections = CodeGenOpts.FunctionSections; 446 Options.DataSections = CodeGenOpts.DataSections; 447 Options.IgnoreXCOFFVisibility = LangOpts.IgnoreXCOFFVisibility; 448 Options.UniqueSectionNames = CodeGenOpts.UniqueSectionNames; 449 Options.UniqueBasicBlockSectionNames = 450 CodeGenOpts.UniqueBasicBlockSectionNames; 451 Options.SeparateNamedSections = CodeGenOpts.SeparateNamedSections; 452 Options.TLSSize = CodeGenOpts.TLSSize; 453 Options.EnableTLSDESC = CodeGenOpts.EnableTLSDESC; 454 Options.EmulatedTLS = CodeGenOpts.EmulatedTLS; 455 Options.DebuggerTuning = CodeGenOpts.getDebuggerTuning(); 456 Options.EmitStackSizeSection = CodeGenOpts.StackSizeSection; 457 Options.StackUsageOutput = CodeGenOpts.StackUsageOutput; 458 Options.EmitAddrsig = CodeGenOpts.Addrsig; 459 Options.ForceDwarfFrameSection = CodeGenOpts.ForceDwarfFrameSection; 460 Options.EmitCallSiteInfo = CodeGenOpts.EmitCallSiteInfo; 461 Options.EnableAIXExtendedAltivecABI = LangOpts.EnableAIXExtendedAltivecABI; 462 Options.XRayFunctionIndex = CodeGenOpts.XRayFunctionIndex; 463 Options.LoopAlignment = CodeGenOpts.LoopAlignment; 464 Options.DebugStrictDwarf = CodeGenOpts.DebugStrictDwarf; 465 Options.ObjectFilenameForDebug = CodeGenOpts.ObjectFilenameForDebug; 466 Options.Hotpatch = CodeGenOpts.HotPatch; 467 Options.JMCInstrument = CodeGenOpts.JMCInstrument; 468 Options.XCOFFReadOnlyPointers = CodeGenOpts.XCOFFReadOnlyPointers; 469 470 switch (CodeGenOpts.getSwiftAsyncFramePointer()) { 471 case CodeGenOptions::SwiftAsyncFramePointerKind::Auto: 472 Options.SwiftAsyncFramePointer = 473 SwiftAsyncFramePointerMode::DeploymentBased; 474 break; 475 476 case CodeGenOptions::SwiftAsyncFramePointerKind::Always: 477 Options.SwiftAsyncFramePointer = SwiftAsyncFramePointerMode::Always; 478 break; 479 480 case CodeGenOptions::SwiftAsyncFramePointerKind::Never: 481 Options.SwiftAsyncFramePointer = SwiftAsyncFramePointerMode::Never; 482 break; 483 } 484 485 Options.MCOptions.SplitDwarfFile = CodeGenOpts.SplitDwarfFile; 486 Options.MCOptions.EmitDwarfUnwind = CodeGenOpts.getEmitDwarfUnwind(); 487 Options.MCOptions.EmitCompactUnwindNonCanonical = 488 CodeGenOpts.EmitCompactUnwindNonCanonical; 489 Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll; 490 Options.MCOptions.MCSaveTempLabels = CodeGenOpts.SaveTempLabels; 491 Options.MCOptions.MCUseDwarfDirectory = 492 CodeGenOpts.NoDwarfDirectoryAsm 493 ? llvm::MCTargetOptions::DisableDwarfDirectory 494 : llvm::MCTargetOptions::EnableDwarfDirectory; 495 Options.MCOptions.MCNoExecStack = CodeGenOpts.NoExecStack; 496 Options.MCOptions.MCIncrementalLinkerCompatible = 497 CodeGenOpts.IncrementalLinkerCompatible; 498 Options.MCOptions.MCFatalWarnings = CodeGenOpts.FatalWarnings; 499 Options.MCOptions.MCNoWarn = CodeGenOpts.NoWarn; 500 Options.MCOptions.AsmVerbose = CodeGenOpts.AsmVerbose; 501 Options.MCOptions.Dwarf64 = CodeGenOpts.Dwarf64; 502 Options.MCOptions.PreserveAsmComments = CodeGenOpts.PreserveAsmComments; 503 Options.MCOptions.Crel = CodeGenOpts.Crel; 504 Options.MCOptions.ImplicitMapSyms = CodeGenOpts.ImplicitMapSyms; 505 Options.MCOptions.X86RelaxRelocations = CodeGenOpts.X86RelaxRelocations; 506 Options.MCOptions.CompressDebugSections = 507 CodeGenOpts.getCompressDebugSections(); 508 if (CodeGenOpts.OutputAsmVariant != 3) // 3 (default): not specified 509 Options.MCOptions.OutputAsmVariant = CodeGenOpts.OutputAsmVariant; 510 Options.MCOptions.ABIName = TargetOpts.ABI; 511 for (const auto &Entry : HSOpts.UserEntries) 512 if (!Entry.IsFramework && 513 (Entry.Group == frontend::IncludeDirGroup::Quoted || 514 Entry.Group == frontend::IncludeDirGroup::Angled || 515 Entry.Group == frontend::IncludeDirGroup::System)) 516 Options.MCOptions.IASSearchPaths.push_back( 517 Entry.IgnoreSysRoot ? Entry.Path : HSOpts.Sysroot + Entry.Path); 518 Options.MCOptions.Argv0 = CodeGenOpts.Argv0 ? CodeGenOpts.Argv0 : ""; 519 Options.MCOptions.CommandlineArgs = flattenClangCommandLine( 520 CodeGenOpts.CommandLineArgs, CodeGenOpts.MainFileName); 521 Options.MCOptions.AsSecureLogFile = CodeGenOpts.AsSecureLogFile; 522 Options.MCOptions.PPCUseFullRegisterNames = 523 CodeGenOpts.PPCUseFullRegisterNames; 524 Options.MisExpect = CodeGenOpts.MisExpect; 525 526 return true; 527 } 528 529 static std::optional<GCOVOptions> 530 getGCOVOptions(const CodeGenOptions &CodeGenOpts, const LangOptions &LangOpts) { 531 if (CodeGenOpts.CoverageNotesFile.empty() && 532 CodeGenOpts.CoverageDataFile.empty()) 533 return std::nullopt; 534 // Not using 'GCOVOptions::getDefault' allows us to avoid exiting if 535 // LLVM's -default-gcov-version flag is set to something invalid. 536 GCOVOptions Options; 537 Options.EmitNotes = !CodeGenOpts.CoverageNotesFile.empty(); 538 Options.EmitData = !CodeGenOpts.CoverageDataFile.empty(); 539 llvm::copy(CodeGenOpts.CoverageVersion, std::begin(Options.Version)); 540 Options.NoRedZone = CodeGenOpts.DisableRedZone; 541 Options.Filter = CodeGenOpts.ProfileFilterFiles; 542 Options.Exclude = CodeGenOpts.ProfileExcludeFiles; 543 Options.Atomic = CodeGenOpts.AtomicProfileUpdate; 544 return Options; 545 } 546 547 static std::optional<InstrProfOptions> 548 getInstrProfOptions(const CodeGenOptions &CodeGenOpts, 549 const LangOptions &LangOpts) { 550 if (!CodeGenOpts.hasProfileClangInstr()) 551 return std::nullopt; 552 InstrProfOptions Options; 553 Options.NoRedZone = CodeGenOpts.DisableRedZone; 554 Options.InstrProfileOutput = CodeGenOpts.InstrProfileOutput; 555 Options.Atomic = CodeGenOpts.AtomicProfileUpdate; 556 return Options; 557 } 558 559 static void setCommandLineOpts(const CodeGenOptions &CodeGenOpts) { 560 SmallVector<const char *, 16> BackendArgs; 561 BackendArgs.push_back("clang"); // Fake program name. 562 if (!CodeGenOpts.DebugPass.empty()) { 563 BackendArgs.push_back("-debug-pass"); 564 BackendArgs.push_back(CodeGenOpts.DebugPass.c_str()); 565 } 566 if (!CodeGenOpts.LimitFloatPrecision.empty()) { 567 BackendArgs.push_back("-limit-float-precision"); 568 BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str()); 569 } 570 // Check for the default "clang" invocation that won't set any cl::opt values. 571 // Skip trying to parse the command line invocation to avoid the issues 572 // described below. 573 if (BackendArgs.size() == 1) 574 return; 575 BackendArgs.push_back(nullptr); 576 // FIXME: The command line parser below is not thread-safe and shares a global 577 // state, so this call might crash or overwrite the options of another Clang 578 // instance in the same process. 579 llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1, 580 BackendArgs.data()); 581 } 582 583 void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) { 584 // Create the TargetMachine for generating code. 585 std::string Error; 586 std::string Triple = TheModule->getTargetTriple(); 587 const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error); 588 if (!TheTarget) { 589 if (MustCreateTM) 590 Diags.Report(diag::err_fe_unable_to_create_target) << Error; 591 return; 592 } 593 594 std::optional<llvm::CodeModel::Model> CM = getCodeModel(CodeGenOpts); 595 std::string FeaturesStr = 596 llvm::join(TargetOpts.Features.begin(), TargetOpts.Features.end(), ","); 597 llvm::Reloc::Model RM = CodeGenOpts.RelocationModel; 598 std::optional<CodeGenOptLevel> OptLevelOrNone = 599 CodeGenOpt::getLevel(CodeGenOpts.OptimizationLevel); 600 assert(OptLevelOrNone && "Invalid optimization level!"); 601 CodeGenOptLevel OptLevel = *OptLevelOrNone; 602 603 llvm::TargetOptions Options; 604 if (!initTargetOptions(CI, Diags, Options)) 605 return; 606 TM.reset(TheTarget->createTargetMachine(Triple, TargetOpts.CPU, FeaturesStr, 607 Options, RM, CM, OptLevel)); 608 TM->setLargeDataThreshold(CodeGenOpts.LargeDataThreshold); 609 } 610 611 bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager &CodeGenPasses, 612 BackendAction Action, 613 raw_pwrite_stream &OS, 614 raw_pwrite_stream *DwoOS) { 615 // Add LibraryInfo. 616 std::unique_ptr<TargetLibraryInfoImpl> TLII( 617 llvm::driver::createTLII(TargetTriple, CodeGenOpts.getVecLib())); 618 CodeGenPasses.add(new TargetLibraryInfoWrapperPass(*TLII)); 619 620 // Normal mode, emit a .s or .o file by running the code generator. Note, 621 // this also adds codegenerator level optimization passes. 622 CodeGenFileType CGFT = getCodeGenFileType(Action); 623 624 if (TM->addPassesToEmitFile(CodeGenPasses, OS, DwoOS, CGFT, 625 /*DisableVerify=*/!CodeGenOpts.VerifyModule)) { 626 Diags.Report(diag::err_fe_unable_to_interface_with_target); 627 return false; 628 } 629 630 return true; 631 } 632 633 static OptimizationLevel mapToLevel(const CodeGenOptions &Opts) { 634 switch (Opts.OptimizationLevel) { 635 default: 636 llvm_unreachable("Invalid optimization level!"); 637 638 case 0: 639 return OptimizationLevel::O0; 640 641 case 1: 642 return OptimizationLevel::O1; 643 644 case 2: 645 switch (Opts.OptimizeSize) { 646 default: 647 llvm_unreachable("Invalid optimization level for size!"); 648 649 case 0: 650 return OptimizationLevel::O2; 651 652 case 1: 653 return OptimizationLevel::Os; 654 655 case 2: 656 return OptimizationLevel::Oz; 657 } 658 659 case 3: 660 return OptimizationLevel::O3; 661 } 662 } 663 664 static void addKCFIPass(const Triple &TargetTriple, const LangOptions &LangOpts, 665 PassBuilder &PB) { 666 // If the back-end supports KCFI operand bundle lowering, skip KCFIPass. 667 if (TargetTriple.getArch() == llvm::Triple::x86_64 || 668 TargetTriple.isAArch64(64) || TargetTriple.isRISCV()) 669 return; 670 671 // Ensure we lower KCFI operand bundles with -O0. 672 PB.registerOptimizerLastEPCallback( 673 [&](ModulePassManager &MPM, OptimizationLevel Level, ThinOrFullLTOPhase) { 674 if (Level == OptimizationLevel::O0 && 675 LangOpts.Sanitize.has(SanitizerKind::KCFI)) 676 MPM.addPass(createModuleToFunctionPassAdaptor(KCFIPass())); 677 }); 678 679 // When optimizations are requested, run KCIFPass after InstCombine to 680 // avoid unnecessary checks. 681 PB.registerPeepholeEPCallback( 682 [&](FunctionPassManager &FPM, OptimizationLevel Level) { 683 if (Level != OptimizationLevel::O0 && 684 LangOpts.Sanitize.has(SanitizerKind::KCFI)) 685 FPM.addPass(KCFIPass()); 686 }); 687 } 688 689 static void addSanitizers(const Triple &TargetTriple, 690 const CodeGenOptions &CodeGenOpts, 691 const LangOptions &LangOpts, PassBuilder &PB) { 692 auto SanitizersCallback = [&](ModulePassManager &MPM, OptimizationLevel Level, 693 ThinOrFullLTOPhase) { 694 if (CodeGenOpts.hasSanitizeCoverage()) { 695 auto SancovOpts = getSancovOptsFromCGOpts(CodeGenOpts); 696 MPM.addPass(SanitizerCoveragePass( 697 SancovOpts, CodeGenOpts.SanitizeCoverageAllowlistFiles, 698 CodeGenOpts.SanitizeCoverageIgnorelistFiles)); 699 } 700 701 if (CodeGenOpts.hasSanitizeBinaryMetadata()) { 702 MPM.addPass(SanitizerBinaryMetadataPass( 703 getSanitizerBinaryMetadataOptions(CodeGenOpts), 704 CodeGenOpts.SanitizeMetadataIgnorelistFiles)); 705 } 706 707 auto MSanPass = [&](SanitizerMask Mask, bool CompileKernel) { 708 if (LangOpts.Sanitize.has(Mask)) { 709 int TrackOrigins = CodeGenOpts.SanitizeMemoryTrackOrigins; 710 bool Recover = CodeGenOpts.SanitizeRecover.has(Mask); 711 712 MemorySanitizerOptions options(TrackOrigins, Recover, CompileKernel, 713 CodeGenOpts.SanitizeMemoryParamRetval); 714 MPM.addPass(MemorySanitizerPass(options)); 715 if (Level != OptimizationLevel::O0) { 716 // MemorySanitizer inserts complex instrumentation that mostly follows 717 // the logic of the original code, but operates on "shadow" values. It 718 // can benefit from re-running some general purpose optimization 719 // passes. 720 MPM.addPass(RequireAnalysisPass<GlobalsAA, llvm::Module>()); 721 FunctionPassManager FPM; 722 FPM.addPass(EarlyCSEPass(true /* Enable mem-ssa. */)); 723 FPM.addPass(InstCombinePass()); 724 FPM.addPass(JumpThreadingPass()); 725 FPM.addPass(GVNPass()); 726 FPM.addPass(InstCombinePass()); 727 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); 728 } 729 } 730 }; 731 MSanPass(SanitizerKind::Memory, false); 732 MSanPass(SanitizerKind::KernelMemory, true); 733 734 if (LangOpts.Sanitize.has(SanitizerKind::Thread)) { 735 MPM.addPass(ModuleThreadSanitizerPass()); 736 MPM.addPass(createModuleToFunctionPassAdaptor(ThreadSanitizerPass())); 737 } 738 739 if (LangOpts.Sanitize.has(SanitizerKind::Type)) 740 MPM.addPass(TypeSanitizerPass()); 741 742 if (LangOpts.Sanitize.has(SanitizerKind::NumericalStability)) 743 MPM.addPass(NumericalStabilitySanitizerPass()); 744 745 if (LangOpts.Sanitize.has(SanitizerKind::Realtime)) 746 MPM.addPass(RealtimeSanitizerPass()); 747 748 auto ASanPass = [&](SanitizerMask Mask, bool CompileKernel) { 749 if (LangOpts.Sanitize.has(Mask)) { 750 bool UseGlobalGC = asanUseGlobalsGC(TargetTriple, CodeGenOpts); 751 bool UseOdrIndicator = CodeGenOpts.SanitizeAddressUseOdrIndicator; 752 llvm::AsanDtorKind DestructorKind = 753 CodeGenOpts.getSanitizeAddressDtor(); 754 AddressSanitizerOptions Opts; 755 Opts.CompileKernel = CompileKernel; 756 Opts.Recover = CodeGenOpts.SanitizeRecover.has(Mask); 757 Opts.UseAfterScope = CodeGenOpts.SanitizeAddressUseAfterScope; 758 Opts.UseAfterReturn = CodeGenOpts.getSanitizeAddressUseAfterReturn(); 759 MPM.addPass(AddressSanitizerPass(Opts, UseGlobalGC, UseOdrIndicator, 760 DestructorKind)); 761 } 762 }; 763 ASanPass(SanitizerKind::Address, false); 764 ASanPass(SanitizerKind::KernelAddress, true); 765 766 auto HWASanPass = [&](SanitizerMask Mask, bool CompileKernel) { 767 if (LangOpts.Sanitize.has(Mask)) { 768 bool Recover = CodeGenOpts.SanitizeRecover.has(Mask); 769 MPM.addPass(HWAddressSanitizerPass( 770 {CompileKernel, Recover, 771 /*DisableOptimization=*/CodeGenOpts.OptimizationLevel == 0})); 772 } 773 }; 774 HWASanPass(SanitizerKind::HWAddress, false); 775 HWASanPass(SanitizerKind::KernelHWAddress, true); 776 777 if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) { 778 MPM.addPass(DataFlowSanitizerPass(LangOpts.NoSanitizeFiles)); 779 } 780 }; 781 if (ClSanitizeOnOptimizerEarlyEP) { 782 PB.registerOptimizerEarlyEPCallback( 783 [SanitizersCallback](ModulePassManager &MPM, OptimizationLevel Level, 784 ThinOrFullLTOPhase Phase) { 785 ModulePassManager NewMPM; 786 SanitizersCallback(NewMPM, Level, Phase); 787 if (!NewMPM.isEmpty()) { 788 // Sanitizers can abandon<GlobalsAA>. 789 NewMPM.addPass(RequireAnalysisPass<GlobalsAA, llvm::Module>()); 790 MPM.addPass(std::move(NewMPM)); 791 } 792 }); 793 } else { 794 // LastEP does not need GlobalsAA. 795 PB.registerOptimizerLastEPCallback(SanitizersCallback); 796 } 797 798 if (LowerAllowCheckPass::IsRequested()) { 799 // We want to call it after inline, which is about OptimizerEarlyEPCallback. 800 PB.registerOptimizerEarlyEPCallback([&](ModulePassManager &MPM, 801 OptimizationLevel Level, 802 ThinOrFullLTOPhase Phase) { 803 LowerAllowCheckPass::Options Opts; 804 MPM.addPass(createModuleToFunctionPassAdaptor(LowerAllowCheckPass(Opts))); 805 }); 806 } 807 } 808 809 void EmitAssemblyHelper::RunOptimizationPipeline( 810 BackendAction Action, std::unique_ptr<raw_pwrite_stream> &OS, 811 std::unique_ptr<llvm::ToolOutputFile> &ThinLinkOS, BackendConsumer *BC) { 812 std::optional<PGOOptions> PGOOpt; 813 814 if (CodeGenOpts.hasProfileIRInstr()) 815 // -fprofile-generate. 816 PGOOpt = PGOOptions( 817 CodeGenOpts.InstrProfileOutput.empty() ? getDefaultProfileGenName() 818 : CodeGenOpts.InstrProfileOutput, 819 "", "", CodeGenOpts.MemoryProfileUsePath, nullptr, PGOOptions::IRInstr, 820 PGOOptions::NoCSAction, ClPGOColdFuncAttr, 821 CodeGenOpts.DebugInfoForProfiling, 822 /*PseudoProbeForProfiling=*/false, CodeGenOpts.AtomicProfileUpdate); 823 else if (CodeGenOpts.hasProfileIRUse()) { 824 // -fprofile-use. 825 auto CSAction = CodeGenOpts.hasProfileCSIRUse() ? PGOOptions::CSIRUse 826 : PGOOptions::NoCSAction; 827 PGOOpt = PGOOptions(CodeGenOpts.ProfileInstrumentUsePath, "", 828 CodeGenOpts.ProfileRemappingFile, 829 CodeGenOpts.MemoryProfileUsePath, VFS, 830 PGOOptions::IRUse, CSAction, ClPGOColdFuncAttr, 831 CodeGenOpts.DebugInfoForProfiling); 832 } else if (!CodeGenOpts.SampleProfileFile.empty()) 833 // -fprofile-sample-use 834 PGOOpt = PGOOptions( 835 CodeGenOpts.SampleProfileFile, "", CodeGenOpts.ProfileRemappingFile, 836 CodeGenOpts.MemoryProfileUsePath, VFS, PGOOptions::SampleUse, 837 PGOOptions::NoCSAction, ClPGOColdFuncAttr, 838 CodeGenOpts.DebugInfoForProfiling, CodeGenOpts.PseudoProbeForProfiling); 839 else if (!CodeGenOpts.MemoryProfileUsePath.empty()) 840 // -fmemory-profile-use (without any of the above options) 841 PGOOpt = PGOOptions("", "", "", CodeGenOpts.MemoryProfileUsePath, VFS, 842 PGOOptions::NoAction, PGOOptions::NoCSAction, 843 ClPGOColdFuncAttr, CodeGenOpts.DebugInfoForProfiling); 844 else if (CodeGenOpts.PseudoProbeForProfiling) 845 // -fpseudo-probe-for-profiling 846 PGOOpt = 847 PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr, 848 PGOOptions::NoAction, PGOOptions::NoCSAction, 849 ClPGOColdFuncAttr, CodeGenOpts.DebugInfoForProfiling, true); 850 else if (CodeGenOpts.DebugInfoForProfiling) 851 // -fdebug-info-for-profiling 852 PGOOpt = PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr, 853 PGOOptions::NoAction, PGOOptions::NoCSAction, 854 ClPGOColdFuncAttr, true); 855 856 // Check to see if we want to generate a CS profile. 857 if (CodeGenOpts.hasProfileCSIRInstr()) { 858 assert(!CodeGenOpts.hasProfileCSIRUse() && 859 "Cannot have both CSProfileUse pass and CSProfileGen pass at " 860 "the same time"); 861 if (PGOOpt) { 862 assert(PGOOpt->Action != PGOOptions::IRInstr && 863 PGOOpt->Action != PGOOptions::SampleUse && 864 "Cannot run CSProfileGen pass with ProfileGen or SampleUse " 865 " pass"); 866 PGOOpt->CSProfileGenFile = CodeGenOpts.InstrProfileOutput.empty() 867 ? getDefaultProfileGenName() 868 : CodeGenOpts.InstrProfileOutput; 869 PGOOpt->CSAction = PGOOptions::CSIRInstr; 870 } else 871 PGOOpt = PGOOptions("", 872 CodeGenOpts.InstrProfileOutput.empty() 873 ? getDefaultProfileGenName() 874 : CodeGenOpts.InstrProfileOutput, 875 "", /*MemoryProfile=*/"", nullptr, 876 PGOOptions::NoAction, PGOOptions::CSIRInstr, 877 ClPGOColdFuncAttr, CodeGenOpts.DebugInfoForProfiling); 878 } 879 if (TM) 880 TM->setPGOOption(PGOOpt); 881 882 PipelineTuningOptions PTO; 883 PTO.LoopUnrolling = CodeGenOpts.UnrollLoops; 884 // For historical reasons, loop interleaving is set to mirror setting for loop 885 // unrolling. 886 PTO.LoopInterleaving = CodeGenOpts.UnrollLoops; 887 PTO.LoopVectorization = CodeGenOpts.VectorizeLoop; 888 PTO.SLPVectorization = CodeGenOpts.VectorizeSLP; 889 PTO.MergeFunctions = CodeGenOpts.MergeFunctions; 890 // Only enable CGProfilePass when using integrated assembler, since 891 // non-integrated assemblers don't recognize .cgprofile section. 892 PTO.CallGraphProfile = !CodeGenOpts.DisableIntegratedAS; 893 PTO.UnifiedLTO = CodeGenOpts.UnifiedLTO; 894 895 LoopAnalysisManager LAM; 896 FunctionAnalysisManager FAM; 897 CGSCCAnalysisManager CGAM; 898 ModuleAnalysisManager MAM; 899 900 bool DebugPassStructure = CodeGenOpts.DebugPass == "Structure"; 901 PassInstrumentationCallbacks PIC; 902 PrintPassOptions PrintPassOpts; 903 PrintPassOpts.Indent = DebugPassStructure; 904 PrintPassOpts.SkipAnalyses = DebugPassStructure; 905 StandardInstrumentations SI( 906 TheModule->getContext(), 907 (CodeGenOpts.DebugPassManager || DebugPassStructure), 908 CodeGenOpts.VerifyEach, PrintPassOpts); 909 SI.registerCallbacks(PIC, &MAM); 910 PassBuilder PB(TM.get(), PTO, PGOOpt, &PIC); 911 912 // Handle the assignment tracking feature options. 913 switch (CodeGenOpts.getAssignmentTrackingMode()) { 914 case CodeGenOptions::AssignmentTrackingOpts::Forced: 915 PB.registerPipelineStartEPCallback( 916 [&](ModulePassManager &MPM, OptimizationLevel Level) { 917 MPM.addPass(AssignmentTrackingPass()); 918 }); 919 break; 920 case CodeGenOptions::AssignmentTrackingOpts::Enabled: 921 // Disable assignment tracking in LTO builds for now as the performance 922 // cost is too high. Disable for LLDB tuning due to llvm.org/PR43126. 923 if (!CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.PrepareForLTO && 924 CodeGenOpts.getDebuggerTuning() != llvm::DebuggerKind::LLDB) { 925 PB.registerPipelineStartEPCallback( 926 [&](ModulePassManager &MPM, OptimizationLevel Level) { 927 // Only use assignment tracking if optimisations are enabled. 928 if (Level != OptimizationLevel::O0) 929 MPM.addPass(AssignmentTrackingPass()); 930 }); 931 } 932 break; 933 case CodeGenOptions::AssignmentTrackingOpts::Disabled: 934 break; 935 } 936 937 // Enable verify-debuginfo-preserve-each for new PM. 938 DebugifyEachInstrumentation Debugify; 939 DebugInfoPerPass DebugInfoBeforePass; 940 if (CodeGenOpts.EnableDIPreservationVerify) { 941 Debugify.setDebugifyMode(DebugifyMode::OriginalDebugInfo); 942 Debugify.setDebugInfoBeforePass(DebugInfoBeforePass); 943 944 if (!CodeGenOpts.DIBugsReportFilePath.empty()) 945 Debugify.setOrigDIVerifyBugsReportFilePath( 946 CodeGenOpts.DIBugsReportFilePath); 947 Debugify.registerCallbacks(PIC, MAM); 948 } 949 // Attempt to load pass plugins and register their callbacks with PB. 950 for (auto &PluginFN : CodeGenOpts.PassPlugins) { 951 auto PassPlugin = PassPlugin::Load(PluginFN); 952 if (PassPlugin) { 953 PassPlugin->registerPassBuilderCallbacks(PB); 954 } else { 955 Diags.Report(diag::err_fe_unable_to_load_plugin) 956 << PluginFN << toString(PassPlugin.takeError()); 957 } 958 } 959 for (const auto &PassCallback : CodeGenOpts.PassBuilderCallbacks) 960 PassCallback(PB); 961 #define HANDLE_EXTENSION(Ext) \ 962 get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB); 963 #include "llvm/Support/Extension.def" 964 965 // Register the target library analysis directly and give it a customized 966 // preset TLI. 967 std::unique_ptr<TargetLibraryInfoImpl> TLII( 968 llvm::driver::createTLII(TargetTriple, CodeGenOpts.getVecLib())); 969 FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); }); 970 971 // Register all the basic analyses with the managers. 972 PB.registerModuleAnalyses(MAM); 973 PB.registerCGSCCAnalyses(CGAM); 974 PB.registerFunctionAnalyses(FAM); 975 PB.registerLoopAnalyses(LAM); 976 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); 977 978 ModulePassManager MPM; 979 // Add a verifier pass, before any other passes, to catch CodeGen issues. 980 if (CodeGenOpts.VerifyModule) 981 MPM.addPass(VerifierPass()); 982 983 if (!CodeGenOpts.DisableLLVMPasses) { 984 // Map our optimization levels into one of the distinct levels used to 985 // configure the pipeline. 986 OptimizationLevel Level = mapToLevel(CodeGenOpts); 987 988 const bool PrepareForThinLTO = CodeGenOpts.PrepareForThinLTO; 989 const bool PrepareForLTO = CodeGenOpts.PrepareForLTO; 990 991 if (LangOpts.ObjCAutoRefCount) { 992 PB.registerPipelineStartEPCallback( 993 [](ModulePassManager &MPM, OptimizationLevel Level) { 994 if (Level != OptimizationLevel::O0) 995 MPM.addPass( 996 createModuleToFunctionPassAdaptor(ObjCARCExpandPass())); 997 }); 998 PB.registerPipelineEarlySimplificationEPCallback( 999 [](ModulePassManager &MPM, OptimizationLevel Level, 1000 ThinOrFullLTOPhase) { 1001 if (Level != OptimizationLevel::O0) 1002 MPM.addPass(ObjCARCAPElimPass()); 1003 }); 1004 PB.registerScalarOptimizerLateEPCallback( 1005 [](FunctionPassManager &FPM, OptimizationLevel Level) { 1006 if (Level != OptimizationLevel::O0) 1007 FPM.addPass(ObjCARCOptPass()); 1008 }); 1009 } 1010 1011 // If we reached here with a non-empty index file name, then the index 1012 // file was empty and we are not performing ThinLTO backend compilation 1013 // (used in testing in a distributed build environment). 1014 bool IsThinLTOPostLink = !CodeGenOpts.ThinLTOIndexFile.empty(); 1015 // If so drop any the type test assume sequences inserted for whole program 1016 // vtables so that codegen doesn't complain. 1017 if (IsThinLTOPostLink) 1018 PB.registerPipelineStartEPCallback( 1019 [](ModulePassManager &MPM, OptimizationLevel Level) { 1020 MPM.addPass(LowerTypeTestsPass( 1021 /*ExportSummary=*/nullptr, 1022 /*ImportSummary=*/nullptr, 1023 /*DropTypeTests=*/lowertypetests::DropTestKind::Assume)); 1024 }); 1025 1026 // Register callbacks to schedule sanitizer passes at the appropriate part 1027 // of the pipeline. 1028 if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds)) 1029 PB.registerScalarOptimizerLateEPCallback([this](FunctionPassManager &FPM, 1030 OptimizationLevel Level) { 1031 BoundsCheckingPass::Options Options; 1032 if (CodeGenOpts.SanitizeSkipHotCutoffs[SanitizerKind::SO_LocalBounds] || 1033 ClSanitizeGuardChecks) { 1034 static_assert(SanitizerKind::SO_LocalBounds <= 1035 std::numeric_limits< 1036 decltype(Options.GuardKind)::value_type>::max(), 1037 "Update type of llvm.allow.ubsan.check to represent " 1038 "SanitizerKind::SO_LocalBounds."); 1039 Options.GuardKind = SanitizerKind::SO_LocalBounds; 1040 } 1041 Options.Merge = 1042 CodeGenOpts.SanitizeMergeHandlers.has(SanitizerKind::LocalBounds); 1043 if (!CodeGenOpts.SanitizeTrap.has(SanitizerKind::LocalBounds)) { 1044 Options.Rt = { 1045 /*MinRuntime=*/static_cast<bool>( 1046 CodeGenOpts.SanitizeMinimalRuntime), 1047 /*MayReturn=*/ 1048 CodeGenOpts.SanitizeRecover.has(SanitizerKind::LocalBounds), 1049 }; 1050 } 1051 FPM.addPass(BoundsCheckingPass(Options)); 1052 }); 1053 1054 // Don't add sanitizers if we are here from ThinLTO PostLink. That already 1055 // done on PreLink stage. 1056 if (!IsThinLTOPostLink) { 1057 addSanitizers(TargetTriple, CodeGenOpts, LangOpts, PB); 1058 addKCFIPass(TargetTriple, LangOpts, PB); 1059 } 1060 1061 if (std::optional<GCOVOptions> Options = 1062 getGCOVOptions(CodeGenOpts, LangOpts)) 1063 PB.registerPipelineStartEPCallback( 1064 [Options](ModulePassManager &MPM, OptimizationLevel Level) { 1065 MPM.addPass(GCOVProfilerPass(*Options)); 1066 }); 1067 if (std::optional<InstrProfOptions> Options = 1068 getInstrProfOptions(CodeGenOpts, LangOpts)) 1069 PB.registerPipelineStartEPCallback( 1070 [Options](ModulePassManager &MPM, OptimizationLevel Level) { 1071 MPM.addPass(InstrProfilingLoweringPass(*Options, false)); 1072 }); 1073 1074 // TODO: Consider passing the MemoryProfileOutput to the pass builder via 1075 // the PGOOptions, and set this up there. 1076 if (!CodeGenOpts.MemoryProfileOutput.empty()) { 1077 PB.registerOptimizerLastEPCallback([](ModulePassManager &MPM, 1078 OptimizationLevel Level, 1079 ThinOrFullLTOPhase) { 1080 MPM.addPass(createModuleToFunctionPassAdaptor(MemProfilerPass())); 1081 MPM.addPass(ModuleMemProfilerPass()); 1082 }); 1083 } 1084 1085 if (CodeGenOpts.FatLTO) { 1086 MPM.addPass(PB.buildFatLTODefaultPipeline( 1087 Level, PrepareForThinLTO, 1088 PrepareForThinLTO || shouldEmitRegularLTOSummary())); 1089 } else if (PrepareForThinLTO) { 1090 MPM.addPass(PB.buildThinLTOPreLinkDefaultPipeline(Level)); 1091 } else if (PrepareForLTO) { 1092 MPM.addPass(PB.buildLTOPreLinkDefaultPipeline(Level)); 1093 } else { 1094 MPM.addPass(PB.buildPerModuleDefaultPipeline(Level)); 1095 } 1096 } 1097 1098 // Link against bitcodes supplied via the -mlink-builtin-bitcode option 1099 if (CodeGenOpts.LinkBitcodePostopt) 1100 MPM.addPass(LinkInModulesPass(BC)); 1101 1102 // Add a verifier pass if requested. We don't have to do this if the action 1103 // requires code generation because there will already be a verifier pass in 1104 // the code-generation pipeline. 1105 // Since we already added a verifier pass above, this 1106 // might even not run the analysis, if previous passes caused no changes. 1107 if (!actionRequiresCodeGen(Action) && CodeGenOpts.VerifyModule) 1108 MPM.addPass(VerifierPass()); 1109 1110 if (Action == Backend_EmitBC || Action == Backend_EmitLL || 1111 CodeGenOpts.FatLTO) { 1112 if (CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.DisableLLVMPasses) { 1113 if (!TheModule->getModuleFlag("EnableSplitLTOUnit")) 1114 TheModule->addModuleFlag(llvm::Module::Error, "EnableSplitLTOUnit", 1115 CodeGenOpts.EnableSplitLTOUnit); 1116 if (Action == Backend_EmitBC) { 1117 if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) { 1118 ThinLinkOS = openOutputFile(CodeGenOpts.ThinLinkBitcodeFile); 1119 if (!ThinLinkOS) 1120 return; 1121 } 1122 MPM.addPass(ThinLTOBitcodeWriterPass( 1123 *OS, ThinLinkOS ? &ThinLinkOS->os() : nullptr)); 1124 } else if (Action == Backend_EmitLL) { 1125 MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists, 1126 /*EmitLTOSummary=*/true)); 1127 } 1128 } else { 1129 // Emit a module summary by default for Regular LTO except for ld64 1130 // targets 1131 bool EmitLTOSummary = shouldEmitRegularLTOSummary(); 1132 if (EmitLTOSummary) { 1133 if (!TheModule->getModuleFlag("ThinLTO") && !CodeGenOpts.UnifiedLTO) 1134 TheModule->addModuleFlag(llvm::Module::Error, "ThinLTO", uint32_t(0)); 1135 if (!TheModule->getModuleFlag("EnableSplitLTOUnit")) 1136 TheModule->addModuleFlag(llvm::Module::Error, "EnableSplitLTOUnit", 1137 uint32_t(1)); 1138 } 1139 if (Action == Backend_EmitBC) { 1140 MPM.addPass(BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists, 1141 EmitLTOSummary)); 1142 } else if (Action == Backend_EmitLL) { 1143 MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists, 1144 EmitLTOSummary)); 1145 } 1146 } 1147 1148 if (shouldEmitUnifiedLTOModueFlag()) 1149 TheModule->addModuleFlag(llvm::Module::Error, "UnifiedLTO", uint32_t(1)); 1150 } 1151 1152 // FIXME: This should eventually be replaced by a first-class driver option. 1153 // This should be done for both clang and flang simultaneously. 1154 // Print a textual, '-passes=' compatible, representation of pipeline if 1155 // requested. 1156 if (PrintPipelinePasses) { 1157 MPM.printPipeline(outs(), [&PIC](StringRef ClassName) { 1158 auto PassName = PIC.getPassNameForClassName(ClassName); 1159 return PassName.empty() ? ClassName : PassName; 1160 }); 1161 outs() << "\n"; 1162 return; 1163 } 1164 1165 if (LangOpts.HIPStdPar && !LangOpts.CUDAIsDevice && 1166 LangOpts.HIPStdParInterposeAlloc) 1167 MPM.addPass(HipStdParAllocationInterpositionPass()); 1168 1169 // Now that we have all of the passes ready, run them. 1170 { 1171 PrettyStackTraceString CrashInfo("Optimizer"); 1172 llvm::TimeTraceScope TimeScope("Optimizer"); 1173 Timer timer; 1174 if (CI.getCodeGenOpts().TimePasses) { 1175 timer.init("optimizer", "Optimizer", CI.getTimerGroup()); 1176 CI.getFrontendTimer().yieldTo(timer); 1177 } 1178 MPM.run(*TheModule, MAM); 1179 if (CI.getCodeGenOpts().TimePasses) 1180 timer.yieldTo(CI.getFrontendTimer()); 1181 } 1182 } 1183 1184 void EmitAssemblyHelper::RunCodegenPipeline( 1185 BackendAction Action, std::unique_ptr<raw_pwrite_stream> &OS, 1186 std::unique_ptr<llvm::ToolOutputFile> &DwoOS) { 1187 // We still use the legacy PM to run the codegen pipeline since the new PM 1188 // does not work with the codegen pipeline. 1189 // FIXME: make the new PM work with the codegen pipeline. 1190 legacy::PassManager CodeGenPasses; 1191 1192 // Append any output we need to the pass manager. 1193 switch (Action) { 1194 case Backend_EmitAssembly: 1195 case Backend_EmitMCNull: 1196 case Backend_EmitObj: 1197 CodeGenPasses.add( 1198 createTargetTransformInfoWrapperPass(getTargetIRAnalysis())); 1199 if (!CodeGenOpts.SplitDwarfOutput.empty()) { 1200 DwoOS = openOutputFile(CodeGenOpts.SplitDwarfOutput); 1201 if (!DwoOS) 1202 return; 1203 } 1204 if (!AddEmitPasses(CodeGenPasses, Action, *OS, 1205 DwoOS ? &DwoOS->os() : nullptr)) 1206 // FIXME: Should we handle this error differently? 1207 return; 1208 break; 1209 default: 1210 return; 1211 } 1212 1213 // If -print-pipeline-passes is requested, don't run the legacy pass manager. 1214 // FIXME: when codegen is switched to use the new pass manager, it should also 1215 // emit pass names here. 1216 if (PrintPipelinePasses) { 1217 return; 1218 } 1219 1220 { 1221 PrettyStackTraceString CrashInfo("Code generation"); 1222 llvm::TimeTraceScope TimeScope("CodeGenPasses"); 1223 Timer timer; 1224 if (CI.getCodeGenOpts().TimePasses) { 1225 timer.init("codegen", "Machine code generation", CI.getTimerGroup()); 1226 CI.getFrontendTimer().yieldTo(timer); 1227 } 1228 CodeGenPasses.run(*TheModule); 1229 if (CI.getCodeGenOpts().TimePasses) 1230 timer.yieldTo(CI.getFrontendTimer()); 1231 } 1232 } 1233 1234 void EmitAssemblyHelper::emitAssembly(BackendAction Action, 1235 std::unique_ptr<raw_pwrite_stream> OS, 1236 BackendConsumer *BC) { 1237 setCommandLineOpts(CodeGenOpts); 1238 1239 bool RequiresCodeGen = actionRequiresCodeGen(Action); 1240 CreateTargetMachine(RequiresCodeGen); 1241 1242 if (RequiresCodeGen && !TM) 1243 return; 1244 if (TM) 1245 TheModule->setDataLayout(TM->createDataLayout()); 1246 1247 // Before executing passes, print the final values of the LLVM options. 1248 cl::PrintOptionValues(); 1249 1250 std::unique_ptr<llvm::ToolOutputFile> ThinLinkOS, DwoOS; 1251 RunOptimizationPipeline(Action, OS, ThinLinkOS, BC); 1252 RunCodegenPipeline(Action, OS, DwoOS); 1253 1254 if (ThinLinkOS) 1255 ThinLinkOS->keep(); 1256 if (DwoOS) 1257 DwoOS->keep(); 1258 } 1259 1260 static void 1261 runThinLTOBackend(CompilerInstance &CI, ModuleSummaryIndex *CombinedIndex, 1262 llvm::Module *M, std::unique_ptr<raw_pwrite_stream> OS, 1263 std::string SampleProfile, std::string ProfileRemapping, 1264 BackendAction Action) { 1265 DiagnosticsEngine &Diags = CI.getDiagnostics(); 1266 const auto &CGOpts = CI.getCodeGenOpts(); 1267 const auto &TOpts = CI.getTargetOpts(); 1268 DenseMap<StringRef, DenseMap<GlobalValue::GUID, GlobalValueSummary *>> 1269 ModuleToDefinedGVSummaries; 1270 CombinedIndex->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); 1271 1272 setCommandLineOpts(CGOpts); 1273 1274 // We can simply import the values mentioned in the combined index, since 1275 // we should only invoke this using the individual indexes written out 1276 // via a WriteIndexesThinBackend. 1277 FunctionImporter::ImportIDTable ImportIDs; 1278 FunctionImporter::ImportMapTy ImportList(ImportIDs); 1279 if (!lto::initImportList(*M, *CombinedIndex, ImportList)) 1280 return; 1281 1282 auto AddStream = [&](size_t Task, const Twine &ModuleName) { 1283 return std::make_unique<CachedFileStream>(std::move(OS), 1284 CGOpts.ObjectFilenameForDebug); 1285 }; 1286 lto::Config Conf; 1287 if (CGOpts.SaveTempsFilePrefix != "") { 1288 if (Error E = Conf.addSaveTemps(CGOpts.SaveTempsFilePrefix + ".", 1289 /* UseInputModulePath */ false)) { 1290 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) { 1291 errs() << "Error setting up ThinLTO save-temps: " << EIB.message() 1292 << '\n'; 1293 }); 1294 } 1295 } 1296 Conf.CPU = TOpts.CPU; 1297 Conf.CodeModel = getCodeModel(CGOpts); 1298 Conf.MAttrs = TOpts.Features; 1299 Conf.RelocModel = CGOpts.RelocationModel; 1300 std::optional<CodeGenOptLevel> OptLevelOrNone = 1301 CodeGenOpt::getLevel(CGOpts.OptimizationLevel); 1302 assert(OptLevelOrNone && "Invalid optimization level!"); 1303 Conf.CGOptLevel = *OptLevelOrNone; 1304 Conf.OptLevel = CGOpts.OptimizationLevel; 1305 initTargetOptions(CI, Diags, Conf.Options); 1306 Conf.SampleProfile = std::move(SampleProfile); 1307 Conf.PTO.LoopUnrolling = CGOpts.UnrollLoops; 1308 // For historical reasons, loop interleaving is set to mirror setting for loop 1309 // unrolling. 1310 Conf.PTO.LoopInterleaving = CGOpts.UnrollLoops; 1311 Conf.PTO.LoopVectorization = CGOpts.VectorizeLoop; 1312 Conf.PTO.SLPVectorization = CGOpts.VectorizeSLP; 1313 // Only enable CGProfilePass when using integrated assembler, since 1314 // non-integrated assemblers don't recognize .cgprofile section. 1315 Conf.PTO.CallGraphProfile = !CGOpts.DisableIntegratedAS; 1316 1317 // Context sensitive profile. 1318 if (CGOpts.hasProfileCSIRInstr()) { 1319 Conf.RunCSIRInstr = true; 1320 Conf.CSIRProfile = std::move(CGOpts.InstrProfileOutput); 1321 } else if (CGOpts.hasProfileCSIRUse()) { 1322 Conf.RunCSIRInstr = false; 1323 Conf.CSIRProfile = std::move(CGOpts.ProfileInstrumentUsePath); 1324 } 1325 1326 Conf.ProfileRemapping = std::move(ProfileRemapping); 1327 Conf.DebugPassManager = CGOpts.DebugPassManager; 1328 Conf.VerifyEach = CGOpts.VerifyEach; 1329 Conf.RemarksWithHotness = CGOpts.DiagnosticsWithHotness; 1330 Conf.RemarksFilename = CGOpts.OptRecordFile; 1331 Conf.RemarksPasses = CGOpts.OptRecordPasses; 1332 Conf.RemarksFormat = CGOpts.OptRecordFormat; 1333 Conf.SplitDwarfFile = CGOpts.SplitDwarfFile; 1334 Conf.SplitDwarfOutput = CGOpts.SplitDwarfOutput; 1335 switch (Action) { 1336 case Backend_EmitNothing: 1337 Conf.PreCodeGenModuleHook = [](size_t Task, const llvm::Module &Mod) { 1338 return false; 1339 }; 1340 break; 1341 case Backend_EmitLL: 1342 Conf.PreCodeGenModuleHook = [&](size_t Task, const llvm::Module &Mod) { 1343 M->print(*OS, nullptr, CGOpts.EmitLLVMUseLists); 1344 return false; 1345 }; 1346 break; 1347 case Backend_EmitBC: 1348 Conf.PreCodeGenModuleHook = [&](size_t Task, const llvm::Module &Mod) { 1349 WriteBitcodeToFile(*M, *OS, CGOpts.EmitLLVMUseLists); 1350 return false; 1351 }; 1352 break; 1353 default: 1354 Conf.CGFileType = getCodeGenFileType(Action); 1355 break; 1356 } 1357 if (Error E = 1358 thinBackend(Conf, -1, AddStream, *M, *CombinedIndex, ImportList, 1359 ModuleToDefinedGVSummaries[M->getModuleIdentifier()], 1360 /*ModuleMap=*/nullptr, Conf.CodeGenOnly, 1361 /*IRAddStream=*/nullptr, CGOpts.CmdArgs)) { 1362 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) { 1363 errs() << "Error running ThinLTO backend: " << EIB.message() << '\n'; 1364 }); 1365 } 1366 } 1367 1368 void clang::emitBackendOutput(CompilerInstance &CI, CodeGenOptions &CGOpts, 1369 StringRef TDesc, llvm::Module *M, 1370 BackendAction Action, 1371 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, 1372 std::unique_ptr<raw_pwrite_stream> OS, 1373 BackendConsumer *BC) { 1374 llvm::TimeTraceScope TimeScope("Backend"); 1375 DiagnosticsEngine &Diags = CI.getDiagnostics(); 1376 1377 std::unique_ptr<llvm::Module> EmptyModule; 1378 if (!CGOpts.ThinLTOIndexFile.empty()) { 1379 // If we are performing a ThinLTO importing compile, load the function index 1380 // into memory and pass it into runThinLTOBackend, which will run the 1381 // function importer and invoke LTO passes. 1382 std::unique_ptr<ModuleSummaryIndex> CombinedIndex; 1383 if (Error E = llvm::getModuleSummaryIndexForFile( 1384 CGOpts.ThinLTOIndexFile, 1385 /*IgnoreEmptyThinLTOIndexFile*/ true) 1386 .moveInto(CombinedIndex)) { 1387 logAllUnhandledErrors(std::move(E), errs(), 1388 "Error loading index file '" + 1389 CGOpts.ThinLTOIndexFile + "': "); 1390 return; 1391 } 1392 1393 // A null CombinedIndex means we should skip ThinLTO compilation 1394 // (LLVM will optionally ignore empty index files, returning null instead 1395 // of an error). 1396 if (CombinedIndex) { 1397 if (!CombinedIndex->skipModuleByDistributedBackend()) { 1398 runThinLTOBackend(CI, CombinedIndex.get(), M, std::move(OS), 1399 CGOpts.SampleProfileFile, CGOpts.ProfileRemappingFile, 1400 Action); 1401 return; 1402 } 1403 // Distributed indexing detected that nothing from the module is needed 1404 // for the final linking. So we can skip the compilation. We sill need to 1405 // output an empty object file to make sure that a linker does not fail 1406 // trying to read it. Also for some features, like CFI, we must skip 1407 // the compilation as CombinedIndex does not contain all required 1408 // information. 1409 EmptyModule = std::make_unique<llvm::Module>("empty", M->getContext()); 1410 EmptyModule->setTargetTriple(M->getTargetTriple()); 1411 M = EmptyModule.get(); 1412 } 1413 } 1414 1415 EmitAssemblyHelper AsmHelper(CI, CGOpts, M, VFS); 1416 AsmHelper.emitAssembly(Action, std::move(OS), BC); 1417 1418 // Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's 1419 // DataLayout. 1420 if (AsmHelper.TM) { 1421 std::string DLDesc = M->getDataLayout().getStringRepresentation(); 1422 if (DLDesc != TDesc) { 1423 unsigned DiagID = Diags.getCustomDiagID( 1424 DiagnosticsEngine::Error, "backend data layout '%0' does not match " 1425 "expected target description '%1'"); 1426 Diags.Report(DiagID) << DLDesc << TDesc; 1427 } 1428 } 1429 } 1430 1431 // With -fembed-bitcode, save a copy of the llvm IR as data in the 1432 // __LLVM,__bitcode section. 1433 void clang::EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts, 1434 llvm::MemoryBufferRef Buf) { 1435 if (CGOpts.getEmbedBitcode() == CodeGenOptions::Embed_Off) 1436 return; 1437 llvm::embedBitcodeInModule( 1438 *M, Buf, CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Marker, 1439 CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Bitcode, 1440 CGOpts.CmdArgs); 1441 } 1442 1443 void clang::EmbedObject(llvm::Module *M, const CodeGenOptions &CGOpts, 1444 DiagnosticsEngine &Diags) { 1445 if (CGOpts.OffloadObjects.empty()) 1446 return; 1447 1448 for (StringRef OffloadObject : CGOpts.OffloadObjects) { 1449 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> ObjectOrErr = 1450 llvm::MemoryBuffer::getFileOrSTDIN(OffloadObject); 1451 if (ObjectOrErr.getError()) { 1452 auto DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 1453 "could not open '%0' for embedding"); 1454 Diags.Report(DiagID) << OffloadObject; 1455 return; 1456 } 1457 1458 llvm::embedBufferInModule(*M, **ObjectOrErr, ".llvm.offloading", 1459 Align(object::OffloadBinary::getAlignment())); 1460 } 1461 } 1462