[PATCH] D65295: [opt] Ensure the IR-Linker is available to plugins.

Michael Kruse via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 25 11:28:40 PDT 2019


Meinersbur created this revision.
Meinersbur added reviewers: jdoerfert, philip.pfaffe, bogner, dsanders, serge-sans-paille, grosser, mehdi_amini, smeenai, beanz, compnerd.
Meinersbur added a project: LLVM.
Herald added a subscriber: mgorny.
Herald added a reviewer: bollu.

When using a statically linked opt executable (no BUILD_SHARED_LIBS or LLVM_LINK_LLVM_DYLIB), the linker only includes symbols used (directly or transitively) by the opt program itself. However, when loading another module using the "-load" mechanism from Passes/PassPlugin.cpp or Support/PluginLoader.cpp, it may require symbols that have not been included into the executable.

In this specific case, Polly uses the LLVM-IR linker, which is not included in the statically linked opt executable. As a result, some Polly regressions tests may fail with the error message

  opt: symbol lookup error: LLVMPolly.so: undefined symbol: _ZN4llvm6LinkerC1ERNS_6ModuleE

(It does work using clang which uses the LLVM linker).

This patch ensures that the LLVMLinker is available in the opt executable by referencing it in one of opt's main object files. The exported symbol `ensurePluginSymbols` creates a linker object without being called anywhere.

This is a pragmatic fix for Polly and unfortunately does not solve the more general problem that not all symbols are available to plugins in statically linked tools. I am open for better solutions.


Repository:
  rL LLVM

https://reviews.llvm.org/D65295

Files:
  tools/opt/CMakeLists.txt
  tools/opt/EnsurePluginSymbols.cpp


Index: tools/opt/EnsurePluginSymbols.cpp
===================================================================
--- /dev/null
+++ tools/opt/EnsurePluginSymbols.cpp
@@ -0,0 +1,27 @@
+//===- EnsurePluginSymbols.cpp - ------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Plugins via the -load mechanism may require symbols that opt itself does not
+// use. This file ensures that those symbols are added to the executable by the
+// static library linker.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/IR/Module.h"
+#include "llvm/Linker/Linker.h"
+
+using namespace llvm;
+
+void ensurePluginSymbols() {
+  LLVMContext C;
+  Module M("", C);
+
+  // Linker is e.g. required by Polly's GPGPU generator to link libdevice with
+  // user code.
+  Linker L(M);
+}
Index: tools/opt/CMakeLists.txt
===================================================================
--- tools/opt/CMakeLists.txt
+++ tools/opt/CMakeLists.txt
@@ -22,6 +22,7 @@
   TransformUtils
   Vectorize
   Passes
+  Linker
   )
 
 # Support plugins.
@@ -31,6 +32,7 @@
   AnalysisWrappers.cpp
   BreakpointPrinter.cpp
   Debugify.cpp
+  EnsurePluginSymbols.cpp
   GraphPrinters.cpp
   NewPMDriver.cpp
   PassPrinters.cpp


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65295.211790.patch
Type: text/x-patch
Size: 1534 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190725/455a8d76/attachment.bin>


More information about the llvm-commits mailing list