| 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 "HIPAMD.h" |
| 11 | #include "clang/Basic/TargetID.h" |
| 12 | #include "clang/Config/config.h" |
| 13 | #include "clang/Driver/CommonArgs.h" |
| 14 | #include "clang/Driver/Compilation.h" |
| 15 | #include "clang/Driver/Driver.h" |
| 16 | #include "clang/Driver/InputInfo.h" |
| 17 | #include "clang/Driver/SanitizerArgs.h" |
| 18 | #include "clang/Options/Options.h" |
| 19 | #include "llvm/ADT/SmallSet.h" |
| 20 | #include "llvm/ADT/StringExtras.h" |
| 21 | #include "llvm/Option/ArgList.h" |
| 22 | #include "llvm/Support/Error.h" |
| 23 | #include "llvm/Support/LineIterator.h" |
| 24 | #include "llvm/Support/Path.h" |
| 25 | #include "llvm/Support/Process.h" |
| 26 | #include "llvm/Support/VirtualFileSystem.h" |
| 27 | #include "llvm/TargetParser/AMDGPUTargetParser.h" |
| 28 | #include "llvm/TargetParser/Host.h" |
| 29 | #include <optional> |
| 30 | #include <system_error> |
| 31 | |
| 32 | using namespace clang::driver; |
| 33 | using namespace clang::driver::tools; |
| 34 | using namespace clang::driver::toolchains; |
| 35 | using namespace clang; |
| 36 | using namespace llvm::opt; |
| 37 | |
| 38 | RocmInstallationDetector::CommonBitcodeLibsPreferences:: |
| 39 | CommonBitcodeLibsPreferences(const Driver &D, |
| 40 | const llvm::opt::ArgList &DriverArgs, |
| 41 | StringRef GPUArch, |
| 42 | const Action::OffloadKind DeviceOffloadingKind, |
| 43 | const bool NeedsASanRT) |
| 44 | : ABIVer(DeviceLibABIVersion::fromCodeObjectVersion( |
| 45 | CodeObjectVersion: tools::getAMDGPUCodeObjectVersion(D, Args: DriverArgs))) { |
| 46 | const auto Kind = llvm::AMDGPU::parseArchAMDGCN(CPU: GPUArch); |
| 47 | const unsigned ArchAttr = llvm::AMDGPU::getArchAttrAMDGCN(AK: Kind); |
| 48 | |
| 49 | IsOpenMP = DeviceOffloadingKind == Action::OFK_OpenMP; |
| 50 | |
| 51 | const bool HasWave32 = (ArchAttr & llvm::AMDGPU::FEATURE_WAVE32); |
| 52 | Wave64 = |
| 53 | !HasWave32 || DriverArgs.hasFlag(Pos: options::OPT_mwavefrontsize64, |
| 54 | Neg: options::OPT_mno_wavefrontsize64, Default: false); |
| 55 | |
| 56 | const bool IsKnownOffloading = DeviceOffloadingKind == Action::OFK_OpenMP || |
| 57 | DeviceOffloadingKind == Action::OFK_HIP; |
| 58 | |
| 59 | // Default to enabling f32 denormals on subtargets where fma is fast with |
| 60 | // denormals |
| 61 | const bool DefaultDAZ = |
| 62 | (Kind == llvm::AMDGPU::GK_NONE) |
| 63 | ? false |
| 64 | : !((ArchAttr & llvm::AMDGPU::FEATURE_FAST_FMA_F32) && |
| 65 | (ArchAttr & llvm::AMDGPU::FEATURE_FAST_DENORMAL_F32)); |
| 66 | // TODO: There are way too many flags that change this. Do we need to |
| 67 | // check them all? |
| 68 | DAZ = IsKnownOffloading |
| 69 | ? DriverArgs.hasFlag(Pos: options::OPT_fgpu_flush_denormals_to_zero, |
| 70 | Neg: options::OPT_fno_gpu_flush_denormals_to_zero, |
| 71 | Default: DefaultDAZ) |
| 72 | : DriverArgs.hasArg(Ids: options::OPT_cl_denorms_are_zero) || DefaultDAZ; |
| 73 | |
| 74 | FiniteOnly = DriverArgs.hasArg(Ids: options::OPT_cl_finite_math_only) || |
| 75 | DriverArgs.hasFlag(Pos: options::OPT_ffinite_math_only, |
| 76 | Neg: options::OPT_fno_finite_math_only, Default: false); |
| 77 | |
| 78 | UnsafeMathOpt = |
| 79 | DriverArgs.hasArg(Ids: options::OPT_cl_unsafe_math_optimizations) || |
| 80 | DriverArgs.hasFlag(Pos: options::OPT_funsafe_math_optimizations, |
| 81 | Neg: options::OPT_fno_unsafe_math_optimizations, Default: false); |
| 82 | |
| 83 | FastRelaxedMath = DriverArgs.hasArg(Ids: options::OPT_cl_fast_relaxed_math) || |
| 84 | DriverArgs.hasFlag(Pos: options::OPT_ffast_math, |
| 85 | Neg: options::OPT_fno_fast_math, Default: false); |
| 86 | |
| 87 | // GPU Sanitizer currently only supports ASan and is enabled through host |
| 88 | // ASan. |
| 89 | GPUSan = (DriverArgs.hasFlag(Pos: options::OPT_fgpu_sanitize, |
| 90 | Neg: options::OPT_fno_gpu_sanitize, Default: true) && |
| 91 | NeedsASanRT); |
| 92 | } |
| 93 | |
| 94 | void RocmInstallationDetector::scanLibDevicePath(llvm::StringRef Path) { |
| 95 | assert(!Path.empty()); |
| 96 | |
| 97 | const StringRef Suffix(".bc" ); |
| 98 | const StringRef Suffix2(".amdgcn.bc" ); |
| 99 | |
| 100 | std::error_code EC; |
| 101 | for (llvm::vfs::directory_iterator LI = D.getVFS().dir_begin(Dir: Path, EC), LE; |
| 102 | !EC && LI != LE; LI = LI.increment(EC)) { |
| 103 | StringRef FilePath = LI->path(); |
| 104 | StringRef FileName = llvm::sys::path::filename(path: FilePath); |
| 105 | if (!FileName.ends_with(Suffix)) |
| 106 | continue; |
| 107 | |
| 108 | StringRef BaseName; |
| 109 | if (FileName.ends_with(Suffix: Suffix2)) |
| 110 | BaseName = FileName.drop_back(N: Suffix2.size()); |
| 111 | else if (FileName.ends_with(Suffix)) |
| 112 | BaseName = FileName.drop_back(N: Suffix.size()); |
| 113 | |
| 114 | const StringRef ABIVersionPrefix = "oclc_abi_version_" ; |
| 115 | if (BaseName == "ocml" ) { |
| 116 | OCML = FilePath; |
| 117 | } else if (BaseName == "ockl" ) { |
| 118 | OCKL = FilePath; |
| 119 | } else if (BaseName == "opencl" ) { |
| 120 | OpenCL = FilePath; |
| 121 | } else if (BaseName == "asanrtl" ) { |
| 122 | AsanRTL = FilePath; |
| 123 | } else if (BaseName == "oclc_finite_only_off" ) { |
| 124 | FiniteOnly.Off = FilePath; |
| 125 | } else if (BaseName == "oclc_finite_only_on" ) { |
| 126 | FiniteOnly.On = FilePath; |
| 127 | } else if (BaseName == "oclc_unsafe_math_on" ) { |
| 128 | UnsafeMath.On = FilePath; |
| 129 | } else if (BaseName == "oclc_unsafe_math_off" ) { |
| 130 | UnsafeMath.Off = FilePath; |
| 131 | } else if (BaseName == "oclc_wavefrontsize64_on" ) { |
| 132 | WavefrontSize64.On = FilePath; |
| 133 | } else if (BaseName == "oclc_wavefrontsize64_off" ) { |
| 134 | WavefrontSize64.Off = FilePath; |
| 135 | } else if (BaseName.starts_with(Prefix: ABIVersionPrefix)) { |
| 136 | unsigned ABIVersionNumber; |
| 137 | if (BaseName.drop_front(N: ABIVersionPrefix.size()) |
| 138 | .getAsInteger(/*Redex=*/Radix: 0, Result&: ABIVersionNumber)) |
| 139 | continue; |
| 140 | ABIVersionMap[ABIVersionNumber] = FilePath.str(); |
| 141 | } else { |
| 142 | // Process all bitcode filenames that look like |
| 143 | // ocl_isa_version_XXX.amdgcn.bc |
| 144 | const StringRef DeviceLibPrefix = "oclc_isa_version_" ; |
| 145 | if (!BaseName.starts_with(Prefix: DeviceLibPrefix)) |
| 146 | continue; |
| 147 | |
| 148 | StringRef IsaVersionNumber = |
| 149 | BaseName.drop_front(N: DeviceLibPrefix.size()); |
| 150 | |
| 151 | llvm::Twine GfxName = Twine("gfx" ) + IsaVersionNumber; |
| 152 | SmallString<8> Tmp; |
| 153 | LibDeviceMap.insert(KV: {GfxName.toStringRef(Out&: Tmp), FilePath.str()}); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // Parse and extract version numbers from `.hipVersion`. Return `true` if |
| 159 | // the parsing fails. |
| 160 | bool RocmInstallationDetector::parseHIPVersionFile(llvm::StringRef V) { |
| 161 | SmallVector<StringRef, 4> VersionParts; |
| 162 | V.split(A&: VersionParts, Separator: '\n'); |
| 163 | unsigned Major = ~0U; |
| 164 | unsigned Minor = ~0U; |
| 165 | for (auto Part : VersionParts) { |
| 166 | auto Splits = Part.rtrim().split(Separator: '='); |
| 167 | if (Splits.first == "HIP_VERSION_MAJOR" ) { |
| 168 | if (Splits.second.getAsInteger(Radix: 0, Result&: Major)) |
| 169 | return true; |
| 170 | } else if (Splits.first == "HIP_VERSION_MINOR" ) { |
| 171 | if (Splits.second.getAsInteger(Radix: 0, Result&: Minor)) |
| 172 | return true; |
| 173 | } else if (Splits.first == "HIP_VERSION_PATCH" ) |
| 174 | VersionPatch = Splits.second.str(); |
| 175 | } |
| 176 | if (Major == ~0U || Minor == ~0U) |
| 177 | return true; |
| 178 | VersionMajorMinor = llvm::VersionTuple(Major, Minor); |
| 179 | DetectedVersion = |
| 180 | (Twine(Major) + "." + Twine(Minor) + "." + VersionPatch).str(); |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | /// \returns a list of candidate directories for ROCm installation, which is |
| 185 | /// cached and populated only once. |
| 186 | const SmallVectorImpl<RocmInstallationDetector::Candidate> & |
| 187 | RocmInstallationDetector::getInstallationPathCandidates() { |
| 188 | |
| 189 | // Return the cached candidate list if it has already been populated. |
| 190 | if (!ROCmSearchDirs.empty()) |
| 191 | return ROCmSearchDirs; |
| 192 | |
| 193 | auto DoPrintROCmSearchDirs = [&]() { |
| 194 | if (PrintROCmSearchDirs) |
| 195 | for (auto Cand : ROCmSearchDirs) { |
| 196 | llvm::errs() << "ROCm installation search path: " << Cand.Path << '\n'; |
| 197 | } |
| 198 | }; |
| 199 | |
| 200 | // For candidate specified by --rocm-path we do not do strict check, i.e., |
| 201 | // checking existence of HIP version file and device library files. |
| 202 | if (!RocmPathArg.empty()) { |
| 203 | ROCmSearchDirs.emplace_back(Args: RocmPathArg.str()); |
| 204 | DoPrintROCmSearchDirs(); |
| 205 | return ROCmSearchDirs; |
| 206 | } else if (std::optional<std::string> RocmPathEnv = |
| 207 | llvm::sys::Process::GetEnv(name: "ROCM_PATH" )) { |
| 208 | if (!RocmPathEnv->empty()) { |
| 209 | ROCmSearchDirs.emplace_back(Args: std::move(*RocmPathEnv)); |
| 210 | DoPrintROCmSearchDirs(); |
| 211 | return ROCmSearchDirs; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | // Try to find relative to the compiler binary. |
| 216 | StringRef InstallDir = D.Dir; |
| 217 | |
| 218 | // Check both a normal Unix prefix position of the clang binary, as well as |
| 219 | // the Windows-esque layout the ROCm packages use with the host architecture |
| 220 | // subdirectory of bin. |
| 221 | auto DeduceROCmPath = [](StringRef ClangPath) { |
| 222 | // Strip off directory (usually bin) |
| 223 | StringRef ParentDir = llvm::sys::path::parent_path(path: ClangPath); |
| 224 | StringRef ParentName = llvm::sys::path::filename(path: ParentDir); |
| 225 | |
| 226 | // Some builds use bin/{host arch}, so go up again. |
| 227 | if (ParentName == "bin" ) { |
| 228 | ParentDir = llvm::sys::path::parent_path(path: ParentDir); |
| 229 | ParentName = llvm::sys::path::filename(path: ParentDir); |
| 230 | } |
| 231 | |
| 232 | // Some versions of the rocm llvm package install to /opt/rocm/llvm/bin |
| 233 | // Some versions of the aomp package install to /opt/rocm/aomp/bin |
| 234 | if (ParentName == "llvm" || ParentName.starts_with(Prefix: "aomp" )) { |
| 235 | ParentDir = llvm::sys::path::parent_path(path: ParentDir); |
| 236 | ParentName = llvm::sys::path::filename(path: ParentDir); |
| 237 | |
| 238 | // Some versions of the rocm llvm package install to |
| 239 | // /opt/rocm/lib/llvm/bin, so also back up if within the lib dir still |
| 240 | if (ParentName == "lib" ) |
| 241 | ParentDir = llvm::sys::path::parent_path(path: ParentDir); |
| 242 | } |
| 243 | |
| 244 | return Candidate(ParentDir.str(), /*StrictChecking=*/true); |
| 245 | }; |
| 246 | |
| 247 | // Deduce ROCm path by the path used to invoke clang. Do not resolve symbolic |
| 248 | // link of clang itself. |
| 249 | ROCmSearchDirs.emplace_back(Args: DeduceROCmPath(InstallDir)); |
| 250 | |
| 251 | // Deduce ROCm path by the real path of the invoked clang, resolving symbolic |
| 252 | // link of clang itself. |
| 253 | llvm::SmallString<256> RealClangPath; |
| 254 | llvm::sys::fs::real_path(path: D.getDriverProgramPath(), output&: RealClangPath); |
| 255 | auto ParentPath = llvm::sys::path::parent_path(path: RealClangPath); |
| 256 | if (ParentPath != InstallDir) |
| 257 | ROCmSearchDirs.emplace_back(Args: DeduceROCmPath(ParentPath)); |
| 258 | |
| 259 | // Device library may be installed in clang or resource directory. |
| 260 | auto ClangRoot = llvm::sys::path::parent_path(path: InstallDir); |
| 261 | auto RealClangRoot = llvm::sys::path::parent_path(path: ParentPath); |
| 262 | ROCmSearchDirs.emplace_back(Args: ClangRoot.str(), /*StrictChecking=*/Args: true); |
| 263 | if (RealClangRoot != ClangRoot) |
| 264 | ROCmSearchDirs.emplace_back(Args: RealClangRoot.str(), /*StrictChecking=*/Args: true); |
| 265 | ROCmSearchDirs.emplace_back(Args: D.ResourceDir, |
| 266 | /*StrictChecking=*/Args: true); |
| 267 | |
| 268 | ROCmSearchDirs.emplace_back(Args: D.SysRoot + "/opt/rocm" , |
| 269 | /*StrictChecking=*/Args: true); |
| 270 | |
| 271 | // Find the latest /opt/rocm-{release} directory. |
| 272 | std::error_code EC; |
| 273 | std::string LatestROCm; |
| 274 | llvm::VersionTuple LatestVer; |
| 275 | // Get ROCm version from ROCm directory name. |
| 276 | auto GetROCmVersion = [](StringRef DirName) { |
| 277 | llvm::VersionTuple V; |
| 278 | std::string VerStr = DirName.drop_front(N: strlen(s: "rocm-" )).str(); |
| 279 | // The ROCm directory name follows the format of |
| 280 | // rocm-{major}.{minor}.{subMinor}[-{build}] |
| 281 | llvm::replace(Range&: VerStr, OldValue: '-', NewValue: '.'); |
| 282 | V.tryParse(string: VerStr); |
| 283 | return V; |
| 284 | }; |
| 285 | for (llvm::vfs::directory_iterator |
| 286 | File = D.getVFS().dir_begin(Dir: D.SysRoot + "/opt" , EC), |
| 287 | FileEnd; |
| 288 | File != FileEnd && !EC; File.increment(EC)) { |
| 289 | llvm::StringRef FileName = llvm::sys::path::filename(path: File->path()); |
| 290 | if (!FileName.starts_with(Prefix: "rocm-" )) |
| 291 | continue; |
| 292 | if (LatestROCm.empty()) { |
| 293 | LatestROCm = FileName.str(); |
| 294 | LatestVer = GetROCmVersion(LatestROCm); |
| 295 | continue; |
| 296 | } |
| 297 | auto Ver = GetROCmVersion(FileName); |
| 298 | if (LatestVer < Ver) { |
| 299 | LatestROCm = FileName.str(); |
| 300 | LatestVer = Ver; |
| 301 | } |
| 302 | } |
| 303 | if (!LatestROCm.empty()) |
| 304 | ROCmSearchDirs.emplace_back(Args: D.SysRoot + "/opt/" + LatestROCm, |
| 305 | /*StrictChecking=*/Args: true); |
| 306 | |
| 307 | ROCmSearchDirs.emplace_back(Args: D.SysRoot + "/usr/local" , |
| 308 | /*StrictChecking=*/Args: true); |
| 309 | ROCmSearchDirs.emplace_back(Args: D.SysRoot + "/usr" , |
| 310 | /*StrictChecking=*/Args: true); |
| 311 | |
| 312 | DoPrintROCmSearchDirs(); |
| 313 | return ROCmSearchDirs; |
| 314 | } |
| 315 | |
| 316 | RocmInstallationDetector::RocmInstallationDetector( |
| 317 | const Driver &D, const llvm::Triple &HostTriple, |
| 318 | const llvm::opt::ArgList &Args, bool DetectHIPRuntime) |
| 319 | : D(D) { |
| 320 | Verbose = Args.hasArg(Ids: options::OPT_v); |
| 321 | RocmPathArg = Args.getLastArgValue(Id: options::OPT_rocm_path_EQ); |
| 322 | PrintROCmSearchDirs = Args.hasArg(Ids: options::OPT_print_rocm_search_dirs); |
| 323 | RocmDeviceLibPathArg = |
| 324 | Args.getAllArgValues(Id: options::OPT_rocm_device_lib_path_EQ); |
| 325 | HIPPathArg = Args.getLastArgValue(Id: options::OPT_hip_path_EQ); |
| 326 | HIPStdParPathArg = Args.getLastArgValue(Id: options::OPT_hipstdpar_path_EQ); |
| 327 | HasHIPStdParLibrary = |
| 328 | !HIPStdParPathArg.empty() && D.getVFS().exists(Path: HIPStdParPathArg + |
| 329 | "/hipstdpar_lib.hpp" ); |
| 330 | HIPRocThrustPathArg = |
| 331 | Args.getLastArgValue(Id: options::OPT_hipstdpar_thrust_path_EQ); |
| 332 | HasRocThrustLibrary = !HIPRocThrustPathArg.empty() && |
| 333 | D.getVFS().exists(Path: HIPRocThrustPathArg + "/thrust" ); |
| 334 | HIPRocPrimPathArg = Args.getLastArgValue(Id: options::OPT_hipstdpar_prim_path_EQ); |
| 335 | HasRocPrimLibrary = !HIPRocPrimPathArg.empty() && |
| 336 | D.getVFS().exists(Path: HIPRocPrimPathArg + "/rocprim" ); |
| 337 | |
| 338 | if (auto *A = Args.getLastArg(Ids: options::OPT_hip_version_EQ)) { |
| 339 | HIPVersionArg = A->getValue(); |
| 340 | unsigned Major = ~0U; |
| 341 | unsigned Minor = ~0U; |
| 342 | SmallVector<StringRef, 3> Parts; |
| 343 | HIPVersionArg.split(A&: Parts, Separator: '.'); |
| 344 | if (!Parts.empty()) |
| 345 | Parts[0].getAsInteger(Radix: 0, Result&: Major); |
| 346 | if (Parts.size() > 1) |
| 347 | Parts[1].getAsInteger(Radix: 0, Result&: Minor); |
| 348 | if (Parts.size() > 2) |
| 349 | VersionPatch = Parts[2].str(); |
| 350 | if (VersionPatch.empty()) |
| 351 | VersionPatch = "0" ; |
| 352 | if (Major != ~0U && Minor == ~0U) |
| 353 | Minor = 0; |
| 354 | if (Major == ~0U || Minor == ~0U) |
| 355 | D.Diag(DiagID: diag::err_drv_invalid_value) |
| 356 | << A->getAsString(Args) << HIPVersionArg; |
| 357 | |
| 358 | VersionMajorMinor = llvm::VersionTuple(Major, Minor); |
| 359 | DetectedVersion = |
| 360 | (Twine(Major) + "." + Twine(Minor) + "." + VersionPatch).str(); |
| 361 | } else { |
| 362 | VersionPatch = DefaultVersionPatch; |
| 363 | VersionMajorMinor = |
| 364 | llvm::VersionTuple(DefaultVersionMajor, DefaultVersionMinor); |
| 365 | DetectedVersion = (Twine(DefaultVersionMajor) + "." + |
| 366 | Twine(DefaultVersionMinor) + "." + VersionPatch) |
| 367 | .str(); |
| 368 | } |
| 369 | |
| 370 | if (DetectHIPRuntime) |
| 371 | detectHIPRuntime(); |
| 372 | } |
| 373 | |
| 374 | void RocmInstallationDetector::detectDeviceLibrary() { |
| 375 | assert(LibDevicePath.empty()); |
| 376 | |
| 377 | if (!RocmDeviceLibPathArg.empty()) |
| 378 | LibDevicePath = RocmDeviceLibPathArg.back(); |
| 379 | else if (std::optional<std::string> LibPathEnv = |
| 380 | llvm::sys::Process::GetEnv(name: "HIP_DEVICE_LIB_PATH" )) |
| 381 | LibDevicePath = std::move(*LibPathEnv); |
| 382 | |
| 383 | auto &FS = D.getVFS(); |
| 384 | if (!LibDevicePath.empty()) { |
| 385 | // Maintain compatability with HIP flag/envvar pointing directly at the |
| 386 | // bitcode library directory. This points directly at the library path instead |
| 387 | // of the rocm root installation. |
| 388 | if (!FS.exists(Path: LibDevicePath)) |
| 389 | return; |
| 390 | |
| 391 | scanLibDevicePath(Path: LibDevicePath); |
| 392 | HasDeviceLibrary = allGenericLibsValid() && !LibDeviceMap.empty(); |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | // Check device library exists at the given path. |
| 397 | auto CheckDeviceLib = [&](StringRef Path, bool StrictChecking) { |
| 398 | bool CheckLibDevice = (!NoBuiltinLibs || StrictChecking); |
| 399 | if (CheckLibDevice && !FS.exists(Path)) |
| 400 | return false; |
| 401 | |
| 402 | scanLibDevicePath(Path); |
| 403 | |
| 404 | if (!NoBuiltinLibs) { |
| 405 | // Check that the required non-target libraries are all available. |
| 406 | if (!allGenericLibsValid()) |
| 407 | return false; |
| 408 | |
| 409 | // Check that we have found at least one libdevice that we can link in |
| 410 | // if -nobuiltinlib hasn't been specified. |
| 411 | if (LibDeviceMap.empty()) |
| 412 | return false; |
| 413 | } |
| 414 | return true; |
| 415 | }; |
| 416 | |
| 417 | // Find device libraries in <LLVM_DIR>/lib/clang/<ver>/lib/amdgcn/bitcode |
| 418 | LibDevicePath = D.ResourceDir; |
| 419 | llvm::sys::path::append(path&: LibDevicePath, CLANG_INSTALL_LIBDIR_BASENAME, |
| 420 | b: "amdgcn" , c: "bitcode" ); |
| 421 | HasDeviceLibrary = CheckDeviceLib(LibDevicePath, true); |
| 422 | if (HasDeviceLibrary) |
| 423 | return; |
| 424 | |
| 425 | // Find device libraries in a legacy ROCm directory structure |
| 426 | // ${ROCM_ROOT}/amdgcn/bitcode/* |
| 427 | auto &ROCmDirs = getInstallationPathCandidates(); |
| 428 | for (const auto &Candidate : ROCmDirs) { |
| 429 | LibDevicePath = Candidate.Path; |
| 430 | llvm::sys::path::append(path&: LibDevicePath, a: "amdgcn" , b: "bitcode" ); |
| 431 | HasDeviceLibrary = CheckDeviceLib(LibDevicePath, Candidate.StrictChecking); |
| 432 | if (HasDeviceLibrary) |
| 433 | return; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | void RocmInstallationDetector::detectHIPRuntime() { |
| 438 | SmallVector<Candidate, 4> HIPSearchDirs; |
| 439 | if (!HIPPathArg.empty()) |
| 440 | HIPSearchDirs.emplace_back(Args: HIPPathArg.str()); |
| 441 | else if (std::optional<std::string> HIPPathEnv = |
| 442 | llvm::sys::Process::GetEnv(name: "HIP_PATH" )) { |
| 443 | if (!HIPPathEnv->empty()) |
| 444 | HIPSearchDirs.emplace_back(Args: std::move(*HIPPathEnv)); |
| 445 | } |
| 446 | if (HIPSearchDirs.empty()) |
| 447 | HIPSearchDirs.append(RHS: getInstallationPathCandidates()); |
| 448 | auto &FS = D.getVFS(); |
| 449 | |
| 450 | for (const auto &Candidate : HIPSearchDirs) { |
| 451 | InstallPath = Candidate.Path; |
| 452 | if (InstallPath.empty() || !FS.exists(Path: InstallPath)) |
| 453 | continue; |
| 454 | |
| 455 | BinPath = InstallPath; |
| 456 | llvm::sys::path::append(path&: BinPath, a: "bin" ); |
| 457 | IncludePath = InstallPath; |
| 458 | llvm::sys::path::append(path&: IncludePath, a: "include" ); |
| 459 | LibPath = InstallPath; |
| 460 | llvm::sys::path::append(path&: LibPath, a: "lib" ); |
| 461 | SharePath = InstallPath; |
| 462 | llvm::sys::path::append(path&: SharePath, a: "share" ); |
| 463 | |
| 464 | // Get parent of InstallPath and append "share" |
| 465 | SmallString<0> ParentSharePath = llvm::sys::path::parent_path(path: InstallPath); |
| 466 | llvm::sys::path::append(path&: ParentSharePath, a: "share" ); |
| 467 | |
| 468 | auto Append = [](SmallString<0> &path, const Twine &a, const Twine &b = "" , |
| 469 | const Twine &c = "" , const Twine &d = "" ) { |
| 470 | SmallString<0> newpath = path; |
| 471 | llvm::sys::path::append(path&: newpath, a, b, c, d); |
| 472 | return newpath; |
| 473 | }; |
| 474 | // If HIP version file can be found and parsed, use HIP version from there. |
| 475 | std::vector<SmallString<0>> VersionFilePaths = { |
| 476 | Append(SharePath, "hip" , "version" ), |
| 477 | InstallPath != D.SysRoot + "/usr/local" |
| 478 | ? Append(ParentSharePath, "hip" , "version" ) |
| 479 | : SmallString<0>(), |
| 480 | Append(BinPath, ".hipVersion" )}; |
| 481 | |
| 482 | for (const auto &VersionFilePath : VersionFilePaths) { |
| 483 | if (VersionFilePath.empty()) |
| 484 | continue; |
| 485 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> VersionFile = |
| 486 | FS.getBufferForFile(Name: VersionFilePath); |
| 487 | if (!VersionFile) |
| 488 | continue; |
| 489 | if (HIPVersionArg.empty() && VersionFile) |
| 490 | if (parseHIPVersionFile(V: (*VersionFile)->getBuffer())) |
| 491 | continue; |
| 492 | |
| 493 | HasHIPRuntime = true; |
| 494 | return; |
| 495 | } |
| 496 | // Otherwise, if -rocm-path is specified (no strict checking), use the |
| 497 | // default HIP version or specified by --hip-version. |
| 498 | if (!Candidate.StrictChecking) { |
| 499 | HasHIPRuntime = true; |
| 500 | return; |
| 501 | } |
| 502 | } |
| 503 | HasHIPRuntime = false; |
| 504 | } |
| 505 | |
| 506 | void RocmInstallationDetector::print(raw_ostream &OS) const { |
| 507 | if (hasHIPRuntime()) |
| 508 | OS << "Found HIP installation: " << InstallPath << ", version " |
| 509 | << DetectedVersion << '\n'; |
| 510 | } |
| 511 | |
| 512 | void RocmInstallationDetector::AddHIPIncludeArgs(const ArgList &DriverArgs, |
| 513 | ArgStringList &CC1Args) const { |
| 514 | bool UsesRuntimeWrapper = VersionMajorMinor > llvm::VersionTuple(3, 5) && |
| 515 | !DriverArgs.hasArg(Ids: options::OPT_nohipwrapperinc); |
| 516 | bool HasHipStdPar = DriverArgs.hasArg(Ids: options::OPT_hipstdpar); |
| 517 | |
| 518 | if (!DriverArgs.hasArg(Ids: options::OPT_nobuiltininc)) { |
| 519 | // HIP header includes standard library wrapper headers under clang |
| 520 | // cuda_wrappers directory. Since these wrapper headers include_next |
| 521 | // standard C++ headers, whereas libc++ headers include_next other clang |
| 522 | // headers. The include paths have to follow this order: |
| 523 | // - wrapper include path |
| 524 | // - standard C++ include path |
| 525 | // - other clang include path |
| 526 | // Since standard C++ and other clang include paths are added in other |
| 527 | // places after this function, here we only need to make sure wrapper |
| 528 | // include path is added. |
| 529 | // |
| 530 | // ROCm 3.5 does not fully support the wrapper headers. Therefore it needs |
| 531 | // a workaround. |
| 532 | SmallString<128> P(D.ResourceDir); |
| 533 | if (UsesRuntimeWrapper) |
| 534 | llvm::sys::path::append(path&: P, a: "include" , b: "cuda_wrappers" ); |
| 535 | CC1Args.push_back(Elt: "-internal-isystem" ); |
| 536 | CC1Args.push_back(Elt: DriverArgs.MakeArgString(Str: P)); |
| 537 | } |
| 538 | |
| 539 | const auto HandleHipStdPar = [=, &DriverArgs, &CC1Args]() { |
| 540 | StringRef Inc = getIncludePath(); |
| 541 | auto &FS = D.getVFS(); |
| 542 | |
| 543 | if (!hasHIPStdParLibrary()) |
| 544 | if (!HIPStdParPathArg.empty() || |
| 545 | !FS.exists(Path: Inc + "/thrust/system/hip/hipstdpar/hipstdpar_lib.hpp" )) { |
| 546 | D.Diag(DiagID: diag::err_drv_no_hipstdpar_lib); |
| 547 | return; |
| 548 | } |
| 549 | if (!HasRocThrustLibrary && !FS.exists(Path: Inc + "/thrust" )) { |
| 550 | D.Diag(DiagID: diag::err_drv_no_hipstdpar_thrust_lib); |
| 551 | return; |
| 552 | } |
| 553 | if (!HasRocPrimLibrary && !FS.exists(Path: Inc + "/rocprim" )) { |
| 554 | D.Diag(DiagID: diag::err_drv_no_hipstdpar_prim_lib); |
| 555 | return; |
| 556 | } |
| 557 | const char *ThrustPath; |
| 558 | if (HasRocThrustLibrary) |
| 559 | ThrustPath = DriverArgs.MakeArgString(Str: HIPRocThrustPathArg); |
| 560 | else |
| 561 | ThrustPath = DriverArgs.MakeArgString(Str: Inc + "/thrust" ); |
| 562 | |
| 563 | const char *HIPStdParPath; |
| 564 | if (hasHIPStdParLibrary()) |
| 565 | HIPStdParPath = DriverArgs.MakeArgString(Str: HIPStdParPathArg); |
| 566 | else |
| 567 | HIPStdParPath = DriverArgs.MakeArgString(Str: StringRef(ThrustPath) + |
| 568 | "/system/hip/hipstdpar" ); |
| 569 | |
| 570 | const char *PrimPath; |
| 571 | if (HasRocPrimLibrary) |
| 572 | PrimPath = DriverArgs.MakeArgString(Str: HIPRocPrimPathArg); |
| 573 | else |
| 574 | PrimPath = DriverArgs.MakeArgString(Str: getIncludePath() + "/rocprim" ); |
| 575 | |
| 576 | CC1Args.append(IL: {"-idirafter" , ThrustPath, "-idirafter" , PrimPath, |
| 577 | "-idirafter" , HIPStdParPath, "-include" , |
| 578 | "hipstdpar_lib.hpp" }); |
| 579 | }; |
| 580 | |
| 581 | if (!DriverArgs.hasFlag(Pos: options::OPT_offload_inc, Neg: options::OPT_no_offload_inc, |
| 582 | Default: true)) { |
| 583 | if (HasHipStdPar) |
| 584 | HandleHipStdPar(); |
| 585 | |
| 586 | return; |
| 587 | } |
| 588 | |
| 589 | if (!hasHIPRuntime()) { |
| 590 | D.Diag(DiagID: diag::err_drv_no_hip_runtime); |
| 591 | return; |
| 592 | } |
| 593 | |
| 594 | CC1Args.push_back(Elt: "-idirafter" ); |
| 595 | CC1Args.push_back(Elt: DriverArgs.MakeArgString(Str: getIncludePath())); |
| 596 | if (UsesRuntimeWrapper) |
| 597 | CC1Args.append(IL: {"-include" , "__clang_hip_runtime_wrapper.h" }); |
| 598 | if (HasHipStdPar) |
| 599 | HandleHipStdPar(); |
| 600 | } |
| 601 | |
| 602 | void amdgpu::Linker::ConstructJob(Compilation &C, const JobAction &JA, |
| 603 | const InputInfo &Output, |
| 604 | const InputInfoList &Inputs, |
| 605 | const ArgList &Args, |
| 606 | const char *LinkingOutput) const { |
| 607 | std::string Linker = getToolChain().GetLinkerPath(); |
| 608 | ArgStringList CmdArgs; |
| 609 | if (!Args.hasArg(Ids: options::OPT_r)) { |
| 610 | CmdArgs.push_back(Elt: "--no-undefined" ); |
| 611 | CmdArgs.push_back(Elt: "-shared" ); |
| 612 | } |
| 613 | |
| 614 | if (Args.hasArg(Ids: options::OPT_hipstdpar)) |
| 615 | CmdArgs.push_back(Elt: "-plugin-opt=-amdgpu-enable-hipstdpar" ); |
| 616 | |
| 617 | if (auto LTO = getToolChain().getLTOMode(Args); LTO != LTOK_None) { |
| 618 | addLTOOptions(ToolChain: getToolChain(), Args, CmdArgs, Output, Inputs, |
| 619 | IsThinLTO: LTO == LTOK_Thin); |
| 620 | } else if (Args.hasArg(Ids: options::OPT_mcpu_EQ)) { |
| 621 | CmdArgs.push_back(Elt: Args.MakeArgString( |
| 622 | Str: "-plugin-opt=mcpu=" + |
| 623 | getProcessorFromTargetID(T: getToolChain().getTriple(), |
| 624 | OffloadArch: Args.getLastArgValue(Id: options::OPT_mcpu_EQ)))); |
| 625 | } |
| 626 | addLinkerCompressDebugSectionsOption(TC: getToolChain(), Args, CmdArgs); |
| 627 | getToolChain().AddFilePathLibArgs(Args, CmdArgs); |
| 628 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_L); |
| 629 | AddLinkerInputs(TC: getToolChain(), Inputs, Args, CmdArgs, JA); |
| 630 | |
| 631 | // Always pass the target-id features to the LTO job. |
| 632 | std::vector<StringRef> Features; |
| 633 | getAMDGPUTargetFeatures(D: C.getDriver(), Triple: getToolChain().getEffectiveTriple(), |
| 634 | Args, Features); |
| 635 | if (!Features.empty()) { |
| 636 | CmdArgs.push_back( |
| 637 | Elt: Args.MakeArgString(Str: "-plugin-opt=-mattr=" + llvm::join(R&: Features, Separator: "," ))); |
| 638 | } |
| 639 | |
| 640 | getToolChain().addProfileRTLibs(Args, CmdArgs); |
| 641 | addSanitizerRuntimes(TC: getToolChain(), Args, CmdArgs); |
| 642 | |
| 643 | if (Args.hasArg(Ids: options::OPT_stdlib)) |
| 644 | CmdArgs.append(IL: {"-lc" , "-lm" }); |
| 645 | if (Args.hasArg(Ids: options::OPT_startfiles)) { |
| 646 | std::optional<std::string> IncludePath = getToolChain().getStdlibPath(); |
| 647 | if (!IncludePath) |
| 648 | IncludePath = "/lib" ; |
| 649 | SmallString<128> P(*IncludePath); |
| 650 | llvm::sys::path::append(path&: P, a: "crt1.o" ); |
| 651 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: P)); |
| 652 | } |
| 653 | |
| 654 | CmdArgs.push_back(Elt: "-o" ); |
| 655 | CmdArgs.push_back(Elt: Output.getFilename()); |
| 656 | C.addCommand(Cmd: std::make_unique<Command>( |
| 657 | args: JA, args: *this, args: ResponseFileSupport::AtFileCurCP(), args: Args.MakeArgString(Str: Linker), |
| 658 | args&: CmdArgs, args: Inputs, args: Output)); |
| 659 | } |
| 660 | |
| 661 | void amdgpu::getAMDGPUTargetFeatures(const Driver &D, |
| 662 | const llvm::Triple &Triple, |
| 663 | const llvm::opt::ArgList &Args, |
| 664 | std::vector<StringRef> &Features) { |
| 665 | if (Args.hasFlag(Pos: options::OPT_mwavefrontsize64, |
| 666 | Neg: options::OPT_mno_wavefrontsize64, Default: false)) |
| 667 | Features.push_back(x: "+wavefrontsize64" ); |
| 668 | |
| 669 | if (Args.hasFlag(Pos: options::OPT_mamdgpu_precise_memory_op, |
| 670 | Neg: options::OPT_mno_amdgpu_precise_memory_op, Default: false)) |
| 671 | Features.push_back(x: "+precise-memory" ); |
| 672 | |
| 673 | handleTargetFeaturesGroup(D, Triple, Args, Features, |
| 674 | Group: options::OPT_m_amdgpu_Features_Group); |
| 675 | } |
| 676 | |
| 677 | /// AMDGPU Toolchain |
| 678 | AMDGPUToolChain::AMDGPUToolChain(const Driver &D, const llvm::Triple &Triple, |
| 679 | const ArgList &Args, const ToolChain *HostTC_, |
| 680 | Action::OffloadKind Kind, |
| 681 | bool ShouldLinkDeviceLibs) |
| 682 | : Generic_ELF(D, Triple, Args), |
| 683 | OptionsDefault( |
| 684 | {{options::OPT_O, "3" }, {options::OPT_cl_std_EQ, "CL1.2" }}), |
| 685 | HostTC(HostTC_), UseHIPLinker(Kind == Action::OFK_HIP), |
| 686 | ShouldLinkDeviceLibs(ShouldLinkDeviceLibs) { |
| 687 | loadMultilibsFromYAML(Args, D); |
| 688 | |
| 689 | // Check code object version options. Emit warnings for legacy options |
| 690 | // and errors for the last invalid code object version options. |
| 691 | // It is done here to avoid repeated warning or error messages for |
| 692 | // each tool invocation. |
| 693 | checkAMDGPUCodeObjectVersion(D, Args); |
| 694 | |
| 695 | if (Triple.getOS() == llvm::Triple::AMDHSA && |
| 696 | Triple.getEnvironment() != llvm::Triple::LLVM) |
| 697 | RocmInstallation->detectDeviceLibrary(); |
| 698 | |
| 699 | if (HostTC) |
| 700 | getProgramPaths().push_back(Elt: getDriver().Dir); |
| 701 | } |
| 702 | |
| 703 | Tool *AMDGPUToolChain::buildLinker() const { |
| 704 | // FIXME: Should not have 2 linker paths. |
| 705 | if (UseHIPLinker) |
| 706 | return new tools::AMDGCN::Linker(*this); |
| 707 | return new tools::amdgpu::Linker(*this); |
| 708 | } |
| 709 | |
| 710 | DerivedArgList * |
| 711 | AMDGPUToolChain::TranslateArgs(const DerivedArgList &Args, BoundArch BA, |
| 712 | Action::OffloadKind DeviceOffloadKind) const { |
| 713 | DerivedArgList *DAL = Generic_ELF::TranslateArgs(Args, BA, DeviceOffloadKind); |
| 714 | if (!DAL) { |
| 715 | DAL = new DerivedArgList(Args.getBaseArgs()); |
| 716 | for (Arg *A : Args) |
| 717 | DAL->append(A); |
| 718 | } |
| 719 | |
| 720 | const OptTable &Opts = getDriver().getOpts(); |
| 721 | |
| 722 | if (DeviceOffloadKind == Action::OFK_None) { |
| 723 | // AMDGPU is intended to use `-mcpu` but we accept `-march` for legacy. |
| 724 | if (Arg *A = DAL->getLastArg(Ids: options::OPT_march_EQ)) { |
| 725 | DAL->eraseArg(Id: options::OPT_march_EQ); |
| 726 | if (!DAL->hasArg(Ids: options::OPT_mcpu_EQ)) |
| 727 | DAL->AddJoinedArg(BaseArg: A, Opt: Opts.getOption(Opt: options::OPT_mcpu_EQ), |
| 728 | Value: A->getValue()); |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | // Replace -mcpu=native with detected GPU. |
| 733 | Arg *LastMCPUArg = DAL->getLastArg(Ids: options::OPT_mcpu_EQ); |
| 734 | if (LastMCPUArg && StringRef(LastMCPUArg->getValue()) == "native" ) { |
| 735 | DAL->eraseArg(Id: options::OPT_mcpu_EQ); |
| 736 | auto GPUsOrErr = getSystemGPUArchs(Args); |
| 737 | if (!GPUsOrErr) { |
| 738 | getDriver().Diag(DiagID: diag::err_drv_undetermined_gpu_arch) |
| 739 | << getArchName() << llvm::toString(E: GPUsOrErr.takeError()) << "-mcpu" ; |
| 740 | } else { |
| 741 | auto &GPUs = *GPUsOrErr; |
| 742 | if (!llvm::all_equal(Range&: GPUs)) |
| 743 | getDriver().Diag(DiagID: diag::warn_drv_multi_gpu_arch) |
| 744 | << getArchName() << llvm::join(R&: GPUs, Separator: ", " ) << "-mcpu" ; |
| 745 | DAL->AddJoinedArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: options::OPT_mcpu_EQ), |
| 746 | Value: Args.MakeArgString(Str: GPUs.front())); |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | if (!BA.empty()) { |
| 751 | DAL->eraseArg(Id: options::OPT_mcpu_EQ); |
| 752 | DAL->AddJoinedArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: options::OPT_mcpu_EQ), |
| 753 | Value: BA.ArchName); |
| 754 | } |
| 755 | |
| 756 | if (!getTriple().isSPIRV()) { |
| 757 | AMDGPUToolChain::ParsedTargetIDType PTID = checkTargetID(DriverArgs: *DAL); |
| 758 | |
| 759 | // Synthesize feature flags for target ID modifiers (xnack, sramecc). |
| 760 | if (PTID.OptionalFeatureMap) { |
| 761 | const llvm::StringMap<bool> &FeatureMap = *PTID.OptionalFeatureMap; |
| 762 | |
| 763 | auto XnackIt = FeatureMap.find(Key: "xnack" ); |
| 764 | if (XnackIt != FeatureMap.end()) { |
| 765 | DAL->AddFlagArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: XnackIt->second |
| 766 | ? options::OPT_mxnack |
| 767 | : options::OPT_mno_xnack)); |
| 768 | } |
| 769 | |
| 770 | auto SrameccIt = FeatureMap.find(Key: "sramecc" ); |
| 771 | if (SrameccIt != FeatureMap.end()) { |
| 772 | DAL->AddFlagArg(BaseArg: nullptr, |
| 773 | Opt: Opts.getOption(Opt: SrameccIt->second |
| 774 | ? options::OPT_msramecc |
| 775 | : options::OPT_mno_sramecc)); |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | // Filter out sanitizer coverage options that are not supported for AMDGPU. |
| 781 | for (Arg *A : Args) { |
| 782 | // Sanitizer coverage is currently not supported for AMDGPU. |
| 783 | if (A->getOption().matches(ID: options::OPT_fsan_cov_Group)) { |
| 784 | // Upgrade to error if the option was explicitly specified for device |
| 785 | bool IsExplicitDevice = |
| 786 | A->getBaseArg().getOption().matches(ID: options::OPT_Xarch_device); |
| 787 | getDriver().Diag(DiagID: IsExplicitDevice |
| 788 | ? diag::err_drv_unsupported_option_for_target |
| 789 | : diag::warn_drv_unsupported_option_for_target) |
| 790 | << A->getAsString(Args) << getTriple().str(); |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | if (Args.getLastArgValue(Id: options::OPT_x) != "cl" ) |
| 795 | return DAL; |
| 796 | |
| 797 | // Phase 1 (.cl -> .bc) |
| 798 | if (Args.hasArg(Ids: options::OPT_c) && Args.hasArg(Ids: options::OPT_emit_llvm)) { |
| 799 | // Have to check OPT_O4, OPT_O0 & OPT_Ofast separately |
| 800 | // as they defined that way in Options.td |
| 801 | if (!Args.hasArg(Ids: options::OPT_O, Ids: options::OPT_O0, Ids: options::OPT_O4, |
| 802 | Ids: options::OPT_Ofast)) |
| 803 | DAL->AddJoinedArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: options::OPT_O), |
| 804 | Value: getOptionDefault(OptID: options::OPT_O)); |
| 805 | } |
| 806 | |
| 807 | return DAL; |
| 808 | } |
| 809 | |
| 810 | bool AMDGPUToolChain::getDefaultDenormsAreZeroForTarget( |
| 811 | llvm::AMDGPU::GPUKind Kind) { |
| 812 | |
| 813 | // Assume nothing without a specific target. |
| 814 | if (Kind == llvm::AMDGPU::GK_NONE) |
| 815 | return false; |
| 816 | |
| 817 | const unsigned ArchAttr = llvm::AMDGPU::getArchAttrAMDGCN(AK: Kind); |
| 818 | |
| 819 | // Default to enabling f32 denormals by default on subtargets where fma is |
| 820 | // fast with denormals |
| 821 | const bool BothDenormAndFMAFast = |
| 822 | (ArchAttr & llvm::AMDGPU::FEATURE_FAST_FMA_F32) && |
| 823 | (ArchAttr & llvm::AMDGPU::FEATURE_FAST_DENORMAL_F32); |
| 824 | return !BothDenormAndFMAFast; |
| 825 | } |
| 826 | |
| 827 | llvm::DenormalMode AMDGPUToolChain::getDefaultDenormalModeForType( |
| 828 | const llvm::opt::ArgList &DriverArgs, const JobAction &JA, |
| 829 | const llvm::fltSemantics *FPType) const { |
| 830 | // Denormals should always be enabled for f16 and f64. |
| 831 | if (!FPType || FPType != &llvm::APFloat::IEEEsingle()) |
| 832 | return llvm::DenormalMode::getIEEE(); |
| 833 | |
| 834 | if (JA.getOffloadingDeviceKind() == Action::OFK_HIP || |
| 835 | JA.getOffloadingDeviceKind() == Action::OFK_Cuda) { |
| 836 | BoundArch BA = JA.getOffloadingArch(); |
| 837 | // FIXME: Missing conversion from OffloadArch to GPUKind |
| 838 | auto Arch = getProcessorFromTargetID(T: getTriple(), OffloadArch: BA.ArchName); |
| 839 | auto Kind = llvm::AMDGPU::parseArchAMDGCN(CPU: Arch); |
| 840 | if (FPType && FPType == &llvm::APFloat::IEEEsingle() && |
| 841 | DriverArgs.hasFlag(Pos: options::OPT_fgpu_flush_denormals_to_zero, |
| 842 | Neg: options::OPT_fno_gpu_flush_denormals_to_zero, |
| 843 | Default: getDefaultDenormsAreZeroForTarget(Kind))) |
| 844 | return llvm::DenormalMode::getPreserveSign(); |
| 845 | |
| 846 | return llvm::DenormalMode::getIEEE(); |
| 847 | } |
| 848 | |
| 849 | const StringRef GpuArch = getGPUArch(DriverArgs); |
| 850 | auto Kind = llvm::AMDGPU::parseArchAMDGCN(CPU: GpuArch); |
| 851 | |
| 852 | // TODO: There are way too many flags that change this. Do we need to check |
| 853 | // them all? |
| 854 | bool DAZ = DriverArgs.hasArg(Ids: options::OPT_cl_denorms_are_zero) || |
| 855 | getDefaultDenormsAreZeroForTarget(Kind); |
| 856 | |
| 857 | // Outputs are flushed to zero (FTZ), preserving sign. Denormal inputs are |
| 858 | // also implicit treated as zero (DAZ). |
| 859 | return DAZ ? llvm::DenormalMode::getPreserveSign() : |
| 860 | llvm::DenormalMode::getIEEE(); |
| 861 | } |
| 862 | |
| 863 | bool AMDGPUToolChain::isWave64(const llvm::opt::ArgList &DriverArgs, |
| 864 | llvm::AMDGPU::GPUKind Kind) { |
| 865 | const unsigned ArchAttr = llvm::AMDGPU::getArchAttrAMDGCN(AK: Kind); |
| 866 | bool HasWave32 = (ArchAttr & llvm::AMDGPU::FEATURE_WAVE32); |
| 867 | |
| 868 | return !HasWave32 || DriverArgs.hasFlag( |
| 869 | Pos: options::OPT_mwavefrontsize64, Neg: options::OPT_mno_wavefrontsize64, Default: false); |
| 870 | } |
| 871 | |
| 872 | void AMDGPUToolChain::addClangTargetOptions( |
| 873 | const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, |
| 874 | BoundArch BA, Action::OffloadKind DeviceOffloadingKind) const { |
| 875 | if (DeviceOffloadingKind == Action::OFK_HIP) { |
| 876 | CC1Args.append(IL: {"-fcuda-is-device" , "-fno-threadsafe-statics" }); |
| 877 | |
| 878 | if (!DriverArgs.hasFlag(Pos: options::OPT_fgpu_rdc, Neg: options::OPT_fno_gpu_rdc, |
| 879 | Default: false)) { |
| 880 | CC1Args.append(IL: {"-mllvm" , "-amdgpu-internalize-symbols" }); |
| 881 | if (DriverArgs.hasArgNoClaim(Ids: options::OPT_hipstdpar)) |
| 882 | CC1Args.append(IL: {"-mllvm" , "-amdgpu-enable-hipstdpar" }); |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | DriverArgs.AddLastArg(Output&: CC1Args, Ids: options::OPT_gpu_max_threads_per_block_EQ); |
| 887 | |
| 888 | // Default to "hidden" visibility, as object level linking will not be |
| 889 | // supported for the foreseeable future. |
| 890 | // TODO: remove the SPIR-V bypass once it can encode (hidden) visibility. |
| 891 | if (!DriverArgs.hasArg(Ids: options::OPT_fvisibility_EQ, |
| 892 | Ids: options::OPT_fvisibility_ms_compat) && |
| 893 | !getEffectiveTriple().isSPIRV() && !getDriver().IsFlangMode()) { |
| 894 | CC1Args.push_back(Elt: "-fvisibility=hidden" ); |
| 895 | CC1Args.push_back(Elt: "-fapply-global-visibility-to-externs" ); |
| 896 | } |
| 897 | |
| 898 | if (getEffectiveTriple().isSPIRV()) { |
| 899 | // For HIP + SPIRV, embed the command-line into the generated binary |
| 900 | if (DeviceOffloadingKind == Action::OFK_HIP && |
| 901 | !DriverArgs.hasArg(Ids: options::OPT_fembed_bitcode_marker)) |
| 902 | CC1Args.push_back(Elt: "-fembed-bitcode=marker" ); |
| 903 | |
| 904 | // For SPIR-V we want to retain the pristine output of Clang CodeGen, since |
| 905 | // optimizations might lose structure / information that is necessary for |
| 906 | // generating optimal concrete AMDGPU code. |
| 907 | // |
| 908 | // For standalone SPIR-V, use -disable-llvm-optzns |
| 909 | // TODO: using the below option is a temporary placeholder until Clang |
| 910 | // provides the required functionality, which essentially boils down |
| 911 | // to -O0 being refactored / reworked to not imply optnone / remove |
| 912 | // TBAA. Once that is added, we should pivot to that functionality, |
| 913 | // being mindful to not corrupt the user provided and subsequently |
| 914 | // embedded command-line (i.e. if the user asks for -O3 this is what |
| 915 | // the finalisation should use). |
| 916 | if (!DriverArgs.hasArg(Ids: options::OPT_disable_llvm_optzns)) |
| 917 | CC1Args.push_back(Elt: "-disable-llvm-optzns" ); |
| 918 | |
| 919 | return; // No DeviceLibs for SPIR-V. |
| 920 | } |
| 921 | |
| 922 | if (DeviceOffloadingKind == Action::OFK_None) { |
| 923 | // For the OpenCL case where there is no offload target, accept -nostdlib to |
| 924 | // disable bitcode linking. |
| 925 | if (DriverArgs.hasArg(Ids: options::OPT_nostdlib)) |
| 926 | return; |
| 927 | |
| 928 | if (addOpenCLBuiltinsLib(D: getDriver(), TT: getTriple(), DriverArgs, CC1Args)) |
| 929 | return; |
| 930 | } |
| 931 | |
| 932 | if (!DriverArgs.hasFlag(Pos: options::OPT_offloadlib, Neg: options::OPT_no_offloadlib, |
| 933 | Default: true)) |
| 934 | return; |
| 935 | |
| 936 | // With an LLVM environment, only use libraries provided by the resource |
| 937 | // directory. |
| 938 | if (getEffectiveTriple().getEnvironment() == llvm::Triple::LLVM) |
| 939 | return; |
| 940 | |
| 941 | // Link device libraries for OpenCL, HIP, and OpenMP |
| 942 | for (auto BCFile : getDeviceLibs(Args: DriverArgs, BA, DeviceOffloadKind: DeviceOffloadingKind)) { |
| 943 | CC1Args.push_back(Elt: BCFile.ShouldInternalize ? "-mlink-builtin-bitcode" |
| 944 | : "-mlink-bitcode-file" ); |
| 945 | CC1Args.push_back(Elt: DriverArgs.MakeArgStringRef(Str: BCFile.Path)); |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | void AMDGPUToolChain::addClangWarningOptions(ArgStringList &CC1Args) const { |
| 950 | // AMDGPU does not support atomic lib call. Treat atomic alignment |
| 951 | // warnings as errors. |
| 952 | CC1Args.push_back(Elt: "-Werror=atomic-alignment" ); |
| 953 | } |
| 954 | |
| 955 | void AMDGPUToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, |
| 956 | ArgStringList &CC1Args) const { |
| 957 | // In an offloading compilation the device toolchain must pick up the host's |
| 958 | // system include paths, even when compiling device code. |
| 959 | if (HostTC) { |
| 960 | HostTC->AddClangSystemIncludeArgs(DriverArgs, CC1Args); |
| 961 | return; |
| 962 | } |
| 963 | |
| 964 | if (DriverArgs.hasArg(Ids: options::OPT_nostdinc) || |
| 965 | DriverArgs.hasArg(Ids: options::OPT_nostdlibinc)) |
| 966 | return; |
| 967 | |
| 968 | // Add multilib variant include paths in priority order. |
| 969 | for (const Multilib &M : getOrderedMultilibs()) { |
| 970 | if (M.isDefault()) |
| 971 | continue; |
| 972 | if (std::optional<std::string> StdlibIncDir = getStdlibIncludePath()) { |
| 973 | SmallString<128> Dir(*StdlibIncDir); |
| 974 | llvm::sys::path::append(path&: Dir, a: M.includeSuffix()); |
| 975 | if (getDriver().getVFS().exists(Path: Dir)) |
| 976 | addSystemInclude(DriverArgs, CC1Args, Path: Dir); |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | if (std::optional<std::string> Path = getStdlibIncludePath()) |
| 981 | addSystemInclude(DriverArgs, CC1Args, Path: *Path); |
| 982 | } |
| 983 | |
| 984 | StringRef |
| 985 | AMDGPUToolChain::getGPUArch(const llvm::opt::ArgList &DriverArgs) const { |
| 986 | return getProcessorFromTargetID( |
| 987 | T: getTriple(), OffloadArch: DriverArgs.getLastArgValue(Id: options::OPT_mcpu_EQ)); |
| 988 | } |
| 989 | |
| 990 | AMDGPUToolChain::ParsedTargetIDType |
| 991 | AMDGPUToolChain::getParsedTargetID(const llvm::opt::ArgList &DriverArgs) const { |
| 992 | StringRef TargetID = DriverArgs.getLastArgValue(Id: options::OPT_mcpu_EQ); |
| 993 | if (TargetID.empty()) |
| 994 | return {}; |
| 995 | |
| 996 | llvm::StringMap<bool> FeatureMap; |
| 997 | auto OptionalGpuArch = parseTargetID(T: getTriple(), OffloadArch: TargetID, FeatureMap: &FeatureMap); |
| 998 | if (!OptionalGpuArch) |
| 999 | return {.OptionalTargetID: TargetID.str(), .OptionalGPUArch: std::nullopt, .OptionalFeatureMap: std::nullopt}; |
| 1000 | |
| 1001 | return {.OptionalTargetID: TargetID.str(), .OptionalGPUArch: OptionalGpuArch->str(), .OptionalFeatureMap: FeatureMap}; |
| 1002 | } |
| 1003 | |
| 1004 | AMDGPUToolChain::ParsedTargetIDType |
| 1005 | AMDGPUToolChain::checkTargetID(const llvm::opt::ArgList &DriverArgs) const { |
| 1006 | auto PTID = getParsedTargetID(DriverArgs); |
| 1007 | if (PTID.OptionalTargetID && !PTID.OptionalGPUArch) { |
| 1008 | getDriver().Diag(DiagID: clang::diag::err_drv_bad_target_id) |
| 1009 | << *PTID.OptionalTargetID; |
| 1010 | } |
| 1011 | return PTID; |
| 1012 | } |
| 1013 | |
| 1014 | Expected<SmallVector<std::string>> |
| 1015 | AMDGPUToolChain::getSystemGPUArchs(const ArgList &Args) const { |
| 1016 | // Detect AMD GPUs availible on the system. |
| 1017 | std::string Program; |
| 1018 | if (Arg *A = Args.getLastArg(Ids: options::OPT_offload_arch_tool_EQ)) |
| 1019 | Program = A->getValue(); |
| 1020 | else |
| 1021 | Program = GetProgramPath(Name: "amdgpu-arch" ); |
| 1022 | |
| 1023 | auto StdoutOrErr = getDriver().executeProgram(Args: {Program}); |
| 1024 | if (!StdoutOrErr) |
| 1025 | return StdoutOrErr.takeError(); |
| 1026 | |
| 1027 | SmallVector<std::string, 1> GPUArchs; |
| 1028 | for (StringRef Arch : llvm::split(Str: (*StdoutOrErr)->getBuffer(), Separator: "\n" )) |
| 1029 | if (!Arch.empty()) |
| 1030 | GPUArchs.push_back(Elt: Arch.str()); |
| 1031 | |
| 1032 | if (GPUArchs.empty()) |
| 1033 | return llvm::createStringError(EC: std::error_code(), |
| 1034 | S: "No AMD GPU detected in the system" ); |
| 1035 | |
| 1036 | return std::move(GPUArchs); |
| 1037 | } |
| 1038 | |
| 1039 | bool RocmInstallationDetector::checkCommonBitcodeLibs( |
| 1040 | StringRef GPUArch, StringRef LibDeviceFile, |
| 1041 | DeviceLibABIVersion ABIVer) const { |
| 1042 | if (!hasDeviceLibrary()) { |
| 1043 | D.Diag(DiagID: diag::err_drv_no_rocm_device_lib) << 0; |
| 1044 | return false; |
| 1045 | } |
| 1046 | if (LibDeviceFile.empty()) { |
| 1047 | D.Diag(DiagID: diag::err_drv_no_rocm_device_lib) << 1 << GPUArch; |
| 1048 | return false; |
| 1049 | } |
| 1050 | if (ABIVer.requiresLibrary() && getABIVersionPath(ABIVer).empty()) { |
| 1051 | // Starting from COV6, we will report minimum ROCm version requirement in |
| 1052 | // the error message. |
| 1053 | if (ABIVer.getAsCodeObjectVersion() < 6) |
| 1054 | D.Diag(DiagID: diag::err_drv_no_rocm_device_lib) << 2 << ABIVer.toString() << 0; |
| 1055 | else |
| 1056 | D.Diag(DiagID: diag::err_drv_no_rocm_device_lib) |
| 1057 | << 2 << ABIVer.toString() << 1 << "6.3" ; |
| 1058 | return false; |
| 1059 | } |
| 1060 | return true; |
| 1061 | } |
| 1062 | |
| 1063 | llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12> |
| 1064 | RocmInstallationDetector::getCommonBitcodeLibs( |
| 1065 | const llvm::opt::ArgList &DriverArgs, StringRef LibDeviceFile, |
| 1066 | StringRef GPUArch, const Action::OffloadKind DeviceOffloadingKind, |
| 1067 | const bool NeedsASanRT) const { |
| 1068 | llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12> BCLibs; |
| 1069 | |
| 1070 | CommonBitcodeLibsPreferences Pref{D, DriverArgs, GPUArch, |
| 1071 | DeviceOffloadingKind, NeedsASanRT}; |
| 1072 | |
| 1073 | auto AddBCLib = [&](ToolChain::BitCodeLibraryInfo BCLib, |
| 1074 | bool Internalize = true) { |
| 1075 | if (!BCLib.Path.empty()) { |
| 1076 | BCLib.ShouldInternalize = Internalize; |
| 1077 | BCLibs.emplace_back(Args&: BCLib); |
| 1078 | } |
| 1079 | }; |
| 1080 | auto AddSanBCLibs = [&]() { |
| 1081 | if (Pref.GPUSan) |
| 1082 | AddBCLib(getAsanRTLPath(), false); |
| 1083 | }; |
| 1084 | |
| 1085 | AddSanBCLibs(); |
| 1086 | AddBCLib(getOCMLPath()); |
| 1087 | if (!Pref.IsOpenMP) |
| 1088 | AddBCLib(getOCKLPath()); |
| 1089 | else if (Pref.GPUSan && Pref.IsOpenMP) |
| 1090 | AddBCLib(getOCKLPath()); |
| 1091 | AddBCLib(getUnsafeMathPath(Enabled: Pref.UnsafeMathOpt || Pref.FastRelaxedMath)); |
| 1092 | AddBCLib(getFiniteOnlyPath(Enabled: Pref.FiniteOnly || Pref.FastRelaxedMath)); |
| 1093 | AddBCLib(getWavefrontSize64Path(Enabled: Pref.Wave64)); |
| 1094 | AddBCLib(LibDeviceFile); |
| 1095 | auto ABIVerPath = getABIVersionPath(ABIVer: Pref.ABIVer); |
| 1096 | if (!ABIVerPath.empty()) |
| 1097 | AddBCLib(ABIVerPath); |
| 1098 | |
| 1099 | return BCLibs; |
| 1100 | } |
| 1101 | |
| 1102 | llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12> |
| 1103 | AMDGPUToolChain::getCommonDeviceLibNames( |
| 1104 | const llvm::opt::ArgList &DriverArgs, llvm::StringRef TargetID, |
| 1105 | llvm::StringRef GPUArch, Action::OffloadKind DeviceOffloadingKind) const { |
| 1106 | auto Kind = llvm::AMDGPU::parseArchAMDGCN(CPU: GPUArch); |
| 1107 | const StringRef CanonArch = llvm::AMDGPU::getArchNameAMDGCN(AK: Kind); |
| 1108 | |
| 1109 | StringRef LibDeviceFile = RocmInstallation->getLibDeviceFile(Gpu: CanonArch); |
| 1110 | auto ABIVer = DeviceLibABIVersion::fromCodeObjectVersion( |
| 1111 | CodeObjectVersion: getAMDGPUCodeObjectVersion(D: getDriver(), Args: DriverArgs)); |
| 1112 | if (!RocmInstallation->checkCommonBitcodeLibs(GPUArch: CanonArch, LibDeviceFile, |
| 1113 | ABIVer)) |
| 1114 | return {}; |
| 1115 | |
| 1116 | return RocmInstallation->getCommonBitcodeLibs( |
| 1117 | DriverArgs, LibDeviceFile, GPUArch, DeviceOffloadingKind, |
| 1118 | NeedsASanRT: getSanitizerArgs(JobArgs: DriverArgs, BA: BoundArch(TargetID), DeviceOffloadKind: DeviceOffloadingKind) |
| 1119 | .needsAsanRt()); |
| 1120 | } |
| 1121 | |
| 1122 | llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12> |
| 1123 | AMDGPUToolChain::getDeviceLibs(const llvm::opt::ArgList &DriverArgs, |
| 1124 | BoundArch BA, |
| 1125 | Action::OffloadKind DeviceOffloadKind) const { |
| 1126 | assert(getEffectiveTriple().isAMDGPU() && |
| 1127 | "spirv should not try to link device libs" ); |
| 1128 | |
| 1129 | if (!DriverArgs.hasFlag(Pos: options::OPT_offloadlib, Neg: options::OPT_no_offloadlib, |
| 1130 | Default: true) || |
| 1131 | getEffectiveTriple().getEnvironment() == llvm::Triple::LLVM) |
| 1132 | return {}; |
| 1133 | |
| 1134 | if (getTriple().getOS() != llvm::Triple::AMDHSA) |
| 1135 | return {}; |
| 1136 | |
| 1137 | StringRef GpuArch; |
| 1138 | StringRef TargetID; |
| 1139 | if (DeviceOffloadKind == Action::OFK_None) { |
| 1140 | TargetID = DriverArgs.getLastArgValue(Id: options::OPT_mcpu_EQ); |
| 1141 | GpuArch = getProcessorFromTargetID(T: getTriple(), OffloadArch: TargetID); |
| 1142 | } else { |
| 1143 | TargetID = BA.ArchName; |
| 1144 | GpuArch = getProcessorFromTargetID(T: getTriple(), OffloadArch: BA.ArchName); |
| 1145 | } |
| 1146 | |
| 1147 | llvm::SmallVector<BitCodeLibraryInfo, 12> BCLibs; |
| 1148 | |
| 1149 | // HIP-specific handling |
| 1150 | if (DeviceOffloadKind == Action::OFK_HIP) { |
| 1151 | // Handle --hip-device-lib manual override |
| 1152 | auto BCLibArgs = DriverArgs.getAllArgValues(Id: options::OPT_hip_device_lib_EQ); |
| 1153 | if (!BCLibArgs.empty()) { |
| 1154 | ArgStringList LibraryPaths; |
| 1155 | for (StringRef Path : RocmInstallation->getRocmDeviceLibPathArg()) |
| 1156 | LibraryPaths.push_back(Elt: DriverArgs.MakeArgStringRef(Str: Path)); |
| 1157 | addDirectoryList(Args: DriverArgs, CmdArgs&: LibraryPaths, ArgName: "" , EnvVar: "HIP_DEVICE_LIB_PATH" ); |
| 1158 | |
| 1159 | for (StringRef BCName : BCLibArgs) { |
| 1160 | bool Found = false; |
| 1161 | for (StringRef LibraryPath : LibraryPaths) { |
| 1162 | SmallString<128> Path(LibraryPath); |
| 1163 | llvm::sys::path::append(path&: Path, a: BCName); |
| 1164 | if (llvm::sys::fs::exists(Path)) { |
| 1165 | BCLibs.emplace_back(Args&: Path); |
| 1166 | Found = true; |
| 1167 | break; |
| 1168 | } |
| 1169 | } |
| 1170 | if (!Found) |
| 1171 | getDriver().Diag(DiagID: diag::err_drv_no_such_file) << BCName; |
| 1172 | } |
| 1173 | return BCLibs; |
| 1174 | } |
| 1175 | |
| 1176 | if (!RocmInstallation->hasDeviceLibrary()) { |
| 1177 | getDriver().Diag(DiagID: diag::err_drv_no_rocm_device_lib) << 0; |
| 1178 | return {}; |
| 1179 | } |
| 1180 | |
| 1181 | // Add common device libraries |
| 1182 | for (auto N : getCommonDeviceLibNames(DriverArgs, TargetID, GPUArch: GpuArch, |
| 1183 | DeviceOffloadingKind: DeviceOffloadKind)) |
| 1184 | BCLibs.emplace_back(Args&: N); |
| 1185 | |
| 1186 | // Add instrument lib for HIP |
| 1187 | auto InstLib = |
| 1188 | DriverArgs.getLastArgValue(Id: options::OPT_gpu_instrument_lib_EQ); |
| 1189 | if (!InstLib.empty()) { |
| 1190 | if (llvm::sys::fs::exists(Path: InstLib)) |
| 1191 | BCLibs.emplace_back(Args&: InstLib); |
| 1192 | else |
| 1193 | getDriver().Diag(DiagID: diag::err_drv_no_such_file) << InstLib; |
| 1194 | } |
| 1195 | |
| 1196 | return BCLibs; |
| 1197 | } |
| 1198 | |
| 1199 | // OpenMP handling |
| 1200 | if (DeviceOffloadKind == Action::OFK_OpenMP) { |
| 1201 | for (auto BCLib : getCommonDeviceLibNames(DriverArgs, TargetID, GPUArch: GpuArch, |
| 1202 | DeviceOffloadingKind: DeviceOffloadKind)) |
| 1203 | BCLibs.emplace_back(Args&: BCLib); |
| 1204 | return BCLibs; |
| 1205 | } |
| 1206 | |
| 1207 | // The libraries are currently only built for amdhsa. |
| 1208 | if (getTriple().getOS() != llvm::Triple::AMDHSA) |
| 1209 | return {}; |
| 1210 | |
| 1211 | // Only link device libraries if requested (set by Driver based on input type) |
| 1212 | if (!ShouldLinkDeviceLibs) |
| 1213 | return {}; |
| 1214 | |
| 1215 | StringRef LibDeviceFile = RocmInstallation->getLibDeviceFile(Gpu: GpuArch); |
| 1216 | |
| 1217 | auto ABIVer = DeviceLibABIVersion::fromCodeObjectVersion( |
| 1218 | CodeObjectVersion: getAMDGPUCodeObjectVersion(D: getDriver(), Args: DriverArgs)); |
| 1219 | if (!RocmInstallation->checkCommonBitcodeLibs(GPUArch: GpuArch, LibDeviceFile, ABIVer)) |
| 1220 | return {}; |
| 1221 | |
| 1222 | // Add the OpenCL specific bitcode library |
| 1223 | BCLibs.emplace_back(Args: RocmInstallation->getOpenCLPath().str()); |
| 1224 | |
| 1225 | // Add the generic set of libraries |
| 1226 | BCLibs.append(RHS: RocmInstallation->getCommonBitcodeLibs( |
| 1227 | DriverArgs, LibDeviceFile, GPUArch: GpuArch, DeviceOffloadingKind: DeviceOffloadKind, |
| 1228 | NeedsASanRT: getSanitizerArgs(JobArgs: DriverArgs, BA: BoundArch{TargetID}, DeviceOffloadKind) |
| 1229 | .needsAsanRt())); |
| 1230 | |
| 1231 | return BCLibs; |
| 1232 | } |
| 1233 | |
| 1234 | ToolChain::CXXStdlibType |
| 1235 | AMDGPUToolChain::GetCXXStdlibType(const ArgList &Args) const { |
| 1236 | if (HostTC) |
| 1237 | return HostTC->GetCXXStdlibType(Args); |
| 1238 | return ToolChain::GetCXXStdlibType(Args); |
| 1239 | } |
| 1240 | |
| 1241 | void AMDGPUToolChain::AddClangCXXStdlibIncludeArgs( |
| 1242 | const ArgList &Args, ArgStringList &CC1Args) const { |
| 1243 | if (HostTC) |
| 1244 | HostTC->AddClangCXXStdlibIncludeArgs(DriverArgs: Args, CC1Args); |
| 1245 | else |
| 1246 | ToolChain::AddClangCXXStdlibIncludeArgs(DriverArgs: Args, CC1Args); |
| 1247 | } |
| 1248 | |
| 1249 | void AMDGPUToolChain::AddIAMCUIncludeArgs(const ArgList &Args, |
| 1250 | ArgStringList &CC1Args) const { |
| 1251 | if (HostTC) |
| 1252 | HostTC->AddIAMCUIncludeArgs(DriverArgs: Args, CC1Args); |
| 1253 | } |
| 1254 | |
| 1255 | void AMDGPUToolChain::AddHIPIncludeArgs(const ArgList &DriverArgs, |
| 1256 | ArgStringList &CC1Args) const { |
| 1257 | if (getTriple().getEnvironment() == llvm::Triple::LLVM) { |
| 1258 | if (DriverArgs.hasFlag(Pos: options::OPT_offload_inc, |
| 1259 | Neg: options::OPT_no_offload_inc, Default: true) && |
| 1260 | !DriverArgs.hasArg(Ids: options::OPT_nohipwrapperinc) && |
| 1261 | !DriverArgs.hasArg(Ids: options::OPT_nobuiltininc)) |
| 1262 | CC1Args.append(IL: {"-include" , "__clang_gpu_device_functions.h" }); |
| 1263 | return; |
| 1264 | } |
| 1265 | |
| 1266 | RocmInstallation->AddHIPIncludeArgs(DriverArgs, CC1Args); |
| 1267 | } |
| 1268 | |
| 1269 | VersionTuple AMDGPUToolChain::computeMSVCVersion(const Driver *D, |
| 1270 | const ArgList &Args) const { |
| 1271 | if (HostTC) |
| 1272 | return HostTC->computeMSVCVersion(D, Args); |
| 1273 | return ToolChain::computeMSVCVersion(D, Args); |
| 1274 | } |
| 1275 | |
| 1276 | LTOKind AMDGPUToolChain::getDefaultLTOMode() const { |
| 1277 | // Offload toolchains use full LTO by default. |
| 1278 | return HostTC == nullptr ? LTOK_None : LTOK_Full; |
| 1279 | } |
| 1280 | |
| 1281 | LTOKind AMDGPUToolChain::getLTOMode(const ArgList &Args, |
| 1282 | Action::OffloadKind Kind) const { |
| 1283 | if (getTriple().isAMDGCN() && getDriver().offloadDeviceOnly() && |
| 1284 | !Args.hasFlag(Pos: options::OPT_fgpu_rdc, Neg: options::OPT_fno_gpu_rdc, Default: false) && |
| 1285 | !Args.hasArg(Ids: options::OPT_foffload_lto, Ids: options::OPT_foffload_lto_EQ)) |
| 1286 | return LTOK_None; |
| 1287 | return ToolChain::getLTOMode(Args, Kind); |
| 1288 | } |
| 1289 | |
| 1290 | static bool isXnackAvailable(const llvm::Triple &TT, llvm::StringRef TargetID) { |
| 1291 | // Arch-specific check - only report as supported if arch has xnack+ |
| 1292 | llvm::StringRef Processor = getProcessorFromTargetID(T: TT, OffloadArch: TargetID); |
| 1293 | auto ProcKind = TT.isAMDGCN() ? llvm::AMDGPU::parseArchAMDGCN(CPU: Processor) |
| 1294 | : llvm::AMDGPU::parseArchR600(CPU: Processor); |
| 1295 | auto Features = TT.isAMDGCN() ? llvm::AMDGPU::getArchAttrAMDGCN(AK: ProcKind) |
| 1296 | : llvm::AMDGPU::getArchAttrR600(AK: ProcKind); |
| 1297 | |
| 1298 | // If processor has xnack but doesn't support on/off modes, xnack is always on |
| 1299 | bool XnackAlwaysOn = (Features & llvm::AMDGPU::FEATURE_XNACK) && |
| 1300 | !(Features & llvm::AMDGPU::FEATURE_XNACK_ON_OFF_MODES); |
| 1301 | if (XnackAlwaysOn) |
| 1302 | return true; |
| 1303 | |
| 1304 | // Otherwise, check if xnack+ is explicitly enabled in the target ID |
| 1305 | llvm::StringMap<bool> FeatureMap; |
| 1306 | auto OptionalGpuArch = parseTargetID(T: TT, OffloadArch: TargetID, FeatureMap: &FeatureMap); |
| 1307 | if (!OptionalGpuArch) |
| 1308 | return false; |
| 1309 | auto Loc = FeatureMap.find(Key: "xnack" ); |
| 1310 | return (Loc != FeatureMap.end() && Loc->second); |
| 1311 | } |
| 1312 | |
| 1313 | SanitizerMask AMDGPUToolChain::getSupportedSanitizers( |
| 1314 | BoundArch BA, Action::OffloadKind DeviceOffloadKind) const { |
| 1315 | SanitizerMask SupportedMask = |
| 1316 | ToolChain::getSupportedSanitizers(BA, DeviceOffloadKind); |
| 1317 | |
| 1318 | // Address sanitizer is potentially supported, but depends on the exact target |
| 1319 | // arch xnack support. |
| 1320 | if (!BA || isXnackAvailable(TT: getTriple(), TargetID: BA.ArchName)) |
| 1321 | SupportedMask |= SanitizerKind::Address; |
| 1322 | |
| 1323 | return SupportedMask; |
| 1324 | } |
| 1325 | |
| 1326 | StringRef AMDGPUToolChain::getSanitizerRequirement(SanitizerMask Kinds, |
| 1327 | BoundArch BA) const { |
| 1328 | // Address sanitizer requires xnack+ feature |
| 1329 | if ((Kinds & SanitizerKind::Address) && BA && |
| 1330 | !isXnackAvailable(TT: getTriple(), TargetID: BA.ArchName)) { |
| 1331 | return "xnack+" ; |
| 1332 | } |
| 1333 | return "" ; |
| 1334 | } |
| 1335 | |