|
| 1 | +//==---- KernelBundleStateFiltering.cpp --- Kernel bundle 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 | +#include <detail/device_impl.hpp> |
| 10 | +#include <detail/kernel_bundle_impl.hpp> |
| 11 | +#include <sycl/sycl.hpp> |
| 12 | + |
| 13 | +#include <helpers/MockKernelInfo.hpp> |
| 14 | +#include <helpers/PiImage.hpp> |
| 15 | +#include <helpers/PiMock.hpp> |
| 16 | + |
| 17 | +#include <gtest/gtest.h> |
| 18 | + |
| 19 | +#include <algorithm> |
| 20 | +#include <set> |
| 21 | +#include <vector> |
| 22 | + |
| 23 | +class KernelA; |
| 24 | +class KernelB; |
| 25 | +class KernelC; |
| 26 | +class KernelD; |
| 27 | +namespace sycl { |
| 28 | +__SYCL_INLINE_VER_NAMESPACE(_V1) { |
| 29 | +namespace detail { |
| 30 | +template <> struct KernelInfo<KernelA> : public unittest::MockKernelInfoBase { |
| 31 | + static constexpr const char *getName() { return "KernelA"; } |
| 32 | +}; |
| 33 | +template <> struct KernelInfo<KernelB> : public unittest::MockKernelInfoBase { |
| 34 | + static constexpr const char *getName() { return "KernelB"; } |
| 35 | +}; |
| 36 | +template <> struct KernelInfo<KernelC> : public unittest::MockKernelInfoBase { |
| 37 | + static constexpr const char *getName() { return "KernelC"; } |
| 38 | +}; |
| 39 | +template <> struct KernelInfo<KernelD> : public unittest::MockKernelInfoBase { |
| 40 | + static constexpr const char *getName() { return "KernelD"; } |
| 41 | +}; |
| 42 | +} // namespace detail |
| 43 | +} // __SYCL_INLINE_VER_NAMESPACE(_V1) |
| 44 | +} // namespace sycl |
| 45 | + |
| 46 | +namespace { |
| 47 | + |
| 48 | +std::set<const void *> TrackedImages; |
| 49 | +sycl::unittest::PiImage |
| 50 | +generateDefaultImage(std::initializer_list<std::string> KernelNames, |
| 51 | + pi_device_binary_type BinaryType, |
| 52 | + const char *DeviceTargetSpec) { |
| 53 | + using namespace sycl::unittest; |
| 54 | + |
| 55 | + PiPropertySet PropSet; |
| 56 | + |
| 57 | + static unsigned char NImage = 0; |
| 58 | + std::vector<unsigned char> Bin{NImage++}; |
| 59 | + |
| 60 | + PiArray<PiOffloadEntry> Entries = makeEmptyKernels(KernelNames); |
| 61 | + |
| 62 | + PiImage Img{BinaryType, // Format |
| 63 | + DeviceTargetSpec, |
| 64 | + "", // Compile options |
| 65 | + "", // Link options |
| 66 | + std::move(Bin), |
| 67 | + std::move(Entries), |
| 68 | + std::move(PropSet)}; |
| 69 | + const void *BinaryPtr = Img.getBinaryPtr(); |
| 70 | + TrackedImages.insert(BinaryPtr); |
| 71 | + |
| 72 | + return Img; |
| 73 | +} |
| 74 | + |
| 75 | +// Image 0: input, KernelA KernelB |
| 76 | +// Image 1: exe, KernelA |
| 77 | +// Image 2: input, KernelC |
| 78 | +// Image 3: exe, KernelC |
| 79 | +// Image 4: input, KernelD |
| 80 | +sycl::unittest::PiImage Imgs[] = { |
| 81 | + generateDefaultImage({"KernelA", "KernelB"}, PI_DEVICE_BINARY_TYPE_SPIRV, |
| 82 | + __SYCL_PI_DEVICE_BINARY_TARGET_SPIRV64), |
| 83 | + generateDefaultImage({"KernelA"}, PI_DEVICE_BINARY_TYPE_NATIVE, |
| 84 | + __SYCL_PI_DEVICE_BINARY_TARGET_SPIRV64_X86_64), |
| 85 | + generateDefaultImage({"KernelC"}, PI_DEVICE_BINARY_TYPE_SPIRV, |
| 86 | + __SYCL_PI_DEVICE_BINARY_TARGET_SPIRV64), |
| 87 | + generateDefaultImage({"KernelC"}, PI_DEVICE_BINARY_TYPE_NATIVE, |
| 88 | + __SYCL_PI_DEVICE_BINARY_TARGET_SPIRV64_X86_64), |
| 89 | + generateDefaultImage({"KernelD"}, PI_DEVICE_BINARY_TYPE_SPIRV, |
| 90 | + __SYCL_PI_DEVICE_BINARY_TARGET_SPIRV64)}; |
| 91 | + |
| 92 | +sycl::unittest::PiImageArray<std::size(Imgs)> ImgArray{Imgs}; |
| 93 | +std::vector<unsigned char> UsedImageIndices; |
| 94 | + |
| 95 | +void redefinedPiProgramCreateCommon(const void *bin) { |
| 96 | + if (TrackedImages.count(bin) != 0) { |
| 97 | + unsigned char ImgIdx = *reinterpret_cast<const unsigned char *>(bin); |
| 98 | + UsedImageIndices.push_back(ImgIdx); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +pi_result redefinedPiProgramCreate(pi_context context, const void *il, |
| 103 | + size_t length, pi_program *res_program) { |
| 104 | + redefinedPiProgramCreateCommon(il); |
| 105 | + return PI_SUCCESS; |
| 106 | +} |
| 107 | + |
| 108 | +pi_result redefinedPiProgramCreateWithBinary( |
| 109 | + pi_context context, pi_uint32 num_devices, const pi_device *device_list, |
| 110 | + const size_t *lengths, const unsigned char **binaries, |
| 111 | + size_t num_metadata_entries, const pi_device_binary_property *metadata, |
| 112 | + pi_int32 *binary_status, pi_program *ret_program) { |
| 113 | + redefinedPiProgramCreateCommon(binaries[0]); |
| 114 | + return PI_SUCCESS; |
| 115 | +} |
| 116 | + |
| 117 | +pi_result redefinedDevicesGet(pi_platform platform, pi_device_type device_type, |
| 118 | + pi_uint32 num_entries, pi_device *devices, |
| 119 | + pi_uint32 *num_devices) { |
| 120 | + if (num_devices) |
| 121 | + *num_devices = 2; |
| 122 | + |
| 123 | + if (devices) { |
| 124 | + devices[0] = reinterpret_cast<pi_device>(1); |
| 125 | + devices[1] = reinterpret_cast<pi_device>(2); |
| 126 | + } |
| 127 | + |
| 128 | + return PI_SUCCESS; |
| 129 | +} |
| 130 | + |
| 131 | +pi_result redefinedExtDeviceSelectBinary(pi_device device, |
| 132 | + pi_device_binary *binaries, |
| 133 | + pi_uint32 num_binaries, |
| 134 | + pi_uint32 *selected_binary_ind) { |
| 135 | + EXPECT_EQ(num_binaries, 1U); |
| 136 | + // Treat image 3 as incompatible with one of the devices. |
| 137 | + if (TrackedImages.count(binaries[0]->BinaryStart) != 0 && |
| 138 | + *binaries[0]->BinaryStart == 3 && |
| 139 | + device == reinterpret_cast<pi_device>(2)) { |
| 140 | + return PI_ERROR_INVALID_BINARY; |
| 141 | + } |
| 142 | + *selected_binary_ind = 0; |
| 143 | + return PI_SUCCESS; |
| 144 | +} |
| 145 | + |
| 146 | +void verifyImageUse(const std::vector<unsigned char> &ExpectedImages) { |
| 147 | + std::sort(UsedImageIndices.begin(), UsedImageIndices.end()); |
| 148 | + EXPECT_TRUE(std::is_sorted(ExpectedImages.begin(), ExpectedImages.end())); |
| 149 | + EXPECT_EQ(UsedImageIndices, ExpectedImages); |
| 150 | + UsedImageIndices.clear(); |
| 151 | +} |
| 152 | + |
| 153 | +TEST(KernelBundle, DeviceImageStateFiltering) { |
| 154 | + sycl::unittest::PiMock Mock; |
| 155 | + Mock.redefineAfter<sycl::detail::PiApiKind::piProgramCreate>( |
| 156 | + redefinedPiProgramCreate); |
| 157 | + Mock.redefineAfter<sycl::detail::PiApiKind::piProgramCreateWithBinary>( |
| 158 | + redefinedPiProgramCreateWithBinary); |
| 159 | + |
| 160 | + // No kernel ids specified. |
| 161 | + { |
| 162 | + const sycl::device Dev = Mock.getPlatform().get_devices()[0]; |
| 163 | + sycl::context Ctx{Dev}; |
| 164 | + |
| 165 | + sycl::kernel_bundle<sycl::bundle_state::executable> KernelBundle = |
| 166 | + sycl::get_kernel_bundle<sycl::bundle_state::executable>(Ctx, {Dev}); |
| 167 | + verifyImageUse({0, 1, 3, 4}); |
| 168 | + } |
| 169 | + |
| 170 | + sycl::kernel_id KernelAID = sycl::get_kernel_id<KernelA>(); |
| 171 | + sycl::kernel_id KernelCID = sycl::get_kernel_id<KernelC>(); |
| 172 | + sycl::kernel_id KernelDID = sycl::get_kernel_id<KernelD>(); |
| 173 | + |
| 174 | + // Request specific kernel ids. |
| 175 | + { |
| 176 | + const sycl::device Dev = Mock.getPlatform().get_devices()[0]; |
| 177 | + sycl::context Ctx{Dev}; |
| 178 | + |
| 179 | + sycl::kernel_bundle<sycl::bundle_state::executable> KernelBundle = |
| 180 | + sycl::get_kernel_bundle<sycl::bundle_state::executable>( |
| 181 | + Ctx, {Dev}, {KernelAID, KernelCID, KernelDID}); |
| 182 | + verifyImageUse({1, 3, 4}); |
| 183 | + } |
| 184 | + |
| 185 | + // Check the case where some executable images are unsupported by one of |
| 186 | + // the devices. |
| 187 | + { |
| 188 | + Mock.redefine<sycl::detail::PiApiKind::piDevicesGet>(redefinedDevicesGet); |
| 189 | + Mock.redefine<sycl::detail::PiApiKind::piextDeviceSelectBinary>( |
| 190 | + redefinedExtDeviceSelectBinary); |
| 191 | + const std::vector<sycl::device> Devs = Mock.getPlatform().get_devices(); |
| 192 | + sycl::context Ctx{Devs}; |
| 193 | + |
| 194 | + sycl::kernel_bundle<sycl::bundle_state::executable> KernelBundle = |
| 195 | + sycl::get_kernel_bundle<sycl::bundle_state::executable>( |
| 196 | + Ctx, Devs, {KernelAID, KernelCID, KernelDID}); |
| 197 | + verifyImageUse({1, 2, 3, 4}); |
| 198 | + } |
| 199 | +} |
| 200 | +} // namespace |
0 commit comments