[Openmp-commits] [openmp] 4fde816 - [OpenMP][libomptarget] Allow overriding function that gets ELF symbol info

Kevin Sala via Openmp-commits openmp-commits at lists.llvm.org
Sat Dec 3 12:51:41 PST 2022


Author: Kevin Sala
Date: 2022-12-03T21:51:09+01:00
New Revision: 4fde81679c78ef1956840e7689bc8415b72292f1

URL: https://github.com/llvm/llvm-project/commit/4fde81679c78ef1956840e7689bc8415b72292f1
DIFF: https://github.com/llvm/llvm-project/commit/4fde81679c78ef1956840e7689bc8415b72292f1.diff

LOG: [OpenMP][libomptarget] Allow overriding function that gets ELF symbol info

The OpenMP target's NextGen plugins retrieve symbol information in the ELF image
(i.e., address and size) through the ELF section and ELF symbol objects. However,
the images of CUDA programs compute the address differently from the images of
AMDGPU programs:

  - Address for CUDA symbols: image begin + section's offset + symbol's st_value
  - Address for AMDGPU symbols: image + begin + symbol's st_value

Differential Revision: https://reviews.llvm.org/D138604

Added: 
    

Modified: 
    openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.cpp
    openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.h

Removed: 
    


################################################################################
diff  --git a/openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.cpp b/openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.cpp
index 14e0532115661..79e2ba5e7dc78 100644
--- a/openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.cpp
+++ b/openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.cpp
@@ -46,6 +46,21 @@ GenericGlobalHandlerTy::getOrCreateELFObjectFile(const GenericDeviceTy &Device,
   return &Result.first->second;
 }
 
+Error GenericGlobalHandlerTy::getGlobalMetadataFromELF(
+    const DeviceImageTy &Image, const ELF64LE::Sym &Symbol,
+    const ELF64LE::Shdr &Section, GlobalTy &ImageGlobal) {
+
+  // The global's address is computed as the image begin + the ELF section
+  // offset + the ELF symbol value.
+  ImageGlobal.setPtr((char *)Image.getStart() + Section.sh_offset +
+                     Symbol.st_value);
+
+  // Set the global's size.
+  ImageGlobal.setSize(Symbol.st_size);
+
+  return Plugin::success();
+}
+
 Error GenericGlobalHandlerTy::moveGlobalBetweenDeviceAndHost(
     GenericDeviceTy &Device, DeviceImageTy &Image, const GlobalTy &HostGlobal,
     bool Device2Host) {
@@ -111,19 +126,14 @@ Error GenericGlobalHandlerTy::getGlobalMetadataFromImage(
                          ImageGlobal.getName().data());
 
   // Get the section to which the symbol belongs.
-  auto SymSecOrErr = ELFObj->getELFFile().getSection((*SymOrErr)->st_shndx);
-  if (!SymSecOrErr)
+  auto SecOrErr = ELFObj->getELFFile().getSection((*SymOrErr)->st_shndx);
+  if (!SecOrErr)
     return Plugin::error("Failed to get ELF section from global '%s': %s",
                          ImageGlobal.getName().data(),
-                         toString(SymOrErr.takeError()).data());
+                         toString(SecOrErr.takeError()).data());
 
-  // Save the global symbol's address and size. The address of the global is the
-  // image base address + the section offset + the symbol value.
-  ImageGlobal.setPtr((char *)Image.getStart() + (*SymSecOrErr)->sh_offset +
-                     (*SymOrErr)->st_value);
-  ImageGlobal.setSize((*SymOrErr)->st_size);
-
-  return Plugin::success();
+  // Setup the global symbol's address and size.
+  return getGlobalMetadataFromELF(Image, **SymOrErr, **SecOrErr, ImageGlobal);
 }
 
 Error GenericGlobalHandlerTy::readGlobalFromImage(GenericDeviceTy &Device,

diff  --git a/openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.h b/openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.h
index e93ade03eed6e..77eb6e3ae4f98 100644
--- a/openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.h
+++ b/openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.h
@@ -96,6 +96,12 @@ class GenericGlobalHandlerTy {
   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,


        


More information about the Openmp-commits mailing list