Skip to content

Commit 4740e27

Browse files
authored
Merge pull request #2219 from kbenzie/benie/cl-adapter-opt-core-func
[CL] Support using old ICD loaders
2 parents 09373ff + ca9d1ef commit 4740e27

File tree

7 files changed

+93
-55
lines changed

7 files changed

+93
-55
lines changed

source/adapters/opencl/adapter.cpp

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,49 @@
88
//
99
//===----------------------------------------------------------------------===//
1010

11+
#include "adapter.hpp"
1112
#include "common.hpp"
12-
#include "logger/ur_logger.hpp"
13+
#include "ur/ur.hpp"
14+
15+
#ifdef _MSC_VER
16+
#include <Windows.h>
17+
#else
18+
#include <dlfcn.h>
19+
#endif
20+
21+
ur_adapter_handle_t_::ur_adapter_handle_t_() {
22+
#ifdef _MSC_VER
23+
// Loading OpenCL.dll increments the libraries internal reference count.
24+
auto handle = LoadLibraryA("OpenCL.dll");
25+
26+
#define CL_CORE_FUNCTION(FUNC) \
27+
FUNC = reinterpret_cast<decltype(::FUNC) *>(GetProcAddress(handle, "FUNC"));
28+
#include "core_functions.def"
29+
#undef CL_CORE_FUNCTION
30+
31+
// So we can safely decrement it here wihtout actually unloading OpenCL.dll.
32+
FreeLibrary(handle);
33+
#else
34+
// Loading libOpenCL.so to get the library handle but don't dlclose it as
35+
// this causes a segfault when attempting to call any OpenCL entry point.
36+
auto handle = dlopen("libOpenCL.so", RTLD_LOCAL);
37+
38+
#define CL_CORE_FUNCTION(FUNC) \
39+
FUNC = reinterpret_cast<decltype(::FUNC) *>(dlsym(handle, #FUNC));
40+
#include "core_functions.def"
41+
#undef CL_CORE_FUNCTION
42+
43+
#endif
44+
}
1345

14-
struct ur_adapter_handle_t_ {
15-
std::atomic<uint32_t> RefCount = 0;
16-
std::mutex Mutex;
17-
logger::Logger &log = logger::get_logger("opencl");
18-
};
46+
static ur_adapter_handle_t adapter = nullptr;
1947

20-
static ur_adapter_handle_t_ *adapter = nullptr;
48+
ur_adapter_handle_t ur::cl::getAdapter() {
49+
if (!adapter) {
50+
die("OpenCL adapter used before initalization or after destruction");
51+
}
52+
return adapter;
53+
}
2154

2255
static void globalAdapterShutdown() {
2356
if (cl_ext::ExtFuncPtrCache) {

source/adapters/opencl/adapter.hpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,26 @@
88
//
99
//===----------------------------------------------------------------------===//
1010

11-
struct ur_adapter_handle_t_;
11+
#include "CL/cl.h"
12+
#include "logger/ur_logger.hpp"
1213

13-
extern ur_adapter_handle_t_ adapter;
14+
struct ur_adapter_handle_t_ {
15+
ur_adapter_handle_t_();
16+
17+
std::atomic<uint32_t> RefCount = 0;
18+
std::mutex Mutex;
19+
logger::Logger &log = logger::get_logger("opencl");
20+
21+
// Function pointers to core OpenCL entry points which may not exist in older
22+
// versions of the OpenCL-ICD-Loader are tracked here and initialized by
23+
// dynamically loading the symbol by name.
24+
#define CL_CORE_FUNCTION(FUNC) decltype(::FUNC) *FUNC = nullptr;
25+
#include "core_functions.def"
26+
#undef CL_CORE_FUNCTION
27+
};
28+
29+
namespace ur {
30+
namespace cl {
31+
ur_adapter_handle_t getAdapter();
32+
} // namespace cl
33+
} // namespace ur

source/adapters/opencl/common.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,6 @@ cl_int(CL_API_CALL *)(cl_command_queue, cl_program, const char *, cl_bool,
239239
size_t, size_t, void *, cl_uint, const cl_event *,
240240
cl_event *);
241241

242-
using clSetProgramSpecializationConstant_fn = CL_API_ENTRY
243-
cl_int(CL_API_CALL *)(cl_program program, cl_uint spec_id, size_t spec_size,
244-
const void *spec_value);
245-
246242
using clEnqueueReadHostPipeINTEL_fn = CL_API_ENTRY
247243
cl_int(CL_API_CALL *)(cl_command_queue queue, cl_program program,
248244
const char *pipe_symbol, cl_bool blocking, void *ptr,

source/adapters/opencl/context.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//===----------------------------------------------------------------------===//
1010

1111
#include "context.hpp"
12+
#include "adapter.hpp"
1213

1314
#include <mutex>
1415
#include <set>
@@ -169,6 +170,13 @@ UR_APIEXPORT ur_result_t UR_APICALL urContextCreateWithNativeHandle(
169170
UR_APIEXPORT ur_result_t UR_APICALL urContextSetExtendedDeleter(
170171
ur_context_handle_t hContext, ur_context_extended_deleter_t pfnDeleter,
171172
void *pUserData) {
173+
if (!ur::cl::getAdapter()->clSetContextDestructorCallback) {
174+
ur::cl::getAdapter()->log.warning(
175+
"clSetContextDestructorCallback not found, consider upgrading the "
176+
"OpenCL-ICD-Loader to the latest version.");
177+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
178+
}
179+
172180
static std::unordered_map<ur_context_handle_t,
173181
std::set<ur_context_extended_deleter_t>>
174182
ContextCallbackMap;
@@ -212,7 +220,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urContextSetExtendedDeleter(
212220
auto *C = static_cast<ContextCallback *>(pUserData);
213221
C->execute();
214222
};
215-
CL_RETURN_ON_FAILURE(clSetContextDestructorCallback(
223+
CL_RETURN_ON_FAILURE(ur::cl::getAdapter()->clSetContextDestructorCallback(
216224
cl_adapter::cast<cl_context>(hContext), ClCallback, Callback));
217225

218226
return UR_RESULT_SUCCESS;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Introduced in OpenCL 2.0
2+
// TODO: clCreateCommandQueueWithProperties
3+
4+
// Introduced in OpenCL 2.1
5+
// TODO: clGetKernelSubGroupInfo
6+
// TODO: clCreateProgramWithIL
7+
// TODO: clGetHostTimer
8+
// TODO: clGetDeviceAndHostTimer
9+
10+
// Introduced in OpenCL 2.2
11+
CL_CORE_FUNCTION(clSetProgramSpecializationConstant)
12+
13+
// Introduced in OpenCL 3.0
14+
CL_CORE_FUNCTION(clSetContextDestructorCallback)

source/adapters/opencl/extension_functions.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ CL_EXTENSION_FUNC(clEnqueueWriteGlobalVariable)
1313
CL_EXTENSION_FUNC(clEnqueueReadGlobalVariable)
1414
CL_EXTENSION_FUNC(clEnqueueReadHostPipeINTEL)
1515
CL_EXTENSION_FUNC(clEnqueueWriteHostPipeINTEL)
16-
CL_EXTENSION_FUNC(clSetProgramSpecializationConstant)
1716
CL_EXTENSION_FUNC(clCreateCommandBufferKHR)
1817
CL_EXTENSION_FUNC(clRetainCommandBufferKHR)
1918
CL_EXTENSION_FUNC(clReleaseCommandBufferKHR)

source/adapters/opencl/program.cpp

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//
99
//===----------------------------------------------------------------------===//
1010

11+
#include "adapter.hpp"
1112
#include "common.hpp"
1213
#include "context.hpp"
1314
#include "device.hpp"
@@ -392,50 +393,17 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramSetSpecializationConstants(
392393
sizeof(cl_platform_id), &CurPlatform,
393394
nullptr));
394395

395-
oclv::OpenCLVersion PlatVer;
396-
cl_adapter::getPlatformVersion(CurPlatform, PlatVer);
397-
398-
bool UseExtensionLookup = false;
399-
if (PlatVer < oclv::V2_2) {
400-
UseExtensionLookup = true;
401-
} else {
402-
for (cl_device_id Dev : *DevicesInCtx) {
403-
oclv::OpenCLVersion DevVer;
404-
405-
UR_RETURN_ON_FAILURE(cl_adapter::getDeviceVersion(Dev, DevVer));
406-
407-
if (DevVer < oclv::V2_2) {
408-
UseExtensionLookup = true;
409-
break;
410-
}
411-
}
412-
}
413-
414-
if (UseExtensionLookup == false) {
396+
if (ur::cl::getAdapter()->clSetProgramSpecializationConstant) {
415397
for (uint32_t i = 0; i < count; ++i) {
416-
CL_RETURN_ON_FAILURE(clSetProgramSpecializationConstant(
417-
CLProg, pSpecConstants[i].id, pSpecConstants[i].size,
418-
pSpecConstants[i].pValue));
398+
CL_RETURN_ON_FAILURE(
399+
ur::cl::getAdapter()->clSetProgramSpecializationConstant(
400+
CLProg, pSpecConstants[i].id, pSpecConstants[i].size,
401+
pSpecConstants[i].pValue));
419402
}
420403
} else {
421-
cl_ext::clSetProgramSpecializationConstant_fn
422-
SetProgramSpecializationConstant = nullptr;
423-
const ur_result_t URResult = cl_ext::getExtFuncFromContext<
424-
decltype(SetProgramSpecializationConstant)>(
425-
Ctx, cl_ext::ExtFuncPtrCache->clSetProgramSpecializationConstantCache,
426-
cl_ext::SetProgramSpecializationConstantName,
427-
&SetProgramSpecializationConstant);
428-
429-
if (URResult != UR_RESULT_SUCCESS) {
430-
return URResult;
431-
}
432-
433-
for (uint32_t i = 0; i < count; ++i) {
434-
CL_RETURN_ON_FAILURE(SetProgramSpecializationConstant(
435-
CLProg, pSpecConstants[i].id, pSpecConstants[i].size,
436-
pSpecConstants[i].pValue));
437-
}
404+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
438405
}
406+
439407
return UR_RESULT_SUCCESS;
440408
}
441409

0 commit comments

Comments
 (0)