[flang-commits] [flang] d34dce2 - [Flang] Allow registering plugin extensions with the pass builder

Usman Nadeem via flang-commits flang-commits at lists.llvm.org
Thu Nov 10 14:16:36 PST 2022


Author: Usman Nadeem
Date: 2022-11-10T14:16:15-08:00
New Revision: d34dce25d9585305c1d7a89f16a259bbbf62ff2b

URL: https://github.com/llvm/llvm-project/commit/d34dce25d9585305c1d7a89f16a259bbbf62ff2b
DIFF: https://github.com/llvm/llvm-project/commit/d34dce25d9585305c1d7a89f16a259bbbf62ff2b.diff

LOG: [Flang] Allow registering plugin extensions with the pass builder

Pass plugins are compiled and linked dynamically by default. Setting
`LLVM_${NAME}_LINK_INTO_TOOLS` to `ON` turns the project into a
statically linked extension. Projects like Polly can be used this way by
adding `-DLLVM_POLLY_LINK_INTO_TOOLS=ON` to the `cmake` command.

The changes in this patch makes the PassBuilder in Flang aware of
statically linked pass plugins, see the documentation for more details:
https://github.com/llvm/llvm-project/blob/main/llvm/docs/WritingAnLLVMNewPMPass.rst#id21

Differential Revision: https://reviews.llvm.org/D137673

Change-Id: Id1aa501dcb4821d0ec779f375cc8e8d6b0b92fce

Added: 
    

Modified: 
    flang/docs/FlangDriver.md
    flang/docs/ReleaseNotes.md
    flang/lib/Frontend/CMakeLists.txt
    flang/lib/Frontend/FrontendActions.cpp
    flang/test/CMakeLists.txt
    flang/test/Driver/pass-plugin.f90
    flang/test/lit.cfg.py
    flang/test/lit.site.cfg.py.in

Removed: 
    


################################################################################
diff  --git a/flang/docs/FlangDriver.md b/flang/docs/FlangDriver.md
index 389bb1cf6cd01..7e182b75d477e 100644
--- a/flang/docs/FlangDriver.md
+++ b/flang/docs/FlangDriver.md
@@ -532,3 +532,15 @@ flang-new -fpass-plugin=/path/to/plugin.so <file.f90>
 
 This option is available in both the compiler driver and the frontend driver.
 Note that LLVM plugins are not officially supported on Windows.
