- 
                Notifications
    You must be signed in to change notification settings 
- Fork 15k
[llvm][unittests][NFCI] Depend on RPATH for loading test plugins #163210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| @llvm/pr-subscribers-llvm-analysis Author: Raul Tambre (tambry) ChangesAs proven by #159126 it's unnecessary to explicitly construct the path for loading the test plugins. On all supported platforms the RPATH is set appropriately for them to be found by the dynamic loader itself. Thus we can just rid ourselves of the extra code. This also cleans up some of the CMake code and C++ includes slightly. Surprisingly  See: #159126 Full diff: https://github.com/llvm/llvm-project/pull/163210.diff 11 Files Affected: 
 diff --git a/llvm/unittests/Analysis/InlineAdvisorPlugin/CMakeLists.txt b/llvm/unittests/Analysis/InlineAdvisorPlugin/CMakeLists.txt
index d9da627ad52e3..5c5cd07b3af84 100644
--- a/llvm/unittests/Analysis/InlineAdvisorPlugin/CMakeLists.txt
+++ b/llvm/unittests/Analysis/InlineAdvisorPlugin/CMakeLists.txt
@@ -1,5 +1,5 @@
-# The advisor plugin expects to not link against the Analysis, Support and Core 
-# libraries, but expects them to exist in the process loading the plugin. This 
+# The advisor plugin expects to not link against the Analysis, Support and Core
+# libraries, but expects them to exist in the process loading the plugin. This
 # doesn't work with DLLs on Windows (where a shared library can't have undefined
 # references), so just skip this testcase on Windows.
 if ((NOT WIN32 AND NOT CYGWIN) OR LLVM_BUILD_LLVM_DYLIB)
@@ -7,15 +7,6 @@ if ((NOT WIN32 AND NOT CYGWIN) OR LLVM_BUILD_LLVM_DYLIB)
   add_llvm_library(InlineAdvisorPlugin MODULE BUILDTREE_ONLY
     InlineAdvisorPlugin.cpp
     )
-  # Put PLUGIN next to the unit test executable.
-  set_output_directory(InlineAdvisorPlugin
-      BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/../
-      LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/../
-      )
   set_target_properties(InlineAdvisorPlugin PROPERTIES FOLDER "LLVM/Tests")
-
-  # The plugin depends on some of the output files of intrinsics_gen, so make sure
-  # it is built before the plugin.
-  add_dependencies(InlineAdvisorPlugin intrinsics_gen)
   add_dependencies(AnalysisTests InlineAdvisorPlugin)
 endif()
diff --git a/llvm/unittests/Analysis/InlineAdvisorPlugin/InlineAdvisorPlugin.cpp b/llvm/unittests/Analysis/InlineAdvisorPlugin/InlineAdvisorPlugin.cpp
index beefff2b3b106..831c95b54fdb4 100644
--- a/llvm/unittests/Analysis/InlineAdvisorPlugin/InlineAdvisorPlugin.cpp
+++ b/llvm/unittests/Analysis/InlineAdvisorPlugin/InlineAdvisorPlugin.cpp
@@ -3,8 +3,6 @@
 #include "llvm/Pass.h"
 #include "llvm/Passes/PassBuilder.h"
 #include "llvm/Passes/PassPlugin.h"
-#include "llvm/Support/CommandLine.h"
-#include "llvm/Support/raw_ostream.h"
 
 #include "llvm/Analysis/InlineAdvisor.h"
 
diff --git a/llvm/unittests/Analysis/InlineOrderPlugin/CMakeLists.txt b/llvm/unittests/Analysis/InlineOrderPlugin/CMakeLists.txt
index 941e18efc1a52..8dac91f90df9b 100644
--- a/llvm/unittests/Analysis/InlineOrderPlugin/CMakeLists.txt
+++ b/llvm/unittests/Analysis/InlineOrderPlugin/CMakeLists.txt
@@ -1,5 +1,5 @@
-# The order plugin expects to not link against the Analysis, Support and Core 
-# libraries, but expects them to exist in the process loading the plugin. This 
+# The order plugin expects to not link against the Analysis, Support and Core
+# libraries, but expects them to exist in the process loading the plugin. This
 # doesn't work with DLLs on Windows (where a shared library can't have undefined
 # references), so just skip this testcase on Windows.
 if ((NOT WIN32 AND NOT CYGWIN) OR LLVM_BUILD_LLVM_DYLIB)
@@ -7,15 +7,6 @@ if ((NOT WIN32 AND NOT CYGWIN) OR LLVM_BUILD_LLVM_DYLIB)
   add_llvm_library(InlineOrderPlugin MODULE BUILDTREE_ONLY
     InlineOrderPlugin.cpp
     )
-  # Put PLUGIN next to the unit test executable.
-  set_output_directory(InlineOrderPlugin
-      BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/../
-      LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/../
-      )
   set_target_properties(InlineOrderPlugin PROPERTIES FOLDER "Tests")
