[PATCH] D77704: [gold] Add support for loading pass plugins

2020-05-17 Thread Dominic Chen via Phabricator via cfe-commits
ddcc added a comment.

ping, any feedback?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D77704



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77704: [gold] Add support for loading pass plugins

2020-04-24 Thread Dominic Chen via Phabricator via cfe-commits
ddcc added a comment.

In order to reload the gold plugin, I'm modifying the Clang driver to pass in 
our own path as a separate argument, which is the most generic approach. 
Another method would be to use e.g. `dladdr()` to grab our own path from one of 
our exported functions, but that method appears to be a glibc extension which 
isn't cross-platform.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D77704



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77704: [gold] Add support for loading pass plugins

2020-04-24 Thread Dominic Chen via Phabricator via cfe-commits
ddcc marked an inline comment as done.
ddcc added a comment.

Thanks, seems to be working now for statically-built passes on a default LLVM 
build without `LLVM_LINK_LLVM_DYLIB`.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D77704



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77704: [gold] Add support for loading pass plugins

2020-04-24 Thread Dominic Chen via Phabricator via cfe-commits
ddcc updated this revision to Diff 259981.
ddcc added a comment.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Support statically-built passes


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D77704

Files:
  clang/lib/Driver/ToolChains/CommonArgs.cpp
  llvm/tools/gold/CMakeLists.txt
  llvm/tools/gold/gold-plugin.cpp
  llvm/tools/gold/gold.exports


Index: llvm/tools/gold/gold.exports
===
--- llvm/tools/gold/gold.exports
+++ /dev/null
@@ -1 +0,0 @@
-onload
Index: llvm/tools/gold/gold-plugin.cpp
===
--- llvm/tools/gold/gold-plugin.cpp
+++ llvm/tools/gold/gold-plugin.cpp
@@ -23,6 +23,7 @@
 #include "llvm/Object/Error.h"
 #include "llvm/Support/CachePruning.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/DynamicLibrary.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/MemoryBuffer.h"
@@ -207,6 +208,12 @@
   static std::string stats_file;
   // Asserts that LTO link has whole program visibility
   static bool whole_program_visibility = false;
+  // List of new PM pass plugins
+  static std::vector pass_plugins;
+  // Path to ourselves, and whether we have been reloaded for pass plugin 
symbol
+  // resolution
+  static std::string self;
+  static bool self_reloaded = false;
 
   // Optimization remarks filename, accepted passes and hotness options
   static std::string RemarksFilename;
@@ -300,6 +307,30 @@
   RemarksFormat = std::string(opt);
 } else if (opt.consume_front("stats-file=")) {
   stats_file = std::string(opt);
+} else if (opt.consume_front("self=")) {
+  // Store our own path, in case we need to be reloaded
+  self = std::string(opt);
+} else if (opt.consume_front("load=")) {
+  std::string Error, Path(opt);
+
+  // This plugin is loaded by gold under RTLD_LOCAL, which means that our
+  // LLVM symbols are not available to subsequent pass plugins. Since this
+  // will break statically-built pass plugins, we reload with RTLD_GLOBAL.
+  if (!self_reloaded) {
+if (self.size()) {
+  if (sys::DynamicLibrary::LoadLibraryPermanently(self.c_str(), 
))
+message(LDPL_ERROR, "Unable to reload LLVM gold plugin: %s",
+Error.c_str());
+  else
+self_reloaded = true;
+} else
+  message(LDPL_ERROR, "Unable to retrieve path to LLVM gold plugin!");
+  }
+
+  if (sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), ))
+message(LDPL_ERROR, "Unable to load plugin: %s", Error.c_str());
+} else if (opt.consume_front("load-pass-plugin=")) {
+  pass_plugins.push_back(opt.data());
 } else {
   // Save this option to pass to the code generator.
   // ParseCommandLineOptions() expects argv[0] to be program name. Lazily
@@ -935,6 +966,8 @@
   Conf.UseNewPM = options::new_pass_manager;
   // Debug new pass manager if requested
   Conf.DebugPassManager = options::debug_pass_manager;
+  // Pass plugins to load
+  Conf.PassPlugins = std::move(options::pass_plugins);
 
   Conf.HasWholeProgramVisibility = options::whole_program_visibility;
 
Index: llvm/tools/gold/CMakeLists.txt
===
--- llvm/tools/gold/CMakeLists.txt
+++ llvm/tools/gold/CMakeLists.txt
@@ -1,5 +1,3 @@
-set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_CURRENT_SOURCE_DIR}/gold.exports)
-
 if( LLVM_ENABLE_PIC AND LLVM_BINUTILS_INCDIR )
   include_directories( ${LLVM_BINUTILS_INCDIR} )
 
Index: clang/lib/Driver/ToolChains/CommonArgs.cpp
===
--- clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -379,6 +379,9 @@
 Suffix,
 Plugin);
 CmdArgs.push_back(Args.MakeArgString(Plugin));
+
+// Pass in our own path in case we need to reload
+CmdArgs.push_back(Args.MakeArgString(Twine("-plugin-opt=self=") + Plugin));
   }
 
   // Try to pass driver level flags relevant to LTO code generation down to


Index: llvm/tools/gold/gold.exports
===
--- llvm/tools/gold/gold.exports
+++ /dev/null
@@ -1 +0,0 @@
-onload
Index: llvm/tools/gold/gold-plugin.cpp
===
--- llvm/tools/gold/gold-plugin.cpp
+++ llvm/tools/gold/gold-plugin.cpp
@@ -23,6 +23,7 @@
 #include "llvm/Object/Error.h"
 #include "llvm/Support/CachePruning.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/DynamicLibrary.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/MemoryBuffer.h"
@@ -207,6 +208,12 @@
   static std::string