Skip to content

Commit adeaea8

Browse files
[SYCL] Support const lambdas in detail::type_erased_cgfo_ty (#16819)
1 parent 0e5b5ee commit adeaea8

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

sycl/include/sycl/handler.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,12 @@ class type_erased_cgfo_ty {
169169
// by a queue. The function object can be a named type, lambda function or
170170
// std::function.
171171
template <typename T> struct invoker {
172-
static void call(void *object, handler &cgh) {
173-
(*static_cast<T *>(object))(cgh);
172+
static void call(const void *object, handler &cgh) {
173+
(*const_cast<T *>(static_cast<const T *>(object)))(cgh);
174174
}
175175
};
176-
void *object;
177-
using invoker_ty = void (*)(void *, handler &);
176+
const void *object;
177+
using invoker_ty = void (*)(const void *, handler &);
178178
const invoker_ty invoker_f;
179179

180180
public:
@@ -183,15 +183,15 @@ class type_erased_cgfo_ty {
183183
// NOTE: Even if `T` is a pointer to a function, `&f` is a pointer to a
184184
// pointer to a function and as such can be casted to `void *` (pointer to
185185
// a function cannot be casted).
186-
: object(static_cast<void *>(&f)), invoker_f(&invoker<T>::call) {}
186+
: object(static_cast<const void *>(&f)), invoker_f(&invoker<T>::call) {}
187187
~type_erased_cgfo_ty() = default;
188188

189189
type_erased_cgfo_ty(const type_erased_cgfo_ty &) = delete;
190190
type_erased_cgfo_ty(type_erased_cgfo_ty &&) = delete;
191191
type_erased_cgfo_ty &operator=(const type_erased_cgfo_ty &) = delete;
192192
type_erased_cgfo_ty &operator=(type_erased_cgfo_ty &&) = delete;
193193

194-
void operator()(sycl::handler &cgh) const { invoker_f(object, cgh); }
194+
void operator()(handler &cgh) const { invoker_f(object, cgh); }
195195
};
196196

197197
class kernel_bundle_impl;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %{build} -o %t.out
2+
// RUN: %{run} %t.out
3+
4+
#include <sycl/detail/core.hpp>
5+
6+
int main() {
7+
sycl::queue q;
8+
sycl::buffer<int, 1> b{1};
9+
sycl::host_accessor{b}[0] = 0;
10+
11+
auto l = [&](sycl::handler &cgh) {
12+
sycl::accessor acc{b, cgh};
13+
cgh.single_task([=]() { ++acc[0]; });
14+
};
15+
q.submit(l);
16+
assert(sycl::host_accessor{b}[0] == 1);
17+
18+
const auto cl = [&](sycl::handler &cgh) {
19+
sycl::accessor acc{b, cgh};
20+
cgh.single_task([=]() { ++acc[0]; });
21+
};
22+
q.submit(cl);
23+
assert(sycl::host_accessor{b}[0] == 2);
24+
25+
return 0;
26+
}

0 commit comments

Comments
 (0)