From 24337db616668125b55781a82d7651800934ae4a Mon Sep 17 00:00:00 2001 From: Michael Liao Date: Wed, 25 Sep 2019 16:51:45 +0000 Subject: [PATCH] [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 llvm-svn: 372898 --- clang/lib/Sema/SemaDeclAttr.cpp | 4 ++- clang/lib/Sema/SemaExpr.cpp | 4 ++- clang/lib/Sema/SemaStmt.cpp | 8 ++++++ clang/lib/Sema/SemaTemplateDeduction.cpp | 7 +++++ clang/test/SemaCUDA/autoret-global.cu | 44 ++++++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 clang/test/SemaCUDA/autoret-global.cu diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp index 242b81a..b69cb5d 100644 --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -4223,7 +4223,9 @@ static void handleGlobalAttr(Sema &S, Decl *D, const ParsedAttr &AL) { return; } const auto *FD = cast(D); - if (!FD->getReturnType()->isVoidType()) { + if (!FD->getReturnType()->isVoidType() && + !FD->getReturnType()->getAs() && + !FD->getReturnType()->isInstantiationDependentType()) { SourceRange RTRange = FD->getReturnTypeSourceRange(); S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) << FD->getType() diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index a3808a9..4293ba1 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -5891,7 +5891,9 @@ ExprResult Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, << FDecl << Fn->getSourceRange()); // CUDA: Kernel function must have 'void' return type - if (!FuncT->getReturnType()->isVoidType()) + if (!FuncT->getReturnType()->isVoidType() && + !FuncT->getReturnType()->getAs() && + !FuncT->getReturnType()->isInstantiationDependentType()) return ExprError(Diag(LParenLoc, diag::err_kern_type_not_void_return) << Fn->getType() << Fn->getSourceRange()); } else { diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp index fdc85f2..bfdc550 100644 --- a/clang/lib/Sema/SemaStmt.cpp +++ b/clang/lib/Sema/SemaStmt.cpp @@ -3500,6 +3500,14 @@ bool Sema::DeduceFunctionTypeFromReturnExpr(FunctionDecl *FD, return true; } + // CUDA: Kernel function must have 'void' return type. + if (getLangOpts().CUDA) + if (FD->hasAttr() && !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, diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp b/clang/lib/Sema/SemaTemplateDeduction.cpp index db50830..8253858 100644 --- a/clang/lib/Sema/SemaTemplateDeduction.cpp +++ b/clang/lib/Sema/SemaTemplateDeduction.cpp @@ -3093,6 +3093,13 @@ Sema::SubstituteExplicitTemplateArguments( 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() && !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 diff --git a/clang/test/SemaCUDA/autoret-global.cu b/clang/test/SemaCUDA/autoret-global.cu new file mode 100644 index 0000000..ae2f1ca --- /dev/null +++ b/clang/test/SemaCUDA/autoret-global.cu @@ -0,0 +1,44 @@ +// RUN: %clang_cc1 -std=c++14 -fsyntax-only -verify %s + +#include "Inputs/cuda.h" + +template +__global__ T foo() { + // expected-note@-1 {{kernel function type 'T ()' must have void return type}} +} + +void f0() { + foo<<<0, 0>>>(); + foo<<<0, 0>>>(); + // expected-error@-1 {{no matching function for call to 'foo'}} +} + +__global__ auto f1() { +} + +__global__ auto f2(int x) { + return x + 1; + // expected-error@-2 {{kernel function type 'auto (int)' must have void return type}} +} + +template struct enable_if { typedef T type; }; +template struct enable_if {}; + +template +__global__ +auto bar() -> typename enable_if::type { + // expected-note@-1 {{requirement '3 == 1' was not satisfied [with N = 3]}} +} + +template +__global__ +auto bar() -> typename enable_if::type { + // expected-note@-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@-1 {{no matching function for call to 'bar'}} +} -- 2.7.4