Skip to content
Open
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
61 changes: 61 additions & 0 deletions docs/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,67 @@ OpenCL Applications or to revert back to normal operation.

See the [cliloader](cliloader.md) documentation for more detail.

## As an ICD Loader Layer

The Intercept Layer for OpenCL Applications can be loaded by the OpenCL ICD
loader as an ICD loader layer.
When used as an ICD loader layer, the OpenCL ICD loader is responsible for
loading and initializing the Intercept Layer for OpenCL applications, then
calling it instead of calling through a dispatch table to the target ICD.

To setup the Intercept Layer for OpenCL Applications as an ICD loader layer, set
the `OPENCL_LAYERS` environment variable so it includes the Intercept Layer for
OpenCL Applications library.
This will usually be a full path to the library, for example:

```sh
$ export OPENCL_LAYERS="/path/to/libOpenCL.so"
```

or:

```sh
> set OPENCL_LAYERS="c:\path\to\OpenCL.dll"
```

The Intercept Layer for OpenCL Applications may also be configured as a system
ICD loader layer.
Please consult your OpenCL ICD loader documentation for information about
installing and using system layers.

When the Intercept Layer for OpenCL Applications is used as an ICD loader layer,
it will first go through typical initialization when it is initially loaded:

```
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
CLIntercept (64-bit) is loading...
<more loading information>
... loading complete.
```

Then, it will go through a separate layer initialization, initiated by the
OpenCL ICD loader:

```
CLIntercept layer is initializing...
... layer initialization complete.
```

After the Intercept Layer for OpenCL Applications has been initialized as an ICD
loader layer, it should operate the same as if it were installed and enabled
through other mechanisms.

