1 //===- GlobalHandler.h - Target independent global & enviroment handling --===//
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
7 //===----------------------------------------------------------------------===//
9 // Target independent global handler and environment manager.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_COMMON_GLOBALHANDLER_H
14 #define LLVM_OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_COMMON_GLOBALHANDLER_H
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/Object/ELFObjectFile.h"
21 #include "Shared/Debug.h"
22 #include "Shared/Utils.h"
24 #include "omptarget.h"
32 struct GenericDeviceTy
;
34 using namespace llvm::object
;
36 /// Common abstraction for globals that live on the host and device.
37 /// It simply encapsulates the symbol name, symbol size, and symbol address
38 /// (which might be host or device depending on the context).
40 // NOTE: Maybe we can have a pointer to the offload entry name instead of
41 // holding a private copy of the name as a std::string.
47 GlobalTy(const std::string
&Name
, uint32_t Size
, void *Ptr
= nullptr)
48 : Name(Name
), Size(Size
), Ptr(Ptr
) {}
50 const std::string
&getName() const { return Name
; }
51 uint32_t getSize() const { return Size
; }
52 void *getPtr() const { return Ptr
; }
54 void setSize(int32_t S
) { Size
= S
; }
55 void setPtr(void *P
) { Ptr
= P
; }
58 /// Subclass of GlobalTy that holds the memory for a global of \p Ty.
59 template <typename Ty
> class StaticGlobalTy
: public GlobalTy
{
63 template <typename
... Args
>
64 StaticGlobalTy(const std::string
&Name
, Args
&&...args
)
65 : GlobalTy(Name
, sizeof(Ty
), &Data
),
66 Data(Ty
{std::forward
<Args
>(args
)...}) {}
68 template <typename
... Args
>
69 StaticGlobalTy(const char *Name
, Args
&&...args
)
70 : GlobalTy(Name
, sizeof(Ty
), &Data
),
71 Data(Ty
{std::forward
<Args
>(args
)...}) {}
73 template <typename
... Args
>
74 StaticGlobalTy(const char *Name
, const char *Suffix
, Args
&&...args
)
75 : GlobalTy(std::string(Name
) + Suffix
, sizeof(Ty
), &Data
),
76 Data(Ty
{std::forward
<Args
>(args
)...}) {}
78 Ty
&getValue() { return Data
; }
79 const Ty
&getValue() const { return Data
; }
80 void setValue(const Ty
&V
) { Data
= V
; }
83 /// Helper class to do the heavy lifting when it comes to moving globals between
84 /// host and device. Through the GenericDeviceTy we access memcpy DtoH and HtoD,
85 /// which means the only things specialized by the subclass is the retrival of
86 /// global metadata (size, addr) from the device.
87 /// \see getGlobalMetadataFromDevice
88 class GenericGlobalHandlerTy
{
89 /// Actually move memory between host and device. See readGlobalFromDevice and
90 /// writeGlobalToDevice for the interface description.
91 Error
moveGlobalBetweenDeviceAndHost(GenericDeviceTy
&Device
,
93 const GlobalTy
&HostGlobal
,
96 /// Actually move memory between host and device. See readGlobalFromDevice and
97 /// writeGlobalToDevice for the interface description.
98 Error
moveGlobalBetweenDeviceAndHost(GenericDeviceTy
&Device
,
99 const GlobalTy
&HostGlobal
,
100 const GlobalTy
&DeviceGlobal
,
104 virtual ~GenericGlobalHandlerTy() {}
106 /// Helper function for getting an ELF from a device image.
107 Expected
<ELF64LEObjectFile
> getELFObjectFile(DeviceImageTy
&Image
);
109 /// Returns whether the symbol named \p SymName is present in the given \p
111 bool isSymbolInImage(GenericDeviceTy
&Device
, DeviceImageTy
&Image
,
114 /// Get the address and size of a global in the image. Address and size are
115 /// return in \p ImageGlobal, the global name is passed in \p ImageGlobal.
116 Error
getGlobalMetadataFromImage(GenericDeviceTy
&Device
,
117 DeviceImageTy
&Image
, GlobalTy
&ImageGlobal
);
119 /// Read the memory associated with a global from the image and store it on
120 /// the host. The name, size, and destination are defined by \p HostGlobal.
121 Error
readGlobalFromImage(GenericDeviceTy
&Device
, DeviceImageTy
&Image
,
122 const GlobalTy
&HostGlobal
);
124 /// Get the address and size of a global from the device. Address is return in
125 /// \p DeviceGlobal, the global name and expected size are passed in
127 virtual Error
getGlobalMetadataFromDevice(GenericDeviceTy
&Device
,
128 DeviceImageTy
&Image
,
129 GlobalTy
&DeviceGlobal
) = 0;
131 /// Copy the memory associated with a global from the device to its
132 /// counterpart on the host. The name, size, and destination are defined by
133 /// \p HostGlobal. The origin is defined by \p DeviceGlobal.
134 Error
readGlobalFromDevice(GenericDeviceTy
&Device
,
135 const GlobalTy
&HostGlobal
,
136 const GlobalTy
&DeviceGlobal
) {
137 return moveGlobalBetweenDeviceAndHost(Device
, HostGlobal
, DeviceGlobal
,
141 /// Copy the memory associated with a global from the device to its
142 /// counterpart on the host. The name, size, and destination are defined by
143 /// \p HostGlobal. The origin is automatically resolved.
144 Error
readGlobalFromDevice(GenericDeviceTy
&Device
, DeviceImageTy
&Image
,
145 const GlobalTy
&HostGlobal
) {
146 return moveGlobalBetweenDeviceAndHost(Device
, Image
, HostGlobal
,
150 /// Copy the memory associated with a global from the host to its counterpart
151 /// on the device. The name, size, and origin are defined by \p HostGlobal.
152 /// The destination is defined by \p DeviceGlobal.
153 Error
writeGlobalToDevice(GenericDeviceTy
&Device
, const GlobalTy
&HostGlobal
,
154 const GlobalTy
&DeviceGlobal
) {
155 return moveGlobalBetweenDeviceAndHost(Device
, HostGlobal
, DeviceGlobal
,
159 /// Copy the memory associated with a global from the host to its counterpart
160 /// on the device. The name, size, and origin are defined by \p HostGlobal.
161 /// The destination is automatically resolved.
162 Error
writeGlobalToDevice(GenericDeviceTy
&Device
, DeviceImageTy
&Image
,
163 const GlobalTy
&HostGlobal
) {
164 return moveGlobalBetweenDeviceAndHost(Device
, Image
, HostGlobal
,
169 } // namespace plugin
170 } // namespace target
174 #endif // LLVM_OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_COMMON_GLOBALHANDLER_H