1 //===--- AMDGPU.cpp - AMDGPU ToolChain Implementations ----------*- C++ -*-===// 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 "AMDGPU.h" 10 #include "CommonArgs.h" 11 #include "InputInfo.h" 12 #include "clang/Basic/TargetID.h" 13 #include "clang/Driver/Compilation.h" 14 #include "clang/Driver/DriverDiagnostic.h" 15 #include "llvm/Option/ArgList.h" 16 #include "llvm/Support/Path.h" 17 #include "llvm/Support/VirtualFileSystem.h" 18 19 using namespace clang::driver; 20 using namespace clang::driver::tools; 21 using namespace clang::driver::toolchains; 22 using namespace clang; 23 using namespace llvm::opt; 24 25 void RocmInstallationDetector::scanLibDevicePath(llvm::StringRef Path) { 26 assert(!Path.empty()); 27 28 const StringRef Suffix(".bc"); 29 const StringRef Suffix2(".amdgcn.bc"); 30 31 std::error_code EC; 32 for (llvm::vfs::directory_iterator LI = D.getVFS().dir_begin(Path, EC), LE; 33 !EC && LI != LE; LI = LI.increment(EC)) { 34 StringRef FilePath = LI->path(); 35 StringRef FileName = llvm::sys::path::filename(FilePath); 36 if (!FileName.endswith(Suffix)) 37 continue; 38 39 StringRef BaseName; 40 if (FileName.endswith(Suffix2)) 41 BaseName = FileName.drop_back(Suffix2.size()); 42 else if (FileName.endswith(Suffix)) 43 BaseName = FileName.drop_back(Suffix.size()); 44 45 if (BaseName == "ocml") { 46 OCML = FilePath; 47 } else if (BaseName == "ockl") { 48 OCKL = FilePath; 49 } else if (BaseName == "opencl") { 50 OpenCL = FilePath; 51 } else if (BaseName == "hip") { 52 HIP = FilePath; 53 } else if (BaseName == "oclc_finite_only_off") { 54 FiniteOnly.Off = FilePath; 55 } else if (BaseName == "oclc_finite_only_on") { 56 FiniteOnly.On = FilePath; 57 } else if (BaseName == "oclc_daz_opt_on") { 58 DenormalsAreZero.On = FilePath; 59 } else if (BaseName == "oclc_daz_opt_off") { 60 DenormalsAreZero.Off = FilePath; 61 } else if (BaseName == "oclc_correctly_rounded_sqrt_on") { 62 CorrectlyRoundedSqrt.On = FilePath; 63 } else if (BaseName == "oclc_correctly_rounded_sqrt_off") { 64 CorrectlyRoundedSqrt.Off = FilePath; 65 } else if (BaseName == "oclc_unsafe_math_on") { 66 UnsafeMath.On = FilePath; 67 } else if (BaseName == "oclc_unsafe_math_off") { 68 UnsafeMath.Off = FilePath; 69 } else if (BaseName == "oclc_wavefrontsize64_on") { 70 WavefrontSize64.On = FilePath; 71 } else if (BaseName == "oclc_wavefrontsize64_off") { 72 WavefrontSize64.Off = FilePath; 73 } else { 74 // Process all bitcode filenames that look like 75 // ocl_isa_version_XXX.amdgcn.bc 76 const StringRef DeviceLibPrefix = "oclc_isa_version_"; 77 if (!BaseName.startswith(DeviceLibPrefix)) 78 continue; 79 80 StringRef IsaVersionNumber = 81 BaseName.drop_front(DeviceLibPrefix.size()); 82 83 llvm::Twine GfxName = Twine("gfx") + IsaVersionNumber; 84 SmallString<8> Tmp; 85 LibDeviceMap.insert( 86 std::make_pair(GfxName.toStringRef(Tmp), FilePath.str())); 87 } 88 } 89 } 90 91 void RocmInstallationDetector::ParseHIPVersionFile(llvm::StringRef V) { 92 SmallVector<StringRef, 4> VersionParts; 93 V.split(VersionParts, '\n'); 94 unsigned Major; 95 unsigned Minor; 96 for (auto Part : VersionParts) { 97 auto Splits = Part.split('='); 98 if (Splits.first == "HIP_VERSION_MAJOR") 99 Splits.second.getAsInteger(0, Major); 100 else if (Splits.first == "HIP_VERSION_MINOR") 101 Splits.second.getAsInteger(0, Minor); 102 else if (Splits.first == "HIP_VERSION_PATCH") 103 VersionPatch = Splits.second.str(); 104 } 105 VersionMajorMinor = llvm::VersionTuple(Major, Minor); 106 DetectedVersion = 107 (Twine(Major) + "." + Twine(Minor) + "." + VersionPatch).str(); 108 } 109 110 // For candidate specified by --rocm-path we do not do strict check. 111 SmallVector<RocmInstallationDetector::Candidate, 4> 112 RocmInstallationDetector::getInstallationPathCandidates() { 113 SmallVector<Candidate, 4> Candidates; 114 if (!RocmPathArg.empty()) { 115 Candidates.emplace_back(RocmPathArg.str()); 116 return Candidates; 117 } 118 119 // Try to find relative to the compiler binary. 120 const char *InstallDir = D.getInstalledDir(); 121 122 // Check both a normal Unix prefix position of the clang binary, as well as 123 // the Windows-esque layout the ROCm packages use with the host architecture 124 // subdirectory of bin. 125 126 // Strip off directory (usually bin) 127 StringRef ParentDir = llvm::sys::path::parent_path(InstallDir); 128 StringRef ParentName = llvm::sys::path::filename(ParentDir); 129 130 // Some builds use bin/{host arch}, so go up again. 131 if (ParentName == "bin") { 132 ParentDir = llvm::sys::path::parent_path(ParentDir); 133 ParentName = llvm::sys::path::filename(ParentDir); 134 } 135 136 // Some versions of the rocm llvm package install to /opt/rocm/llvm/bin 137 if (ParentName == "llvm") 138 ParentDir = llvm::sys::path::parent_path(ParentDir); 139 140 Candidates.emplace_back(ParentDir.str(), /*StrictChecking=*/true); 141 142 // Device library may be installed in clang resource directory. 143 Candidates.emplace_back(D.ResourceDir, /*StrictChecking=*/true); 144 145 Candidates.emplace_back(D.SysRoot + "/opt/rocm", /*StrictChecking=*/true); 146 return Candidates; 147 } 148 149 RocmInstallationDetector::RocmInstallationDetector( 150 const Driver &D, const llvm::Triple &HostTriple, 151 const llvm::opt::ArgList &Args, bool DetectHIPRuntime, bool DetectDeviceLib) 152 : D(D) { 153 RocmPathArg = Args.getLastArgValue(clang::driver::options::OPT_rocm_path_EQ); 154 RocmDeviceLibPathArg = 155 Args.getAllArgValues(clang::driver::options::OPT_rocm_device_lib_path_EQ); 156 if (auto *A = Args.getLastArg(clang::driver::options::OPT_hip_version_EQ)) { 157 HIPVersionArg = A->getValue(); 158 unsigned Major = 0; 159 unsigned Minor = 0; 160 SmallVector<StringRef, 3> Parts; 161 HIPVersionArg.split(Parts, '.'); 162 if (Parts.size()) 163 Parts[0].getAsInteger(0, Major); 164 if (Parts.size() > 1) 165 Parts[1].getAsInteger(0, Minor); 166 if (Parts.size() > 2) 167 VersionPatch = Parts[2].str(); 168 if (VersionPatch.empty()) 169 VersionPatch = "0"; 170 if (Major == 0 || Minor == 0) 171 D.Diag(diag::err_drv_invalid_value) 172 << A->getAsString(Args) << HIPVersionArg; 173 174 VersionMajorMinor = llvm::VersionTuple(Major, Minor); 175 DetectedVersion = 176 (Twine(Major) + "." + Twine(Minor) + "." + VersionPatch).str(); 177 } else { 178 VersionPatch = DefaultVersionPatch; 179 VersionMajorMinor = 180 llvm::VersionTuple(DefaultVersionMajor, DefaultVersionMinor); 181 DetectedVersion = (Twine(DefaultVersionMajor) + "." + 182 Twine(DefaultVersionMinor) + "." + VersionPatch) 183 .str(); 184 } 185 186 if (DetectHIPRuntime) 187 detectHIPRuntime(); 188 if (DetectDeviceLib) 189 detectDeviceLibrary(); 190 } 191 192 void RocmInstallationDetector::detectDeviceLibrary() { 193 assert(LibDevicePath.empty()); 194 195 if (!RocmDeviceLibPathArg.empty()) 196 LibDevicePath = RocmDeviceLibPathArg[RocmDeviceLibPathArg.size() - 1]; 197 else if (const char *LibPathEnv = ::getenv("HIP_DEVICE_LIB_PATH")) 198 LibDevicePath = LibPathEnv; 199 200 auto &FS = D.getVFS(); 201 if (!LibDevicePath.empty()) { 202 // Maintain compatability with HIP flag/envvar pointing directly at the 203 // bitcode library directory. This points directly at the library path instead 204 // of the rocm root installation. 205 if (!FS.exists(LibDevicePath)) 206 return; 207 208 scanLibDevicePath(LibDevicePath); 209 HasDeviceLibrary = allGenericLibsValid() && !LibDeviceMap.empty(); 210 return; 211 } 212 213 // The install path situation in old versions of ROCm is a real mess, and 214 // use a different install layout. Multiple copies of the device libraries 215 // exist for each frontend project, and differ depending on which build 216 // system produced the packages. Standalone OpenCL builds also have a 217 // different directory structure from the ROCm OpenCL package. 218 auto Candidates = getInstallationPathCandidates(); 219 for (const auto &Candidate : Candidates) { 220 auto CandidatePath = Candidate.Path; 221 222 // Check device library exists at the given path. 223 auto CheckDeviceLib = [&](StringRef Path) { 224 bool CheckLibDevice = (!NoBuiltinLibs || Candidate.StrictChecking); 225 if (CheckLibDevice && !FS.exists(Path)) 226 return false; 227 228 scanLibDevicePath(Path); 229 230 if (!NoBuiltinLibs) { 231 // Check that the required non-target libraries are all available. 232 if (!allGenericLibsValid()) 233 return false; 234 235 // Check that we have found at least one libdevice that we can link in 236 // if -nobuiltinlib hasn't been specified. 237 if (LibDeviceMap.empty()) 238 return false; 239 } 240 return true; 241 }; 242 243 // The possible structures are: 244 // - ${ROCM_ROOT}/amdgcn/bitcode/* 245 // - ${ROCM_ROOT}/lib/* 246 // - ${ROCM_ROOT}/lib/bitcode/* 247 // so try to detect these layouts. 248 static constexpr std::array<const char *, 2> SubDirsList[] = { 249 {"amdgcn", "bitcode"}, 250 {"lib", ""}, 251 {"lib", "bitcode"}, 252 }; 253 254 // Make a path by appending sub-directories to InstallPath. 255 auto MakePath = [&](const llvm::ArrayRef<const char *> &SubDirs) { 256 auto Path = CandidatePath; 257 for (auto SubDir : SubDirs) 258 llvm::sys::path::append(Path, SubDir); 259 return Path; 260 }; 261 262 for (auto SubDirs : SubDirsList) { 263 LibDevicePath = MakePath(SubDirs); 264 HasDeviceLibrary = CheckDeviceLib(LibDevicePath); 265 if (HasDeviceLibrary) 266 return; 267 } 268 } 269 } 270 271 void RocmInstallationDetector::detectHIPRuntime() { 272 auto Candidates = getInstallationPathCandidates(); 273 auto &FS = D.getVFS(); 274 275 for (const auto &Candidate : Candidates) { 276 InstallPath = Candidate.Path; 277 if (InstallPath.empty() || !FS.exists(InstallPath)) 278 continue; 279 280 BinPath = InstallPath; 281 llvm::sys::path::append(BinPath, "bin"); 282 IncludePath = InstallPath; 283 llvm::sys::path::append(IncludePath, "include"); 284 LibPath = InstallPath; 285 llvm::sys::path::append(LibPath, "lib"); 286 287 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> VersionFile = 288 FS.getBufferForFile(BinPath + "/.hipVersion"); 289 if (!VersionFile && Candidate.StrictChecking) 290 continue; 291 292 if (HIPVersionArg.empty() && VersionFile) 293 ParseHIPVersionFile((*VersionFile)->getBuffer()); 294 295 HasHIPRuntime = true; 296 return; 297 } 298 HasHIPRuntime = false; 299 } 300 301 void RocmInstallationDetector::print(raw_ostream &OS) const { 302 if (hasHIPRuntime()) 303 OS << "Found HIP installation: " << InstallPath << ", version " 304 << DetectedVersion << '\n'; 305 } 306 307 void RocmInstallationDetector::AddHIPIncludeArgs(const ArgList &DriverArgs, 308 ArgStringList &CC1Args) const { 309 bool UsesRuntimeWrapper = VersionMajorMinor > llvm::VersionTuple(3, 5); 310 311 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { 312 // HIP header includes standard library wrapper headers under clang 313 // cuda_wrappers directory. Since these wrapper headers include_next 314 // standard C++ headers, whereas libc++ headers include_next other clang 315 // headers. The include paths have to follow this order: 316 // - wrapper include path 317 // - standard C++ include path 318 // - other clang include path 319 // Since standard C++ and other clang include paths are added in other 320 // places after this function, here we only need to make sure wrapper 321 // include path is added. 322 // 323 // ROCm 3.5 does not fully support the wrapper headers. Therefore it needs 324 // a workaround. 325 SmallString<128> P(D.ResourceDir); 326 if (UsesRuntimeWrapper) 327 llvm::sys::path::append(P, "include", "cuda_wrappers"); 328 CC1Args.push_back("-internal-isystem"); 329 CC1Args.push_back(DriverArgs.MakeArgString(P)); 330 } 331 332 if (DriverArgs.hasArg(options::OPT_nogpuinc)) 333 return; 334 335 if (!hasHIPRuntime()) { 336 D.Diag(diag::err_drv_no_hip_runtime); 337 return; 338 } 339 340 CC1Args.push_back("-internal-isystem"); 341 CC1Args.push_back(DriverArgs.MakeArgString(getIncludePath())); 342 if (UsesRuntimeWrapper) 343 CC1Args.append({"-include", "__clang_hip_runtime_wrapper.h"}); 344 } 345 346 void amdgpu::Linker::ConstructJob(Compilation &C, const JobAction &JA, 347 const InputInfo &Output, 348 const InputInfoList &Inputs, 349 const ArgList &Args, 350 const char *LinkingOutput) const { 351 352 std::string Linker = getToolChain().GetProgramPath(getShortName()); 353 ArgStringList CmdArgs; 354 addLinkerCompressDebugSectionsOption(getToolChain(), Args, CmdArgs); 355 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA); 356 CmdArgs.push_back("-shared"); 357 CmdArgs.push_back("-o"); 358 CmdArgs.push_back(Output.getFilename()); 359 C.addCommand(std::make_unique<Command>( 360 JA, *this, ResponseFileSupport::AtFileCurCP(), Args.MakeArgString(Linker), 361 CmdArgs, Inputs, Output)); 362 } 363 364 void amdgpu::getAMDGPUTargetFeatures(const Driver &D, 365 const llvm::Triple &Triple, 366 const llvm::opt::ArgList &Args, 367 std::vector<StringRef> &Features) { 368 // Add target ID features to -target-feature options. No diagnostics should 369 // be emitted here since invalid target ID is diagnosed at other places. 370 StringRef TargetID = Args.getLastArgValue(options::OPT_mcpu_EQ); 371 if (!TargetID.empty()) { 372 llvm::StringMap<bool> FeatureMap; 373 auto OptionalGpuArch = parseTargetID(Triple, TargetID, &FeatureMap); 374 if (OptionalGpuArch) { 375 StringRef GpuArch = OptionalGpuArch.getValue(); 376 // Iterate through all possible target ID features for the given GPU. 377 // If it is mapped to true, add +feature. 378 // If it is mapped to false, add -feature. 379 // If it is not in the map (default), do not add it 380 for (auto &&Feature : getAllPossibleTargetIDFeatures(Triple, GpuArch)) { 381 auto Pos = FeatureMap.find(Feature); 382 if (Pos == FeatureMap.end()) 383 continue; 384 Features.push_back(Args.MakeArgStringRef( 385 (Twine(Pos->second ? "+" : "-") + Feature).str())); 386 } 387 } 388 } 389 390 if (Args.hasFlag(options::OPT_mwavefrontsize64, 391 options::OPT_mno_wavefrontsize64, false)) 392 Features.push_back("+wavefrontsize64"); 393 394 handleTargetFeaturesGroup( 395 Args, Features, options::OPT_m_amdgpu_Features_Group); 396 } 397 398 /// AMDGPU Toolchain 399 AMDGPUToolChain::AMDGPUToolChain(const Driver &D, const llvm::Triple &Triple, 400 const ArgList &Args) 401 : Generic_ELF(D, Triple, Args), 402 OptionsDefault({{options::OPT_O, "3"}, 403 {options::OPT_cl_std_EQ, "CL1.2"}}) {} 404 405 Tool *AMDGPUToolChain::buildLinker() const { 406 return new tools::amdgpu::Linker(*this); 407 } 408 409 DerivedArgList * 410 AMDGPUToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch, 411 Action::OffloadKind DeviceOffloadKind) const { 412 413 DerivedArgList *DAL = 414 Generic_ELF::TranslateArgs(Args, BoundArch, DeviceOffloadKind); 415 416 const OptTable &Opts = getDriver().getOpts(); 417 418 if (!DAL) 419 DAL = new DerivedArgList(Args.getBaseArgs()); 420 421 for (Arg *A : Args) { 422 if (!shouldSkipArgument(A)) 423 DAL->append(A); 424 } 425 426 checkTargetID(*DAL); 427 428 if (!Args.getLastArgValue(options::OPT_x).equals("cl")) 429 return DAL; 430 431 // Phase 1 (.cl -> .bc) 432 if (Args.hasArg(options::OPT_c) && Args.hasArg(options::OPT_emit_llvm)) { 433 DAL->AddFlagArg(nullptr, Opts.getOption(getTriple().isArch64Bit() 434 ? options::OPT_m64 435 : options::OPT_m32)); 436 437 // Have to check OPT_O4, OPT_O0 & OPT_Ofast separately 438 // as they defined that way in Options.td 439 if (!Args.hasArg(options::OPT_O, options::OPT_O0, options::OPT_O4, 440 options::OPT_Ofast)) 441 DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_O), 442 getOptionDefault(options::OPT_O)); 443 } 444 445 return DAL; 446 } 447 448 bool AMDGPUToolChain::getDefaultDenormsAreZeroForTarget( 449 llvm::AMDGPU::GPUKind Kind) { 450 451 // Assume nothing without a specific target. 452 if (Kind == llvm::AMDGPU::GK_NONE) 453 return false; 454 455 const unsigned ArchAttr = llvm::AMDGPU::getArchAttrAMDGCN(Kind); 456 457 // Default to enabling f32 denormals by default on subtargets where fma is 458 // fast with denormals 459 const bool BothDenormAndFMAFast = 460 (ArchAttr & llvm::AMDGPU::FEATURE_FAST_FMA_F32) && 461 (ArchAttr & llvm::AMDGPU::FEATURE_FAST_DENORMAL_F32); 462 return !BothDenormAndFMAFast; 463 } 464 465 llvm::DenormalMode AMDGPUToolChain::getDefaultDenormalModeForType( 466 const llvm::opt::ArgList &DriverArgs, const JobAction &JA, 467 const llvm::fltSemantics *FPType) const { 468 // Denormals should always be enabled for f16 and f64. 469 if (!FPType || FPType != &llvm::APFloat::IEEEsingle()) 470 return llvm::DenormalMode::getIEEE(); 471 472 if (JA.getOffloadingDeviceKind() == Action::OFK_HIP || 473 JA.getOffloadingDeviceKind() == Action::OFK_Cuda) { 474 auto Arch = getProcessorFromTargetID(getTriple(), JA.getOffloadingArch()); 475 auto Kind = llvm::AMDGPU::parseArchAMDGCN(Arch); 476 if (FPType && FPType == &llvm::APFloat::IEEEsingle() && 477 DriverArgs.hasFlag(options::OPT_fcuda_flush_denormals_to_zero, 478 options::OPT_fno_cuda_flush_denormals_to_zero, 479 getDefaultDenormsAreZeroForTarget(Kind))) 480 return llvm::DenormalMode::getPreserveSign(); 481 482 return llvm::DenormalMode::getIEEE(); 483 } 484 485 const StringRef GpuArch = getGPUArch(DriverArgs); 486 auto Kind = llvm::AMDGPU::parseArchAMDGCN(GpuArch); 487 488 // TODO: There are way too many flags that change this. Do we need to check 489 // them all? 490 bool DAZ = DriverArgs.hasArg(options::OPT_cl_denorms_are_zero) || 491 getDefaultDenormsAreZeroForTarget(Kind); 492 493 // Outputs are flushed to zero (FTZ), preserving sign. Denormal inputs are 494 // also implicit treated as zero (DAZ). 495 return DAZ ? llvm::DenormalMode::getPreserveSign() : 496 llvm::DenormalMode::getIEEE(); 497 } 498 499 bool AMDGPUToolChain::isWave64(const llvm::opt::ArgList &DriverArgs, 500 llvm::AMDGPU::GPUKind Kind) { 501 const unsigned ArchAttr = llvm::AMDGPU::getArchAttrAMDGCN(Kind); 502 static bool HasWave32 = (ArchAttr & llvm::AMDGPU::FEATURE_WAVE32); 503 504 return !HasWave32 || DriverArgs.hasFlag( 505 options::OPT_mwavefrontsize64, options::OPT_mno_wavefrontsize64, false); 506 } 507 508 509 /// ROCM Toolchain 510 ROCMToolChain::ROCMToolChain(const Driver &D, const llvm::Triple &Triple, 511 const ArgList &Args) 512 : AMDGPUToolChain(D, Triple, Args) { 513 RocmInstallation.detectDeviceLibrary(); 514 } 515 516 void AMDGPUToolChain::addClangTargetOptions( 517 const llvm::opt::ArgList &DriverArgs, 518 llvm::opt::ArgStringList &CC1Args, 519 Action::OffloadKind DeviceOffloadingKind) const { 520 // Default to "hidden" visibility, as object level linking will not be 521 // supported for the foreseeable future. 522 if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ, 523 options::OPT_fvisibility_ms_compat)) { 524 CC1Args.push_back("-fvisibility"); 525 CC1Args.push_back("hidden"); 526 CC1Args.push_back("-fapply-global-visibility-to-externs"); 527 } 528 } 529 530 StringRef 531 AMDGPUToolChain::getGPUArch(const llvm::opt::ArgList &DriverArgs) const { 532 return getProcessorFromTargetID( 533 getTriple(), DriverArgs.getLastArgValue(options::OPT_mcpu_EQ)); 534 } 535 536 void AMDGPUToolChain::checkTargetID( 537 const llvm::opt::ArgList &DriverArgs) const { 538 StringRef TargetID = DriverArgs.getLastArgValue(options::OPT_mcpu_EQ); 539 if (TargetID.empty()) 540 return; 541 542 llvm::StringMap<bool> FeatureMap; 543 auto OptionalGpuArch = parseTargetID(getTriple(), TargetID, &FeatureMap); 544 if (!OptionalGpuArch) { 545 getDriver().Diag(clang::diag::err_drv_bad_target_id) << TargetID; 546 } 547 } 548 549 void ROCMToolChain::addClangTargetOptions( 550 const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, 551 Action::OffloadKind DeviceOffloadingKind) const { 552 AMDGPUToolChain::addClangTargetOptions(DriverArgs, CC1Args, 553 DeviceOffloadingKind); 554 555 // For the OpenCL case where there is no offload target, accept -nostdlib to 556 // disable bitcode linking. 557 if (DeviceOffloadingKind == Action::OFK_None && 558 DriverArgs.hasArg(options::OPT_nostdlib)) 559 return; 560 561 if (DriverArgs.hasArg(options::OPT_nogpulib)) 562 return; 563 564 if (!RocmInstallation.hasDeviceLibrary()) { 565 getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 0; 566 return; 567 } 568 569 // Get the device name and canonicalize it 570 const StringRef GpuArch = getGPUArch(DriverArgs); 571 auto Kind = llvm::AMDGPU::parseArchAMDGCN(GpuArch); 572 const StringRef CanonArch = llvm::AMDGPU::getArchNameAMDGCN(Kind); 573 std::string LibDeviceFile = RocmInstallation.getLibDeviceFile(CanonArch); 574 if (LibDeviceFile.empty()) { 575 getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 1 << GpuArch; 576 return; 577 } 578 579 bool Wave64 = isWave64(DriverArgs, Kind); 580 581 // TODO: There are way too many flags that change this. Do we need to check 582 // them all? 583 bool DAZ = DriverArgs.hasArg(options::OPT_cl_denorms_are_zero) || 584 getDefaultDenormsAreZeroForTarget(Kind); 585 bool FiniteOnly = DriverArgs.hasArg(options::OPT_cl_finite_math_only); 586 587 bool UnsafeMathOpt = 588 DriverArgs.hasArg(options::OPT_cl_unsafe_math_optimizations); 589 bool FastRelaxedMath = DriverArgs.hasArg(options::OPT_cl_fast_relaxed_math); 590 bool CorrectSqrt = 591 DriverArgs.hasArg(options::OPT_cl_fp32_correctly_rounded_divide_sqrt); 592 593 // Add the OpenCL specific bitcode library. 594 CC1Args.push_back("-mlink-builtin-bitcode"); 595 CC1Args.push_back(DriverArgs.MakeArgString(RocmInstallation.getOpenCLPath())); 596 597 // Add the generic set of libraries. 598 RocmInstallation.addCommonBitcodeLibCC1Args( 599 DriverArgs, CC1Args, LibDeviceFile, Wave64, DAZ, FiniteOnly, 600 UnsafeMathOpt, FastRelaxedMath, CorrectSqrt); 601 } 602 603 void RocmInstallationDetector::addCommonBitcodeLibCC1Args( 604 const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, 605 StringRef LibDeviceFile, bool Wave64, bool DAZ, bool FiniteOnly, 606 bool UnsafeMathOpt, bool FastRelaxedMath, bool CorrectSqrt) const { 607 static const char LinkBitcodeFlag[] = "-mlink-builtin-bitcode"; 608 609 CC1Args.push_back(LinkBitcodeFlag); 610 CC1Args.push_back(DriverArgs.MakeArgString(getOCMLPath())); 611 612 CC1Args.push_back(LinkBitcodeFlag); 613 CC1Args.push_back(DriverArgs.MakeArgString(getOCKLPath())); 614 615 CC1Args.push_back(LinkBitcodeFlag); 616 CC1Args.push_back(DriverArgs.MakeArgString(getDenormalsAreZeroPath(DAZ))); 617 618 CC1Args.push_back(LinkBitcodeFlag); 619 CC1Args.push_back(DriverArgs.MakeArgString( 620 getUnsafeMathPath(UnsafeMathOpt || FastRelaxedMath))); 621 622 CC1Args.push_back(LinkBitcodeFlag); 623 CC1Args.push_back(DriverArgs.MakeArgString( 624 getFiniteOnlyPath(FiniteOnly || FastRelaxedMath))); 625 626 CC1Args.push_back(LinkBitcodeFlag); 627 CC1Args.push_back( 628 DriverArgs.MakeArgString(getCorrectlyRoundedSqrtPath(CorrectSqrt))); 629 630 CC1Args.push_back(LinkBitcodeFlag); 631 CC1Args.push_back(DriverArgs.MakeArgString(getWavefrontSize64Path(Wave64))); 632 633 CC1Args.push_back(LinkBitcodeFlag); 634 CC1Args.push_back(DriverArgs.MakeArgString(LibDeviceFile)); 635 } 636 637 bool AMDGPUToolChain::shouldSkipArgument(const llvm::opt::Arg *A) const { 638 Option O = A->getOption(); 639 if (O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie)) 640 return true; 641 return false; 642 } 643