-
Notifications
You must be signed in to change notification settings - Fork 750
/
Copy pathhelpers.cpp
104 lines (93 loc) · 4.02 KB
/
helpers.cpp
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
//==---------------- helpers.cpp - SYCL helpers ---------------------------==//
//
// 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/helpers.hpp>
#include <detail/scheduler/commands.hpp>
#include <sycl/detail/helpers.hpp>
#include <detail/buffer_impl.hpp>
#include <detail/context_impl.hpp>
#include <detail/device_binary_image.hpp>
#include <detail/event_impl.hpp>
#include <detail/kernel_bundle_impl.hpp>
#include <detail/queue_impl.hpp>
#include <sycl/event.hpp>
#include <memory>
#include <tuple>
namespace sycl {
inline namespace _V1 {
using ContextImplPtr = std::shared_ptr<sycl::detail::context_impl>;
namespace detail {
void waitEvents(std::vector<sycl::event> DepEvents) {
for (auto SyclEvent : DepEvents) {
detail::getSyclObjImpl(SyclEvent)->waitInternal();
}
}
__SYCL_EXPORT void
markBufferAsInternal(const std::shared_ptr<buffer_impl> &BufImpl) {
BufImpl->markAsInternal();
}
std::tuple<const RTDeviceBinaryImage *, ur_program_handle_t>
retrieveKernelBinary(const QueueImplPtr &Queue, const char *KernelName,
CGExecKernel *KernelCG) {
bool isNvidia =
Queue->getDeviceImplPtr()->getBackend() == backend::ext_oneapi_cuda;
bool isHIP =
Queue->getDeviceImplPtr()->getBackend() == backend::ext_oneapi_hip;
if (isNvidia || isHIP) {
auto KernelID = ProgramManager::getInstance().getSYCLKernelID(KernelName);
std::vector<kernel_id> KernelIds{KernelID};
auto DeviceImages =
ProgramManager::getInstance().getRawDeviceImages(KernelIds);
auto DeviceImage = std::find_if(
DeviceImages.begin(), DeviceImages.end(),
[isNvidia](RTDeviceBinaryImage *DI) {
const std::string &TargetSpec = isNvidia ? std::string("llvm_nvptx64")
: std::string("llvm_amdgcn");
return DI->getFormat() == SYCL_DEVICE_BINARY_TYPE_LLVMIR_BITCODE &&
DI->getRawData().DeviceTargetSpec == TargetSpec;
});
if (DeviceImage == DeviceImages.end()) {
return {nullptr, nullptr};
}
auto ContextImpl = Queue->getContextImplPtr();
auto Context = detail::createSyclObjFromImpl<context>(ContextImpl);
auto DeviceImpl = Queue->getDeviceImplPtr();
auto Device = detail::createSyclObjFromImpl<device>(DeviceImpl);
ur_program_handle_t Program =
detail::ProgramManager::getInstance().createURProgram(
**DeviceImage, Context, {Device});
return {*DeviceImage, Program};
}
const RTDeviceBinaryImage *DeviceImage = nullptr;
ur_program_handle_t Program = nullptr;
if (KernelCG->getKernelBundle() != nullptr) {
// Retrieve the device image from the kernel bundle.
auto KernelBundle = KernelCG->getKernelBundle();
kernel_id KernelID =
detail::ProgramManager::getInstance().getSYCLKernelID(KernelName);
auto SyclKernel = detail::getSyclObjImpl(
KernelBundle->get_kernel(KernelID, KernelBundle));
DeviceImage = SyclKernel->getDeviceImage()->get_bin_image_ref();
Program = SyclKernel->getDeviceImage()->get_ur_program_ref();
} else if (KernelCG->MSyclKernel != nullptr) {
DeviceImage = KernelCG->MSyclKernel->getDeviceImage()->get_bin_image_ref();
Program = KernelCG->MSyclKernel->getDeviceImage()->get_ur_program_ref();
} else {
auto ContextImpl = Queue->getContextImplPtr();
auto Context = detail::createSyclObjFromImpl<context>(ContextImpl);
auto DeviceImpl = Queue->getDeviceImplPtr();
auto Device = detail::createSyclObjFromImpl<device>(DeviceImpl);
DeviceImage = &detail::ProgramManager::getInstance().getDeviceImage(
KernelName, Context, Device);
Program = detail::ProgramManager::getInstance().createURProgram(
*DeviceImage, Context, {std::move(Device)});
}
return {DeviceImage, Program};
}
} // namespace detail
} // namespace _V1
} // namespace sycl