Skip to content

Commit

Permalink
Add support for OpenCL 3.0 Provisional (#108)
Browse files Browse the repository at this point in the history
* Add support for experimental entry points

The new entry points will only be enabled if `CL_EXPERIMENTAL` is manually set.

* Update experimental implementation points to 3.0 provisional

* Use configure_file to selectively include symbols
  • Loading branch information
alycm committed May 7, 2020
1 parent fe09ad1 commit 23475bd
Show file tree
Hide file tree
Showing 9 changed files with 316 additions and 4 deletions.
41 changes: 37 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ find_package (Threads REQUIRED)
# advance. Use it with discretion.
option (BUILD_SHARED_LIBS "Build shared libs" ON)

# This option enables support for OpenCL 3.0 Provisional in the ICD loader. It
# is currently off by default while the specification is provisional, as it may
# change.
option (ENABLE_OPENCL30_PROVISIONAL "Enable 3.0 provisional entry points" OFF)

include(CheckFunctionExists)
check_function_exists(secure_getenv HAVE_SECURE_GETENV)
check_function_exists(__secure_getenv HAVE___SECURE_GETENV)
Expand All @@ -49,6 +54,17 @@ set (OPENCL_ICD_LOADER_SOURCES
loader/icd_platform.h)

if (WIN32)
# By default don't include OpenCL 3.0 symbol definitions (i.e. comment them
# out), but include them for OpenCL 3.0 builds. Once the symbols are no
# longer provisional then they may be included unconditionally.
set(ENABLE_OPENCL30_SYMBOLS ";")
if (ENABLE_OPENCL30_PROVISIONAL)
set(ENABLE_OPENCL30_SYMBOLS "")
endif ()
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/loader/windows/OpenCL.def.in
${CMAKE_CURRENT_BINARY_DIR}/loader/windows/OpenCL.def)

list (APPEND OPENCL_ICD_LOADER_SOURCES
loader/windows/adapter.h
loader/windows/icd_windows.c
Expand All @@ -60,7 +76,7 @@ if (WIN32)
loader/windows/icd_windows_hkr.h
loader/windows/icd_windows_apppackage.cpp
loader/windows/icd_windows_apppackage.h
loader/windows/OpenCL.def
${CMAKE_CURRENT_BINARY_DIR}/loader/windows/OpenCL.def
loader/windows/OpenCL.rc)
# Only add the DXSDK include directory if the environment variable is
# defined. Since the DXSDK has merged into the Windows SDK, this is
Expand All @@ -69,10 +85,23 @@ if (WIN32)
include_directories ($ENV{DXSDK_DIR}/Include)
endif ()
else ()
# By default don't include OpenCL 3.0 symbol definitions (i.e. comment them
# out), but include them for OpenCL 3.0 builds. Once the symbols are no
# longer provisional then they may be included unconditionally.
set(ENABLE_OPENCL30_SYMBOLS_START "/*")
set(ENABLE_OPENCL30_SYMBOLS_END "*/")
if (ENABLE_OPENCL30_PROVISIONAL)
set(ENABLE_OPENCL30_SYMBOLS_START "")
set(ENABLE_OPENCL30_SYMBOLS_END "")
endif ()
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/loader/linux/icd_exports.map.in
${CMAKE_CURRENT_BINARY_DIR}/loader/linux/icd_exports.map)

list (APPEND OPENCL_ICD_LOADER_SOURCES
loader/linux/icd_linux.c
loader/linux/icd_linux_envvars.c
loader/linux/icd_exports.map)
${CMAKE_CURRENT_BINARY_DIR}/loader/linux/icd_exports.map)
endif ()

set (OPENCL_ICD_LOADER_HEADERS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/inc CACHE PATH "Path to OpenCL Headers")
Expand All @@ -97,13 +126,17 @@ else()
if (APPLE)
target_link_libraries (OpenCL ${CMAKE_THREAD_LIBS_INIT})
else ()
set_target_properties (OpenCL PROPERTIES LINK_FLAGS "-Wl,--version-script -Wl,${CMAKE_CURRENT_SOURCE_DIR}/loader/linux/icd_exports.map")
set_target_properties (OpenCL PROPERTIES LINK_FLAGS "-Wl,--version-script -Wl,${CMAKE_CURRENT_BINARY_DIR}/loader/linux/icd_exports.map")
target_link_libraries (OpenCL ${CMAKE_THREAD_LIBS_INIT})
endif ()
endif ()

include_directories (${OPENCL_ICD_LOADER_HEADERS_DIR})
add_definitions (-DCL_TARGET_OPENCL_VERSION=220)
if (ENABLE_OPENCL30_PROVISIONAL)
add_definitions (-DCL_TARGET_OPENCL_VERSION=300)
else()
add_definitions (-DCL_TARGET_OPENCL_VERSION=220)
endif()

target_include_directories (OpenCL PRIVATE ${CMAKE_CURRENT_BINARY_DIR} loader)
target_link_libraries (OpenCL ${CMAKE_DL_LIBS})
Expand Down
45 changes: 45 additions & 0 deletions loader/icd_dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,51 @@ clCreateImage(cl_context context,
errcode_ret);
}

