Skip to content

[SYCL] Allow calls through constant expr function pointers #5390

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 7 commits into from
Jan 28, 2022
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
13 changes: 10 additions & 3 deletions clang/lib/Sema/SemaSYCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,9 +636,16 @@ class DiagDeviceFunction : public RecursiveASTVisitor<DiagDeviceFunction> {
}
} else if (!SemaRef.getLangOpts().SYCLAllowFuncPtr &&
!e->isTypeDependent() &&
!isa<CXXPseudoDestructorExpr>(e->getCallee()))
SemaRef.Diag(e->getExprLoc(), diag::err_sycl_restrict)
<< Sema::KernelCallFunctionPointer;
!isa<CXXPseudoDestructorExpr>(e->getCallee())) {
bool MaybeConstantExpr = false;
Expr *NonDirectCallee = e->getCallee();
if (!NonDirectCallee->isValueDependent())
MaybeConstantExpr =
NonDirectCallee->isCXX11ConstantExpr(SemaRef.getASTContext());
if (!MaybeConstantExpr)
SemaRef.Diag(e->getExprLoc(), diag::err_sycl_restrict)
<< Sema::KernelCallFunctionPointer;
}
return true;
}

Expand Down
51 changes: 51 additions & 0 deletions clang/test/SemaSYCL/constexpr-function-pointer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// RUN: %clang_cc1 -fsycl-is-device -fsyntax-only -verify -sycl-std=2020 -std=c++17 %s

// This test checks that the compiler doesn't emit an error when indirect call
// was made through a function pointer that is constant expression, and makes
// sure that the error is emitted when a function pointer is not a constant
// expression.

void t() {}

constexpr auto F = t;
const auto F1 = t;

typedef void (*SomeFunc)();

constexpr SomeFunc foo() { return t; }

const SomeFunc foo1() { return t; }

void bar1(const SomeFunc fptr) {
fptr();
}

template <auto f> void fooNTTP() { f(); }

__attribute__((sycl_device)) void bar() {
// OK
constexpr auto f = t;
f();
const auto f1 = t;
// expected-error@+1 {{SYCL kernel cannot call through a function pointer}}
f1();
auto f2 = t;
// expected-error@+1 {{SYCL kernel cannot call through a function pointer}}
f2();

// OK
F();
// expected-error@+1 {{SYCL kernel cannot call through a function pointer}}
F1();

constexpr auto ff = foo();
ff();
const auto ff1 = foo();
// expected-error@+1 {{SYCL kernel cannot call through a function pointer}}
ff1();
const auto fff = foo1();
// expected-error@+1 {{SYCL kernel cannot call through a function pointer}}
fff();

fooNTTP<t>();
}