forked from intel/llvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.cpp
More file actions
375 lines (328 loc) · 14.8 KB
/
backend.cpp
File metadata and controls
375 lines (328 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
//==------------------- backend.cpp ----------------------------------------==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "detail/adapter.hpp"
#include "detail/context_impl.hpp"
#include "detail/event_impl.hpp"
#include "detail/kernel_bundle_impl.hpp"
#include "detail/kernel_id_impl.hpp"
#include "detail/platform_impl.hpp"
#include "detail/queue_impl.hpp"
#include <detail/ur.hpp>
#include <sycl/backend.hpp>
#include <sycl/detail/common.hpp>
#include <sycl/detail/export.hpp>
#include <sycl/detail/impl_utils.hpp>
#include <sycl/detail/ur.hpp>
#include <sycl/exception.hpp>
#include <sycl/exception_list.hpp>
#include <sycl/kernel_bundle.hpp>
#include <algorithm>
#include <memory>
namespace sycl {
inline namespace _V1 {
namespace detail {
static const AdapterPtr &getAdapter(backend Backend) {
switch (Backend) {
case backend::opencl:
return ur::getAdapter<backend::opencl>();
case backend::ext_oneapi_level_zero:
return ur::getAdapter<backend::ext_oneapi_level_zero>();
case backend::ext_oneapi_cuda:
return ur::getAdapter<backend::ext_oneapi_cuda>();
case backend::ext_oneapi_hip:
return ur::getAdapter<backend::ext_oneapi_hip>();
default:
throw sycl::exception(
sycl::make_error_code(sycl::errc::runtime),
"getAdapter: Unsupported backend " +
detail::codeToString(UR_RESULT_ERROR_INVALID_OPERATION));
}
}
backend convertUrBackend(ur_platform_backend_t UrBackend) {
switch (UrBackend) {
case UR_PLATFORM_BACKEND_UNKNOWN:
return backend::all; // No specific backend
case UR_PLATFORM_BACKEND_LEVEL_ZERO:
return backend::ext_oneapi_level_zero;
case UR_PLATFORM_BACKEND_OPENCL:
return backend::opencl;
case UR_PLATFORM_BACKEND_CUDA:
return backend::ext_oneapi_cuda;
case UR_PLATFORM_BACKEND_HIP:
return backend::ext_oneapi_hip;
case UR_PLATFORM_BACKEND_NATIVE_CPU:
return backend::ext_oneapi_native_cpu;
default:
throw exception(make_error_code(errc::runtime),
"convertBackend: Unsupported backend");
}
}
platform make_platform(ur_native_handle_t NativeHandle, backend Backend) {
const auto &Adapter = getAdapter(Backend);
// Create UR platform first.
ur_platform_handle_t UrPlatform = nullptr;
Adapter->call<UrApiKind::urPlatformCreateWithNativeHandle>(
NativeHandle, Adapter->getUrAdapter(), nullptr, &UrPlatform);
return detail::createSyclObjFromImpl<platform>(
platform_impl::getOrMakePlatformImpl(UrPlatform, Adapter));
}
__SYCL_EXPORT device make_device(ur_native_handle_t NativeHandle,
backend Backend) {
const auto &Adapter = getAdapter(Backend);
ur_device_handle_t UrDevice = nullptr;
Adapter->call<UrApiKind::urDeviceCreateWithNativeHandle>(
NativeHandle, Adapter->getUrAdapter(), nullptr, &UrDevice);
// Construct the SYCL device from UR device.
auto &Platform = platform_impl::getPlatformFromUrDevice(UrDevice, Adapter);
return detail::createSyclObjFromImpl<device>(
Platform.getOrMakeDeviceImpl(UrDevice, Platform));
}
__SYCL_EXPORT context make_context(ur_native_handle_t NativeHandle,
const async_handler &Handler,
backend Backend, bool KeepOwnership,
const std::vector<device> &DeviceList) {
const auto &Adapter = getAdapter(Backend);
ur_context_handle_t UrContext = nullptr;
ur_context_native_properties_t Properties{};
Properties.stype = UR_STRUCTURE_TYPE_CONTEXT_NATIVE_PROPERTIES;
Properties.isNativeHandleOwned = false;
std::vector<ur_device_handle_t> DeviceHandles;
for (const auto &Dev : DeviceList) {
DeviceHandles.push_back(detail::getSyclObjImpl(Dev)->getHandleRef());
}
Adapter->call<UrApiKind::urContextCreateWithNativeHandle>(
NativeHandle, Adapter->getUrAdapter(), DeviceHandles.size(),
DeviceHandles.data(), &Properties, &UrContext);
// Construct the SYCL context from UR context.
return detail::createSyclObjFromImpl<context>(std::make_shared<context_impl>(
UrContext, Handler, Adapter, DeviceList, !KeepOwnership));
}
__SYCL_EXPORT queue make_queue(ur_native_handle_t NativeHandle,
int32_t NativeHandleDesc, const context &Context,
const device *Device, bool KeepOwnership,
const property_list &PropList,
const async_handler &Handler, backend Backend) {
ur_device_handle_t UrDevice =
Device ? getSyclObjImpl(*Device)->getHandleRef() : nullptr;
const auto &Adapter = getAdapter(Backend);
const auto &ContextImpl = getSyclObjImpl(Context);
if (PropList.has_property<ext::intel::property::queue::compute_index>()) {
throw sycl::exception(
make_error_code(errc::invalid),
"Queue create using make_queue cannot have compute_index property.");
}
ur_queue_native_desc_t Desc{};
Desc.stype = UR_STRUCTURE_TYPE_QUEUE_NATIVE_DESC;
Desc.pNativeData = &NativeHandleDesc;
ur_queue_properties_t Properties{};
Properties.stype = UR_STRUCTURE_TYPE_QUEUE_PROPERTIES;
Properties.flags = queue_impl::createUrQueueFlags(
PropList, PropList.has_property<property::queue::in_order>()
? QueueOrder::Ordered
: QueueOrder::OOO);
ur_queue_native_properties_t NativeProperties{};
NativeProperties.stype = UR_STRUCTURE_TYPE_QUEUE_NATIVE_PROPERTIES;
NativeProperties.isNativeHandleOwned = !KeepOwnership;
Properties.pNext = &Desc;
NativeProperties.pNext = &Properties;
// Create UR queue first.
ur_queue_handle_t UrQueue = nullptr;
Adapter->call<UrApiKind::urQueueCreateWithNativeHandle>(
NativeHandle, ContextImpl->getHandleRef(), UrDevice, &NativeProperties,
&UrQueue);
// Construct the SYCL queue from UR queue.
return detail::createSyclObjFromImpl<queue>(
std::make_shared<queue_impl>(UrQueue, ContextImpl, Handler, PropList));
}
__SYCL_EXPORT event make_event(ur_native_handle_t NativeHandle,
const context &Context, backend Backend) {
return make_event(NativeHandle, Context, false, Backend);
}
__SYCL_EXPORT event make_event(ur_native_handle_t NativeHandle,
const context &Context, bool KeepOwnership,
backend Backend) {
const auto &Adapter = getAdapter(Backend);
const auto &ContextImpl = getSyclObjImpl(Context);
ur_event_handle_t UrEvent = nullptr;
ur_event_native_properties_t Properties{};
Properties.stype = UR_STRUCTURE_TYPE_EVENT_NATIVE_PROPERTIES;
Properties.isNativeHandleOwned = !KeepOwnership;
Adapter->call<UrApiKind::urEventCreateWithNativeHandle>(
NativeHandle, ContextImpl->getHandleRef(), &Properties, &UrEvent);
event Event = detail::createSyclObjFromImpl<event>(
std::make_shared<event_impl>(UrEvent, Context));
if (Backend == backend::opencl)
__SYCL_OCL_CALL(clRetainEvent, ur::cast<cl_event>(NativeHandle));
return Event;
}
std::shared_ptr<detail::kernel_bundle_impl>
make_kernel_bundle(ur_native_handle_t NativeHandle,
const context &TargetContext, bool KeepOwnership,
bundle_state State, backend Backend) {
const auto &Adapter = getAdapter(Backend);
const auto &ContextImpl = getSyclObjImpl(TargetContext);
ur_program_handle_t UrProgram = nullptr;
ur_program_native_properties_t Properties{};
Properties.stype = UR_STRUCTURE_TYPE_PROGRAM_NATIVE_PROPERTIES;
Properties.isNativeHandleOwned = !KeepOwnership;
Adapter->call<UrApiKind::urProgramCreateWithNativeHandle>(
NativeHandle, ContextImpl->getHandleRef(), &Properties, &UrProgram);
if (UrProgram == nullptr)
throw sycl::exception(
sycl::make_error_code(sycl::errc::invalid),
"urProgramCreateWithNativeHandle resulted in a null program handle.");
if (ContextImpl->getBackend() == backend::opencl)
__SYCL_OCL_CALL(clRetainProgram, ur::cast<cl_program>(NativeHandle));
std::vector<ur_device_handle_t> ProgramDevices;
uint32_t NumDevices = 0;
Adapter->call<UrApiKind::urProgramGetInfo>(
UrProgram, UR_PROGRAM_INFO_NUM_DEVICES, sizeof(NumDevices), &NumDevices,
nullptr);
ProgramDevices.resize(NumDevices);
Adapter->call<UrApiKind::urProgramGetInfo>(
UrProgram, UR_PROGRAM_INFO_DEVICES,
sizeof(ur_device_handle_t) * NumDevices, ProgramDevices.data(), nullptr);
for (auto &Dev : ProgramDevices) {
ur_program_binary_type_t BinaryType;
Adapter->call<UrApiKind::urProgramGetBuildInfo>(
UrProgram, Dev, UR_PROGRAM_BUILD_INFO_BINARY_TYPE,
sizeof(ur_program_binary_type_t), &BinaryType, nullptr);
switch (BinaryType) {
case (UR_PROGRAM_BINARY_TYPE_NONE):
if (State == bundle_state::object) {
auto Res = Adapter->call_nocheck<UrApiKind::urProgramCompileExp>(
UrProgram, 1, &Dev, nullptr);
if (Res == UR_RESULT_ERROR_UNSUPPORTED_FEATURE) {
Res = Adapter->call_nocheck<UrApiKind::urProgramCompile>(
ContextImpl->getHandleRef(), UrProgram, nullptr);
}
Adapter->checkUrResult<errc::build>(Res);
}
else if (State == bundle_state::executable) {
auto Res = Adapter->call_nocheck<UrApiKind::urProgramBuildExp>(
UrProgram, 1, &Dev, nullptr);
if (Res == UR_RESULT_ERROR_UNSUPPORTED_FEATURE) {
Res = Adapter->call_nocheck<UrApiKind::urProgramBuild>(
ContextImpl->getHandleRef(), UrProgram, nullptr);
}
Adapter->checkUrResult<errc::build>(Res);
}
break;
case (UR_PROGRAM_BINARY_TYPE_COMPILED_OBJECT):
case (UR_PROGRAM_BINARY_TYPE_LIBRARY):
if (State == bundle_state::input)
throw sycl::exception(
sycl::make_error_code(sycl::errc::runtime),
"Program and kernel_bundle state mismatch " +
detail::codeToString(UR_RESULT_ERROR_INVALID_VALUE));
if (State == bundle_state::executable) {
ur_program_handle_t UrLinkedProgram = nullptr;
auto Res = Adapter->call_nocheck<UrApiKind::urProgramLinkExp>(
ContextImpl->getHandleRef(), 1, &Dev, 1, &UrProgram, nullptr,
&UrLinkedProgram);
if (Res == UR_RESULT_ERROR_UNSUPPORTED_FEATURE) {
Res = Adapter->call_nocheck<UrApiKind::urProgramLink>(
ContextImpl->getHandleRef(), 1, &UrProgram, nullptr,
&UrLinkedProgram);
}
Adapter->checkUrResult<errc::build>(Res);
if (UrLinkedProgram != nullptr) {
UrProgram = UrLinkedProgram;
}
}
break;
case (UR_PROGRAM_BINARY_TYPE_EXECUTABLE):
if (State == bundle_state::input || State == bundle_state::object)
throw sycl::exception(
sycl::make_error_code(sycl::errc::runtime),
"Program and kernel_bundle state mismatch " +
detail::codeToString(UR_RESULT_ERROR_INVALID_VALUE));
break;
default:
break;
}
}
std::vector<device> Devices;
Devices.reserve(ProgramDevices.size());
std::transform(
ProgramDevices.begin(), ProgramDevices.end(), std::back_inserter(Devices),
[&Adapter](const auto &Dev) {
platform_impl &Platform =
detail::platform_impl::getPlatformFromUrDevice(Dev, Adapter);
auto DeviceImpl = Platform.getOrMakeDeviceImpl(Dev, Platform);
return createSyclObjFromImpl<device>(DeviceImpl);
});
// Unlike SYCL, other backends, like OpenCL or Level Zero, may not support
// getting kernel IDs before executable is built. The SYCL Runtime workarounds
// this by pre-building the device image and extracting kernel info. We can't
// do the same to user images, since they may contain references to undefined
// symbols (e.g. when kernel_bundle is supposed to be joined with another).
auto KernelIDs = std::make_shared<std::vector<kernel_id>>();
auto DevImgImpl = std::make_shared<device_image_impl>(
nullptr, TargetContext, Devices, State, KernelIDs, UrProgram,
ImageOriginInterop);
device_image_plain DevImg{DevImgImpl};
return std::make_shared<kernel_bundle_impl>(TargetContext, Devices, DevImg);
}
// TODO: Unused. Remove when allowed.
std::shared_ptr<detail::kernel_bundle_impl>
make_kernel_bundle(ur_native_handle_t NativeHandle,
const context &TargetContext, bundle_state State,
backend Backend) {
return make_kernel_bundle(NativeHandle, TargetContext, false, State, Backend);
}
kernel make_kernel(const context &TargetContext,
const kernel_bundle<bundle_state::executable> &KernelBundle,
ur_native_handle_t NativeHandle, bool KeepOwnership,
backend Backend) {
const auto &Adapter = getAdapter(Backend);
const auto &ContextImpl = getSyclObjImpl(TargetContext);
const auto &KernelBundleImpl = getSyclObjImpl(KernelBundle);
// For Level-Zero expect exactly one device image in the bundle. This is
// natural for interop kernel to get created out of a single native
// program/module. This way we don't need to search the exact device image for
// the kernel, which may not be trivial.
//
// Other backends don't need UR program.
//
ur_program_handle_t UrProgram = nullptr;
if (Backend == backend::ext_oneapi_level_zero) {
if (KernelBundleImpl->size() != 1)
throw sycl::exception(
sycl::make_error_code(sycl::errc::runtime),
"make_kernel: kernel_bundle must have single program image " +
detail::codeToString(UR_RESULT_ERROR_INVALID_PROGRAM));
const device_image<bundle_state::executable> &DeviceImage =
*KernelBundle.begin();
const auto &DeviceImageImpl = getSyclObjImpl(DeviceImage);
UrProgram = DeviceImageImpl->get_ur_program_ref();
}
// Create UR kernel first.
ur_kernel_handle_t UrKernel = nullptr;
ur_kernel_native_properties_t Properties{};
Properties.stype = UR_STRUCTURE_TYPE_KERNEL_NATIVE_PROPERTIES;
Properties.isNativeHandleOwned = !KeepOwnership;
Adapter->call<UrApiKind::urKernelCreateWithNativeHandle>(
NativeHandle, ContextImpl->getHandleRef(), UrProgram, &Properties,
&UrKernel);
if (Backend == backend::opencl)
__SYCL_OCL_CALL(clRetainKernel, ur::cast<cl_kernel>(NativeHandle));
// Construct the SYCL queue from UR queue.
return detail::createSyclObjFromImpl<kernel>(
std::make_shared<kernel_impl>(UrKernel, ContextImpl, KernelBundleImpl));
}
kernel make_kernel(ur_native_handle_t NativeHandle,
const context &TargetContext, backend Backend) {
return make_kernel(
TargetContext,
get_empty_interop_kernel_bundle<bundle_state::executable>(TargetContext),
NativeHandle, false, Backend);
}
} // namespace detail
} // namespace _V1
} // namespace sycl