Skip to content

Commit

Permalink
[SYCL][NFC] Simplify working with mock images in unit tests (#14816)
Browse files Browse the repository at this point in the history
Most of our unit-tests only care about getting a device image with
certain kernels and certain properties regardless of how other fields
are set.

This patch introduced a new constructor to UrImage to simplify
unit-tests code. Also it cleaned up some includes and used existing
helpers where possible to generate device images for tests.
  • Loading branch information
AlexeySachkov authored Oct 11, 2024
1 parent 66256ef commit de5e4ee
Show file tree
Hide file tree
Showing 20 changed files with 44 additions and 177 deletions.
20 changes: 2 additions & 18 deletions sycl/unittests/Extensions/DeviceGlobal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,10 @@ static sycl::unittest::UrImage generateDeviceGlobalImage() {
PropSet.insert(__SYCL_PROPERTY_SET_SYCL_DEVICE_GLOBALS,
std::vector<UrProperty>{std::move(DevGlobInfo)});

std::vector<unsigned char> Bin{10, 11, 12, 13, 14, 15}; // Random data

std::vector<UrOffloadEntry> Entries =
makeEmptyKernels({DeviceGlobalTestKernelName});

UrImage Img{SYCL_DEVICE_BINARY_TYPE_SPIRV, // Format
__SYCL_DEVICE_BINARY_TARGET_SPIRV64, // DeviceTargetSpec
"", // Compile options
"", // Link options
std::move(Bin),
std::move(Entries),
std::move(PropSet)};
UrImage Img(std::move(Entries), std::move(PropSet));

return Img;
}
Expand All @@ -98,18 +90,10 @@ static sycl::unittest::UrImage generateDeviceGlobalImgScopeImage() {
PropSet.insert(__SYCL_PROPERTY_SET_SYCL_DEVICE_GLOBALS,
std::vector<UrProperty>{std::move(DevGlobInfo)});

std::vector<unsigned char> Bin{10, 11, 12, 13, 14, 15}; // Random data

std::vector<UrOffloadEntry> Entries =
makeEmptyKernels({DeviceGlobalImgScopeTestKernelName});

UrImage Img{SYCL_DEVICE_BINARY_TYPE_SPIRV, // Format
__SYCL_DEVICE_BINARY_TARGET_SPIRV64, // DeviceTargetSpec
"", // Compile options
"", // Link options
std::move(Bin),
std::move(Entries),
std::move(PropSet)};
UrImage Img(std::move(Entries), std::move(PropSet));

return Img;
}
Expand Down
26 changes: 3 additions & 23 deletions sycl/unittests/Extensions/USMMemcpy2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,30 +125,10 @@ struct KernelInfo<class __usmmemcpy2d<unsigned char>>
} // namespace _V1
} // namespace sycl

static sycl::unittest::UrImage generateMemopsImage() {
using namespace sycl::unittest;

UrPropertySet PropSet;

std::vector<unsigned char> Bin{10, 11, 12, 13, 14, 15}; // Random data

std::vector<UrOffloadEntry> Entries = makeEmptyKernels(
{USMFillHelperKernelNameLong, USMFillHelperKernelNameChar,
USMMemcpyHelperKernelNameLong, USMMemcpyHelperKernelNameChar});

UrImage Img{SYCL_DEVICE_BINARY_TYPE_SPIRV, // Format
__SYCL_DEVICE_BINARY_TARGET_SPIRV64, // DeviceTargetSpec
"", // Compile options
"", // Link options
std::move(Bin),
std::move(Entries),
std::move(PropSet)};

return Img;
}

