Skip to content

Commit 405c4be

Browse files
KerilkEwanC
andauthored
Added an OpenCL deinitialization test and plugin infrastructure. (#268)
* Added an OpenCL plugin test. * Update loader/windows/icd_windows_formats.h Co-authored-by: Ewan Crawford <ewan.cr@gmail.com> * Update loader/linux/icd_linux_library.c Co-authored-by: Ewan Crawford <ewan.cr@gmail.com> * Move plugin test program to test directory. * Add missing copyright headers. * Add missing include guards. * Add comment to plugin test explaining workflow and code structure. * Remove extraneous blank lines. * Move cl_plugin_loader_test to test directory by using relative path. --------- Co-authored-by: Ewan Crawford <ewan.cr@gmail.com>
1 parent c987c97 commit 405c4be

20 files changed

Lines changed: 531 additions & 122 deletions

CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ set (OPENCL_ICD_LOADER_SOURCES
7575
loader/icd_dispatch.c
7676
loader/icd_dispatch.h
7777
loader/icd_dispatch_generated.c
78+
loader/icd_trace.c
79+
loader/icd_trace.h
80+
loader/icd_library.h
7881
loader/icd_envvars.h
7982
loader/icd_platform.h)
8083
include_directories (include)
@@ -84,8 +87,10 @@ if (WIN32)
8487
loader/windows/adapter.h
8588
loader/windows/icd_windows.c
8689
loader/windows/icd_windows.h
90+
loader/windows/icd_windows_formats.h
8791
loader/windows/icd_windows_dxgk.c
8892
loader/windows/icd_windows_dxgk.h
93+
loader/windows/icd_windows_library.c
8994
loader/windows/icd_windows_envvars.c
9095
loader/windows/icd_windows_hkr.c
9196
loader/windows/icd_windows_hkr.h
@@ -109,6 +114,7 @@ if (WIN32)
109114
else ()
110115
list (APPEND OPENCL_ICD_LOADER_SOURCES
111116
loader/linux/icd_linux.c
117+
loader/linux/icd_linux_library.c
112118
loader/linux/icd_linux_envvars.c
113119
loader/linux/icd_exports.map)
114120
endif ()
@@ -223,7 +229,9 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME OR OPENCL_ICD_LOADER_BUILD_TESTING)
223229
endif()
224230
if((CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME OR OPENCL_ICD_LOADER_BUILD_TESTING) AND BUILD_TESTING)
225231
add_subdirectory (test)
226-
endif()
232+
233+
endif ()
234+
227235

