Skip to content

Commit 5a795f6

Browse files
committed
[Clang][Driver] Installation detectors in user facing include dir (#151114)
This patch moves `LazyDetector` and target specific (Cuda, Hip, SYCL) installation detectors to clang's include directory. It was problematic for downstream to use headers from clang's lib dir. The use of lib headers could lead to subtle errors, as some of the symbols there are annotated with `LLVM_LIBRARY_VISIBILITY`. For instance [`ROCMToolChain::getCommonDeviceLibNames`](https://github.com/jchlanda/llvm-project/blob/jakub/installation_detectors/clang/lib/Driver/ToolChains/AMDGPU.h#L147) is c++ public, but because of the annotation it ends up as ELF hidden symbol, which causes errors when accessed from another shared library.
1 parent 766d10f commit 5a795f6

File tree

14 files changed

+155
-121
lines changed

14 files changed

+155
-121
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: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +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"
14-
#include "clang/Driver/CommonArgs.h"
1512
#include "clang/Driver/Driver.h"
16-
#include "clang/Driver/Options.h"
17-
#include "clang/Driver/SanitizerArgs.h"
18-
#include "llvm/ADT/SmallString.h"
19-
#include "llvm/ADT/StringMap.h"
20-
#include "llvm/Option/ArgList.h"
21-
#include "llvm/Support/VersionTuple.h"
22-
#include "llvm/TargetParser/TargetParser.h"
23-
#include "llvm/TargetParser/Triple.h"
2413

2514
namespace clang {
2615
namespace driver {
@@ -308,7 +297,7 @@ class RocmInstallationDetector {
308297
StringRef getHIPVersion() const { return DetectedVersion; }
309298
};
310299

311-
} // end namespace driver
312-
} // end namespace clang
300+
} // namespace driver
301+
} // namespace clang
313302

314-
#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/ToolChains/AMDGPU.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_AMDGPU_H
1111

1212
#include "Gnu.h"
13-
#include "ROCm.h"
1413
#include "clang/Basic/TargetID.h"
1514
#include "clang/Driver/Options.h"
1615
#include "clang/Driver/Tool.h"

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "Arch/SystemZ.h"
1717
#include "Hexagon.h"
1818
#include "PS4CPU.h"
19+
#include "ToolChains/Cuda.h"
1920
#include "clang/Basic/CLWarnings.h"
2021
#include "clang/Basic/CodeGenOptions.h"
2122
#include "clang/Basic/HeaderInclude.h"

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "Hexagon.h"
2424
#include "MSP430.h"
2525
#include "Solaris.h"
26+
#include "ToolChains/Cuda.h"
2627
#include "clang/Basic/CodeGenOptions.h"
2728
#include "clang/Config/config.h"
2829
#include "clang/Driver/Action.h"

clang/lib/Driver/ToolChains/Cuda.h

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "SYCL.h"
1313
#include "clang/Basic/Cuda.h"
1414
#include "clang/Driver/Action.h"
15+
#include "clang/Driver/CudaInstallationDetector.h"
1516
#include "clang/Driver/Multilib.h"
1617
#include "clang/Driver/Tool.h"
1718
#include "clang/Driver/ToolChain.h"
@@ -23,61 +24,6 @@
2324

2425
namespace clang {
2526
namespace driver {
26-
27-
/// A class to find a viable CUDA installation
28-
class CudaInstallationDetector {
29-
private:
30-
const Driver &D;
31-
bool IsValid = false;
32-
CudaVersion Version = CudaVersion::UNKNOWN;
33-
std::string InstallPath;
34-
std::string BinPath;
35-
std::string LibDevicePath;
36-
std::string IncludePath;
37-
llvm::StringMap<std::string> LibDeviceMap;
38-
39-
// CUDA architectures for which we have raised an error in
40-
// CheckCudaVersionSupportsArch.
41-
mutable std::bitset<(int)OffloadArch::LAST> ArchsWithBadVersion;
42-
43-
public:
44-
CudaInstallationDetector(const Driver &D, const llvm::Triple &HostTriple,
45-
const llvm::opt::ArgList &Args);
46-
47-
void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs,
48-
llvm::opt::ArgStringList &CC1Args) const;
49-
50-
/// Emit an error if Version does not support the given Arch.
51-
///
52-
/// If either Version or Arch is unknown, does not emit an error. Emits at
53-
/// most one error per Arch.
54-
void CheckCudaVersionSupportsArch(OffloadArch Arch) const;
55-
56-
/// Check whether we detected a valid Cuda install.
57-
bool isValid() const { return IsValid; }
58-
/// Print information about the detected CUDA installation.
59-
void print(raw_ostream &OS) const;
60-
61-
/// Get the detected Cuda install's version.
62-
CudaVersion version() const {
63-
return Version == CudaVersion::NEW ? CudaVersion::PARTIALLY_SUPPORTED
64-
: Version;
65-
}
66-
/// Get the detected Cuda installation path.
67-
StringRef getInstallPath() const { return InstallPath; }
68-
/// Get the detected path to Cuda's bin directory.
69-
StringRef getBinPath() const { return BinPath; }
70-
/// Get the detected Cuda Include path.
71-
StringRef getIncludePath() const { return IncludePath; }
72-
/// Get the detected Cuda device library path.
73-
StringRef getLibDevicePath() const { return LibDevicePath; }
74-
/// Get libdevice file for given architecture
75-
std::string getLibDeviceFile(StringRef Gpu) const {
76-
return LibDeviceMap.lookup(Gpu);
77-
}
78-
void WarnIfUnsupportedVersion() const;
79-
};
80-
8127
namespace tools {
8228
namespace NVPTX {
8329

clang/lib/Driver/ToolChains/Darwin.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
#ifndef LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_DARWIN_H
1010
#define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_DARWIN_H
1111

12-
#include "Cuda.h"
13-
#include "LazyDetector.h"
14-
#include "ROCm.h"
15-
#include "SYCL.h"
1612
#include "clang/Basic/DarwinSDKInfo.h"
1713
#include "clang/Basic/LangOptions.h"
14+
#include "clang/Driver/CudaInstallationDetector.h"
15+
#include "clang/Driver/LazyDetector.h"
16+
#include "clang/Driver/RocmInstallationDetector.h"
17+
#include "clang/Driver/SyclInstallationDetector.h"
1818
#include "clang/Driver/Tool.h"
1919
#include "clang/Driver/ToolChain.h"
2020
#include "clang/Driver/XRayArgs.h"

clang/lib/Driver/ToolChains/Gnu.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
#ifndef LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_GNU_H
1010
#define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_GNU_H
1111

12-
#include "Cuda.h"
13-
#include "LazyDetector.h"
14-
#include "ROCm.h"
15-
#include "SYCL.h"
12+
#include "clang/Driver/CudaInstallationDetector.h"
13+
#include "clang/Driver/LazyDetector.h"
14+
#include "clang/Driver/RocmInstallationDetector.h"
15+
#include "clang/Driver/SyclInstallationDetector.h"
1616
#include "clang/Driver/Tool.h"
1717
#include "clang/Driver/ToolChain.h"
1818
#include <set>

0 commit comments

Comments
 (0)