-
-  # The plugin depends on some of the output files of intrinsics_gen, so make sure
-  # it is built before the plugin.
-  add_dependencies(InlineOrderPlugin intrinsics_gen)
   add_dependencies(AnalysisTests InlineOrderPlugin)
 endif()
diff --git a/llvm/unittests/Analysis/InlineOrderPlugin/InlineOrderPlugin.cpp b/llvm/unittests/Analysis/InlineOrderPlugin/InlineOrderPlugin.cpp
index 9c46c1b8e22ba..b4063b5d86203 100644
--- a/llvm/unittests/Analysis/InlineOrderPlugin/InlineOrderPlugin.cpp
+++ b/llvm/unittests/Analysis/InlineOrderPlugin/InlineOrderPlugin.cpp
@@ -3,8 +3,6 @@
 #include "llvm/Pass.h"
 #include "llvm/Passes/PassBuilder.h"
 #include "llvm/Passes/PassPlugin.h"
-#include "llvm/Support/CommandLine.h"
-#include "llvm/Support/raw_ostream.h"
 
 #include "llvm/Analysis/InlineOrder.h"
 
diff --git a/llvm/unittests/Analysis/PluginInlineAdvisorAnalysisTest.cpp b/llvm/unittests/Analysis/PluginInlineAdvisorAnalysisTest.cpp
index ca4ea8b627e83..fbecb1fe6a288 100644
--- a/llvm/unittests/Analysis/PluginInlineAdvisorAnalysisTest.cpp
+++ b/llvm/unittests/Analysis/PluginInlineAdvisorAnalysisTest.cpp
@@ -4,28 +4,13 @@
 #include "llvm/IR/Module.h"
 #include "llvm/Passes/PassBuilder.h"
 #include "llvm/Passes/PassPlugin.h"
