Skip to content

Commit b37d0d4

Browse files
authored
[SYCL] Simplify detection of device bitcode libraries for RTC (#19756)
By using installation detectors we can move away from creating full toolchains and querying them.
1 parent 5493c53 commit b37d0d4

21 files changed

+329
-265
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//===-- CudaInstallationDetector.h - Cuda Instalation Detector --*- 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+
#ifndef LLVM_CLANG_DRIVER_CUDAINSTALLATIONDETECTOR_H
10+
#define LLVM_CLANG_DRIVER_CUDAINSTALLATIONDETECTOR_H
11+
12+
#include "clang/Basic/Cuda.h"
13+
#include "clang/Driver/Driver.h"
14+
#include <bitset>
15+
16+
namespace clang {
17+
namespace driver {
18+
19+
/// A class to find a viable CUDA installation
20+
class CudaInstallationDetector {
21+
private:
22+
const Driver &D;
23+
bool IsValid = false;
24+
CudaVersion Version = CudaVersion::UNKNOWN;
25+
std::string InstallPath;
26+
std::string BinPath;
27+
std::string LibDevicePath;
28+
std::string IncludePath;
29+
llvm::StringMap<std::string> LibDeviceMap;
30+
31+
// CUDA architectures for which we have raised an error in
32+
// CheckCudaVersionSupportsArch.
33+
mutable std::bitset<(int)OffloadArch::LAST> ArchsWithBadVersion;
34+
35+
public:
36+
CudaInstallationDetector(const Driver &D, const llvm::Triple &HostTriple,
37+
const llvm::opt::ArgList &Args);
38+
39+
void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs,
40+
llvm::opt::ArgStringList &CC1Args) const;
41+
42+
/// Emit an error if Version does not support the given Arch.
43+
///
44+
/// If either Version or Arch is unknown, does not emit an error. Emits at
45+
/// most one error per Arch.
46+
void CheckCudaVersionSupportsArch(OffloadArch Arch) const;
47+
48+
/// Check whether we detected a valid Cuda install.
49+
bool isValid() const { return IsValid; }
50+
/// Print information about the detected CUDA installation.
51+
void print(raw_ostream &OS) const;
52+
53+
/// Get the detected Cuda install's version.
54+
CudaVersion version() const {
55+
return Version == CudaVersion::NEW ? CudaVersion::PARTIALLY_SUPPORTED
56+
: Version;
57+
}
58+
/// Get the detected Cuda installation path.
59+
StringRef getInstallPath() const { return InstallPath; }
60+
/// Get the detected path to Cuda's bin directory.
61+
StringRef getBinPath() const { return BinPath; }
62+
/// Get the detected Cuda Include path.
63+
StringRef getIncludePath() const { return IncludePath; }
64+
/// Get the detected Cuda device library path.
65+
StringRef getLibDevicePath() const { return LibDevicePath; }
66+
/// Get libdevice file for given architecture
67+
std::string getLibDeviceFile(StringRef Gpu) const {
68+
return LibDeviceMap.lookup(Gpu);
69+
}
70+
void WarnIfUnsupportedVersion() const;
71+
};
72+
73+
} // namespace driver
74+
} // namespace clang
75+
76+
#endif // LLVM_CLANG_DRIVER_CUDAINSTALLATIONDETECTOR_H

clang/lib/Driver/ToolChains/LazyDetector.h renamed to clang/include/clang/Driver/LazyDetector.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_LAZYDETECTOR_H
10-
#define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_LAZYDETECTOR_H
9+
#ifndef LLVM_CLANG_DRIVER_LAZYDETECTOR_H
10+
#define LLVM_CLANG_DRIVER_LAZYDETECTOR_H
1111

1212
#include "clang/Driver/Tool.h"
1313
#include "clang/Driver/ToolChain.h"
@@ -42,4 +42,4 @@ template <class T> class LazyDetector {
4242

4343
} // end namespace clang
4444

45-
#endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_LAZYDETECTOR_H
45+
#endif // LLVM_CLANG_DRIVER_LAZYDETECTOR_H

clang/lib/Driver/ToolChains/ROCm.h renamed to clang/include/clang/Driver/RocmInstallationDetector.h

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
1-
//===--- ROCm.h - ROCm installation detector --------------------*- C++ -*-===//
1+
//===-- RocmInstallationDetector.h - ROCm Instalation Detector --*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_ROCM_H
10-
#define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_ROCM_H
9+
#ifndef LLVM_CLANG_DRIVER_ROCMINSTALLATIONDETECTOR_H
10+
#define LLVM_CLANG_DRIVER_ROCMINSTALLATIONDETECTOR_H
1111

12-
#include "clang/Basic/Cuda.h"
13-
#include "clang/Basic/LLVM.h"
1412
#include "clang/Driver/Driver.h"
15-
#include "clang/Driver/Options.h"
16-
#include "clang/Driver/SanitizerArgs.h"
17-
#include "llvm/ADT/SmallString.h"
18-
#include "llvm/ADT/StringMap.h"
19-
#include "llvm/Option/ArgList.h"
20-
#include "llvm/Support/VersionTuple.h"
21-
#include "llvm/TargetParser/Triple.h"
2213

