Skip to content

Commit d32170d

Browse files
Artem-Bzmodem
authored andcommitted
[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 (cherry picked from commit eb2ba2e)
1 parent 2476548 commit d32170d

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
@@ -32,24 +32,28 @@ using namespace llvm::opt;
3232

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

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

5559
CudaInstallationDetector::CudaInstallationDetector(
@@ -147,7 +151,7 @@ CudaInstallationDetector::CudaInstallationDetector(
147151
// version.txt isn't present.
148152
Version = CudaVersion::CUDA_70;
149153
} else {
150-
Version = ParseCudaVersionFile(D, (*VersionFile)->getBuffer());
154+
ParseCudaVersionFile((*VersionFile)->getBuffer());
151155
}
152156

153157
if (Version >= CudaVersion::CUDA_90) {
@@ -565,8 +569,10 @@ CudaToolChain::CudaToolChain(const Driver &D, const llvm::Triple &Triple,
565569
const Action::OffloadKind OK)
566570
: ToolChain(D, Triple, Args), HostTC(HostTC),
567571
CudaInstallation(D, HostTC.getTriple(), Args), OK(OK) {
568-
if (CudaInstallation.isValid())
572+
if (CudaInstallation.isValid()) {
573+
CudaInstallation.WarnIfUnsupportedVersion();
569574
getProgramPaths().push_back(CudaInstallation.getBinPath());
575+
}
570576
// Lookup binaries into the driver directory, this is used to
571577
// discover the clang-offload-bundler executable.
572578
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)