228236
install(
229237
TARGETS OpenCL

loader/icd.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
KHRicdVendor *khrIcdVendors = NULL;
2626
static KHRicdVendor *lastVendor = NULL;
27-
int khrEnableTrace = 0;
2827
static int khrDisableLibraryUnloading = 0;
2928
static int khrForceLegacyTermination = 0;
3029

loader/icd.h

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
#include "icd_platform.h"
2323
#include "icd_dispatch.h"
24+
#include "icd_trace.h"
25+
#include "icd_library.h"
2426

2527
#ifndef CL_USE_DEPRECATED_OPENCL_1_0_APIS
2628
#define CL_USE_DEPRECATED_OPENCL_1_0_APIS
@@ -110,8 +112,6 @@ struct KHRicdVendorRec
110112
// the global state
111113
extern KHRicdVendor * khrIcdVendors;
112114

113-
extern int khrEnableTrace;
114-
115115
#if defined(CL_ENABLE_LAYERS)
116116
/*
117117
* KHRLayer
@@ -177,18 +177,6 @@ void khrIcdLayersEnumerateEnv(void);
177177
// add a layer to the layer chain
178178
void khrIcdLayerAdd(const char *libraryName);
179179

180-
// dynamically load a library. returns NULL on failure
181-
// n.b, this call is OS-specific
182-
void *khrIcdOsLibraryLoad(const char *libraryName);
183-
184-
// get a function pointer from a loaded library. returns NULL on failure.
185-
// n.b, this call is OS-specific
186-
void *khrIcdOsLibraryGetFunctionAddress(void *library, const char *functionName);
187-
188-
// unload a library.
189-
// n.b, this call is OS-specific
190-
void khrIcdOsLibraryUnload(void *library);
191-
192180
// parse properties and determine the platform to use from them
193181
void khrIcdContextPropertiesGetPlatform(
194182
const cl_context_properties *properties,
@@ -201,32 +189,6 @@ void khrIcdContextPropertiesGetPlatform(
201189
#define ICD_ANON_UNION_INIT_MEMBER(a) a
202190
#endif
203191

204-
// internal tracing macros
205-
#define KHR_ICD_TRACE(...) \
206-
do \
207-
{ \
208-
if (khrEnableTrace) \
209-
{ \
210-
fprintf(stderr, "KHR ICD trace at %s:%d: ", __FILE__, __LINE__); \
211-
fprintf(stderr, __VA_ARGS__); \
212-
} \
213-
} while (0)
214-
215-
#ifdef _WIN32
216-
#define KHR_ICD_WIDE_TRACE(...) \
217-
do \
218-
{ \
219-
if (khrEnableTrace) \
220-
{ \
221-
fwprintf(stderr, L"KHR ICD trace at %hs:%d: ", __FILE__, __LINE__); \
222-
fwprintf(stderr, __VA_ARGS__); \
223-
} \
224-
} while (0)
225-
226-
#else
227-
#define KHR_ICD_WIDE_TRACE(...)
228-
#endif
229-
230192
#define KHR_ICD_ERROR_RETURN_ERROR(_error) \
231193
do { \
232194
return _error; \

loader/icd_library.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) 2026 The Khronos Group Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* OpenCL is a trademark of Apple Inc. used under license by Khronos.
17+
*/
18+
19+
#ifndef _ICD_LIBRARY_H_
20+
#define _ICD_LIBRARY_H_
21+
22+
// dynamically load a library. returns NULL on failure
23+
// n.b, this call is OS-specific
24+
void *khrIcdOsLibraryLoad(const char *libraryName);
25+
26+
// get a function pointer from a loaded library. returns NULL on failure.
27+
// n.b, this call is OS-specific
28+
void *khrIcdOsLibraryGetFunctionAddress(void *library, const char *functionName);
29+
30+
// unload a library.
31+
// n.b, this call is OS-specific
32+
void khrIcdOsLibraryUnload(void *library);
33+
34+
#endif

loader/icd_trace.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (c) 2026 The Khronos Group Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* OpenCL is a trademark of Apple Inc. used under license by Khronos.
17+
*/
18+
#include "icd_trace.h"
19+
20+
int khrEnableTrace = 0;

loader/icd_trace.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2026 The Khronos Group Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* OpenCL is a trademark of Apple Inc. used under license by Khronos.
17+
*/
18+
19+
#ifndef _ICD_TRACE_H_
20+
#define _ICD_TRACE_H_
21+
22+
#include <stdio.h>
23+
24+
extern int khrEnableTrace;
25+
26+
// internal tracing macros
27+
#define KHR_ICD_TRACE(...) \
28+
do \
29+
{ \
30+
if (khrEnableTrace) \
31+
{ \
32+
fprintf(stderr, "KHR ICD trace at %s:%d: ", __FILE__, __LINE__); \
33+
fprintf(stderr, __VA_ARGS__); \
34+
} \
35+
} while (0)
36+
37+
#ifdef _WIN32
38+
#define KHR_ICD_WIDE_TRACE(...) \
39+
do \
40+
{ \
41+
if (khrEnableTrace) \
42+
{ \
43+
fwprintf(stderr, L"KHR ICD trace at %hs:%d: ", __FILE__, __LINE__); \
44+
fwprintf(stderr, __VA_ARGS__); \
45+
} \
46+
} while (0)
47+
48+
#else
49+
#define KHR_ICD_WIDE_TRACE(...)
50+
#endif
51+
52+
#endif

loader/linux/icd_linux.c

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "icd.h"
2020
#include "icd_envvars.h"
2121

22-
#include <dlfcn.h>
2322
#include <stdio.h>
2423
#include <string.h>
2524
#include <stdlib.h>
@@ -237,35 +236,6 @@ void khrIcdOsVendorsEnumerateOnce(void)
237236
pthread_once(&initialized, khrIcdOsVendorsEnumerate);
238237
}
239238

