|
5 | 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
6 | 6 | //
|
7 | 7 | //===----------------------------------------------------------------------===//
|
8 |
| -#include <CL/sycl/detail/pi.hpp> |
9 | 8 | #include "CL/opencl.h"
|
| 9 | +#include <CL/sycl/detail/pi.hpp> |
| 10 | +#include <cassert> |
| 11 | +#include <cstring> |
10 | 12 |
|
11 | 13 | namespace cl {
|
12 | 14 | namespace sycl {
|
@@ -79,6 +81,87 @@ pi_result OCL(piextDeviceSelectBinary)(
|
79 | 81 | return PI_SUCCESS;
|
80 | 82 | }
|
81 | 83 |
|
| 84 | +pi_program OCL(piProgramCreate)(pi_context context, const void *il, |
| 85 | + size_t length, pi_result *err) { |
| 86 | + |
| 87 | + size_t deviceCount; |
| 88 | + cl_program resProgram; |
| 89 | + |
| 90 | + cl_int ret_err = clGetContextInfo(pi_cast<cl_context>(context), |
| 91 | + CL_CONTEXT_DEVICES, 0, NULL, &deviceCount); |
| 92 | + |
| 93 | + std::vector<cl_device_id> devicesInCtx; |
| 94 | + devicesInCtx.reserve(deviceCount); |
| 95 | + |
| 96 | + ret_err = clGetContextInfo(pi_cast<cl_context>(context), CL_CONTEXT_DEVICES, |
| 97 | + deviceCount * sizeof(cl_device_id), |
| 98 | + devicesInCtx.data(), NULL); |
| 99 | + |
| 100 | + if (ret_err != CL_SUCCESS || deviceCount < 1) { |
| 101 | + if (err != nullptr) |
| 102 | + *err = pi_cast<pi_result>(CL_INVALID_CONTEXT); |
| 103 | + return pi_cast<pi_program>(resProgram); |
| 104 | + } |
| 105 | + |
| 106 | + cl_platform_id curPlatform; |
| 107 | + ret_err = clGetDeviceInfo(devicesInCtx[0], CL_DEVICE_PLATFORM, |
| 108 | + sizeof(cl_platform_id), &curPlatform, NULL); |
| 109 | + |
| 110 | + if (ret_err != CL_SUCCESS) { |
| 111 | + if (err != nullptr) |
| 112 | + *err = pi_cast<pi_result>(CL_INVALID_CONTEXT); |
| 113 | + return pi_cast<pi_program>(resProgram); |
| 114 | + } |
| 115 | + |
| 116 | + size_t devVerSize; |
| 117 | + ret_err = |
| 118 | + clGetPlatformInfo(curPlatform, CL_PLATFORM_VERSION, 0, NULL, &devVerSize); |
| 119 | + std::string devVer(devVerSize, '\0'); |
| 120 | + ret_err = clGetPlatformInfo(curPlatform, CL_PLATFORM_VERSION, devVerSize, |
| 121 | + &devVer.front(), NULL); |
| 122 | + |
| 123 | + if (ret_err != CL_SUCCESS) { |
| 124 | + if (err != nullptr) |
| 125 | + *err = pi_cast<pi_result>(CL_INVALID_CONTEXT); |
| 126 | + return pi_cast<pi_program>(resProgram); |
| 127 | + } |
| 128 | + |
| 129 | + if (devVer.find("OpenCL 1.0") == std::string::npos && |
| 130 | + devVer.find("OpenCL 1.1") == std::string::npos && |
| 131 | + devVer.find("OpenCL 1.2") == std::string::npos && |
| 132 | + devVer.find("OpenCL 2.0") == std::string::npos) { |
| 133 | + resProgram = clCreateProgramWithIL(pi_cast<cl_context>(context), il, length, |
| 134 | + pi_cast<cl_int *>(err)); |
| 135 | + return pi_cast<pi_program>(resProgram); |
| 136 | + } |
| 137 | + |
| 138 | + size_t extSize; |
| 139 | + ret_err = clGetPlatformInfo(curPlatform, CL_PLATFORM_EXTENSIONS, 0, NULL, |
| 140 | + &extSize); |
| 141 | + std::string extStr(extSize, '\0'); |
| 142 | + ret_err = clGetPlatformInfo(curPlatform, CL_PLATFORM_EXTENSIONS, |
| 143 | + extSize, &extStr.front(), NULL); |
| 144 | + |
| 145 | + if (ret_err != CL_SUCCESS || |
| 146 | + extStr.find("cl_khr_il_program") == std::string::npos) { |
| 147 | + if (err != nullptr) |
| 148 | + *err = pi_cast<pi_result>(CL_INVALID_CONTEXT); |
| 149 | + return pi_cast<pi_program>(resProgram); |
| 150 | + } |
| 151 | + |
| 152 | + using apiFuncT = |
| 153 | + cl_program(CL_API_CALL *)(cl_context, const void *, size_t, cl_int *); |
| 154 | + apiFuncT funcPtr = |
| 155 | + reinterpret_cast<apiFuncT>(clGetExtensionFunctionAddressForPlatform( |
| 156 | + curPlatform, "clCreateProgramWithILKHR")); |
| 157 | + |
| 158 | + assert(funcPtr != nullptr); |
| 159 | + resProgram = funcPtr(pi_cast<cl_context>(context), il, length, |
| 160 | + pi_cast<cl_int *>(err)); |
| 161 | + |
| 162 | + return pi_cast<pi_program>(resProgram); |
| 163 | +} |
| 164 | + |
82 | 165 | // TODO: implement portable call forwarding (ifunc is a GNU extension).
|
83 | 166 | // TODO: reuse same PI -> OCL mapping in pi_opencl.hpp, or maybe just
|
84 | 167 | // wait until that one is completely removed.
|
@@ -116,7 +199,6 @@ _PI_CL(piMemGetInfo, clGetMemObjectInfo)
|
116 | 199 | _PI_CL(piMemRetain, clRetainMemObject)
|
117 | 200 | _PI_CL(piMemRelease, clReleaseMemObject)
|
118 | 201 | // Program
|
119 |
| -_PI_CL(piProgramCreate, clCreateProgramWithIL) |
120 | 202 | _PI_CL(piclProgramCreateWithSource, clCreateProgramWithSource)
|
121 | 203 | _PI_CL(piclProgramCreateWithBinary, clCreateProgramWithBinary)
|
122 | 204 | _PI_CL(piProgramGetInfo, clGetProgramInfo)
|
|
0 commit comments