#ifdef CL_VERSION_3_0
/* ICD loader entry points should not normally be ifdef'ed, but prevent
* OpenCL 3.0 provisional entry points from being in general builds before the
* specification is finalized. */

CL_API_ENTRY cl_mem CL_API_CALL
clCreateBufferWithProperties(cl_context context,
const cl_mem_properties * properties,
cl_mem_flags flags,
size_t size,
void * host_ptr,
cl_int * errcode_ret) CL_API_SUFFIX__VERSION_3_0
{
KHR_ICD_VALIDATE_HANDLE_RETURN_HANDLE(context, CL_INVALID_CONTEXT);
return context->dispatch->clCreateBufferWithProperties(
context,
properties,
flags,
size,
host_ptr,
errcode_ret);
}

CL_API_ENTRY cl_mem CL_API_CALL
clCreateImageWithProperties(cl_context context,
const cl_mem_properties * properties,
cl_mem_flags flags,
const cl_image_format * image_format,
const cl_image_desc * image_desc,
void * host_ptr,
cl_int * errcode_ret) CL_API_SUFFIX__VERSION_3_0
{
KHR_ICD_VALIDATE_HANDLE_RETURN_HANDLE(context, CL_INVALID_CONTEXT);
return context->dispatch->clCreateImageWithProperties(
context,
properties,
flags,
image_format,
image_desc,
host_ptr,
errcode_ret);
}

#endif // CL_VERSION_3_0

CL_API_ENTRY cl_int CL_API_CALL
clRetainMemObject(cl_mem memobj) CL_API_SUFFIX__VERSION_1_0
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,11 @@ OPENCL_2.2 {
clSetProgramReleaseCallback;
clSetProgramSpecializationConstant;
} OPENCL_2.1;

@ENABLE_OPENCL30_SYMBOLS_START@
OPENCL_3.0 {
global:
clCreateBufferWithProperties;
clCreateImageWithProperties;
} OPENCL_2.2;
@ENABLE_OPENCL30_SYMBOLS_END@
3 changes: 3 additions & 0 deletions loader/windows/OpenCL.def → loader/windows/OpenCL.def.in
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,6 @@ clSetDefaultDeviceCommandQueue
clSetProgramReleaseCallback
clSetProgramSpecializationConstant

; OpenCL 3.0 API
@ENABLE_OPENCL30_SYMBOLS@clCreateBufferWithProperties
@ENABLE_OPENCL30_SYMBOLS@clCreateImageWithProperties
50 changes: 50 additions & 0 deletions test/driver_stub/cl.c
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,56 @@ clCreateImage3D(cl_context context,
return obj;
}

#ifdef CL_VERSION_3_0

CL_API_ENTRY cl_mem CL_API_CALL
clCreateBufferWithProperties(cl_context context ,
const cl_mem_properties * properties,
cl_mem_flags flags ,
size_t size ,
void * host_ptr ,
cl_int * errcode_ret) CL_API_SUFFIX__VERSION_3_0
{
cl_mem obj = (cl_mem) malloc(sizeof(struct _cl_mem));
obj->dispatch = dispatchTable;
test_icd_stub_log("clCreateBufferWithProperties(%p, %p, %x, %u, %p, %p)\n",
context,
properties,
flags,
size,
host_ptr,
errcode_ret);

test_icd_stub_log("Value returned: %p\n", obj);
return obj;
}

CL_API_ENTRY cl_mem CL_API_CALL
clCreateImageWithProperties(cl_context context,
const cl_mem_properties * properties,
cl_mem_flags flags,
const cl_image_format * image_format,
const cl_image_desc * image_desc,
void * host_ptr,
cl_int * errcode_ret) CL_API_SUFFIX__VERSION_3_0
{
cl_mem obj = (cl_mem) malloc(sizeof(struct _cl_mem));
obj->dispatch = dispatchTable;
test_icd_stub_log("clCreateImageWithProperties(%p, %p, %x, %p, %p, %p, %p)\n",
context,
properties,
flags,
image_format,
image_desc,
host_ptr,
errcode_ret);

test_icd_stub_log("Value returned: %p\n", obj);
return obj;
}

#endif // CL_VERSION_3_0

