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 // Look for sub-directory starts with PackageName under ROCm candidate path. 26 // If there is one and only one matching sub-directory found, append the 27 // sub-directory to Path. If there is no matching sub-directory or there are 28 // more than one matching sub-directories, diagnose them. Returns the full 29 // path of the package if there is only one matching sub-directory, otherwise 30 // returns an empty string. 31 llvm::SmallString<0> 32 RocmInstallationDetector::findSPACKPackage(const Candidate &Cand, 33 StringRef PackageName) { 34 if (!Cand.isSPACK()) 35 return {}; 36 std::error_code EC; 37 std::string Prefix = Twine(PackageName + "-" + Cand.SPACKReleaseStr).str(); 38 llvm::SmallVector<llvm::SmallString<0>> SubDirs; 39 for (llvm::vfs::directory_iterator File = D.getVFS().dir_begin(Cand.Path, EC), 40 FileEnd; 41 File != FileEnd && !EC; File.increment(EC)) { 42 llvm::StringRef FileName = llvm::sys::path::filename(File->path()); 43 if (FileName.startswith(Prefix)) { 44 SubDirs.push_back(FileName); 45 if (SubDirs.size() > 1) 46 break; 47 } 48 } 49 if (SubDirs.size() == 1) { 50 auto PackagePath = Cand.Path; 51 llvm::sys::path::append(PackagePath, SubDirs[0]); 52 return PackagePath; 53 } 54 if (SubDirs.size() == 0) { 55 unsigned DiagID = D.getDiags().getCustomDiagID( 56 DiagnosticsEngine::Error, 57 "Expecting SPACK package %0 at %1 but not found"); 58 D.Diag(DiagID) << Prefix << Cand.Path; 59 return {}; 60 } 61 62 assert(SubDirs.size() > 1); 63 unsigned DiagID = D.getDiags().getCustomDiagID( 64 DiagnosticsEngine::Error, 65 "Expecting one SPACK package %0 at %1 but found more"); 66 D.Diag(DiagID) << Prefix << Cand.Path; 67 return {}; 68 } 69 70 void RocmInstallationDetector::scanLibDevicePath(llvm::StringRef Path) { 71 assert(!Path.empty()); 72 73 const StringRef Suffix(".bc"); 74 const StringRef Suffix2(".amdgcn.bc"); 75 76 std::error_code EC; 77 for (llvm::vfs::directory_iterator LI = D.getVFS().dir_begin(Path, EC), LE; 78 !EC && LI != LE; LI = LI.increment(EC)) { 79 StringRef FilePath = LI->path(); 80 StringRef FileName = llvm::sys::path::filename(FilePath); 81 if (!FileName.endswith(Suffix)) 82 continue; 83 84 StringRef BaseName; 85 if (FileName.endswith(Suffix2)) 86 BaseName = FileName.drop_back(Suffix2.size()); 87 else if (FileName.endswith(Suffix)) 88 BaseName = FileName.drop_back(Suffix.size()); 89 90 if (BaseName == "ocml") { 91 OCML = FilePath; 92 } else if (BaseName == "ockl") { 93 OCKL = FilePath; 94 } else if (BaseName == "opencl") { 95 OpenCL = FilePath; 96 } else if (BaseName == "hip") { 97 HIP = FilePath; 98 } else if (BaseName == "asanrtl") { 99 AsanRTL = FilePath; 100 } else if (BaseName == "oclc_finite_only_off") { 101 FiniteOnly.Off = FilePath; 102 } else if (BaseName == "oclc_finite_only_on") { 103 FiniteOnly.On = FilePath; 104 } else if (BaseName == "oclc_daz_opt_on") { 105 DenormalsAreZero.On = FilePath; 106 } else if (BaseName == "oclc_daz_opt_off") { 107 DenormalsAreZero.Off = FilePath; 108 } else if (BaseName == "oclc_correctly_rounded_sqrt_on") { 109 CorrectlyRoundedSqrt.On = FilePath; 110 } else if (BaseName == "oclc_correctly_rounded_sqrt_off") { 111 CorrectlyRoundedSqrt.Off = FilePath; 112 } else if (BaseName == "oclc_unsafe_math_on") { 113 UnsafeMath.On = FilePath; 114 } else if (BaseName == "oclc_unsafe_math_off") { 115 UnsafeMath.Off = FilePath; 116 } else if (BaseName == "oclc_wavefrontsize64_on") { 117 WavefrontSize64.On = FilePath; 118 } else if (BaseName == "oclc_wavefrontsize64_off") { 119 WavefrontSize64.Off = FilePath; 120 } else { 121 // Process all bitcode filenames that look like 122 // ocl_isa_version_XXX.amdgcn.bc 123 const StringRef DeviceLibPrefix = "oclc_isa_version_"; 124 if (!BaseName.startswith(DeviceLibPrefix)) 125 continue; 126 127 StringRef IsaVersionNumber = 128 BaseName.drop_front(DeviceLibPrefix.size()); 129 130 llvm::Twine GfxName = Twine("gfx") + IsaVersionNumber; 131 SmallString<8> Tmp; 132 LibDeviceMap.insert( 133 std::make_pair(GfxName.toStringRef(Tmp), FilePath.str())); 134 } 135 } 136 } 137 138 // Parse and extract version numbers from `.hipVersion`. Return `true` if 139 // the parsing fails. 140 bool RocmInstallationDetector::parseHIPVersionFile(llvm::StringRef V) { 141 SmallVector<StringRef, 4> VersionParts; 142 V.split(VersionParts, '\n'); 143 unsigned Major = ~0U; 144 unsigned Minor = ~0U; 145 for (auto Part : VersionParts) { 146 auto Splits = Part.rtrim().split('='); 147 if (Splits.first == "HIP_VERSION_MAJOR") { 148 if (Splits.second.getAsInteger(0, Major)) 149 return true; 150 } else if (Splits.first == "HIP_VERSION_MINOR") { 151 if (Splits.second.getAsInteger(0, Minor)) 152 return true; 153 } else if (Splits.first == "HIP_VERSION_PATCH") 154 VersionPatch = Splits.second.str(); 155 } 156 if (Major == ~0U || Minor == ~0U) 157 return true; 158 VersionMajorMinor = llvm::VersionTuple(Major, Minor); 159 DetectedVersion = 160 (Twine(Major) + "." + Twine(Minor) + "." + VersionPatch).str(); 161 return false; 162 } 163 164 /// \returns a list of candidate directories for ROCm installation, which is 165 /// cached and populated only once. 166 const SmallVectorImpl<RocmInstallationDetector::Candidate> & 167 RocmInstallationDetector::getInstallationPathCandidates() { 168 169 // Return the cached candidate list if it has already been populated. 170 if (!ROCmSearchDirs.empty()) 171 return ROCmSearchDirs; 172 173 auto DoPrintROCmSearchDirs = [&]() { 174 if (PrintROCmSearchDirs) 175 for (auto Cand : ROCmSearchDirs) { 176 llvm::errs() << "ROCm installation search path"; 177 if (Cand.isSPACK()) 178 llvm::errs() << " (Spack " << Cand.SPACKReleaseStr << ")"; 179 llvm::errs() << ": " << Cand.Path << '\n'; 180 } 181 }; 182 183 // For candidate specified by --rocm-path we do not do strict check, i.e., 184 // checking existence of HIP version file and device library files. 185 if (!RocmPathArg.empty()) { 186 ROCmSearchDirs.emplace_back(RocmPathArg.str()); 187 DoPrintROCmSearchDirs(); 188 return ROCmSearchDirs; 189 } else if (const char *RocmPathEnv = ::getenv("ROCM_PATH")) { 190 if (!StringRef(RocmPathEnv).empty()) { 191 ROCmSearchDirs.emplace_back(RocmPathEnv); 192 DoPrintROCmSearchDirs(); 193 return ROCmSearchDirs; 194 } 195 } 196 197 // Try to find relative to the compiler binary. 198 const char *InstallDir = D.getInstalledDir(); 199 200 // Check both a normal Unix prefix position of the clang binary, as well as 201 // the Windows-esque layout the ROCm packages use with the host architecture 202 // subdirectory of bin. 203 auto DeduceROCmPath = [](StringRef ClangPath) { 204 // Strip off directory (usually bin) 205 StringRef ParentDir = llvm::sys::path::parent_path(ClangPath); 206 StringRef ParentName = llvm::sys::path::filename(ParentDir); 207 208 // Some builds use bin/{host arch}, so go up again. 209 if (ParentName == "bin") { 210 ParentDir = llvm::sys::path::parent_path(ParentDir); 211 ParentName = llvm::sys::path::filename(ParentDir); 212 } 213 214 // Detect ROCm packages built with SPACK. 215 // clang is installed at 216 // <rocm_root>/llvm-amdgpu-<rocm_release_string>-<hash>/bin directory. 217 // We only consider the parent directory of llvm-amdgpu package as ROCm 218 // installation candidate for SPACK. 219 if (ParentName.startswith("llvm-amdgpu-")) { 220 auto SPACKPostfix = 221 ParentName.drop_front(strlen("llvm-amdgpu-")).split('-'); 222 auto SPACKReleaseStr = SPACKPostfix.first; 223 if (!SPACKReleaseStr.empty()) { 224 ParentDir = llvm::sys::path::parent_path(ParentDir); 225 return Candidate(ParentDir.str(), /*StrictChecking=*/true, 226 SPACKReleaseStr); 227 } 228 } 229 230 // Some versions of the rocm llvm package install to /opt/rocm/llvm/bin 231 // Some versions of the aomp package install to /opt/rocm/aomp/bin 232 if (ParentName == "llvm" || ParentName.startswith("aomp")) 233 ParentDir = llvm::sys::path::parent_path(ParentDir); 234 235 return Candidate(ParentDir.str(), /*StrictChecking=*/true); 236 }; 237 238 // Deduce ROCm path by the path used to invoke clang. Do not resolve symbolic 239 // link of clang itself. 240 ROCmSearchDirs.emplace_back(DeduceROCmPath(InstallDir)); 241 242 // Deduce ROCm path by the real path of the invoked clang, resolving symbolic 243 // link of clang itself. 244 llvm::SmallString<256> RealClangPath; 245 llvm::sys::fs::real_path(D.getClangProgramPath(), RealClangPath); 246 auto ParentPath = llvm::sys::path::parent_path(RealClangPath); 247 if (ParentPath != InstallDir) 248 ROCmSearchDirs.emplace_back(DeduceROCmPath(ParentPath)); 249 250 // Device library may be installed in clang resource directory. 251 ROCmSearchDirs.emplace_back(D.ResourceDir, 252 /*StrictChecking=*/true); 253 254 ROCmSearchDirs.emplace_back(D.SysRoot + "/opt/rocm", 255 /*StrictChecking=*/true); 256 257 // Find the latest /opt/rocm-{release} directory. 258 std::error_code EC; 259 std::string LatestROCm; 260 llvm::VersionTuple LatestVer; 261 // Get ROCm version from ROCm directory name. 262 auto GetROCmVersion = [](StringRef DirName) { 263 llvm::VersionTuple V; 264 std::string VerStr = DirName.drop_front(strlen("rocm-")).str(); 265 // The ROCm directory name follows the format of 266 // rocm-{major}.{minor}.{subMinor}[-{build}] 267 std::replace(VerStr.begin(), VerStr.end(), '-', '.'); 268 V.tryParse(VerStr); 269 return V; 270 }; 271 for (llvm::vfs::directory_iterator 272 File = D.getVFS().dir_begin(D.SysRoot + "/opt", EC), 273 FileEnd; 274 File != FileEnd && !EC; File.increment(EC)) { 275 llvm::StringRef FileName = llvm::sys::path::filename(File->path()); 276 if (!FileName.startswith("rocm-")) 277 continue; 278 if (LatestROCm.empty()) { 279 LatestROCm = FileName.str(); 280 LatestVer = GetROCmVersion(LatestROCm); 281 continue; 282 } 283 auto Ver = GetROCmVersion(FileName); 284 if (LatestVer < Ver) { 285 LatestROCm = FileName.str(); 286 LatestVer = Ver; 287 } 288 } 289 if (!LatestROCm.empty()) 290 ROCmSearchDirs.emplace_back(D.SysRoot + "/opt/" + LatestROCm, 291 /*StrictChecking=*/true); 292 293 DoPrintROCmSearchDirs(); 294 return ROCmSearchDirs; 295 } 296 297 RocmInstallationDetector::RocmInstallationDetector( 298 const Driver &D, const llvm::Triple &HostTriple, 299 const llvm::opt::ArgList &Args, bool DetectHIPRuntime, bool DetectDeviceLib) 300 : D(D) { 301 RocmPathArg = Args.getLastArgValue(clang::driver::options::OPT_rocm_path_EQ); 302 PrintROCmSearchDirs = 303 Args.hasArg(clang::driver::options::OPT_print_rocm_search_dirs); 304 RocmDeviceLibPathArg = 305 Args.getAllArgValues(clang::driver::options::OPT_rocm_device_lib_path_EQ); 306 HIPPathArg = Args.getLastArgValue(clang::driver::options::OPT_hip_path_EQ); 307 if (auto *A = Args.getLastArg(clang::driver::options::OPT_hip_version_EQ)) { 308 HIPVersionArg = A->getValue(); 309 unsigned Major = 0; 310 unsigned Minor = 0; 311 SmallVector<StringRef, 3> Parts; 312 HIPVersionArg.split(Parts, '.'); 313 if (Parts.size()) 314 Parts[0].getAsInteger(0, Major); 315 if (Parts.size() > 1) 316 Parts[1].getAsInteger(0, Minor); 317 if (Parts.size() > 2) 318 VersionPatch = Parts[2].str(); 319 if (VersionPatch.empty()) 320 VersionPatch = "0"; 321 if (Major == 0 || Minor == 0) 322 D.Diag(diag::err_drv_invalid_value) 323 << A->getAsString(Args) << HIPVersionArg; 324 325 VersionMajorMinor = llvm::VersionTuple(Major, Minor); 326 DetectedVersion = 327 (Twine(Major) + "." + Twine(Minor) + "." + VersionPatch).str(); 328 } else { 329 VersionPatch = DefaultVersionPatch; 330 VersionMajorMinor = 331 llvm::VersionTuple(DefaultVersionMajor, DefaultVersionMinor); 332 DetectedVersion = (Twine(DefaultVersionMajor) + "." + 333 Twine(DefaultVersionMinor) + "." + VersionPatch) 334 .str(); 335 } 336 337 if (DetectHIPRuntime) 338 detectHIPRuntime(); 339 if (DetectDeviceLib) 340 detectDeviceLibrary(); 341 } 342 343 void RocmInstallationDetector::detectDeviceLibrary() { 344 assert(LibDevicePath.empty()); 345 346 if (!RocmDeviceLibPathArg.empty()) 347 LibDevicePath = RocmDeviceLibPathArg[RocmDeviceLibPathArg.size() - 1]; 348 else if (const char *LibPathEnv = ::getenv("HIP_DEVICE_LIB_PATH")) 349 LibDevicePath = LibPathEnv; 350 351 auto &FS = D.getVFS(); 352 if (!LibDevicePath.empty()) { 353 // Maintain compatability with HIP flag/envvar pointing directly at the 354 // bitcode library directory. This points directly at the library path instead 355 // of the rocm root installation. 356 if (!FS.exists(LibDevicePath)) 357 return; 358 359 scanLibDevicePath(LibDevicePath); 360 HasDeviceLibrary = allGenericLibsValid() && !LibDeviceMap.empty(); 361 return; 362 } 363 364 // The install path situation in old versions of ROCm is a real mess, and 365 // use a different install layout. Multiple copies of the device libraries 366 // exist for each frontend project, and differ depending on which build 367 // system produced the packages. Standalone OpenCL builds also have a 368 // different directory structure from the ROCm OpenCL package. 369 auto &ROCmDirs = getInstallationPathCandidates(); 370 for (const auto &Candidate : ROCmDirs) { 371 auto CandidatePath = Candidate.Path; 372 373 // Check device library exists at the given path. 374 auto CheckDeviceLib = [&](StringRef Path) { 375 bool CheckLibDevice = (!NoBuiltinLibs || Candidate.StrictChecking); 376 if (CheckLibDevice && !FS.exists(Path)) 377 return false; 378 379 scanLibDevicePath(Path); 380 381 if (!NoBuiltinLibs) { 382 // Check that the required non-target libraries are all available. 383 if (!allGenericLibsValid()) 384 return false; 385 386 // Check that we have found at least one libdevice that we can link in 387 // if -nobuiltinlib hasn't been specified. 388 if (LibDeviceMap.empty()) 389 return false; 390 } 391 return true; 392 }; 393 394 // The possible structures are: 395 // - ${ROCM_ROOT}/amdgcn/bitcode/* 396 // - ${ROCM_ROOT}/lib/* 397 // - ${ROCM_ROOT}/lib/bitcode/* 398 // so try to detect these layouts. 399 static constexpr std::array<const char *, 2> SubDirsList[] = { 400 {"amdgcn", "bitcode"}, 401 {"lib", ""}, 402 {"lib", "bitcode"}, 403 }; 404 405 // Make a path by appending sub-directories to InstallPath. 406 auto MakePath = [&](const llvm::ArrayRef<const char *> &SubDirs) { 407 // Device library built by SPACK is installed to 408 // <rocm_root>/rocm-device-libs-<rocm_release_string>-<hash> directory. 409 auto SPACKPath = findSPACKPackage(Candidate, "rocm-device-libs"); 410 auto Path = SPACKPath.empty() ? CandidatePath : SPACKPath; 411 for (auto SubDir : SubDirs) 412 llvm::sys::path::append(Path, SubDir); 413 return Path; 414 }; 415 416 for (auto SubDirs : SubDirsList) { 417 LibDevicePath = MakePath(SubDirs); 418 HasDeviceLibrary = CheckDeviceLib(LibDevicePath); 419 if (HasDeviceLibrary) 420 return; 421 } 422 } 423 } 424 425 void RocmInstallationDetector::detectHIPRuntime() { 426 SmallVector<Candidate, 4> HIPSearchDirs; 427 if (!HIPPathArg.empty()) 428 HIPSearchDirs.emplace_back(HIPPathArg.str(), /*StrictChecking=*/true); 429 else 430 HIPSearchDirs.append(getInstallationPathCandidates()); 431 auto &FS = D.getVFS(); 432 433 for (const auto &Candidate : HIPSearchDirs) { 434 InstallPath = Candidate.Path; 435 if (InstallPath.empty() || !FS.exists(InstallPath)) 436 continue; 437 // HIP runtime built by SPACK is installed to 438 // <rocm_root>/hip-<rocm_release_string>-<hash> directory. 439 auto SPACKPath = findSPACKPackage(Candidate, "hip"); 440 InstallPath = SPACKPath.empty() ? InstallPath : SPACKPath; 441 442 BinPath = InstallPath; 443 llvm::sys::path::append(BinPath, "bin"); 444 IncludePath = InstallPath; 445 llvm::sys::path::append(IncludePath, "include"); 446 LibPath = InstallPath; 447 llvm::sys::path::append(LibPath, "lib"); 448 449 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> VersionFile = 450 FS.getBufferForFile(BinPath + "/.hipVersion"); 451 if (!VersionFile && Candidate.StrictChecking) 452 continue; 453 454 if (HIPVersionArg.empty() && VersionFile) 455 if (parseHIPVersionFile((*VersionFile)->getBuffer())) 456 continue; 457 458 HasHIPRuntime = true; 459 return; 460 } 461 HasHIPRuntime = false; 462 } 463 464 void RocmInstallationDetector::print(raw_ostream &OS) const { 465 if (hasHIPRuntime()) 466 OS << "Found HIP installation: " << InstallPath << ", version " 467 << DetectedVersion << '\n'; 468 } 469 470 void RocmInstallationDetector::AddHIPIncludeArgs(const ArgList &DriverArgs, 471 ArgStringList &CC1Args) const { 472 bool UsesRuntimeWrapper = VersionMajorMinor > llvm::VersionTuple(3, 5); 473 474 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { 475 // HIP header includes standard library wrapper headers under clang 476 // cuda_wrappers directory. Since these wrapper headers include_next 477 // standard C++ headers, whereas libc++ headers include_next other clang 478 // headers. The include paths have to follow this order: 479 // - wrapper include path 480 // - standard C++ include path 481 // - other clang include path 482 // Since standard C++ and other clang include paths are added in other 483 // places after this function, here we only need to make sure wrapper 484 // include path is added. 485 // 486 // ROCm 3.5 does not fully support the wrapper headers. Therefore it needs 487 // a workaround. 488 SmallString<128> P(D.ResourceDir); 489 if (UsesRuntimeWrapper) 490 llvm::sys::path::append(P, "include", "cuda_wrappers"); 491 CC1Args.push_back("-internal-isystem"); 492 CC1Args.push_back(DriverArgs.MakeArgString(P)); 493 } 494 495 if (DriverArgs.hasArg(options::OPT_nogpuinc)) 496 return; 497 498 if (!hasHIPRuntime()) { 499 D.Diag(diag::err_drv_no_hip_runtime); 500 return; 501 } 502 503 CC1Args.push_back("-internal-isystem"); 504 CC1Args.push_back(DriverArgs.MakeArgString(getIncludePath())); 505 if (UsesRuntimeWrapper) 506 CC1Args.append({"-include", "__clang_hip_runtime_wrapper.h"}); 507 } 508 509 void amdgpu::Linker::ConstructJob(Compilation &C, const JobAction &JA, 510 const InputInfo &Output, 511 const InputInfoList &Inputs, 512 const ArgList &Args, 513 const char *LinkingOutput) const { 514 515 std::string Linker = getToolChain().GetProgramPath(getShortName()); 516 ArgStringList CmdArgs; 517 addLinkerCompressDebugSectionsOption(getToolChain(), Args, CmdArgs); 518 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA); 519 CmdArgs.push_back("-shared"); 520 CmdArgs.push_back("-o"); 521 CmdArgs.push_back(Output.getFilename()); 522 C.addCommand(std::make_unique<Command>( 523 JA, *this, ResponseFileSupport::AtFileCurCP(), Args.MakeArgString(Linker), 524 CmdArgs, Inputs, Output)); 525 } 526 527 void amdgpu::getAMDGPUTargetFeatures(const Driver &D, 528 const llvm::Triple &Triple, 529 const llvm::opt::ArgList &Args, 530 std::vector<StringRef> &Features) { 531 // Add target ID features to -target-feature options. No diagnostics should 532 // be emitted here since invalid target ID is diagnosed at other places. 533 StringRef TargetID = Args.getLastArgValue(options::OPT_mcpu_EQ); 534 if (!TargetID.empty()) { 535 llvm::StringMap<bool> FeatureMap; 536 auto OptionalGpuArch = parseTargetID(Triple, TargetID, &FeatureMap); 537 if (OptionalGpuArch) { 538 StringRef GpuArch = OptionalGpuArch.getValue(); 539 // Iterate through all possible target ID features for the given GPU. 540 // If it is mapped to true, add +feature. 541 // If it is mapped to false, add -feature. 542 // If it is not in the map (default), do not add it 543 for (auto &&Feature : getAllPossibleTargetIDFeatures(Triple, GpuArch)) { 544 auto Pos = FeatureMap.find(Feature); 545 if (Pos == FeatureMap.end()) 546 continue; 547 Features.push_back(Args.MakeArgStringRef( 548 (Twine(Pos->second ? "+" : "-") + Feature).str())); 549 } 550 } 551 } 552 553 if (Args.hasFlag(options::OPT_mwavefrontsize64, 554 options::OPT_mno_wavefrontsize64, false)) 555 Features.push_back("+wavefrontsize64"); 556 557 handleTargetFeaturesGroup( 558 Args, Features, options::OPT_m_amdgpu_Features_Group); 559 } 560 561 /// AMDGPU Toolchain 562 AMDGPUToolChain::AMDGPUToolChain(const Driver &D, const llvm::Triple &Triple, 563 const ArgList &Args) 564 : Generic_ELF(D, Triple, Args), 565 OptionsDefault( 566 {{options::OPT_O, "3"}, {options::OPT_cl_std_EQ, "CL1.2"}}) { 567 // Check code object version options. Emit warnings for legacy options 568 // and errors for the last invalid code object version options. 569 // It is done here to avoid repeated warning or error messages for 570 // each tool invocation. 571 (void)getOrCheckAMDGPUCodeObjectVersion(D, Args, /*Diagnose=*/true); 572 } 573 574 Tool *AMDGPUToolChain::buildLinker() const { 575 return new tools::amdgpu::Linker(*this); 576 } 577 578 DerivedArgList * 579 AMDGPUToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch, 580 Action::OffloadKind DeviceOffloadKind) const { 581 582 DerivedArgList *DAL = 583 Generic_ELF::TranslateArgs(Args, BoundArch, DeviceOffloadKind); 584 585 const OptTable &Opts = getDriver().getOpts(); 586 587 if (!DAL) 588 DAL = new DerivedArgList(Args.getBaseArgs()); 589 590 for (Arg *A : Args) { 591 if (!shouldSkipArgument(A)) 592 DAL->append(A); 593 } 594 595 checkTargetID(*DAL); 596 597 if (!Args.getLastArgValue(options::OPT_x).equals("cl")) 598 return DAL; 599 600 // Phase 1 (.cl -> .bc) 601 if (Args.hasArg(options::OPT_c) && Args.hasArg(options::OPT_emit_llvm)) { 602 DAL->AddFlagArg(nullptr, Opts.getOption(getTriple().isArch64Bit() 603 ? options::OPT_m64 604 : options::OPT_m32)); 605 606 // Have to check OPT_O4, OPT_O0 & OPT_Ofast separately 607 // as they defined that way in Options.td 608 if (!Args.hasArg(options::OPT_O, options::OPT_O0, options::OPT_O4, 609 options::OPT_Ofast)) 610 DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_O), 611 getOptionDefault(options::OPT_O)); 612 } 613 614 return DAL; 615 } 616 617 bool AMDGPUToolChain::getDefaultDenormsAreZeroForTarget( 618 llvm::AMDGPU::GPUKind Kind) { 619 620 // Assume nothing without a specific target. 621 if (Kind == llvm::AMDGPU::GK_NONE) 622 return false; 623 624 const unsigned ArchAttr = llvm::AMDGPU::getArchAttrAMDGCN(Kind); 625 626 // Default to enabling f32 denormals by default on subtargets where fma is 627 // fast with denormals 628 const bool BothDenormAndFMAFast = 629 (ArchAttr & llvm::AMDGPU::FEATURE_FAST_FMA_F32) && 630 (ArchAttr & llvm::AMDGPU::FEATURE_FAST_DENORMAL_F32); 631 return !BothDenormAndFMAFast; 632 } 633 634 llvm::DenormalMode AMDGPUToolChain::getDefaultDenormalModeForType( 635 const llvm::opt::ArgList &DriverArgs, const JobAction &JA, 636 const llvm::fltSemantics *FPType) const { 637 // Denormals should always be enabled for f16 and f64. 638 if (!FPType || FPType != &llvm::APFloat::IEEEsingle()) 639 return llvm::DenormalMode::getIEEE(); 640 641 if (JA.getOffloadingDeviceKind() == Action::OFK_HIP || 642 JA.getOffloadingDeviceKind() == Action::OFK_Cuda) { 643 auto Arch = getProcessorFromTargetID(getTriple(), JA.getOffloadingArch()); 644 auto Kind = llvm::AMDGPU::parseArchAMDGCN(Arch); 645 if (FPType && FPType == &llvm::APFloat::IEEEsingle() && 646 DriverArgs.hasFlag(options::OPT_fcuda_flush_denormals_to_zero, 647 options::OPT_fno_cuda_flush_denormals_to_zero, 648 getDefaultDenormsAreZeroForTarget(Kind))) 649 return llvm::DenormalMode::getPreserveSign(); 650 651 return llvm::DenormalMode::getIEEE(); 652 } 653 654 const StringRef GpuArch = getGPUArch(DriverArgs); 655 auto Kind = llvm::AMDGPU::parseArchAMDGCN(GpuArch); 656 657 // TODO: There are way too many flags that change this. Do we need to check 658 // them all? 659 bool DAZ = DriverArgs.hasArg(options::OPT_cl_denorms_are_zero) || 660 getDefaultDenormsAreZeroForTarget(Kind); 661 662 // Outputs are flushed to zero (FTZ), preserving sign. Denormal inputs are 663 // also implicit treated as zero (DAZ). 664 return DAZ ? llvm::DenormalMode::getPreserveSign() : 665 llvm::DenormalMode::getIEEE(); 666 } 667 668 bool AMDGPUToolChain::isWave64(const llvm::opt::ArgList &DriverArgs, 669 llvm::AMDGPU::GPUKind Kind) { 670 const unsigned ArchAttr = llvm::AMDGPU::getArchAttrAMDGCN(Kind); 671 bool HasWave32 = (ArchAttr & llvm::AMDGPU::FEATURE_WAVE32); 672 673 return !HasWave32 || DriverArgs.hasFlag( 674 options::OPT_mwavefrontsize64, options::OPT_mno_wavefrontsize64, false); 675 } 676 677 678 /// ROCM Toolchain 679 ROCMToolChain::ROCMToolChain(const Driver &D, const llvm::Triple &Triple, 680 const ArgList &Args) 681 : AMDGPUToolChain(D, Triple, Args) { 682 RocmInstallation.detectDeviceLibrary(); 683 } 684 685 void AMDGPUToolChain::addClangTargetOptions( 686 const llvm::opt::ArgList &DriverArgs, 687 llvm::opt::ArgStringList &CC1Args, 688 Action::OffloadKind DeviceOffloadingKind) const { 689 // Default to "hidden" visibility, as object level linking will not be 690 // supported for the foreseeable future. 691 if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ, 692 options::OPT_fvisibility_ms_compat)) { 693 CC1Args.push_back("-fvisibility"); 694 CC1Args.push_back("hidden"); 695 CC1Args.push_back("-fapply-global-visibility-to-externs"); 696 } 697 } 698 699 StringRef 700 AMDGPUToolChain::getGPUArch(const llvm::opt::ArgList &DriverArgs) const { 701 return getProcessorFromTargetID( 702 getTriple(), DriverArgs.getLastArgValue(options::OPT_mcpu_EQ)); 703 } 704 705 void AMDGPUToolChain::checkTargetID( 706 const llvm::opt::ArgList &DriverArgs) const { 707 StringRef TargetID = DriverArgs.getLastArgValue(options::OPT_mcpu_EQ); 708 if (TargetID.empty()) 709 return; 710 711 llvm::StringMap<bool> FeatureMap; 712 auto OptionalGpuArch = parseTargetID(getTriple(), TargetID, &FeatureMap); 713 if (!OptionalGpuArch) { 714 getDriver().Diag(clang::diag::err_drv_bad_target_id) << TargetID; 715 } 716 } 717 718 void ROCMToolChain::addClangTargetOptions( 719 const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, 720 Action::OffloadKind DeviceOffloadingKind) const { 721 AMDGPUToolChain::addClangTargetOptions(DriverArgs, CC1Args, 722 DeviceOffloadingKind); 723 724 // For the OpenCL case where there is no offload target, accept -nostdlib to 725 // disable bitcode linking. 726 if (DeviceOffloadingKind == Action::OFK_None && 727 DriverArgs.hasArg(options::OPT_nostdlib)) 728 return; 729 730 if (DriverArgs.hasArg(options::OPT_nogpulib)) 731 return; 732 733 if (!RocmInstallation.hasDeviceLibrary()) { 734 getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 0; 735 return; 736 } 737 738 // Get the device name and canonicalize it 739 const StringRef GpuArch = getGPUArch(DriverArgs); 740 auto Kind = llvm::AMDGPU::parseArchAMDGCN(GpuArch); 741 const StringRef CanonArch = llvm::AMDGPU::getArchNameAMDGCN(Kind); 742 std::string LibDeviceFile = RocmInstallation.getLibDeviceFile(CanonArch); 743 if (LibDeviceFile.empty()) { 744 getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 1 << GpuArch; 745 return; 746 } 747 748 bool Wave64 = isWave64(DriverArgs, Kind); 749 750 // TODO: There are way too many flags that change this. Do we need to check 751 // them all? 752 bool DAZ = DriverArgs.hasArg(options::OPT_cl_denorms_are_zero) || 753 getDefaultDenormsAreZeroForTarget(Kind); 754 bool FiniteOnly = DriverArgs.hasArg(options::OPT_cl_finite_math_only); 755 756 bool UnsafeMathOpt = 757 DriverArgs.hasArg(options::OPT_cl_unsafe_math_optimizations); 758 bool FastRelaxedMath = DriverArgs.hasArg(options::OPT_cl_fast_relaxed_math); 759 bool CorrectSqrt = 760 DriverArgs.hasArg(options::OPT_cl_fp32_correctly_rounded_divide_sqrt); 761 762 // Add the OpenCL specific bitcode library. 763 llvm::SmallVector<std::string, 12> BCLibs; 764 BCLibs.push_back(RocmInstallation.getOpenCLPath().str()); 765 766 // Add the generic set of libraries. 767 BCLibs.append(RocmInstallation.getCommonBitcodeLibs( 768 DriverArgs, LibDeviceFile, Wave64, DAZ, FiniteOnly, UnsafeMathOpt, 769 FastRelaxedMath, CorrectSqrt)); 770 771 llvm::for_each(BCLibs, [&](StringRef BCFile) { 772 CC1Args.push_back("-mlink-builtin-bitcode"); 773 CC1Args.push_back(DriverArgs.MakeArgString(BCFile)); 774 }); 775 } 776 777 llvm::SmallVector<std::string, 12> 778 RocmInstallationDetector::getCommonBitcodeLibs( 779 const llvm::opt::ArgList &DriverArgs, StringRef LibDeviceFile, bool Wave64, 780 bool DAZ, bool FiniteOnly, bool UnsafeMathOpt, bool FastRelaxedMath, 781 bool CorrectSqrt) const { 782 783 llvm::SmallVector<std::string, 12> BCLibs; 784 785 auto AddBCLib = [&](StringRef BCFile) { BCLibs.push_back(BCFile.str()); }; 786 787 AddBCLib(getOCMLPath()); 788 AddBCLib(getOCKLPath()); 789 AddBCLib(getDenormalsAreZeroPath(DAZ)); 790 AddBCLib(getUnsafeMathPath(UnsafeMathOpt || FastRelaxedMath)); 791 AddBCLib(getFiniteOnlyPath(FiniteOnly || FastRelaxedMath)); 792 AddBCLib(getCorrectlyRoundedSqrtPath(CorrectSqrt)); 793 AddBCLib(getWavefrontSize64Path(Wave64)); 794 AddBCLib(LibDeviceFile); 795 796 return BCLibs; 797 } 798 799 bool AMDGPUToolChain::shouldSkipArgument(const llvm::opt::Arg *A) const { 800 Option O = A->getOption(); 801 if (O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie)) 802 return true; 803 return false; 804 } 805