For more information about ICD loader layers, please refer to the
[cl_loader_layers](https://github.com/KhronosGroup/OpenCL-Docs/blob/main/extensions/cl_loader_layers.asciidoc)
documentation, or follow the [OpenCL Layers Tutorial](https://github.com/Kerilk/OpenCL-Layers-Tutorial).

To troubleshoot issues using the Intercept Layer for OpenCL Applications as an
ICD loader layer, consider setting the `OCL_ICD_ENABLE_TRACE` environment
variable (for the Khronos
[OpenCL-ICD-Loader](https://github.com/KhronosGroup/OpenCL-ICD-Loader)) or the
`OCL_ICD_DEBUG` environment variable (for the
[ocl-icd](https://github.com/OCL-dev/ocl-icd) loader).

## Windows

### Local Install
Expand Down
2 changes: 1 addition & 1 deletion intercept/CL/cl_function_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -1191,7 +1191,7 @@ typedef cl_int CL_API_CALL clGetKernelSuggestedLocalWorkSize_t(
cl_uint work_dim,
const size_t* global_work_offset,
const size_t* global_work_size,
size_t* suggested_local_work_size) CL_API_SUFFIX__VERSION_3_1;
size_t* suggested_local_work_size);

typedef clGetKernelSuggestedLocalWorkSize_t *
clGetKernelSuggestedLocalWorkSize_fn CL_API_SUFFIX__VERSION_3_1;
Expand Down
165 changes: 165 additions & 0 deletions intercept/CL/cl_layer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*******************************************************************************
* Copyright (c) 2008-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.
******************************************************************************/

#ifndef OPENCL_CL_LAYER_H_
#define OPENCL_CL_LAYER_H_

/*
** This header is generated from the Khronos OpenCL XML API Registry.
*/

// Unlike the Khronos header file, we want to unconditionally include the
// CLIntercept cl_icd.h and cl.h, system headers.
#include "CL/cl_icd.h"

#include "CL/cl.h"

/* CL_NO_PROTOTYPES implies CL_NO_EXTENSION_PROTOTYPES: */
#if defined(CL_NO_PROTOTYPES) && !defined(CL_NO_EXTENSION_PROTOTYPES)
#define CL_NO_EXTENSION_PROTOTYPES
#endif

/* CL_NO_EXTENSION_PROTOTYPES implies
CL_NO_ICD_DISPATCH_EXTENSION_PROTOTYPES and
CL_NO_NON_ICD_DISPATCH_EXTENSION_PROTOTYPES: */
#if defined(CL_NO_EXTENSION_PROTOTYPES) && \
!defined(CL_NO_ICD_DISPATCH_EXTENSION_PROTOTYPES)
#define CL_NO_ICD_DISPATCH_EXTENSION_PROTOTYPES
#endif
#if defined(CL_NO_EXTENSION_PROTOTYPES) && \
!defined(CL_NO_NON_ICD_DISPATCH_EXTENSION_PROTOTYPES)
#define CL_NO_NON_ICD_DISPATCH_EXTENSION_PROTOTYPES
#endif

#ifdef __cplusplus
extern "C" {
#endif

/***************************************************************
* cl_loader_layers
***************************************************************/
#define cl_loader_layers 1
#define CL_LOADER_LAYERS_EXTENSION_NAME \
"cl_loader_layers"


#define CL_LOADER_LAYERS_EXTENSION_VERSION CL_MAKE_VERSION(1, 0, 1)

typedef cl_uint cl_layer_info;
typedef cl_uint cl_layer_api_version;
typedef cl_properties cl_layer_properties;

/* cl_layer_info */
#define CL_LAYER_API_VERSION 0x4240
#define CL_LAYER_NAME 0x4241

/* Misc API enums */
#define CL_LAYER_API_VERSION_100 100
#define CL_LAYER_PROPERTIES_LIST_END ((cl_layer_properties)0)


typedef cl_int CL_API_CALL
clGetLayerInfo_t(
cl_layer_info param_name,
size_t param_value_size,
void* param_value,
size_t* param_value_size_ret);

typedef clGetLayerInfo_t *
clGetLayerInfo_fn ;

typedef cl_int CL_API_CALL
clInitLayer_t(
cl_uint num_entries,
const cl_icd_dispatch* target_dispatch,
cl_uint* num_entries_ret,
const cl_icd_dispatch** layer_dispatch_ret);

typedef clInitLayer_t *
clInitLayer_fn ;

typedef cl_int CL_API_CALL
clInitLayerWithProperties_t(
cl_uint num_entries,
const cl_icd_dispatch* target_dispatch,
cl_uint* num_entries_ret,
const cl_icd_dispatch** layer_dispatch_ret,
const cl_layer_properties* properties);

typedef clInitLayerWithProperties_t *
clInitLayerWithProperties_fn ;

typedef cl_int CL_API_CALL
clDeinitLayer_t(
void );

typedef clDeinitLayer_t *
clDeinitLayer_fn ;

/*
** The function pointer typedefs prefixed with "pfn_" are provided for
** compatibility with earlier versions of the headers. New code is
** encouraged to use the function pointer typedefs that are suffixed with
** "_fn" instead, for consistency.
*/

typedef clGetLayerInfo_t *
pfn_clGetLayerInfo ;

typedef clInitLayer_t *
pfn_clInitLayer ;

typedef clInitLayerWithProperties_t *
pfn_clInitLayerWithProperties ;

typedef clDeinitLayer_t *
pfn_clDeinitLayer ;

#if !defined(CL_NO_NON_ICD_DISPATCH_EXTENSION_PROTOTYPES)

extern CL_API_ENTRY cl_int CL_API_CALL
clGetLayerInfo(
cl_layer_info param_name,
size_t param_value_size,
void* param_value,
size_t* param_value_size_ret) ;

extern CL_API_ENTRY cl_int CL_API_CALL
clInitLayer(
cl_uint num_entries,
const cl_icd_dispatch* target_dispatch,
cl_uint* num_entries_ret,
const cl_icd_dispatch** layer_dispatch_ret) ;

extern CL_API_ENTRY cl_int CL_API_CALL
clInitLayerWithProperties(
cl_uint num_entries,
const cl_icd_dispatch* target_dispatch,
cl_uint* num_entries_ret,
const cl_icd_dispatch** layer_dispatch_ret,
const cl_layer_properties* properties) ;

extern CL_API_ENTRY cl_int CL_API_CALL
clDeinitLayer(
void ) ;

#endif /* !defined(CL_NO_NON_ICD_DISPATCH_EXTENSION_PROTOTYPES) */

#ifdef __cplusplus
}
#endif

#endif /* OPENCL_CL_LAYER_H_ */
2 changes: 1 addition & 1 deletion intercept/CL/cl_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ typedef double cl_double;

#define CL_NAN (CL_INFINITY - CL_INFINITY)
#define CL_HUGE_VALF ((cl_float) 1e50)
#define CL_HUGE_VAL ((cl_double) 1e500)
#define CL_HUGE_VAL ((cl_double) (1e300 * 1e300))
#define CL_MAXFLOAT CL_FLT_MAX
#define CL_INFINITY CL_HUGE_VALF

Expand Down
1 change: 1 addition & 0 deletions intercept/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ set(CLINTERCEPT_SOURCE_FILES
src/instrumentation.h
src/intercept.cpp
src/intercept.h
src/layer.cpp
src/main.cpp
src/objtracker.cpp
src/objtracker.h
Expand Down
16 changes: 7 additions & 9 deletions intercept/OS/OS_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,7 @@
#endif
#endif

// Since we just need the symbol clGetPlatformIDs, we can use any OpenCL
// version.
#if !defined(CL_TARGET_OPENCL_VERSION)
#define CL_TARGET_OPENCL_VERSION 100
#endif
#include "CL/cl.h"
void CLIntercept_Load(void);

namespace OS
{
Expand Down Expand Up @@ -75,7 +70,8 @@ class Services
const std::string& str ) const;

void* LoadLibrary(
const std::string& libraryName ) const;
const std::string& libraryName,
const bool deepBind = false ) const;
void UnloadLibrary(
void*& pLibrary ) const;

Expand Down Expand Up @@ -192,7 +188,8 @@ inline void Services::OutputDebugString(
}

inline void* Services::LoadLibrary(
const std::string& libraryName ) const
const std::string& libraryName,
const bool deepBind ) const
{
void* pLibrary = dlopen( libraryName.c_str(), RTLD_NOW | RTLD_GLOBAL );
//if( pLibrary == NULL )
Expand Down Expand Up @@ -298,9 +295,10 @@ inline bool Services::GetCLInterceptName(
std::string& name ) const
{
Dl_info info;
if( dladdr( (void*)clGetPlatformIDs, &info ) )
if( dladdr( (void*)CLIntercept_Load, &info ) )
{
name = info.dli_fname;
return true;
}
return false;
}
Expand Down
10 changes: 7 additions & 3 deletions intercept/OS/OS_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class Services
const std::string& str ) const;

void* LoadLibrary(
const std::string& libraryName ) const;
const std::string& libraryName,
const bool deepBind = false ) const;
void UnloadLibrary(
void*& pLibrary ) const;

Expand Down Expand Up @@ -145,9 +146,11 @@ inline void Services::OutputDebugString(
}

inline void* Services::LoadLibrary(
const std::string& libraryName ) const
const std::string& libraryName,
const bool deepBind ) const
{
void* pLibrary = dlopen( libraryName.c_str(), RTLD_NOW );
const int flags = RTLD_NOW | (deepBind ? RTLD_FIRST : 0);
void* pLibrary = dlopen( libraryName.c_str(), flags );
return pLibrary;
}

Expand Down Expand Up @@ -241,6 +244,7 @@ inline bool Services::GetCLInterceptName(
if( dladdr( (void*)CLIntercept_Load, &info ) )
{
name = info.dli_fname;
return true;
}
return false;
}
Expand Down
6 changes: 4 additions & 2 deletions intercept/OS/OS_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class Services
const std::string& str ) const;

void* LoadLibrary(
const std::string& libraryName ) const;
const std::string& libraryName,
bool deepBind = false ) const;
void UnloadLibrary(
void*& pLibrary ) const;

