diff --git a/flang/docs/FlangDriver.md b/flang/docs/FlangDriver.md --- a/flang/docs/FlangDriver.md +++ b/flang/docs/FlangDriver.md @@ -532,3 +532,15 @@ 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 --- a/flang/docs/ReleaseNotes.md +++ b/flang/docs/ReleaseNotes.md @@ -27,6 +27,9 @@ * 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 --- a/flang/lib/Frontend/CMakeLists.txt +++ b/flang/lib/Frontend/CMakeLists.txt @@ -42,6 +42,7 @@ LINK_COMPONENTS Passes Analysis + Extensions IRReader Option Support diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp --- 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 @@ << 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 --- a/flang/test/CMakeLists.txt +++ b/flang/test/CMakeLists.txt @@ -5,6 +5,7 @@ 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 --- 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 --- 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 --- a/flang/test/lit.site.cfg.py.in +++ b/flang/test/lit.site.cfg.py.in @@ -17,6 +17,7 @@ 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@"