diff --git a/sycl/include/sycl/handler.hpp b/sycl/include/sycl/handler.hpp index b815869f4d707..552c6afe195be 100644 --- a/sycl/include/sycl/handler.hpp +++ b/sycl/include/sycl/handler.hpp @@ -169,12 +169,12 @@ class type_erased_cgfo_ty { // by a queue. The function object can be a named type, lambda function or // std::function. template struct invoker { - static void call(void *object, handler &cgh) { - (*static_cast(object))(cgh); + static void call(const void *object, handler &cgh) { + (*const_cast(static_cast(object)))(cgh); } }; - void *object; - using invoker_ty = void (*)(void *, handler &); + const void *object; + using invoker_ty = void (*)(const void *, handler &); const invoker_ty invoker_f; public: @@ -183,7 +183,7 @@ class type_erased_cgfo_ty { // NOTE: Even if `T` is a pointer to a function, `&f` is a pointer to a // pointer to a function and as such can be casted to `void *` (pointer to // a function cannot be casted). - : object(static_cast(&f)), invoker_f(&invoker::call) {} + : object(static_cast(&f)), invoker_f(&invoker::call) {} ~type_erased_cgfo_ty() = default; type_erased_cgfo_ty(const type_erased_cgfo_ty &) = delete; @@ -191,7 +191,7 @@ class type_erased_cgfo_ty { type_erased_cgfo_ty &operator=(const type_erased_cgfo_ty &) = delete; type_erased_cgfo_ty &operator=(type_erased_cgfo_ty &&) = delete; - void operator()(sycl::handler &cgh) const { invoker_f(object, cgh); } + void operator()(handler &cgh) const { invoker_f(object, cgh); } }; class kernel_bundle_impl; diff --git a/sycl/test-e2e/Basic/cgfo_const_lambda.cpp b/sycl/test-e2e/Basic/cgfo_const_lambda.cpp new file mode 100644 index 0000000000000..30f77aaf808ab --- /dev/null +++ b/sycl/test-e2e/Basic/cgfo_const_lambda.cpp @@ -0,0 +1,26 @@ +// RUN: %{build} -o %t.out +// RUN: %{run} %t.out + +#include + +int main() { + sycl::queue q; + sycl::buffer b{1}; + sycl::host_accessor{b}[0] = 0; + + auto l = [&](sycl::handler &cgh) { + sycl::accessor acc{b, cgh}; + cgh.single_task([=]() { ++acc[0]; }); + }; + q.submit(l); + assert(sycl::host_accessor{b}[0] == 1); + + const auto cl = [&](sycl::handler &cgh) { + sycl::accessor acc{b, cgh}; + cgh.single_task([=]() { ++acc[0]; }); + }; + q.submit(cl); + assert(sycl::host_accessor{b}[0] == 2); + + return 0; +}