Skip to content

Commit a4f6af9

Browse files
committed
[SYCL] Add unittest for recent handler::set_arg fixes
Related PR: intel#7313
1 parent 6dbeb2e commit a4f6af9

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

sycl/unittests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,4 @@ add_subdirectory(event)
4848
add_subdirectory(buffer)
4949
add_subdirectory(context)
5050
add_subdirectory(accessor)
51+
add_subdirectory(handler)

sycl/unittests/handler/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
add_sycl_unittest(HandlerTests OBJECT
2+
SetArgForLocalAccessor.cpp
3+
)
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#include <gtest/gtest.h>
2+
#include <helpers/PiMock.hpp>
3+
4+
#include <sycl/sycl.hpp>
5+
6+
// This test checks that we pass the correct buffer size value when setting
7+
// local_accessor as an argument through handler::set_arg to a kernel created
8+
// using OpenCL interoperability methods.
9+
10+
namespace {
11+
12+
struct TestContext {
13+
size_t localBufferArgSize = 0;
14+
15+
// SYCL RT has number of checks that all devices and contexts are consistent
16+
// between kernel, kernel_bundle and other objects.
17+
//
18+
// To ensure that those checks pass, we intercept some PI calls to extract
19+
// the exact PI handles of device and context used in queue creation to later
20+
// return them when program/context/kernel info is requested.
21+
pi_device deviceHandle;
22+
pi_context contextHandle;
23+
};
24+
25+
TestContext GlobalContext;
26+
27+
} // namespace
28+
29+
pi_result redefined_piKernelSetArg(pi_kernel kernel, pi_uint32 arg_index,
30+
size_t arg_size, const void *arg_value) {
31+
GlobalContext.localBufferArgSize = arg_size;
32+
33+
return PI_SUCCESS;
34+
}
35+
36+
pi_result after_piContextGetInfo(pi_context context, pi_context_info param_name,
37+
size_t param_value_size, void *param_value,
38+
size_t *param_value_size_ret) {
39+
switch (param_name) {
40+
case PI_CONTEXT_INFO_DEVICES:
41+
if (param_value)
42+
*static_cast<pi_device *>(param_value) = GlobalContext.deviceHandle;
43+
if (param_value_size_ret)
44+
*param_value_size_ret = sizeof(GlobalContext.deviceHandle);
45+
default:;
46+
}
47+
48+
return PI_SUCCESS;
49+
}
50+
51+
pi_result after_piProgramGetInfo(pi_program program, pi_program_info param_name,
52+
size_t param_value_size, void *param_value,
53+
size_t *param_value_size_ret) {
54+
55+
switch (param_name) {
56+
case PI_PROGRAM_INFO_DEVICES:
57+
if (param_value_size_ret)
58+
*param_value_size_ret = sizeof(GlobalContext.deviceHandle);
59+
if (param_value)
60+
*static_cast<pi_device *>(param_value) = GlobalContext.deviceHandle;
61+
default:;
62+
}
63+
64+
return PI_SUCCESS;
65+
}
66+
67+
pi_result after_piContextCreate(const pi_context_properties *properties,
68+
pi_uint32 num_devices, const pi_device *devices,
69+
void (*pfn_notify)(const char *errinfo,
70+
const void *private_info,
71+
size_t cb, void *user_data),
72+
void *user_data, pi_context *ret_context) {
73+
if (ret_context)
74+
GlobalContext.contextHandle = *ret_context;
75+
GlobalContext.deviceHandle = *devices;
76+
return PI_SUCCESS;
77+
}
78+
79+
pi_result after_piKernelGetInfo(pi_kernel kernel, pi_kernel_info param_name,
80+
size_t param_value_size, void *param_value,
81+
size_t *param_value_size_ret) {
82+
switch (param_name) {
83+
case PI_KERNEL_INFO_CONTEXT:
84+
if (param_value_size_ret)
85+
*param_value_size_ret = sizeof(GlobalContext.contextHandle);
86+
if (param_value)
87+
*static_cast<pi_context *>(param_value) = GlobalContext.contextHandle;
88+
default:;
89+
}
90+
91+
return PI_SUCCESS;
92+
}
93+
94+
TEST(HandlerSetArg, LocalAccessor) {
95+
sycl::unittest::PiMock Mock;
96+
97+
Mock.redefine<sycl::detail::PiApiKind::piKernelSetArg>(
98+
redefined_piKernelSetArg);
99+
Mock.redefineAfter<sycl::detail::PiApiKind::piContextCreate>(
100+
after_piContextCreate);
101+
Mock.redefineAfter<sycl::detail::PiApiKind::piProgramGetInfo>(
102+
after_piProgramGetInfo);
103+
Mock.redefineAfter<sycl::detail::PiApiKind::piContextGetInfo>(
104+
after_piContextGetInfo);
105+
Mock.redefineAfter<sycl::detail::PiApiKind::piKernelGetInfo>(
106+
after_piKernelGetInfo);
107+
108+
constexpr size_t Size = 128;
109+
sycl::queue Q;
110+
111+
DummyHandleT handle;
112+
auto KernelCL = reinterpret_cast<typename sycl::backend_traits<
113+
sycl::backend::opencl>::template input_type<sycl::kernel>>(&handle);
114+
auto Kernel =
115+
sycl::make_kernel<sycl::backend::opencl>(KernelCL, Q.get_context());
116+
117+
Q.submit([&](sycl::handler &CGH) {
118+
sycl::local_accessor<float, 1> Acc(Size, CGH);
119+
CGH.set_arg(0, Acc);
120+
CGH.single_task(Kernel);
121+
}).wait();
122+
123+
ASSERT_EQ(GlobalContext.localBufferArgSize, Size * sizeof(float));
124+
}

sycl/unittests/helpers/PiMockPlugin.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,16 @@ inline pi_result mock_piKernelGetInfo(pi_kernel kernel,
601601
size_t param_value_size,
602602
void *param_value,
603603
size_t *param_value_size_ret) {
604+
switch (param_name) {
605+
case PI_KERNEL_INFO_PROGRAM:
606+
if (param_value_size_ret)
607+
*param_value_size_ret = sizeof(DummyHandlePtrT);
608+
if (param_value)
609+
*(pi_program*)param_value = createDummyHandle<pi_program>();
610+
break;
611+
default:
612+
;
613+
}
604614
return PI_SUCCESS;
605615
}
606616

0 commit comments

Comments
 (0)