[Openmp-commits] [PATCH] D87413: [OpenMP] Load plugins from same directory as the libomptarget.so

Saiyedul Islam via Phabricator via Openmp-commits openmp-commits at lists.llvm.org
Thu Sep 10 11:07:25 PDT 2020


saiislam updated this revision to Diff 291033.
saiislam added a comment.

Moved the code to load plugins to a helper function and before loop on plugins.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D87413/new/

https://reviews.llvm.org/D87413

Files:
  openmp/libomptarget/src/rtl.cpp


Index: openmp/libomptarget/src/rtl.cpp
===================================================================
--- openmp/libomptarget/src/rtl.cpp
+++ openmp/libomptarget/src/rtl.cpp
@@ -18,6 +18,7 @@
 #include <cstdlib>
 #include <cstring>
 #include <dlfcn.h>
+#include <link.h>
 #include <mutex>
 #include <string>
 
@@ -60,6 +61,31 @@
   delete TblMapMtx;
 }
 
+/// Load plugins from same directory as libomptarget.so
+static bool LoadLibFromCWD(char **PathName) {
+  struct link_map *Map;
+
+  DP("Trying to load plugins from same directory as libomptarget.so: %s\n",
+     Name);
+  void *Handle = dlopen("libomptarget.so", RTLD_NOW);
+  if (!Handle) {
+    DP("Unable to load library : %s!\n", dlerror());
+    return false;
+  }
+  if (dlinfo(Handle, RTLD_DI_LINKMAP, &Map) == -1) {
+    DP("RTLD_DI_LINKMAP failed: %s\n", dlerror());
+    dlclose(Handle);
+    return false;
+  }
+  if (!Map->l_name) {
+    DP("Absolute pathname not found: %s\n", dlerror());
+    dlclose(Handle);
+    return false;
+  }
+  *PathName = Map->l_name;
+  return true;
+}
+
 void RTLsTy::LoadRTLs() {
   // Parse environment variable OMP_TARGET_OFFLOAD (if set)
   TargetOffloadPolicy = (kmp_target_offload_kind_t) __kmpc_get_target_offload();
@@ -69,6 +95,10 @@
 
   DP("Loading RTLs...\n");
 
+  std::string FullPluginName;
+  char *LibompPathName = NULL;
+  bool LibFoundInCWD = LoadLibFromCWD(&LibompPathName);
+
   // 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) {
@@ -77,11 +107,23 @@
 
     if (!dynlib_handle) {
       // Library does not exist or cannot be found.
-      DP("Unable to load library '%s': %s!\n", Name, dlerror());
-      continue;
+      // Try to load plugins from same directory as libomptarget.so
+
+      if (!LibFoundInCWD)
+        continue;
+
+      FullPluginName.assign(LibompPathName).append("/").append(Name);
+      dynlib_handle = dlopen(FullPluginName.c_str(), RTLD_NOW);
+      if (!dynlib_handle) {
+        DP("Unable to load plugin '%s' from same directory as libomptarget.so: "
+           "%s\n",
+           Name, dlerror());
+        continue;
+      }
     }
 
-    DP("Successfully loaded library '%s'!\n", Name);
+    DP("Successfully loaded library '%s'!\n",
+       FullPluginName ? FullPluginName.c_str() : Name);
 
     // Retrieve the RTL information from the runtime library.
     RTLInfoTy R;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D87413.291033.patch
Type: text/x-patch
Size: 2453 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/openmp-commits/attachments/20200910/527057bc/attachment.bin>


More information about the Openmp-commits mailing list