Skip to content

Commit b824beb

Browse files
committed
[SYCL] Implement more verbose error handling.
This ensures that no exception is silently ignored by sycl. This also includes API names and error codes in the error messages. Signed-off-by: rehana begam <[email protected]>
1 parent 9778952 commit b824beb

File tree

11 files changed

+83
-17
lines changed

11 files changed

+83
-17
lines changed

sycl/include/CL/sycl/detail/common.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ static inline std::string codeToString(cl_int code) {
104104
/* ":" __SYCL_STRINGIFY_LINE(__LINE__) ": " */ \
105105
"Native API returns: "
106106

107+
#define __SYCL_REPORT_PLUGIN_ERR(expr, name, exc) \
108+
{ \
109+
auto code = expr; \
110+
if (code != PI_SUCCESS) { \
111+
throw exc(name + " API failed with error: " + \
112+
cl::sycl::detail::codeToString(code), \
113+
code); \
114+
} \
115+
}
116+
107117
#ifndef __SYCL_SUPPRESS_OCL_ERROR_REPORT
108118
#include <iostream>
109119
// TODO: rename all names with direct use of OCL/OPENCL to be backend agnostic.

sycl/source/detail/device_info.hpp

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,11 @@ template <> struct get_device_info<bool, info::device::usm_device_allocations> {
969969
dev, pi::cast<RT::PiDeviceInfo>(info::device::usm_device_allocations),
970970
sizeof(pi_usm_capabilities), &caps, nullptr);
971971

972-
return (Err != PI_SUCCESS) ? false : (caps & PI_USM_ACCESS);
972+
if (Err != PI_SUCCESS) {
973+
Plugin.reportPiError(Err, "piDeviceGetInfo()");
974+
}
975+
976+
return (caps & PI_USM_ACCESS);
973977
}
974978
};
975979

@@ -981,7 +985,11 @@ template <> struct get_device_info<bool, info::device::usm_host_allocations> {
981985
dev, pi::cast<RT::PiDeviceInfo>(info::device::usm_host_allocations),
982986
sizeof(pi_usm_capabilities), &caps, nullptr);
983987

984-
return (Err != PI_SUCCESS) ? false : (caps & PI_USM_ACCESS);
988+
if (Err != PI_SUCCESS) {
989+
Plugin.reportPiError(Err, "piDeviceGetInfo()");
990+
}
991+
992+
return (caps & PI_USM_ACCESS);
985993
}
986994
};
987995

@@ -992,7 +1000,12 @@ template <> struct get_device_info<bool, info::device::usm_shared_allocations> {
9921000
pi_result Err = Plugin.call_nocheck<PiApiKind::piDeviceGetInfo>(
9931001
dev, pi::cast<RT::PiDeviceInfo>(info::device::usm_shared_allocations),
9941002
sizeof(pi_usm_capabilities), &caps, nullptr);
995-
return (Err != PI_SUCCESS) ? false : (caps & PI_USM_ACCESS);
1003+
1004+
if (Err != PI_SUCCESS) {
1005+
Plugin.reportPiError(Err, "piDeviceGetInfo()");
1006+
}
1007+
1008+
return (caps & PI_USM_ACCESS);
9961009
}
9971010
};
9981011

@@ -1007,9 +1020,11 @@ struct get_device_info<bool, info::device::usm_restricted_shared_allocations> {
10071020
info::device::usm_restricted_shared_allocations),
10081021
sizeof(pi_usm_capabilities), &caps, nullptr);
10091022
// Check that we don't support any cross device sharing
1010-
return (Err != PI_SUCCESS)
1011-
? false
1012-
: !(caps & (PI_USM_ACCESS | PI_USM_CONCURRENT_ACCESS));
1023+
if (Err != PI_SUCCESS) {
1024+
Plugin.reportPiError(Err, "piDeviceGetInfo()");
1025+
}
1026+
1027+
return !(caps & (PI_USM_ACCESS | PI_USM_CONCURRENT_ACCESS));
10131028
}
10141029
};
10151030

@@ -1020,7 +1035,12 @@ template <> struct get_device_info<bool, info::device::usm_system_allocator> {
10201035
pi_result Err = Plugin.call_nocheck<PiApiKind::piDeviceGetInfo>(
10211036
dev, pi::cast<RT::PiDeviceInfo>(info::device::usm_system_allocator),
10221037
sizeof(pi_usm_capabilities), &caps, nullptr);
1023-
return (Err != PI_SUCCESS) ? false : (caps & PI_USM_ACCESS);
1038+
1039+
if (Err != PI_SUCCESS) {
1040+
Plugin.reportPiError(Err, "piDeviceGetInfo()");
1041+
}
1042+
1043+
return (caps & PI_USM_ACCESS);
10241044
}
10251045
};
10261046

@@ -1031,7 +1051,12 @@ template <> struct get_device_info<bool, info::device::ext_intel_mem_channel> {
10311051
pi_result Err = Plugin.call_nocheck<PiApiKind::piDeviceGetInfo>(
10321052
dev, pi::cast<RT::PiDeviceInfo>(info::device::ext_intel_mem_channel),
10331053
sizeof(pi_mem_properties), &caps, nullptr);
1034-
return (Err != PI_SUCCESS) ? false : (caps & PI_MEM_PROPERTIES_CHANNEL);
1054+
1055+
if (Err != PI_SUCCESS) {
1056+
Plugin.reportPiError(Err, "piDeviceGetInfo()");
1057+
}
1058+
1059+
return (caps & PI_MEM_PROPERTIES_CHANNEL);
10351060
}
10361061
};
10371062

