Skip to content

Commit cac5f4f

Browse files
committed
Don't dynamically allocate OpenCL adapter
There is always only one, so there's no point in allocating it via `new`. This fixes an issue where calling `urReleaseAdapter` (or any other UR function) in an `atexit` handler could be called after the adapter is deleted.
1 parent e0cb264 commit cac5f4f

File tree

1 file changed

+12
-39
lines changed

1 file changed

+12
-39
lines changed

unified-runtime/source/adapters/opencl/adapter.cpp

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -44,43 +44,19 @@ ur_adapter_handle_t_::ur_adapter_handle_t_() {
4444
#endif // _MSC_VER
4545
}
4646

47-
static ur_adapter_handle_t adapter = nullptr;
48-
49-
ur_adapter_handle_t ur::cl::getAdapter() {
50-
if (!adapter) {
51-
die("OpenCL adapter used before initalization or after destruction");
52-
}
53-
return adapter;
54-
}
55-
56-
static void globalAdapterShutdown() {
57-
if (cl_ext::ExtFuncPtrCache) {
58-
delete cl_ext::ExtFuncPtrCache;
59-
cl_ext::ExtFuncPtrCache = nullptr;
60-
}
61-
if (adapter) {
62-
delete adapter;
63-
adapter = nullptr;
64-
}
65-
}
47+
static ur_adapter_handle_t_ adapter{};
48+
ur_adapter_handle_t ur::cl::getAdapter() { return &adapter; }
6649

6750
UR_APIEXPORT ur_result_t UR_APICALL
6851
urAdapterGet(uint32_t NumEntries, ur_adapter_handle_t *phAdapters,
6952
uint32_t *pNumAdapters) {
7053
if (NumEntries > 0 && phAdapters) {
71-
// Sometimes urAdaterGet may be called after the library already been torn
72-
// down, we also need to create a temporary handle for it.
73-
if (!adapter) {
74-
adapter = new ur_adapter_handle_t_();
75-
atexit(globalAdapterShutdown);
76-
}
77-
78-
std::lock_guard<std::mutex> Lock{adapter->Mutex};
79-
if (adapter->RefCount++ == 0) {
54+
std::lock_guard<std::mutex> Lock{adapter.Mutex};
55+
if (adapter.RefCount++ == 0) {
8056
cl_ext::ExtFuncPtrCache = new cl_ext::ExtFuncPtrCacheT();
8157
}
8258

83-
*phAdapters = adapter;
59+
*phAdapters = &adapter;
8460
}
8561

8662
if (pNumAdapters) {
@@ -91,19 +67,16 @@ urAdapterGet(uint32_t NumEntries, ur_adapter_handle_t *phAdapters,
9167
}
9268

9369
UR_APIEXPORT ur_result_t UR_APICALL urAdapterRetain(ur_adapter_handle_t) {
94-
++adapter->RefCount;
70+
++adapter.RefCount;
9571
return UR_RESULT_SUCCESS;
9672
}
9773

9874
UR_APIEXPORT ur_result_t UR_APICALL urAdapterRelease(ur_adapter_handle_t) {
99-
// Check first if the adapter is valid pointer
100-
if (adapter) {
101-
std::lock_guard<std::mutex> Lock{adapter->Mutex};
102-
if (--adapter->RefCount == 0) {
103-
if (cl_ext::ExtFuncPtrCache) {
104-
delete cl_ext::ExtFuncPtrCache;
105-
cl_ext::ExtFuncPtrCache = nullptr;
106-
}
75+
std::lock_guard<std::mutex> Lock{adapter.Mutex};
76+
if (--adapter.RefCount == 0) {
77+
if (cl_ext::ExtFuncPtrCache) {
78+
delete cl_ext::ExtFuncPtrCache;
79+
cl_ext::ExtFuncPtrCache = nullptr;
10780
}
10881
}
10982
return UR_RESULT_SUCCESS;
@@ -128,7 +101,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urAdapterGetInfo(ur_adapter_handle_t,
128101
case UR_ADAPTER_INFO_BACKEND:
129102
return ReturnValue(UR_ADAPTER_BACKEND_OPENCL);
130103
case UR_ADAPTER_INFO_REFERENCE_COUNT:
131-
return ReturnValue(adapter->RefCount.load());
104+
return ReturnValue(adapter.RefCount.load());
132105
case UR_ADAPTER_INFO_VERSION:
133106
return ReturnValue(uint32_t{1});
134107
default:

0 commit comments

Comments
 (0)