Skip to content

Commit f697799

Browse files
committed
Fix test cases checking for exception
1 parent a3bddec commit f697799

File tree

1 file changed

+53
-65
lines changed

1 file changed

+53
-65
lines changed

sycl/test-e2e/Basic/kernel_info.cpp

Lines changed: 53 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,31 @@
1616
using namespace sycl;
1717
namespace syclex = sycl::ext::oneapi;
1818

19+
auto checkExceptionIsThrown = [](auto &getInfoFunc,
20+
const std::string &refErrMsg,
21+
std::error_code refErrc) {
22+
std::string errMsg = "";
23+
std::error_code errc;
24+
bool exceptionWasThrown = false;
25+
try {
26+
std::ignore = getInfoFunc();
27+
} catch (exception &e) {
28+
errMsg = e.what();
29+
errc = e.code();
30+
exceptionWasThrown = true;
31+
}
32+
assert(exceptionWasThrown);
33+
assert(errMsg == refErrMsg);
34+
assert(errc == refErrc);
35+
};
36+
1937
int main() {
2038
queue q;
2139
auto ctx = q.get_context();
2240
buffer<int, 1> buf(range<1>(1));
23-
auto KernelID = sycl::get_kernel_id<class SingleTask>();
24-
auto KB = get_kernel_bundle<bundle_state::executable>(ctx, {KernelID});
25-
kernel krn = KB.get_kernel(KernelID);
41+
auto kernelID = sycl::get_kernel_id<class SingleTask>();
42+
auto kb = get_kernel_bundle<bundle_state::executable>(ctx, {kernelID});
43+
kernel krn = kb.get_kernel(kernelID);
2644

2745
q.submit([&](handler &cgh) {
2846
auto acc = buf.get_access<access::mode::read_write>(cgh);
@@ -32,27 +50,19 @@ int main() {
3250
const std::string krnName = krn.get_info<info::kernel::function_name>();
3351
assert(!krnName.empty());
3452

35-
std::string ErrMsg = "";
36-
std::error_code Errc;
37-
bool ExceptionWasThrown = false;
38-
try {
39-
const cl_uint krnArgCount = krn.get_info<info::kernel::num_args>();
40-
// Use ext_oneapi_get_kernel_info extension and check that answers match.
41-
const cl_uint krnArgCountExt =
42-
syclex::get_kernel_info<SingleTask, info::kernel::num_args>(ctx);
43-
assert(krnArgCountExt == krnArgCount);
44-
} catch (exception &e) {
45-
ErrMsg = e.what();
46-
Errc = e.code();
47-
ExceptionWasThrown = true;
48-
}
49-
assert(ExceptionWasThrown && "Invalid using of \"info::kernel::num_args\" "
50-
"query should throw an exception.");
51-
assert(ErrMsg ==
52-
"info::kernel::num_args descriptor may only be used to query a kernel "
53-
"that resides in a kernel bundle constructed using a backend specific"
54-
"interoperability function or to query a device built-in kernel");
55-
assert(Errc == errc::invalid);
53+
auto refErrMsg =
54+
"info::kernel::num_args descriptor may only be used to query a kernel "
55+
"that resides in a kernel bundle constructed using a backend specific"
56+
"interoperability function or to query a device built-in kernel";
57+
auto refErrc = errc::invalid;
58+
auto getInfoNumArgsFunc = [&]() -> cl_uint {
59+
return krn.get_info<info::kernel::num_args>();
60+
};
61+
checkExceptionIsThrown(getInfoNumArgsFunc, refErrMsg, refErrc);
62+
auto getInfoNumArgsFuncExt = [&]() {
63+
return syclex::get_kernel_info<SingleTask, info::kernel::num_args>(ctx);
64+
};
65+
checkExceptionIsThrown(getInfoNumArgsFuncExt, refErrMsg, refErrc);
5666

5767
const context krnCtx = krn.get_info<info::kernel::context>();
5868
assert(krnCtx == q.get_context());
@@ -136,45 +146,23 @@ int main() {
136146
SingleTask, info::kernel_device_specific::compile_num_sub_groups>(q);
137147
assert(compileNumSgExtQ == compileNumSg);
138148

139-
{
140-
std::error_code Errc;
141-
std::string ErrMsg = "";
142-
bool IsExceptionThrown = false;
143-
try {
144-
auto globalWorkSize =
145-
krn.get_info<sycl::info::kernel_device_specific::global_work_size>(
146-
dev);
147-
// Use ext_oneapi_get_kernel_info extension and check that answers match.
148-
auto globalWorkSizeExt = syclex::get_kernel_info<
149-
SingleTask, info::kernel_device_specific::global_work_size>(ctx, dev);
150-
assert(globalWorkSize == globalWorkSizeExt);
151-
// Use ext_oneapi_get_kernel_info extension with queue parameter and check
152-
// the result.
153-
auto globalWorkSizeExtQ = syclex::get_kernel_info<
154-
SingleTask, info::kernel_device_specific::global_work_size>(q);
155-
assert(globalWorkSize == globalWorkSizeExtQ);
156-
auto BuiltInIds = dev.get_info<info::device::built_in_kernel_ids>();
157-
bool isBuiltInKernel = std::find(BuiltInIds.begin(), BuiltInIds.end(),
158-
KernelID) != BuiltInIds.end();
159-
bool isCustomDevice = dev.get_info<sycl::info::device::device_type>() ==
160-
sycl::info::device_type::custom;
161-
assert((isCustomDevice || isBuiltInKernel) &&
162-
"info::kernel_device_specific::global_work_size descriptor can "
163-
"only be used with custom device "
164-
"or built-in kernel.");
165-
166-
} catch (sycl::exception &e) {
167-
IsExceptionThrown = true;
168-
Errc = e.code();
169-
ErrMsg = e.what();
170-
}
171-
assert(IsExceptionThrown &&
172-
"Invalid using of info::kernel_device_specific::global_work_size "
173-
"query should throw an exception.");
174-
assert(Errc == errc::invalid);
175-
assert(ErrMsg ==
176-
"info::kernel_device_specific::global_work_size descriptor may only "
177-
"be used if the device type is device_type::custom or if the "
178-
"kernel is a built-in kernel.");
179-
}
149+
refErrMsg =
150+
"info::kernel_device_specific::global_work_size descriptor may only "
151+
"be used if the device type is device_type::custom or if the "
152+
"kernel is a built-in kernel.";
153+
auto getInfoGWSFunc = [&]() {
154+
return krn.get_info<sycl::info::kernel_device_specific::global_work_size>(
155+
dev);
156+
};
157+
checkExceptionIsThrown(getInfoGWSFunc, refErrMsg, refErrc);
158+
auto getInfoGWSFuncExt = [&]() {
159+
return syclex::get_kernel_info<
160+
SingleTask, info::kernel_device_specific::global_work_size>(ctx, dev);
161+
};
162+
checkExceptionIsThrown(getInfoGWSFuncExt, refErrMsg, refErrc);
163+
auto getInfoGWSFuncExtQ = [&]() {
164+
return syclex::get_kernel_info<
165+
SingleTask, info::kernel_device_specific::global_work_size>(q);
166+
};
167+
checkExceptionIsThrown(getInfoGWSFuncExtQ, refErrMsg, refErrc);
180168
}

0 commit comments

Comments
 (0)