Skip to content

Commit eb2ba2e

Browse files
committed
[CUDA] Warn about unsupported CUDA SDK version only if it's used.
This fixes an issue with clang issuing a warning about unknown CUDA SDK if it's detected during non-CUDA compilation. Differential Revision: https://reviews.llvm.org/D76030
1 parent f09c7d6 commit eb2ba2e

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

clang/lib/Driver/ToolChains/Cuda.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,28 @@ using namespace llvm::opt;
3434

3535
// Parses the contents of version.txt in an CUDA installation. It should
3636
// contain one line of the from e.g. "CUDA Version 7.5.2".
37-
static CudaVersion ParseCudaVersionFile(const Driver &D, llvm::StringRef V) {
37+
void CudaInstallationDetector::ParseCudaVersionFile(llvm::StringRef V) {
38+
Version = CudaVersion::UNKNOWN;
3839
if (!V.startswith("CUDA Version "))
39-
return CudaVersion::UNKNOWN;
40+
return;
4041
V = V.substr(strlen("CUDA Version "));
4142
SmallVector<StringRef,4> VersionParts;
4243
V.split(VersionParts, '.');
4344
if (VersionParts.size() < 2)
44-
return CudaVersion::UNKNOWN;
45-
std::string MajorMinor = join_items(".", VersionParts[0], VersionParts[1]);
46-
CudaVersion Version = CudaStringToVersion(MajorMinor);
45+
return;
46+
DetectedVersion = join_items(".", VersionParts[0], VersionParts[1]);
47+
Version = CudaStringToVersion(DetectedVersion);
4748
if (Version != CudaVersion::UNKNOWN)
48-
return Version;
49+
return;
4950

50-
// Issue a warning and assume that the version we've found is compatible with
51-
// the latest version we support.
52-
D.Diag(diag::warn_drv_unknown_cuda_version)
53-
<< MajorMinor << CudaVersionToString(CudaVersion::LATEST);
54-
return CudaVersion::LATEST;
51+
Version = CudaVersion::LATEST;
52+
DetectedVersionIsNotSupported = true;
53+
}
54+
55+
void CudaInstallationDetector::WarnIfUnsupportedVersion() {
56+
if (DetectedVersionIsNotSupported)
57+
D.Diag(diag::warn_drv_unknown_cuda_version)
58+
<< DetectedVersion << CudaVersionToString(Version);
5559
}
5660

5761
CudaInstallationDetector::CudaInstallationDetector(
@@ -150,7 +154,7 @@ CudaInstallationDetector::CudaInstallationDetector(
150154
// version.txt isn't present.
151155
Version = CudaVersion::CUDA_70;
152156
} else {
153-
Version = ParseCudaVersionFile(D, (*VersionFile)->getBuffer());
157+
ParseCudaVersionFile((*VersionFile)->getBuffer());
154158
}
155159

156160
if (Version >= CudaVersion::CUDA_90) {
@@ -568,8 +572,10 @@ CudaToolChain::CudaToolChain(const Driver &D, const llvm::Triple &Triple,
568572
const Action::OffloadKind OK)
569573
: ToolChain(D, Triple, Args), HostTC(HostTC),
570574
CudaInstallation(D, HostTC.getTriple(), Args), OK(OK) {
571-
if (CudaInstallation.isValid())
575+
if (CudaInstallation.isValid()) {
576+
CudaInstallation.WarnIfUnsupportedVersion();
572577
getProgramPaths().push_back(std::string(CudaInstallation.getBinPath()));
578+
}
573579
// Lookup binaries into the driver directory, this is used to
574580
// discover the clang-offload-bundler executable.
575581
getProgramPaths().push_back(getDriver().Dir);

clang/lib/Driver/ToolChains/Cuda.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class CudaInstallationDetector {
3030
const Driver &D;
3131
bool IsValid = false;
3232
CudaVersion Version = CudaVersion::UNKNOWN;
33+
std::string DetectedVersion;
34+
bool DetectedVersionIsNotSupported = false;
3335
std::string InstallPath;
3436
std::string BinPath;
3537
std::string LibPath;
@@ -75,6 +77,10 @@ class CudaInstallationDetector {
7577
std::string getLibDeviceFile(StringRef Gpu) const {
7678
return LibDeviceMap.lookup(Gpu);
7779
}
80+
void WarnIfUnsupportedVersion();
81+
82+
private:
83+
void ParseCudaVersionFile(llvm::StringRef V);
7884
};
7985

8086
namespace tools {

clang/test/Driver/cuda-version-check.cu

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
// RUN: FileCheck %s --check-prefix=OK
1111
// RUN: %clang --target=x86_64-linux -v -### --cuda-gpu-arch=sm_60 --cuda-path=%S/Inputs/CUDA-unknown/usr/local/cuda 2>&1 %s | \
1212
// RUN: FileCheck %s --check-prefix=UNKNOWN_VERSION
13+
// Make sure that we don't warn about CUDA version during C++ compilation.
14+
// RUN: %clang --target=x86_64-linux -v -### -x c++ --cuda-gpu-arch=sm_60 \
15+
// RUN: --cuda-path=%S/Inputs/CUDA-unknown/usr/local/cuda 2>&1 %s | \
16+
// RUN: FileCheck %s --check-prefix=UNKNOWN_VERSION_CXX
1317

1418
// The installation at Inputs/CUDA is CUDA 7.0, which doesn't support sm_60.
1519
// RUN: %clang --target=x86_64-linux -v -### --cuda-gpu-arch=sm_60 --cuda-path=%S/Inputs/CUDA/usr/local/cuda 2>&1 %s | \
@@ -62,3 +66,4 @@
6266
// ERR_SM61-NOT: error: GPU arch sm_61
6367

6468
// UNKNOWN_VERSION: Unknown CUDA version 999.999. Assuming the latest supported version
69+
// UNKNOWN_VERSION_CXX-NOT: Unknown CUDA version

0 commit comments

Comments
 (0)