Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
63 changes: 32 additions & 31 deletions clang/lib/Sema/SemaSYCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,12 +503,16 @@ void Sema::checkSYCLDeviceVarDecl(VarDecl *Var) {
checkSYCLType(*this, Ty, Loc, Visited);
}

// Tests whether given function is a lambda function or '()' operator used as
// SYCL kernel body function (e.g. in parallel_for).
// Tests whether OperatorFD is a lambda function or '()' operator used as
// SYCL kernel body function (e.g. in parallel_for). If OperatorFD is a SYCL
// kernel body function, it's parent (or parent's parent for wrapped kernel),
Comment thread
elizabethandrews marked this conversation as resolved.
Outdated
// will be marked with SYCLKernelAttr.
// NOTE: This is incomplete implemenation. See TODO in the FE TODO list for the
// ESIMD extension.
static bool isSYCLKernelBodyFunction(FunctionDecl *FD) {
return FD->getOverloadedOperator() == OO_Call;
static bool isSYCLKernelBodyFunction(FunctionDecl *OperatorFD,
FunctionDecl *KernelFD) {
return OperatorFD->getOverloadedOperator() == OO_Call &&
KernelFD->hasAttr<SYCLKernelAttr>();
}

static bool isSYCLUndefinedAllowed(const FunctionDecl *Callee,
Expand Down Expand Up @@ -818,32 +822,29 @@ class SingleDeviceFunctionTracker {
DirectlyCalled);
}

// Calculate the kernel body. Note the 'isSYCLKernelBodyFunction' only
// tests that it is operator(), so hopefully this doesn't get us too many
// false-positives.
if (isSYCLKernelBodyFunction(CurrentDecl)) {
Comment thread
elizabethandrews marked this conversation as resolved.
// This is a direct callee of the kernel.
if (CallStack.size() == 1) {
assert(!KernelBody && "inconsistent call graph - only one kernel body "
"function can be called");
KernelBody = CurrentDecl;
} else if (CallStack.size() == 2 && KernelBody == CallStack.back()) {
// To implement rounding-up of a parallel-for range the
// SYCL header implementation modifies the kernel call like this:
// auto Wrapper = [=](TransformedArgType Arg) {
// if (Arg[0] >= NumWorkItems[0])
// return;
// Arg.set_allowed_range(NumWorkItems);
// KernelFunc(Arg);
// };
//
// This transformation leads to a condition where a kernel body
// function becomes callable from a new kernel body function.
// Hence this test.
// FIXME: We need to be more selective here, this can be hit by simply
// having a kernel lambda with a lambda call inside of it.
KernelBody = CurrentDecl;
}
// Calculate the kernel body. Since Kernel body is a direct callee of the
// kernel, or a wrapped inside a parallel-for, size of CallStack will be
// either 1 or 2.
if (CallStack.size() == 1 &&
isSYCLKernelBodyFunction(CurrentDecl, CallStack.front())) {
assert(!KernelBody && "inconsistent call graph - only one kernel body "
"function can be called");
KernelBody = CurrentDecl;
} else if (CallStack.size() == 2 &&
isSYCLKernelBodyFunction(CurrentDecl, CallStack.front())) {
// To implement rounding-up of a parallel-for range the
// SYCL header implementation modifies the kernel call like this:
// auto Wrapper = [=](TransformedArgType Arg) {
// if (Arg[0] >= NumWorkItems[0])
// return;
// Arg.set_allowed_range(NumWorkItems);
// KernelFunc(Arg);
// };
//
// This transformation leads to a condition where a kernel body
// function becomes callable from a new kernel body function.
// Hence this test.
KernelBody = CurrentDecl;
}

// Recurse.
Expand Down Expand Up @@ -3615,7 +3616,7 @@ void Sema::copySYCLKernelAttrs(const CXXRecordDecl *KernelObj) {
FunctionDecl *FD = WorkList.back().first;
FunctionDecl *ParentFD = WorkList.back().second;

if ((ParentFD == OpParens) && isSYCLKernelBodyFunction(FD)) {
if ((ParentFD == OpParens) && (FD->getOverloadedOperator() == OO_Call)) {
KernelBody = FD;
break;
}
Expand Down
21 changes: 21 additions & 0 deletions clang/test/SemaSYCL/non-kernel-functor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: %clang_cc1 -fsycl-is-device -triple spir64-unknown-unknown -verify %s
Comment thread
elizabethandrews marked this conversation as resolved.
Outdated

// Test to verify that non-kernel functors are not processed as SYCL kernel
// functors

// expected-no-diagnostics
Comment thread
Fznamznon marked this conversation as resolved.
class First {
public:
void operator()() { return; }
};

class Second {
public:
First operator()() { return First(); }
};

SYCL_EXTERNAL
void foo() {
Second NonKernelFunctorObj;
NonKernelFunctorObj()();
}