Skip to content

Commit 7c755c4

Browse files
fabiomestremartygrantPetr Veselycallumfareaarongreig
committed
[SYCL][OpenCL] Port OpenCL plugin to Unified Runtime (#10490)
Co-authored-by: Martin Morrison-Grant <martin.morrisongrant@codeplay.com> Co-authored-by: Petr Vesely <petr.vesely@codeplay.com> Co-authored-by: Callum Fare <callum@codeplay.com> Co-authored-by: aarongreig <aarongreig01@gmail.com>
1 parent ab45620 commit 7c755c4

23 files changed

+5196
-0
lines changed

source/adapters/opencl/adapter.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//===-------------- adapter.cpp - OpenCL Adapter ---------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===-----------------------------------------------------------------===//
8+
9+
#include "common.hpp"
10+
11+
struct ur_adapter_handle_t_ {
12+
std::atomic<uint32_t> RefCount = 0;
13+
};
14+
15+
ur_adapter_handle_t_ adapter{};
16+
17+
UR_APIEXPORT ur_result_t UR_APICALL urInit(ur_device_init_flags_t,
18+
ur_loader_config_handle_t) {
19+
cl_ext::ExtFuncPtrCache = new cl_ext::ExtFuncPtrCacheT();
20+
return UR_RESULT_SUCCESS;
21+
}
22+
23+
UR_APIEXPORT ur_result_t UR_APICALL urTearDown(void *) {
24+
if (cl_ext::ExtFuncPtrCache) {
25+
delete cl_ext::ExtFuncPtrCache;
26+
cl_ext::ExtFuncPtrCache = nullptr;
27+
}
28+
return UR_RESULT_SUCCESS;
29+
}
30+
31+
UR_APIEXPORT ur_result_t UR_APICALL
32+
urAdapterGet(uint32_t NumEntries, ur_adapter_handle_t *phAdapters,
33+
uint32_t *pNumAdapters) {
34+
if (NumEntries > 0 && phAdapters) {
35+
*phAdapters = &adapter;
36+
}
37+
38+
if (pNumAdapters) {
39+
*pNumAdapters = 1;
40+
}
41+
42+
return UR_RESULT_SUCCESS;
43+
}
44+
45+
UR_APIEXPORT ur_result_t UR_APICALL urAdapterRetain(ur_adapter_handle_t) {
46+
++adapter.RefCount;
47+
return UR_RESULT_SUCCESS;
48+
}
49+
50+
UR_APIEXPORT ur_result_t UR_APICALL urAdapterRelease(ur_adapter_handle_t) {
51+
--adapter.RefCount;
52+
return UR_RESULT_SUCCESS;
53+
}
54+
55+
UR_APIEXPORT ur_result_t UR_APICALL urAdapterGetLastError(
56+
ur_adapter_handle_t, const char **ppMessage, int32_t *pError) {
57+
*ppMessage = cl_adapter::ErrorMessage;
58+
*pError = cl_adapter::ErrorMessageCode;
59+
60+
return UR_RESULT_SUCCESS;
61+
}
62+
63+
UR_APIEXPORT ur_result_t UR_APICALL urAdapterGetInfo(ur_adapter_handle_t,
64+
ur_adapter_info_t propName,
65+
size_t propSize,
66+
void *pPropValue,
67+
size_t *pPropSizeRet) {
68+
UrReturnHelper ReturnValue(propSize, pPropValue, pPropSizeRet);
69+
70+
switch (propName) {
71+
case UR_ADAPTER_INFO_BACKEND:
72+
return ReturnValue(UR_ADAPTER_BACKEND_CUDA);
73+
case UR_ADAPTER_INFO_REFERENCE_COUNT:
74+
return ReturnValue(adapter.RefCount.load());
75+
default:
76+
return UR_RESULT_ERROR_INVALID_ENUMERATION;
77+
}
78+
79+
return UR_RESULT_SUCCESS;
80+
}

source/adapters/opencl/adapter.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//===-------------- adapter.hpp - OpenCL Adapter ---------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===-----------------------------------------------------------------===//
8+
9+
struct ur_adapter_handle_t_;
10+
11+
extern ur_adapter_handle_t_ adapter;
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
//===--------- command_buffer.cpp - OpenCL Adapter ---------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===-----------------------------------------------------------------===//
8+
9+
#include "command_buffer.hpp"
10+
#include "common.hpp"
11+
12+
/// Stub implementations of UR experimental feature command-buffers
13+
14+
UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferCreateExp(
15+
[[maybe_unused]] ur_context_handle_t hContext,
16+
[[maybe_unused]] ur_device_handle_t hDevice,
17+
[[maybe_unused]] const ur_exp_command_buffer_desc_t *pCommandBufferDesc,
18+
[[maybe_unused]] ur_exp_command_buffer_handle_t *phCommandBuffer) {
19+
20+
cl_adapter::die("Experimental Command-buffer feature is not "
21+
"implemented for OpenCL adapter.");
22+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
23+
}
24+
25+
UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferRetainExp(
26+
[[maybe_unused]] ur_exp_command_buffer_handle_t hCommandBuffer) {
27+
28+
cl_adapter::die("Experimental Command-buffer feature is not "
29+
"implemented for OpenCL adapter.");
30+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
31+
}
32+
33+
UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferReleaseExp(
34+
[[maybe_unused]] ur_exp_command_buffer_handle_t hCommandBuffer) {
35+
36+
cl_adapter::die("Experimental Command-buffer feature is not "
37+
"implemented for OpenCL adapter.");
38+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
39+
}
40+
41+
UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferFinalizeExp(
42+
[[maybe_unused]] ur_exp_command_buffer_handle_t hCommandBuffer) {
43+
44+
cl_adapter::die("Experimental Command-buffer feature is not "
45+
"implemented for OpenCL adapter.");
46+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
47+
}
48+
49+
UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
50+
[[maybe_unused]] ur_exp_command_buffer_handle_t hCommandBuffer,
51+
[[maybe_unused]] ur_kernel_handle_t hKernel,
52+
[[maybe_unused]] uint32_t workDim,
53+
[[maybe_unused]] const size_t *pGlobalWorkOffset,
54+
[[maybe_unused]] const size_t *pGlobalWorkSize,
55+
[[maybe_unused]] const size_t *pLocalWorkSize,
56+
[[maybe_unused]] uint32_t numSyncPointsInWaitList,
57+
[[maybe_unused]] const ur_exp_command_buffer_sync_point_t
58+
*pSyncPointWaitList,
59+
[[maybe_unused]] ur_exp_command_buffer_sync_point_t *pSyncPoint) {
60+
61+
cl_adapter::die("Experimental Command-buffer feature is not "
62+
"implemented for OpenCL adapter.");
63+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
64+
}
65+
66+
UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendMemcpyUSMExp(
67+
[[maybe_unused]] ur_exp_command_buffer_handle_t hCommandBuffer,
68+
[[maybe_unused]] void *pDst, [[maybe_unused]] const void *pSrc,
69+
[[maybe_unused]] size_t size,
70+
[[maybe_unused]] uint32_t numSyncPointsInWaitList,
71+
[[maybe_unused]] const ur_exp_command_buffer_sync_point_t
72+
*pSyncPointWaitList,
73+
[[maybe_unused]] ur_exp_command_buffer_sync_point_t *pSyncPoint) {
74+
75+
cl_adapter::die("Experimental Command-buffer feature is not "
76+
"implemented for OpenCL adapter.");
77+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
78+
}
79+
80+
UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendMembufferCopyExp(
81+
[[maybe_unused]] ur_exp_command_buffer_handle_t hCommandBuffer,
82+
[[maybe_unused]] ur_mem_handle_t hSrcMem,
83+
[[maybe_unused]] ur_mem_handle_t hDstMem, [[maybe_unused]] size_t srcOffset,
84+
[[maybe_unused]] size_t dstOffset, [[maybe_unused]] size_t size,
85+
[[maybe_unused]] uint32_t numSyncPointsInWaitList,
86+
[[maybe_unused]] const ur_exp_command_buffer_sync_point_t
87+
*pSyncPointWaitList,
88+
[[maybe_unused]] ur_exp_command_buffer_sync_point_t *pSyncPoint) {
89+
90+
cl_adapter::die("Experimental Command-buffer feature is not "
91+
"implemented for OpenCL adapter.");
92+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
93+
}
94+
95+
UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendMembufferCopyRectExp(
96+
[[maybe_unused]] ur_exp_command_buffer_handle_t hCommandBuffer,
97+
[[maybe_unused]] ur_mem_handle_t hSrcMem,
98+
[[maybe_unused]] ur_mem_handle_t hDstMem,
99+
[[maybe_unused]] ur_rect_offset_t srcOrigin,
100+
[[maybe_unused]] ur_rect_offset_t dstOrigin,
101+
[[maybe_unused]] ur_rect_region_t region,
102+
[[maybe_unused]] size_t srcRowPitch, [[maybe_unused]] size_t srcSlicePitch,
103+
[[maybe_unused]] size_t dstRowPitch, [[maybe_unused]] size_t dstSlicePitch,
104+
[[maybe_unused]] uint32_t numSyncPointsInWaitList,
105+
[[maybe_unused]] const ur_exp_command_buffer_sync_point_t
106+
*pSyncPointWaitList,
107+
[[maybe_unused]] ur_exp_command_buffer_sync_point_t *pSyncPoint) {
108+
109+
cl_adapter::die("Experimental Command-buffer feature is not "
110+
"implemented for OpenCL adapter.");
111+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
112+
}
113+
114+
UR_APIEXPORT
115+
ur_result_t UR_APICALL urCommandBufferAppendMembufferWriteExp(
116+
[[maybe_unused]] ur_exp_command_buffer_handle_t hCommandBuffer,
117+
[[maybe_unused]] ur_mem_handle_t hBuffer, [[maybe_unused]] size_t offset,
118+
[[maybe_unused]] size_t size, [[maybe_unused]] const void *pSrc,
119+
[[maybe_unused]] uint32_t numSyncPointsInWaitList,
120+
[[maybe_unused]] const ur_exp_command_buffer_sync_point_t
121+
*pSyncPointWaitList,
122+
[[maybe_unused]] ur_exp_command_buffer_sync_point_t *pSyncPoint) {
123+
124+
cl_adapter::die("Experimental Command-buffer feature is not "
125+
"implemented for OpenCL adapter.");
126+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
127+
}
128+
129+
UR_APIEXPORT
130+
ur_result_t UR_APICALL urCommandBufferAppendMembufferReadExp(
131+
[[maybe_unused]] ur_exp_command_buffer_handle_t hCommandBuffer,
132+
[[maybe_unused]] ur_mem_handle_t hBuffer, [[maybe_unused]] size_t offset,
133+
[[maybe_unused]] size_t size, [[maybe_unused]] void *pDst,
134+
[[maybe_unused]] uint32_t numSyncPointsInWaitList,
135+
[[maybe_unused]] const ur_exp_command_buffer_sync_point_t
136+
*pSyncPointWaitList,
137+
[[maybe_unused]] ur_exp_command_buffer_sync_point_t *pSyncPoint) {
138+
139+
cl_adapter::die("Experimental Command-buffer feature is not "
140+
"implemented for OpenCL adapter.");
141+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
142+
}
143+
144+
UR_APIEXPORT
145+
ur_result_t UR_APICALL urCommandBufferAppendMembufferWriteRectExp(
146+
[[maybe_unused]] ur_exp_command_buffer_handle_t hCommandBuffer,
147+
[[maybe_unused]] ur_mem_handle_t hBuffer,
148+
[[maybe_unused]] ur_rect_offset_t bufferOffset,
149+
[[maybe_unused]] ur_rect_offset_t hostOffset,
150+
[[maybe_unused]] ur_rect_region_t region,
151+
[[maybe_unused]] size_t bufferRowPitch,
152+
[[maybe_unused]] size_t bufferSlicePitch,
153+
[[maybe_unused]] size_t hostRowPitch,
154+
[[maybe_unused]] size_t hostSlicePitch, [[maybe_unused]] void *pSrc,
155+
[[maybe_unused]] uint32_t numSyncPointsInWaitList,
156+
[[maybe_unused]] const ur_exp_command_buffer_sync_point_t
157+
*pSyncPointWaitList,
158+
[[maybe_unused]] ur_exp_command_buffer_sync_point_t *pSyncPoint) {
159+
160+
cl_adapter::die("Experimental Command-buffer feature is not "
161+
"implemented for OpenCL adapter.");
162+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
163+
}
164+
165+
UR_APIEXPORT
166+
ur_result_t UR_APICALL urCommandBufferAppendMembufferReadRectExp(
167+
[[maybe_unused]] ur_exp_command_buffer_handle_t hCommandBuffer,
168+
[[maybe_unused]] ur_mem_handle_t hBuffer,
169+
[[maybe_unused]] ur_rect_offset_t bufferOffset,
170+
[[maybe_unused]] ur_rect_offset_t hostOffset,
171+
[[maybe_unused]] ur_rect_region_t region,
172+
[[maybe_unused]] size_t bufferRowPitch,
173+
[[maybe_unused]] size_t bufferSlicePitch,
174+
[[maybe_unused]] size_t hostRowPitch,
175+
[[maybe_unused]] size_t hostSlicePitch, [[maybe_unused]] void *pDst,
176+
[[maybe_unused]] uint32_t numSyncPointsInWaitList,
177+
[[maybe_unused]] const ur_exp_command_buffer_sync_point_t
178+
*pSyncPointWaitList,
179+
[[maybe_unused]] ur_exp_command_buffer_sync_point_t *pSyncPoint) {
180+
181+
cl_adapter::die("Experimental Command-buffer feature is not "
182+
"implemented for OpenCL adapter.");
183+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
184+
}
185+
186+
UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferEnqueueExp(
187+
[[maybe_unused]] ur_exp_command_buffer_handle_t hCommandBuffer,
188+
[[maybe_unused]] ur_queue_handle_t hQueue,
189+
[[maybe_unused]] uint32_t numEventsInWaitList,
190+
[[maybe_unused]] const ur_event_handle_t *phEventWaitList,
191+
[[maybe_unused]] ur_event_handle_t *phEvent) {
192+
193+
cl_adapter::die("Experimental Command-buffer feature is not "
194+
"implemented for OpenCL adapter.");
195+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
196+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===--------- command_buffer.hpp - OpenCL Adapter ---------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===-----------------------------------------------------------------===//
8+
9+
#include <ur/ur.hpp>
10+
11+
/// Stub implementation of command-buffers for OpenCL
12+
13+
struct ur_exp_command_buffer_handle_t_ {};

source/adapters/opencl/common.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//===--------- common.hpp - OpenCL Adapter ---------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===-----------------------------------------------------------------===//
8+
9+
#include "common.hpp"
10+
11+
namespace cl_adapter {
12+
13+
/* Global variables for urPlatformGetLastError() */
14+
thread_local int32_t ErrorMessageCode = 0;
15+
thread_local char ErrorMessage[MaxMessageSize];
16+
17+
[[maybe_unused]] void setErrorMessage(const char *Message, int32_t ErrorCode) {
18+
assert(strlen(Message) <= cl_adapter::MaxMessageSize);
19+
strcpy(cl_adapter::ErrorMessage, Message);
20+
ErrorMessageCode = ErrorCode;
21+
}
22+
} // namespace cl_adapter
23+
24+
ur_result_t mapCLErrorToUR(cl_int Result) {
25+
switch (Result) {
26+
case CL_SUCCESS:
27+
return UR_RESULT_SUCCESS;
28+
case CL_OUT_OF_HOST_MEMORY:
29+
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
30+
case CL_INVALID_VALUE:
31+
case CL_INVALID_BUILD_OPTIONS:
32+
return UR_RESULT_ERROR_INVALID_VALUE;
33+
case CL_INVALID_PLATFORM:
34+
return UR_RESULT_ERROR_INVALID_PLATFORM;
35+
case CL_DEVICE_NOT_FOUND:
36+
return UR_RESULT_ERROR_DEVICE_NOT_FOUND;
37+
case CL_INVALID_OPERATION:
38+
return UR_RESULT_ERROR_INVALID_OPERATION;
39+
case CL_INVALID_ARG_VALUE:
40+
return UR_RESULT_ERROR_INVALID_ARGUMENT;
41+
case CL_INVALID_EVENT:
42+
return UR_RESULT_ERROR_INVALID_EVENT;
43+
case CL_INVALID_EVENT_WAIT_LIST:
44+
return UR_RESULT_ERROR_INVALID_EVENT_WAIT_LIST;
45+
case CL_INVALID_BINARY:
46+
return UR_RESULT_ERROR_INVALID_BINARY;
47+
case CL_INVALID_KERNEL_NAME:
48+
return UR_RESULT_ERROR_INVALID_KERNEL_NAME;
49+
case CL_BUILD_PROGRAM_FAILURE:
50+
return UR_RESULT_ERROR_PROGRAM_BUILD_FAILURE;
51+
case CL_INVALID_WORK_GROUP_SIZE:
52+
return UR_RESULT_ERROR_INVALID_WORK_GROUP_SIZE;
53+
case CL_INVALID_WORK_ITEM_SIZE:
54+
return UR_RESULT_ERROR_INVALID_WORK_ITEM_SIZE;
55+
case CL_INVALID_WORK_DIMENSION:
56+
return UR_RESULT_ERROR_INVALID_WORK_DIMENSION;
57+
case CL_OUT_OF_RESOURCES:
58+
return UR_RESULT_ERROR_OUT_OF_RESOURCES;
59+
case CL_INVALID_MEM_OBJECT:
60+
return UR_RESULT_ERROR_INVALID_MEM_OBJECT;
61+
default:
62+
return UR_RESULT_ERROR_UNKNOWN;
63+
}
64+
}
65+
66+
void cl_adapter::die(const char *Message) {
67+
std::cerr << "ur_die: " << Message << "\n";
68+
std::terminate();
69+
}
70+
71+
/// Common API for getting the native handle of a UR object
72+
///
73+
/// \param URObj is the UR object to get the native handle of
74+
/// \param NativeHandle is a pointer to be set to the native handle
75+
///
76+
/// UR_RESULT_SUCCESS
77+
ur_result_t getNativeHandle(void *URObj, ur_native_handle_t *NativeHandle) {
78+
*NativeHandle = reinterpret_cast<ur_native_handle_t>(URObj);
79+
return UR_RESULT_SUCCESS;
80+
}

0 commit comments

Comments
 (0)