240-
/*
241-
*
242-
* Dynamic library loading functions
243-
*
244-
*/
245-
246-
// dynamically load a library. returns NULL on failure
247-
void *khrIcdOsLibraryLoad(const char *libraryName)
248-
{
249-
void* ret = dlopen (libraryName, RTLD_NOW);
250-
if (NULL == ret)
251-
{
252-
KHR_ICD_TRACE("Failed to load driver because %s.\n", dlerror());
253-
}
254-
return ret;
255-
}
256-
257-
// get a function pointer from a loaded library. returns NULL on failure.
258-
void *khrIcdOsLibraryGetFunctionAddress(void *library, const char *functionName)
259-
{
260-
return dlsym(library, functionName);
261-
}
262-
263-
// unload a library
264-
void khrIcdOsLibraryUnload(void *library)
265-
{
266-
dlclose(library);
267-
}
268-
269239
#ifndef CL_LAYER_INFO
270240
static
271241
void __attribute__((destructor)) khrIcdDestructor(void) {

loader/linux/icd_linux_library.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2026 The Khronos Group Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* OpenCL is a trademark of Apple Inc. used under license by Khronos.
17+
*/
18+
19+
#include "icd_trace.h"
20+
#include "icd_library.h"
21+
#include <dlfcn.h>
22+
#include <stddef.h>
23+
24+
/*
25+
*
26+
* Dynamic library loading functions
27+
*
28+
*/
29+
30+
// dynamically load a library. returns NULL on failure
31+
void *khrIcdOsLibraryLoad(const char *libraryName)
32+
{
33+
void* ret = dlopen(libraryName, RTLD_NOW);
34+
if (NULL == ret)
35+
{
36+
KHR_ICD_TRACE("Failed to load driver because %s.\n", dlerror());
37+
}
38+
return ret;
39+
}
40+
41+
// get a function pointer from a loaded library. returns NULL on failure.
42+
void *khrIcdOsLibraryGetFunctionAddress(void *library, const char *functionName)
43+
{
44+
return dlsym(library, functionName);
45+
}
46+
47+
// unload a library
48+
void khrIcdOsLibraryUnload(void *library)
49+
{
50+
dlclose(library);
51+
}

loader/windows/icd_windows.c

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -411,43 +411,6 @@ void khrIcdOsVendorsEnumerateOnce()
411411
InitOnceExecuteOnce(&initialized, khrIcdOsVendorsEnumerate, NULL, NULL);
412412
}
413413

414-
/*
415-
*
416-
* Dynamic library loading functions
417-
*
418-
*/
419-
420-
// dynamically load a library. returns NULL on failure
421-
void *khrIcdOsLibraryLoad(const char *libraryName)
422-
{
423-
HMODULE hTemp = LoadLibraryExA(libraryName, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
424-
if (!hTemp && GetLastError() == ERROR_INVALID_PARAMETER)
425-
{
426-
hTemp = LoadLibraryExA(libraryName, NULL, 0);
427-
}
428-
if (!hTemp)
429-
{
430-
KHR_ICD_TRACE("Failed to load driver. Windows error code is %"PRIuDW".\n", GetLastError());
431-
}
432-
return (void*)hTemp;
433-
}
434-
435-
// get a function pointer from a loaded library. returns NULL on failure.
436-
void *khrIcdOsLibraryGetFunctionAddress(void *library, const char *functionName)
437-
{
438-
if (!library || !functionName)
439-
{
440-
return NULL;
441-
}
442-
return GetProcAddress( (HMODULE)library, functionName);
443-
}
444-
445-
// unload a library.
446-
void khrIcdOsLibraryUnload(void *library)
447-
{
448-
FreeLibrary( (HMODULE)library);
449-
}
450-
451414
#ifndef CL_LAYER_INFO
452415
BOOL APIENTRY DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved) {
453416
(void)hinst;

loader/windows/icd_windows.h

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,12 @@
1515
*
1616
* OpenCL is a trademark of Apple Inc. used under license by Khronos.
1717
*/
18+
#ifndef _ICD_WINDOWS_H_
19+
#define _ICD_WINDOWS_H_
1820

1921
#include <stdbool.h>
2022
#include <windows.h>
21-
22-
#ifdef _LP64
23-
#define PRIDW_PREFIX
24-
#define PRIUL_PREFIX
25-
#else
26-
#define PRIDW_PREFIX "l"
27-
#define PRIUL_PREFIX "l"
28-
#endif
29-
#define PRIuDW PRIDW_PREFIX "u"
30-
#define PRIxDW PRIDW_PREFIX "x"
31-
#define PRIuUL PRIUL_PREFIX "u"
32-
#define PRIxUL PRIUL_PREFIX "x"
23+
#include "icd_windows_formats.h"
3324

3425
#ifdef __cplusplus
3526
extern "C" {
@@ -44,3 +35,5 @@ const char* getOpenCLRegKeyName(void);
4435
#ifdef __cplusplus
4536
}
4637
#endif
38+
39+
#endif

0 commit comments

Comments
 (0)