Skip to content

[SYCL] Add support for templated call operator in functors #7970

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 3 commits into from
Jan 13, 2023
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
32 changes: 23 additions & 9 deletions clang/lib/Sema/SemaSYCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -991,12 +991,26 @@ static QualType GetSYCLKernelObjectType(const FunctionDecl *KernelCaller) {
return KernelParamTy.getUnqualifiedType();
}

static CXXMethodDecl *getOperatorParens(const CXXRecordDecl *Rec) {
for (auto *MD : Rec->methods()) {
if (MD->getOverloadedOperator() == OO_Call)
return MD;
}
return nullptr;
// Get the call operator function associated with the function object
// for both templated and non-templated operator()().

static CXXMethodDecl *getFunctorCallOperator(const CXXRecordDecl *RD) {
DeclarationName Name =
RD->getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
DeclContext::lookup_result Calls = RD->lookup(Name);

if (Calls.empty())
return nullptr;

NamedDecl *CallOp = Calls.front();

if (CallOp == nullptr)
return nullptr;

if (const auto *CallOpTmpl = dyn_cast<FunctionTemplateDecl>(CallOp))
return cast<CXXMethodDecl>(CallOpTmpl->getTemplatedDecl());

return cast<CXXMethodDecl>(CallOp);
}

// Fetch the associated call operator of the kernel object
Expand All @@ -1009,7 +1023,7 @@ GetCallOperatorOfKernelObject(const CXXRecordDecl *KernelObjType) {
if (KernelObjType->isLambda())
CallOperator = KernelObjType->getLambdaCallOperator();
else
CallOperator = getOperatorParens(KernelObjType);
CallOperator = getFunctorCallOperator(KernelObjType);
return CallOperator;
}

Expand Down Expand Up @@ -2802,7 +2816,7 @@ class SyclOptReportCreator : public SyclKernelFieldHandler {
};

static bool isESIMDKernelType(const CXXRecordDecl *KernelObjType) {
const CXXMethodDecl *OpParens = getOperatorParens(KernelObjType);
const CXXMethodDecl *OpParens = getFunctorCallOperator(KernelObjType);
return (OpParens != nullptr) && OpParens->hasAttr<SYCLSimdAttr>();
}

Expand Down Expand Up @@ -4041,7 +4055,7 @@ void Sema::CheckSYCLKernelCall(FunctionDecl *KernelFunc,
// kernel to wrapped kernel.
void Sema::copySYCLKernelAttrs(const CXXRecordDecl *KernelObj) {
// Get the operator() function of the wrapper.
CXXMethodDecl *OpParens = getOperatorParens(KernelObj);
CXXMethodDecl *OpParens = getFunctorCallOperator(KernelObj);
assert(OpParens && "invalid kernel object");

typedef std::pair<FunctionDecl *, FunctionDecl *> ChildParentPair;
Expand Down
10 changes: 10 additions & 0 deletions clang/test/SemaSYCL/undefined-functor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ class FunctorWithCallOpDefined {
void operator()() const {}
};

class FunctorWithCallOpTemplated {
public:
template <int x = 0>
void operator()() const {}
};

int main() {

q.submit([&](sycl::handler &cgh) {
Expand All @@ -37,4 +43,8 @@ int main() {
cgh.single_task(FunctorWithCallOpDefined{});
});

q.submit([&](sycl::handler &cgh) {
cgh.single_task(FunctorWithCallOpTemplated{});
});

}