[Openmp-commits] [openmp] [Libomptarget] Remove extra cache for offloading entries (PR #77012)

Joseph Huber via Openmp-commits openmp-commits at lists.llvm.org
Fri Jan 5 14:29:51 PST 2024


https://github.com/jhuber6 updated https://github.com/llvm/llvm-project/pull/77012

>From 66028b56e052f7ee1671817dfe748967f56de766 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Thu, 4 Jan 2024 15:45:04 -0600
Subject: [PATCH] [Libomptarget] Remove extra cache for offloading entries

Summary:
The offloading entries right now are assumed to be baked into the binary
itself, and thus always valid whenever the library is executing. This
means that we don't need to copy them to additional storage and can
instead simply pass around references to it.

This is not likely to change in the expected operation of the OpenMP
library. Additionally, the indirection for the offload entry struct is
simply two pointers, so moving it by value is trivial.
---
 openmp/libomptarget/include/DeviceImage.h  |  5 +++--
 openmp/libomptarget/include/OffloadEntry.h |  8 ++++----
 openmp/libomptarget/include/device.h       |  4 ++--
 openmp/libomptarget/src/DeviceImage.cpp    |  4 ----
 openmp/libomptarget/src/PluginManager.cpp  |  4 ++--
 openmp/libomptarget/src/device.cpp         | 15 +++++++--------
 6 files changed, 18 insertions(+), 22 deletions(-)

diff --git a/openmp/libomptarget/include/DeviceImage.h b/openmp/libomptarget/include/DeviceImage.h
index 63b4b6d14e0ef4..ba46a837f402cf 100644
--- a/openmp/libomptarget/include/DeviceImage.h
+++ b/openmp/libomptarget/include/DeviceImage.h
@@ -26,7 +26,6 @@
 class DeviceImageTy {
 
   std::unique_ptr<llvm::object::OffloadBinary> Binary;
-  llvm::SmallVector<std::unique_ptr<OffloadEntryTy>> OffloadEntries;
 
   __tgt_bin_desc *BinaryDesc;
   __tgt_device_image Image;
@@ -37,7 +36,9 @@ class DeviceImageTy {
   __tgt_device_image &getExecutableImage() { return Image; }
   __tgt_bin_desc &getBinaryDesc() { return *BinaryDesc; }
 
-  auto entries() { return llvm::make_pointee_range(OffloadEntries); }
+  auto entries() {
+    return llvm::make_range(Image.EntriesBegin, Image.EntriesEnd);
+  }
 };
 
 #endif // OMPTARGET_DEVICE_IMAGE_H
diff --git a/openmp/libomptarget/include/OffloadEntry.h b/openmp/libomptarget/include/OffloadEntry.h
index f645fe81db2dc5..5173841f20a44e 100644
--- a/openmp/libomptarget/include/OffloadEntry.h
+++ b/openmp/libomptarget/include/OffloadEntry.h
@@ -36,11 +36,11 @@ class OffloadEntryTy {
   const char *getNameAsCStr() const { return OffloadEntry.name; }
   __tgt_bin_desc *getBinaryDescription() const;
 
-  bool isCTor() { return hasFlags(OMP_DECLARE_TARGET_CTOR); }
-  bool isDTor() { return hasFlags(OMP_DECLARE_TARGET_DTOR); }
-  bool isLink() { return hasFlags(OMP_DECLARE_TARGET_LINK); }
+  bool isCTor() const { return hasFlags(OMP_DECLARE_TARGET_CTOR); }
+  bool isDTor() const { return hasFlags(OMP_DECLARE_TARGET_DTOR); }
+  bool isLink() const { return hasFlags(OMP_DECLARE_TARGET_LINK); }
 
-  bool hasFlags(OpenMPOffloadingDeclareTargetFlags Flags) {
+  bool hasFlags(OpenMPOffloadingDeclareTargetFlags Flags) const {
     return Flags & OffloadEntry.flags;
   }
 };
diff --git a/openmp/libomptarget/include/device.h b/openmp/libomptarget/include/device.h
index d28d3c508faf56..e94f48891dab36 100644
--- a/openmp/libomptarget/include/device.h
+++ b/openmp/libomptarget/include/device.h
@@ -159,7 +159,7 @@ struct DeviceTy {
   /// }
 
   /// Register \p Entry as an offload entry that is avalable on this device.
-  void addOffloadEntry(OffloadEntryTy &Entry);
+  void addOffloadEntry(const OffloadEntryTy &Entry);
 
   /// Print all offload entries to stderr.
   void dumpOffloadEntries();
@@ -170,7 +170,7 @@ struct DeviceTy {
 
   /// All offload entries available on this device.
   using DeviceOffloadEntriesMapTy =
-      llvm::DenseMap<llvm::StringRef, OffloadEntryTy *>;
+      llvm::DenseMap<llvm::StringRef, OffloadEntryTy>;
   ProtectedObj<DeviceOffloadEntriesMapTy> DeviceOffloadEntries;
 
   /// Handler to collect and organize host-2-device mapping information.
diff --git a/openmp/libomptarget/src/DeviceImage.cpp b/openmp/libomptarget/src/DeviceImage.cpp
index 1d39bb9ab8da65..e42460b5cca4f1 100644
--- a/openmp/libomptarget/src/DeviceImage.cpp
+++ b/openmp/libomptarget/src/DeviceImage.cpp
@@ -27,10 +27,6 @@ DeviceImageTy::DeviceImageTy(__tgt_bin_desc &BinaryDesc,
                              __tgt_device_image &TgtDeviceImage)
     : BinaryDesc(&BinaryDesc), Image(TgtDeviceImage) {
 
-  for (__tgt_offload_entry &Entry :
-       llvm::make_range(Image.EntriesBegin, Image.EntriesEnd))
-    OffloadEntries.emplace_back(std::make_unique<OffloadEntryTy>(*this, Entry));
-
   llvm::StringRef ImageStr(
       static_cast<char *>(Image.ImageStart),
       llvm::omp::target::getPtrDiff(Image.ImageEnd, Image.ImageStart));
diff --git a/openmp/libomptarget/src/PluginManager.cpp b/openmp/libomptarget/src/PluginManager.cpp
index da2e08180eead8..83bf65f0f0de56 100644
--- a/openmp/libomptarget/src/PluginManager.cpp
+++ b/openmp/libomptarget/src/PluginManager.cpp
@@ -97,8 +97,8 @@ void PluginAdaptorTy::addOffloadEntries(DeviceImageTy &DI) {
                     toString(DeviceOrErr.takeError()).c_str());
 
     DeviceTy &Device = *DeviceOrErr;
-    for (OffloadEntryTy &Entry : DI.entries())
-      Device.addOffloadEntry(Entry);
+    for (__tgt_offload_entry &Entry : DI.entries())
+      Device.addOffloadEntry(OffloadEntryTy(DI, Entry));
   }
 }
 
diff --git a/openmp/libomptarget/src/device.cpp b/openmp/libomptarget/src/device.cpp
index dbad13b92bcc14..fa8932361a51c9 100644
--- a/openmp/libomptarget/src/device.cpp
+++ b/openmp/libomptarget/src/device.cpp
@@ -291,10 +291,9 @@ int32_t DeviceTy::destroyEvent(void *Event) {
   return OFFLOAD_SUCCESS;
 }
 
-void DeviceTy::addOffloadEntry(OffloadEntryTy &Entry) {
+void DeviceTy::addOffloadEntry(const OffloadEntryTy &Entry) {
   std::lock_guard<decltype(PendingGlobalsMtx)> Lock(PendingGlobalsMtx);
-  DeviceOffloadEntries.getExclusiveAccessor()->insert(
-      {Entry.getName(), &Entry});
+  DeviceOffloadEntries.getExclusiveAccessor()->insert({Entry.getName(), Entry});
   if (Entry.isGlobal())
     return;
 
@@ -329,14 +328,14 @@ void DeviceTy::dumpOffloadEntries() {
   fprintf(stderr, "Device %i offload entries:\n", DeviceID);
   for (auto &It : *DeviceOffloadEntries.getExclusiveAccessor()) {
     const char *Kind = "kernel";
-    if (It.second->isCTor())
+    if (It.second.isCTor())
       Kind = "constructor";
-    else if (It.second->isDTor())
+    else if (It.second.isDTor())
       Kind = "destructor";
-    else if (It.second->isLink())
+    else if (It.second.isLink())
       Kind = "link";
-    else if (It.second->isGlobal())
+    else if (It.second.isGlobal())
       Kind = "global var.";
-    fprintf(stderr, "  %11s: %s\n", Kind, It.second->getNameAsCStr());
+    fprintf(stderr, "  %11s: %s\n", Kind, It.second.getNameAsCStr());
   }
 }



More information about the Openmp-commits mailing list