Skip to content

Commit

Permalink
[SYCL][NFC] Introduce more helpers to unittests (#14716)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AlexeySachkov committed Jul 23, 2024
1 parent 67fa084 commit 625ade1
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 276 deletions.
60 changes: 7 additions & 53 deletions sycl/unittests/SYCL2020/IsCompatible.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,59 +14,13 @@ class TestKernelCPUValidReqdWGSize3D;
class TestKernelGPU;
class TestKernelACC;

namespace sycl {
inline namespace _V1 {
namespace detail {
template <>
struct KernelInfo<TestKernelCPU> : public unittest::MockKernelInfoBase {
static constexpr const char *getName() { return "TestKernelCPU"; }
};

template <>
struct KernelInfo<TestKernelCPUInvalidReqdWGSize1D>
: public unittest::MockKernelInfoBase {
static constexpr const char *getName() {
return "TestKernelCPUInvalidReqdWGSize1D";
}
};

template <>
struct KernelInfo<TestKernelCPUInvalidReqdWGSize2D>
: public unittest::MockKernelInfoBase {
static constexpr const char *getName() {
return "TestKernelCPUInvalidReqdWGSize2D";
}
};

template <>
struct KernelInfo<TestKernelCPUInvalidReqdWGSize3D>
: public unittest::MockKernelInfoBase {
static constexpr const char *getName() {
return "TestKernelCPUInvalidReqdWGSize3D";
}
};

template <>
struct KernelInfo<TestKernelCPUValidReqdWGSize3D>
: public unittest::MockKernelInfoBase {
static constexpr const char *getName() {
return "TestKernelCPUValidReqdWGSize3D";
}
};

template <>
struct KernelInfo<TestKernelGPU> : public unittest::MockKernelInfoBase {
static constexpr const char *getName() { return "TestKernelGPU"; }
};

template <>
struct KernelInfo<TestKernelACC> : public unittest::MockKernelInfoBase {
static constexpr const char *getName() { return "TestKernelACC"; }
};

} // namespace detail
} // namespace _V1
} // namespace sycl
MOCK_INTEGRATION_HEADER(TestKernelCPU)
MOCK_INTEGRATION_HEADER(TestKernelCPUInvalidReqdWGSize1D)
MOCK_INTEGRATION_HEADER(TestKernelCPUInvalidReqdWGSize2D)
MOCK_INTEGRATION_HEADER(TestKernelCPUInvalidReqdWGSize3D)
MOCK_INTEGRATION_HEADER(TestKernelCPUValidReqdWGSize3D)
MOCK_INTEGRATION_HEADER(TestKernelGPU)
MOCK_INTEGRATION_HEADER(TestKernelACC)

static sycl::unittest::PiImage
generateDefaultImage(std::initializer_list<std::string> KernelNames,
Expand Down
24 changes: 3 additions & 21 deletions sycl/unittests/SYCL2020/KernelBundle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,9 @@ class TestKernel;
class TestKernelExeOnly;
class TestKernelWithAspects;

namespace sycl {
inline namespace _V1 {
namespace detail {
template <>
struct KernelInfo<TestKernel> : public unittest::MockKernelInfoBase {
static constexpr const char *getName() { return "TestKernel"; }
};

template <>
struct KernelInfo<TestKernelExeOnly> : public unittest::MockKernelInfoBase {
static constexpr const char *getName() { return "TestKernelExeOnly"; }
};

template <>
struct KernelInfo<TestKernelWithAspects> : public unittest::MockKernelInfoBase {
static constexpr const char *getName() { return "TestKernelWithAspects"; }
};

} // namespace detail
} // namespace _V1
} // namespace sycl
MOCK_INTEGRATION_HEADER(TestKernel)
MOCK_INTEGRATION_HEADER(TestKernelExeOnly)
MOCK_INTEGRATION_HEADER(TestKernelWithAspects)

static sycl::unittest::PiImage
generateDefaultImage(std::initializer_list<std::string> KernelNames,
Expand Down
27 changes: 6 additions & 21 deletions sycl/unittests/SYCL2020/KernelBundleStateFiltering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,12 @@ class KernelB;
class KernelC;
class KernelD;
class KernelE;
namespace sycl {
inline namespace _V1 {
namespace detail {
template <> struct KernelInfo<KernelA> : public unittest::MockKernelInfoBase {
static constexpr const char *getName() { return "KernelA"; }
};
template <> struct KernelInfo<KernelB> : public unittest::MockKernelInfoBase {
static constexpr const char *getName() { return "KernelB"; }
};
template <> struct KernelInfo<KernelC> : public unittest::MockKernelInfoBase {
static constexpr const char *getName() { return "KernelC"; }
};
template <> struct KernelInfo<KernelD> : public unittest::MockKernelInfoBase {
static constexpr const char *getName() { return "KernelD"; }
};
template <> struct KernelInfo<KernelE> : public unittest::MockKernelInfoBase {
static constexpr const char *getName() { return "KernelE"; }
};
} // namespace detail
} // namespace _V1
} // namespace sycl

MOCK_INTEGRATION_HEADER(KernelA)
MOCK_INTEGRATION_HEADER(KernelB)
MOCK_INTEGRATION_HEADER(KernelC)
MOCK_INTEGRATION_HEADER(KernelD)
MOCK_INTEGRATION_HEADER(KernelE)

namespace {

Expand Down
23 changes: 2 additions & 21 deletions sycl/unittests/buffer/KernelArgMemObj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,8 @@ struct KernelInfo<TestKernelWithMemObj> : public unittest::MockKernelInfoBase {
} // namespace _V1
} // namespace sycl

static sycl::unittest::PiImage generateImage() {
using namespace sycl::unittest;

PiPropertySet PropSet;

std::vector<unsigned char> Bin{0, 1, 2, 3, 4, 5}; // Random data

PiArray<PiOffloadEntry> Entries = makeEmptyKernels({"TestKernelWithMemObj"});

PiImage Img{PI_DEVICE_BINARY_TYPE_SPIRV, // Format
__SYCL_PI_DEVICE_BINARY_TARGET_SPIRV64, // DeviceTargetSpec
"", // Compile options
"", // Link options
std::move(Bin),
std::move(Entries),
std::move(PropSet)};

return Img;
}

static sycl::unittest::PiImage Img = generateImage();
static auto Img =
sycl::unittest::generateDefaultImage({"TestKernelWithMemObj"});
static sycl::unittest::PiImageArray<1> ImgArray{&Img};

using namespace sycl;
Expand Down
15 changes: 15 additions & 0 deletions sycl/unittests/helpers/MockKernelInfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,18 @@ struct MockKernelInfoBase {
} // namespace unittest
} // namespace _V1
} // namespace sycl

// In most cases we don't need to redefine any other method besides getName(),
// so here we only have the simplest helper. If any test needs to redefine more
// methods, they can do that explicitly.
#define MOCK_INTEGRATION_HEADER(KernelName) \
namespace sycl { \
inline namespace _V1 { \
namespace detail { \
template <> \
struct KernelInfo<KernelName> : public unittest::MockKernelInfoBase { \
static constexpr const char *getName() { return #KernelName; } \
}; \
} /* namespace detail */ \
} /* namespace _V1 */ \
} /* namespace sycl */
19 changes: 19 additions & 0 deletions sycl/unittests/helpers/PiImage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,25 @@ addDeviceRequirementsProps(PiPropertySet &Props,
std::move(Value));
}

inline PiImage
generateDefaultImage(std::initializer_list<std::string> KernelNames) {
PiPropertySet PropSet;

std::vector<unsigned char> Bin{0, 1, 2, 3, 4, 5}; // Random data

PiArray<PiOffloadEntry> Entries = makeEmptyKernels(KernelNames);

PiImage Img{PI_DEVICE_BINARY_TYPE_SPIRV, // Format
__SYCL_PI_DEVICE_BINARY_TARGET_SPIRV64, // DeviceTargetSpec
"", // Compile options
"", // Link options
std::move(Bin),
std::move(Entries),
std::move(PropSet)};

return Img;
}

} // namespace unittest
} // namespace _V1
} // namespace sycl
23 changes: 2 additions & 21 deletions sycl/unittests/helpers/TestKernel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,6 @@ struct KernelInfo<TestKernel<KernelSize>>
} // namespace _V1
} // namespace sycl

