Skip to content

Commit 6143388

Browse files
authored
[SYCL] Throw for invalid global_work_size query (#9072)
SYCL2020: This descriptor may only be used if the device type is device_type::custom or if the kernel is a built-in kernel. Attempting to use this descriptor for other devices or kernels throws an exception with the errc::invalid error code. The previous implementation is based on handling OpenCL exception, but the other backends don't throw anything, so it works only on OCL. This approch is more generic.
1 parent 1dee8c0 commit 6143388

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

sycl/source/detail/kernel_impl.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,18 @@ inline context kernel_impl::get_info<info::kernel::context>() const {
214214
template <typename Param>
215215
inline typename Param::return_type
216216
kernel_impl::get_info(const device &Device) const {
217+
if constexpr (std::is_same_v<
218+
Param, info::kernel_device_specific::global_work_size>) {
219+
bool isDeviceCustom = Device.get_info<info::device::device_type>() ==
220+
info::device_type::custom;
221+
if (!isDeviceCustom && !isBuiltInKernel(Device))
222+
throw exception(
223+
sycl::make_error_code(errc::invalid),
224+
"info::kernel_device_specific::global_work_size descriptor may only "
225+
"be used if the device type is device_type::custom or if the kernel "
226+
"is a built-in kernel.");
227+
}
228+
217229
if (is_host()) {
218230
return get_kernel_device_specific_info_host<Param>(Device);
219231
}

sycl/test-e2e/Basic/kernel_info.cpp

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,34 @@ int main() {
8282
krn.get_info<info::kernel_device_specific::compile_num_sub_groups>(dev);
8383
assert(compileNumSg <= maxNumSg);
8484

85-
try {
86-
// To check (a) first if the kernel is device built-in, (b) then check if
87-
// the device type is custom
88-
if (!sycl::is_compatible({KernelID}, q.get_device())) {
89-
assert(dev.get_info<sycl::info::device::device_type>() ==
90-
sycl::info::device_type::custom);
91-
}
85+
{
86+
std::error_code Errc;
87+
std::string ErrMsg = "";
88+
bool IsExceptionThrown = false;
89+
try {
90+
krn.get_info<sycl::info::kernel_device_specific::global_work_size>(dev);
91+
auto BuiltInIds = dev.get_info<info::device::built_in_kernel_ids>();
92+
bool isBuiltInKernel = std::find(BuiltInIds.begin(), BuiltInIds.end(),
93+
KernelID) != BuiltInIds.end();
94+
bool isCustomDevice = dev.get_info<sycl::info::device::device_type>() ==
95+
sycl::info::device_type::custom;
96+
assert((isCustomDevice || isBuiltInKernel) &&
97+
"info::kernel_device_specific::global_work_size descriptor can "
98+
"only be used with custom device "
99+
"or built-in kernel.");
92100

93-
krn.get_info<sycl::info::kernel_device_specific::global_work_size>(dev);
94-
} catch (sycl::exception &e) {
95-
assert(e.code() == sycl::errc::invalid);
101+
} catch (sycl::exception &e) {
102+
IsExceptionThrown = true;
103+
Errc = e.code();
104+
ErrMsg = e.what();
105+
}
106+
assert(IsExceptionThrown &&
107+
"Invalid using of info::kernel_device_specific::global_work_size "
108+
"query should throw an exception.");
109+
assert(Errc == errc::invalid);
110+
assert(ErrMsg ==
111+
"info::kernel_device_specific::global_work_size descriptor may only "
112+
"be used if the device type is device_type::custom or if the "
113+
"kernel is a built-in kernel.");
96114
}
97115
}

0 commit comments

Comments
 (0)