Skip to content

Commit 625ade1

Browse files
[SYCL][NFC] Introduce more helpers to unittests (#14716)
Introduced a macro to hide bolierplate of specializing `KernelInfo` structs. Added a helper function to generate default image to avoid repeating the same code in every test.
1 parent 67fa084 commit 625ade1

File tree

12 files changed

+81
-276
lines changed

12 files changed

+81
-276
lines changed

sycl/unittests/SYCL2020/IsCompatible.cpp

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -14,59 +14,13 @@ class TestKernelCPUValidReqdWGSize3D;
1414
class TestKernelGPU;
1515
class TestKernelACC;
1616

17-
namespace sycl {
18-
inline namespace _V1 {
19-
namespace detail {
20-
template <>
21-
struct KernelInfo<TestKernelCPU> : public unittest::MockKernelInfoBase {
22-
static constexpr const char *getName() { return "TestKernelCPU"; }
23-
};
24-
25-
template <>
26-
struct KernelInfo<TestKernelCPUInvalidReqdWGSize1D>
27-
: public unittest::MockKernelInfoBase {
28-
static constexpr const char *getName() {
29-
return "TestKernelCPUInvalidReqdWGSize1D";
30-
}
31-
};
32-
33-
template <>
34-
struct KernelInfo<TestKernelCPUInvalidReqdWGSize2D>
35-
: public unittest::MockKernelInfoBase {
36-
static constexpr const char *getName() {
37-
return "TestKernelCPUInvalidReqdWGSize2D";
38-
}
39-
};
40-
41-
template <>
42-
struct KernelInfo<TestKernelCPUInvalidReqdWGSize3D>
43-
: public unittest::MockKernelInfoBase {
44-
static constexpr const char *getName() {
45-
return "TestKernelCPUInvalidReqdWGSize3D";
46-
}
47-
};
48-
49-
template <>
50-
struct KernelInfo<TestKernelCPUValidReqdWGSize3D>
51-
: public unittest::MockKernelInfoBase {
52-
static constexpr const char *getName() {
53-
return "TestKernelCPUValidReqdWGSize3D";
54-
}
55-
};
56-
57-
template <>
58-
struct KernelInfo<TestKernelGPU> : public unittest::MockKernelInfoBase {
59-
static constexpr const char *getName() { return "TestKernelGPU"; }
60-
};
61-
62-
template <>
63-
struct KernelInfo<TestKernelACC> : public unittest::MockKernelInfoBase {
64-
static constexpr const char *getName() { return "TestKernelACC"; }
65-
};
66-
67-
} // namespace detail
68-
} // namespace _V1
69-
} // namespace sycl
17+
MOCK_INTEGRATION_HEADER(TestKernelCPU)
18+
MOCK_INTEGRATION_HEADER(TestKernelCPUInvalidReqdWGSize1D)
19+
MOCK_INTEGRATION_HEADER(TestKernelCPUInvalidReqdWGSize2D)
20+
MOCK_INTEGRATION_HEADER(TestKernelCPUInvalidReqdWGSize3D)
21+
MOCK_INTEGRATION_HEADER(TestKernelCPUValidReqdWGSize3D)
22+
MOCK_INTEGRATION_HEADER(TestKernelGPU)
23+
MOCK_INTEGRATION_HEADER(TestKernelACC)
7024

7125
static sycl::unittest::PiImage
7226
generateDefaultImage(std::initializer_list<std::string> KernelNames,

sycl/unittests/SYCL2020/KernelBundle.cpp

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,9 @@ class TestKernel;
2020
class TestKernelExeOnly;
2121
class TestKernelWithAspects;
2222

23-
namespace sycl {
24-
inline namespace _V1 {
25-
namespace detail {
26-
template <>
27-
struct KernelInfo<TestKernel> : public unittest::MockKernelInfoBase {
28-
static constexpr const char *getName() { return "TestKernel"; }
29-
};
30-
31-
template <>
32-
struct KernelInfo<TestKernelExeOnly> : public unittest::MockKernelInfoBase {
33-
static constexpr const char *getName() { return "TestKernelExeOnly"; }
34-
};
35-
36-
template <>
37-
struct KernelInfo<TestKernelWithAspects> : public unittest::MockKernelInfoBase {
38-
static constexpr const char *getName() { return "TestKernelWithAspects"; }
39-
};
40-
41-
} // namespace detail
42-
} // namespace _V1
43-
} // namespace sycl
23+
MOCK_INTEGRATION_HEADER(TestKernel)
24+
MOCK_INTEGRATION_HEADER(TestKernelExeOnly)
25+
MOCK_INTEGRATION_HEADER(TestKernelWithAspects)
4426

4527
static sycl::unittest::PiImage
4628
generateDefaultImage(std::initializer_list<std::string> KernelNames,

sycl/unittests/SYCL2020/KernelBundleStateFiltering.cpp

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,12 @@ class KernelB;
2525
class KernelC;
2626
class KernelD;
2727
class KernelE;
28-
namespace sycl {
29-
inline namespace _V1 {
30-
namespace detail {
31-
template <> struct KernelInfo<KernelA> : public unittest::MockKernelInfoBase {
32-
static constexpr const char *getName() { return "KernelA"; }
33-
};
34-
template <> struct KernelInfo<KernelB> : public unittest::MockKernelInfoBase {
35-
static constexpr const char *getName() { return "KernelB"; }
36-
};
37-
template <> struct KernelInfo<KernelC> : public unittest::MockKernelInfoBase {
38-
static constexpr const char *getName() { return "KernelC"; }
39-
};
40-
template <> struct KernelInfo<KernelD> : public unittest::MockKernelInfoBase {
41-
static constexpr const char *getName() { return "KernelD"; }
42-
};
43-
template <> struct KernelInfo<KernelE> : public unittest::MockKernelInfoBase {
44-
static constexpr const char *getName() { return "KernelE"; }
45-
};
46-
} // namespace detail
47-
} // namespace _V1
48-
} // namespace sycl
28+
29+
MOCK_INTEGRATION_HEADER(KernelA)
30+
MOCK_INTEGRATION_HEADER(KernelB)
31+
MOCK_INTEGRATION_HEADER(KernelC)
32+
MOCK_INTEGRATION_HEADER(KernelD)
33+
MOCK_INTEGRATION_HEADER(KernelE)
4934

5035
namespace {
5136

sycl/unittests/buffer/KernelArgMemObj.cpp

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,8 @@ struct KernelInfo<TestKernelWithMemObj> : public unittest::MockKernelInfoBase {
3333
} // namespace _V1
3434
} // namespace sycl
3535

36-
static sycl::unittest::PiImage generateImage() {
37-
using namespace sycl::unittest;
38-
39-
PiPropertySet PropSet;
40-
41-
std::vector<unsigned char> Bin{0, 1, 2, 3, 4, 5}; // Random data
42-
43-
PiArray<PiOffloadEntry> Entries = makeEmptyKernels({"TestKernelWithMemObj"});
44-
45-
PiImage Img{PI_DEVICE_BINARY_TYPE_SPIRV, // Format
46-
__SYCL_PI_DEVICE_BINARY_TARGET_SPIRV64, // DeviceTargetSpec
47-
"", // Compile options
48-
"", // Link options
49-
std::move(Bin),
50-
std::move(Entries),
51-
std::move(PropSet)};
52-
53-
return Img;
54-
}
55-
56-
static sycl::unittest::PiImage Img = generateImage();
36+
static auto Img =
37+
sycl::unittest::generateDefaultImage({"TestKernelWithMemObj"});
5738
static sycl::unittest::PiImageArray<1> ImgArray{&Img};
5839

5940
using namespace sycl;

sycl/unittests/helpers/MockKernelInfo.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,18 @@ struct MockKernelInfoBase {
2929
} // namespace unittest
3030
} // namespace _V1
3131
} // namespace sycl
32+
33+
// In most cases we don't need to redefine any other method besides getName(),
34+
// so here we only have the simplest helper. If any test needs to redefine more
35+
// methods, they can do that explicitly.
36+
#define MOCK_INTEGRATION_HEADER(KernelName) \
37+
namespace sycl { \
38+
inline namespace _V1 { \
39+
namespace detail { \
40+
template <> \
41+
struct KernelInfo<KernelName> : public unittest::MockKernelInfoBase { \
42+
static constexpr const char *getName() { return #KernelName; } \
43+
}; \
44+
} /* namespace detail */ \
45+
} /* namespace _V1 */ \
46+
} /* namespace sycl */

sycl/unittests/helpers/PiImage.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,25 @@ addDeviceRequirementsProps(PiPropertySet &Props,
536536
std::move(Value));
537537
}
538538

539+
inline PiImage
540+
generateDefaultImage(std::initializer_list<std::string> KernelNames) {
541+
PiPropertySet PropSet;
542+
543+
std::vector<unsigned char> Bin{0, 1, 2, 3, 4, 5}; // Random data
544+
545+
PiArray<PiOffloadEntry> Entries = makeEmptyKernels(KernelNames);
546+
547+
PiImage Img{PI_DEVICE_BINARY_TYPE_SPIRV, // Format
548+
__SYCL_PI_DEVICE_BINARY_TARGET_SPIRV64, // DeviceTargetSpec
549+
"", // Compile options
550+
"", // Link options
551+
std::move(Bin),
552+
std::move(Entries),
553+
std::move(PropSet)};
554+
555+
return Img;
556+
}
557+
539558
} // namespace unittest
540559
} // namespace _V1
541560
} // namespace sycl