2314
namespace clang {
2415
namespace driver {
@@ -77,6 +68,24 @@ class RocmInstallationDetector {
7768
SPACKReleaseStr(SPACKReleaseStr.str()) {}
7869
};
7970

71+
struct CommonBitcodeLibsPreferences {
72+
CommonBitcodeLibsPreferences(const Driver &D,
73+
const llvm::opt::ArgList &DriverArgs,
74+
StringRef GPUArch,
75+
const Action::OffloadKind DeviceOffloadingKind,
76+
const bool NeedsASanRT);
77+
78+
DeviceLibABIVersion ABIVer;
79+
bool IsOpenMP;
80+
bool Wave64;
81+
bool DAZ;
82+
bool FiniteOnly;
83+
bool UnsafeMathOpt;
84+
bool FastRelaxedMath;
85+
bool CorrectSqrt;
86+
bool GPUSan;
87+
};
88+
8089
const Driver &D;
8190
bool HasHIPRuntime = false;
8291
bool HasDeviceLibrary = false;
@@ -175,11 +184,11 @@ class RocmInstallationDetector {
175184

176185
/// Get file paths of default bitcode libraries common to AMDGPU based
177186
/// toolchains.
178-
llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12> getCommonBitcodeLibs(
179-
const llvm::opt::ArgList &DriverArgs, StringRef LibDeviceFile,
180-
bool Wave64, bool DAZ, bool FiniteOnly, bool UnsafeMathOpt,
181-
bool FastRelaxedMath, bool CorrectSqrt, DeviceLibABIVersion ABIVer,
182-
bool GPUSan, bool isOpenMP) const;
187+
llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
188+
getCommonBitcodeLibs(const llvm::opt::ArgList &DriverArgs,
189+
StringRef LibDeviceFile, StringRef GPUArch,
190+
const Action::OffloadKind DeviceOffloadingKind,
191+
const bool NeedsASanRT) const;
183192
/// Check file paths of default bitcode libraries common to AMDGPU based
184193
/// toolchains. \returns false if there are invalid or missing files.
185194
bool checkCommonBitcodeLibs(StringRef GPUArch, StringRef LibDeviceFile,
@@ -288,7 +297,7 @@ class RocmInstallationDetector {
288297
StringRef getHIPVersion() const { return DetectedVersion; }
289298
};
290299

291-
} // end namespace driver
292-
} // end namespace clang
300+
} // namespace driver
301+
} // namespace clang
293302

294-
#endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_ROCM_H
303+
#endif // LLVM_CLANG_DRIVER_ROCMINSTALLATIONDETECTOR_H
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//===-- SyclInstallationDetector.h - SYCL Instalation Detector --*- 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+
#ifndef LLVM_CLANG_DRIVER_SYCLINSTALLATIONDETECTOR_H
10+
#define LLVM_CLANG_DRIVER_SYCLINSTALLATIONDETECTOR_H
11+
12+
#include "clang/Driver/Driver.h"
13+
14+
namespace clang {
15+
namespace driver {
16+
17+
class SYCLInstallationDetector {
18+
public:
19+
SYCLInstallationDetector(const Driver &D);
20+
SYCLInstallationDetector(const Driver &D, const llvm::Triple &HostTriple,
21+
const llvm::opt::ArgList &Args);
22+
23+
/// \brief Find and return the path to the libspirv library for the target
24+
/// \return The path to the libspirv library if found, otherwise nullptr.
25+
/// The lifetime of the returned string is managed by \p Args.
26+
const char *findLibspirvPath(const llvm::Triple &DeviceTriple,
27+
const llvm::opt::ArgList &Args,
28+
const llvm::Triple &HostTriple) const;
29+
30+
void addLibspirvLinkArgs(const llvm::Triple &DeviceTriple,
31+
const llvm::opt::ArgList &DriverArgs,
32+
const llvm::Triple &HostTriple,
33+
llvm::opt::ArgStringList &CC1Args) const;
34+
35+
void getSYCLDeviceLibPath(
36+
llvm::SmallVector<llvm::SmallString<128>, 4> &DeviceLibPaths) const;
37+
void addSYCLIncludeArgs(const llvm::opt::ArgList &DriverArgs,
38+
llvm::opt::ArgStringList &CC1Args) const;
39+
void print(llvm::raw_ostream &OS) const;
40+
41+
private:
42+
const Driver &D;
43+
llvm::SmallVector<llvm::SmallString<128>, 4> InstallationCandidates;
44+
};
45+
46+
} // namespace driver
47+
} // namespace clang
48+
49+
#endif // LLVM_CLANG_DRIVER_SYCLINSTALLATIONDETECTOR_H

clang/lib/Driver/ToolChain.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,9 +1692,8 @@ void ToolChain::addSYCLIncludeArgs(const ArgList &DriverArgs,
16921692
ArgStringList &CC1Args) const {}
16931693

16941694
llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
1695-
ToolChain::getDeviceLibs(
1696-
const ArgList &DriverArgs,
1697-
const Action::OffloadKind DeviceOffloadingKind) const {
1695+
ToolChain::getDeviceLibs(const ArgList &DriverArgs,
1696+
const Action::OffloadKind DeviceOffloadingKind) const {
16981697
return {};
16991698
}
17001699

0 commit comments

Comments
 (0)