DPC++ Runtime
Runtime libraries for oneAPI DPC++
jit_device_binaries.cpp
Go to the documentation of this file.
1 //==- jit_device_binaries.cpp - Runtime construction of PI device binaries -==//
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 
10 
11 #include <cassert>
12 
13 namespace sycl {
14 inline namespace _V1 {
15 namespace detail {
16 
18  void *Addr, size_t Size,
19  int32_t Flags, int32_t Reserved)
20  : KernelName{new char[Name.length() + 1]}, Address{Addr}, EntrySize{Size},
21  EntryFlags{Flags}, EntryReserved{Reserved} {
22  std::memcpy(KernelName.get(), Name.c_str(), Name.length() + 1);
23 }
24 
26  return _pi_offload_entry_struct{Address, KernelName.get(), EntrySize,
27  EntryFlags, EntryReserved};
28 }
29 
30 PropertyContainer::PropertyContainer(const std::string &Name, void *Data,
31  size_t Size, uint32_t Type)
32  : PropName{new char[Name.length() + 1]}, Value{new unsigned char[Size]},
33  ValueSize{Size}, PropType{Type} {
34  std::memcpy(PropName.get(), Name.c_str(), Name.length() + 1);
35  std::memcpy(Value.get(), Data, Size);
36 }
37 
38 PropertyContainer::PropertyContainer(const std::string &Name, uint32_t Data)
39  : PropName{new char[Name.length() + 1]}, Value{}, ValueSize{Data},
40  PropType{PI_PROPERTY_TYPE_UINT32} {
41  std::memcpy(PropName.get(), Name.c_str(), Name.length() + 1);
42 }
43 
45  return _pi_device_binary_property_struct{PropName.get(), Value.get(),
46  PropType, ValueSize};
47 }
48 
50  : SetName{new char[Name.length() + 1]} {
51  std::memcpy(SetName.get(), Name.c_str(), Name.length() + 1);
52 }
53 
55  // Adding to the vectors might trigger reallocation, which would invalidate
56  // the pointers used for PI structs if a PI struct has already been created
57  // via getPIPropertySet(). Forbid calls to this method after the first PI
58  // struct has been created.
59  assert(Fused && "Adding to container would invalidate existing PI structs");
60  PIProperties.push_back(Prop.getPIProperty());
61  Properties.push_back(std::move(Prop));
62 }
63 
65  Fused = false;
67  const_cast<char *>(SetName.get()), PIProperties.data(),
68  PIProperties.data() + Properties.size()};
69 }
70 
72  // Adding to the vectors might trigger reallocation, which would invalidate
73  // the pointers used for PI structs if a PI struct has already been created
74  // via getPIDeviceBinary(). Forbid calls to this method after the first PI
75  // struct has been created.
76  assert(Fused && "Adding to container would invalidate existing PI structs");
77  PIOffloadEntries.push_back(Cont.getPIOffloadEntry());
78  OffloadEntries.push_back(std::move(Cont));
79 }
80 
82  // Adding to the vectors might trigger reallocation, which would invalidate
83  // the pointers used for PI structs if a PI struct has already been created
84  // via getPIDeviceBinary(). Forbid calls to this method after the first PI
85  // struct has been created.
86  assert(Fused && "Adding to container would invalidate existing PI structs");
87  PIPropertySets.push_back(Cont.getPIPropertySet());
88  PropertySets.push_back(std::move(Cont));
89 }
90 
92  const unsigned char *BinaryStart, size_t BinarySize, const char *TargetSpec,
93  pi_device_binary_type Format) {
94  pi_device_binary_struct DeviceBinary;
95  DeviceBinary.Version = PI_DEVICE_BINARY_VERSION;
97  DeviceBinary.Format = Format;
98  DeviceBinary.CompileOptions = "";
99  DeviceBinary.LinkOptions = "";
100  DeviceBinary.ManifestStart = nullptr;
101  DeviceBinary.ManifestEnd = nullptr;
102  // It is safe to use these pointers here, as their lifetime is managed by
103  // the JITContext.
104  DeviceBinary.BinaryStart = BinaryStart;
105  DeviceBinary.BinaryEnd = BinaryStart + BinarySize;
106  DeviceBinary.DeviceTargetSpec = TargetSpec;
107  DeviceBinary.EntriesBegin = PIOffloadEntries.data();
108  DeviceBinary.EntriesEnd = PIOffloadEntries.data() + PIOffloadEntries.size();
109  DeviceBinary.PropertySetsBegin = PIPropertySets.data();
110  DeviceBinary.PropertySetsEnd = PIPropertySets.data() + PIPropertySets.size();
111  Fused = false;
112  return DeviceBinary;
113 }
114 
116  const unsigned char *BinaryStart,
117  size_t BinarySize,
118  const char *TargetSpec,
119  pi_device_binary_type Format) {
120  // Adding to the vectors might trigger reallocation, which would invalidate
121  // the pointers used for PI structs if a PI struct has already been created
122  // via getPIDeviceStruct(). Forbid calls to this method after the first PI
123  // struct has been created.
124  assert(Fused && "Adding to container would invalidate existing PI structs");
125  PIBinaries.push_back(
126  Cont.getPIDeviceBinary(BinaryStart, BinarySize, TargetSpec, Format));
127  Binaries.push_back(std::move(Cont));
128 }
129 
131 
132  PIStruct = std::make_unique<pi_device_binaries_struct>();
133  PIStruct->Version = PI_DEVICE_BINARIES_VERSION;
134  PIStruct->NumDeviceBinaries = PIBinaries.size();
135  PIStruct->DeviceBinaries = PIBinaries.data();
136  // According to documentation in pi.h, the HostEntries are not used and
137  // can therefore be null.
138  PIStruct->HostEntriesBegin = nullptr;
139  PIStruct->HostEntriesEnd = nullptr;
140  Fused = false;
141  return PIStruct.get();
142 }
143 
144 } // namespace detail
145 } // namespace _V1
146 } // namespace sycl
void addDeviceBinary(DeviceBinaryContainer &&Cont, const unsigned char *BinaryStart, size_t BinarySize, const char *TargetSpec, pi_device_binary_type Format)
Representation of pi_device_binary_struct for creation of JIT device binaries at runtime.
void addOffloadEntry(OffloadEntryContainer &&Cont)
pi_device_binary_struct getPIDeviceBinary(const unsigned char *BinaryStart, size_t BinarySize, const char *TargetSpec, pi_device_binary_type Format)
void addProperty(PropertySetContainer &&Cont)
Representation of _pi_offload_entry for creation of JIT device binaries at runtime.
OffloadEntryContainer(const std::string &Name, void *Addr, size_t Size, int32_t Flags, int32_t Reserved)
Representation of _pi_device_binary_property_struct for creation of JIT device binaries at runtime.
_pi_device_binary_property_struct getPIProperty()
PropertyContainer(const std::string &Name, void *Data, size_t Size, uint32_t Type)
Representation of _pi_device_binary_property_set_struct for creation of JIT device binaries at runtim...
void addProperty(PropertyContainer &&Prop)
_pi_device_binary_property_set_struct getPIPropertySet()
Definition: access.hpp:18
ValueT length(const ValueT *a, const int len)
Calculate the square root of the input array.
Definition: math.hpp:161
@ PI_PROPERTY_TYPE_UINT32
Definition: pi.h:911
uint8_t pi_device_binary_type
Types of device binary.
Definition: pi.h:939
static const uint16_t PI_DEVICE_BINARY_VERSION
Definition: pi.h:951
static const uint8_t PI_DEVICE_BINARY_OFFLOAD_KIND_SYCL
Definition: pi.h:954
static const uint16_t PI_DEVICE_BINARIES_VERSION
Definition: pi.h:1117
Definition: pi.h:898
This struct is a record of all the device code that may be offloaded.
Definition: pi.h:1122
This struct is a record of the device binary information.
Definition: pi.h:1025
_pi_offload_entry EntriesEnd
Definition: pi.h:1062
const char * LinkOptions
a null-terminated string; target- and compiler-specific options which are suggested to use to "link" ...
Definition: pi.h:1051
_pi_offload_entry EntriesBegin
the offload entry table
Definition: pi.h:1061
const char * CompileOptions
a null-terminated string; target- and compiler-specific options which are suggested to use to "compil...
Definition: pi.h:1048
const unsigned char * BinaryEnd
Pointer to the target code end.
Definition: pi.h:1059
const char * ManifestStart
Pointer to the manifest data start.
Definition: pi.h:1053
uint16_t Version
version of this structure - for backward compatibility; all modifications which change order/type/off...
Definition: pi.h:1029
const char * DeviceTargetSpec
null-terminated string representation of the device's target architecture which holds one of: __SYCL_...
Definition: pi.h:1045
pi_device_binary_property_set PropertySetsEnd
Definition: pi.h:1066
pi_device_binary_property_set PropertySetsBegin
Definition: pi.h:1065
uint8_t Format
format of the binary data - SPIR-V, LLVM IR bitcode,...
Definition: pi.h:1033
const char * ManifestEnd
Pointer to the manifest data end.
Definition: pi.h:1055
uint8_t Kind
the type of offload model the binary employs; must be 4 for SYCL
Definition: pi.h:1031
const unsigned char * BinaryStart
Pointer to the target code start.
Definition: pi.h:1057