sycl/unittests/helpers/TestKernel.hpp

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,6 @@ struct KernelInfo<TestKernel<KernelSize>>
3333
} // namespace _V1
3434
} // namespace sycl
3535

36-
static sycl::unittest::PiImage generateDefaultImage() {
37-
using namespace sycl::unittest;
38-
39-
PiPropertySet PropSet;
40-
41-
std::vector<unsigned char> Bin{0, 1, 2, 3, 4, 5}; // Random data
42-
43-
PiArray<PiOffloadEntry> Entries = makeEmptyKernels({"TestKernel"});
44-
45-
PiImage Img{PI_DEVICE_BINARY_TYPE_SPIRV, // Format
46-
__SYCL_PI_DEVICE_BINARY_TARGET_SPIRV64, // DeviceTargetSpec
47-
"", // Compile options
48-
"", // Link options
49-
std::move(Bin),
50-
std::move(Entries),
51-
std::move(PropSet)};
52-
53-
return Img;
54-
}
55-
56-
static sycl::unittest::PiImage Img = generateDefaultImage();
36+
static sycl::unittest::PiImage Img =
37+
sycl::unittest::generateDefaultImage({"TestKernel"});
5738
static sycl::unittest::PiImageArray<1> ImgArray{&Img};

sycl/unittests/kernel-and-program/Cache.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,13 @@ class CacheTestKernel2 {
3636
void operator()(sycl::item<1>){};
3737
};
3838

