Skip to content

Commit 8fe7dd9

Browse files
authored
[SYCL][NFC] Refactor RT unit tests (#4021)
Merged get_native_interop and spec_constants tests into SYCL2020 test suite, introduced a header with common PI API redefinitions.
1 parent 4a6d21f commit 8fe7dd9

File tree

9 files changed

+315
-323
lines changed

9 files changed

+315
-323
lines changed

sycl/unittests/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ endforeach()
1010
include(AddSYCLUnitTest)
1111

1212
add_subdirectory(allowlist)
13-
add_subdirectory(get_native_interop)
1413
add_subdirectory(misc)
1514
add_subdirectory(pi)
1615
add_subdirectory(kernel-and-program)
1716
add_subdirectory(queue)
1817
add_subdirectory(scheduler)
19-
add_subdirectory(spec_constants)
18+
add_subdirectory(SYCL2020)
2019
add_subdirectory(thread_safety)
2120
add_subdirectory(program_manager)

sycl/unittests/spec_constants/CMakeLists.txt renamed to sycl/unittests/SYCL2020/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ set(CMAKE_CXX_EXTENSIONS OFF)
22

33
# Enable exception handling for these unit tests
44
set(LLVM_REQUIRES_EH 1)
5-
add_sycl_unittest(SpecConstantsTests OBJECT
6-
DefaultValues.cpp
5+
add_sycl_unittest(SYCL2020Tests OBJECT
6+
GetNativeOpenCL.cpp
7+
SpecConstDefaultValues.cpp
78
)
89

sycl/unittests/get_native_interop/test_get_native.cpp renamed to sycl/unittests/SYCL2020/GetNativeOpenCL.cpp

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
//==----------- test_get_native.cpp --- get_native interop unit test only for
2-
// opencl
3-
//-------------==//
1+
//==----------- GetNativeOpenCL.cpp --- interop unit test only for opencl -==//
42
//
53
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
64
// See https://llvm.org/LICENSE.txt for license information.
@@ -13,9 +11,12 @@
1311
#include <CL/sycl.hpp>
1412
#include <CL/sycl/backend/opencl.hpp>
1513
#include <detail/context_impl.hpp>
16-
#include <gtest/gtest.h>
14+
15+
#include <helpers/CommonRedefinitions.hpp>
1716
#include <helpers/PiMock.hpp>
1817

18+
#include <gtest/gtest.h>
19+
1920
#include <iostream>
2021
#include <memory>
2122

@@ -48,27 +49,6 @@ static pi_result redefinedEventRetain(pi_event c) {
4849
return PI_SUCCESS;
4950
}
5051

51-
static pi_result redefinedProgramCreateWithSource(pi_context context,
52-
pi_uint32 count,
53-
const char **strings,
54-
const size_t *lengths,
55-
pi_program *ret_program) {
56-
return PI_SUCCESS;
57-
}
58-
59-
static pi_result
60-
redefinedProgramBuild(pi_program program, pi_uint32 num_devices,
61-
const pi_device *device_list, const char *options,
62-
void (*pfn_notify)(pi_program program, void *user_data),
63-
void *user_data) {
64-
return PI_SUCCESS;
65-
}
66-
67-
pi_result redefinedEventsWait(pi_uint32 num_events,
68-
const pi_event *event_list) {
69-
return PI_SUCCESS;
70-
}
71-
7252
pi_result redefinedEventGetInfo(pi_event event, pi_event_info param_name,
7353
size_t param_value_size, void *param_value,
7454
size_t *param_value_size_ret) {
@@ -81,9 +61,7 @@ pi_result redefinedEventGetInfo(pi_event event, pi_event_info param_name,
8161
return PI_SUCCESS;
8262
}
8363

84-
pi_result redefinedEventRelease(pi_event event) { return PI_SUCCESS; }
85-
86-
TEST(GetNativeTest, GetNativeHandle) {
64+
TEST(GetNative, GetNativeHandle) {
8765
platform Plt{default_selector()};
8866
if (Plt.get_backend() != backend::opencl) {
8967
std::cout << "Test is created for opencl only" << std::endl;
@@ -97,14 +75,9 @@ TEST(GetNativeTest, GetNativeHandle) {
9775
TestCounter = 0;
9876

9977
unittest::PiMock Mock{Plt};
100-
Mock.redefine<detail::PiApiKind::piclProgramCreateWithSource>(
101-
redefinedProgramCreateWithSource);
102-
Mock.redefine<detail::PiApiKind::piProgramBuild>(redefinedProgramBuild);
78+
setupDefaultMockAPIs(Mock);
10379

104-
Mock.redefine<detail::PiApiKind::piEventsWait>(redefinedEventsWait);
10580
Mock.redefine<detail::PiApiKind::piEventGetInfo>(redefinedEventGetInfo);
106-
Mock.redefine<detail::PiApiKind::piEventRelease>(redefinedEventRelease);
107-
10881
Mock.redefine<detail::PiApiKind::piContextRetain>(redefinedContextRetain);
10982
Mock.redefine<detail::PiApiKind::piQueueRetain>(redefinedQueueRetain);
11083
Mock.redefine<detail::PiApiKind::piDeviceRetain>(redefinedDeviceRetain);
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
//==---- DefaultValues.cpp --- Spec constants default values unit test -----==//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#define SYCL2020_DISABLE_DEPRECATION_WARNINGS
10+
11+
#include <CL/sycl.hpp>
12+
13+
#include <helpers/CommonRedefinitions.hpp>
14+
#include <helpers/PiImage.hpp>
15+
#include <helpers/PiMock.hpp>
16+
17+
#include <gtest/gtest.h>
18+
19+
class TestKernel;
20+
const static sycl::specialization_id<int> SpecConst1{42};
21+
22+
__SYCL_INLINE_NAMESPACE(cl) {
23+
namespace sycl {
24+
namespace detail {
25+
template <> struct KernelInfo<TestKernel> {
26+
static constexpr unsigned getNumParams() { return 0; }
27+
static const kernel_param_desc_t &getParamDesc(int) {
28+
static kernel_param_desc_t Dummy;
29+
return Dummy;
30+
}
31+
static constexpr const char *getName() { return "TestKernel"; }
32+
static constexpr bool isESIMD() { return false; }
33+
static constexpr bool callsThisItem() { return false; }
34+
static constexpr bool callsAnyThisFreeFunction() { return false; }
35+
};
36+
37+
template <> const char *get_spec_constant_symbolic_ID<SpecConst1>() {
38+
return "SC1";
39+
}
40+
} // namespace detail
41+
} // namespace sycl
42+
} // __SYCL_INLINE_NAMESPACE(cl)
43+
44+
int SpecConstVal0 = 0;
45+
int SpecConstVal1 = 0;
46+
47+
static pi_result
48+
redefinedProgramSetSpecializationConstant(pi_program prog, pi_uint32 spec_id,
49+
size_t spec_size,
50+
const void *spec_value) {
51+
if (spec_id == 0)
52+
SpecConstVal0 = *static_cast<const int *>(spec_value);
53+
if (spec_id == 1)
54+
SpecConstVal1 = *static_cast<const int *>(spec_value);
55+
56+
return PI_SUCCESS;
57+
}
58+
59+
static sycl::unittest::PiImage generateImageWithSpecConsts() {
60+
using namespace sycl::unittest;
61+
62+
std::vector<char> SpecConstData;
63+
PiProperty SC1 = makeSpecConstant<int>(SpecConstData, "SC1", {0}, {0}, {42});
64+
PiProperty SC2 = makeSpecConstant<int>(SpecConstData, "SC2", {1}, {0}, {8});
65+
66+
PiPropertySet PropSet;
67+
addSpecConstants({SC1, SC2}, std::move(SpecConstData), PropSet);
68+
69+
std::vector<unsigned char> Bin{0, 1, 2, 3, 4, 5}; // Random data
70+
71+
PiArray<PiOffloadEntry> Entries = makeEmptyKernels({"TestKernel"});
72+
73+
PiImage Img{PI_DEVICE_BINARY_TYPE_SPIRV, // Format
74+
__SYCL_PI_DEVICE_BINARY_TARGET_SPIRV64, // DeviceTargetSpec
75+
"", // Compile options
76+
"", // Link options
77+
std::move(Bin),
78+
std::move(Entries),
79+
std::move(PropSet)};
80+
81+
return Img;
82+
}
83+
84+
sycl::unittest::PiImage Img = generateImageWithSpecConsts();
85+
sycl::unittest::PiImageArray ImgArray{Img};
86+
87+
TEST(SpecConstDefaultValues, DISABLED_DefaultValuesAreSet) {
88+
sycl::platform Plt{sycl::default_selector()};
89+
if (Plt.is_host()) {
90+
std::cerr << "Test is not supported on host, skipping\n";
91+
return; // test is not supported on host.
92+
}
93+
94+
if (Plt.get_backend() == sycl::backend::cuda) {
95+
std::cerr << "Test is not supported on CUDA platform, skipping\n";
96+
return;
97+
}
98+
99+
sycl::unittest::PiMock Mock{Plt};
100+
setupDefaultMockAPIs(Mock);
101+
Mock.redefine<sycl::detail::PiApiKind::piextProgramSetSpecializationConstant>(
102+
redefinedProgramSetSpecializationConstant);
103+
104+
const sycl::device Dev = Plt.get_devices()[0];
105+
106+
sycl::queue Queue{Dev};
107+
108+
const sycl::context Ctx = Queue.get_context();
109+
110+
sycl::kernel_bundle KernelBundle =
111+
sycl::get_kernel_bundle<sycl::bundle_state::input>(Ctx, {Dev});
112+
auto ExecBundle = sycl::build(KernelBundle);
113+
Queue.submit([&](sycl::handler &CGH) {
114+
CGH.use_kernel_bundle(ExecBundle);
115+
CGH.single_task<TestKernel>([] {}); // Actual kernel does not matter
116+
});
117+
118+
EXPECT_EQ(SpecConstVal0, 42);
119+
EXPECT_EQ(SpecConstVal1, 8);
120+
}
121+
122+
TEST(SpecConstDefaultValues, DISABLED_DefaultValuesAreOverriden) {
123+
sycl::platform Plt{sycl::default_selector()};
124+
if (Plt.is_host()) {
125+
std::cerr << "Test is not supported on host, skipping\n";
126+
return; // test is not supported on host.
127+
}
128+
129+
if (Plt.get_backend() == sycl::backend::cuda) {
130+
std::cerr << "Test is not supported on CUDA platform, skipping\n";
131+
return;
132+
}
133+
134+
sycl::unittest::PiMock Mock{Plt};
135+
setupDefaultMockAPIs(Mock);
136+
Mock.redefine<sycl::detail::PiApiKind::piextProgramSetSpecializationConstant>(
137+
redefinedProgramSetSpecializationConstant);
138+
139+
const sycl::device Dev = Plt.get_devices()[0];
140+
141+
sycl::queue Queue{Dev};
142+
143+
const sycl::context Ctx = Queue.get_context();
144+
145+
sycl::kernel_bundle KernelBundle =
146+
sycl::get_kernel_bundle<sycl::bundle_state::input>(Ctx, {Dev});
147+
KernelBundle.set_specialization_constant<SpecConst1>(80);
148+
auto ExecBundle = sycl::build(KernelBundle);
149+
Queue.submit([&](sycl::handler &CGH) {
150+
CGH.use_kernel_bundle(ExecBundle);
151+
CGH.single_task<TestKernel>([] {}); // Actual kernel does not matter
152+
});
153+
154+
EXPECT_EQ(SpecConstVal0, 80);
155+
EXPECT_EQ(SpecConstVal1, 8);
156+
}

sycl/unittests/get_native_interop/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
//==---- CommonRedefinitions.hpp --- Header with common PI redefinitions ---==//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <CL/sycl.hpp>
10+
#include <helpers/PiImage.hpp>
11+
#include <helpers/PiMock.hpp>
12+
13+
inline pi_result redefinedProgramCreateCommon(pi_context, const void *, size_t,
14+
pi_program *) {
15+
return PI_SUCCESS;
16+
}
17+
18+
inline pi_result redefinedProgramBuildCommon(
19+
pi_program prog, pi_uint32, const pi_device *, const char *,
20+
void (*pfn_notify)(pi_program program, void *user_data), void *user_data) {
21+
if (pfn_notify) {
22+
pfn_notify(prog, user_data);
23+
}
24+
return PI_SUCCESS;
25+
}
26+
27+
inline pi_result redefinedProgramCompileCommon(
28+
pi_program, pi_uint32, const pi_device *, const char *, pi_uint32,
29+
const pi_program *, const char **, void (*)(pi_program, void *), void *) {
30+
return PI_SUCCESS;
31+
}
32+
33+
inline pi_result redefinedProgramLinkCommon(pi_context, pi_uint32,
34+
const pi_device *, const char *,
35+
pi_uint32, const pi_program *,
36+
void (*)(pi_program, void *),
37+
void *, pi_program *) {
38+
return PI_SUCCESS;
39+
}
40+
41+
inline pi_result redefinedProgramGetInfoCommon(pi_program program,
42+
pi_program_info param_name,
43+
size_t param_value_size,
44+
void *param_value,
45+
size_t *param_value_size_ret) {
46+
if (param_name == PI_PROGRAM_INFO_NUM_DEVICES) {
47+
auto value = reinterpret_cast<unsigned int *>(param_value);
48+
*value = 1;
49+
}
50+
51+
if (param_name == PI_PROGRAM_INFO_BINARY_SIZES) {
52+
auto value = reinterpret_cast<size_t *>(param_value);
53+
value[0] = 1;
54+
}
55+
56+
if (param_name == PI_PROGRAM_INFO_BINARIES) {
57+
auto value = reinterpret_cast<unsigned char *>(param_value);
58+
value[0] = 1;
59+
}
60+
61+
return PI_SUCCESS;
62+
}
63+
64+
inline pi_result redefinedProgramRetainCommon(pi_program program) {
65+
return PI_SUCCESS;
66+
}
67+
68+
inline pi_result redefinedProgramReleaseCommon(pi_program program) {
69+
return PI_SUCCESS;
70+
}
71+
72+
inline pi_result redefinedKernelCreateCommon(pi_program program,
73+
const char *kernel_name,
74+
pi_kernel *ret_kernel) {
75+
*ret_kernel = reinterpret_cast<pi_kernel>(new int[1]);
76+
return PI_SUCCESS;
77+
}
78+
79+
inline pi_result redefinedKernelRetainCommon(pi_kernel kernel) {
80+
return PI_SUCCESS;
81+
}
82+
83+
inline pi_result redefinedKernelReleaseCommon(pi_kernel kernel) {
84+
delete[] reinterpret_cast<int *>(kernel);
85+
return PI_SUCCESS;
86+
}
87+
88+
inline pi_result redefinedKernelGetInfoCommon(pi_kernel kernel,
89+
pi_kernel_info param_name,
90+
size_t param_value_size,
91+
void *param_value,
92+
size_t *param_value_size_ret) {
93+
return PI_SUCCESS;
94+
}
95+
96+
inline pi_result redefinedKernelSetExecInfoCommon(
97+
pi_kernel kernel, pi_kernel_exec_info value_name, size_t param_value_size,
98+
const void *param_value) {
99+
return PI_SUCCESS;
100+
}
101+
102+
inline pi_result redefinedEventsWaitCommon(pi_uint32 num_events,
103+
const pi_event *event_list) {
104+
return PI_SUCCESS;
105+
}
106+
107+
inline pi_result redefinedEventReleaseCommon(pi_event event) {
108+
return PI_SUCCESS;
109+
}
110+
111+
inline pi_result redefinedEnqueueKernelLaunchCommon(
112+
pi_queue, pi_kernel, pi_uint32, const size_t *, const size_t *,
113+
const size_t *, pi_uint32, const pi_event *, pi_event *) {
114+
return PI_SUCCESS;
115+
}
116+
117+
inline void setupDefaultMockAPIs(sycl::unittest::PiMock &Mock) {
118+
using namespace sycl::detail;
119+
Mock.redefine<PiApiKind::piProgramCreate>(redefinedProgramCreateCommon);
120+
Mock.redefine<PiApiKind::piProgramCompile>(redefinedProgramCompileCommon);
121+
Mock.redefine<PiApiKind::piProgramLink>(redefinedProgramLinkCommon);
122+
Mock.redefine<PiApiKind::piProgramBuild>(redefinedProgramBuildCommon);
123+
Mock.redefine<PiApiKind::piProgramGetInfo>(redefinedProgramGetInfoCommon);
124+
Mock.redefine<PiApiKind::piProgramRetain>(redefinedProgramRetainCommon);
125+
Mock.redefine<PiApiKind::piProgramRelease>(redefinedProgramReleaseCommon);
126+
Mock.redefine<PiApiKind::piKernelCreate>(redefinedKernelCreateCommon);
127+
Mock.redefine<PiApiKind::piKernelRetain>(redefinedKernelRetainCommon);
128+
Mock.redefine<PiApiKind::piKernelRelease>(redefinedKernelReleaseCommon);
129+
Mock.redefine<PiApiKind::piKernelGetInfo>(redefinedKernelGetInfoCommon);
130+
Mock.redefine<PiApiKind::piKernelSetExecInfo>(
131+
redefinedKernelSetExecInfoCommon);
132+
Mock.redefine<PiApiKind::piEventsWait>(redefinedEventsWaitCommon);
133+
Mock.redefine<PiApiKind::piEventRelease>(redefinedEventReleaseCommon);
134+
Mock.redefine<PiApiKind::piEnqueueKernelLaunch>(
135+
redefinedEnqueueKernelLaunchCommon);
136+
}

0 commit comments

Comments
 (0)