From 9a2c0fbaf56af2d03933affa6f5a99c215911231 Mon Sep 17 00:00:00 2001 From: Justin Lebar Date: Thu, 11 Feb 2016 02:00:52 +0000 Subject: [PATCH] [CUDA] Don't crash when trying to printf a non-scalar object. Summary: We can't do the right thing, since there's no right thing to do, but at least we can not crash the compiler. Reviewers: majnemer, rnk Subscribers: cfe-commits, jhen, tra Differential Revision: http://reviews.llvm.org/D17103 llvm-svn: 260479 --- clang/lib/CodeGen/CGCUDABuiltin.cpp | 7 +++++++ clang/test/CodeGenCUDA/printf-aggregate.cu | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 clang/test/CodeGenCUDA/printf-aggregate.cu diff --git a/clang/lib/CodeGen/CGCUDABuiltin.cpp b/clang/lib/CodeGen/CGCUDABuiltin.cpp index 0ccba89..ea3b888 100644 --- a/clang/lib/CodeGen/CGCUDABuiltin.cpp +++ b/clang/lib/CodeGen/CGCUDABuiltin.cpp @@ -83,6 +83,13 @@ CodeGenFunction::EmitCUDADevicePrintfCallExpr(const CallExpr *E, E->arguments(), E->getDirectCallee(), /* ParamsToSkip = */ 0); + // We don't know how to emit non-scalar varargs. + if (std::any_of(Args.begin() + 1, Args.end(), + [](const CallArg &A) { return !A.RV.isScalar(); })) { + CGM.ErrorUnsupported(E, "non-scalar arg to printf"); + return RValue::get(llvm::ConstantInt::get(IntTy, 0)); + } + // Construct and fill the args buffer that we'll pass to vprintf. llvm::Value *BufferPtr; if (Args.size() <= 1) { diff --git a/clang/test/CodeGenCUDA/printf-aggregate.cu b/clang/test/CodeGenCUDA/printf-aggregate.cu new file mode 100644 index 0000000..2e703b8 --- /dev/null +++ b/clang/test/CodeGenCUDA/printf-aggregate.cu @@ -0,0 +1,17 @@ +// REQUIRES: x86-registered-target +// REQUIRES: nvptx-registered-target + +// RUN: not %clang_cc1 -triple nvptx64-nvidia-cuda -fcuda-is-device -emit-llvm \ +// RUN: -o - %s 2>&1 | FileCheck %s + +#include "Inputs/cuda.h" + +// Check that we don't crash when asked to printf a non-scalar arg. +struct Struct { + int x; + int y; +}; +__device__ void PrintfNonScalar() { + // CHECK: cannot compile this non-scalar arg to printf + printf("%d", Struct()); +} -- 2.7.4