Expand Down Expand Up @@ -223,7 +224,8 @@ inline void Services::OutputDebugString(
}

inline void* Services::LoadLibrary(
const std::string& libraryName ) const
const std::string& libraryName,
const bool ) const
{
HMODULE hModule = ::LoadLibraryA( libraryName.c_str() );
return hModule;
Expand Down
1 change: 1 addition & 0 deletions intercept/src/clIntercept.def
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ EXPORTS
; ==========================================================
clGetLayerInfo
clInitLayer
clInitLayerWithProperties

; ==========================================================
; Standard OpenCL APIs
Expand Down
3 changes: 3 additions & 0 deletions intercept/src/clIntercept.map
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ INTERNAL {
_binary_kernels_builtin_kernels_cl_end;
_binary_kernels_precompiled_kernels_cl_start;
_binary_kernels_precompiled_kernels_cl_end;
clGetLayerInfo;
clInitLayer;
clInitLayerWithProperties;
local:
*;
};
Expand Down
6 changes: 3 additions & 3 deletions intercept/src/git_version.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

#include "intercept.h"

const char* CLIntercept::sc_GitDescribe = "@GIT_DESCRIBE@";
const char* CLIntercept::sc_GitRefSpec = "@GIT_REFSPEC@";
const char* CLIntercept::sc_GitHash = "@GIT_SHA1@";
const char* const CLIntercept::sc_GitDescribe = "@GIT_DESCRIBE@";
const char* const CLIntercept::sc_GitRefSpec = "@GIT_REFSPEC@";
const char* const CLIntercept::sc_GitHash = "@GIT_SHA1@";
Loading
Loading