namespace {
sycl::unittest::UrImage Imgs[] = {generateMemopsImage()};
sycl::unittest::UrImage Imgs[] = {sycl::unittest::generateDefaultImage(
{USMFillHelperKernelNameLong, USMFillHelperKernelNameChar,
USMMemcpyHelperKernelNameLong, USMMemcpyHelperKernelNameChar})};
sycl::unittest::UrImageArray<1> ImgArray{Imgs};

ur_context_info_t LastMemopsQuery = UR_CONTEXT_INFO_NUM_DEVICES;
Expand Down
1 change: 0 additions & 1 deletion sycl/unittests/SYCL2020/DeviceGetInfoAspects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include <sycl/sycl.hpp>

#include <helpers/UrImage.hpp>
#include <helpers/UrMock.hpp>

#include <gtest/gtest.h>
Expand Down
14 changes: 3 additions & 11 deletions sycl/unittests/SYCL2020/IsCompatible.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,15 @@ MOCK_INTEGRATION_HEADER(TestKernelACC)

static sycl::unittest::UrImage
generateDefaultImage(std::initializer_list<std::string> KernelNames,
const std::vector<sycl::aspect> &Aspects, const std::vector<int> &ReqdWGSize = {}) {
const std::vector<sycl::aspect> &Aspects,
const std::vector<int> &ReqdWGSize = {}) {
using namespace sycl::unittest;

UrPropertySet PropSet;
addDeviceRequirementsProps(PropSet, Aspects, ReqdWGSize);

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

std::vector<UrOffloadEntry> Entries = makeEmptyKernels(KernelNames);

UrImage Img{SYCL_DEVICE_BINARY_TYPE_SPIRV, // Format
__SYCL_DEVICE_BINARY_TARGET_SPIRV64, // DeviceTargetSpec
"", // Compile options
"", // Link options
std::move(Bin),
std::move(Entries),
std::move(PropSet)};
UrImage Img(std::move(Entries), std::move(PropSet));

return Img;
}
Expand Down
26 changes: 3 additions & 23 deletions sycl/unittests/SYCL2020/KernelID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,10 @@ struct KernelInfo<ServiceKernel1> : public unittest::MockKernelInfoBase {
} // namespace _V1
} // namespace sycl

static sycl::unittest::UrImage
generateDefaultImage(std::initializer_list<std::string> Kernels) {
using namespace sycl::unittest;

UrPropertySet PropSet;

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

std::vector<UrOffloadEntry> Entries = makeEmptyKernels(Kernels);

UrImage Img{SYCL_DEVICE_BINARY_TYPE_SPIRV, // Format
__SYCL_DEVICE_BINARY_TARGET_SPIRV64, // DeviceTargetSpec
"", // Compile options
"", // Link options
std::move(Bin),
std::move(Entries),
std::move(PropSet)};

return Img;
}

