|
| 1 | +//==------- device_image_impl.hpp - SYCL device_image_impl -----------------==// |
| 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 | +#pragma once |
| 10 | + |
| 11 | +#include <CL/sycl/context.hpp> |
| 12 | +#include <CL/sycl/detail/common.hpp> |
| 13 | +#include <CL/sycl/detail/pi.h> |
| 14 | +#include <CL/sycl/device.hpp> |
| 15 | +#include <CL/sycl/kernel_bundle.hpp> |
| 16 | +#include <detail/device_impl.hpp> |
| 17 | +#include <detail/kernel_id_impl.hpp> |
| 18 | +#include <detail/program_manager/program_manager.hpp> |
| 19 | + |
| 20 | +#include <algorithm> |
| 21 | +#include <cassert> |
| 22 | +#include <cstring> |
| 23 | +#include <memory> |
| 24 | +#include <mutex> |
| 25 | +#include <vector> |
| 26 | + |
| 27 | +__SYCL_INLINE_NAMESPACE(cl) { |
| 28 | +namespace sycl { |
| 29 | +namespace detail { |
| 30 | + |
| 31 | +// Used for sorting vector of kernel_id's |
| 32 | +struct LessByNameComp { |
| 33 | + bool operator()(const sycl::kernel_id &LHS, const sycl::kernel_id &RHS) { |
| 34 | + return std::strcmp(LHS.get_name(), RHS.get_name()) < 0; |
| 35 | + } |
| 36 | +}; |
| 37 | + |
| 38 | +// The class is impl counterpart for sycl::device_image |
| 39 | +// It can represent a program in different states, kernel_id's it has and state |
| 40 | +// of specialization constants for it |
| 41 | +class device_image_impl { |
| 42 | +public: |
| 43 | + device_image_impl(RTDeviceBinaryImage *BinImage, context Context, |
| 44 | + std::vector<device> Devices, bundle_state State) |
| 45 | + : MBinImage(BinImage), MContext(std::move(Context)), |
| 46 | + MDevices(std::move(Devices)), MState(State) { |
| 47 | + |
| 48 | + // Collect kernel names for the image |
| 49 | + pi_device_binary DevBin = |
| 50 | + const_cast<pi_device_binary>(&BinImage->getRawData()); |
| 51 | + for (_pi_offload_entry EntriesIt = DevBin->EntriesBegin; |
| 52 | + EntriesIt != DevBin->EntriesEnd; ++EntriesIt) { |
| 53 | + |
| 54 | + std::shared_ptr<detail::kernel_id_impl> KernleIDImpl = |
| 55 | + std::make_shared<detail::kernel_id_impl>(EntriesIt->name); |
| 56 | + |
| 57 | + sycl::kernel_id KernelID = |
| 58 | + detail::createSyclObjFromImpl<sycl::kernel_id>(KernleIDImpl); |
| 59 | + |
| 60 | + // Insert new element keeping MKernelIDs sorted. |
| 61 | + auto It = std::lower_bound(MKernelIDs.begin(), MKernelIDs.end(), KernelID, |
| 62 | + LessByNameComp{}); |
| 63 | + MKernelIDs.insert(It, std::move(KernelID)); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + bool has_kernel(const kernel_id &KernelIDCand) const noexcept { |
| 68 | + return std::binary_search(MKernelIDs.begin(), MKernelIDs.end(), |
| 69 | + KernelIDCand, LessByNameComp{}); |
| 70 | + } |
| 71 | + |
| 72 | + bool has_kernel(const kernel_id &KernelIDCand, |
| 73 | + const device &DeviceCand) const noexcept { |
| 74 | + for (const device &Device : MDevices) |
| 75 | + if (Device == DeviceCand) |
| 76 | + return has_kernel(KernelIDCand); |
| 77 | + |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + const std::vector<kernel_id> &get_kernel_ids() const noexcept { |
| 82 | + return MKernelIDs; |
| 83 | + } |
| 84 | + |
| 85 | + bool has_specialization_constants() const noexcept { |
| 86 | + return !MSpecConstsBlob.empty(); |
| 87 | + } |
| 88 | + |
| 89 | + bool all_specialization_constant_native() const noexcept { |
| 90 | + assert(false && "Not implemented"); |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + // The struct maps specialization ID to offset in the binary blob where value |
| 95 | + // for this spec const should be. |
| 96 | + struct SpecConstIDOffset { |
| 97 | + unsigned int ID = 0; |
| 98 | + unsigned int Offset = 0; |
| 99 | + }; |
| 100 | + |
| 101 | + bool has_specialization_constant(unsigned int SpecID) const noexcept { |
| 102 | + return std::any_of( |
| 103 | + MSpecConstOffsets.begin(), MSpecConstOffsets.end(), |
| 104 | + [SpecID](const SpecConstIDOffset &Pair) { return Pair.ID == SpecID; }); |
| 105 | + } |
| 106 | + |
| 107 | + void set_specialization_constant_raw_value(unsigned int SpecID, |
| 108 | + const void *Value, |
| 109 | + size_t ValueSize) noexcept { |
| 110 | + for (const SpecConstIDOffset &Pair : MSpecConstOffsets) |
| 111 | + if (Pair.ID == SpecID) { |
| 112 | + // Lock the mutex to prevent when one thread in the middle of writing a |
| 113 | + // new value while another thread is reading the value to pass it to |
| 114 | + // JIT compiler. |
| 115 | + const std::lock_guard<std::mutex> SpecConstLock(MSpecConstAccessMtx); |
| 116 | + std::memcpy(MSpecConstsBlob.data() + Pair.Offset, Value, ValueSize); |
| 117 | + return; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + void get_specialization_constant_raw_value(unsigned int SpecID, |
| 122 | + void *ValueRet, |
| 123 | + size_t ValueSize) const noexcept { |
| 124 | + for (const SpecConstIDOffset &Pair : MSpecConstOffsets) |
| 125 | + if (Pair.ID == SpecID) { |
| 126 | + // Lock the mutex to prevent when one thread in the middle of writing a |
| 127 | + // new value while another thread is reading the value to pass it to |
| 128 | + // JIT compiler. |
| 129 | + const std::lock_guard<std::mutex> SpecConstLock(MSpecConstAccessMtx); |
| 130 | + std::memcpy(ValueRet, MSpecConstsBlob.data() + Pair.Offset, ValueSize); |
| 131 | + return; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + bundle_state get_state() const noexcept { return MState; } |
| 136 | + |
| 137 | + void set_state(bundle_state NewState) noexcept { MState = NewState; } |
| 138 | + |
| 139 | +private: |
| 140 | + RTDeviceBinaryImage *MBinImage = nullptr; |
| 141 | + context MContext; |
| 142 | + std::vector<device> MDevices; |
| 143 | + bundle_state MState; |
| 144 | + // List of kernel ids available in this image, elements should be sorted |
| 145 | + // according to LessByNameComp |
| 146 | + std::vector<kernel_id> MKernelIDs; |
| 147 | + |
| 148 | + // A mutex for sycnhronizing access to spec constants blob. Mutable because |
| 149 | + // needs to be locked in the const method for getting spec constant value. |
| 150 | + mutable std::mutex MSpecConstAccessMtx; |
| 151 | + // Binary blob which can have values of all specialization constants in the |
| 152 | + // image |
| 153 | + std::vector<unsigned char> MSpecConstsBlob; |
| 154 | + // Contains list of spec ID + their offsets in the MSpecConstsBlob |
| 155 | + std::vector<SpecConstIDOffset> MSpecConstOffsets; |
| 156 | +}; |
| 157 | + |
| 158 | +} // namespace detail |
| 159 | +} // namespace sycl |
| 160 | +} // __SYCL_INLINE_NAMESPACE(cl) |
0 commit comments