Skip to content

[SYCL]Move kernel diagnostics to the call, enable template trail on errors #2289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -10937,6 +10937,8 @@ def err_sycl_kernel_incorrectly_named : Error<
"|needs to have a globally-visible name"
"|name is invalid. Unscoped enum requires fixed underlying type"
"}0">;
def err_sycl_kernel_not_function_object
: Error<"kernel parameter must be a lambda or function object">;
def err_sycl_restrict : Error<
"SYCL kernel cannot "
"%select{use a non-const global variable"
Expand Down
5 changes: 4 additions & 1 deletion clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -12306,6 +12306,9 @@ class Sema final {
bool IsMemberFunction, SourceLocation Loc, SourceRange Range,
VariadicCallType CallType);

void CheckSYCLKernelCall(FunctionDecl *CallerFunc, SourceRange CallLoc,
ArrayRef<const Expr *> Args);

bool CheckObjCString(Expr *Arg);
ExprResult CheckOSLogFormatStringArg(Expr *Arg);

Expand Down Expand Up @@ -12727,7 +12730,7 @@ class Sema final {
// Used to suppress diagnostics during kernel construction, since these were
// already emitted earlier. Diagnosing during Kernel emissions also skips the
// useful notes that shows where the kernel was called.
bool ConstructingOpenCLKernel = false;
bool DiagnosingSYCLKernel = false;

public:
void addSyclDeviceDecl(Decl *d) { SyclDeviceDecls.insert(d); }
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Sema/SemaChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4459,6 +4459,9 @@ void Sema::checkCall(NamedDecl *FDecl, const FunctionProtoType *Proto,
if (FD)
diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);

if (FD && FD->hasAttr<SYCLKernelAttr>())
CheckSYCLKernelCall(FD, Range, Args);

// Diagnose variadic calls in SYCL.
if (FD && FD ->isVariadic() && getLangOpts().SYCLIsDevice &&
!isUnevaluatedContext() && !isKnownGoodSYCLDecl(FD))
Expand Down
66 changes: 42 additions & 24 deletions clang/lib/Sema/SemaSYCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,8 @@ getKernelInvocationKind(FunctionDecl *KernelCallerFunc) {
}

static const CXXRecordDecl *getKernelObjectType(FunctionDecl *Caller) {
return (*Caller->param_begin())->getType()->getAsCXXRecordDecl();
assert(Caller->getNumParams() > 0 && "Insufficient kernel parameters");
return Caller->getParamDecl(0)->getType()->getAsCXXRecordDecl();
}

/// Creates a kernel parameter descriptor
Expand Down Expand Up @@ -1205,7 +1206,6 @@ class SyclKernelFieldChecker : public SyclKernelFieldHandler {
class SyclKernelDeclCreator : public SyclKernelFieldHandler {
FunctionDecl *KernelDecl;
llvm::SmallVector<ParmVarDecl *, 8> Params;
SyclKernelFieldChecker &ArgChecker;
Sema::ContextRAII FuncContext;
// Holds the last handled field's first parameter. This doesn't store an
// iterator as push_back invalidates iterators.
Expand Down Expand Up @@ -1339,13 +1339,12 @@ class SyclKernelDeclCreator : public SyclKernelFieldHandler {
}

public:
SyclKernelDeclCreator(Sema &S, SyclKernelFieldChecker &ArgChecker,
StringRef Name, SourceLocation Loc, bool IsInline,
bool IsSIMDKernel)
SyclKernelDeclCreator(Sema &S, StringRef Name, SourceLocation Loc,
bool IsInline, bool IsSIMDKernel)
: SyclKernelFieldHandler(S),
KernelDecl(createKernelDecl(S.getASTContext(), Name, Loc, IsInline,
IsSIMDKernel)),
ArgChecker(ArgChecker), FuncContext(SemaRef, KernelDecl) {}
FuncContext(SemaRef, KernelDecl) {}

~SyclKernelDeclCreator() {
ASTContext &Ctx = SemaRef.getASTContext();
Expand All @@ -1360,8 +1359,7 @@ class SyclKernelDeclCreator : public SyclKernelFieldHandler {
KernelDecl->setType(FuncType);
KernelDecl->setParams(Params);

if (ArgChecker.isValid())
SemaRef.addSyclDeviceDecl(KernelDecl);
SemaRef.addSyclDeviceDecl(KernelDecl);
}

bool handleSyclAccessorType(const CXXBaseSpecifier &BS,
Expand Down Expand Up @@ -2015,8 +2013,38 @@ class SyclKernelIntHeaderCreator : public SyclKernelFieldHandler {
using SyclKernelFieldHandler::handleSyclHalfType;
using SyclKernelFieldHandler::handleSyclSamplerType;
};

} // namespace

void Sema::CheckSYCLKernelCall(FunctionDecl *KernelFunc, SourceRange CallLoc,
ArrayRef<const Expr *> Args) {
const CXXRecordDecl *KernelObj = getKernelObjectType(KernelFunc);
if (!KernelObj) {
Diag(Args[0]->getExprLoc(), diag::err_sycl_kernel_not_function_object);
KernelFunc->setInvalidDecl();
return;
}

if (KernelObj->isLambda()) {
for (const LambdaCapture &LC : KernelObj->captures())
if (LC.capturesThis() && LC.isImplicit()) {
Diag(LC.getLocation(), diag::err_implicit_this_capture);
Diag(CallLoc.getBegin(), diag::note_used_here);
KernelFunc->setInvalidDecl();
}
}

SyclKernelFieldChecker Checker(*this);

KernelObjVisitor Visitor{*this};
DiagnosingSYCLKernel = true;
Visitor.VisitRecordBases(KernelObj, Checker);
Visitor.VisitRecordFields(KernelObj, Checker);
DiagnosingSYCLKernel = false;
if (!Checker.isValid())
KernelFunc->setInvalidDecl();
}

// Generates the OpenCL kernel using KernelCallerFunc (kernel caller
// function) defined is SYCL headers.
// Generated OpenCL kernel contains the body of the kernel caller function,
Expand Down Expand Up @@ -2051,29 +2079,19 @@ void Sema::ConstructOpenCLKernel(FunctionDecl *KernelCallerFunc,
constructKernelName(*this, KernelCallerFunc, MC);
StringRef KernelName(getLangOpts().SYCLUnnamedLambda ? StableName
: CalculatedName);
if (KernelObj->isLambda()) {
for (const LambdaCapture &LC : KernelObj->captures())
if (LC.capturesThis() && LC.isImplicit())
Diag(LC.getLocation(), diag::err_implicit_this_capture);
}
SyclKernelFieldChecker checker(*this);
SyclKernelDeclCreator kernel_decl(
*this, checker, KernelName, KernelObj->getLocation(),
KernelCallerFunc->isInlined(), KernelCallerFunc->hasAttr<SYCLSimdAttr>());
SyclKernelDeclCreator kernel_decl(*this, KernelName, KernelObj->getLocation(),
KernelCallerFunc->isInlined(),
KernelCallerFunc->hasAttr<SYCLSimdAttr>());
SyclKernelBodyCreator kernel_body(*this, kernel_decl, KernelObj,
KernelCallerFunc);
SyclKernelIntHeaderCreator int_header(
*this, getSyclIntegrationHeader(), KernelObj,
calculateKernelNameType(Context, KernelCallerFunc), KernelName,
StableName);

ConstructingOpenCLKernel = true;
KernelObjVisitor Visitor{*this};
Visitor.VisitRecordBases(KernelObj, checker, kernel_decl, kernel_body,
int_header);
Visitor.VisitRecordFields(KernelObj, checker, kernel_decl, kernel_body,
int_header);
ConstructingOpenCLKernel = false;
Visitor.VisitRecordBases(KernelObj, kernel_decl, kernel_body, int_header);
Visitor.VisitRecordFields(KernelObj, kernel_decl, kernel_body, int_header);
}

// This function marks all the callees of explicit SIMD kernel
Expand Down Expand Up @@ -2242,7 +2260,7 @@ Sema::DeviceDiagBuilder Sema::SYCLDiagIfDeviceCode(SourceLocation Loc,
"Should only be called during SYCL compilation");
FunctionDecl *FD = dyn_cast<FunctionDecl>(getCurLexicalContext());
DeviceDiagBuilder::Kind DiagKind = [this, FD] {
if (ConstructingOpenCLKernel)
if (DiagnosingSYCLKernel)
return DeviceDiagBuilder::K_ImmediateWithCallStack;
if (!FD)
return DeviceDiagBuilder::K_Nop;
Expand Down
25 changes: 25 additions & 0 deletions clang/test/SemaSYCL/kernel-not-functor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// RUN: %clang_cc1 -fsycl -fsycl-is-device -fsyntax-only -verify %s

template <typename Name, typename F>
__attribute__((sycl_kernel)) void kernel(F kernelFunc) {
kernelFunc();
}

template <typename Name, typename F>
void uses_kernel(F kernelFunc) {
// expected-error@+1{{kernel parameter must be a lambda or function object}}
kernel<Name>(kernelFunc);
}

void func() {}

template <typename Name>
void kernel_wrapper() {
// expected-note@+1{{in instantiation of function template specialization}}
uses_kernel<Name>(func);
}

void use() {
// expected-note@+1{{in instantiation of function template specialization}}
kernel_wrapper<class Foo>();
}
1 change: 1 addition & 0 deletions clang/test/SemaSYCL/lambda_implicit_capture_this.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Class {
};

void Class::function() {
// expected-note@+1{{used here}}
kernel<class kernel_wrapper>(
[=]() {
int acc[1] = {5};
Expand Down