static sycl::unittest::UrImage Imgs[2] = {
generateDefaultImage({"KernelID_TestKernel1", "KernelID_TestKernel3"}),
generateDefaultImage(
sycl::unittest::generateDefaultImage(
{"KernelID_TestKernel1", "KernelID_TestKernel3"}),
sycl::unittest::generateDefaultImage(
{"KernelID_TestKernel2",
"_ZTSN2cl4sycl6detail23__sycl_service_kernel__14ServiceKernel1"})};
static sycl::unittest::UrImageArray<2> ImgArray{Imgs};
Expand Down
10 changes: 1 addition & 9 deletions sycl/unittests/SYCL2020/SpecializationConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,9 @@ static sycl::unittest::UrImage generateImageWithSpecConsts() {
UrPropertySet PropSet;
addSpecConstants({SC1, SC2}, std::move(SpecConstData), PropSet);

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

std::vector<UrOffloadEntry> Entries =
makeEmptyKernels({"SpecializationConstant_TestKernel"});
UrImage Img{SYCL_DEVICE_BINARY_TYPE_SPIRV, // Format
__SYCL_DEVICE_BINARY_TARGET_SPIRV64, // DeviceTargetSpec
"", // Compile options
"", // Link options
std::move(Bin),
std::move(Entries),
std::move(PropSet)};
UrImage Img(std::move(Entries), std::move(PropSet));

return Img;
}
Expand Down
1 change: 0 additions & 1 deletion sycl/unittests/accessor/AccessorPlaceholder.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <gtest/gtest.h>

#include <helpers/UrImage.hpp>
#include <helpers/UrMock.hpp>
#include <numeric>
#include <sycl/sycl.hpp>
Expand Down
20 changes: 2 additions & 18 deletions sycl/unittests/assert/assert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,9 @@ static sycl::unittest::UrImage generateDefaultImage() {

setKernelUsesAssert({KernelName}, PropSet);

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

std::vector<UrOffloadEntry> Entries = makeEmptyKernels({KernelName});

UrImage Img{SYCL_DEVICE_BINARY_TYPE_SPIRV, // Format
__SYCL_DEVICE_BINARY_TARGET_SPIRV64, // DeviceTargetSpec
"", // Compile options
"", // Link options
std::move(Bin),
std::move(Entries),
std::move(PropSet)};
UrImage Img(std::move(Entries), std::move(PropSet));

return Img;
}
Expand All @@ -107,17 +99,9 @@ static sycl::unittest::UrImage generateCopierKernelImage() {

UrPropertySet PropSet;

std::vector<unsigned char> Bin{10, 11, 12, 13, 14, 15}; // Random data

std::vector<UrOffloadEntry> Entries = makeEmptyKernels({CopierKernelName});

UrImage Img{SYCL_DEVICE_BINARY_TYPE_SPIRV, // Format
__SYCL_DEVICE_BINARY_TARGET_SPIRV64, // DeviceTargetSpec
"", // Compile options
"", // Link options
std::move(Bin),
std::move(Entries),
std::move(PropSet)};
UrImage Img(std::move(Entries), std::move(PropSet));

return Img;
}
Expand Down
24 changes: 23 additions & 1 deletion sycl/unittests/helpers/UrImage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,29 @@ class UrImage {
internal::LifetimeExtender(std::move(OffloadEntries)),
std::move(PropertySet)) {}

/// Constructs a mock SYCL device image with:
/// - the latest version
/// - SPIR-V format
/// - empty compile and link options
/// - placeholder binary data
UrImage(std::vector<UrOffloadEntry> &&OffloadEntries,
UrPropertySet PropertySet)
: UrImage(
SYCL_DEVICE_BINARY_VERSION, SYCL_DEVICE_BINARY_OFFLOAD_KIND_SYCL,
SYCL_DEVICE_BINARY_TYPE_SPIRV, __SYCL_DEVICE_BINARY_TARGET_SPIRV64,
"", "", {}, std::move(std::vector<unsigned char>{1, 2, 3, 4, 5}),
internal::LifetimeExtender(std::move(OffloadEntries)),
std::move(PropertySet)) {}

/// Constructs a mock SYCL device image with:
/// - the latest version
/// - SPIR-V format
/// - empty compile and link options
/// - placeholder binary data
/// - no properties
UrImage(std::vector<UrOffloadEntry> &&OffloadEntries)
: UrImage(std::move(OffloadEntries), {}) {}

sycl_device_binary_struct convertToNativeType() {
return sycl_device_binary_struct{
MVersion,
Expand Down Expand Up @@ -575,7 +598,6 @@ generateDefaultImage(std::initializer_list<std::string> KernelNames) {
std::move(Bin),
std::move(Entries),
std::move(PropSet)};

return Img;
}

Expand Down
10 changes: 1 addition & 9 deletions sycl/unittests/kernel-and-program/Cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,10 @@ static sycl::unittest::UrImage generateDefaultImage() {
UrPropertySet PropSet;
addSpecConstants({SC1}, std::move(SpecConstData), PropSet);

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

std::vector<UrOffloadEntry> Entries =
makeEmptyKernels({"CacheTestKernel", "CacheTestKernel2"});

UrImage Img{SYCL_DEVICE_BINARY_TYPE_SPIRV, // Format
__SYCL_DEVICE_BINARY_TARGET_SPIRV64, // DeviceTargetSpec
"", // Compile options
"", // Link options
std::move(Bin),
std::move(Entries),
std::move(PropSet)};
UrImage Img(std::move(Entries), std::move(PropSet));

return Img;
}
Expand Down
10 changes: 1 addition & 9 deletions sycl/unittests/pipes/host_pipe_registration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,9 @@ static sycl::unittest::UrImage generateDefaultImage() {
PropSet.insert(__SYCL_PROPERTY_SET_SYCL_HOST_PIPES,
std::vector<UrProperty>{std::move(HostPipeInfo)});

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

std::vector<UrOffloadEntry> Entries = makeEmptyKernels({"TestKernel"});

UrImage Img{SYCL_DEVICE_BINARY_TYPE_SPIRV, // Format
__SYCL_DEVICE_BINARY_TARGET_SPIRV64, // DeviceTargetSpec
"", // Compile options
"", // Link options
std::move(Bin),
std::move(Entries),
std::move(PropSet)};
UrImage Img(std::move(Entries), std::move(PropSet));

return Img;
}
Expand Down
1 change: 0 additions & 1 deletion sycl/unittests/program_manager/BuildLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <detail/context_impl.hpp>
#include <detail/program_manager/program_manager.hpp>
#include <helpers/ScopedEnvVar.hpp>
#include <helpers/UrImage.hpp>
#include <helpers/UrMock.hpp>
#include <sycl/sycl.hpp>

Expand Down
1 change: 0 additions & 1 deletion sycl/unittests/program_manager/SubDevices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include <detail/kernel_bundle_impl.hpp>

#include <helpers/UrImage.hpp>
#include <helpers/UrMock.hpp>

#include <gtest/gtest.h>
Expand Down
43 changes: 4 additions & 39 deletions sycl/unittests/program_manager/arg_mask/EliminatedArgMask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,37 +63,9 @@ static sycl::unittest::UrImage generateEAMTestKernelImage() {
UrPropertySet PropSet;
PropSet.insert(__SYCL_PROPERTY_SET_KERNEL_PARAM_OPT_INFO, std::move(ImgKPOI));

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

std::vector<UrOffloadEntry> Entries = makeEmptyKernels({EAMTestKernelName});

UrImage Img{SYCL_DEVICE_BINARY_TYPE_SPIRV, // Format
__SYCL_DEVICE_BINARY_TARGET_SPIRV64, // DeviceTargetSpec
"", // Compile options
"", // Link options
std::move(Bin),
std::move(Entries),
std::move(PropSet)};

return Img;
}

static sycl::unittest::UrImage generateEAMTestKernel2Image() {
using namespace sycl::unittest;

UrPropertySet PropSet;

std::vector<unsigned char> Bin{6, 7, 8, 9, 10, 11}; // Random data

std::vector<UrOffloadEntry> Entries = makeEmptyKernels({EAMTestKernel2Name});

UrImage Img{SYCL_DEVICE_BINARY_TYPE_SPIRV, // Format
__SYCL_DEVICE_BINARY_TARGET_SPIRV64, // DeviceTargetSpec
"", // Compile options
"", // Link options
std::move(Bin),
std::move(Entries),
std::move(PropSet)};
UrImage Img(std::move(Entries), std::move(PropSet));

return Img;
}
Expand All @@ -110,23 +82,16 @@ static sycl::unittest::UrImage generateEAMTestKernel3Image() {
UrPropertySet PropSet;
PropSet.insert(__SYCL_PROPERTY_SET_KERNEL_PARAM_OPT_INFO, std::move(ImgKPOI));

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

std::vector<UrOffloadEntry> Entries = makeEmptyKernels({EAMTestKernel3Name});

UrImage Img{SYCL_DEVICE_BINARY_TYPE_SPIRV, // Format
__SYCL_DEVICE_BINARY_TARGET_SPIRV64, // DeviceTargetSpec
"", // Compile options
"", // Link options
std::move(Bin),
std::move(Entries),
std::move(PropSet)};
UrImage Img(std::move(Entries), std::move(PropSet));

return Img;
}

static sycl::unittest::UrImage EAMImg = generateEAMTestKernelImage();
static sycl::unittest::UrImage EAM2Img = generateEAMTestKernel2Image();
static sycl::unittest::UrImage EAM2Img =
sycl::unittest::generateDefaultImage({EAMTestKernel2Name});
static sycl::unittest::UrImage EAM3Img = generateEAMTestKernel3Image();
static sycl::unittest::UrImageArray<1> EAMImgArray{&EAMImg};
static sycl::unittest::UrImageArray<1> EAM2ImgArray{&EAM2Img};
Expand Down
1 change: 0 additions & 1 deletion sycl/unittests/program_manager/itt_annotations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include <detail/config.hpp>
#include <detail/program_manager/program_manager.hpp>
#include <helpers/UrImage.hpp>
#include <helpers/UrMock.hpp>
#include <sycl/sycl.hpp>

Expand Down
9 changes: 1 addition & 8 deletions sycl/unittests/scheduler/CommandsWaitForEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,11 @@ static sycl::unittest::UrImage generateDefaultImage() {

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

std::vector<UrOffloadEntry> Entries =
makeEmptyKernels({"StreamAUXCmdsWait_TestKernel"});

UrImage Img{SYCL_DEVICE_BINARY_TYPE_SPIRV, // Format
__SYCL_DEVICE_BINARY_TARGET_SPIRV64, // DeviceTargetSpec
"", // Compile options
"", // Link options
std::move(Bin),
std::move(Entries),
std::move(PropSet)};
UrImage Img(std::move(Entries), std::move(PropSet));

return Img;
}
Expand Down
Loading

0 comments on commit de5e4ee

Please sign in to comment.