-#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/raw_ostream.h"
-#include "llvm/Testing/Support/Error.h"
 #include "gtest/gtest.h"
 
 namespace llvm {
 
 namespace {
 
-void anchor() {}
-
-static std::string libPath(const std::string Name = "InlineAdvisorPlugin") {
-  const auto &Argvs = testing::internal::GetArgvs();
-  const char *Argv0 =
-      Argvs.size() > 0 ? Argvs[0].c_str() : "PluginInlineAdvisorAnalysisTest";
-  void *Ptr = (void *)(intptr_t)anchor;
-  std::string Path = sys::fs::getMainExecutable(Argv0, Ptr);
-  llvm::SmallString<256> Buf{sys::path::parent_path(Path)};
-  sys::path::append(Buf, (Name + LLVM_PLUGIN_EXT).c_str());
-  return std::string(Buf.str());
-}
-
 // Example of a custom InlineAdvisor that only inlines calls to functions called
 // "foo".
 class FooOnlyInlineAdvisor : public InlineAdvisor {
@@ -61,8 +46,7 @@ struct CompilerInstance {
 
   // connect the plugin to our compiler instance
   void setupPlugin() {
-    auto PluginPath = libPath();
-    ASSERT_NE("", PluginPath);
+    auto PluginPath{std::string{"InlineAdvisorPlugin"} + LLVM_PLUGIN_EXT};
     Expected<PassPlugin> Plugin = PassPlugin::Load(PluginPath);
     ASSERT_TRUE(!!Plugin) << "Plugin path: " << PluginPath;
     Plugin->registerPassBuilderCallbacks(PB);
diff --git a/llvm/unittests/Analysis/PluginInlineOrderAnalysisTest.cpp b/llvm/unittests/Analysis/PluginInlineOrderAnalysisTest.cpp
index 0b31b0892d75a..9f12bb502109f 100644
--- a/llvm/unittests/Analysis/PluginInlineOrderAnalysisTest.cpp
+++ b/llvm/unittests/Analysis/PluginInlineOrderAnalysisTest.cpp
@@ -4,30 +4,13 @@
 #include "llvm/IR/Module.h"
 #include "llvm/Passes/PassBuilder.h"
 #include "llvm/Passes/PassPlugin.h"
-#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/raw_ostream.h"
-#include "llvm/Testing/Support/Error.h"
 #include "gtest/gtest.h"
 
-#include "llvm/Analysis/InlineOrder.h"
-
 namespace llvm {
 
 namespace {
 
-void anchor() {}
-
-std::string libPath(const std::string Name = "InlineOrderPlugin") {
-  const auto &Argvs = testing::internal::GetArgvs();
-  const char *Argv0 =
-      Argvs.size() > 0 ? Argvs[0].c_str() : "PluginInlineOrderAnalysisTest";
-  void *Ptr = (void *)(intptr_t)anchor;
-  std::string Path = sys::fs::getMainExecutable(Argv0, Ptr);
-  llvm::SmallString<256> Buf{sys::path::parent_path(Path)};
-  sys::path::append(Buf, (Name + LLVM_PLUGIN_EXT).c_str());
-  return std::string(Buf.str());
-}
-
 struct CompilerInstance {
   LLVMContext Ctx;
   ModulePassManager MPM;
@@ -43,8 +26,7 @@ struct CompilerInstance {
 
   // Connect the plugin to our compiler instance.
   void setupPlugin() {
-    auto PluginPath = libPath();
-    ASSERT_NE("", PluginPath);
+    auto PluginPath{std::string{"InlineOrderPlugin"} + LLVM_PLUGIN_EXT};
     Expected<PassPlugin> Plugin = PassPlugin::Load(PluginPath);
     ASSERT_TRUE(!!Plugin) << "Plugin path: " << PluginPath;
     Plugin->registerPassBuilderCallbacks(PB);
diff --git a/llvm/unittests/Passes/Plugins/DoublerPlugin/CMakeLists.txt b/llvm/unittests/Passes/Plugins/DoublerPlugin/CMakeLists.txt
index 5b855b2bae338..d04a66ad0f3ee 100644
--- a/llvm/unittests/Passes/Plugins/DoublerPlugin/CMakeLists.txt
+++ b/llvm/unittests/Passes/Plugins/DoublerPlugin/CMakeLists.txt
@@ -1,15 +1,5 @@
 add_llvm_library(DoublerPlugin MODULE BUILDTREE_ONLY
-    DoublerPlugin.cpp
-    )
-
-# Put PLUGIN next to the unit test executable.
-set_output_directory(DoublerPlugin
-    BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/../
-    LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/../
-    )
+  DoublerPlugin.cpp
+)
 set_target_properties(DoublerPlugin PROPERTIES FOLDER "Tests")
-
-# The plugin depends on some of the output files of intrinsics_gen, so make sure
-# it is built before the plugin.
-add_dependencies(DoublerPlugin intrinsics_gen)
 add_dependencies(PluginsTests DoublerPlugin)
diff --git a/llvm/unittests/Passes/Plugins/PluginsTest.cpp b/llvm/unittests/Passes/Plugins/PluginsTest.cpp
index b1f09e966d6e5..a7bb234bf7bd0 100644
--- a/llvm/unittests/Passes/Plugins/PluginsTest.cpp
+++ b/llvm/unittests/Passes/Plugins/PluginsTest.cpp
@@ -27,27 +27,13 @@
 
 using namespace llvm;
 
-void anchor() {}
-
-static std::string LibPath(const std::string Name = "TestPlugin") {
-  const auto &Argvs = testing::internal::GetArgvs();
-  const char *Argv0 = Argvs.size() > 0 ? Argvs[0].c_str() : "PluginsTests";
-  void *Ptr = (void *)(intptr_t)anchor;
-  std::string Path = sys::fs::getMainExecutable(Argv0, Ptr);
-  llvm::SmallString<256> Buf{sys::path::parent_path(Path)};
-  sys::path::append(Buf, (Name + LLVM_PLUGIN_EXT).c_str());
-  return std::string(Buf.str());
-}
-
 TEST(PluginsTests, LoadPlugin) {
 #if !defined(LLVM_ENABLE_PLUGINS)
   // Skip the test if plugins are disabled.
   GTEST_SKIP();
 #endif
 
-  auto PluginPath = LibPath();
-  ASSERT_NE("", PluginPath);
-
+  auto PluginPath{std::string{"TestPlugin"} + LLVM_PLUGIN_EXT};
   Expected<PassPlugin> Plugin = PassPlugin::Load(PluginPath);
   ASSERT_TRUE(!!Plugin) << "Plugin path: " << PluginPath;
 
@@ -71,10 +57,8 @@ TEST(PluginsTests, LoadMultiplePlugins) {
   GTEST_SKIP();
 #endif
 
-  auto DoublerPluginPath = LibPath("DoublerPlugin");
-  auto TestPluginPath = LibPath("TestPlugin");
-  ASSERT_NE("", DoublerPluginPath);
-  ASSERT_NE("", TestPluginPath);
+  auto DoublerPluginPath{std::string{"DoublerPlugin"} + LLVM_PLUGIN_EXT};
+  auto TestPluginPath{std::string{"TestPlugin"} + LLVM_PLUGIN_EXT};
 
   Expected<PassPlugin> DoublerPlugin1 = PassPlugin::Load(DoublerPluginPath);
   ASSERT_TRUE(!!DoublerPlugin1)
diff --git a/llvm/unittests/Passes/Plugins/TestPlugin/CMakeLists.txt b/llvm/unittests/Passes/Plugins/TestPlugin/CMakeLists.txt
index ba94b0144be33..403b42c41afb2 100644
--- a/llvm/unittests/Passes/Plugins/TestPlugin/CMakeLists.txt
+++ b/llvm/unittests/Passes/Plugins/TestPlugin/CMakeLists.txt
@@ -1,15 +1,6 @@
 add_llvm_library(TestPlugin MODULE BUILDTREE_ONLY
-    TestPlugin.cpp
-    )
-
-# Put PLUGIN next to the unit test executable.
-set_output_directory(TestPlugin
-    BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/../
-    LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/../
-    )
+  TestPlugin.cpp
+)
 set_target_properties(TestPlugin PROPERTIES FOLDER "Tests")
 
-# The plugin depends on some of the output files of intrinsics_gen, so make sure
-# it is built before the plugin.
-add_dependencies(TestPlugin intrinsics_gen)
 add_dependencies(PluginsTests TestPlugin)
diff --git a/llvm/unittests/Support/DynamicLibrary/CMakeLists.txt b/llvm/unittests/Support/DynamicLibrary/CMakeLists.txt
index 98b7f2d19ee46..f41dc9843802e 100644
--- a/llvm/unittests/Support/DynamicLibrary/CMakeLists.txt
+++ b/llvm/unittests/Support/DynamicLibrary/CMakeLists.txt
@@ -33,19 +33,11 @@ if("${CMAKE_SYSTEM_NAME}" MATCHES "AIX")
 endif()
 
 function(dynlib_add_module NAME)
-  add_library(${NAME} MODULE
+  add_llvm_library(${NAME} MODULE BUILDTREE_ONLY
     PipSqueak.cpp
     )
-  set_target_properties(${NAME} PROPERTIES FOLDER "LLVM/Tests/Support")
-
-  set_output_directory(${NAME}
-    BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}
-    LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}
-    )
-
-  set_target_properties(${NAME}
-    PROPERTIES PREFIX ""
-    SUFFIX ${LLVM_PLUGIN_EXT}
+  set_target_properties(${NAME} PROPERTIES
+    FOLDER "LLVM/Tests/Support"
     )
 
   add_dependencies(DynamicLibraryTests ${NAME})
diff --git a/llvm/unittests/Support/DynamicLibrary/DynamicLibraryTest.cpp b/llvm/unittests/Support/DynamicLibrary/DynamicLibraryTest.cpp
index fb6f636e65b70..bea43afc31f7f 100644
--- a/llvm/unittests/Support/DynamicLibrary/DynamicLibraryTest.cpp
+++ b/llvm/unittests/Support/DynamicLibrary/DynamicLibraryTest.cpp
@@ -9,8 +9,6 @@
 #include "llvm/Support/DynamicLibrary.h"
 #include "llvm/Config/config.h"
 #include "llvm/Support/Compiler.h"
-#include "llvm/Support/FileSystem.h"
-#include "llvm/Support/Path.h"
 #include "gtest/gtest.h"
 
 #include "PipSqueak.h"
@@ -21,17 +19,6 @@
 using namespace llvm;
 using namespace llvm::sys;
 
-std::string LibPath(const std::string Name = "PipSqueak") {
-  const auto &Argvs = testing::internal::GetArgvs();
-  const char *Argv0 =
-      Argvs.size() > 0 ? Argvs[0].c_str() : "DynamicLibraryTests";
-  void *Ptr = (void*)(intptr_t)TestA;
-  std::string Path = fs::getMainExecutable(Argv0, Ptr);
-  llvm::SmallString<256> Buf(path::parent_path(Path));
-  path::append(Buf, (Name + LLVM_PLUGIN_EXT).c_str());
-  return std::string(Buf.str());
-}
-
 #if defined(_WIN32) || defined(HAVE_DLOPEN)
 
 typedef void (*SetStrings)(std::string &GStr, std::string &LStr);
@@ -62,8 +49,9 @@ std::string StdString(const char *Ptr) { return Ptr ? Ptr : ""; }
 TEST(DynamicLibrary, Overload) {
   {
     std::string Err;
+    auto LibPath{std::string{"PipSqueak"} + LLVM_PLUGIN_EXT};
     DynamicLibrary DL =
-        DynamicLibrary::getPermanentLibrary(LibPath().c_str(), &Err);
+        DynamicLibrary::getPermanentLibrary(LibPath.c_str(), &Err);
     EXPECT_TRUE(DL.isValid());
     EXPECT_TRUE(Err.empty());
 
@@ -115,8 +103,9 @@ TEST(DynamicLibrary, Overload) {
 
 TEST(DynamicLibrary, Unsupported) {
   std::string Err;
+  auto LibPath{std::string{"PipSqueak"} + LLVM_PLUGIN_EXT};
   DynamicLibrary DL =
-      DynamicLibrary::getPermanentLibrary(LibPath().c_str(), &Err);
+      DynamicLibrary::getPermanentLibrary(LibPath.c_str(), &Err);
   EXPECT_FALSE(DL.isValid());
   EXPECT_EQ(Err, "dlopen() not supported on this platform");
 }
 | 
| @llvm/pr-subscribers-llvm-support Author: Raul Tambre (tambry) ChangesAs proven by #159126 it's unnecessary to explicitly construct the path for loading the test plugins. On all supported platforms the RPATH is set appropriately for them to be found by the dynamic loader itself. Thus we can just rid ourselves of the extra code. This also cleans up some of the CMake code and C++ includes slightly. Surprisingly  See: #159126 Full diff: https://github.com/llvm/llvm-project/pull/163210.diff 11 Files Affected: 
 diff --git a/llvm/unittests/Analysis/InlineAdvisorPlugin/CMakeLists.txt b/llvm/unittests/Analysis/InlineAdvisorPlugin/CMakeLists.txt
index d9da627ad52e3..5c5cd07b3af84 100644
--- a/llvm/unittests/Analysis/InlineAdvisorPlugin/CMakeLists.txt
+++ b/llvm/unittests/Analysis/InlineAdvisorPlugin/CMakeLists.txt
@@ -1,5 +1,5 @@
-# The advisor plugin expects to not link against the Analysis, Support and Core 
-# libraries, but expects them to exist in the process loading the plugin. This 
+# The advisor plugin expects to not link against the Analysis, Support and Core
+# libraries, but expects them to exist in the process loading the plugin. This
 # doesn't work with DLLs on Windows (where a shared library can't have undefined
 # references), so just skip this testcase on Windows.
 if ((NOT WIN32 AND NOT CYGWIN) OR LLVM_BUILD_LLVM_DYLIB)
@@ -7,15 +7,6 @@ if ((NOT WIN32 AND NOT CYGWIN) OR LLVM_BUILD_LLVM_DYLIB)
   add_llvm_library(InlineAdvisorPlugin MODULE BUILDTREE_ONLY
     InlineAdvisorPlugin.cpp
     )
-  # Put PLUGIN next to the unit test executable.
-  set_output_directory(InlineAdvisorPlugin
-      BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/../
-      LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/../
-      )
   set_target_properties(InlineAdvisorPlugin PROPERTIES FOLDER "LLVM/Tests")
-
-  # The plugin depends on some of the output files of intrinsics_gen, so make sure
-  # it is built before the plugin.
-  add_dependencies(InlineAdvisorPlugin intrinsics_gen)
   add_dependencies(AnalysisTests InlineAdvisorPlugin)
 endif()
diff --git a/llvm/unittests/Analysis/InlineAdvisorPlugin/InlineAdvisorPlugin.cpp b/llvm/unittests/Analysis/InlineAdvisorPlugin/InlineAdvisorPlugin.cpp
index beefff2b3b106..831c95b54fdb4 100644
--- a/llvm/unittests/Analysis/InlineAdvisorPlugin/InlineAdvisorPlugin.cpp
+++ b/llvm/unittests/Analysis/InlineAdvisorPlugin/InlineAdvisorPlugin.cpp
@@ -3,8 +3,6 @@
 #include "llvm/Pass.h"
 #include "llvm/Passes/PassBuilder.h"
 #include "llvm/Passes/PassPlugin.h"
-#include "llvm/Support/CommandLine.h"
-#include "llvm/Support/raw_ostream.h"
 
 #include "llvm/Analysis/InlineAdvisor.h"
 
diff --git a/llvm/unittests/Analysis/InlineOrderPlugin/CMakeLists.txt b/llvm/unittests/Analysis/InlineOrderPlugin/CMakeLists.txt
index 941e18efc1a52..8dac91f90df9b 100644
--- a/llvm/unittests/Analysis/InlineOrderPlugin/CMakeLists.txt
+++ b/llvm/unittests/Analysis/InlineOrderPlugin/CMakeLists.txt
@@ -1,5 +1,5 @@
-# The order plugin expects to not link against the Analysis, Support and Core 
-# libraries, but expects them to exist in the process loading the plugin. This 
+# The order plugin expects to not link against the Analysis, Support and Core
+# libraries, but expects them to exist in the process loading the plugin. This
 # doesn't work with DLLs on Windows (where a shared library can't have undefined
 # references), so just skip this testcase on Windows.
 if ((NOT WIN32 AND NOT CYGWIN) OR LLVM_BUILD_LLVM_DYLIB)
@@ -7,15 +7,6 @@ if ((NOT WIN32 AND NOT CYGWIN) OR LLVM_BUILD_LLVM_DYLIB)
   add_llvm_library(InlineOrderPlugin MODULE BUILDTREE_ONLY
     InlineOrderPlugin.cpp
     )
-  # Put PLUGIN next to the unit test executable.
-  set_output_directory(InlineOrderPlugin
-      BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/../
-      LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/../
-      )
   set_target_properties(InlineOrderPlugin PROPERTIES FOLDER "Tests")
-
-  # The plugin depends on some of the output files of intrinsics_gen, so make sure
-  # it is built before the plugin.
-  add_dependencies(InlineOrderPlugin intrinsics_gen)
   add_dependencies(AnalysisTests InlineOrderPlugin)
 endif()
diff --git a/llvm/unittests/Analysis/InlineOrderPlugin/InlineOrderPlugin.cpp b/llvm/unittests/Analysis/InlineOrderPlugin/InlineOrderPlugin.cpp
index 9c46c1b8e22ba..b4063b5d86203 100644
--- a/llvm/unittests/Analysis/InlineOrderPlugin/InlineOrderPlugin.cpp
+++ b/llvm/unittests/Analysis/InlineOrderPlugin/InlineOrderPlugin.cpp
@@ -3,8 +3,6 @@
 #include "llvm/Pass.h"
 #include "llvm/Passes/PassBuilder.h"
 #include "llvm/Passes/PassPlugin.h"
-#include "llvm/Support/CommandLine.h"
-#include "llvm/Support/raw_ostream.h"
 
 #include "llvm/Analysis/InlineOrder.h"
 
diff --git a/llvm/unittests/Analysis/PluginInlineAdvisorAnalysisTest.cpp b/llvm/unittests/Analysis/PluginInlineAdvisorAnalysisTest.cpp
index ca4ea8b627e83..fbecb1fe6a288 100644
--- a/llvm/unittests/Analysis/PluginInlineAdvisorAnalysisTest.cpp
+++ b/llvm/unittests/Analysis/PluginInlineAdvisorAnalysisTest.cpp
@@ -4,28 +4,13 @@
 #include "llvm/IR/Module.h"
 #include "llvm/Passes/PassBuilder.h"
 #include "llvm/Passes/PassPlugin.h"
-#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/raw_ostream.h"
-#include "llvm/Testing/Support/Error.h"
 #include "gtest/gtest.h"
 
 namespace llvm {
 
 namespace {
 
-void anchor() {}
-
-static std::string libPath(const std::string Name = "InlineAdvisorPlugin") {
-  const auto &Argvs = testing::internal::GetArgvs();
-  const char *Argv0 =
-      Argvs.size() > 0 ? Argvs[0].c_str() : "PluginInlineAdvisorAnalysisTest";
-  void *Ptr = (void *)(intptr_t)anchor;
-  std::string Path = sys::fs::getMainExecutable(Argv0, Ptr);
-  llvm::SmallString<256> Buf{sys::path::parent_path(Path)};
-  sys::path::append(Buf, (Name + LLVM_PLUGIN_EXT).c_str());
-  return std::string(Buf.str());
-}
-
 // Example of a custom InlineAdvisor that only inlines calls to functions called
 // "foo".
 class FooOnlyInlineAdvisor : public InlineAdvisor {
@@ -61,8 +46,7 @@ struct CompilerInstance {
 
   // connect the plugin to our compiler instance
   void setupPlugin() {
-    auto PluginPath = libPath();
-    ASSERT_NE("", PluginPath);
+    auto PluginPath{std::string{"InlineAdvisorPlugin"} + LLVM_PLUGIN_EXT};
     Expected<PassPlugin> Plugin = PassPlugin::Load(PluginPath);
     ASSERT_TRUE(!!Plugin) << "Plugin path: " << PluginPath;
     Plugin->registerPassBuilderCallbacks(PB);
diff --git a/llvm/unittests/Analysis/PluginInlineOrderAnalysisTest.cpp b/llvm/unittests/Analysis/PluginInlineOrderAnalysisTest.cpp
index 0b31b0892d75a..9f12bb502109f 100644
--- a/llvm/unittests/Analysis/PluginInlineOrderAnalysisTest.cpp
+++ b/llvm/unittests/Analysis/PluginInlineOrderAnalysisTest.cpp
@@ -4,30 +4,13 @@
 #include "llvm/IR/Module.h"
 #include "llvm/Passes/PassBuilder.h"
 #include "llvm/Passes/PassPlugin.h"
-#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/raw_ostream.h"
-#include "llvm/Testing/Support/Error.h"
 #include "gtest/gtest.h"
 
-#include "llvm/Analysis/InlineOrder.h"
-
 namespace llvm {
 
 namespace {
 
-void anchor() {}
-
-std::string libPath(const std::string Name = "InlineOrderPlugin") {
-  const auto &Argvs = testing::internal::GetArgvs();
-  const char *Argv0 =
-      Argvs.size() > 0 ? Argvs[0].c_str() : "PluginInlineOrderAnalysisTest";
-  void *Ptr = (void *)(intptr_t)anchor;
-  std::string Path = sys::fs::getMainExecutable(Argv0, Ptr);
-  llvm::SmallString<256> Buf{sys::path::parent_path(Path)};
-  sys::path::append(Buf, (Name + LLVM_PLUGIN_EXT).c_str());
-  return std::string(Buf.str());
-}
-
 struct CompilerInstance {
   LLVMContext Ctx;
   ModulePassManager MPM;
@@ -43,8 +26,7 @@ struct CompilerInstance {
 
   // Connect the plugin to our compiler instance.
   void setupPlugin() {
-    auto PluginPath = libPath();
-    ASSERT_NE("", PluginPath);
+    auto PluginPath{std::string{"InlineOrderPlugin"} + LLVM_PLUGIN_EXT};
     Expected<PassPlugin> Plugin = PassPlugin::Load(PluginPath);
     ASSERT_TRUE(!!Plugin) << "Plugin path: " << PluginPath;
     Plugin->registerPassBuilderCallbacks(PB);
diff --git a/llvm/unittests/Passes/Plugins/DoublerPlugin/CMakeLists.txt b/llvm/unittests/Passes/Plugins/DoublerPlugin/CMakeLists.txt
index 5b855b2bae338..d04a66ad0f3ee 100644
--- a/llvm/unittests/Passes/Plugins/DoublerPlugin/CMakeLists.txt
+++ b/llvm/unittests/Passes/Plugins/DoublerPlugin/CMakeLists.txt
@@ -1,15 +1,5 @@
 add_llvm_library(DoublerPlugin MODULE BUILDTREE_ONLY
-    DoublerPlugin.cpp
-    )
-
-# Put PLUGIN next to the unit test executable.
-set_output_directory(DoublerPlugin
-    BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/../
-    LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/../
-    )
+  DoublerPlugin.cpp
+)
 set_target_properties(DoublerPlugin PROPERTIES FOLDER "Tests")
-
-# The plugin depends on some of the output files of intrinsics_gen, so make sure
-# it is built before the plugin.
-add_dependencies(DoublerPlugin intrinsics_gen)
 add_dependencies(PluginsTests DoublerPlugin)
diff --git a/llvm/unittests/Passes/Plugins/PluginsTest.cpp b/llvm/unittests/Passes/Plugins/PluginsTest.cpp
index b1f09e966d6e5..a7bb234bf7bd0 100644
--- a/llvm/unittests/Passes/Plugins/PluginsTest.cpp
+++ b/llvm/unittests/Passes/Plugins/PluginsTest.cpp
@@ -27,27 +27,13 @@
 
 using namespace llvm;
 
-void anchor() {}
-
-static std::string LibPath(const std::string Name = "TestPlugin") {
-  const auto &Argvs = testing::internal::GetArgvs();
-  const char *Argv0 = Argvs.size() > 0 ? Argvs[0].c_str() : "PluginsTests";
-  void *Ptr = (void *)(intptr_t)anchor;
-  std::string Path = sys::fs::getMainExecutable(Argv0, Ptr);
-  llvm::SmallString<256> Buf{sys::path::parent_path(Path)};
-  sys::path::append(Buf, (Name + LLVM_PLUGIN_EXT).c_str());
-  return std::string(Buf.str());
-}
-
 TEST(PluginsTests, LoadPlugin) {
 #if !defined(LLVM_ENABLE_PLUGINS)
   // Skip the test if plugins are disabled.
   GTEST_SKIP();
 #endif
 
-  auto PluginPath = LibPath();
-  ASSERT_NE("", PluginPath);
-
+  auto PluginPath{std::string{"TestPlugin"} + LLVM_PLUGIN_EXT};
   Expected<PassPlugin> Plugin = PassPlugin::Load(PluginPath);
   ASSERT_TRUE(!!Plugin) << "Plugin path: " << PluginPath;
 
@@ -71,10 +57,8 @@ TEST(PluginsTests, LoadMultiplePlugins) {
   GTEST_SKIP();
 #endif
 
-  auto DoublerPluginPath = LibPath("DoublerPlugin");
-  auto TestPluginPath = LibPath("TestPlugin");
-  ASSERT_NE("", DoublerPluginPath);
-  ASSERT_NE("", TestPluginPath);
+  auto DoublerPluginPath{std::string{"DoublerPlugin"} + LLVM_PLUGIN_EXT};
+  auto TestPluginPath{std::string{"TestPlugin"} + LLVM_PLUGIN_EXT};
 
   Expected<PassPlugin> DoublerPlugin1 = PassPlugin::Load(DoublerPluginPath);
   ASSERT_TRUE(!!DoublerPlugin1)
diff --git a/llvm/unittests/Passes/Plugins/TestPlugin/CMakeLists.txt b/llvm/unittests/Passes/Plugins/TestPlugin/CMakeLists.txt
index ba94b0144be33..403b42c41afb2 100644
--- a/llvm/unittests/Passes/Plugins/TestPlugin/CMakeLists.txt
+++ b/llvm/unittests/Passes/Plugins/TestPlugin/CMakeLists.txt
@@ -1,15 +1,6 @@
 add_llvm_library(TestPlugin MODULE BUILDTREE_ONLY
-    TestPlugin.cpp
-    )
-
-# Put PLUGIN next to the unit test executable.
-set_output_directory(TestPlugin
-    BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/../
-    LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/../
-    )
+  TestPlugin.cpp
+)
 set_target_properties(TestPlugin PROPERTIES FOLDER "Tests")
 
-# The plugin depends on some of the output files of intrinsics_gen, so make sure
-# it is built before the plugin.
-add_dependencies(TestPlugin intrinsics_gen)
 add_dependencies(PluginsTests TestPlugin)
diff --git a/llvm/unittests/Support/DynamicLibrary/CMakeLists.txt b/llvm/unittests/Support/DynamicLibrary/CMakeLists.txt
index 98b7f2d19ee46..f41dc9843802e 100644
--- a/llvm/unittests/Support/DynamicLibrary/CMakeLists.txt
+++ b/llvm/unittests/Support/DynamicLibrary/CMakeLists.txt
@@ -33,19 +33,11 @@ if("${CMAKE_SYSTEM_NAME}" MATCHES "AIX")
 endif()
 
 function(dynlib_add_module NAME)
-  add_library(${NAME} MODULE
+  add_llvm_library(${NAME} MODULE BUILDTREE_ONLY
     PipSqueak.cpp
     )
-  set_target_properties(${NAME} PROPERTIES FOLDER "LLVM/Tests/Support")
-
-  set_output_directory(${NAME}
-    BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}
-    LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}
-    )
-
-  set_target_properties(${NAME}
-    PROPERTIES PREFIX ""
-    SUFFIX ${LLVM_PLUGIN_EXT}
+  set_target_properties(${NAME} PROPERTIES
+    FOLDER "LLVM/Tests/Support"
     )
 
   add_dependencies(DynamicLibraryTests ${NAME})
diff --git a/llvm/unittests/Support/DynamicLibrary/DynamicLibraryTest.cpp b/llvm/unittests/Support/DynamicLibrary/DynamicLibraryTest.cpp
index fb6f636e65b70..bea43afc31f7f 100644
--- a/llvm/unittests/Support/DynamicLibrary/DynamicLibraryTest.cpp
+++ b/llvm/unittests/Support/DynamicLibrary/DynamicLibraryTest.cpp
@@ -9,8 +9,6 @@
 #include "llvm/Support/DynamicLibrary.h"
 #include "llvm/Config/config.h"
 #include "llvm/Support/Compiler.h"
-#include "llvm/Support/FileSystem.h"
-#include "llvm/Support/Path.h"
 #include "gtest/gtest.h"
 
 #include "PipSqueak.h"
@@ -21,17 +19,6 @@
 using namespace llvm;
 using namespace llvm::sys;
 
-std::string LibPath(const std::string Name = "PipSqueak") {
-  const auto &Argvs = testing::internal::GetArgvs();
-  const char *Argv0 =
-      Argvs.size() > 0 ? Argvs[0].c_str() : "DynamicLibraryTests";
-  void *Ptr = (void*)(intptr_t)TestA;
-  std::string Path = fs::getMainExecutable(Argv0, Ptr);
-  llvm::SmallString<256> Buf(path::parent_path(Path));
-  path::append(Buf, (Name + LLVM_PLUGIN_EXT).c_str());
-  return std::string(Buf.str());
-}
-
 #if defined(_WIN32) || defined(HAVE_DLOPEN)
 
 typedef void (*SetStrings)(std::string &GStr, std::string &LStr);
@@ -62,8 +49,9 @@ std::string StdString(const char *Ptr) { return Ptr ? Ptr : ""; }
 TEST(DynamicLibrary, Overload) {
   {
     std::string Err;
+    auto LibPath{std::string{"PipSqueak"} + LLVM_PLUGIN_EXT};
     DynamicLibrary DL =
-        DynamicLibrary::getPermanentLibrary(LibPath().c_str(), &Err);
+        DynamicLibrary::getPermanentLibrary(LibPath.c_str(), &Err);
     EXPECT_TRUE(DL.isValid());
     EXPECT_TRUE(Err.empty());
 
@@ -115,8 +103,9 @@ TEST(DynamicLibrary, Overload) {
 
 TEST(DynamicLibrary, Unsupported) {
   std::string Err;
+  auto LibPath{std::string{"PipSqueak"} + LLVM_PLUGIN_EXT};
   DynamicLibrary DL =
-      DynamicLibrary::getPermanentLibrary(LibPath().c_str(), &Err);
+      DynamicLibrary::getPermanentLibrary(LibPath.c_str(), &Err);
   EXPECT_FALSE(DL.isValid());
   EXPECT_EQ(Err, "dlopen() not supported on this platform");
 }
 | 
As proven by llvm#159126 it's unnecessary to explicitly construct the path for loading the test plugins. On all supported platforms the rpath is set appropriately for them to be found by the dynamic loader itself. Thus we can just rid ourselves of the extra code. The only exception is `Support/DynamicLibrary` as it also runs on Windows, which lacks the concept of a rpath. This also cleans up some of the CMake code and C++ includes slightly. Surprisingly `intrinsics_gen` isn't necessary for any of them as none of them actually do much. See: llvm#159126
c334ac2    to
    354eaa1      
    Compare
  
    | Seems to fail in CI and I can't think of why. Windows I can understand, but not the Linux failures. Works perfectly locally on amd64. | 
As proven by #159126 it's unnecessary to explicitly construct the path for loading the test plugins. On all supported platforms the RPATH is set appropriately for them to be found by the dynamic loader itself. Thus we can just rid ourselves of the extra code.
This also cleans up some of the CMake code and C++ includes slightly. Surprisingly
intrinsics_genisn't necessary for any of them as none of them actually do much.See: #159126