Skip to content

Commit 63d1d41

Browse files
committed
Deinitialization support.
1 parent c3527af commit 63d1d41

17 files changed

+787
-619
lines changed

include/cl_khr_icd2.h

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
#if !defined(CL_PLATFORM_UNLOADABLE)
2+
#define CL_PLATFORM_UNLOADABLE_KHR 0x0921
3+
#endif
4+
5+
#if defined(CL_ENABLE_LAYERS) && !defined(CL_LAYER_API_VERSION_200)
6+
#define CL_LAYER_API_VERSION_200 200
7+
8+
typedef cl_int CL_API_CALL
9+
clDeinitLayer_t(void);
10+
11+
typedef clDeinitLayer_t *pfn_clDeinitLayer;
12+
#endif //defined(CL_ENABLE_LAYERS) && !defined(CL_LAYER_API_VERSION_200)
13+
114
#if !defined(CL_ICD2_TAG_KHR)
215
#if INTPTR_MAX == INT32_MAX
316
#define CL_ICD2_TAG_KHR ((intptr_t)0x434C3331)

loader/cllayerinfo.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "icd.h"
2020
#include <stdio.h>
2121
#include <stdlib.h>
22-
#include <CL/cl_layer.h>
2322
#if defined(_WIN32)
2423
#include <io.h>
2524
#include <share.h>
@@ -90,7 +89,7 @@ static void restore_outputs(void)
9089
void printLayerInfo(const struct KHRLayer *layer)
9190
{
9291
cl_layer_api_version api_version = 0;
93-
pfn_clGetLayerInfo p_clGetLayerInfo = (pfn_clGetLayerInfo)(size_t)layer->p_clGetLayerInfo;
92+
pfn_clGetLayerInfo p_clGetLayerInfo = layer->p_clGetLayerInfo;
9493
cl_int result = CL_SUCCESS;
9594
size_t sz;
9695

loader/icd.c

+68-11
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@
1919
#include "icd.h"
2020
#include "icd_dispatch.h"
2121
#include "icd_envvars.h"
22-
#if defined(CL_ENABLE_LAYERS)
23-
#include <CL/cl_layer.h>
24-
#endif // defined(CL_ENABLE_LAYERS)
2522
#include <stdlib.h>
2623
#include <string.h>
2724

2825
KHRicdVendor *khrIcdVendors = NULL;
26+
static KHRicdVendor *lastVendor = NULL;
2927
int khrEnableTrace = 0;
3028

3129
#if defined(CL_ENABLE_LAYERS)
@@ -181,6 +179,14 @@ void khrIcdVendorAdd(const char *libraryName)
181179
#endif
182180

183181
// call clGetPlatformInfo on the returned platform to get the suffix
182+
183+
KHR_ICD2_DISPATCH(platforms[i])->clGetPlatformInfo(
184+
platforms[i],
185+
CL_PLATFORM_UNLOADABLE_KHR,
186+
sizeof(vendor->unloadable),
187+
&vendor->unloadable,
188+
NULL);
189+
184190
result = KHR_ICD2_DISPATCH(platforms[i])->clGetPlatformInfo(
185191
platforms[i],
186192
CL_PLATFORM_ICD_SUFFIX_KHR,
@@ -224,11 +230,13 @@ void khrIcdVendorAdd(const char *libraryName)
224230
vendor->suffix = suffix;
225231

226232
// add this vendor to the list of vendors at the tail
227-
{
228-
KHRicdVendor **prevNextPointer = NULL;
229-
for (prevNextPointer = &khrIcdVendors; *prevNextPointer; prevNextPointer = &( (*prevNextPointer)->next) );
230-
*prevNextPointer = vendor;
233+
if (lastVendor) {
234+
lastVendor->next = vendor;
235+
vendor->prev = lastVendor;
236+
} else {
237+
khrIcdVendors = vendor;
231238
}
239+
lastVendor = vendor;
232240

233241
KHR_ICD_TRACE("successfully added vendor %s with suffix %s\n", libraryName, suffix);
234242

@@ -253,6 +261,7 @@ void khrIcdLayerAdd(const char *libraryName)
253261
cl_int result = CL_SUCCESS;
254262
pfn_clGetLayerInfo p_clGetLayerInfo = NULL;
255263
pfn_clInitLayer p_clInitLayer = NULL;
264+
pfn_clDeinitLayer p_clDeinitLayer = NULL;
256265
struct KHRLayer *layerIterator = NULL;
257266
struct KHRLayer *layer = NULL;
258267
cl_layer_api_version api_version = 0;
@@ -302,14 +311,21 @@ void khrIcdLayerAdd(const char *libraryName)
302311
goto Done;
303312
}
304313

314+
p_clDeinitLayer = (pfn_clDeinitLayer)(size_t)khrIcdOsLibraryGetFunctionAddress(library, "clDeinitLayer");
315+
if (!p_clDeinitLayer)
316+
{
317+
KHR_ICD_TRACE("failed to get function address clDeinitLayer\n");
318+
goto Done;
319+
}
320+
305321
result = p_clGetLayerInfo(CL_LAYER_API_VERSION, sizeof(api_version), &api_version, NULL);
306322
if (CL_SUCCESS != result)
307323
{
308324
KHR_ICD_TRACE("failed to query layer version\n");
309325
goto Done;
310326
}
311327

312-
if (CL_LAYER_API_VERSION_100 != api_version)
328+
if (CL_LAYER_API_VERSION_200 != api_version)
313329
{
314330
KHR_ICD_TRACE("unsupported api version\n");
315331
goto Done;
@@ -332,17 +348,18 @@ void khrIcdLayerAdd(const char *libraryName)
332348
goto Done;
333349
}
334350
memcpy(layer->libraryName, libraryName, sz_name);
335-
layer->p_clGetLayerInfo = (void *)(size_t)p_clGetLayerInfo;
351+
layer->p_clGetLayerInfo = p_clGetLayerInfo;
336352
}
337353
#endif
354+
layer->p_clDeinitLayer = p_clDeinitLayer;
338355

339356
if (khrFirstLayer) {
340357
targetDispatch = &(khrFirstLayer->dispatch);
341358
} else {
342-
targetDispatch = &khrMasterDispatch;
359+
targetDispatch = &khrMainDispatch;
343360
}
344361

345-
loaderDispatchNumEntries = sizeof(khrMasterDispatch)/sizeof(void*);
362+
loaderDispatchNumEntries = sizeof(khrMainDispatch)/sizeof(void*);
346363
result = p_clInitLayer(
347364
loaderDispatchNumEntries,
348365
targetDispatch,
@@ -466,3 +483,43 @@ void khrIcdContextPropertiesGetPlatform(const cl_context_properties *properties,
466483
}
467484
}
468485

486+
#if defined(CL_ENABLE_LAYERS)
487+
static struct KHRLayer deinitLayer = {0};
488+
#endif
489+
490+
void khrIcdDeinitialize(void) {
491+
492+
KHR_ICD_TRACE("ICD Loader deinitialization\n");
493+
494+
#if defined(CL_ENABLE_LAYERS)
495+
// free layers first in reverse order of their creation (front to back)
496+
// they may still need to use vendors while terminating
497+
KHR_ICD_TRACE("Finalizing and unloading layers\n");
498+
struct KHRLayer *head = khrFirstLayer;
499+
deinitLayer.dispatch = khrDeinitDispatch;
500+
khrFirstLayer = &deinitLayer;
501+
502+
while(head) {
503+
struct KHRLayer *cur = head;
504+
#ifdef CL_LAYER_INFO
505+
free(cur->libraryName);
506+
#endif
507+
cur->p_clDeinitLayer();
508+
khrIcdOsLibraryUnload(cur->library);
509+
head = cur->next;
510+
free(cur);
511+
}
512+
#endif // defined(CL_ENABLE_LAYERS)
513+
514+
// free vendor in reverse order of their creation (back to front)
515+
KHR_ICD_TRACE("Finalizing and unloading vendors\n");
516+
while (lastVendor) {
517+
KHRicdVendor *cur = lastVendor;
518+
free(cur->suffix);
519+
if (cur->unloadable)
520+
khrIcdOsLibraryUnload(cur->library);
521+
lastVendor = cur->prev;
522+
free(cur);
523+
}
524+
khrIcdVendors = NULL;
525+
}

loader/icd.h

+16-3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
#include <CL/cl.h>
5050
#include <CL/cl_ext.h>
5151
#include <CL/cl_icd.h>
52+
#if defined(CL_ENABLE_LAYERS)
53+
#include <CL/cl_layer.h>
54+
#endif // defined(CL_ENABLE_LAYERS)
5255
#include <stdio.h>
5356

5457
/*
@@ -85,6 +88,9 @@ struct KHRicdVendorRec
8588
// the extension suffix for this platform
8689
char *suffix;
8790

91+
// can this vendor library be unloaded?
92+
cl_bool unloadable;
93+
8894
// function pointer to the ICD platform IDs extracted from the library
8995
pfn_clGetExtensionFunctionAddress clGetExtensionFunctionAddress;
9096

@@ -98,6 +104,7 @@ struct KHRicdVendorRec
98104

99105
// next vendor in the list vendors
100106
KHRicdVendor *next;
107+
KHRicdVendor *prev;
101108
};
102109

103110
// the global state
@@ -123,14 +130,17 @@ struct KHRLayer
123130
#ifdef CL_LAYER_INFO
124131
// The layer library name
125132
char *libraryName;
126-
// the pointer to the clGetLayerInfo funciton
127-
void *p_clGetLayerInfo;
133+
// the pointer to the clGetLayerInfo function
134+
pfn_clGetLayerInfo p_clGetLayerInfo;
128135
#endif
136+
// the pointer to the clDeinitLayer function
137+
pfn_clDeinitLayer p_clDeinitLayer;
129138
};
130139

131140
// the global layer state
132141
extern struct KHRLayer * khrFirstLayer;
133-
extern struct _cl_icd_dispatch khrMasterDispatch;
142+
extern const struct _cl_icd_dispatch khrMainDispatch;
143+
extern const struct _cl_icd_dispatch khrDeinitDispatch;
134144
#endif // defined(CL_ENABLE_LAYERS)
135145

136146
/*
@@ -147,6 +157,9 @@ void khrIcdInitialize(void);
147157
// entrypoint to check and initialize trace.
148158
void khrIcdInitializeTrace(void);
149159

160+
// entrypoint to release icd resources
161+
void khrIcdDeinitialize(void);
162+
150163
// go through the list of vendors (in /etc/OpenCL.conf or through
151164
// the registry) and call khrIcdVendorAdd for each vendor encountered
152165
// n.b, this call is OS-specific

0 commit comments

Comments
 (0)