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/StringExtras.h" 20 #include "llvm/ADT/StringSwitch.h" 21 #include "llvm/Analysis/GlobalsModRef.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/TargetSubtargetInfo.h" 28 #include "llvm/Frontend/Driver/CodeGenOptions.h" 29 #include "llvm/IR/DataLayout.h" 30 #include "llvm/IR/DebugInfo.h" 31 #include "llvm/IR/LegacyPassManager.h" 32 #include "llvm/IR/Module.h" 33 #include "llvm/IR/ModuleSummaryIndex.h" 34 #include "llvm/IR/PassManager.h" 35 #include "llvm/IR/Verifier.h" 36 #include "llvm/IRPrinter/IRPrintingPasses.h" 37 #include "llvm/LTO/LTOBackend.h" 38 #include "llvm/MC/TargetRegistry.h" 39 #include "llvm/Object/OffloadBinary.h" 40 #include "llvm/Passes/PassBuilder.h" 41 #include "llvm/Passes/PassPlugin.h" 42 #include "llvm/Passes/StandardInstrumentations.h" 43 #include "llvm/ProfileData/InstrProfCorrelator.h" 44 #include "llvm/Support/BuryPointer.h" 45 #include "llvm/Support/CommandLine.h" 46 #include "llvm/Support/MemoryBuffer.h" 47 #include "llvm/Support/PrettyStackTrace.h" 48 #include "llvm/Support/Program.h" 49 #include "llvm/Support/TimeProfiler.h" 50 #include "llvm/Support/Timer.h" 51 #include "llvm/Support/ToolOutputFile.h" 52 #include "llvm/Support/VirtualFileSystem.h" 53 #include "llvm/Support/raw_ostream.h" 54 #include "llvm/Target/TargetMachine.h" 55 #include "llvm/Target/TargetOptions.h" 56 #include "llvm/TargetParser/SubtargetFeature.h" 57 #include "llvm/TargetParser/Triple.h" 58 #include "llvm/Transforms/HipStdPar/HipStdPar.h" 59 #include "llvm/Transforms/IPO/EmbedBitcodePass.h" 60 #include "llvm/Transforms/IPO/LowerTypeTests.h" 61 #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h" 62 #include "llvm/Transforms/InstCombine/InstCombine.h" 63 #include "llvm/Transforms/Instrumentation/AddressSanitizer.h" 64 #include "llvm/Transforms/Instrumentation/AddressSanitizerOptions.h" 65 #include "llvm/Transforms/Instrumentation/BoundsChecking.h" 66 #include "llvm/Transforms/Instrumentation/DataFlowSanitizer.h" 67 #include "llvm/Transforms/Instrumentation/GCOVProfiler.h" 68 #include "llvm/Transforms/Instrumentation/HWAddressSanitizer.h" 69 #include "llvm/Transforms/Instrumentation/InstrProfiling.h" 70 #include "llvm/Transforms/Instrumentation/KCFI.h" 71 #include "llvm/Transforms/Instrumentation/LowerAllowCheckPass.h" 72 #include "llvm/Transforms/Instrumentation/MemProfiler.h" 73 #include "llvm/Transforms/Instrumentation/MemorySanitizer.h" 74 #include "llvm/Transforms/Instrumentation/NumericalStabilitySanitizer.h" 75 #include "llvm/Transforms/Instrumentation/PGOInstrumentation.h" 76 #include "llvm/Transforms/Instrumentation/RealtimeSanitizer.h" 77 #include "llvm/Transforms/Instrumentation/SanitizerBinaryMetadata.h" 78 #include "llvm/Transforms/Instrumentation/SanitizerCoverage.h" 79 #include "llvm/Transforms/Instrumentation/ThreadSanitizer.h" 80 #include "llvm/Transforms/Instrumentation/TypeSanitizer.h" 81 #include "llvm/Transforms/ObjCARC.h" 82 #include "llvm/Transforms/Scalar/EarlyCSE.h" 83 #include "llvm/Transforms/Scalar/GVN.h" 84 #include "llvm/Transforms/Scalar/JumpThreading.h" 85 #include "llvm/Transforms/Utils/Debugify.h" 86 #include "llvm/Transforms/Utils/ModuleUtils.h" 87 #include <memory> 88 #include <optional> 89 using namespace clang; 90 using namespace llvm; 91 92 #define HANDLE_EXTENSION(Ext) \ 93 llvm::PassPluginLibraryInfo get##Ext##PluginInfo(); 94 #include "llvm/Support/Extension.def" 95 96 namespace llvm { 97 extern cl::opt<bool> PrintPipelinePasses; 98 99 // Experiment to move sanitizers earlier. 100 static cl::opt<bool> ClSanitizeOnOptimizerEarlyEP( 101 "sanitizer-early-opt-ep", cl::Optional, 102 cl::desc("Insert sanitizers on OptimizerEarlyEP.")); 103 104 // Experiment to mark cold functions as optsize/minsize/optnone. 105 // TODO: remove once this is exposed as a proper driver flag. 106 static cl::opt<PGOOptions::ColdFuncOpt> ClPGOColdFuncAttr( 107 "pgo-cold-func-opt", cl::init(PGOOptions::ColdFuncOpt::Default), cl::Hidden, 108 cl::desc( 109 "Function attribute to apply to cold functions as determined by PGO"), 110 cl::values(clEnumValN(PGOOptions::ColdFuncOpt::Default, "default", 111 "Default (no attribute)"), 112 clEnumValN(PGOOptions::ColdFuncOpt::OptSize, "optsize", 113 "Mark cold functions with optsize."), 114 clEnumValN(PGOOptions::ColdFuncOpt::MinSize, "minsize", 115 "Mark cold functions with minsize."), 116 clEnumValN(PGOOptions::ColdFuncOpt::OptNone, "optnone", 117 "Mark cold functions with optnone."))); 118 119 extern cl::opt<InstrProfCorrelator::ProfCorrelatorKind> ProfileCorrelate; 120 } // namespace llvm 121 122 namespace { 123 124 // Default filename used for profile generation. 125 std::string getDefaultProfileGenName() { 126 return DebugInfoCorrelate || ProfileCorrelate != InstrProfCorrelator::NONE 127 ? "default_%m.proflite" 128 : "default_%m.profraw"; 129 } 130 131 class EmitAssemblyHelper { 132 DiagnosticsEngine &Diags; 133 const HeaderSearchOptions &HSOpts; 134 const CodeGenOptions &CodeGenOpts; 135 const clang::TargetOptions &TargetOpts; 136 const LangOptions &LangOpts; 137 llvm::Module *TheModule; 138 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS; 139 140 Timer CodeGenerationTime; 141 142 std::unique_ptr<raw_pwrite_stream> OS; 143 144 Triple TargetTriple; 145 146 TargetIRAnalysis getTargetIRAnalysis() const { 147 if (TM) 148 return TM->getTargetIRAnalysis(); 149 150 return TargetIRAnalysis(); 151 } 152 153 /// Generates the TargetMachine. 154 /// Leaves TM unchanged if it is unable to create the target machine. 155 /// Some of our clang tests specify triples which are not built 156 /// into clang. This is okay because these tests check the generated 157 /// IR, and they require DataLayout which depends on the triple. 158 /// In this case, we allow this method to fail and not report an error. 159 /// When MustCreateTM is used, we print an error if we are unable to load 160 /// the requested target. 161 void CreateTargetMachine(bool MustCreateTM); 162 163 /// Add passes necessary to emit assembly or LLVM IR. 164 /// 165 /// \return True on success. 166 bool AddEmitPasses(legacy::PassManager &CodeGenPasses, BackendAction Action, 167 raw_pwrite_stream &OS, raw_pwrite_stream *DwoOS); 168 169 std::unique_ptr<llvm::ToolOutputFile> openOutputFile(StringRef Path) { 170 std::error_code EC; 171 auto F = std::make_unique<llvm::ToolOutputFile>(Path, EC, 172 llvm::sys::fs::OF_None); 173 if (EC) { 174 Diags.Report(diag::err_fe_unable_to_open_output) << Path << EC.message(); 175 F.reset(); 176 } 177 return F; 178 } 179 180 void RunOptimizationPipeline( 181 BackendAction Action, std::unique_ptr<raw_pwrite_stream> &OS, 182 std::unique_ptr<llvm::ToolOutputFile> &ThinLinkOS, BackendConsumer *BC); 183 void RunCodegenPipeline(BackendAction Action, 184 std::unique_ptr<raw_pwrite_stream> &OS, 185 std::unique_ptr<llvm::ToolOutputFile> &DwoOS); 186 187 /// Check whether we should emit a module summary for regular LTO. 188 /// The module summary should be emitted by default for regular LTO 189 /// except for ld64 targets. 190 /// 191 /// \return True if the module summary should be emitted. 192 bool shouldEmitRegularLTOSummary() const { 193 return CodeGenOpts.PrepareForLTO && !CodeGenOpts.DisableLLVMPasses && 194 TargetTriple.getVendor() != llvm::Triple::Apple; 195 } 196 197 /// Check whether we should emit a flag for UnifiedLTO. 198 /// The UnifiedLTO module flag should be set when UnifiedLTO is enabled for 199 /// ThinLTO or Full LTO with module summaries. 200 bool shouldEmitUnifiedLTOModueFlag() const { 201 return CodeGenOpts.UnifiedLTO && 202 (CodeGenOpts.PrepareForThinLTO || shouldEmitRegularLTOSummary()); 203 } 204 205 public: 206 EmitAssemblyHelper(DiagnosticsEngine &_Diags, 207 const HeaderSearchOptions &HeaderSearchOpts, 208 const CodeGenOptions &CGOpts, 209 const clang::TargetOptions &TOpts, 210 const LangOptions &LOpts, llvm::Module *M, 211 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) 212 : Diags(_Diags), HSOpts(HeaderSearchOpts), CodeGenOpts(CGOpts), 213 TargetOpts(TOpts), LangOpts(LOpts), TheModule(M), VFS(std::move(VFS)), 214 CodeGenerationTime("codegen", "Code Generation Time"), 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(DiagnosticsEngine &Diags, 355 llvm::TargetOptions &Options, 356 const CodeGenOptions &CodeGenOpts, 357 const clang::TargetOptions &TargetOpts, 358 const LangOptions &LangOpts, 359 const HeaderSearchOptions &HSOpts) { 360 switch (LangOpts.getThreadModel()) { 361 case LangOptions::ThreadModelKind::POSIX: 362 Options.ThreadModel = llvm::ThreadModel::POSIX; 363 break; 364 case LangOptions::ThreadModelKind::Single: 365 Options.ThreadModel = llvm::ThreadModel::Single; 366 break; 367 } 368 369 // Set float ABI type. 370 assert((CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp" || 371 CodeGenOpts.FloatABI == "hard" || CodeGenOpts.FloatABI.empty()) && 372 "Invalid Floating Point ABI!"); 373 Options.FloatABIType = 374 llvm::StringSwitch<llvm::FloatABI::ABIType>(CodeGenOpts.FloatABI) 375 .Case("soft", llvm::FloatABI::Soft) 376 .Case("softfp", llvm::FloatABI::Soft) 377 .Case("hard", llvm::FloatABI::Hard) 378 .Default(llvm::FloatABI::Default); 379 380 // Set FP fusion mode. 381 switch (LangOpts.getDefaultFPContractMode()) { 382 case LangOptions::FPM_Off: 383 // Preserve any contraction performed by the front-end. (Strict performs 384 // splitting of the muladd intrinsic in the backend.) 385 Options.AllowFPOpFusion = llvm::FPOpFusion::Standard; 386 break; 387 case LangOptions::FPM_On: 388 case LangOptions::FPM_FastHonorPragmas: 389 Options.AllowFPOpFusion = llvm::FPOpFusion::Standard; 390 break; 391 case LangOptions::FPM_Fast: 392 Options.AllowFPOpFusion = llvm::FPOpFusion::Fast; 393 break; 394 } 395 396 Options.BinutilsVersion = 397 llvm::TargetMachine::parseBinutilsVersion(CodeGenOpts.BinutilsVersion); 398 Options.UseInitArray = CodeGenOpts.UseInitArray; 399 Options.DisableIntegratedAS = CodeGenOpts.DisableIntegratedAS; 400 401 // Set EABI version. 402 Options.EABIVersion = TargetOpts.EABIVersion; 403 404 if (LangOpts.hasSjLjExceptions()) 405 Options.ExceptionModel = llvm::ExceptionHandling::SjLj; 406 if (LangOpts.hasSEHExceptions()) 407 Options.ExceptionModel = llvm::ExceptionHandling::WinEH; 408 if (LangOpts.hasDWARFExceptions()) 409 Options.ExceptionModel = llvm::ExceptionHandling::DwarfCFI; 410 if (LangOpts.hasWasmExceptions()) 411 Options.ExceptionModel = llvm::ExceptionHandling::Wasm; 412 413 Options.NoInfsFPMath = LangOpts.NoHonorInfs; 414 Options.NoNaNsFPMath = LangOpts.NoHonorNaNs; 415 Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS; 416 Options.UnsafeFPMath = LangOpts.AllowFPReassoc && LangOpts.AllowRecip && 417 LangOpts.NoSignedZero && LangOpts.ApproxFunc && 418 (LangOpts.getDefaultFPContractMode() == 419 LangOptions::FPModeKind::FPM_Fast || 420 LangOpts.getDefaultFPContractMode() == 421 LangOptions::FPModeKind::FPM_FastHonorPragmas); 422 Options.ApproxFuncFPMath = LangOpts.ApproxFunc; 423 424 Options.BBAddrMap = CodeGenOpts.BBAddrMap; 425 Options.BBSections = 426 llvm::StringSwitch<llvm::BasicBlockSection>(CodeGenOpts.BBSections) 427 .Case("all", llvm::BasicBlockSection::All) 428 .StartsWith("list=", llvm::BasicBlockSection::List) 429 .Case("none", llvm::BasicBlockSection::None) 430 .Default(llvm::BasicBlockSection::None); 431 432 if (Options.BBSections == llvm::BasicBlockSection::List) { 433 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = 434 MemoryBuffer::getFile(CodeGenOpts.BBSections.substr(5)); 435 if (!MBOrErr) { 436 Diags.Report(diag::err_fe_unable_to_load_basic_block_sections_file) 437 << MBOrErr.getError().message(); 438 return false; 439 } 440 Options.BBSectionsFuncListBuf = std::move(*MBOrErr); 441 } 442 443 Options.EnableMachineFunctionSplitter = CodeGenOpts.SplitMachineFunctions; 444 Options.FunctionSections = CodeGenOpts.FunctionSections; 445 Options.DataSections = CodeGenOpts.DataSections; 446 Options.IgnoreXCOFFVisibility = LangOpts.IgnoreXCOFFVisibility; 447 Options.UniqueSectionNames = CodeGenOpts.UniqueSectionNames; 448 Options.UniqueBasicBlockSectionNames = 449 CodeGenOpts.UniqueBasicBlockSectionNames; 450 Options.SeparateNamedSections = CodeGenOpts.SeparateNamedSections; 451 Options.TLSSize = CodeGenOpts.TLSSize; 452 Options.EnableTLSDESC = CodeGenOpts.EnableTLSDESC; 453 Options.EmulatedTLS = CodeGenOpts.EmulatedTLS; 454 Options.DebuggerTuning = CodeGenOpts.getDebuggerTuning(); 455 Options.EmitStackSizeSection = CodeGenOpts.StackSizeSection; 456 Options.StackUsageOutput = CodeGenOpts.StackUsageOutput; 457 Options.EmitAddrsig = CodeGenOpts.Addrsig; 458 Options.ForceDwarfFrameSection = CodeGenOpts.ForceDwarfFrameSection; 459 Options.EmitCallSiteInfo = CodeGenOpts.EmitCallSiteInfo; 460 Options.EnableAIXExtendedAltivecABI = LangOpts.EnableAIXExtendedAltivecABI; 461 Options.XRayFunctionIndex = CodeGenOpts.XRayFunctionIndex; 462 Options.LoopAlignment = CodeGenOpts.LoopAlignment; 463 Options.DebugStrictDwarf = CodeGenOpts.DebugStrictDwarf; 464 Options.ObjectFilenameForDebug = CodeGenOpts.ObjectFilenameForDebug; 465 Options.Hotpatch = CodeGenOpts.HotPatch; 466 Options.JMCInstrument = CodeGenOpts.JMCInstrument; 467 Options.XCOFFReadOnlyPointers = CodeGenOpts.XCOFFReadOnlyPointers; 468 469 switch (CodeGenOpts.getSwiftAsyncFramePointer()) { 470 case CodeGenOptions::SwiftAsyncFramePointerKind::Auto: 471 Options.SwiftAsyncFramePointer = 472 SwiftAsyncFramePointerMode::DeploymentBased; 473 break; 474 475 case CodeGenOptions::SwiftAsyncFramePointerKind::Always: 476 Options.SwiftAsyncFramePointer = SwiftAsyncFramePointerMode::Always; 477 break; 478 479 case CodeGenOptions::SwiftAsyncFramePointerKind::Never: 480 Options.SwiftAsyncFramePointer = SwiftAsyncFramePointerMode::Never; 481 break; 482 } 483 484 Options.MCOptions.SplitDwarfFile = CodeGenOpts.SplitDwarfFile; 485 Options.MCOptions.EmitDwarfUnwind = CodeGenOpts.getEmitDwarfUnwind(); 486 Options.MCOptions.EmitCompactUnwindNonCanonical = 487 CodeGenOpts.EmitCompactUnwindNonCanonical; 488 Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll; 489 Options.MCOptions.MCSaveTempLabels = CodeGenOpts.SaveTempLabels; 490 Options.MCOptions.MCUseDwarfDirectory = 491 CodeGenOpts.NoDwarfDirectoryAsm 492 ? llvm::MCTargetOptions::DisableDwarfDirectory 493 : llvm::MCTargetOptions::EnableDwarfDirectory; 494 Options.MCOptions.MCNoExecStack = CodeGenOpts.NoExecStack; 495 Options.MCOptions.MCIncrementalLinkerCompatible = 496 CodeGenOpts.IncrementalLinkerCompatible; 497 Options.MCOptions.MCFatalWarnings = CodeGenOpts.FatalWarnings; 498 Options.MCOptions.MCNoWarn = CodeGenOpts.NoWarn; 499 Options.MCOptions.AsmVerbose = CodeGenOpts.AsmVerbose; 500 Options.MCOptions.Dwarf64 = CodeGenOpts.Dwarf64; 501 Options.MCOptions.PreserveAsmComments = CodeGenOpts.PreserveAsmComments; 502 Options.MCOptions.Crel = CodeGenOpts.Crel; 503 Options.MCOptions.ImplicitMapSyms = CodeGenOpts.ImplicitMapSyms; 504 Options.MCOptions.X86RelaxRelocations = CodeGenOpts.X86RelaxRelocations; 505 Options.MCOptions.CompressDebugSections = 506 CodeGenOpts.getCompressDebugSections(); 507 if (CodeGenOpts.OutputAsmVariant != 3) // 3 (default): not specified 508 Options.MCOptions.OutputAsmVariant = CodeGenOpts.OutputAsmVariant; 509 Options.MCOptions.ABIName = TargetOpts.ABI; 510 for (const auto &Entry : HSOpts.UserEntries) 511 if (!Entry.IsFramework && 512 (Entry.Group == frontend::IncludeDirGroup::Quoted || 513 Entry.Group == frontend::IncludeDirGroup::Angled || 514 Entry.Group == frontend::IncludeDirGroup::System)) 515 Options.MCOptions.IASSearchPaths.push_back( 516 Entry.IgnoreSysRoot ? Entry.Path : HSOpts.Sysroot + Entry.Path); 517 Options.MCOptions.Argv0 = CodeGenOpts.Argv0 ? CodeGenOpts.Argv0 : ""; 518 Options.MCOptions.CommandlineArgs = flattenClangCommandLine( 519 CodeGenOpts.CommandLineArgs, CodeGenOpts.MainFileName); 520 Options.MCOptions.AsSecureLogFile = CodeGenOpts.AsSecureLogFile; 521 Options.MCOptions.PPCUseFullRegisterNames = 522 CodeGenOpts.PPCUseFullRegisterNames; 523 Options.MisExpect = CodeGenOpts.MisExpect; 524 525 return true; 526 } 527 528 static std::optional<GCOVOptions> 529 getGCOVOptions(const CodeGenOptions &CodeGenOpts, const LangOptions &LangOpts) { 530 if (CodeGenOpts.CoverageNotesFile.empty() && 531 CodeGenOpts.CoverageDataFile.empty()) 532 return std::nullopt; 533 // Not using 'GCOVOptions::getDefault' allows us to avoid exiting if 534 // LLVM's -default-gcov-version flag is set to something invalid. 535 GCOVOptions Options; 536 Options.EmitNotes = !CodeGenOpts.CoverageNotesFile.empty(); 537 Options.EmitData = !CodeGenOpts.CoverageDataFile.empty(); 538 llvm::copy(CodeGenOpts.CoverageVersion, std::begin(Options.Version)); 539 Options.NoRedZone = CodeGenOpts.DisableRedZone; 540 Options.Filter = CodeGenOpts.ProfileFilterFiles; 541 Options.Exclude = CodeGenOpts.ProfileExcludeFiles; 542 Options.Atomic = CodeGenOpts.AtomicProfileUpdate; 543 return Options; 544 } 545 546 static std::optional<InstrProfOptions> 547 getInstrProfOptions(const CodeGenOptions &CodeGenOpts, 548 const LangOptions &LangOpts) { 549 if (!CodeGenOpts.hasProfileClangInstr()) 550 return std::nullopt; 551 InstrProfOptions Options; 552 Options.NoRedZone = CodeGenOpts.DisableRedZone; 553 Options.InstrProfileOutput = CodeGenOpts.InstrProfileOutput; 554 Options.Atomic = CodeGenOpts.AtomicProfileUpdate; 555 return Options; 556 } 557 558 static void setCommandLineOpts(const CodeGenOptions &CodeGenOpts) { 559 SmallVector<const char *, 16> BackendArgs; 560 BackendArgs.push_back("clang"); // Fake program name. 561 if (!CodeGenOpts.DebugPass.empty()) { 562 BackendArgs.push_back("-debug-pass"); 563 BackendArgs.push_back(CodeGenOpts.DebugPass.c_str()); 564 } 565 if (!CodeGenOpts.LimitFloatPrecision.empty()) { 566 BackendArgs.push_back("-limit-float-precision"); 567 BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str()); 568 } 569 // Check for the default "clang" invocation that won't set any cl::opt values. 570 // Skip trying to parse the command line invocation to avoid the issues 571 // described below. 572 if (BackendArgs.size() == 1) 573 return; 574 BackendArgs.push_back(nullptr); 575 // FIXME: The command line parser below is not thread-safe and shares a global 576 // state, so this call might crash or overwrite the options of another Clang 577 // instance in the same process. 578 llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1, 579 BackendArgs.data()); 580 } 581 582 void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) { 583 // Create the TargetMachine for generating code. 584 std::string Error; 585 std::string Triple = TheModule->getTargetTriple(); 586 const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error); 587 if (!TheTarget) { 588 if (MustCreateTM) 589 Diags.Report(diag::err_fe_unable_to_create_target) << Error; 590 return; 591 } 592 593 std::optional<llvm::CodeModel::Model> CM = getCodeModel(CodeGenOpts); 594 std::string FeaturesStr = 595 llvm::join(TargetOpts.Features.begin(), TargetOpts.Features.end(), ","); 596 llvm::Reloc::Model RM = CodeGenOpts.RelocationModel; 597 std::optional<CodeGenOptLevel> OptLevelOrNone = 598 CodeGenOpt::getLevel(CodeGenOpts.OptimizationLevel); 599 assert(OptLevelOrNone && "Invalid optimization level!"); 600 CodeGenOptLevel OptLevel = *OptLevelOrNone; 601 602 llvm::TargetOptions Options; 603 if (!initTargetOptions(Diags, Options, CodeGenOpts, TargetOpts, LangOpts, 604 HSOpts)) 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(ModuleTypeSanitizerPass()); 741 MPM.addPass(createModuleToFunctionPassAdaptor(TypeSanitizerPass())); 742 } 743 744 if (LangOpts.Sanitize.has(SanitizerKind::NumericalStability)) 745 MPM.addPass(NumericalStabilitySanitizerPass()); 746 747 if (LangOpts.Sanitize.has(SanitizerKind::Realtime)) 748 MPM.addPass(RealtimeSanitizerPass()); 749 750 auto ASanPass = [&](SanitizerMask Mask, bool CompileKernel) { 751 if (LangOpts.Sanitize.has(Mask)) { 752 bool UseGlobalGC = asanUseGlobalsGC(TargetTriple, CodeGenOpts); 753 bool UseOdrIndicator = CodeGenOpts.SanitizeAddressUseOdrIndicator; 754 llvm::AsanDtorKind DestructorKind = 755 CodeGenOpts.getSanitizeAddressDtor(); 756 AddressSanitizerOptions Opts; 757 Opts.CompileKernel = CompileKernel; 758 Opts.Recover = CodeGenOpts.SanitizeRecover.has(Mask); 759 Opts.UseAfterScope = CodeGenOpts.SanitizeAddressUseAfterScope; 760 Opts.UseAfterReturn = CodeGenOpts.getSanitizeAddressUseAfterReturn(); 761 MPM.addPass(AddressSanitizerPass(Opts, UseGlobalGC, UseOdrIndicator, 762 DestructorKind)); 763 } 764 }; 765 ASanPass(SanitizerKind::Address, false); 766 ASanPass(SanitizerKind::KernelAddress, true); 767 768 auto HWASanPass = [&](SanitizerMask Mask, bool CompileKernel) { 769 if (LangOpts.Sanitize.has(Mask)) { 770 bool Recover = CodeGenOpts.SanitizeRecover.has(Mask); 771 MPM.addPass(HWAddressSanitizerPass( 772 {CompileKernel, Recover, 773 /*DisableOptimization=*/CodeGenOpts.OptimizationLevel == 0})); 774 } 775 }; 776 HWASanPass(SanitizerKind::HWAddress, false); 777 HWASanPass(SanitizerKind::KernelHWAddress, true); 778 779 if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) { 780 MPM.addPass(DataFlowSanitizerPass(LangOpts.NoSanitizeFiles)); 781 } 782 }; 783 if (ClSanitizeOnOptimizerEarlyEP) { 784 PB.registerOptimizerEarlyEPCallback( 785 [SanitizersCallback](ModulePassManager &MPM, OptimizationLevel Level, 786 ThinOrFullLTOPhase Phase) { 787 ModulePassManager NewMPM; 788 SanitizersCallback(NewMPM, Level, Phase); 789 if (!NewMPM.isEmpty()) { 790 // Sanitizers can abandon<GlobalsAA>. 791 NewMPM.addPass(RequireAnalysisPass<GlobalsAA, llvm::Module>()); 792 MPM.addPass(std::move(NewMPM)); 793 } 794 }); 795 } else { 796 // LastEP does not need GlobalsAA. 797 PB.registerOptimizerLastEPCallback(SanitizersCallback); 798 } 799 800 if (LowerAllowCheckPass::IsRequested()) { 801 // We want to call it after inline, which is about OptimizerEarlyEPCallback. 802 PB.registerOptimizerEarlyEPCallback([](ModulePassManager &MPM, 803 OptimizationLevel Level, 804 ThinOrFullLTOPhase Phase) { 805 MPM.addPass(createModuleToFunctionPassAdaptor(LowerAllowCheckPass())); 806 }); 807 } 808 } 809 810 void EmitAssemblyHelper::RunOptimizationPipeline( 811 BackendAction Action, std::unique_ptr<raw_pwrite_stream> &OS, 812 std::unique_ptr<llvm::ToolOutputFile> &ThinLinkOS, BackendConsumer *BC) { 813 std::optional<PGOOptions> PGOOpt; 814 815 if (CodeGenOpts.hasProfileIRInstr()) 816 // -fprofile-generate. 817 PGOOpt = PGOOptions( 818 CodeGenOpts.InstrProfileOutput.empty() ? getDefaultProfileGenName() 819 : CodeGenOpts.InstrProfileOutput, 820 "", "", CodeGenOpts.MemoryProfileUsePath, nullptr, PGOOptions::IRInstr, 821 PGOOptions::NoCSAction, ClPGOColdFuncAttr, 822 CodeGenOpts.DebugInfoForProfiling, 823 /*PseudoProbeForProfiling=*/false, CodeGenOpts.AtomicProfileUpdate); 824 else if (CodeGenOpts.hasProfileIRUse()) { 825 // -fprofile-use. 826 auto CSAction = CodeGenOpts.hasProfileCSIRUse() ? PGOOptions::CSIRUse 827 : PGOOptions::NoCSAction; 828 PGOOpt = PGOOptions(CodeGenOpts.ProfileInstrumentUsePath, "", 829 CodeGenOpts.ProfileRemappingFile, 830 CodeGenOpts.MemoryProfileUsePath, VFS, 831 PGOOptions::IRUse, CSAction, ClPGOColdFuncAttr, 832 CodeGenOpts.DebugInfoForProfiling); 833 } else if (!CodeGenOpts.SampleProfileFile.empty()) 834 // -fprofile-sample-use 835 PGOOpt = PGOOptions( 836 CodeGenOpts.SampleProfileFile, "", CodeGenOpts.ProfileRemappingFile, 837 CodeGenOpts.MemoryProfileUsePath, VFS, PGOOptions::SampleUse, 838 PGOOptions::NoCSAction, ClPGOColdFuncAttr, 839 CodeGenOpts.DebugInfoForProfiling, CodeGenOpts.PseudoProbeForProfiling); 840 else if (!CodeGenOpts.MemoryProfileUsePath.empty()) 841 // -fmemory-profile-use (without any of the above options) 842 PGOOpt = PGOOptions("", "", "", CodeGenOpts.MemoryProfileUsePath, VFS, 843 PGOOptions::NoAction, PGOOptions::NoCSAction, 844 ClPGOColdFuncAttr, CodeGenOpts.DebugInfoForProfiling); 845 else if (CodeGenOpts.PseudoProbeForProfiling) 846 // -fpseudo-probe-for-profiling 847 PGOOpt = 848 PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr, 849 PGOOptions::NoAction, PGOOptions::NoCSAction, 850 ClPGOColdFuncAttr, CodeGenOpts.DebugInfoForProfiling, true); 851 else if (CodeGenOpts.DebugInfoForProfiling) 852 // -fdebug-info-for-profiling 853 PGOOpt = PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr, 854 PGOOptions::NoAction, PGOOptions::NoCSAction, 855 ClPGOColdFuncAttr, true); 856 857 // Check to see if we want to generate a CS profile. 858 if (CodeGenOpts.hasProfileCSIRInstr()) { 859 assert(!CodeGenOpts.hasProfileCSIRUse() && 860 "Cannot have both CSProfileUse pass and CSProfileGen pass at " 861 "the same time"); 862 if (PGOOpt) { 863 assert(PGOOpt->Action != PGOOptions::IRInstr && 864 PGOOpt->Action != PGOOptions::SampleUse && 865 "Cannot run CSProfileGen pass with ProfileGen or SampleUse " 866 " pass"); 867 PGOOpt->CSProfileGenFile = CodeGenOpts.InstrProfileOutput.empty() 868 ? getDefaultProfileGenName() 869 : CodeGenOpts.InstrProfileOutput; 870 PGOOpt->CSAction = PGOOptions::CSIRInstr; 871 } else 872 PGOOpt = PGOOptions("", 873 CodeGenOpts.InstrProfileOutput.empty() 874 ? getDefaultProfileGenName() 875 : CodeGenOpts.InstrProfileOutput, 876 "", /*MemoryProfile=*/"", nullptr, 877 PGOOptions::NoAction, PGOOptions::CSIRInstr, 878 ClPGOColdFuncAttr, CodeGenOpts.DebugInfoForProfiling); 879 } 880 if (TM) 881 TM->setPGOOption(PGOOpt); 882 883 PipelineTuningOptions PTO; 884 PTO.LoopUnrolling = CodeGenOpts.UnrollLoops; 885 // For historical reasons, loop interleaving is set to mirror setting for loop 886 // unrolling. 887 PTO.LoopInterleaving = CodeGenOpts.UnrollLoops; 888 PTO.LoopVectorization = CodeGenOpts.VectorizeLoop; 889 PTO.SLPVectorization = CodeGenOpts.VectorizeSLP; 890 PTO.MergeFunctions = CodeGenOpts.MergeFunctions; 891 // Only enable CGProfilePass when using integrated assembler, since 892 // non-integrated assemblers don't recognize .cgprofile section. 893 PTO.CallGraphProfile = !CodeGenOpts.DisableIntegratedAS; 894 PTO.UnifiedLTO = CodeGenOpts.UnifiedLTO; 895 896 LoopAnalysisManager LAM; 897 FunctionAnalysisManager FAM; 898 CGSCCAnalysisManager CGAM; 899 ModuleAnalysisManager MAM; 900 901 bool DebugPassStructure = CodeGenOpts.DebugPass == "Structure"; 902 PassInstrumentationCallbacks PIC; 903 PrintPassOptions PrintPassOpts; 904 PrintPassOpts.Indent = DebugPassStructure; 905 PrintPassOpts.SkipAnalyses = DebugPassStructure; 906 StandardInstrumentations SI( 907 TheModule->getContext(), 908 (CodeGenOpts.DebugPassManager || DebugPassStructure), 909 CodeGenOpts.VerifyEach, PrintPassOpts); 910 SI.registerCallbacks(PIC, &MAM); 911 PassBuilder PB(TM.get(), PTO, PGOOpt, &PIC); 912 913 // Handle the assignment tracking feature options. 914 switch (CodeGenOpts.getAssignmentTrackingMode()) { 915 case CodeGenOptions::AssignmentTrackingOpts::Forced: 916 PB.registerPipelineStartEPCallback( 917 [&](ModulePassManager &MPM, OptimizationLevel Level) { 918 MPM.addPass(AssignmentTrackingPass()); 919 }); 920 break; 921 case CodeGenOptions::AssignmentTrackingOpts::Enabled: 922 // Disable assignment tracking in LTO builds for now as the performance 923 // cost is too high. Disable for LLDB tuning due to llvm.org/PR43126. 924 if (!CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.PrepareForLTO && 925 CodeGenOpts.getDebuggerTuning() != llvm::DebuggerKind::LLDB) { 926 PB.registerPipelineStartEPCallback( 927 [&](ModulePassManager &MPM, OptimizationLevel Level) { 928 // Only use assignment tracking if optimisations are enabled. 929 if (Level != OptimizationLevel::O0) 930 MPM.addPass(AssignmentTrackingPass()); 931 }); 932 } 933 break; 934 case CodeGenOptions::AssignmentTrackingOpts::Disabled: 935 break; 936 } 937 938 // Enable verify-debuginfo-preserve-each for new PM. 939 DebugifyEachInstrumentation Debugify; 940 DebugInfoPerPass DebugInfoBeforePass; 941 if (CodeGenOpts.EnableDIPreservationVerify) { 942 Debugify.setDebugifyMode(DebugifyMode::OriginalDebugInfo); 943 Debugify.setDebugInfoBeforePass(DebugInfoBeforePass); 944 945 if (!CodeGenOpts.DIBugsReportFilePath.empty()) 946 Debugify.setOrigDIVerifyBugsReportFilePath( 947 CodeGenOpts.DIBugsReportFilePath); 948 Debugify.registerCallbacks(PIC, MAM); 949 } 950 // Attempt to load pass plugins and register their callbacks with PB. 951 for (auto &PluginFN : CodeGenOpts.PassPlugins) { 952 auto PassPlugin = PassPlugin::Load(PluginFN); 953 if (PassPlugin) { 954 PassPlugin->registerPassBuilderCallbacks(PB); 955 } else { 956 Diags.Report(diag::err_fe_unable_to_load_plugin) 957 << PluginFN << toString(PassPlugin.takeError()); 958 } 959 } 960 for (const auto &PassCallback : CodeGenOpts.PassBuilderCallbacks) 961 PassCallback(PB); 962 #define HANDLE_EXTENSION(Ext) \ 963 get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB); 964 #include "llvm/Support/Extension.def" 965 966 // Register the target library analysis directly and give it a customized 967 // preset TLI. 968 std::unique_ptr<TargetLibraryInfoImpl> TLII( 969 llvm::driver::createTLII(TargetTriple, CodeGenOpts.getVecLib())); 970 FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); }); 971 972 // Register all the basic analyses with the managers. 973 PB.registerModuleAnalyses(MAM); 974 PB.registerCGSCCAnalyses(CGAM); 975 PB.registerFunctionAnalyses(FAM); 976 PB.registerLoopAnalyses(LAM); 977 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); 978 979 ModulePassManager MPM; 980 // Add a verifier pass, before any other passes, to catch CodeGen issues. 981 if (CodeGenOpts.VerifyModule) 982 MPM.addPass(VerifierPass()); 983 984 if (!CodeGenOpts.DisableLLVMPasses) { 985 // Map our optimization levels into one of the distinct levels used to 986 // configure the pipeline. 987 OptimizationLevel Level = mapToLevel(CodeGenOpts); 988 989 const bool PrepareForThinLTO = CodeGenOpts.PrepareForThinLTO; 990 const bool PrepareForLTO = CodeGenOpts.PrepareForLTO; 991 992 if (LangOpts.ObjCAutoRefCount) { 993 PB.registerPipelineStartEPCallback( 994 [](ModulePassManager &MPM, OptimizationLevel Level) { 995 if (Level != OptimizationLevel::O0) 996 MPM.addPass( 997 createModuleToFunctionPassAdaptor(ObjCARCExpandPass())); 998 }); 999 PB.registerPipelineEarlySimplificationEPCallback( 1000 [](ModulePassManager &MPM, OptimizationLevel Level, 1001 ThinOrFullLTOPhase) { 1002 if (Level != OptimizationLevel::O0) 1003 MPM.addPass(ObjCARCAPElimPass()); 1004 }); 1005 PB.registerScalarOptimizerLateEPCallback( 1006 [](FunctionPassManager &FPM, OptimizationLevel Level) { 1007 if (Level != OptimizationLevel::O0) 1008 FPM.addPass(ObjCARCOptPass()); 1009 }); 1010 } 1011 1012 // If we reached here with a non-empty index file name, then the index 1013 // file was empty and we are not performing ThinLTO backend compilation 1014 // (used in testing in a distributed build environment). 1015 bool IsThinLTOPostLink = !CodeGenOpts.ThinLTOIndexFile.empty(); 1016 // If so drop any the type test assume sequences inserted for whole program 1017 // vtables so that codegen doesn't complain. 1018 if (IsThinLTOPostLink) 1019 PB.registerPipelineStartEPCallback( 1020 [](ModulePassManager &MPM, OptimizationLevel Level) { 1021 MPM.addPass(LowerTypeTestsPass( 1022 /*ExportSummary=*/nullptr, 1023 /*ImportSummary=*/nullptr, 1024 /*DropTypeTests=*/lowertypetests::DropTestKind::Assume)); 1025 }); 1026 1027 // Register callbacks to schedule sanitizer passes at the appropriate part 1028 // of the pipeline. 1029 if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds)) 1030 PB.registerScalarOptimizerLateEPCallback( 1031 [](FunctionPassManager &FPM, OptimizationLevel Level) { 1032 FPM.addPass( 1033 BoundsCheckingPass(BoundsCheckingPass::ReportingMode::Trap)); 1034 }); 1035 1036 // Don't add sanitizers if we are here from ThinLTO PostLink. That already 1037 // done on PreLink stage. 1038 if (!IsThinLTOPostLink) { 1039 addSanitizers(TargetTriple, CodeGenOpts, LangOpts, PB); 1040 addKCFIPass(TargetTriple, LangOpts, PB); 1041 } 1042 1043 if (std::optional<GCOVOptions> Options = 1044 getGCOVOptions(CodeGenOpts, LangOpts)) 1045 PB.registerPipelineStartEPCallback( 1046 [Options](ModulePassManager &MPM, OptimizationLevel Level) { 1047 MPM.addPass(GCOVProfilerPass(*Options)); 1048 }); 1049 if (std::optional<InstrProfOptions> Options = 1050 getInstrProfOptions(CodeGenOpts, LangOpts)) 1051 PB.registerPipelineStartEPCallback( 1052 [Options](ModulePassManager &MPM, OptimizationLevel Level) { 1053 MPM.addPass(InstrProfilingLoweringPass(*Options, false)); 1054 }); 1055 1056 // TODO: Consider passing the MemoryProfileOutput to the pass builder via 1057 // the PGOOptions, and set this up there. 1058 if (!CodeGenOpts.MemoryProfileOutput.empty()) { 1059 PB.registerOptimizerLastEPCallback([](ModulePassManager &MPM, 1060 OptimizationLevel Level, 1061 ThinOrFullLTOPhase) { 1062 MPM.addPass(createModuleToFunctionPassAdaptor(MemProfilerPass())); 1063 MPM.addPass(ModuleMemProfilerPass()); 1064 }); 1065 } 1066 1067 if (CodeGenOpts.FatLTO) { 1068 MPM.addPass(PB.buildFatLTODefaultPipeline( 1069 Level, PrepareForThinLTO, 1070 PrepareForThinLTO || shouldEmitRegularLTOSummary())); 1071 } else if (PrepareForThinLTO) { 1072 MPM.addPass(PB.buildThinLTOPreLinkDefaultPipeline(Level)); 1073 } else if (PrepareForLTO) { 1074 MPM.addPass(PB.buildLTOPreLinkDefaultPipeline(Level)); 1075 } else { 1076 MPM.addPass(PB.buildPerModuleDefaultPipeline(Level)); 1077 } 1078 } 1079 1080 // Link against bitcodes supplied via the -mlink-builtin-bitcode option 1081 if (CodeGenOpts.LinkBitcodePostopt) 1082 MPM.addPass(LinkInModulesPass(BC)); 1083 1084 // Add a verifier pass if requested. We don't have to do this if the action 1085 // requires code generation because there will already be a verifier pass in 1086 // the code-generation pipeline. 1087 // Since we already added a verifier pass above, this 1088 // might even not run the analysis, if previous passes caused no changes. 1089 if (!actionRequiresCodeGen(Action) && CodeGenOpts.VerifyModule) 1090 MPM.addPass(VerifierPass()); 1091 1092 if (Action == Backend_EmitBC || Action == Backend_EmitLL || 1093 CodeGenOpts.FatLTO) { 1094 if (CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.DisableLLVMPasses) { 1095 if (!TheModule->getModuleFlag("EnableSplitLTOUnit")) 1096 TheModule->addModuleFlag(llvm::Module::Error, "EnableSplitLTOUnit", 1097 CodeGenOpts.EnableSplitLTOUnit); 1098 if (Action == Backend_EmitBC) { 1099 if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) { 1100 ThinLinkOS = openOutputFile(CodeGenOpts.ThinLinkBitcodeFile); 1101 if (!ThinLinkOS) 1102 return; 1103 } 1104 MPM.addPass(ThinLTOBitcodeWriterPass( 1105 *OS, ThinLinkOS ? &ThinLinkOS->os() : nullptr)); 1106 } else if (Action == Backend_EmitLL) { 1107 MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists, 1108 /*EmitLTOSummary=*/true)); 1109 } 1110 } else { 1111 // Emit a module summary by default for Regular LTO except for ld64 1112 // targets 1113 bool EmitLTOSummary = shouldEmitRegularLTOSummary(); 1114 if (EmitLTOSummary) { 1115 if (!TheModule->getModuleFlag("ThinLTO") && !CodeGenOpts.UnifiedLTO) 1116 TheModule->addModuleFlag(llvm::Module::Error, "ThinLTO", uint32_t(0)); 1117 if (!TheModule->getModuleFlag("EnableSplitLTOUnit")) 1118 TheModule->addModuleFlag(llvm::Module::Error, "EnableSplitLTOUnit", 1119 uint32_t(1)); 1120 } 1121 if (Action == Backend_EmitBC) { 1122 MPM.addPass(BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists, 1123 EmitLTOSummary)); 1124 } else if (Action == Backend_EmitLL) { 1125 MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists, 1126 EmitLTOSummary)); 1127 } 1128 } 1129 1130 if (shouldEmitUnifiedLTOModueFlag()) 1131 TheModule->addModuleFlag(llvm::Module::Error, "UnifiedLTO", uint32_t(1)); 1132 } 1133 1134 // FIXME: This should eventually be replaced by a first-class driver option. 1135 // This should be done for both clang and flang simultaneously. 1136 // Print a textual, '-passes=' compatible, representation of pipeline if 1137 // requested. 1138 if (PrintPipelinePasses) { 1139 MPM.printPipeline(outs(), [&PIC](StringRef ClassName) { 1140 auto PassName = PIC.getPassNameForClassName(ClassName); 1141 return PassName.empty() ? ClassName : PassName; 1142 }); 1143 outs() << "\n"; 1144 return; 1145 } 1146 1147 if (LangOpts.HIPStdPar && !LangOpts.CUDAIsDevice && 1148 LangOpts.HIPStdParInterposeAlloc) 1149 MPM.addPass(HipStdParAllocationInterpositionPass()); 1150 1151 // Now that we have all of the passes ready, run them. 1152 { 1153 PrettyStackTraceString CrashInfo("Optimizer"); 1154 llvm::TimeTraceScope TimeScope("Optimizer"); 1155 MPM.run(*TheModule, MAM); 1156 } 1157 } 1158 1159 void EmitAssemblyHelper::RunCodegenPipeline( 1160 BackendAction Action, std::unique_ptr<raw_pwrite_stream> &OS, 1161 std::unique_ptr<llvm::ToolOutputFile> &DwoOS) { 1162 // We still use the legacy PM to run the codegen pipeline since the new PM 1163 // does not work with the codegen pipeline. 1164 // FIXME: make the new PM work with the codegen pipeline. 1165 legacy::PassManager CodeGenPasses; 1166 1167 // Append any output we need to the pass manager. 1168 switch (Action) { 1169 case Backend_EmitAssembly: 1170 case Backend_EmitMCNull: 1171 case Backend_EmitObj: 1172 CodeGenPasses.add( 1173 createTargetTransformInfoWrapperPass(getTargetIRAnalysis())); 1174 if (!CodeGenOpts.SplitDwarfOutput.empty()) { 1175 DwoOS = openOutputFile(CodeGenOpts.SplitDwarfOutput); 1176 if (!DwoOS) 1177 return; 1178 } 1179 if (!AddEmitPasses(CodeGenPasses, Action, *OS, 1180 DwoOS ? &DwoOS->os() : nullptr)) 1181 // FIXME: Should we handle this error differently? 1182 return; 1183 break; 1184 default: 1185 return; 1186 } 1187 1188 // If -print-pipeline-passes is requested, don't run the legacy pass manager. 1189 // FIXME: when codegen is switched to use the new pass manager, it should also 1190 // emit pass names here. 1191 if (PrintPipelinePasses) { 1192 return; 1193 } 1194 1195 { 1196 PrettyStackTraceString CrashInfo("Code generation"); 1197 llvm::TimeTraceScope TimeScope("CodeGenPasses"); 1198 CodeGenPasses.run(*TheModule); 1199 } 1200 } 1201 1202 void EmitAssemblyHelper::EmitAssembly(BackendAction Action, 1203 std::unique_ptr<raw_pwrite_stream> OS, 1204 BackendConsumer *BC) { 1205 TimeRegion Region(CodeGenOpts.TimePasses ? &CodeGenerationTime : nullptr); 1206 setCommandLineOpts(CodeGenOpts); 1207 1208 bool RequiresCodeGen = actionRequiresCodeGen(Action); 1209 CreateTargetMachine(RequiresCodeGen); 1210 1211 if (RequiresCodeGen && !TM) 1212 return; 1213 if (TM) 1214 TheModule->setDataLayout(TM->createDataLayout()); 1215 1216 // Before executing passes, print the final values of the LLVM options. 1217 cl::PrintOptionValues(); 1218 1219 std::unique_ptr<llvm::ToolOutputFile> ThinLinkOS, DwoOS; 1220 RunOptimizationPipeline(Action, OS, ThinLinkOS, BC); 1221 RunCodegenPipeline(Action, OS, DwoOS); 1222 1223 if (ThinLinkOS) 1224 ThinLinkOS->keep(); 1225 if (DwoOS) 1226 DwoOS->keep(); 1227 } 1228 1229 static void runThinLTOBackend( 1230 DiagnosticsEngine &Diags, ModuleSummaryIndex *CombinedIndex, 1231 llvm::Module *M, const HeaderSearchOptions &HeaderOpts, 1232 const CodeGenOptions &CGOpts, const clang::TargetOptions &TOpts, 1233 const LangOptions &LOpts, std::unique_ptr<raw_pwrite_stream> OS, 1234 std::string SampleProfile, std::string ProfileRemapping, 1235 BackendAction Action) { 1236 DenseMap<StringRef, DenseMap<GlobalValue::GUID, GlobalValueSummary *>> 1237 ModuleToDefinedGVSummaries; 1238 CombinedIndex->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); 1239 1240 setCommandLineOpts(CGOpts); 1241 1242 // We can simply import the values mentioned in the combined index, since 1243 // we should only invoke this using the individual indexes written out 1244 // via a WriteIndexesThinBackend. 1245 FunctionImporter::ImportIDTable ImportIDs; 1246 FunctionImporter::ImportMapTy ImportList(ImportIDs); 1247 if (!lto::initImportList(*M, *CombinedIndex, ImportList)) 1248 return; 1249 1250 auto AddStream = [&](size_t Task, const Twine &ModuleName) { 1251 return std::make_unique<CachedFileStream>(std::move(OS), 1252 CGOpts.ObjectFilenameForDebug); 1253 }; 1254 lto::Config Conf; 1255 if (CGOpts.SaveTempsFilePrefix != "") { 1256 if (Error E = Conf.addSaveTemps(CGOpts.SaveTempsFilePrefix + ".", 1257 /* UseInputModulePath */ false)) { 1258 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) { 1259 errs() << "Error setting up ThinLTO save-temps: " << EIB.message() 1260 << '\n'; 1261 }); 1262 } 1263 } 1264 Conf.CPU = TOpts.CPU; 1265 Conf.CodeModel = getCodeModel(CGOpts); 1266 Conf.MAttrs = TOpts.Features; 1267 Conf.RelocModel = CGOpts.RelocationModel; 1268 std::optional<CodeGenOptLevel> OptLevelOrNone = 1269 CodeGenOpt::getLevel(CGOpts.OptimizationLevel); 1270 assert(OptLevelOrNone && "Invalid optimization level!"); 1271 Conf.CGOptLevel = *OptLevelOrNone; 1272 Conf.OptLevel = CGOpts.OptimizationLevel; 1273 initTargetOptions(Diags, Conf.Options, CGOpts, TOpts, LOpts, HeaderOpts); 1274 Conf.SampleProfile = std::move(SampleProfile); 1275 Conf.PTO.LoopUnrolling = CGOpts.UnrollLoops; 1276 // For historical reasons, loop interleaving is set to mirror setting for loop 1277 // unrolling. 1278 Conf.PTO.LoopInterleaving = CGOpts.UnrollLoops; 1279 Conf.PTO.LoopVectorization = CGOpts.VectorizeLoop; 1280 Conf.PTO.SLPVectorization = CGOpts.VectorizeSLP; 1281 // Only enable CGProfilePass when using integrated assembler, since 1282 // non-integrated assemblers don't recognize .cgprofile section. 1283 Conf.PTO.CallGraphProfile = !CGOpts.DisableIntegratedAS; 1284 1285 // Context sensitive profile. 1286 if (CGOpts.hasProfileCSIRInstr()) { 1287 Conf.RunCSIRInstr = true; 1288 Conf.CSIRProfile = std::move(CGOpts.InstrProfileOutput); 1289 } else if (CGOpts.hasProfileCSIRUse()) { 1290 Conf.RunCSIRInstr = false; 1291 Conf.CSIRProfile = std::move(CGOpts.ProfileInstrumentUsePath); 1292 } 1293 1294 Conf.ProfileRemapping = std::move(ProfileRemapping); 1295 Conf.DebugPassManager = CGOpts.DebugPassManager; 1296 Conf.VerifyEach = CGOpts.VerifyEach; 1297 Conf.RemarksWithHotness = CGOpts.DiagnosticsWithHotness; 1298 Conf.RemarksFilename = CGOpts.OptRecordFile; 1299 Conf.RemarksPasses = CGOpts.OptRecordPasses; 1300 Conf.RemarksFormat = CGOpts.OptRecordFormat; 1301 Conf.SplitDwarfFile = CGOpts.SplitDwarfFile; 1302 Conf.SplitDwarfOutput = CGOpts.SplitDwarfOutput; 1303 switch (Action) { 1304 case Backend_EmitNothing: 1305 Conf.PreCodeGenModuleHook = [](size_t Task, const llvm::Module &Mod) { 1306 return false; 1307 }; 1308 break; 1309 case Backend_EmitLL: 1310 Conf.PreCodeGenModuleHook = [&](size_t Task, const llvm::Module &Mod) { 1311 M->print(*OS, nullptr, CGOpts.EmitLLVMUseLists); 1312 return false; 1313 }; 1314 break; 1315 case Backend_EmitBC: 1316 Conf.PreCodeGenModuleHook = [&](size_t Task, const llvm::Module &Mod) { 1317 WriteBitcodeToFile(*M, *OS, CGOpts.EmitLLVMUseLists); 1318 return false; 1319 }; 1320 break; 1321 default: 1322 Conf.CGFileType = getCodeGenFileType(Action); 1323 break; 1324 } 1325 if (Error E = 1326 thinBackend(Conf, -1, AddStream, *M, *CombinedIndex, ImportList, 1327 ModuleToDefinedGVSummaries[M->getModuleIdentifier()], 1328 /*ModuleMap=*/nullptr, Conf.CodeGenOnly, 1329 /*IRAddStream=*/nullptr, CGOpts.CmdArgs)) { 1330 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) { 1331 errs() << "Error running ThinLTO backend: " << EIB.message() << '\n'; 1332 }); 1333 } 1334 } 1335 1336 void clang::EmitBackendOutput( 1337 DiagnosticsEngine &Diags, const HeaderSearchOptions &HeaderOpts, 1338 const CodeGenOptions &CGOpts, const clang::TargetOptions &TOpts, 1339 const LangOptions &LOpts, StringRef TDesc, llvm::Module *M, 1340 BackendAction Action, IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, 1341 std::unique_ptr<raw_pwrite_stream> OS, BackendConsumer *BC) { 1342 1343 llvm::TimeTraceScope TimeScope("Backend"); 1344 1345 std::unique_ptr<llvm::Module> EmptyModule; 1346 if (!CGOpts.ThinLTOIndexFile.empty()) { 1347 // If we are performing a ThinLTO importing compile, load the function index 1348 // into memory and pass it into runThinLTOBackend, which will run the 1349 // function importer and invoke LTO passes. 1350 std::unique_ptr<ModuleSummaryIndex> CombinedIndex; 1351 if (Error E = llvm::getModuleSummaryIndexForFile( 1352 CGOpts.ThinLTOIndexFile, 1353 /*IgnoreEmptyThinLTOIndexFile*/ true) 1354 .moveInto(CombinedIndex)) { 1355 logAllUnhandledErrors(std::move(E), errs(), 1356 "Error loading index file '" + 1357 CGOpts.ThinLTOIndexFile + "': "); 1358 return; 1359 } 1360 1361 // A null CombinedIndex means we should skip ThinLTO compilation 1362 // (LLVM will optionally ignore empty index files, returning null instead 1363 // of an error). 1364 if (CombinedIndex) { 1365 if (!CombinedIndex->skipModuleByDistributedBackend()) { 1366 runThinLTOBackend(Diags, CombinedIndex.get(), M, HeaderOpts, CGOpts, 1367 TOpts, LOpts, std::move(OS), CGOpts.SampleProfileFile, 1368 CGOpts.ProfileRemappingFile, Action); 1369 return; 1370 } 1371 // Distributed indexing detected that nothing from the module is needed 1372 // for the final linking. So we can skip the compilation. We sill need to 1373 // output an empty object file to make sure that a linker does not fail 1374 // trying to read it. Also for some features, like CFI, we must skip 1375 // the compilation as CombinedIndex does not contain all required 1376 // information. 1377 EmptyModule = std::make_unique<llvm::Module>("empty", M->getContext()); 1378 EmptyModule->setTargetTriple(M->getTargetTriple()); 1379 M = EmptyModule.get(); 1380 } 1381 } 1382 1383 EmitAssemblyHelper AsmHelper(Diags, HeaderOpts, CGOpts, TOpts, LOpts, M, VFS); 1384 AsmHelper.EmitAssembly(Action, std::move(OS), BC); 1385 1386 // Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's 1387 // DataLayout. 1388 if (AsmHelper.TM) { 1389 std::string DLDesc = M->getDataLayout().getStringRepresentation(); 1390 if (DLDesc != TDesc) { 1391 unsigned DiagID = Diags.getCustomDiagID( 1392 DiagnosticsEngine::Error, "backend data layout '%0' does not match " 1393 "expected target description '%1'"); 1394 Diags.Report(DiagID) << DLDesc << TDesc; 1395 } 1396 } 1397 } 1398 1399 // With -fembed-bitcode, save a copy of the llvm IR as data in the 1400 // __LLVM,__bitcode section. 1401 void clang::EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts, 1402 llvm::MemoryBufferRef Buf) { 1403 if (CGOpts.getEmbedBitcode() == CodeGenOptions::Embed_Off) 1404 return; 1405 llvm::embedBitcodeInModule( 1406 *M, Buf, CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Marker, 1407 CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Bitcode, 1408 CGOpts.CmdArgs); 1409 } 1410 1411 void clang::EmbedObject(llvm::Module *M, const CodeGenOptions &CGOpts, 1412 DiagnosticsEngine &Diags) { 1413 if (CGOpts.OffloadObjects.empty()) 1414 return; 1415 1416 for (StringRef OffloadObject : CGOpts.OffloadObjects) { 1417 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> ObjectOrErr = 1418 llvm::MemoryBuffer::getFileOrSTDIN(OffloadObject); 1419 if (ObjectOrErr.getError()) { 1420 auto DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 1421 "could not open '%0' for embedding"); 1422 Diags.Report(DiagID) << OffloadObject; 1423 return; 1424 } 1425 1426 llvm::embedBufferInModule(*M, **ObjectOrErr, ".llvm.offloading", 1427 Align(object::OffloadBinary::getAlignment())); 1428 } 1429 } 1430