CL_API_ENTRY cl_int CL_API_CALL
clRetainMemObject(cl_mem memobj) CL_API_SUFFIX__VERSION_1_0
{
Expand Down
68 changes: 68 additions & 0 deletions test/driver_stub/icd.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#define CL_USE_DEPRECATED_OPENCL_1_0_APIS
#define CL_USE_DEPRECATED_OPENCL_1_1_APIS
#define CL_USE_DEPRECATED_OPENCL_1_2_APIS
#define CL_USE_DEPRECATED_OPENCL_2_0_APIS
#define CL_USE_DEPRECATED_OPENCL_2_1_APIS
#define CL_USE_DEPRECATED_OPENCL_2_2_APIS

// Need to rename all CL API functions to prevent ICD loader functions calling
// themselves via the dispatch table. Include this before cl headers.
Expand Down Expand Up @@ -148,12 +151,15 @@ cl_int cliIcdDispatchTableCreate(CLIicdDispatchTable **outDispatchTable)
ICD_DISPATCH_TABLE_ENTRY ( clEnqueueWriteBufferRect);
ICD_DISPATCH_TABLE_ENTRY ( clEnqueueCopyBufferRect);

/* cl_ext_device_fission */
ICD_DISPATCH_TABLE_ENTRY ( /*clCreateSubDevicesEXT*/NULL);
ICD_DISPATCH_TABLE_ENTRY ( /*clRetainDeviceEXT*/ NULL);
ICD_DISPATCH_TABLE_ENTRY ( /*clReleaseDevice*/NULL);

/* cl_khr_gl_event */
ICD_DISPATCH_TABLE_ENTRY ( clCreateEventFromGLsyncKHR);

/* OpenCL 1.2 */
ICD_DISPATCH_TABLE_ENTRY ( clCreateSubDevices);
ICD_DISPATCH_TABLE_ENTRY ( clRetainDevice);
ICD_DISPATCH_TABLE_ENTRY ( clReleaseDevice);
Expand All @@ -171,6 +177,68 @@ cl_int cliIcdDispatchTableCreate(CLIicdDispatchTable **outDispatchTable)
ICD_DISPATCH_TABLE_ENTRY ( clGetExtensionFunctionAddressForPlatform);
ICD_DISPATCH_TABLE_ENTRY ( clCreateFromGLTexture);

/* cl_khr_d3d11_sharing */
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );

/* cl_khr_dx9_media_sharing */
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );

/* cl_khr_egl_image */
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );

/* cl_khr_egl_event */
ICD_DISPATCH_TABLE_ENTRY( NULL );

/* OpenCL 2.0 */
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );

/* cl_khr_sub_groups */
ICD_DISPATCH_TABLE_ENTRY( NULL );

/* OpenCL 2.1 */
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );

/* OpenCL 2.2 */
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );

#ifdef CL_VERSION_3_0
/* OpenCL 3.0 */
ICD_DISPATCH_TABLE_ENTRY ( clCreateBufferWithProperties );
ICD_DISPATCH_TABLE_ENTRY ( clCreateImageWithProperties );
#else
ICD_DISPATCH_TABLE_ENTRY( NULL );
ICD_DISPATCH_TABLE_ENTRY( NULL );
#endif // CL_VERSION_3_0

// return success
*outDispatchTable = dispatchTable;
return CL_SUCCESS;
Expand Down
2 changes: 2 additions & 0 deletions test/driver_stub/rename_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,7 @@
#define clEnqueueReleaseGLObjects ___clEnqueueReleaseGLObjects
#define clGetGLContextInfoKHR ___clGetGLContextInfoKHR
#define clCreateEventFromGLsyncKHR ___clCreateEventFromGLsyncKHR
#define clCreateBufferWithProperties ___clCreateBufferWithProperties
#define clCreateImageWithProperties ___clCreateImageWithProperties

#endif /* __RENAME_API_H__ */
26 changes: 26 additions & 0 deletions test/loader_test/param_struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ struct clReleaseDevice_st


#define NUM_ITEMS_clCreateBuffer 1
#define NUM_ITEMS_clCreateBufferWithProperties 1
#define NUM_ITEMS_clCreateSubBuffer 1
#define NUM_ITEMS_clEnqueueReadBuffer 1
#define NUM_ITEMS_clEnqueueWriteBuffer 1
Expand All @@ -174,6 +175,17 @@ struct clCreateBuffer_st
void *host_ptr;
cl_int *errcode_ret;
};
#ifdef CL_VERSION_3_0
struct clCreateBufferWithProperties_st
{
cl_context context;
const cl_mem_properties * properties;
cl_mem_flags flags;
size_t size;
void *host_ptr;
cl_int *errcode_ret;
};
#endif // CL_VERSION_3_0
struct clCreateSubBuffer_st
{
cl_mem buffer;
Expand Down Expand Up @@ -473,6 +485,7 @@ struct clGetProgramBuildInfo_st
#define NUM_ITEMS_clCreateImage2D 1
#define NUM_ITEMS_clCreateImage3D 1
#define NUM_ITEMS_clCreateImage 1
#define NUM_ITEMS_clCreateImageWithProperties 1
#define NUM_ITEMS_clGetSupportedImageFormats 1
#define NUM_ITEMS_clEnqueueCopyImageToBuffer 1
#define NUM_ITEMS_clEnqueueCopyBufferToImage 1
Expand All @@ -494,6 +507,19 @@ struct clCreateImage_st
cl_int *errcode_ret;
};

#ifdef CL_VERSION_3_0
struct clCreateImageWithProperties_st
{
cl_context context;
const cl_mem_properties * properties;
cl_mem_flags flags;
const cl_image_format *image_format;
const cl_image_desc *image_desc;
void *host_ptr;
cl_int *errcode_ret;
};
#endif // CL_VERSION_3_0

struct clCreateImage2D_st
{
cl_context context;
Expand Down

0 comments on commit 23475bd

Please sign in to comment.