static sycl::unittest::PiImage generateDefaultImage() {
using namespace sycl::unittest;

PiPropertySet PropSet;

std::vector<unsigned char> Bin{0, 1, 2, 3, 4, 5}; // Random data

PiArray<PiOffloadEntry> Entries = makeEmptyKernels({"TestKernel"});

PiImage Img{PI_DEVICE_BINARY_TYPE_SPIRV, // Format
__SYCL_PI_DEVICE_BINARY_TARGET_SPIRV64, // DeviceTargetSpec
"", // Compile options
"", // Link options
std::move(Bin),
std::move(Entries),
std::move(PropSet)};

return Img;
}

static sycl::unittest::PiImage Img = generateDefaultImage();
static sycl::unittest::PiImage Img =
sycl::unittest::generateDefaultImage({"TestKernel"});
static sycl::unittest::PiImageArray<1> ImgArray{&Img};
11 changes: 3 additions & 8 deletions sycl/unittests/kernel-and-program/Cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,13 @@ class CacheTestKernel2 {
void operator()(sycl::item<1>){};
};

MOCK_INTEGRATION_HEADER(CacheTestKernel)
MOCK_INTEGRATION_HEADER(CacheTestKernel2)

namespace sycl {
const static specialization_id<int> SpecConst1{42};
inline namespace _V1 {
namespace detail {
template <>
struct KernelInfo<CacheTestKernel> : public unittest::MockKernelInfoBase {
static constexpr const char *getName() { return "CacheTestKernel"; }
};
template <>
struct KernelInfo<CacheTestKernel2> : public unittest::MockKernelInfoBase {
static constexpr const char *getName() { return "CacheTestKernel2"; }
};
template <> const char *get_spec_constant_symbolic_ID<SpecConst1>() {
return "SC1";
}
Expand Down
39 changes: 3 additions & 36 deletions sycl/unittests/kernel-and-program/MultipleDevsCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,10 @@ using namespace sycl;

class MultipleDevsCacheTestKernel;

namespace sycl {
inline namespace _V1 {
namespace detail {
template <>
struct KernelInfo<MultipleDevsCacheTestKernel>
: public unittest::MockKernelInfoBase {
static constexpr const char *getName() {
return "MultipleDevsCacheTestKernel";
}
};

} // namespace detail
} // namespace _V1
} // namespace sycl

static sycl::unittest::PiImage generateDefaultImage() {
using namespace sycl::unittest;

PiPropertySet PropSet;

std::vector<unsigned char> Bin{0, 1, 2, 3, 4, 5}; // Random data

PiArray<PiOffloadEntry> Entries =
makeEmptyKernels({"MultipleDevsCacheTestKernel"});

PiImage Img{PI_DEVICE_BINARY_TYPE_SPIRV, // Format
__SYCL_PI_DEVICE_BINARY_TARGET_SPIRV64, // DeviceTargetSpec
"", // Compile options
"", // Link options
std::move(Bin),
std::move(Entries),
std::move(PropSet)};

return Img;
}
MOCK_INTEGRATION_HEADER(MultipleDevsCacheTestKernel)

static sycl::unittest::PiImage Img = generateDefaultImage();
static sycl::unittest::PiImage Img =
sycl::unittest::generateDefaultImage({"MultipleDevsCacheTestKernel"});
static sycl::unittest::PiImageArray<1> ImgArray{&Img};

static pi_result redefinedDevicesGetAfter(pi_platform platform,
Expand Down
43 changes: 5 additions & 38 deletions sycl/unittests/kernel-and-program/OutOfResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,45 +24,12 @@ using namespace sycl;
class OutOfResourcesKernel1;
class OutOfResourcesKernel2;

namespace sycl {
inline namespace _V1 {
namespace detail {
template <>
struct KernelInfo<OutOfResourcesKernel1> : public unittest::MockKernelInfoBase {
static constexpr const char *getName() { return "OutOfResourcesKernel1"; }
};
MOCK_INTEGRATION_HEADER(OutOfResourcesKernel1)
MOCK_INTEGRATION_HEADER(OutOfResourcesKernel2)

template <>
struct KernelInfo<OutOfResourcesKernel2> : public unittest::MockKernelInfoBase {
static constexpr const char *getName() { return "OutOfResourcesKernel2"; }
};

} // namespace detail
} // namespace _V1
} // namespace sycl

static sycl::unittest::PiImage makeImage(const char *kname) {
using namespace sycl::unittest;

PiPropertySet PropSet;

std::vector<unsigned char> Bin{0, 1, 2, 3, 4, 5}; // Random data

PiArray<PiOffloadEntry> Entries = makeEmptyKernels({kname});

PiImage Img{PI_DEVICE_BINARY_TYPE_SPIRV, // Format
__SYCL_PI_DEVICE_BINARY_TARGET_SPIRV64, // DeviceTargetSpec
"", // Compile options
"", // Link options
std::move(Bin),
std::move(Entries),
std::move(PropSet)};

return Img;
}

static sycl::unittest::PiImage Img[2] = {makeImage("OutOfResourcesKernel1"),
makeImage("OutOfResourcesKernel2")};
static sycl::unittest::PiImage Img[2] = {
sycl::unittest::generateDefaultImage({"OutOfResourcesKernel1"}),
sycl::unittest::generateDefaultImage({"OutOfResourcesKernel2"})};

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

Expand Down
24 changes: 3 additions & 21 deletions sycl/unittests/pipes/host_pipe_registration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,13 @@
#include <detail/device_binary_image.hpp>
#include <detail/host_pipe_map_entry.hpp>
#include <gtest/gtest.h>
#include <helpers/MockKernelInfo.hpp>
#include <helpers/PiImage.hpp>
#include <helpers/PiMock.hpp>
#include <sycl/detail/host_pipe_map.hpp>

template <size_t KernelSize = 1> class TestKernel;

namespace sycl {
inline namespace _V1 {
namespace detail {
template <size_t KernelSize> struct KernelInfo<TestKernel<KernelSize>> {
static constexpr unsigned getNumParams() { return 0; }
static const kernel_param_desc_t &getParamDesc(int) {
static kernel_param_desc_t Dummy;
return Dummy;
}
static constexpr const char *getName() { return "TestKernel"; }
static constexpr bool isESIMD() { return false; }
static constexpr bool callsThisItem() { return false; }
static constexpr bool callsAnyThisFreeFunction() { return false; }
static constexpr int64_t getKernelSize() { return KernelSize; }
};

} // namespace detail
} // namespace _V1
} // namespace sycl
class TestKernel;
MOCK_INTEGRATION_HEADER(TestKernel)

using namespace sycl;
using default_pipe_properties =
Expand Down
Loading

0 comments on commit 625ade1

Please sign in to comment.