Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ set (OPENCL_ICD_LOADER_SOURCES
loader/icd_dispatch.c
loader/icd_dispatch.h
loader/icd_dispatch_generated.c
loader/icd_trace.c
loader/icd_trace.h
loader/icd_library.h
loader/icd_envvars.h
loader/icd_platform.h)
include_directories (include)
Expand All @@ -84,8 +87,10 @@ if (WIN32)
loader/windows/adapter.h
loader/windows/icd_windows.c
loader/windows/icd_windows.h
loader/windows/icd_windows_formats.h
loader/windows/icd_windows_dxgk.c
loader/windows/icd_windows_dxgk.h
loader/windows/icd_windows_library.c
loader/windows/icd_windows_envvars.c
loader/windows/icd_windows_hkr.c
loader/windows/icd_windows_hkr.h
Expand All @@ -109,6 +114,7 @@ if (WIN32)
else ()
list (APPEND OPENCL_ICD_LOADER_SOURCES
loader/linux/icd_linux.c
loader/linux/icd_linux_library.c
loader/linux/icd_linux_envvars.c
loader/linux/icd_exports.map)
endif ()
Expand Down Expand Up @@ -223,7 +229,9 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME OR OPENCL_ICD_LOADER_BUILD_TESTING)
endif()
if((CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME OR OPENCL_ICD_LOADER_BUILD_TESTING) AND BUILD_TESTING)
add_subdirectory (test)
endif()

endif ()


install(
TARGETS OpenCL
Expand Down
1 change: 0 additions & 1 deletion loader/icd.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

KHRicdVendor *khrIcdVendors = NULL;
static KHRicdVendor *lastVendor = NULL;
int khrEnableTrace = 0;
static int khrDisableLibraryUnloading = 0;
static int khrForceLegacyTermination = 0;

Expand Down
42 changes: 2 additions & 40 deletions loader/icd.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#include "icd_platform.h"
#include "icd_dispatch.h"
#include "icd_trace.h"
#include "icd_library.h"

#ifndef CL_USE_DEPRECATED_OPENCL_1_0_APIS
#define CL_USE_DEPRECATED_OPENCL_1_0_APIS
Expand Down Expand Up @@ -110,8 +112,6 @@ struct KHRicdVendorRec
// the global state
extern KHRicdVendor * khrIcdVendors;

extern int khrEnableTrace;

#if defined(CL_ENABLE_LAYERS)
/*
* KHRLayer
Expand Down Expand Up @@ -177,18 +177,6 @@ void khrIcdLayersEnumerateEnv(void);
// add a layer to the layer chain
void khrIcdLayerAdd(const char *libraryName);

// dynamically load a library. returns NULL on failure
// n.b, this call is OS-specific
void *khrIcdOsLibraryLoad(const char *libraryName);

// get a function pointer from a loaded library. returns NULL on failure.
// n.b, this call is OS-specific
void *khrIcdOsLibraryGetFunctionAddress(void *library, const char *functionName);

// unload a library.
// n.b, this call is OS-specific
void khrIcdOsLibraryUnload(void *library);

// parse properties and determine the platform to use from them
void khrIcdContextPropertiesGetPlatform(
const cl_context_properties *properties,
Expand All @@ -201,32 +189,6 @@ void khrIcdContextPropertiesGetPlatform(
#define ICD_ANON_UNION_INIT_MEMBER(a) a
#endif

// internal tracing macros
#define KHR_ICD_TRACE(...) \
do \
{ \
if (khrEnableTrace) \
{ \
fprintf(stderr, "KHR ICD trace at %s:%d: ", __FILE__, __LINE__); \
fprintf(stderr, __VA_ARGS__); \
} \
} while (0)

#ifdef _WIN32
#define KHR_ICD_WIDE_TRACE(...) \
do \
{ \
if (khrEnableTrace) \
{ \
fwprintf(stderr, L"KHR ICD trace at %hs:%d: ", __FILE__, __LINE__); \
fwprintf(stderr, __VA_ARGS__); \
} \
} while (0)

#else
#define KHR_ICD_WIDE_TRACE(...)
#endif

#define KHR_ICD_ERROR_RETURN_ERROR(_error) \
do { \
return _error; \
Expand Down
34 changes: 34 additions & 0 deletions loader/icd_library.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2026 The Khronos Group Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* OpenCL is a trademark of Apple Inc. used under license by Khronos.
*/

#ifndef _ICD_LIBRARY_H_
#define _ICD_LIBRARY_H_

// dynamically load a library. returns NULL on failure
// n.b, this call is OS-specific
void *khrIcdOsLibraryLoad(const char *libraryName);

// get a function pointer from a loaded library. returns NULL on failure.
// n.b, this call is OS-specific
void *khrIcdOsLibraryGetFunctionAddress(void *library, const char *functionName);

// unload a library.
// n.b, this call is OS-specific
void khrIcdOsLibraryUnload(void *library);

#endif
20 changes: 20 additions & 0 deletions loader/icd_trace.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2026 The Khronos Group Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* OpenCL is a trademark of Apple Inc. used under license by Khronos.
*/
#include "icd_trace.h"

int khrEnableTrace = 0;
52 changes: 52 additions & 0 deletions loader/icd_trace.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2026 The Khronos Group Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* OpenCL is a trademark of Apple Inc. used under license by Khronos.
*/

#ifndef _ICD_TRACE_H_
#define _ICD_TRACE_H_

#include <stdio.h>

extern int khrEnableTrace;

// internal tracing macros
#define KHR_ICD_TRACE(...) \
do \
{ \
if (khrEnableTrace) \
{ \
fprintf(stderr, "KHR ICD trace at %s:%d: ", __FILE__, __LINE__); \
fprintf(stderr, __VA_ARGS__); \
} \
} while (0)

#ifdef _WIN32
#define KHR_ICD_WIDE_TRACE(...) \
do \
{ \
if (khrEnableTrace) \
{ \
fwprintf(stderr, L"KHR ICD trace at %hs:%d: ", __FILE__, __LINE__); \
fwprintf(stderr, __VA_ARGS__); \
} \
} while (0)

#else
#define KHR_ICD_WIDE_TRACE(...)
#endif

#endif
30 changes: 0 additions & 30 deletions loader/linux/icd_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "icd.h"
#include "icd_envvars.h"

#include <dlfcn.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
Expand Down Expand Up @@ -237,35 +236,6 @@ void khrIcdOsVendorsEnumerateOnce(void)
pthread_once(&initialized, khrIcdOsVendorsEnumerate);
}

