From d34dce25d9585305c1d7a89f16a259bbbf62ff2b Mon Sep 17 00:00:00 2001 From: Usman Nadeem Date: Thu, 10 Nov 2022 14:09:51 -0800 Subject: [PATCH] [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 --- flang/docs/FlangDriver.md | 12 ++++++++++++ flang/docs/ReleaseNotes.md | 3 +++ flang/lib/Frontend/CMakeLists.txt | 1 + flang/lib/Frontend/FrontendActions.cpp | 9 +++++++++ flang/test/CMakeLists.txt | 1 + flang/test/Driver/pass-plugin.f90 | 10 +++++++--- flang/test/lit.cfg.py | 7 +++++++ flang/test/lit.site.cfg.py.in | 1 + 8 files changed, 41 insertions(+), 3 deletions(-) diff --git a/flang/docs/FlangDriver.md b/flang/docs/FlangDriver.md index 389bb1c..7e182b7 100644 --- a/flang/docs/FlangDriver.md +++ b/flang/docs/FlangDriver.md @@ -532,3 +532,15 @@ flang-new -fpass-plugin=/path/to/plugin.so 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 0cc85db..d30d7e2 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 fac5f2c..d3947f6 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 40f30a9..9deef99 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 f31396a..d3b20ca 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 ed3a3e2..8c8c4c7 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 9e3cfcc..1d353a6 100644 --- a/flang/test/lit.cfg.py +++ b/flang/test/lit.cfg.py @@ -58,6 +58,13 @@ if config.flang_examples: 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 dcdb119..ce8e02a 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@" -- 2.7.4