Skip to content

Commit d404d1c

Browse files
alexeyvoronov-intelromanovvlad
authored andcommitted
[SYCL] Fix handler set_arg(s) method.
Fixed incorrect storing and extractiong of global/constant accessor in set_arg(s) logic. Signed-off-by: Alexey Voronov <[email protected]>
1 parent eacc876 commit d404d1c

File tree

2 files changed

+95
-5
lines changed

2 files changed

+95
-5
lines changed

sycl/include/CL/sycl/handler2.hpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,18 @@ class handler {
217217
void *Ptr = LambdaPtr + KernelArgs[I].offset;
218218
const detail::kernel_param_kind_t &Kind = KernelArgs[I].kind;
219219
const int &Size = KernelArgs[I].info;
220+
if (Kind == detail::kernel_param_kind_t::kind_accessor) {
221+
// For args kind of accessor Size is information about accessor.
222+
// The first 11 bits of Size encodes the accessor target.
223+
const access::target AccTarget =
224+
static_cast<access::target>(Size & 0x7ff);
225+
if (AccTarget == access::target::global_buffer ||
226+
AccTarget == access::target::constant_buffer) {
227+
detail::AccessorBaseHost *AccBase =
228+
static_cast<detail::AccessorBaseHost *>(Ptr);
229+
Ptr = detail::getSyclObjImpl(*AccBase).get();
230+
}
231+
}
220232
processArg(Ptr, Kind, Size, I, IndexShift, IsKernelCreatedFromSource);
221233
}
222234
}
@@ -264,9 +276,7 @@ class handler {
264276
switch (AccTarget) {
265277
case access::target::global_buffer:
266278
case access::target::constant_buffer: {
267-
detail::AccessorBaseHost *AccBase =
268-
static_cast<detail::AccessorBaseHost *>(Ptr);
269-
detail::Requirement *AccImpl = detail::getSyclObjImpl(*AccBase).get();
279+
detail::Requirement *AccImpl = static_cast<detail::Requirement *>(Ptr);
270280
MArgs.emplace_back(Kind, AccImpl, Size, Index + IndexShift);
271281
if (!IsKernelCreatedFromSource) {
272282
const size_t SizeAccField = sizeof(size_t) * AccImpl->MDims;
@@ -312,7 +322,6 @@ class handler {
312322
case access::target::image_array: {
313323
throw cl::sycl::invalid_parameter_error(
314324
"Unsupported accessor target case.");
315-
assert(0);
316325
break;
317326
}
318327
}
@@ -445,7 +454,7 @@ class handler {
445454
// Store copy of the accessor.
446455
MAccStorage.push_back(std::move(AccImpl));
447456
// Add accessor to the list of arguments.
448-
MArgs.emplace_back(detail::kernel_param_kind_t::kind_accessor, AccBase,
457+
MArgs.emplace_back(detail::kernel_param_kind_t::kind_accessor, Req,
449458
static_cast<int>(AccessTarget), ArgIndex);
450459
}
451460

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// RUN: %clang -std=c++11 -fsycl %s -o %t.out -lstdc++ -lOpenCL -lsycl -O3
2+
// RUN: %CPU_RUN_PLACEHOLDER %t.out
3+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
4+
// RUN: %ACC_RUN_PLACEHOLDER %t.out
5+
6+
#include <CL/sycl.hpp>
7+
8+
#include <cassert>
9+
10+
using namespace cl::sycl;
11+
12+
int main() {
13+
queue Queue;
14+
if (!Queue.is_host()) {
15+
context Context = Queue.get_context();
16+
17+
cl_context ClContext = Context.get();
18+
19+
const size_t CountSources = 2;
20+
const char *Sources[CountSources] = {
21+
"kernel void foo1(global float* Array, global int* Value) { *Array = "
22+
"42; *Value = 1; }\n",
23+
"kernel void foo2(global float* Array) { int id = get_global_id(0); "
24+
"Array[id] = id; }\n",
25+
};
26+
27+
cl_int Err;
28+
cl_program ClProgram = clCreateProgramWithSource(ClContext, CountSources,
29+
Sources, nullptr, &Err);
30+
assert(Err == CL_SUCCESS);
31+
32+
Err = clBuildProgram(ClProgram, 0, nullptr, nullptr, nullptr, nullptr);
33+
assert(Err == CL_SUCCESS);
34+
35+
cl_kernel FirstCLKernel = clCreateKernel(ClProgram, "foo1", &Err);
36+
assert(Err == CL_SUCCESS);
37+
38+
cl_kernel SecondCLKernel = clCreateKernel(ClProgram, "foo2", &Err);
39+
assert(Err == CL_SUCCESS);
40+
41+
const size_t Count = 100;
42+
float Array[Count];
43+
44+
kernel FirstKernel(FirstCLKernel, Context);
45+
kernel SecondKernel(SecondCLKernel, Context);
46+
int Value;
47+
{
48+
buffer<float, 1> FirstBuffer(Array, range<1>(1));
49+
buffer<int, 1> SecondBuffer(&Value, range<1>(1));
50+
Queue.submit([&](handler &CGH) {
51+
CGH.set_arg(0, FirstBuffer.get_access<access::mode::write>(CGH));
52+
CGH.set_arg(1, SecondBuffer.get_access<access::mode::write>(CGH));
53+
CGH.single_task(FirstKernel);
54+
});
55+
}
56+
Queue.wait_and_throw();
57+
58+
assert(Array[0] == 42);
59+
assert(Value == 1);
60+
61+
{
62+
buffer<float, 1> FirstBuffer(Array, range<1>(Count));
63+
Queue.submit([&](handler &CGH) {
64+
auto Acc = FirstBuffer.get_access<access::mode::read_write>(CGH);
65+
CGH.set_arg(0, FirstBuffer.get_access<access::mode::read_write>(CGH));
66+
CGH.parallel_for(range<1>{Count}, SecondKernel);
67+
});
68+
}
69+
Queue.wait_and_throw();
70+
71+
for (size_t I = 0; I < Count; ++I) {
72+
assert(Array[I] == I);
73+
}
74+
75+
clReleaseContext(ClContext);
76+
clReleaseKernel(FirstCLKernel);
77+
clReleaseKernel(SecondCLKernel);
78+
clReleaseProgram(ClProgram);
79+
}
80+
return 0;
81+
}

0 commit comments

Comments
 (0)