/*
*
* Dynamic library loading functions
*
*/

// dynamically load a library. returns NULL on failure
void *khrIcdOsLibraryLoad(const char *libraryName)
{
void* ret = dlopen (libraryName, RTLD_NOW);
if (NULL == ret)
{
KHR_ICD_TRACE("Failed to load driver because %s.\n", dlerror());
}
return ret;
}

// get a function pointer from a loaded library. returns NULL on failure.
void *khrIcdOsLibraryGetFunctionAddress(void *library, const char *functionName)
{
return dlsym(library, functionName);
}

// unload a library
void khrIcdOsLibraryUnload(void *library)
{
dlclose(library);
}

#ifndef CL_LAYER_INFO
static
void __attribute__((destructor)) khrIcdDestructor(void) {
Expand Down
51 changes: 51 additions & 0 deletions loader/linux/icd_linux_library.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2026 The Khronos Group Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* OpenCL is a trademark of Apple Inc. used under license by Khronos.
*/

#include "icd_trace.h"
#include "icd_library.h"
#include <dlfcn.h>
#include <stddef.h>

/*
*
* Dynamic library loading functions
*
*/

// dynamically load a library. returns NULL on failure
void *khrIcdOsLibraryLoad(const char *libraryName)
{
void* ret = dlopen(libraryName, RTLD_NOW);
if (NULL == ret)
{
KHR_ICD_TRACE("Failed to load driver because %s.\n", dlerror());
}
return ret;
}

// get a function pointer from a loaded library. returns NULL on failure.
void *khrIcdOsLibraryGetFunctionAddress(void *library, const char *functionName)
{
return dlsym(library, functionName);
}

// unload a library
void khrIcdOsLibraryUnload(void *library)
{
dlclose(library);
}
37 changes: 0 additions & 37 deletions loader/windows/icd_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -411,43 +411,6 @@ void khrIcdOsVendorsEnumerateOnce()
InitOnceExecuteOnce(&initialized, khrIcdOsVendorsEnumerate, NULL, NULL);
}

/*
*
* Dynamic library loading functions
*
*/

// dynamically load a library. returns NULL on failure
void *khrIcdOsLibraryLoad(const char *libraryName)
{
HMODULE hTemp = LoadLibraryExA(libraryName, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
if (!hTemp && GetLastError() == ERROR_INVALID_PARAMETER)
{
hTemp = LoadLibraryExA(libraryName, NULL, 0);
}
if (!hTemp)
{
KHR_ICD_TRACE("Failed to load driver. Windows error code is %"PRIuDW".\n", GetLastError());
}
return (void*)hTemp;
}

// get a function pointer from a loaded library. returns NULL on failure.
void *khrIcdOsLibraryGetFunctionAddress(void *library, const char *functionName)
{
if (!library || !functionName)
{
return NULL;
}
return GetProcAddress( (HMODULE)library, functionName);
}

// unload a library.
void khrIcdOsLibraryUnload(void *library)
{
FreeLibrary( (HMODULE)library);
}

#ifndef CL_LAYER_INFO
BOOL APIENTRY DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved) {
(void)hinst;
Expand Down
17 changes: 5 additions & 12 deletions loader/windows/icd_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,12 @@
*
* OpenCL is a trademark of Apple Inc. used under license by Khronos.
*/
#ifndef _ICD_WINDOWS_H_
#define _ICD_WINDOWS_H_

#include <stdbool.h>
#include <windows.h>

#ifdef _LP64
#define PRIDW_PREFIX
#define PRIUL_PREFIX
#else
#define PRIDW_PREFIX "l"
#define PRIUL_PREFIX "l"
#endif
#define PRIuDW PRIDW_PREFIX "u"
#define PRIxDW PRIDW_PREFIX "x"
#define PRIuUL PRIUL_PREFIX "u"
#define PRIxUL PRIUL_PREFIX "x"
#include "icd_windows_formats.h"

#ifdef __cplusplus
extern "C" {
Expand All @@ -44,3 +35,5 @@ const char* getOpenCLRegKeyName(void);
#ifdef __cplusplus
}
#endif

#endif
Loading