diff --git a/clang/tools/driver/CMakeLists.txt b/clang/tools/driver/CMakeLists.txt --- a/clang/tools/driver/CMakeLists.txt +++ b/clang/tools/driver/CMakeLists.txt @@ -123,6 +123,4 @@ endif() endif() -if(WITH_POLLY AND LINK_POLLY_INTO_TOOLS) - target_link_libraries(clang PRIVATE Polly) -endif(WITH_POLLY AND LINK_POLLY_INTO_TOOLS) +target_link_libraries(clang PRIVATE $) diff --git a/clang/tools/driver/cc1_main.cpp b/clang/tools/driver/cc1_main.cpp --- a/clang/tools/driver/cc1_main.cpp +++ b/clang/tools/driver/cc1_main.cpp @@ -69,11 +69,10 @@ exit(GenCrashDiag ? 70 : 1); } -#ifdef LINK_POLLY_INTO_TOOLS -namespace polly { -void initializePollyPasses(llvm::PassRegistry &Registry); -} -#endif +#define HANDLE_EXTENSION(Ext) \ + void initialize##Ext##Passes(llvm::PassRegistry &Registry); + +#include "llvm/Support/Extension.def" #ifdef CLANG_HAVE_RLIMITS #if defined(__linux__) && defined(__PIE__) @@ -183,10 +182,13 @@ llvm::InitializeAllAsmPrinters(); llvm::InitializeAllAsmParsers(); -#ifdef LINK_POLLY_INTO_TOOLS - llvm::PassRegistry &Registry = *llvm::PassRegistry::getPassRegistry(); - polly::initializePollyPasses(Registry); -#endif +#define HANDLE_EXTENSION(Ext) \ + { \ + llvm::PassRegistry &Registry = *llvm::PassRegistry::getPassRegistry(); \ + initialize##Ext##Passes(Registry); \ + } + +#include "llvm/Support/Extension.def" // Buffer diagnostics from argument parsing so that we can output them using a // well formed diagnostic object. diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt --- a/llvm/CMakeLists.txt +++ b/llvm/CMakeLists.txt @@ -494,29 +494,6 @@ option(LLVM_USE_SPLIT_DWARF "Use -gsplit-dwarf when compiling llvm." OFF) -option(LLVM_POLLY_LINK_INTO_TOOLS "Statically link Polly into tools (if available)" ON) -option(LLVM_POLLY_BUILD "Build LLVM with Polly" ON) - -if (EXISTS ${LLVM_MAIN_SRC_DIR}/tools/polly/CMakeLists.txt) - set(POLLY_IN_TREE TRUE) -elseif(LLVM_EXTERNAL_POLLY_SOURCE_DIR) - set(POLLY_IN_TREE TRUE) -else() - set(POLLY_IN_TREE FALSE) -endif() - -if (LLVM_POLLY_BUILD AND POLLY_IN_TREE) - set(WITH_POLLY ON) -else() - set(WITH_POLLY OFF) -endif() - -if (LLVM_POLLY_LINK_INTO_TOOLS AND WITH_POLLY) - set(LINK_POLLY_INTO_TOOLS ON) -else() - set(LINK_POLLY_INTO_TOOLS OFF) -endif() - # Define an option controlling whether we should build for 32-bit on 64-bit # platforms, where supported. if( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 ) @@ -1143,3 +1120,29 @@ add_subdirectory(utils/benchmark) add_subdirectory(benchmarks) endif() + +# generate X Macro file for extension handling. It provides a +# HANDLE_EXTENSION(extension_namespace, ExtensionProject) call for each extension +# allowing client code to define HANDLE_EXTENSION to have a specific code be run for +# each extension. +get_property(LLVM_EXTENSIONS GLOBAL PROPERTY LLVM_EXTENSIONS) +file(WRITE "${CMAKE_BINARY_DIR}/include/llvm/Support/Extension.def.tmp" "//extension handlers\n") +foreach(llvm_extension ${LLVM_EXTENSIONS}) + string(TOLOWER ${llvm_extension} llvm_extension_lower) + + string(TOUPPER ${llvm_extension} llvm_extension_upper) + string(SUBSTRING ${llvm_extension_upper} 0 1 llvm_extension_upper_first) + string(SUBSTRING ${llvm_extension_lower} 1 -1 llvm_extension_lower_tail) + string(CONCAT llvm_extension_project ${llvm_extension_upper_first} ${llvm_extension_lower_tail}) + + if(LLVM_${llvm_extension_upper}_LINK_INTO_TOOLS) + file(APPEND "${CMAKE_BINARY_DIR}/include/llvm/Support/Extension.def.tmp" "HANDLE_EXTENSION(${llvm_extension_project})\n") + endif() +endforeach() +file(APPEND "${CMAKE_BINARY_DIR}/include/llvm/Support/Extension.def.tmp" "#undef HANDLE_EXTENSION\n") + +# only replace if there's an actual change +execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_BINARY_DIR}/include/llvm/Support/Extension.def.tmp" "${CMAKE_BINARY_DIR}/include/llvm/Support/Extension.def") +file(REMOVE "${CMAKE_BINARY_DIR}/include/llvm/Support/Extension.def.tmp") + + diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake --- a/llvm/cmake/modules/AddLLVM.cmake +++ b/llvm/cmake/modules/AddLLVM.cmake @@ -790,6 +790,41 @@ llvm_codesign(${name} ENTITLEMENTS ${ARG_ENTITLEMENTS}) endmacro(add_llvm_executable name) +# register_llvm_extension(name) +# Register ${name} as an llvm compiler extension. +# A compiler extension must define two functions in the top-level namespace +# - void initialize${Name}Passes(llvm::PassRegistry &Registry) that calls the initializer of dependant passes/ analyse for the legacy PM +# - llvm::PassPluginLibraryInfo getByePluginInfo() used by the new PM + +function(register_llvm_extension llvm_extension) + string(TOLOWER ${llvm_extension} llvm_extension_lower) + + string(TOUPPER ${llvm_extension} llvm_extension_upper) + string(SUBSTRING ${llvm_extension_upper} 0 1 llvm_extension_upper_first) + string(SUBSTRING ${llvm_extension_lower} 1 -1 llvm_extension_lower_tail) + string(CONCAT llvm_extension_project ${llvm_extension_upper_first} ${llvm_extension_lower_tail}) + + option(LLVM_${llvm_extension_upper}_LINK_INTO_TOOLS "Statically link ${llvm_extension_project} into tools (if available)" ON) + option(LLVM_${llvm_extension_upper}_BUILD "Build LLVM with ${llvm_extension_project}" ON) + + if(LLVM_${llvm_extension_upper}_BUILD) + message(STATUS "Registering ${llvm_extension_project} as a compiler extension (static build: ${LLVM_${llvm_extension_upper}_LINK_INTO_TOOLS})") + if(LLVM_${llvm_extension_upper}_LINK_INTO_TOOLS) + set_property(GLOBAL APPEND PROPERTY LLVM_EXTENSIONS ${name}) + + set(LLVM_ALL_EXTENSION_TARGETS clang;bugpoint;opt) + foreach(tool ${LLVM_ALL_EXTENSION_TARGETS}) + if(TARGET ${tool}) + set_property(TARGET ${tool} APPEND PROPERTY LLVM_COMPILER_EXTENSIONS ${llvm_extension_project}) + endif() + endforeach() + endif() + else() + message(STATUS "Skipping compiler extension ${llvm_extension_project}") + endif() +endfunction(register_llvm_extension) + + function(export_executable_symbols target) if (LLVM_EXPORTED_SYMBOL_FILE) # The symbol file should contain the symbols we want the executable to diff --git a/llvm/docs/WritingAnLLVMPass.rst b/llvm/docs/WritingAnLLVMPass.rst --- a/llvm/docs/WritingAnLLVMPass.rst +++ b/llvm/docs/WritingAnLLVMPass.rst @@ -1219,6 +1219,31 @@ this internal state. This method is called after the ``run*`` method for the class, before the next call of ``run*`` in your pass. +Building out-of-tree passes +=========================== + +LLVM provides a mechanism to automatically register compiler passes for ``clang``, +``opt`` and ``bugpoint``. One first needs to create an independent project and add +it to either ``tools/`` or, using the MonoRepo layout, at the root of the repo alongside +other projects. This project must contain the following minimal ``CMakeLists.txt``: + +.. code-block:: cmake + + add_llvm_library(Name + source0.cpp) + register_llvm_extension(Name) + +The project should define the following functions to perform the appropriate pass initialization/registration: + +- ``void initialize${Name}Passes(llvm::PassRegistry &Registry)`` that calls the initializer of dependant passes/analyse for legacy PM; +- ``llvm::PassPluginLibraryInfo getByePluginInfo()`` used by the new PM. + +Out-of-tree passes are compiled and link statically by default, but it's possible to set the following variables to change this behavior: + +- ``LLVM_${NAME}_LINK_INTO_TOOLS``, when sets to ``OFF``, turns the project into a dynamically loaded extension. +- ``LLVM_${NAME}_BUILD``, when sets to ``OFF`` deactivate the extension. + + Registering dynamically loaded passes ===================================== diff --git a/llvm/include/llvm/Config/llvm-config.h.cmake b/llvm/include/llvm/Config/llvm-config.h.cmake --- a/llvm/include/llvm/Config/llvm-config.h.cmake +++ b/llvm/include/llvm/Config/llvm-config.h.cmake @@ -17,9 +17,6 @@ /* Define if LLVM_ENABLE_DUMP is enabled */ #cmakedefine LLVM_ENABLE_DUMP -/* Define if we link Polly to the tools */ -#cmakedefine LINK_POLLY_INTO_TOOLS - /* Target triple LLVM will generate code for by default */ #cmakedefine LLVM_DEFAULT_TARGET_TRIPLE "${LLVM_DEFAULT_TARGET_TRIPLE}" diff --git a/llvm/tools/CMakeLists.txt b/llvm/tools/CMakeLists.txt --- a/llvm/tools/CMakeLists.txt +++ b/llvm/tools/CMakeLists.txt @@ -9,14 +9,6 @@ # traversing each directory. create_llvm_tool_options() -# Build polly before the tools: the tools link against polly when -# LINK_POLLY_INTO_TOOLS is set. -if(WITH_POLLY) - add_llvm_external_project(polly) -else() - set(LLVM_TOOL_POLLY_BUILD Off) -endif() - if(NOT LLVM_BUILD_LLVM_DYLIB AND NOT LLVM_BUILD_LLVM_C_DYLIB) set(LLVM_TOOL_LLVM_SHLIB_BUILD Off) endif() @@ -46,6 +38,11 @@ add_llvm_external_project(lld) add_llvm_external_project(lldb) +# projects sensible to extension property (namely clang, opt and bugpoint) +# must be declared before extensions +add_llvm_external_project(opt) +add_llvm_external_project(bugpoint) + # Automatically add remaining sub-directories containing a 'CMakeLists.txt' # file as external projects. add_llvm_implicit_projects() diff --git a/llvm/tools/bugpoint/CMakeLists.txt b/llvm/tools/bugpoint/CMakeLists.txt --- a/llvm/tools/bugpoint/CMakeLists.txt +++ b/llvm/tools/bugpoint/CMakeLists.txt @@ -36,9 +36,4 @@ intrinsics_gen ) export_executable_symbols(bugpoint) - -if(WITH_POLLY AND LINK_POLLY_INTO_TOOLS) - target_link_libraries(bugpoint PRIVATE Polly) - # Ensure LLVMTarget can resolve dependences in Polly. - target_link_libraries(bugpoint PRIVATE LLVMTarget) -endif(WITH_POLLY AND LINK_POLLY_INTO_TOOLS) +target_link_libraries(bugpoint PRIVATE $) diff --git a/llvm/tools/bugpoint/bugpoint.cpp b/llvm/tools/bugpoint/bugpoint.cpp --- a/llvm/tools/bugpoint/bugpoint.cpp +++ b/llvm/tools/bugpoint/bugpoint.cpp @@ -109,11 +109,10 @@ }; } -#ifdef LINK_POLLY_INTO_TOOLS -namespace polly { -void initializePollyPasses(llvm::PassRegistry &Registry); -} -#endif +#define HANDLE_EXTENSION(Ext) \ + void initialize##Ext##Passes(llvm::PassRegistry &Registry); + +#include "llvm/Support/Extension.def" int main(int argc, char **argv) { #ifndef DEBUG_BUGPOINT @@ -134,9 +133,8 @@ initializeInstrumentation(Registry); initializeTarget(Registry); -#ifdef LINK_POLLY_INTO_TOOLS - polly::initializePollyPasses(Registry); -#endif +#define HANDLE_EXTENSION(Ext) initialize##Ext##Passes(Registry); +#include "llvm/Support/Extension.def" if (std::getenv("bar") == (char*) -1) { InitializeAllTargets(); diff --git a/llvm/tools/opt/CMakeLists.txt b/llvm/tools/opt/CMakeLists.txt --- a/llvm/tools/opt/CMakeLists.txt +++ b/llvm/tools/opt/CMakeLists.txt @@ -37,7 +37,4 @@ intrinsics_gen ) export_executable_symbols(opt) - -if(WITH_POLLY AND LINK_POLLY_INTO_TOOLS) - target_link_libraries(opt PRIVATE Polly) -endif(WITH_POLLY AND LINK_POLLY_INTO_TOOLS) +target_link_libraries(opt PRIVATE $) diff --git a/llvm/tools/opt/NewPMDriver.cpp b/llvm/tools/opt/NewPMDriver.cpp --- a/llvm/tools/opt/NewPMDriver.cpp +++ b/llvm/tools/opt/NewPMDriver.cpp @@ -202,11 +202,9 @@ }); } -#ifdef LINK_POLLY_INTO_TOOLS -namespace polly { -void RegisterPollyPasses(PassBuilder &); -} -#endif +#define HANDLE_EXTENSION(Ext) \ + llvm::PassPluginLibraryInfo get##Ext##PluginInfo(); +#include "llvm/Support/Extension.def" bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM, ToolOutputFile *Out, ToolOutputFile *ThinLTOLinkOut, @@ -290,9 +288,9 @@ return false; }); -#ifdef LINK_POLLY_INTO_TOOLS - polly::RegisterPollyPasses(PB); -#endif +#define HANDLE_EXTENSION(Ext) \ + get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB); +#include "llvm/Support/Extension.def" // Specially handle the alias analysis manager so that we can register // a custom pipeline of AA passes with it. diff --git a/llvm/tools/opt/opt.cpp b/llvm/tools/opt/opt.cpp --- a/llvm/tools/opt/opt.cpp +++ b/llvm/tools/opt/opt.cpp @@ -470,11 +470,10 @@ getCodeModel(), GetCodeGenOptLevel()); } -#ifdef LINK_POLLY_INTO_TOOLS -namespace polly { -void initializePollyPasses(llvm::PassRegistry &Registry); -} -#endif +#define HANDLE_EXTENSION(Ext) \ + void initialize##Ext##Passes(llvm::PassRegistry &Registry); + +#include "llvm/Support/Extension.def" //===----------------------------------------------------------------------===// // main for opt @@ -529,9 +528,8 @@ initializeWasmEHPreparePass(Registry); initializeWriteBitcodePassPass(Registry); -#ifdef LINK_POLLY_INTO_TOOLS - polly::initializePollyPasses(Registry); -#endif +#define HANDLE_EXTENSION(Ext) initialize##Ext##Passes(Registry); +#include "llvm/Support/Extension.def" cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .bc modular optimizer and analysis printer\n"); diff --git a/llvm/utils/gn/secondary/llvm/include/llvm/Config/BUILD.gn b/llvm/utils/gn/secondary/llvm/include/llvm/Config/BUILD.gn --- a/llvm/utils/gn/secondary/llvm/include/llvm/Config/BUILD.gn +++ b/llvm/utils/gn/secondary/llvm/include/llvm/Config/BUILD.gn @@ -321,7 +321,6 @@ output = "$target_gen_dir/llvm-config.h" values = [ "LLVM_ENABLE_DUMP=", - "LINK_POLLY_INTO_TOOLS=", "LLVM_DEFAULT_TARGET_TRIPLE=$llvm_target_triple", "LLVM_HAS_ATOMICS=1", "LLVM_HOST_TRIPLE=$llvm_current_triple", diff --git a/polly/CMakeLists.txt b/polly/CMakeLists.txt --- a/polly/CMakeLists.txt +++ b/polly/CMakeLists.txt @@ -162,6 +162,11 @@ add_definitions( -D_GNU_SOURCE ) + +register_llvm_extension(Polly) +# Polly-ACC requires the NVPTX target to be present in the executable it is linked to +set_property(TARGET bugpoint APPEND PROPERTY LLVM_COMPILER_EXTENSIONS LLVMTarget) + add_subdirectory(docs) add_subdirectory(lib) add_subdirectory(test) @@ -207,4 +212,3 @@ add_custom_target(polly-update-format DEPENDS ${update_format_depends}) set_target_properties(polly-update-format PROPERTIES FOLDER "Polly") - diff --git a/polly/lib/Support/RegisterPasses.cpp b/polly/lib/Support/RegisterPasses.cpp --- a/polly/lib/Support/RegisterPasses.cpp +++ b/polly/lib/Support/RegisterPasses.cpp @@ -234,7 +234,6 @@ cl::desc("Bail out on unprofitable SCoPs before rescheduling"), cl::Hidden, cl::init(true), cl::cat(PollyCategory)); -namespace polly { void initializePollyPasses(PassRegistry &Registry) { initializeCodeGenerationPass(Registry); @@ -271,6 +270,7 @@ initializePruneUnprofitablePass(Registry); } +namespace polly { /// Register Polly passes such that they form a polyhedral optimizer. /// /// The individual Polly passes are registered in the pass manager such that @@ -701,9 +701,13 @@ } } // namespace polly -// Plugin Entrypoint: -extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK -llvmGetPassPluginInfo() { +// Pass Plugin Entrypoints +llvm::PassPluginLibraryInfo getPollyPluginInfo() { return {LLVM_PLUGIN_API_VERSION, "Polly", LLVM_VERSION_STRING, polly::RegisterPollyPasses}; } + +extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK +llvmGetPassPluginInfo() { + return getPollyPluginInfo(); +} diff --git a/polly/test/Unit/lit.site.cfg.in b/polly/test/Unit/lit.site.cfg.in --- a/polly/test/Unit/lit.site.cfg.in +++ b/polly/test/Unit/lit.site.cfg.in @@ -13,7 +13,7 @@ config.shlibdir = "@SHLIBDIR@" config.target_triple = "@TARGET_TRIPLE@" config.enable_gpgpu_codegen = "@GPU_CODEGEN@" -config.link_polly_into_tools = "@LINK_POLLY_INTO_TOOLS@" +config.llvm_polly_link_into_tools = "@LLVM_POLLY_LINK_INTO_TOOLS@" config.has_unittests = @POLLY_GTEST_AVAIL@ # Support substitution of the tools_dir, libs_dirs, and build_mode with user diff --git a/polly/test/lit.site.cfg.in b/polly/test/lit.site.cfg.in --- a/polly/test/lit.site.cfg.in +++ b/polly/test/lit.site.cfg.in @@ -8,7 +8,7 @@ config.polly_lib_dir = "@POLLY_LIB_DIR@" config.target_triple = "@TARGET_TRIPLE@" config.enable_gpgpu_codegen = "@GPU_CODEGEN@" -config.link_polly_into_tools = "@LINK_POLLY_INTO_TOOLS@" +config.llvm_polly_link_into_tools = "@LLVM_POLLY_LINK_INTO_TOOLS@" config.targets_to_build = "@TARGETS_TO_BUILD@" config.extra_paths = "@POLLY_TEST_EXTRA_PATHS@".split(";") @@ -36,14 +36,14 @@ # directories. config.excludes = ['Inputs'] -if config.link_polly_into_tools == '' or \ - config.link_polly_into_tools.lower() == '0' or \ - config.link_polly_into_tools.lower() == 'n' or \ - config.link_polly_into_tools.lower() == 'no' or \ - config.link_polly_into_tools.lower() == 'off' or \ - config.link_polly_into_tools.lower() == 'false' or \ - config.link_polly_into_tools.lower() == 'notfound' or \ - config.link_polly_into_tools.lower() == 'link_polly_into_tools-notfound': +if config.llvm_polly_link_into_tools == '' or \ + config.llvm_polly_link_into_tools.lower() == '0' or \ + config.llvm_polly_link_into_tools.lower() == 'n' or \ + config.llvm_polly_link_into_tools.lower() == 'no' or \ + config.llvm_polly_link_into_tools.lower() == 'off' or \ + config.llvm_polly_link_into_tools.lower() == 'false' or \ + config.llvm_polly_link_into_tools.lower() == 'notfound' or \ + config.llvm_polly_link_into_tools.lower() == 'llvm_polly_link_into_tools-notfound': config.substitutions.append(('%loadPolly', '-load ' + config.polly_lib_dir + '/LLVMPolly@LLVM_SHLIBEXT@' + ' -load-pass-plugin ' diff --git a/polly/test/update_check.py b/polly/test/update_check.py --- a/polly/test/update_check.py +++ b/polly/test/update_check.py @@ -15,7 +15,7 @@ polly_lib_dir = '''@POLLY_LIB_DIR@''' shlibext = '''@LLVM_SHLIBEXT@''' llvm_tools_dir = '''@LLVM_TOOLS_DIR@''' -link_polly_into_tools = not '''@LINK_POLLY_INTO_TOOLS@'''.lower() in {'','0','n','no','off','false','notfound','link_polly_into_tools-notfound'} +llvm_polly_link_into_tools = not '''@LLVM_POLLY_LINK_INTO_TOOLS@'''.lower() in {'','0','n','no','off','false','notfound','llvm_polly_link_into_tools-notfound'} runre = re.compile(r'\s*\;\s*RUN\s*\:(?P.*)') filecheckre = re.compile(r'\s*(?P.*)\|\s*(?PFileCheck\s[^|]*)') @@ -298,7 +298,7 @@ toolarg = toolarg.replace('%s', filename) toolarg = toolarg.replace('%S', os.path.dirname(filename)) if toolarg == '%loadPolly': - if not link_polly_into_tools: + if not llvm_polly_link_into_tools: newtool += ['-load',os.path.join(polly_lib_dir,'LLVMPolly' + shlibext)] newtool.append('-polly-process-unprofitable') newtool.append('-polly-remarks-minimal')