[Openmp-commits] [PATCH] D138604: [OpenMP][libomptarget] Allow overriding function that gets ELF symbol info

Kevin Sala Penadés via Phabricator via Openmp-commits openmp-commits at lists.llvm.org
Wed Nov 23 13:35:15 PST 2022


kevinsala created this revision.
kevinsala added reviewers: jdoerfert, jhuber6, tianshilei1992, JonChesterfield, josemonsalve2.
kevinsala added a project: OpenMP.
Herald added subscribers: kosarev, guansong, tpr, yaxunl.
Herald added a project: All.
kevinsala requested review of this revision.
Herald added subscribers: openmp-commits, sstefan1.

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

This patch prepares the PluginInterface for the new AMDGPU NextGen plugin.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D138604

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


Index: openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.h
===================================================================
--- openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.h
+++ openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.h
@@ -96,6 +96,12 @@
   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,
Index: openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.cpp
===================================================================
--- openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.cpp
+++ openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.cpp
@@ -46,6 +46,21 @@
   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 @@
                          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,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138604.477589.patch
Type: text/x-patch
Size: 3203 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/openmp-commits/attachments/20221123/81496854/attachment.bin>


More information about the Openmp-commits mailing list