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
|
//===- GlobalHandler.h - Target independent global & enviroment handling --===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// Target independent global handler and environment manager.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_COMMON_GLOBALHANDLER_H
#define LLVM_OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_COMMON_GLOBALHANDLER_H
#include <string>
#include "llvm/ADT/DenseMap.h"
#include "llvm/Object/ELFObjectFile.h"
#include "Debug.h"
#include "Utilities.h"
#include "omptarget.h"
namespace llvm {
namespace omp {
namespace target {
namespace plugin {
class DeviceImageTy;
struct GenericDeviceTy;
using namespace llvm::object;
/// Common abstraction for globals that live on the host and device.
/// It simply encapsulates the symbol name, symbol size, and symbol address
/// (which might be host or device depending on the context).
class GlobalTy {
// NOTE: Maybe we can have a pointer to the offload entry name instead of
// holding a private copy of the name as a std::string.
std::string Name;
uint32_t Size;
void *Ptr;
public:
GlobalTy(const std::string &Name, uint32_t Size, void *Ptr = nullptr)
: Name(Name), Size(Size), Ptr(Ptr) {}
GlobalTy(const __tgt_offload_entry &Entry)
: Name(Entry.name), Size(Entry.size), Ptr(Entry.addr) {}
const std::string &getName() const { return Name; }
uint32_t getSize() const { return Size; }
void *getPtr() const { return Ptr; }
void setSize(int32_t S) { Size = S; }
void setPtr(void *P) { Ptr = P; }
};
/// Subclass of GlobalTy that holds the memory for a global of \p Ty.
template <typename Ty> class StaticGlobalTy : public GlobalTy {
Ty Data;
public:
template <typename... Args>
StaticGlobalTy(const std::string &Name, Args &&...args)
: GlobalTy(Name, sizeof(Ty), &Data),
Data(Ty{std::forward<Args>(args)...}) {}
template <typename... Args>
StaticGlobalTy(const char *Name, Args &&...args)
: GlobalTy(Name, sizeof(Ty), &Data),
Data(Ty{std::forward<Args>(args)...}) {}
template <typename... Args>
StaticGlobalTy(const char *Name, const char *Suffix, Args &&...args)
: GlobalTy(std::string(Name) + Suffix, sizeof(Ty), &Data),
Data(Ty{std::forward<Args>(args)...}) {}
Ty &getValue() { return Data; }
const Ty &getValue() const { return Data; }
void setValue(const Ty &V) { Data = V; }
};
/// Helper class to do the heavy lifting when it comes to moving globals between
/// host and device. Through the GenericDeviceTy we access memcpy DtoH and HtoD,
/// which means the only things specialized by the subclass is the retrival of
/// global metadata (size, addr) from the device.
/// \see getGlobalMetadataFromDevice
class GenericGlobalHandlerTy {
/// Map to store the ELF object files that have been loaded.
llvm::DenseMap<int32_t, ELF64LEObjectFile> ELFObjectFiles;
/// Get the cached ELF64LEObjectFile previosuly created for a specific
/// device image or create it if did not exist.
const ELF64LEObjectFile *
getOrCreateELFObjectFile(const GenericDeviceTy &Device, DeviceImageTy &Image);
/// Extract the global's information from the ELF image, section, and symbol.
virtual Error getGlobalMetadataFromELF(const DeviceImageTy &Image,
const ELF64LE::Sym &Symbol,
const ELF64LE::Shdr &Section,
GlobalTy &ImageGlobal);
/// Actually move memory between host and device. See readGlobalFromDevice and
/// writeGlobalToDevice for the interface description.
Error moveGlobalBetweenDeviceAndHost(GenericDeviceTy &Device,
DeviceImageTy &Image,
const GlobalTy &HostGlobal,
bool Device2Host);
/// Actually move memory between host and device. See readGlobalFromDevice and
/// writeGlobalToDevice for the interface description.
Error moveGlobalBetweenDeviceAndHost(GenericDeviceTy &Device,
DeviceImageTy &Image,
const GlobalTy &HostGlobal,
const GlobalTy &DeviceGlobal,
bool Device2Host);
public:
virtual ~GenericGlobalHandlerTy() {}
/// Get the address and size of a global in the image. Address and size are
/// return in \p ImageGlobal, the global name is passed in \p ImageGlobal.
Error getGlobalMetadataFromImage(GenericDeviceTy &Device,
DeviceImageTy &Image, GlobalTy &ImageGlobal);
/// Read the memory associated with a global from the image and store it on
/// the host. The name, size, and destination are defined by \p HostGlobal.
Error readGlobalFromImage(GenericDeviceTy &Device, DeviceImageTy &Image,
const GlobalTy &HostGlobal);
/// Get the address and size of a global from the device. Address is return in
/// \p DeviceGlobal, the global name and expected size are passed in
/// \p DeviceGlobal.
virtual Error getGlobalMetadataFromDevice(GenericDeviceTy &Device,
DeviceImageTy &Image,
GlobalTy &DeviceGlobal) = 0;
/// Copy the memory associated with a global from the device to its
/// counterpart on the host. The name, size, and destination are defined by
/// \p HostGlobal. The origin is defined by \p DeviceGlobal.
Error readGlobalFromDevice(GenericDeviceTy &Device, DeviceImageTy &Image,
const GlobalTy &HostGlobal,
const GlobalTy &DeviceGlobal) {
return moveGlobalBetweenDeviceAndHost(Device, Image, HostGlobal,
DeviceGlobal,
/* D2H */ true);
}
/// Copy the memory associated with a global from the device to its
/// counterpart on the host. The name, size, and destination are defined by
/// \p HostGlobal. The origin is automatically resolved.
Error readGlobalFromDevice(GenericDeviceTy &Device, DeviceImageTy &Image,
const GlobalTy &HostGlobal) {
return moveGlobalBetweenDeviceAndHost(Device, Image, HostGlobal,
/* D2H */ true);
}
/// Copy the memory associated with a global from the host to its counterpart
/// on the device. The name, size, and origin are defined by \p HostGlobal.
/// The destination is defined by \p DeviceGlobal.
Error writeGlobalToDevice(GenericDeviceTy &Device, DeviceImageTy &Image,
const GlobalTy &HostGlobal,
const GlobalTy &DeviceGlobal) {
return moveGlobalBetweenDeviceAndHost(Device, Image, HostGlobal,
DeviceGlobal,
/* D2H */ false);
}
/// Copy the memory associated with a global from the host to its counterpart
/// on the device. The name, size, and origin are defined by \p HostGlobal.
/// The destination is automatically resolved.
Error writeGlobalToDevice(GenericDeviceTy &Device, DeviceImageTy &Image,
const GlobalTy &HostGlobal) {
return moveGlobalBetweenDeviceAndHost(Device, Image, HostGlobal,
/* D2H */ false);
}
};
} // namespace plugin
} // namespace target
} // namespace omp
} // namespace llvm
#endif // LLVM_OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_COMMON_GLOBALHANDLER_H
|