[Openmp-commits] [PATCH] D102043: [libomptarget] Look for plugins next to libomptarget.so

Jon Chesterfield via Phabricator via Openmp-commits openmp-commits at lists.llvm.org
Thu May 6 19:39:11 PDT 2021


JonChesterfield created this revision.
JonChesterfield added reviewers: jdoerfert, grokos, ABataev, gregrodgers, RaviNarayanaswamy, pdhaliwal, hfinkel, tlwilmar, AndreyChurbanov, jlpeyton, protze.joachim, ye-luo, tianshilei1992, guansong, jvesely, kerbowa, lebedev.ri, mgorny, sstefan1, yaxunl, nhaehnle.
JonChesterfield requested review of this revision.
Herald added a project: OpenMP.
Herald added a subscriber: openmp-commits.

[libomptarget] Look for plugins next to libomptarget.so
Currently libomptarget.so finds plugins through LD_LIBRARY_PATH. This
patch changes the logic to first look in the same directory as
libomptarget.so and then try the current approach.

It seems likely that the right plugin to load is the one next to
libomptarget but we can swap the order around to preserve existing
behaviour (in the case where LD_LIBRARY_PATH finds one)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D102043

Files:
  openmp/libomptarget/src/rtl.cpp


Index: openmp/libomptarget/src/rtl.cpp
===================================================================
--- openmp/libomptarget/src/rtl.cpp
+++ openmp/libomptarget/src/rtl.cpp
@@ -65,6 +65,30 @@
 #endif
 }
 
+namespace {
+std::string findLibomptargetDirectory() {
+  Dl_info dl_info;
+  // look up a symbol which is known to be from libomptarget
+  if (dladdr((void *)&__tgt_register_lib, &dl_info) != 0) {
+    std::string libomptargetPath = std::string(dl_info.dli_fname);
+    size_t slash = libomptargetPath.find_last_of('/');
+    if (slash != std::string::npos) {
+      return libomptargetPath.substr(0, slash + 1); // keep the /
+    }
+  }
+  return "";
+}
+
+void *verbose_dlopen(const char *Name) {
+  DP("Loading library '%s'...\n", Name);
+  void *dynlib_handle = dlopen(Name, RTLD_NOW);
+  if (!dynlib_handle) {
+    DP("Unable to load library '%s': %s!\n", Name, dlerror());
+  }
+  return dynlib_handle;
+}
+} // namespace
+
 void RTLsTy::LoadRTLs() {
   // Parse environment variable OMP_TARGET_OFFLOAD (if set)
   PM->TargetOffloadPolicy =
@@ -74,17 +98,21 @@
   }
 
   DP("Loading RTLs...\n");
+  std::string libomptargetPath = findLibomptargetDirectory();
 
   // Attempt to open all the plugins and, if they exist, check if the interface
   // is correct and if they are supporting any devices.
   for (auto *Name : RTLNames) {
-    DP("Loading library '%s'...\n", Name);
-    void *dynlib_handle = dlopen(Name, RTLD_NOW);
+    std::string adjacentPluginName = libomptargetPath + std::string(Name);
 
+    void *dynlib_handle = verbose_dlopen(adjacentPluginName.c_str());
     if (!dynlib_handle) {
-      // Library does not exist or cannot be found.
-      DP("Unable to load library '%s': %s!\n", Name, dlerror());
-      continue;
+      // Try again without the libomptarget library path prefix
+      dynlib_handle = verbose_dlopen(Name);
+      if (!dynlib_handle) {
+        // Library does not exist or cannot be found.
+        continue;
+      }
     }
 
     DP("Successfully loaded library '%s'!\n", Name);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102043.343564.patch
Type: text/x-patch
Size: 2044 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/openmp-commits/attachments/20210507/43d1cc54/attachment.bin>


More information about the Openmp-commits mailing list