Skip to content

Commit

Permalink
[CUDA] Reject values for --cuda-gpu-arch that are not of the form /sm…
Browse files Browse the repository at this point in the history
…_\d+/.

Reviewers: tra

Subscribers: cfe-commits, jhen, echristo

Differential Revision: http://reviews.llvm.org/D16079

llvm-svn: 257413
  • Loading branch information
Justin Lebar committed Jan 11, 2016
1 parent 36a425b commit 7bf7798
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
1 change: 1 addition & 0 deletions clang/include/clang/Basic/DiagnosticDriverKinds.td
Expand Up @@ -22,6 +22,7 @@ def err_drv_unknown_stdin_type_clang_cl : Error<
def err_drv_unknown_language : Error<"language not recognized: '%0'">;
def err_drv_invalid_arch_name : Error<
"invalid arch name '%0'">;
def err_drv_cuda_bad_gpu_arch : Error<"Unsupported CUDA gpu architecture: %0">;
def err_drv_invalid_thread_model_for_target : Error<
"invalid thread model '%0' in '%1' for this target">;
def err_drv_invalid_linker_name : Error<
Expand Down
7 changes: 6 additions & 1 deletion clang/include/clang/Driver/Action.h
Expand Up @@ -15,6 +15,9 @@
#include "llvm/ADT/SmallVector.h"

namespace llvm {

class StringRef;

namespace opt {
class Arg;
}
Expand Down Expand Up @@ -133,7 +136,7 @@ class BindArchAction : public Action {

class CudaDeviceAction : public Action {
virtual void anchor();
/// GPU architecture to bind -- e.g 'sm_35'.
/// GPU architecture to bind. Always of the form /sm_\d+/.
const char *GpuArchName;
/// True when action results are not consumed by the host action (e.g when
/// -fsyntax-only or --cuda-device-only options are used).
Expand All @@ -145,6 +148,8 @@ class CudaDeviceAction : public Action {
const char *getGpuArchName() const { return GpuArchName; }
bool isAtTopLevel() const { return AtTopLevel; }

static bool IsValidGpuArchName(llvm::StringRef ArchName);

static bool classof(const Action *A) {
return A->getKind() == CudaDeviceClass;
}
Expand Down
10 changes: 9 additions & 1 deletion clang/lib/Driver/Action.cpp
Expand Up @@ -9,6 +9,7 @@

#include "clang/Driver/Action.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Regex.h"
#include <cassert>
using namespace clang::driver;
using namespace llvm::opt;
Expand Down Expand Up @@ -54,7 +55,14 @@ void CudaDeviceAction::anchor() {}
CudaDeviceAction::CudaDeviceAction(Action *Input, const char *ArchName,
bool AtTopLevel)
: Action(CudaDeviceClass, Input), GpuArchName(ArchName),
AtTopLevel(AtTopLevel) {}
AtTopLevel(AtTopLevel) {
assert(IsValidGpuArchName(GpuArchName));
}

bool CudaDeviceAction::IsValidGpuArchName(llvm::StringRef ArchName) {
static llvm::Regex RE("^sm_[0-9]+$");
return RE.match(ArchName);
}

void CudaHostAction::anchor() {}

Expand Down
8 changes: 6 additions & 2 deletions clang/lib/Driver/Driver.cpp
Expand Up @@ -1297,8 +1297,12 @@ static Action *buildCudaActions(Compilation &C, DerivedArgList &Args,
if (!A->getOption().matches(options::OPT_cuda_gpu_arch_EQ))
continue;
A->claim();
if (GpuArchNames.insert(A->getValue()).second)
GpuArchList.push_back(A->getValue());

const auto& Arch = A->getValue();
if (!CudaDeviceAction::IsValidGpuArchName(Arch))
C.getDriver().Diag(clang::diag::err_drv_cuda_bad_gpu_arch) << Arch;
else if (GpuArchNames.insert(Arch).second)
GpuArchList.push_back(Arch);
}

// Default to sm_20 which is the lowest common denominator for supported GPUs.
Expand Down
34 changes: 34 additions & 0 deletions clang/test/Driver/cuda-bad-arch.cu
@@ -0,0 +1,34 @@
// Checks errors generated by passing a bad value for --cuda-gpu-arch.
// REQUIRES: clang-driver
// REQUIRES: x86-registered-target
// REQUIRES: nvptx-registered-target

// RUN: %clang -### -target x86_64-linux-gnu --cuda-gpu-arch=compute_20 -c %s 2>&1 \
// RUN: | FileCheck -check-prefix BAD %s
// RUN: %clang -### -target x86_64-linux-gnu --cuda-gpu-arch=foo_20 -c %s 2>&1 \
// RUN: | FileCheck -check-prefix BAD %s
// RUN: %clang -### -target x86_64-linux-gnu --cuda-gpu-arch=sm20 -c %s 2>&1 \
// RUN: | FileCheck -check-prefix BAD %s
// RUN: %clang -### -target x86_64-linux-gnu --cuda-gpu-arch=sm_abc -c %s 2>&1 \
// RUN: | FileCheck -check-prefix BAD %s
// RUN: %clang -### -target x86_64-linux-gnu --cuda-gpu-arch=sm_20a -c %s 2>&1 \
// RUN: | FileCheck -check-prefix BAD %s
// RUN: %clang -### -target x86_64-linux-gnu --cuda-gpu-arch=sm_a20 -c %s 2>&1 \
// RUN: | FileCheck -check-prefix BAD %s
// RUN: %clang -### -target x86_64-linux-gnu --cuda-gpu-arch=ssm_20 -c %s 2>&1 \
// RUN: | FileCheck -check-prefix BAD %s
// RUN: %clang -### -target x86_64-linux-gnu --cuda-gpu-arch=sm_ -c %s 2>&1 \
// RUN: | FileCheck -check-prefix BAD %s

// BAD: error: Unsupported CUDA gpu architecture

// RUN: %clang -### -target x86_64-linux-gnu --cuda-gpu-arch=sm_2 -c %s 2>&1 \
// RUN: | FileCheck -check-prefix OK %s
// RUN: %clang -### -target x86_64-linux-gnu --cuda-gpu-arch=sm_20 -c %s 2>&1 \
// RUN: | FileCheck -check-prefix OK %s
// RUN: %clang -### -target x86_64-linux-gnu --cuda-gpu-arch=sm_999 -c %s 2>&1 \
// RUN: | FileCheck -check-prefix OK %s
// RUN: %clang -### -target x86_64-linux-gnu -c %s 2>&1 \
// RUN: | FileCheck -check-prefix OK %s

// OK-NOT: error: Unsupported CUDA gpu architecture

0 comments on commit 7bf7798

Please sign in to comment.