Skip to content

Commit 65a5d4f

Browse files
Added tests for null-dref, null-spirv pointer
Added a test for non-supported backend (host). This only runs if SYCL_ENABLE_HOST_DEVICE environment variable is set.
1 parent fc400fd commit 65a5d4f

File tree

2 files changed

+124
-29
lines changed

2 files changed

+124
-29
lines changed

libsyclinterface/source/dpctl_sycl_kernel_bundle_interface.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,11 @@ DPCTLKernelBundle_CreateFromSpirv(__dpctl_keep const DPCTLSyclContextRef CtxRef,
594594
__FILE__, __func__, __LINE__);
595595
return KBRef;
596596
}
597+
if ((!IL) || (length == 0)) {
598+
error_handler("Cannot create program from null SPIR-V buffer.",
599+
__FILE__, __func__, __LINE__);
600+
return KBRef;
601+
}
597602

598603
context *SyclCtx = unwrap(CtxRef);
599604
device *SyclDev = unwrap(DevRef);

libsyclinterface/tests/test_sycl_program_interface.cpp renamed to libsyclinterface/tests/test_sycl_kernel_bundle_interface.cpp

Lines changed: 119 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,22 @@
4242

4343
using namespace cl::sycl;
4444

45-
struct TestDPCTLSyclProgramInterface
45+
struct TestDPCTLSyclKernelBundleInterface
4646
: public ::testing::TestWithParam<const char *>
4747
{
4848
DPCTLSyclDeviceRef DRef = nullptr;
4949
DPCTLSyclContextRef CRef = nullptr;
50-
DPCTLSyclQueueRef QRef = nullptr;
5150
DPCTLSyclKernelBundleRef KBRef = nullptr;
5251
std::ifstream spirvFile;
5352
size_t spirvFileSize;
5453
std::vector<char> spirvBuffer;
5554

56-
TestDPCTLSyclProgramInterface()
55+
TestDPCTLSyclKernelBundleInterface()
5756
{
5857
auto DS = DPCTLFilterSelector_Create(GetParam());
5958
DRef = DPCTLDevice_CreateFromSelector(DS);
6059
DPCTLDeviceSelector_Delete(DS);
6160
CRef = DPCTLDeviceMgr_GetCachedContext(DRef);
62-
QRef = DPCTLQueue_Create(CRef, DRef, nullptr, DPCTL_DEFAULT_PROPERTY);
6361

6462
if (DRef) {
6563
spirvFile.open("./multi_kernel.spv",
@@ -82,18 +80,20 @@ struct TestDPCTLSyclProgramInterface
8280
}
8381
}
8482

85-
~TestDPCTLSyclProgramInterface()
83+
~TestDPCTLSyclKernelBundleInterface()
8684
{
87-
if (DRef)
85+
if (DRef) {
8886
spirvFile.close();
89-
DPCTLDevice_Delete(DRef);
90-
DPCTLQueue_Delete(QRef);
91-
DPCTLContext_Delete(CRef);
92-
DPCTLKernelBundle_Delete(KBRef);
87+
DPCTLDevice_Delete(DRef);
88+
}
89+
if (CRef)
90+
DPCTLContext_Delete(CRef);
91+
if (KBRef)
92+
DPCTLKernelBundle_Delete(KBRef);
9393
}
9494
};
9595

96-
TEST_P(TestDPCTLSyclProgramInterface, ChkCreateFromSpirv)
96+
TEST_P(TestDPCTLSyclKernelBundleInterface, ChkCreateFromSpirv)
9797
{
9898

9999
ASSERT_TRUE(KBRef != nullptr);
@@ -102,25 +102,36 @@ TEST_P(TestDPCTLSyclProgramInterface, ChkCreateFromSpirv)
102102
ASSERT_FALSE(DPCTLKernelBundle_HasKernel(KBRef, nullptr));
103103
}
104104

105-
TEST_P(TestDPCTLSyclProgramInterface, ChkCreateFromSpirvNull)
105+
TEST_P(TestDPCTLSyclKernelBundleInterface, ChkCreateFromSpirvNull)
106106
{
107107
DPCTLSyclContextRef Null_CRef = nullptr;
108108
DPCTLSyclDeviceRef Null_DRef = nullptr;
109109
const void *null_spirv = nullptr;
110110
DPCTLSyclKernelBundleRef KBRef = nullptr;
111+
// Null context
111112
EXPECT_NO_FATAL_FAILURE(KBRef = DPCTLKernelBundle_CreateFromSpirv(
112113
Null_CRef, Null_DRef, null_spirv, 0, nullptr));
113114
ASSERT_TRUE(KBRef == nullptr);
115+
116+
// Null device
117+
EXPECT_NO_FATAL_FAILURE(KBRef = DPCTLKernelBundle_CreateFromSpirv(
118+
CRef, Null_DRef, null_spirv, 0, nullptr));
119+
ASSERT_TRUE(KBRef == nullptr);
120+
121+
// Null IL
122+
EXPECT_NO_FATAL_FAILURE(KBRef = DPCTLKernelBundle_CreateFromSpirv(
123+
CRef, DRef, null_spirv, 0, nullptr));
124+
ASSERT_TRUE(KBRef == nullptr);
114125
}
115126

116-
TEST_P(TestDPCTLSyclProgramInterface, ChkHasKernelNullProgram)
127+
TEST_P(TestDPCTLSyclKernelBundleInterface, ChkHasKernelNullProgram)
117128
{
118129

119130
DPCTLSyclKernelBundleRef NullRef = nullptr;
120131
ASSERT_FALSE(DPCTLKernelBundle_HasKernel(NullRef, "add"));
121132
}
122133

123-
TEST_P(TestDPCTLSyclProgramInterface, ChkGetKernel)
134+
TEST_P(TestDPCTLSyclKernelBundleInterface, ChkGetKernel)
124135
{
125136
auto AddKernel = DPCTLKernelBundle_GetKernel(KBRef, "add");
126137
auto AxpyKernel = DPCTLKernelBundle_GetKernel(KBRef, "axpy");
@@ -134,7 +145,7 @@ TEST_P(TestDPCTLSyclProgramInterface, ChkGetKernel)
134145
EXPECT_NO_FATAL_FAILURE(DPCTLKernel_Delete(NullKernel));
135146
}
136147

137-
TEST_P(TestDPCTLSyclProgramInterface, ChkGetKernelNullProgram)
148+
TEST_P(TestDPCTLSyclKernelBundleInterface, ChkGetKernelNullProgram)
138149
{
139150
DPCTLSyclKernelBundleRef NullRef = nullptr;
140151
DPCTLSyclKernelRef KRef = nullptr;
@@ -143,7 +154,7 @@ TEST_P(TestDPCTLSyclProgramInterface, ChkGetKernelNullProgram)
143154
EXPECT_TRUE(KRef == nullptr);
144155
}
145156

146-
struct TestOCLProgramFromSource : public ::testing::Test
157+
struct TestOCLKernelBundleFromSource : public ::testing::Test
147158
{
148159
const char *CLProgramStr = R"CLC(
149160
kernel void add(global int* a, global int* b, global int* c) {
@@ -160,32 +171,32 @@ struct TestOCLProgramFromSource : public ::testing::Test
160171
const char *CompileOpts = "-cl-fast-relaxed-math";
161172
DPCTLSyclDeviceRef DRef = nullptr;
162173
DPCTLSyclContextRef CRef = nullptr;
163-
DPCTLSyclQueueRef QRef = nullptr;
164174
DPCTLSyclKernelBundleRef KBRef = nullptr;
165175

166-
TestOCLProgramFromSource()
176+
TestOCLKernelBundleFromSource()
167177
{
168178
auto DS = DPCTLFilterSelector_Create("opencl:gpu");
169179
DRef = DPCTLDevice_CreateFromSelector(DS);
170180
DPCTLDeviceSelector_Delete(DS);
171181
CRef = DPCTLDeviceMgr_GetCachedContext(DRef);
172-
QRef = DPCTLQueue_Create(CRef, DRef, nullptr, DPCTL_DEFAULT_PROPERTY);
173182

174183
if (DRef)
175184
KBRef = DPCTLKernelBundle_CreateFromOCLSource(
176185
CRef, DRef, CLProgramStr, CompileOpts);
177186
}
178187

179-
~TestOCLProgramFromSource()
188+
~TestOCLKernelBundleFromSource()
180189
{
181-
DPCTLDevice_Delete(DRef);
182-
DPCTLQueue_Delete(QRef);
183-
DPCTLContext_Delete(CRef);
184-
DPCTLKernelBundle_Delete(KBRef);
190+
if (DRef)
191+
DPCTLDevice_Delete(DRef);
192+
if (CRef)
193+
DPCTLContext_Delete(CRef);
194+
if (KBRef)
195+
DPCTLKernelBundle_Delete(KBRef);
185196
}
186197
};
187198

188-
TEST_F(TestOCLProgramFromSource, CheckCreateFromOCLSource)
199+
TEST_F(TestOCLKernelBundleFromSource, CheckCreateFromOCLSource)
189200
{
190201
if (!DRef)
191202
GTEST_SKIP_("Skipping as no OpenCL GPU device found.\n");
@@ -195,7 +206,7 @@ TEST_F(TestOCLProgramFromSource, CheckCreateFromOCLSource)
195206
ASSERT_TRUE(DPCTLKernelBundle_HasKernel(KBRef, "axpy"));
196207
}
197208

198-
TEST_F(TestOCLProgramFromSource, CheckCreateFromOCLSourceNull)
209+
TEST_F(TestOCLKernelBundleFromSource, CheckCreateFromOCLSourceNull)
199210
{
200211
const char *InvalidCLProgramStr = R"CLC(
201212
kernel void invalid(global foo* a, global bar* b) {
@@ -211,9 +222,21 @@ TEST_F(TestOCLProgramFromSource, CheckCreateFromOCLSourceNull)
211222
EXPECT_NO_FATAL_FAILURE(KBRef = DPCTLKernelBundle_CreateFromOCLSource(
212223
CRef, DRef, InvalidCLProgramStr, CompileOpts););
213224
ASSERT_TRUE(KBRef == nullptr);
225+
226+
DPCTLSyclContextRef Null_CRef = nullptr;
227+
EXPECT_NO_FATAL_FAILURE(
228+
KBRef = DPCTLKernelBundle_CreateFromOCLSource(
229+
Null_CRef, DRef, InvalidCLProgramStr, CompileOpts););
230+
ASSERT_TRUE(KBRef == nullptr);
231+
232+
DPCTLSyclDeviceRef Null_DRef = nullptr;
233+
EXPECT_NO_FATAL_FAILURE(
234+
KBRef = DPCTLKernelBundle_CreateFromOCLSource(
235+
CRef, Null_DRef, InvalidCLProgramStr, CompileOpts););
236+
ASSERT_TRUE(KBRef == nullptr);
214237
}
215238

216-
TEST_F(TestOCLProgramFromSource, CheckGetKernelOCLSource)
239+
TEST_F(TestOCLKernelBundleFromSource, CheckGetKernelOCLSource)
217240
{
218241
if (!DRef)
219242
GTEST_SKIP_("Skipping as no OpenCL GPU device found.\n");
@@ -226,8 +249,8 @@ TEST_F(TestOCLProgramFromSource, CheckGetKernelOCLSource)
226249
DPCTLKernel_Delete(AxpyKernel);
227250
}
228251

229-
INSTANTIATE_TEST_SUITE_P(ProgramCreationFromSpriv,
230-
TestDPCTLSyclProgramInterface,
252+
INSTANTIATE_TEST_SUITE_P(KernelBundleCreationFromSpirv,
253+
TestDPCTLSyclKernelBundleInterface,
231254
::testing::Values("opencl",
232255
"opencl:gpu",
233256
"opencl:cpu",
@@ -237,3 +260,70 @@ INSTANTIATE_TEST_SUITE_P(ProgramCreationFromSpriv,
237260
"level_zero:gpu",
238261
#endif
239262
"opencl:cpu:0"));
263+
264+
struct TestKernelBundleUnsupportedBackend : public ::testing::Test
265+
{
266+
DPCTLSyclDeviceRef DRef = nullptr;
267+
DPCTLSyclContextRef CRef = nullptr;
268+
269+
TestKernelBundleUnsupportedBackend()
270+
{
271+
auto DS = DPCTLFilterSelector_Create("host:host");
272+
DRef = DPCTLDevice_CreateFromSelector(DS);
273+
DPCTLDeviceSelector_Delete(DS);
274+
if (DRef)
275+
CRef = DPCTLDeviceMgr_GetCachedContext(DRef);
276+
}
277+
278+
void SetUp()
279+
{
280+
if (!DRef) {
281+
std::string message = "Skipping as host device is not enabled.";
282+
GTEST_SKIP_(message.c_str());
283+
}
284+
}
285+
286+
~TestKernelBundleUnsupportedBackend()
287+
{
288+
if (DRef)
289+
DPCTLDevice_Delete(DRef);
290+
if (CRef)
291+
DPCTLContext_Delete(CRef);
292+
}
293+
};
294+
295+
TEST_F(TestKernelBundleUnsupportedBackend, CheckCreateFromSource)
296+
{
297+
const char *src = R"CLC(
298+
kernel void set(global int* a, int v) {
299+
size_t index = get_global_id(0);
300+
a[index] = v;
301+
}
302+
)CLC";
303+
const char *opts = "";
304+
305+
DPCTLSyclKernelBundleRef KBRef = nullptr;
306+
EXPECT_NO_FATAL_FAILURE(
307+
KBRef = DPCTLKernelBundle_CreateFromOCLSource(CRef, DRef, src, opts));
308+
ASSERT_TRUE(KBRef == nullptr);
309+
}
310+
311+
TEST_F(TestKernelBundleUnsupportedBackend, CheckCreateFromSpirv)
312+
{
313+
std::ifstream spirvFile;
314+
size_t spirvFileSize;
315+
std::vector<char> spirvBuffer;
316+
317+
spirvFile.open("./multi_kernel.spv", std::ios::binary | std::ios::ate);
318+
spirvFileSize = std::filesystem::file_size("./multi_kernel.spv");
319+
spirvBuffer.reserve(spirvFileSize);
320+
spirvFile.seekg(0, std::ios::beg);
321+
spirvFile.read(spirvBuffer.data(), spirvFileSize);
322+
spirvFile.close();
323+
324+
DPCTLSyclKernelBundleRef KBRef = nullptr;
325+
EXPECT_NO_FATAL_FAILURE(
326+
KBRef = DPCTLKernelBundle_CreateFromSpirv(
327+
CRef, DRef, spirvBuffer.data(), spirvFileSize, nullptr));
328+
ASSERT_TRUE(KBRef == nullptr);
329+
}

0 commit comments

Comments
 (0)