Skip to content

Commit 906f598

Browse files
authored
[SYCL][UR] Include backend name in exception messages (#18889)
To aid with more accurate identification of the source of an error in the event of a thrown exception, include the backend name in error messages resulting from calls to Unified Runtime entry points. Here's an example of the output. Before: ``` ==== DeviceSanitizer: ASAN terminate called after throwing an instance of 'sycl::_V1::exception' what(): UR backend failed. UR backend returns:40 (UR_RESULT_ERROR_OUT_OF_RESOURCES) ``` After: ``` ==== DeviceSanitizer: ASAN terminate called after throwing an instance of 'sycl::_V1::exception' what(): level_zero backend failed with error: 40 (UR_RESULT_ERROR_OUT_OF_RESOURCES) ```
1 parent b4dbfbb commit 906f598

File tree

9 files changed

+35
-23
lines changed

9 files changed

+35
-23
lines changed

sycl/include/sycl/detail/common.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,9 @@ class __SYCL_EXPORT tls_code_loc_t {
168168
#define __SYCL_ASSERT(x) assert(x)
169169
#endif // #ifdef __SYCL_DEVICE_ONLY__
170170

171-
#define __SYCL_UR_ERROR_REPORT \
172-
"Native API failed. " /*__FILE__*/ \
173-
/* TODO: replace __FILE__ to report only relative path*/ \
174-
/* ":" __SYCL_STRINGIFY(__LINE__) ": " */ \
175-
"Native API returns: "
171+
#define __SYCL_UR_ERROR_REPORT(backend) \
172+
std::string(sycl::detail::get_backend_name_no_vendor(backend)) + \
173+
" backend failed with error: "
176174

177175
#include <sycl/exception.hpp>
178176

sycl/source/detail/adapter.hpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <detail/config.hpp>
1212
#include <detail/ur.hpp>
13+
#include <sycl/backend_types.hpp>
1314
#include <sycl/detail/common.hpp>
1415
#include <sycl/detail/iostream_proxy.hpp>
1516
#include <sycl/detail/type_traits.hpp>
@@ -23,12 +24,12 @@
2324
#include <memory>
2425
#include <mutex>
2526

26-
#define __SYCL_CHECK_UR_CODE_NO_EXC(expr) \
27+
#define __SYCL_CHECK_UR_CODE_NO_EXC(expr, backend) \
2728
{ \
2829
auto code = expr; \
2930
if (code != UR_RESULT_SUCCESS) { \
30-
std::cerr << __SYCL_UR_ERROR_REPORT << sycl::detail::codeToString(code) \
31-
<< std::endl; \
31+
std::cerr << __SYCL_UR_ERROR_REPORT(backend) \
32+
<< sycl::detail::codeToString(code) << std::endl; \
3233
} \
3334
}
3435

@@ -74,7 +75,8 @@ class Adapter {
7475
throw sycl::detail::set_ur_error(
7576
sycl::exception(
7677
sycl::make_error_code(errc),
77-
__SYCL_UR_ERROR_REPORT + sycl::detail::codeToString(ur_result) +
78+
__SYCL_UR_ERROR_REPORT(MBackend) +
79+
sycl::detail::codeToString(ur_result) +
7880
(message ? "\n" + std::string(message) + "(adapter error )" +
7981
std::to_string(adapter_error) + "\n"
8082
: std::string{})),
@@ -83,7 +85,7 @@ class Adapter {
8385
if (ur_result != UR_RESULT_SUCCESS) {
8486
throw sycl::detail::set_ur_error(
8587
sycl::exception(sycl::make_error_code(errc),
86-
__SYCL_UR_ERROR_REPORT +
88+
__SYCL_UR_ERROR_REPORT(MBackend) +
8789
sycl::detail::codeToString(ur_result)),
8890
ur_result);
8991
}
@@ -143,6 +145,9 @@ class Adapter {
143145
checkUrResult<errc>(Err);
144146
}
145147

148+
/// Returns the backend reported by the adapter.
149+
backend getBackend() const { return MBackend; }
150+
146151
/// Tells if this adapter can serve specified backend.
147152
/// For example, Unified Runtime adapter will be able to serve
148153
/// multiple backends as determined by the platforms reported by the adapter.

sycl/source/detail/device_impl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ device_impl::~device_impl() {
4141
const AdapterPtr &Adapter = getAdapter();
4242
ur_result_t Err =
4343
Adapter->call_nocheck<UrApiKind::urDeviceRelease>(MDevice);
44-
__SYCL_CHECK_UR_CODE_NO_EXC(Err);
44+
__SYCL_CHECK_UR_CODE_NO_EXC(Err, Adapter->getBackend());
4545
} catch (std::exception &e) {
4646
__SYCL_REPORT_EXCEPTION_TO_STREAM("exception in ~device_impl", e);
4747
}

sycl/source/detail/error_handling/error_handling.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "error_handling.hpp"
14+
#include "sycl/detail/common.hpp"
1415

1516
#include <detail/adapter.hpp>
1617
#include <sycl/backend_types.hpp>
@@ -69,8 +70,7 @@ void handleOutOfResources(const device_impl &DeviceImpl,
6970
// Fallback
7071
constexpr ur_result_t Error = UR_RESULT_ERROR_OUT_OF_RESOURCES;
7172
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
72-
"UR backend failed. UR backend returns:" +
73-
codeToString(Error));
73+
__SYCL_UR_ERROR_REPORT(Backend) + codeToString(Error));
7474
}
7575

7676
void handleInvalidWorkGroupSize(const device_impl &DeviceImpl,
@@ -459,7 +459,8 @@ void handleErrorOrWarning(ur_result_t Error, const device_impl &DeviceImpl,
459459
default:
460460
throw detail::set_ur_error(
461461
exception(make_error_code(errc::runtime),
462-
"UR error: " + sycl::detail::codeToString(Error)),
462+
__SYCL_UR_ERROR_REPORT(DeviceImpl.getBackend()) +
463+
sycl::detail::codeToString(Error)),
463464
Error);
464465
}
465466
}

sycl/source/detail/kernel_program_cache.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class KernelProgramCache {
129129
ur_result_t Err =
130130
AdapterSharedPtr->call_nocheck<UrApiKind::urProgramRelease>(
131131
Val);
132-
__SYCL_CHECK_UR_CODE_NO_EXC(Err);
132+
__SYCL_CHECK_UR_CODE_NO_EXC(Err, AdapterSharedPtr->getBackend());
133133
}
134134
}
135135
} catch (std::exception &e) {
@@ -214,7 +214,7 @@ class KernelProgramCache {
214214
ur_result_t Err =
215215
AdapterSharedPtr->call_nocheck<UrApiKind::urKernelRelease>(
216216
Val.first);
217-
__SYCL_CHECK_UR_CODE_NO_EXC(Err);
217+
__SYCL_CHECK_UR_CODE_NO_EXC(Err, AdapterSharedPtr->getBackend());
218218
}
219219
}
220220
} catch (std::exception &e) {

sycl/source/detail/queue_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ class queue_impl : public std::enable_shared_from_this<queue_impl> {
269269
// ->call<>() instead of ->call_nocheck<>() above.
270270
if (status != UR_RESULT_SUCCESS &&
271271
status != UR_RESULT_ERROR_UNINITIALIZED) {
272-
__SYCL_CHECK_UR_CODE_NO_EXC(status);
272+
__SYCL_CHECK_UR_CODE_NO_EXC(status, getAdapter()->getBackend());
273273
}
274274
} catch (std::exception &e) {
275275
__SYCL_REPORT_EXCEPTION_TO_STREAM("exception in ~queue_impl", e);

sycl/source/detail/scheduler/scheduler.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,12 @@ void Scheduler::enqueueCommandForCG(EventImplPtr NewEvent,
190190
NewCmd, Lock, Res, ToCleanUp, NewCmd, Blocking);
191191
if (!Enqueued && EnqueueResultT::SyclEnqueueFailed == Res.MResult) {
192192
throw sycl::detail::set_ur_error(
193-
sycl::exception(sycl::make_error_code(errc::runtime),
194-
std::string("Enqueue process failed.\n") +
195-
__SYCL_UR_ERROR_REPORT +
196-
sycl::detail::codeToString(Res.MErrCode)),
193+
sycl::exception(
194+
sycl::make_error_code(errc::runtime),
195+
std::string("Enqueue process failed.\n") +
196+
__SYCL_UR_ERROR_REPORT(
197+
NewCmd->getWorkerContext()->getBackend()) +
198+
sycl::detail::codeToString(Res.MErrCode)),
197199
Res.MErrCode);
198200
}
199201
} catch (...) {

sycl/source/detail/ur.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,13 @@ std::vector<AdapterPtr> &initializeUr(ur_loader_config_handle_t LoaderConfig) {
119119

120120
static void initializeAdapters(std::vector<AdapterPtr> &Adapters,
121121
ur_loader_config_handle_t LoaderConfig) {
122-
#define CHECK_UR_SUCCESS(Call) __SYCL_CHECK_UR_CODE_NO_EXC(Call)
122+
#define CHECK_UR_SUCCESS(Call) \
123+
{ \
124+
if (ur_result_t error = Call) { \
125+
std::cerr << "UR adapter initialization failed: " \
126+
<< sycl::detail::codeToString(error) << std::endl; \
127+
} \
128+
}
123129

124130
UrFuncInfo<UrApiKind::urLoaderConfigCreate> loaderConfigCreateInfo;
125131
auto loaderConfigCreate =

sycl/unittests/xpti_trace/QueueApiFailures.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class QueueApiFailures : public ::testing::Test {
8888
const std::string TestKernelLocationMessage = BuildCodeLocationMessage(
8989
TestKI::getFileName(), TestKI::getFunctionName(), TestKI::getLineNumber(),
9090
TestKI::getColumnNumber());
91-
const std::string URLevelFailMessage = "Native API failed";
91+
const std::string URLevelFailMessage = " backend failed with error: ";
9292
const std::string SYCLLevelFailMessage = "Enqueue process failed";
9393
};
9494

0 commit comments

Comments
 (0)