r372898 - [CUDA][HIP] Enable kernel function return type deduction.

Michael Liao via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 25 09:51:45 PDT 2019


Author: hliao
Date: Wed Sep 25 09:51:45 2019
New Revision: 372898

URL: http://llvm.org/viewvc/llvm-project?rev=372898&view=rev
Log:
[CUDA][HIP] Enable kernel function return type deduction.

Summary:
- Even though only `void` is still accepted as the deduced return type,
  enabling deduction/instantiation on the return type allows more
  consistent coding.

Reviewers: tra, jlebar

Subscribers: cfe-commits, yaxunl

Tags: #clang

Differential Revision: https://reviews.llvm.org/D68031

Added:
    cfe/trunk/test/SemaCUDA/autoret-global.cu
Modified:
    cfe/trunk/lib/Sema/SemaDeclAttr.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/lib/Sema/SemaStmt.cpp
    cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp

Modified: cfe/trunk/lib/Sema/SemaDeclAttr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=372898&r1=372897&r2=372898&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Wed Sep 25 09:51:45 2019
@@ -4223,7 +4223,9 @@ static void handleGlobalAttr(Sema &S, De
     return;
   }
   const auto *FD = cast<FunctionDecl>(D);
-  if (!FD->getReturnType()->isVoidType()) {
+  if (!FD->getReturnType()->isVoidType() &&
+      !FD->getReturnType()->getAs<AutoType>() &&
+      !FD->getReturnType()->isInstantiationDependentType()) {
     SourceRange RTRange = FD->getReturnTypeSourceRange();
     S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
         << FD->getType()

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=372898&r1=372897&r2=372898&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Wed Sep 25 09:51:45 2019
@@ -5891,7 +5891,9 @@ ExprResult Sema::BuildResolvedCallExpr(E
             << FDecl << Fn->getSourceRange());
 
       // CUDA: Kernel function must have 'void' return type
-      if (!FuncT->getReturnType()->isVoidType())
+      if (!FuncT->getReturnType()->isVoidType() &&
+          !FuncT->getReturnType()->getAs<AutoType>() &&
+          !FuncT->getReturnType()->isInstantiationDependentType())
         return ExprError(Diag(LParenLoc, diag::err_kern_type_not_void_return)
             << Fn->getType() << Fn->getSourceRange());
     } else {

Modified: cfe/trunk/lib/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=372898&r1=372897&r2=372898&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmt.cpp Wed Sep 25 09:51:45 2019
@@ -3500,6 +3500,14 @@ bool Sema::DeduceFunctionTypeFromReturnE
       return true;
   }
 
+  // CUDA: Kernel function must have 'void' return type.
+  if (getLangOpts().CUDA)
+    if (FD->hasAttr<CUDAGlobalAttr>() && !Deduced->isVoidType()) {
+      Diag(FD->getLocation(), diag::err_kern_type_not_void_return)
+          << FD->getType() << FD->getSourceRange();
+      return true;
+    }
+
   //  If a function with a declared return type that contains a placeholder type
   //  has multiple return statements, the return type is deduced for each return
   //  statement. [...] if the type deduced is not the same in each deduction,

Modified: cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp?rev=372898&r1=372897&r2=372898&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp Wed Sep 25 09:51:45 2019
@@ -3093,6 +3093,13 @@ Sema::SubstituteExplicitTemplateArgument
                   Function->getTypeSpecStartLoc(), Function->getDeclName());
     if (ResultType.isNull() || Trap.hasErrorOccurred())
       return TDK_SubstitutionFailure;
+    // CUDA: Kernel function must have 'void' return type.
+    if (getLangOpts().CUDA)
+      if (Function->hasAttr<CUDAGlobalAttr>() && !ResultType->isVoidType()) {
+        Diag(Function->getLocation(), diag::err_kern_type_not_void_return)
+            << Function->getType() << Function->getSourceRange();
+        return TDK_SubstitutionFailure;
+      }
   }
 
   // Instantiate the types of each of the function parameters given the

Added: cfe/trunk/test/SemaCUDA/autoret-global.cu
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCUDA/autoret-global.cu?rev=372898&view=auto
==============================================================================
--- cfe/trunk/test/SemaCUDA/autoret-global.cu (added)
+++ cfe/trunk/test/SemaCUDA/autoret-global.cu Wed Sep 25 09:51:45 2019
@@ -0,0 +1,44 @@
+// RUN: %clang_cc1 -std=c++14 -fsyntax-only -verify %s
+
+#include "Inputs/cuda.h"
+
+template <typename T>
+__global__ T foo() {
+  // expected-note at -1 {{kernel function type 'T ()' must have void return type}}
+}
+
+void f0() {
+  foo<void><<<0, 0>>>();
+  foo<int><<<0, 0>>>();
+  // expected-error at -1 {{no matching function for call to 'foo'}}
+}
+
+__global__ auto f1() {
+}
+
+__global__ auto f2(int x) {
+  return x + 1;
+  // expected-error at -2 {{kernel function type 'auto (int)' must have void return type}}
+}
+
+template <bool Cond, typename T = void> struct enable_if { typedef T type; };
+template <typename T> struct enable_if<false, T> {};
+
+template <int N>
+__global__
+auto bar() -> typename enable_if<N == 1>::type {
+  // expected-note at -1 {{requirement '3 == 1' was not satisfied [with N = 3]}}
+}
+
+template <int N>
+__global__
+auto bar() -> typename enable_if<N == 2>::type {
+  // expected-note at -1 {{requirement '3 == 2' was not satisfied [with N = 3]}}
+}
+
+void f3() {
+  bar<1><<<0, 0>>>();
+  bar<2><<<0, 0>>>();
+  bar<3><<<0, 0>>>();
+  // expected-error at -1 {{no matching function for call to 'bar'}}
+}




More information about the cfe-commits mailing list