brintos

brintos / llvm-project-archived public Read only

0
0
Text · 8.7 KiB · af7dac6 Raw
216 lines · c
1//===- GlobalHandler.h - Target independent global & environment handling -===//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-exception6//7//===----------------------------------------------------------------------===//8//9// Target independent global handler and environment manager.10//11//===----------------------------------------------------------------------===//12 13#ifndef LLVM_OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_COMMON_GLOBALHANDLER_H14#define LLVM_OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_COMMON_GLOBALHANDLER_H15 16#include <type_traits>17 18#include "llvm/ADT/DenseMap.h"19#include "llvm/Object/ELFObjectFile.h"20#include "llvm/ProfileData/InstrProf.h"21 22#include "Shared/Debug.h"23#include "Shared/Utils.h"24 25#include "omptarget.h"26 27namespace llvm {28namespace omp {29namespace target {30namespace plugin {31 32class DeviceImageTy;33struct GenericDeviceTy;34 35using namespace llvm::object;36 37/// Common abstraction for globals that live on the host and device.38/// It simply encapsulates the symbol name, symbol size, and symbol address39/// (which might be host or device depending on the context).40/// Both size and address may be absent (signified by 0/nullptr), and can be41/// populated with getGlobalMetadataFromDevice/Image.42class GlobalTy {43  // NOTE: Maybe we can have a pointer to the offload entry name instead of44  // holding a private copy of the name as a std::string.45  std::string Name;46  uint32_t Size;47  void *Ptr;48 49public:50  GlobalTy(const std::string &Name, uint32_t Size = 0, void *Ptr = nullptr)51      : Name(Name), Size(Size), Ptr(Ptr) {}52 53  const std::string &getName() const { return Name; }54  uint32_t getSize() const { return Size; }55  void *getPtr() const { return Ptr; }56 57  void setSize(int32_t S) { Size = S; }58  void setPtr(void *P) { Ptr = P; }59};60 61using IntPtrT = void *;62struct __llvm_profile_data {63#define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer)                     \64  std::remove_const<Type>::type Name;65#include "llvm/ProfileData/InstrProfData.inc"66};67 68extern "C" {69extern int __attribute__((weak)) __llvm_write_custom_profile(70    const char *Target, const __llvm_profile_data *DataBegin,71    const __llvm_profile_data *DataEnd, const char *CountersBegin,72    const char *CountersEnd, const char *NamesBegin, const char *NamesEnd,73    const uint64_t *VersionOverride);74}75/// PGO profiling data extracted from a GPU device76struct GPUProfGlobals {77  SmallVector<int64_t> Counts;78  SmallVector<__llvm_profile_data> Data;79  SmallVector<uint8_t> NamesData;80  Triple TargetTriple;81  uint64_t Version = INSTR_PROF_RAW_VERSION;82 83  void dump() const;84  Error write() const;85  bool empty() const;86};87 88/// Subclass of GlobalTy that holds the memory for a global of \p Ty.89template <typename Ty> class StaticGlobalTy : public GlobalTy {90  Ty Data;91 92public:93  template <typename... Args>94  StaticGlobalTy(const std::string &Name, Args &&...args)95      : GlobalTy(Name, sizeof(Ty), &Data),96        Data(Ty{std::forward<Args>(args)...}) {}97 98  template <typename... Args>99  StaticGlobalTy(const char *Name, Args &&...args)100      : GlobalTy(Name, sizeof(Ty), &Data),101        Data(Ty{std::forward<Args>(args)...}) {}102 103  template <typename... Args>104  StaticGlobalTy(const char *Name, const char *Suffix, Args &&...args)105      : GlobalTy(std::string(Name) + Suffix, sizeof(Ty), &Data),106        Data(Ty{std::forward<Args>(args)...}) {}107 108  Ty &getValue() { return Data; }109  const Ty &getValue() const { return Data; }110  void setValue(const Ty &V) { Data = V; }111};112 113/// Helper class to do the heavy lifting when it comes to moving globals between114/// host and device. Through the GenericDeviceTy we access memcpy DtoH and HtoD,115/// which means the only things specialized by the subclass is the retrieval of116/// global metadata (size, addr) from the device.117/// \see getGlobalMetadataFromDevice118class GenericGlobalHandlerTy {119  /// Actually move memory between host and device. See readGlobalFromDevice and120  /// writeGlobalToDevice for the interface description.121  Error moveGlobalBetweenDeviceAndHost(GenericDeviceTy &Device,122                                       DeviceImageTy &Image,123                                       const GlobalTy &HostGlobal,124                                       bool Device2Host);125 126  /// Actually move memory between host and device. See readGlobalFromDevice and127  /// writeGlobalToDevice for the interface description.128  Error moveGlobalBetweenDeviceAndHost(GenericDeviceTy &Device,129                                       const GlobalTy &HostGlobal,130                                       const GlobalTy &DeviceGlobal,131                                       bool Device2Host);132 133public:134  virtual ~GenericGlobalHandlerTy() {}135 136  /// Helper function for getting an ELF from a device image.137  Expected<std::unique_ptr<ObjectFile>> getELFObjectFile(DeviceImageTy &Image);138 139  /// Returns whether the symbol named \p SymName is present in the given \p140  /// Image.141  bool isSymbolInImage(GenericDeviceTy &Device, DeviceImageTy &Image,142                       StringRef SymName);143 144  /// Get the address and size of a global in the image. Address is145  /// returned in \p ImageGlobal and the global name is passed in \p146  /// ImageGlobal. If no size is present in \p ImageGlobal, then the size of the147  /// global will be stored there. If it is present, it will be validated148  /// against the real size of the global.149  Error getGlobalMetadataFromImage(GenericDeviceTy &Device,150                                   DeviceImageTy &Image, GlobalTy &ImageGlobal);151 152  /// Read the memory associated with a global from the image and store it on153  /// the host. The name, size, and destination are defined by \p HostGlobal.154  Error readGlobalFromImage(GenericDeviceTy &Device, DeviceImageTy &Image,155                            const GlobalTy &HostGlobal);156 157  /// Get the address and size of a global from the device. Address is158  /// returned in \p ImageGlobal and the global name is passed in \p159  /// ImageGlobal. If no size is present in \p ImageGlobal, then the size of the160  /// global will be stored there. If it is present, it will be validated161  /// against the real size of the global.162  virtual Error getGlobalMetadataFromDevice(GenericDeviceTy &Device,163                                            DeviceImageTy &Image,164                                            GlobalTy &DeviceGlobal) = 0;165 166  /// Copy the memory associated with a global from the device to its167  /// counterpart on the host. The name, size, and destination are defined by168  /// \p HostGlobal. The origin is defined by \p DeviceGlobal.169  Error readGlobalFromDevice(GenericDeviceTy &Device,170                             const GlobalTy &HostGlobal,171                             const GlobalTy &DeviceGlobal) {172    return moveGlobalBetweenDeviceAndHost(Device, HostGlobal, DeviceGlobal,173                                          /*D2H=*/true);174  }175 176  /// Copy the memory associated with a global from the device to its177  /// counterpart on the host. The name, size, and destination are defined by178  /// \p HostGlobal. The origin is automatically resolved.179  Error readGlobalFromDevice(GenericDeviceTy &Device, DeviceImageTy &Image,180                             const GlobalTy &HostGlobal) {181    return moveGlobalBetweenDeviceAndHost(Device, Image, HostGlobal,182                                          /*D2H=*/true);183  }184 185  /// Copy the memory associated with a global from the host to its counterpart186  /// on the device. The name, size, and origin are defined by \p HostGlobal.187  /// The destination is defined by \p DeviceGlobal.188  Error writeGlobalToDevice(GenericDeviceTy &Device, const GlobalTy &HostGlobal,189                            const GlobalTy &DeviceGlobal) {190    return moveGlobalBetweenDeviceAndHost(Device, HostGlobal, DeviceGlobal,191                                          /*D2H=*/false);192  }193 194  /// Copy the memory associated with a global from the host to its counterpart195  /// on the device. The name, size, and origin are defined by \p HostGlobal.196  /// The destination is automatically resolved.197  Error writeGlobalToDevice(GenericDeviceTy &Device, DeviceImageTy &Image,198                            const GlobalTy &HostGlobal) {199    return moveGlobalBetweenDeviceAndHost(Device, Image, HostGlobal,200                                          /*D2H=*/false);201  }202 203  /// Reads profiling data from a GPU image to supplied profdata struct.204  /// Iterates through the image symbol table and stores global values205  /// with profiling prefixes.206  Expected<GPUProfGlobals> readProfilingGlobals(GenericDeviceTy &Device,207                                                DeviceImageTy &Image);208};209 210} // namespace plugin211} // namespace target212} // namespace omp213} // namespace llvm214 215#endif // LLVM_OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_COMMON_GLOBALHANDLER_H216