Skip to content

Commit 49405f5

Browse files
[SYCL][NFC] Pass adapter by ref in ur::getAdapter and event:getAdapter (#19202)
It's a part of larger refactoring effort to pass adapter via reference instead of pointer everywhere in the codebase. Follow-up of: #19186 #19184 #19187
1 parent f467518 commit 49405f5

19 files changed

+83
-84
lines changed

sycl/source/backend.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@ namespace sycl {
3030
inline namespace _V1 {
3131
namespace detail {
3232

33-
static const adapter_impl &getAdapter(backend Backend) {
33+
static adapter_impl &getAdapter(backend Backend) {
3434
switch (Backend) {
3535
case backend::opencl:
36-
return *ur::getAdapter<backend::opencl>();
36+
return ur::getAdapter<backend::opencl>();
3737
case backend::ext_oneapi_level_zero:
38-
return *ur::getAdapter<backend::ext_oneapi_level_zero>();
38+
return ur::getAdapter<backend::ext_oneapi_level_zero>();
3939
case backend::ext_oneapi_cuda:
40-
return *ur::getAdapter<backend::ext_oneapi_cuda>();
40+
return ur::getAdapter<backend::ext_oneapi_cuda>();
4141
case backend::ext_oneapi_hip:
42-
return *ur::getAdapter<backend::ext_oneapi_hip>();
42+
return ur::getAdapter<backend::ext_oneapi_hip>();
4343
default:
4444
throw sycl::exception(
4545
sycl::make_error_code(sycl::errc::runtime),
@@ -71,7 +71,7 @@ backend convertUrBackend(ur_backend_t UrBackend) {
7171
}
7272

7373
platform make_platform(ur_native_handle_t NativeHandle, backend Backend) {
74-
const adapter_impl &Adapter = getAdapter(Backend);
74+
adapter_impl &Adapter = getAdapter(Backend);
7575

7676
// Create UR platform first.
7777
ur_platform_handle_t UrPlatform = nullptr;
@@ -84,7 +84,7 @@ platform make_platform(ur_native_handle_t NativeHandle, backend Backend) {
8484

8585
__SYCL_EXPORT device make_device(ur_native_handle_t NativeHandle,
8686
backend Backend) {
87-
const adapter_impl &Adapter = getAdapter(Backend);
87+
adapter_impl &Adapter = getAdapter(Backend);
8888

8989
ur_device_handle_t UrDevice = nullptr;
9090
Adapter.call<UrApiKind::urDeviceCreateWithNativeHandle>(
@@ -100,7 +100,7 @@ __SYCL_EXPORT context make_context(ur_native_handle_t NativeHandle,
100100
const async_handler &Handler,
101101
backend Backend, bool KeepOwnership,
102102
const std::vector<device> &DeviceList) {
103-
const adapter_impl &Adapter = getAdapter(Backend);
103+
adapter_impl &Adapter = getAdapter(Backend);
104104

105105
ur_context_handle_t UrContext = nullptr;
106106
ur_context_native_properties_t Properties{};
@@ -193,7 +193,7 @@ std::shared_ptr<detail::kernel_bundle_impl>
193193
make_kernel_bundle(ur_native_handle_t NativeHandle,
194194
const context &TargetContext, bool KeepOwnership,
195195
bundle_state State, backend Backend) {
196-
const adapter_impl &Adapter = getAdapter(Backend);
196+
adapter_impl &Adapter = getAdapter(Backend);
197197
context_impl &ContextImpl = *getSyclObjImpl(TargetContext);
198198

199199
ur_program_handle_t UrProgram = nullptr;

sycl/source/backend/level_zero.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ using namespace sycl::detail;
1919

2020
__SYCL_EXPORT device make_device(const platform &Platform,
2121
ur_native_handle_t NativeHandle) {
22-
const auto &Adapter = ur::getAdapter<backend::ext_oneapi_level_zero>();
22+
adapter_impl &Adapter = ur::getAdapter<backend::ext_oneapi_level_zero>();
2323
// Create UR device first.
2424
ur_device_handle_t UrDevice;
25-
Adapter->call<UrApiKind::urDeviceCreateWithNativeHandle>(
26-
NativeHandle, Adapter->getUrAdapter(), nullptr, &UrDevice);
25+
Adapter.call<UrApiKind::urDeviceCreateWithNativeHandle>(
26+
NativeHandle, Adapter.getUrAdapter(), nullptr, &UrDevice);
2727

2828
return detail::createSyclObjFromImpl<device>(
2929
getSyclObjImpl(Platform)->getOrMakeDeviceImpl(UrDevice));

sycl/source/context.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,16 @@ context::context(const std::vector<device> &DeviceList,
7272
impl = detail::context_impl::create(DeviceList, AsyncHandler, PropList);
7373
}
7474
context::context(cl_context ClContext, async_handler AsyncHandler) {
75-
const auto &Adapter = sycl::detail::ur::getAdapter<backend::opencl>();
75+
detail::adapter_impl &Adapter =
76+
sycl::detail::ur::getAdapter<backend::opencl>();
7677

7778
ur_context_handle_t hContext = nullptr;
7879
ur_native_handle_t nativeHandle =
7980
reinterpret_cast<ur_native_handle_t>(ClContext);
80-
Adapter->call<detail::UrApiKind::urContextCreateWithNativeHandle>(
81-
nativeHandle, Adapter->getUrAdapter(), 0, nullptr, nullptr, &hContext);
81+
Adapter.call<detail::UrApiKind::urContextCreateWithNativeHandle>(
82+
nativeHandle, Adapter.getUrAdapter(), 0, nullptr, nullptr, &hContext);
8283

83-
impl = detail::context_impl::create(hContext, AsyncHandler, *Adapter);
84+
impl = detail::context_impl::create(hContext, AsyncHandler, Adapter);
8485
}
8586

8687
template <typename Param>

sycl/source/detail/adapter_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class adapter_impl {
107107
return UrPlatforms;
108108
}
109109

110-
ur_adapter_handle_t getUrAdapter() const { return MAdapter; }
110+
ur_adapter_handle_t getUrAdapter() { return MAdapter; }
111111

112112
/// Calls the UR Api, traces the call, and returns the result.
113113
///

sycl/source/detail/context_impl.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ context_impl::context_impl(const std::vector<sycl::device> Devices,
6161
}
6262

6363
context_impl::context_impl(ur_context_handle_t UrContext,
64-
async_handler AsyncHandler,
65-
const adapter_impl &Adapter,
64+
async_handler AsyncHandler, adapter_impl &Adapter,
6665
const std::vector<sycl::device> &DeviceList,
6766
bool OwnedByRuntime, private_tag)
6867
: MOwnedByRuntime(OwnedByRuntime), MAsyncHandler(AsyncHandler),
@@ -366,7 +365,7 @@ std::vector<ur_event_handle_t> context_impl::initializeDeviceGlobals(
366365
InitEventsRef.begin(), InitEventsRef.end(),
367366
[&Adapter](const ur_event_handle_t &Event) {
368367
return get_event_info<info::event::command_execution_status>(
369-
Event, Adapter) == info::event_command_status::complete;
368+
Event, *Adapter) == info::event_command_status::complete;
370369
});
371370
// Release the removed events.
372371
for (auto EventIt = NewEnd; EventIt != InitEventsRef.end(); ++EventIt)

sycl/source/detail/context_impl.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ class context_impl : public std::enable_shared_from_this<context_impl> {
6262
/// \param OwnedByRuntime is the flag if ownership is kept by user or
6363
/// transferred to runtime
6464
context_impl(ur_context_handle_t UrContext, async_handler AsyncHandler,
65-
const adapter_impl &Adapter,
65+
adapter_impl &Adapter,
6666
const std::vector<sycl::device> &DeviceList, bool OwnedByRuntime,
6767
private_tag);
6868

6969
context_impl(ur_context_handle_t UrContext, async_handler AsyncHandler,
70-
const adapter_impl &Adapter, private_tag tag)
70+
adapter_impl &Adapter, private_tag tag)
7171
: context_impl(UrContext, AsyncHandler, Adapter,
7272
std::vector<sycl::device>{},
7373
/*OwnedByRuntime*/ true, tag) {}

sycl/source/detail/device_global_map_entry.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ OwnedUrEvent DeviceGlobalUSMMem::getInitEvent(const AdapterPtr &Adapter) {
3030
// If there is a init event we can remove it if it is done.
3131
if (MInitEvent.has_value()) {
3232
if (get_event_info<info::event::command_execution_status>(
33-
*MInitEvent, Adapter) == info::event_command_status::complete) {
33+
*MInitEvent, *Adapter) == info::event_command_status::complete) {
3434
Adapter->call<UrApiKind::urEventRelease>(*MInitEvent);
3535
MInitEvent = {};
3636
return OwnedUrEvent(Adapter);

sycl/source/detail/event_impl.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ event_impl::~event_impl() {
4848
try {
4949
auto Handle = this->getHandle();
5050
if (Handle)
51-
getAdapter()->call<UrApiKind::urEventRelease>(Handle);
51+
getAdapter().call<UrApiKind::urEventRelease>(Handle);
5252
} catch (std::exception &e) {
5353
__SYCL_REPORT_EXCEPTION_TO_STREAM("exception in ~event_impl", e);
5454
}
@@ -59,7 +59,7 @@ void event_impl::waitInternal(bool *Success) {
5959
if (!MIsHostEvent && Handle) {
6060
// Wait for the native event
6161
ur_result_t Err =
62-
getAdapter()->call_nocheck<UrApiKind::urEventWait>(1, &Handle);
62+
getAdapter().call_nocheck<UrApiKind::urEventWait>(1, &Handle);
6363
// TODO drop the UR_RESULT_ERROR_UKNOWN from here (this was waiting for
6464
// https://github.com/oneapi-src/unified-runtime/issues/1459 which is now
6565
// closed).
@@ -68,7 +68,7 @@ void event_impl::waitInternal(bool *Success) {
6868
Err == UR_RESULT_ERROR_IN_EVENT_LIST_EXEC_STATUS))
6969
*Success = false;
7070
else {
71-
getAdapter()->checkUrResult(Err);
71+
getAdapter().checkUrResult(Err);
7272
if (Success != nullptr)
7373
*Success = true;
7474
}
@@ -148,9 +148,9 @@ context_impl &event_impl::getContextImpl() {
148148
return *MContext;
149149
}
150150

151-
const AdapterPtr &event_impl::getAdapter() {
151+
adapter_impl &event_impl::getAdapter() {
152152
initContextIfNeeded();
153-
return MContext->getAdapter();
153+
return *MContext->getAdapter();
154154
}
155155

156156
void event_impl::setStateIncomplete() { MState = HES_NotComplete; }
@@ -166,7 +166,7 @@ event_impl::event_impl(ur_event_handle_t Event, const context &SyclContext,
166166
MIsFlushed(true), MState(HES_Complete) {
167167

168168
ur_context_handle_t TempContext;
169-
getAdapter()->call<UrApiKind::urEventGetInfo>(
169+
getAdapter().call<UrApiKind::urEventGetInfo>(
170170
this->getHandle(), UR_EVENT_INFO_CONTEXT, sizeof(ur_context_handle_t),
171171
&TempContext, nullptr);
172172

@@ -519,19 +519,19 @@ ur_native_handle_t event_impl::getNative() {
519519
return {};
520520
initContextIfNeeded();
521521

522-
auto Adapter = getAdapter();
522+
adapter_impl &Adapter = getAdapter();
523523
auto Handle = getHandle();
524524
if (MIsDefaultConstructed && !Handle) {
525525
auto TempContext = MContext.get()->getHandleRef();
526526
ur_event_native_properties_t NativeProperties{};
527527
ur_event_handle_t UREvent = nullptr;
528-
Adapter->call<UrApiKind::urEventCreateWithNativeHandle>(
528+
Adapter.call<UrApiKind::urEventCreateWithNativeHandle>(
529529
0, TempContext, &NativeProperties, &UREvent);
530530
this->setHandle(UREvent);
531531
Handle = UREvent;
532532
}
533533
ur_native_handle_t OutHandle;
534-
Adapter->call<UrApiKind::urEventGetNativeHandle>(Handle, &OutHandle);
534+
Adapter.call<UrApiKind::urEventGetNativeHandle>(Handle, &OutHandle);
535535
if (MContext->getBackend() == backend::opencl)
536536
__SYCL_OCL_CALL(clRetainEvent, ur::cast<cl_event>(OutHandle));
537537
return OutHandle;
@@ -569,11 +569,11 @@ void event_impl::flushIfNeeded(queue_impl *UserQueue) {
569569

570570
// Check if the task for this event has already been submitted.
571571
ur_event_status_t Status = UR_EVENT_STATUS_QUEUED;
572-
getAdapter()->call<UrApiKind::urEventGetInfo>(
572+
getAdapter().call<UrApiKind::urEventGetInfo>(
573573
Handle, UR_EVENT_INFO_COMMAND_EXECUTION_STATUS, sizeof(ur_event_status_t),
574574
&Status, nullptr);
575575
if (Status == UR_EVENT_STATUS_QUEUED) {
576-
getAdapter()->call<UrApiKind::urQueueFlush>(Queue->getHandleRef());
576+
getAdapter().call<UrApiKind::urQueueFlush>(Queue->getHandleRef());
577577
}
578578
MIsFlushed = true;
579579
}

sycl/source/detail/event_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ class event_impl {
176176

177177
/// \return the Adapter associated with the context of this event.
178178
/// Should be called when this is not a Host Event.
179-
const AdapterPtr &getAdapter();
179+
adapter_impl &getAdapter();
180180

181181
/// Associate event with the context.
182182
///

sycl/source/detail/event_info.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,26 @@ inline namespace _V1 {
2020
namespace detail {
2121

2222
template <typename Param>
23-
typename Param::return_type
24-
get_event_profiling_info(ur_event_handle_t Event, const AdapterPtr &Adapter) {
23+
typename Param::return_type get_event_profiling_info(ur_event_handle_t Event,
24+
adapter_impl &Adapter) {
2525
static_assert(is_event_profiling_info_desc<Param>::value,
2626
"Unexpected event profiling info descriptor");
2727
typename Param::return_type Result{0};
2828
// TODO catch an exception and put it to list of asynchronous exceptions
29-
Adapter->call<UrApiKind::urEventGetProfilingInfo>(
29+
Adapter.call<UrApiKind::urEventGetProfilingInfo>(
3030
Event, UrInfoCode<Param>::value, sizeof(Result), &Result, nullptr);
3131
return Result;
3232
}
3333

3434
template <typename Param>
3535
typename Param::return_type get_event_info(ur_event_handle_t Event,
36-
const AdapterPtr &Adapter) {
36+
adapter_impl &Adapter) {
3737
static_assert(is_event_info_desc<Param>::value,
3838
"Unexpected event info descriptor");
3939
typename Param::return_type Result{0};
4040
// TODO catch an exception and put it to list of asynchronous exceptions
41-
Adapter->call<UrApiKind::urEventGetInfo>(Event, UrInfoCode<Param>::value,
42-
sizeof(Result), &Result, nullptr);
41+
Adapter.call<UrApiKind::urEventGetInfo>(Event, UrInfoCode<Param>::value,
42+
sizeof(Result), &Result, nullptr);
4343

4444
// If the status is UR_EVENT_STATUS_QUEUED We need to change it since QUEUE is
4545
// not a valid status in sycl.

sycl/source/detail/memory_manager.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,13 @@ static void waitForEvents(const std::vector<EventImplPtr> &Events) {
122122
// Assuming all events will be on the same device or
123123
// devices associated with the same Backend.
124124
if (!Events.empty()) {
125-
const AdapterPtr &Adapter = Events[0]->getAdapter();
125+
adapter_impl &Adapter = Events[0]->getAdapter();
126126
std::vector<ur_event_handle_t> UrEvents(Events.size());
127127
std::transform(
128128
Events.begin(), Events.end(), UrEvents.begin(),
129129
[](const EventImplPtr &EventImpl) { return EventImpl->getHandle(); });
130130
if (!UrEvents.empty() && UrEvents[0]) {
131-
Adapter->call<UrApiKind::urEventWait>(UrEvents.size(), &UrEvents[0]);
131+
Adapter.call<UrApiKind::urEventWait>(UrEvents.size(), &UrEvents[0]);
132132
}
133133
}
134134
}
@@ -318,8 +318,8 @@ void *MemoryManager::allocateInteropMemObject(
318318
// Retain the event since it will be released during alloca command
319319
// destruction
320320
if (nullptr != OutEventToWait) {
321-
const AdapterPtr &Adapter = InteropEvent->getAdapter();
322-
Adapter->call<UrApiKind::urEventRetain>(OutEventToWait);
321+
adapter_impl &Adapter = InteropEvent->getAdapter();
322+
Adapter.call<UrApiKind::urEventRetain>(OutEventToWait);
323323
}
324324
return UserPtr;
325325
}

sycl/source/detail/platform_impl.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace detail {
3232

3333
platform_impl &
3434
platform_impl::getOrMakePlatformImpl(ur_platform_handle_t UrPlatform,
35-
const adapter_impl &Adapter) {
35+
adapter_impl &Adapter) {
3636
std::shared_ptr<platform_impl> Result;
3737
{
3838
const std::lock_guard<std::mutex> Guard(
@@ -50,8 +50,8 @@ platform_impl::getOrMakePlatformImpl(ur_platform_handle_t UrPlatform,
5050
// Otherwise make the impl. Our ctor/dtor are private, so std::make_shared
5151
// needs a bit of help...
5252
struct creator : platform_impl {
53-
creator(ur_platform_handle_t APlatform, const adapter_impl &AAdapter)
54-
: platform_impl(APlatform, &AAdapter) {}
53+
creator(ur_platform_handle_t APlatform, adapter_impl &AAdapter)
54+
: platform_impl(APlatform, AAdapter) {}
5555
};
5656
Result = std::make_shared<creator>(UrPlatform, Adapter);
5757
PlatformCache.emplace_back(Result);
@@ -62,7 +62,7 @@ platform_impl::getOrMakePlatformImpl(ur_platform_handle_t UrPlatform,
6262

6363
platform_impl &
6464
platform_impl::getPlatformFromUrDevice(ur_device_handle_t UrDevice,
65-
const adapter_impl &Adapter) {
65+
adapter_impl &Adapter) {
6666
ur_platform_handle_t Plt =
6767
nullptr; // TODO catch an exception and put it to list
6868
// of asynchronous exceptions

sycl/source/detail/platform_impl.hpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,16 @@ class platform_impl : public std::enable_shared_from_this<platform_impl> {
3535
/// Constructs platform_impl from a UR platform handle.
3636
///
3737
/// \param APlatform is a raw plug-in platform handle.
38-
/// \param AAdapter is a plug-in handle.
38+
/// \param Adapter is a plug-in handle.
3939
//
4040
// Platforms can only be created under `GlobalHandler`'s ownership via
4141
// `platform_impl::getOrMakePlatformImpl` method.
42-
explicit platform_impl(ur_platform_handle_t APlatform,
43-
const adapter_impl *AAdapter)
44-
: MPlatform(APlatform) {
45-
46-
MAdapter = const_cast<AdapterPtr>(AAdapter);
42+
explicit platform_impl(ur_platform_handle_t APlatform, adapter_impl &Adapter)
43+
: MPlatform(APlatform), MAdapter(&Adapter) {
4744

4845
// Find out backend of the platform
4946
ur_backend_t UrBackend = UR_BACKEND_UNKNOWN;
50-
AAdapter->call_nocheck<UrApiKind::urPlatformGetInfo>(
47+
Adapter.call_nocheck<UrApiKind::urPlatformGetInfo>(
5148
APlatform, UR_PLATFORM_INFO_BACKEND, sizeof(ur_backend_t), &UrBackend,
5249
nullptr);
5350
MBackend = convertUrBackend(UrBackend);
@@ -183,7 +180,7 @@ class platform_impl : public std::enable_shared_from_this<platform_impl> {
183180
/// \param Adapter is the UR adapter providing the backend for the platform
184181
/// \return the platform_impl representing the UR platform
185182
static platform_impl &getOrMakePlatformImpl(ur_platform_handle_t UrPlatform,
186-
const adapter_impl &Adapter);
183+
adapter_impl &Adapter);
187184

188185
/// Queries the cache for the specified platform based on an input device.
189186
/// If found, returns the the cached platform_impl, otherwise creates a new
@@ -195,7 +192,7 @@ class platform_impl : public std::enable_shared_from_this<platform_impl> {
195192
/// platform
196193
/// \return the platform_impl that contains the input device
197194
static platform_impl &getPlatformFromUrDevice(ur_device_handle_t UrDevice,
198-
const adapter_impl &Adapter);
195+
adapter_impl &Adapter);
199196

200197
context_impl &khr_get_default_context();
201198

sycl/source/detail/scheduler/commands.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,12 +345,12 @@ class DispatchHostTask {
345345
std::vector<ur_mem_handle_t> MReqUrMem;
346346

347347
bool waitForEvents() const {
348-
std::map<const AdapterPtr, std::vector<EventImplPtr>>
348+
std::map<adapter_impl *, std::vector<EventImplPtr>>
349349
RequiredEventsPerAdapter;
350350

351351
for (const EventImplPtr &Event : MThisCmd->MPreparedDepsEvents) {
352-
const AdapterPtr &Adapter = Event->getAdapter();
353-
RequiredEventsPerAdapter[Adapter].push_back(Event);
352+
adapter_impl &Adapter = Event->getAdapter();
353+
RequiredEventsPerAdapter[&Adapter].push_back(Event);
354354
}
355355

356356
// wait for dependency device events

0 commit comments

Comments
 (0)