Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SYCL] Improve is_compatible #9769

Merged
merged 10 commits into from
Jun 14, 2023
52 changes: 46 additions & 6 deletions sycl/source/kernel_bundle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,52 @@ std::vector<kernel_id> get_kernel_ids() {
}

bool is_compatible(const std::vector<kernel_id> &KernelIDs, const device &Dev) {
std::set<detail::RTDeviceBinaryImage *> BinImages =
detail::ProgramManager::getInstance().getRawDeviceImages(KernelIDs);
return std::all_of(BinImages.begin(), BinImages.end(),
[&Dev](const detail::RTDeviceBinaryImage *Img) {
return doesDevSupportDeviceRequirements(Dev, *Img);
});
if (KernelIDs.empty())
return false;
KornevNikita marked this conversation as resolved.
Show resolved Hide resolved
// TODO: also need to check architectures matching
auto doesImageTargetMatchDevice = [](const device &Dev,
const detail::RTDeviceBinaryImage &Img) {
const char *Target = Img.getRawData().DeviceTargetSpec;
auto BE = Dev.get_backend();
if (strcmp(Target, __SYCL_PI_DEVICE_BINARY_TARGET_SPIRV64) == 0) {
return (BE == sycl::backend::opencl ||
KornevNikita marked this conversation as resolved.
Show resolved Hide resolved
BE == sycl::backend::ext_oneapi_level_zero);
} else if (strcmp(Target, __SYCL_PI_DEVICE_BINARY_TARGET_SPIRV64_X86_64) ==
0) {
return Dev.is_cpu();
} else if (strcmp(Target, __SYCL_PI_DEVICE_BINARY_TARGET_SPIRV64_GEN) ==
0) {
return Dev.is_gpu() && (BE == sycl::backend::opencl ||
dm-vodopyanov marked this conversation as resolved.
Show resolved Hide resolved
BE == sycl::backend::ext_oneapi_level_zero);
} else if (strcmp(Target, __SYCL_PI_DEVICE_BINARY_TARGET_SPIRV64_FPGA) ==
0) {
return Dev.is_accelerator();
} else if (strcmp(Target, __SYCL_PI_DEVICE_BINARY_TARGET_NVPTX64) == 0) {
return BE == sycl::backend::ext_oneapi_cuda;
} else if (strcmp(Target, __SYCL_PI_DEVICE_BINARY_TARGET_AMDGCN) == 0) {
return BE == sycl::backend::ext_oneapi_hip;
}

return false;
};

// One kernel may be contained in several binary images depending on the
KornevNikita marked this conversation as resolved.
Show resolved Hide resolved
// number of targets. This kernel is compatible with the device if there is
// at least one image (containing this kernel) whose aspects are supported by
// the device and whose target matches the device.
for (const auto &KernelID : KernelIDs) {
std::set<detail::RTDeviceBinaryImage *> BinImages =
detail::ProgramManager::getInstance().getRawDeviceImages({KernelID});

if (std::none_of(BinImages.begin(), BinImages.end(),
[&](const detail::RTDeviceBinaryImage *Img) {
return doesDevSupportDeviceRequirements(Dev, *Img) &&
doesImageTargetMatchDevice(Dev, *Img);
}))
return false;
}

return true;
}

} // __SYCL_INLINE_VER_NAMESPACE(_V1)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <sycl/sycl.hpp>

int main() {
sycl::device dev;
// Should not throw any exception as it should only run on the specific
// target device, defined during compilation.
if (sycl::is_compatible<class Kernel>(dev)) {
sycl::queue q(dev);
q.submit([&](sycl::handler &cgh) {
cgh.parallel_for<class Kernel>(sycl::range<1>{1},
[=](sycl::id<1> Id) { int x = Id[0]; });
}).wait_and_throw();
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// REQUIRES: cpu, gpu, accelerator

// RUN: %clangxx -fsycl -fsycl-targets=spir64_fpga %S/Inputs/is_compatible_with_env.cpp -o %t.out
// RUN: env ONEAPI_DEVICE_SELECTOR=opencl:fpga %{run} %t.out
// RUN: env ONEAPI_DEVICE_SELECTOR=opencl:gpu %{run} %t.out
// RUN: env ONEAPI_DEVICE_SELECTOR=opencl:cpu %{run} %t.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// REQUIRES: ocloc, gpu, level_zero, opencl

// RUN: %clangxx -fsycl -fsycl-targets=spir64_gen -Xsycl-target-backend "-device *" %S/Inputs/is_compatible_with_env.cpp -o %t.out
// RUN: env ONEAPI_DEVICE_SELECTOR=opencl:gpu %{run} %t.out
// RUN: env ONEAPI_DEVICE_SELECTOR=level_zerp:gpu %{run} %t.out
// RUN: env ONEAPI_DEVICE_SELECTOR=opencl:cpu %{run} %t.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// REQUIRES: gpu, level_zero, opencl
KornevNikita marked this conversation as resolved.
Show resolved Hide resolved

// RUN: %clangxx -fsycl -fsycl-targets=spir64_x86_64 %S/Inputs/is_compatible_with_env.cpp -o %t.out
// RUN: env ONEAPI_DEVICE_SELECTOR=opencl:cpu %{run} %t.out
// RUN: env ONEAPI_DEVICE_SELECTOR=level_zero:gpu %{run} %t.out
// RUN: env ONEAPI_DEVICE_SELECTOR=opencl:gpu %{run} %t.out