+
+## LLVM Pass Extensions
+
+Pass extensions are similar to plugins, except that they can also be linked
+statically. Setting `-DLLVM_${NAME}_LINK_INTO_TOOLS` to `ON` in the cmake
+command turns the project into a statically linked extension. An example would
+be Polly, e.g., using `-DLLVM_POLLY_LINK_INTO_TOOLS=ON` would link Polly passes
+into `flang-new` as built-in middle-end passes.
+
+See the
+[`WritingAnLLVMNewPMPass`](https://llvm.org/docs/WritingAnLLVMNewPMPass.html#id9)
+documentation for more details.

diff  --git a/flang/docs/ReleaseNotes.md b/flang/docs/ReleaseNotes.md
index 0cc85db9debc5..d30d7e28917ad 100644
--- a/flang/docs/ReleaseNotes.md
+++ b/flang/docs/ReleaseNotes.md
@@ -27,6 +27,9 @@ page](https://llvm.org/releases/).
 * Flang now supports loading LLVM pass plugins with the `-fpass-plugin` option
   which is also available in clang. The option mimics the behavior of the
   corresponding option in clang and has the same capabilities and limitations.
+* Flang also supports statically linked LLVM pass extensions. Projects can be
+  linked statically into `flang-new` if the cmake command includes
+  `-DLLVM_${NAME}_LINK_INTO_TOOLS=ON`. This behavior is also similar to clang.
 
 ## Bug Fixes
 

diff  --git a/flang/lib/Frontend/CMakeLists.txt b/flang/lib/Frontend/CMakeLists.txt
index fac5f2c1a1f87..d3947f64d0295 100644
--- a/flang/lib/Frontend/CMakeLists.txt
+++ b/flang/lib/Frontend/CMakeLists.txt
@@ -42,6 +42,7 @@ add_flang_library(flangFrontend
   LINK_COMPONENTS
   Passes
   Analysis
+  Extensions
   IRReader
   Option
   Support

diff  --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp
index 40f30a90554e7..9deef99608bb5 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -57,6 +57,11 @@
 
 using namespace Fortran::frontend;
 
+// Declare plugin extension function declarations.
+#define HANDLE_EXTENSION(Ext)                                                  \
+  llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
+#include "llvm/Support/Extension.def"
+
 //===----------------------------------------------------------------------===//
 // Custom BeginSourceFileAction
 //===----------------------------------------------------------------------===//
@@ -703,6 +708,10 @@ void CodeGenAction::runOptimizationPipeline(llvm::raw_pwrite_stream &os) {
           << pluginFile << passPlugin.takeError();
     }
   }
+  // Register static plugin extensions.
+#define HANDLE_EXTENSION(Ext)                                                  \
+  get##Ext##PluginInfo().RegisterPassBuilderCallbacks(pb);
+#include "llvm/Support/Extension.def"
 
   // Register all the basic analyses with the managers.
   pb.registerModuleAnalyses(mam);

diff  --git a/flang/test/CMakeLists.txt b/flang/test/CMakeLists.txt
index f31396ad2bc4b..d3b20caa35311 100644
--- a/flang/test/CMakeLists.txt
+++ b/flang/test/CMakeLists.txt
@@ -5,6 +5,7 @@ add_subdirectory(lib)
 llvm_canonicalize_cmake_booleans(
   FLANG_BUILD_EXAMPLES
   FLANG_STANDALONE_BUILD
+  LLVM_BYE_LINK_INTO_TOOLS
   LLVM_ENABLE_PLUGINS
 )
 

diff  --git a/flang/test/Driver/pass-plugin.f90 b/flang/test/Driver/pass-plugin.f90
index ed3a3e2403a31..8c8c4c70def86 100644
--- a/flang/test/Driver/pass-plugin.f90
+++ b/flang/test/Driver/pass-plugin.f90
@@ -1,11 +1,15 @@
-! Verify that the plugin passed to -fpass-plugin is loaded and run
+! Verify that the static and dynamically loaded pass plugins work as expected.
 
 ! UNSUPPORTED: system-windows
 
 ! REQUIRES: plugins, shell, examples
 
-! RUN: %flang -S %s -fpass-plugin=%llvmshlibdir/Bye%pluginext -Xflang -fdebug-pass-manager -o /dev/null 2>&1 | FileCheck %s
-! RUN: %flang_fc1 -S %s -fpass-plugin=%llvmshlibdir/Bye%pluginext -fdebug-pass-manager -o /dev/null 2>&1 | FileCheck %s
+! RUN: %flang -S %s %loadbye -Xflang -fdebug-pass-manager -o /dev/null \
+! RUN: 2>&1 | FileCheck %s
+
+! RUN: %flang_fc1 -S %s %loadbye -fdebug-pass-manager -o /dev/null \
+! RUN: 2>&1 | FileCheck %s
+
 
 ! CHECK: Running pass: {{.*}}Bye on empty_
 

diff  --git a/flang/test/lit.cfg.py b/flang/test/lit.cfg.py
index 9e3cfcc97000a..1d353a6e2dde2 100644
--- a/flang/test/lit.cfg.py
+++ b/flang/test/lit.cfg.py
@@ -58,6 +58,13 @@
 if config.has_plugins:
     config.available_features.add('plugins')
 
+if config.linked_bye_extension:
+    config.substitutions.append(('%loadbye', ''))
+else:
+    config.substitutions.append(('%loadbye',
+                                 '-fpass-plugin={}/Bye{}'.format(config.llvm_shlib_dir,
+                                                                 config.llvm_plugin_ext)))
+
 # test_source_root: The root path where tests are located.
 config.test_source_root = os.path.dirname(__file__)
 

diff  --git a/flang/test/lit.site.cfg.py.in b/flang/test/lit.site.cfg.py.in
index dcdb119524fd3..ce8e02a061f94 100644
--- a/flang/test/lit.site.cfg.py.in
+++ b/flang/test/lit.site.cfg.py.in
@@ -17,6 +17,7 @@ config.flang_examples = @FLANG_BUILD_EXAMPLES@
 config.python_executable = "@PYTHON_EXECUTABLE@"
 config.flang_standalone_build = @FLANG_STANDALONE_BUILD@
 config.has_plugins = @LLVM_ENABLE_PLUGINS@
+config.linked_bye_extension = @LLVM_BYE_LINK_INTO_TOOLS@
 config.cc = "@CMAKE_C_COMPILER@"
 config.targets_to_build = "@TARGETS_TO_BUILD@"
 


        


More information about the flang-commits mailing list