sycl/source/detail/global_handler.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,13 @@ void shutdown() {
140140
// some parameters in the plugin tear-down process.
141141
// Currently, it is not used.
142142
void *PluginParameter = nullptr;
143-
Plugin.call_nocheck<PiApiKind::piTearDown>(PluginParameter);
143+
auto Error = Plugin.call_nocheck<PiApiKind::piTearDown>(
144+
PluginParameter);
145+
146+
if (Error != PI_SUCCESS) {
147+
Plugin.reportPiError(Error, "piTearDown()");
148+
}
149+
144150
Plugin.unload();
145151
}
146152
GlobalHandler::instance().MPlugins.reset(nullptr);

sycl/source/detail/memory_manager.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ void *MemoryManager::allocateMemSubBuffer(ContextImplPtr TargetContext,
230230
"Specified offset of the sub-buffer being constructed is not a "
231231
"multiple of the memory base address alignment",
232232
PI_INVALID_VALUE);
233+
234+
if (Error != PI_SUCCESS) {
235+
Plugin.reportPiError(Error, "piMemBufferPartition()");
236+
}
237+
233238
return NewMem;
234239
}
235240

sycl/source/detail/pi.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,12 @@ void contextSetExtendedDeleter(const cl::sycl::context &context,
141141
auto impl = getSyclObjImpl(context);
142142
auto contextHandle = reinterpret_cast<pi_context>(impl->getHandleRef());
143143
auto plugin = impl->getPlugin();
144-
plugin.call_nocheck<PiApiKind::piextContextSetExtendedDeleter>(
145-
contextHandle, func, user_data);
144+
auto Error = plugin.call_nocheck<PiApiKind::piextContextSetExtendedDeleter>(
145+
contextHandle, func, user_data);
146+
147+
if (Error != PI_SUCCESS) {
148+
plugin.reportPiError(Error, "piextContextSetExtendedDeleter()");
149+
}
146150
}
147151

148152
std::string platformInfoToString(pi_platform_info info) {

sycl/source/detail/plugin.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ class plugin {
6666
/// \endcode
6767
///
6868
/// \sa plugin::checkPiResult
69+
70+
template <typename Exception = cl::sycl::runtime_error>
71+
void reportPiError(RT::PiResult pi_result, string_class api_name) const {
72+
__SYCL_REPORT_PLUGIN_ERR(pi_result, api_name, Exception);
73+
}
74+
6975
template <PiApiKind PiApiOffset, typename... ArgsT>
7076
RT::PiResult call_nocheck(ArgsT... Args) const {
7177
RT::PiFuncInfo<PiApiOffset> PiCallInfo;

sycl/source/detail/program_impl.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,10 @@ void program_impl::create_cl_program_with_source(const string_class &Source) {
360360
"program::compile_with_source is not supported by the selected backend",
361361
PI_INVALID_OPERATION);
362362
}
363+
364+
if (Err != PI_SUCCESS) {
365+
Plugin.reportPiError(Err, "piclProgramCreateWithSource()");
366+
}
363367
}
364368

365369
void program_impl::compile(const string_class &Options) {

sycl/source/detail/program_manager/program_manager.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,10 +1498,11 @@ ProgramManager::link(const std::vector<device_image_plain> &DeviceImages,
14981498
/*user_data=*/nullptr, &LinkedProg);
14991499

15001500
if (Error != PI_SUCCESS) {
1501-
const string_class ErrorMsg =
1502-
LinkedProg ? getProgramBuildLog(LinkedProg, ContextImpl)
1503-
: "Online link operation failed";
1504-
throw sycl::exception(make_error_code(errc::build), ErrorMsg);
1501+
if (LinkedProg) {
1502+
const string_class ErrorMsg = getProgramBuildLog(LinkedProg, ContextImpl);
1503+
throw sycl::exception(make_error_code(errc::build), ErrorMsg);
1504+
}
1505+
Plugin.reportPiError(Error, "piProgramLink()");
15051506
}
15061507

15071508
std::vector<kernel_id> KernelIDs;

sycl/source/detail/queue_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ class queue_impl {
259259

260260
// If creating out-of-order queue failed and this property is not
261261
// supported (for example, on FPGA), it will return
262-
// CL_INVALID_QUEUE_PROPERTIES and will try to create in-order queue.
262+
// PI_INVALID_QUEUE_PROPERTIES and will try to create in-order queue.
263263
if (MSupportOOO && Error == PI_INVALID_QUEUE_PROPERTIES) {
264264
MSupportOOO = false;
265265
Queue = createQueue(QueueOrder::Ordered);

sycl/source/detail/scheduler/commands.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,6 +1762,11 @@ pi_result ExecCGCommand::SetKernelParamsAndLaunch(
17621762
MQueue->getHandleRef(), Kernel, NDRDesc.Dims, &NDRDesc.GlobalOffset[0],
17631763
&NDRDesc.GlobalSize[0], LocalSize, RawEvents.size(),
17641764
RawEvents.empty() ? nullptr : &RawEvents[0], &Event);
1765+
1766+
if (Error != PI_SUCCESS) {
1767+
Plugin.reportPiError(Error, "piEnqueueKernelLaunch()");
1768+
}
1769+
17651770
return Error;
17661771
}
17671772

sycl/source/detail/usm/usm_impl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ alloc get_pointer_type(const void *Ptr, const context &Ctxt) {
351351
return alloc::unknown;
352352
// otherwise PI_SUCCESS is expected
353353
if (Err != PI_SUCCESS) {
354-
throw runtime_error("Error querying USM pointer: ", Err);
354+
Plugin.reportPiError(Err, "piextUSMGetMemAllocInfo()");
355355
}
356356

357357
alloc ResultAlloc;

0 commit comments

Comments
 (0)