39+
MOCK_INTEGRATION_HEADER(CacheTestKernel)
40+
MOCK_INTEGRATION_HEADER(CacheTestKernel2)
41+
3942
namespace sycl {
4043
const static specialization_id<int> SpecConst1{42};
4144
inline namespace _V1 {
4245
namespace detail {
43-
template <>
44-
struct KernelInfo<CacheTestKernel> : public unittest::MockKernelInfoBase {
45-
static constexpr const char *getName() { return "CacheTestKernel"; }
46-
};
47-
template <>
48-
struct KernelInfo<CacheTestKernel2> : public unittest::MockKernelInfoBase {
49-
static constexpr const char *getName() { return "CacheTestKernel2"; }
50-
};
5146
template <> const char *get_spec_constant_symbolic_ID<SpecConst1>() {
5247
return "SC1";
5348
}

sycl/unittests/kernel-and-program/MultipleDevsCache.cpp

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,10 @@ using namespace sycl;
2323

2424
class MultipleDevsCacheTestKernel;
2525

26-
namespace sycl {
27-
inline namespace _V1 {
28-
namespace detail {
29-
template <>
30-
struct KernelInfo<MultipleDevsCacheTestKernel>
31-
: public unittest::MockKernelInfoBase {
32-
static constexpr const char *getName() {
33-
return "MultipleDevsCacheTestKernel";
34-
}
35-
};
36-
37-
} // namespace detail
38-
} // namespace _V1
39-
} // namespace sycl
40-
41-
static sycl::unittest::PiImage generateDefaultImage() {
42-
using namespace sycl::unittest;
43-
44-
PiPropertySet PropSet;
45-
46-
std::vector<unsigned char> Bin{0, 1, 2, 3, 4, 5}; // Random data
47-
48-
PiArray<PiOffloadEntry> Entries =
49-
makeEmptyKernels({"MultipleDevsCacheTestKernel"});
50-
51-
PiImage Img{PI_DEVICE_BINARY_TYPE_SPIRV, // Format
52-
__SYCL_PI_DEVICE_BINARY_TARGET_SPIRV64, // DeviceTargetSpec
53-
"", // Compile options
54-
"", // Link options
55-
std::move(Bin),
56-
std::move(Entries),
57-
std::move(PropSet)};
58-
59-
return Img;
60-
}
26+
MOCK_INTEGRATION_HEADER(MultipleDevsCacheTestKernel)
6127

62-
static sycl::unittest::PiImage Img = generateDefaultImage();
28+
static sycl::unittest::PiImage Img =
29+
sycl::unittest::generateDefaultImage({"MultipleDevsCacheTestKernel"});
6330
static sycl::unittest::PiImageArray<1> ImgArray{&Img};
6431

6532
static pi_result redefinedDevicesGetAfter(pi_platform platform,

sycl/unittests/kernel-and-program/OutOfResources.cpp

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,45 +24,12 @@ using namespace sycl;
2424
class OutOfResourcesKernel1;
2525
class OutOfResourcesKernel2;
2626

27-
namespace sycl {
28-
inline namespace _V1 {
29-
namespace detail {
30-
template <>
31-
struct KernelInfo<OutOfResourcesKernel1> : public unittest::MockKernelInfoBase {
32-
static constexpr const char *getName() { return "OutOfResourcesKernel1"; }
33-
};
27+
MOCK_INTEGRATION_HEADER(OutOfResourcesKernel1)
28+
MOCK_INTEGRATION_HEADER(OutOfResourcesKernel2)
3429

35-
template <>
36-
struct KernelInfo<OutOfResourcesKernel2> : public unittest::MockKernelInfoBase {
37-
static constexpr const char *getName() { return "OutOfResourcesKernel2"; }
38-
};
39-
40-
} // namespace detail
41-
} // namespace _V1
42-
} // namespace sycl
43-
44-
static sycl::unittest::PiImage makeImage(const char *kname) {
45-
using namespace sycl::unittest;
46-
47-
PiPropertySet PropSet;
48-
49-
std::vector<unsigned char> Bin{0, 1, 2, 3, 4, 5}; // Random data
50-
51-
PiArray<PiOffloadEntry> Entries = makeEmptyKernels({kname});
52-
53-
PiImage Img{PI_DEVICE_BINARY_TYPE_SPIRV, // Format
54-
__SYCL_PI_DEVICE_BINARY_TARGET_SPIRV64, // DeviceTargetSpec
55-
"", // Compile options
56-
"", // Link options
57-
std::move(Bin),
58-
std::move(Entries),
59-
std::move(PropSet)};
60-
61-
return Img;
62-
}
63-
64-
static sycl::unittest::PiImage Img[2] = {makeImage("OutOfResourcesKernel1"),
65-
makeImage("OutOfResourcesKernel2")};
30+
static sycl::unittest::PiImage Img[2] = {
31+
sycl::unittest::generateDefaultImage({"OutOfResourcesKernel1"}),
32+
sycl::unittest::generateDefaultImage({"OutOfResourcesKernel2"})};
6633

6734
static sycl::unittest::PiImageArray<2> ImgArray{Img};
6835

sycl/unittests/pipes/host_pipe_registration.cpp

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,13 @@
1212
#include <detail/device_binary_image.hpp>
1313
#include <detail/host_pipe_map_entry.hpp>
1414
#include <gtest/gtest.h>
15+
#include <helpers/MockKernelInfo.hpp>
1516
#include <helpers/PiImage.hpp>
1617
#include <helpers/PiMock.hpp>
1718
#include <sycl/detail/host_pipe_map.hpp>
1819

19-
template <size_t KernelSize = 1> class TestKernel;
20-
21-
namespace sycl {
22-
inline namespace _V1 {
23-
namespace detail {
24-
template <size_t KernelSize> struct KernelInfo<TestKernel<KernelSize>> {
25-
static constexpr unsigned getNumParams() { return 0; }
26-
static const kernel_param_desc_t &getParamDesc(int) {
27-
static kernel_param_desc_t Dummy;
28-
return Dummy;
29-
}
30-
static constexpr const char *getName() { return "TestKernel"; }
31-
static constexpr bool isESIMD() { return false; }
32-
static constexpr bool callsThisItem() { return false; }
33-
static constexpr bool callsAnyThisFreeFunction() { return false; }
34-
static constexpr int64_t getKernelSize() { return KernelSize; }
35-
};
36-
37-
} // namespace detail
38-
} // namespace _V1
39-
} // namespace sycl
20+
class TestKernel;
21+
MOCK_INTEGRATION_HEADER(TestKernel)
4022

4123
using namespace sycl;
4224
using default_pipe_properties =

0 commit comments

Comments
 (0)