[PATCH] D16484: [CUDA] Disallow variadic functions other than printf in device code.

Justin Lebar via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 22 12:58:10 PST 2016


jlebar created this revision.
jlebar added a reviewer: tra.
jlebar added subscribers: jhen, echristo, cfe-commits.

http://reviews.llvm.org/D16484

Files:
  include/clang/Basic/DiagnosticSemaKinds.td
  lib/Sema/SemaDecl.cpp
  test/SemaCUDA/va-arg.cu
  test/SemaCUDA/vararg.cu

Index: test/SemaCUDA/vararg.cu
===================================================================
--- test/SemaCUDA/vararg.cu
+++ test/SemaCUDA/vararg.cu
@@ -2,7 +2,7 @@
 // REQUIRES: nvptx-registered-target
 // RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -fcuda-is-device -fsyntax-only \
 // RUN:   -verify -DEXPECT_ERR %s
-// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fsyntax-only %s
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fsyntax-only -verify %s
 
 #include <stdarg.h>
 #include "Inputs/cuda.h"
@@ -26,3 +26,12 @@
   va_arg(list, int);  // OK: only seen when compiling for host
 #endif
 }
+
+__device__ void vararg(const char* x, ...) {}
+// expected-error at -1 {{CUDA device code does not support variadic functions}}
+
+__device__ int printf(const char* fmt, ...);  // OK, special case.
+
+// Definition of printf not allowed.
+__device__ int printf(const char* fmt, ...) { return 0; }
+// expected-error at -1 {{CUDA device code does not support variadic functions}}
Index: lib/Sema/SemaDecl.cpp
===================================================================
--- lib/Sema/SemaDecl.cpp
+++ lib/Sema/SemaDecl.cpp
@@ -8276,18 +8276,25 @@
 
   MarkUnusedFileScopedDecl(NewFD);
 
-  if (getLangOpts().CUDA)
-    if (IdentifierInfo *II = NewFD->getIdentifier())
-      if (!NewFD->isInvalidDecl() &&
-          NewFD->getDeclContext()->getRedeclContext()->isTranslationUnit()) {
-        if (II->isStr("cudaConfigureCall")) {
-          if (!R->getAs<FunctionType>()->getReturnType()->isScalarType())
-            Diag(NewFD->getLocation(), diag::err_config_scalar_return);
+  if (getLangOpts().CUDA) {
+    IdentifierInfo *II = NewFD->getIdentifier();
+    if (II && II->isStr("cudaConfigureCall") && !NewFD->isInvalidDecl() &&
+        NewFD->getDeclContext()->getRedeclContext()->isTranslationUnit()) {
+      if (!R->getAs<FunctionType>()->getReturnType()->isScalarType())
+        Diag(NewFD->getLocation(), diag::err_config_scalar_return);
+
+      Context.setcudaConfigureCallDecl(NewFD);
+    }
+
+    // Variadic functions, other than a *declaration* of printf, are not allowed
+    // in device-side CUDA code.
+    if (NewFD->isVariadic() && (NewFD->hasAttr<CUDADeviceAttr>() ||
+                                NewFD->hasAttr<CUDAGlobalAttr>()) &&
+        !(II && II->isStr("printf") && !D.isFunctionDefinition())) {
+      Diag(NewFD->getLocation(), diag::err_variadic_device_fn);
+    }
+  }
 
-          Context.setcudaConfigureCallDecl(NewFD);
-        }
-      }
-  
   // Here we have an function template explicit specialization at class scope.
   // The actually specialization will be postponed to template instatiation
   // time via the ClassScopeFunctionSpecializationDecl node.
Index: include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- include/clang/Basic/DiagnosticSemaKinds.td
+++ include/clang/Basic/DiagnosticSemaKinds.td
@@ -6423,6 +6423,8 @@
 def warn_kern_is_inline : Warning<
   "ignored 'inline' attribute on kernel function %0">,
   InGroup<CudaCompat>;
+def err_variadic_device_fn : Error<
+  "CUDA device code does not support variadic functions">;
 def err_va_arg_in_device : Error<
   "CUDA device code does not support va_arg">;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D16484.45730.patch
Type: text/x-patch
Size: 3260 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160122/9ec38f7b/attachment.bin>


More information about the cfe-commits mailing list