Skip to content

Commit c865b9f

Browse files
committed
[SYCL] optimized calling UR functions
1 parent b3df18d commit c865b9f

File tree

7 files changed

+52
-39
lines changed

7 files changed

+52
-39
lines changed

sycl/source/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ set(SYCL_COMMON_SOURCES
266266
"builtins/native_math_functions.cpp"
267267
"builtins/relational_functions.cpp"
268268
"detail/accessor_impl.cpp"
269+
"detail/adapter_impl.cpp"
269270
"detail/allowlist.cpp"
270271
"detail/bindless_images.cpp"
271272
"detail/buffer_impl.cpp"
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
///
9+
/// \file
10+
/// This file contains the definitions for the members of the adapter_impl
11+
/// class.
12+
///
13+
//===----------------------------------------------------------------------===//
14+
15+
#include "adapter_impl.hpp"
16+
17+
namespace sycl {
18+
inline namespace _V1 {
19+
namespace detail {
20+
21+
void adapter_impl::ur_failed_throw_exception(sycl::errc errc,
22+
ur_result_t ur_result) const {
23+
assert(ur_result != UR_RESULT_SUCCESS);
24+
std::string message =
25+
__SYCL_UR_ERROR_REPORT(MBackend) + codeToString(ur_result);
26+
27+
if (ur_result == UR_RESULT_ERROR_ADAPTER_SPECIFIC) {
28+
assert(!adapterReleased);
29+
const char *last_error_message = nullptr;
30+
int32_t adapter_error = 0;
31+
ur_result = call_nocheck<UrApiKind::urAdapterGetLastError>(
32+
MAdapter, &last_error_message, &adapter_error);
33+
if (last_error_message)
34+
message += "\n" + std::string(last_error_message) + "(adapter error )" +
35+
std::to_string(adapter_error) + "\n";
36+
}
37+
38+
throw set_ur_error(sycl::exception(sycl::make_error_code(errc), message),
39+
ur_result);
40+
}
41+
42+
} // namespace detail
43+
} // namespace _V1
44+
} // namespace sycl

sycl/source/detail/adapter_impl.hpp

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -71,29 +71,8 @@ class adapter_impl {
7171
/// \throw SYCL 2020 exception(errc) if ur_result is not UR_RESULT_SUCCESS
7272
template <sycl::errc errc = sycl::errc::runtime>
7373
void checkUrResult(ur_result_t ur_result) const {
74-
if (ur_result == UR_RESULT_ERROR_ADAPTER_SPECIFIC) {
75-
assert(!adapterReleased);
76-
const char *message = nullptr;
77-
int32_t adapter_error = 0;
78-
ur_result = call_nocheck<UrApiKind::urAdapterGetLastError>(
79-
MAdapter, &message, &adapter_error);
80-
throw sycl::detail::set_ur_error(
81-
sycl::exception(
82-
sycl::make_error_code(errc),
83-
__SYCL_UR_ERROR_REPORT(MBackend) +
84-
sycl::detail::codeToString(ur_result) +
85-
(message ? "\n" + std::string(message) + "(adapter error )" +
86-
std::to_string(adapter_error) + "\n"
87-
: std::string{})),
88-
ur_result);
89-
}
90-
if (ur_result != UR_RESULT_SUCCESS) {
91-
throw sycl::detail::set_ur_error(
92-
sycl::exception(sycl::make_error_code(errc),
93-
__SYCL_UR_ERROR_REPORT(MBackend) +
94-
sycl::detail::codeToString(ur_result)),
95-
ur_result);
96-
}
74+
if (__builtin_expect(ur_result != UR_RESULT_SUCCESS, false))
75+
ur_failed_throw_exception(errc, ur_result);
9776
}
9877

9978
std::vector<ur_platform_handle_t> &getUrPlatforms() {
@@ -225,6 +204,8 @@ class adapter_impl {
225204
bool adapterReleased = false;
226205

227206
private:
207+
void ur_failed_throw_exception(sycl::errc errc, ur_result_t ur_result) const;
208+
228209
ur_adapter_handle_t MAdapter;
229210
backend MBackend;
230211
// Mutex to guard UrPlatforms and LastDeviceIds.

sycl/source/detail/event_impl.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,6 @@ static uint64_t inline getTimestamp(device_impl *Device) {
131131
}
132132
}
133133

134-
ur_event_handle_t event_impl::getHandle() const { return MEvent.load(); }
135-
136-
void event_impl::setHandle(const ur_event_handle_t &UREvent) {
137-
MEvent.store(UREvent);
138-
}
139-
140134
context_impl &event_impl::getContextImpl() {
141135
initContextIfNeeded();
142136
assert(MContext && "Trying to get context from a host event!");

sycl/source/detail/event_impl.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ class event_impl {
163163
void setComplete();
164164

165165
/// Returns raw interoperability event handle.
166-
ur_event_handle_t getHandle() const;
166+
ur_event_handle_t getHandle() const { return MEvent.load(); }
167167

168168
/// Set event handle for this event object.
169-
void setHandle(const ur_event_handle_t &UREvent);
169+
void setHandle(const ur_event_handle_t &UREvent) { MEvent.store(UREvent); }
170170

171171
/// Returns context that is associated with this event.
172172
context_impl &getContextImpl();

unified-runtime/source/loader/ur_lib.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,9 @@
2828
#include <stdlib.h>
2929

3030
namespace ur_lib {
31-
///////////////////////////////////////////////////////////////////////////////
32-
context_t *getContext() { return context_t::get_direct(); }
33-
3431
///////////////////////////////////////////////////////////////////////////////
3532
context_t::context_t() { parseEnvEnabledLayers(); }
3633

37-
///////////////////////////////////////////////////////////////////////////////
38-
context_t::~context_t() {}
39-
4034
void context_t::parseEnvEnabledLayers() {
4135
auto maybeEnableEnvVarVec = getenv_to_vec("UR_ENABLE_LAYERS");
4236
if (!maybeEnableEnvVarVec.has_value()) {

unified-runtime/source/loader/ur_lib.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ class __urdlllocal context_t : public AtomicSingleton<context_t> {
6161
#endif
6262

6363
context_t();
64-
~context_t();
6564

6665
std::once_flag initOnce;
6766

@@ -117,7 +116,7 @@ class __urdlllocal context_t : public AtomicSingleton<context_t> {
117116
void tearDownLayers() const;
118117
};
119118

120-
context_t *getContext();
119+
inline context_t *getContext() { return context_t::get_direct(); }
121120

122121
ur_result_t urLoaderConfigCreate(ur_loader_config_handle_t *phLoaderConfig);
123122
ur_result_t urLoaderConfigRetain(ur_loader_config_handle_t hLoaderConfig);

0 commit comments

Comments
 (0)