From 223154d267e2e935d1bbcba77fe222c8ef96e789 Mon Sep 17 00:00:00 2001 From: Jonathan Roelofs Date: Sun, 5 Apr 2020 14:42:16 -0600 Subject: [PATCH] [mlir] Remove need for static global ctors from mlir-translate Summary: https://bugs.llvm.org/show_bug.cgi?id=45436 Reviewers: mehdi_amini, mravishankar, antiagainst, rriddle, stephenneuendorffer Reviewed By: mehdi_amini, rriddle, stephenneuendorffer Subscribers: frgossen, stephenneuendorffer, jholewinski, mgorny, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, Joonsoo, bader, grosul1, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D77515 --- mlir/cmake/modules/AddMLIR.cmake | 27 -------------- mlir/include/mlir/InitAllTranslations.h | 43 ++++++++++++++++++++++ mlir/include/mlir/Translation.h | 10 +++-- .../SPIRV/Serialization/TranslateRegistration.cpp | 43 ++++++++++++++-------- mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp | 13 ++++--- mlir/lib/Target/LLVMIR/ConvertToLLVMIR.cpp | 20 ++++++---- mlir/lib/Target/LLVMIR/ConvertToNVVMIR.cpp | 22 ++++++----- mlir/lib/Target/LLVMIR/ConvertToROCDLIR.cpp | 22 ++++++----- mlir/lib/Target/LLVMIR/LLVMAVX512Intr.cpp | 22 ++++++----- mlir/test/EDSC/CMakeLists.txt | 9 ----- mlir/test/SDBM/CMakeLists.txt | 4 -- mlir/tools/mlir-shlib/CMakeLists.txt | 1 - mlir/tools/mlir-translate/CMakeLists.txt | 8 ---- mlir/tools/mlir-translate/mlir-translate.cpp | 10 +++++ mlir/unittests/Dialect/SPIRV/CMakeLists.txt | 3 -- mlir/unittests/SDBM/CMakeLists.txt | 1 - 16 files changed, 147 insertions(+), 111 deletions(-) create mode 100644 mlir/include/mlir/InitAllTranslations.h diff --git a/mlir/cmake/modules/AddMLIR.cmake b/mlir/cmake/modules/AddMLIR.cmake index 7449f54..2a71b2d 100644 --- a/mlir/cmake/modules/AddMLIR.cmake +++ b/mlir/cmake/modules/AddMLIR.cmake @@ -4,33 +4,6 @@ function(mlir_tablegen ofn) PARENT_SCOPE) endfunction() -# TODO: This is to handle the current static registration, but should be -# factored out a bit. -function(whole_archive_link target) - add_dependencies(${target} ${ARGN}) - if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin") - set(link_flags "-L${CMAKE_BINARY_DIR}/lib ") - FOREACH(LIB ${ARGN}) - if("${CMAKE_GENERATOR}" STREQUAL "Xcode") - string(CONCAT link_flags ${link_flags} "-Wl,-force_load ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib/lib${LIB}.a ") - else() - string(CONCAT link_flags ${link_flags} "-Wl,-force_load ${CMAKE_BINARY_DIR}/lib/lib${LIB}.a ") - endif() - ENDFOREACH(LIB) - elseif(MSVC) - FOREACH(LIB ${ARGN}) - string(CONCAT link_flags ${link_flags} "/WHOLEARCHIVE:${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib/${LIB}.lib ") - ENDFOREACH(LIB) - else() - set(link_flags "-L${CMAKE_BINARY_DIR}/lib -Wl,--whole-archive,") - FOREACH(LIB ${ARGN}) - string(CONCAT link_flags ${link_flags} "-l${LIB},") - ENDFOREACH(LIB) - string(CONCAT link_flags ${link_flags} "--no-whole-archive") - endif() - set_target_properties(${target} PROPERTIES LINK_FLAGS ${link_flags}) -endfunction(whole_archive_link) - # Declare a dialect in the include directory function(add_mlir_dialect dialect dialect_namespace) set(LLVM_TARGET_DEFINITIONS ${dialect}.td) diff --git a/mlir/include/mlir/InitAllTranslations.h b/mlir/include/mlir/InitAllTranslations.h new file mode 100644 index 0000000..4952713c --- /dev/null +++ b/mlir/include/mlir/InitAllTranslations.h @@ -0,0 +1,43 @@ +//===- InitAllTranslations.h - MLIR Translations Registration ---*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// This file defines a helper to trigger the registration of all translations +// in and out of MLIR to the system. +// +//===----------------------------------------------------------------------===// + +#ifndef MLIR_INITALLTRANSLATIONS_H +#define MLIR_INITALLTRANSLATIONS_H + +namespace mlir { + +void registerFromLLVMIRTranslation(); +void registerToLLVMIRTranslation(); +void registerToSPIRVTranslation(); +void registerToNVVMIRTranslation(); +void registerToROCLDIRTranslation(); +void registerAVX512ToLLVMIRTranslation(); + +// This function should be called before creating any MLIRContext if one +// expects all the possible translations to be made available to the context +// automatically. +inline void registerAllTranslations() { + static bool init_once = []() { + registerFromLLVMIRTranslation(); + registerToLLVMIRTranslation(); + registerToSPIRVTranslation(); + registerToNVVMIRTranslation(); + registerToROCLDIRTranslation(); + registerAVX512ToLLVMIRTranslation(); + return true; + }(); + (void)init_once; +} +} // namespace mlir + +#endif // MLIR_INITALLTRANSLATIONS_H diff --git a/mlir/include/mlir/Translation.h b/mlir/include/mlir/Translation.h index 72cbee8..a7682ca 100644 --- a/mlir/include/mlir/Translation.h +++ b/mlir/include/mlir/Translation.h @@ -53,14 +53,18 @@ using TranslateFromMLIRFunction = using TranslateFunction = std::function; -/// Use Translate[ToMLIR|FromMLIR]Registration as a global initializer that +/// Use Translate[ToMLIR|FromMLIR]Registration as an initializer that /// registers a function and associates it with name. This requires that a /// translation has not been registered to a given name. /// /// Usage: /// -/// // At namespace scope. -/// static TranslateToMLIRRegistration Unused(&MySubCommand, [] { ... }); +/// // At file scope. +/// namespace mlir { +/// void registerTRexToMLIRRegistration() { +/// TranslateToMLIRRegistration Unused(&MySubCommand, [] { ... }); +/// } +/// } // namespace mlir /// /// \{ struct TranslateToMLIRRegistration { diff --git a/mlir/lib/Dialect/SPIRV/Serialization/TranslateRegistration.cpp b/mlir/lib/Dialect/SPIRV/Serialization/TranslateRegistration.cpp index 555bf95..4850be5 100644 --- a/mlir/lib/Dialect/SPIRV/Serialization/TranslateRegistration.cpp +++ b/mlir/lib/Dialect/SPIRV/Serialization/TranslateRegistration.cpp @@ -60,12 +60,17 @@ static OwningModuleRef deserializeModule(const llvm::MemoryBuffer *input, return module; } -static TranslateToMLIRRegistration fromBinary( - "deserialize-spirv", [](llvm::SourceMgr &sourceMgr, MLIRContext *context) { - assert(sourceMgr.getNumBuffers() == 1 && "expected one buffer"); - return deserializeModule( - sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID()), context); - }); +namespace mlir { +void registerToSPIRVTranslation() { + TranslateToMLIRRegistration fromBinary( + "deserialize-spirv", + [](llvm::SourceMgr &sourceMgr, MLIRContext *context) { + assert(sourceMgr.getNumBuffers() == 1 && "expected one buffer"); + return deserializeModule( + sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID()), context); + }); +} +} // namespace mlir //===----------------------------------------------------------------------===// // Serialization registration @@ -95,10 +100,14 @@ static LogicalResult serializeModule(ModuleOp module, raw_ostream &output) { return mlir::success(); } -static TranslateFromMLIRRegistration - toBinary("serialize-spirv", [](ModuleOp module, raw_ostream &output) { - return serializeModule(module, output); - }); +namespace mlir { +void registerFromSPIRVTranslation() { + TranslateFromMLIRRegistration toBinary( + "serialize-spirv", [](ModuleOp module, raw_ostream &output) { + return serializeModule(module, output); + }); +} +} // namespace mlir //===----------------------------------------------------------------------===// // Round-trip registration @@ -139,8 +148,12 @@ static LogicalResult roundTripModule(llvm::SourceMgr &sourceMgr, return mlir::success(); } -static TranslateRegistration roundtrip( - "test-spirv-roundtrip", - [](llvm::SourceMgr &sourceMgr, raw_ostream &output, MLIRContext *context) { - return roundTripModule(sourceMgr, output, context); - }); +namespace mlir { +void registerTestRoundtripSPIRV() { + TranslateRegistration roundtrip( + "test-spirv-roundtrip", [](llvm::SourceMgr &sourceMgr, + raw_ostream &output, MLIRContext *context) { + return roundTripModule(sourceMgr, output, context); + }); +} +} // namespace mlir diff --git a/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp b/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp index dfe95f3..5f1ae73 100644 --- a/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp +++ b/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp @@ -934,8 +934,11 @@ OwningModuleRef translateLLVMIRToModule(llvm::SourceMgr &sourceMgr, return translateLLVMIRToModule(std::move(llvmModule), context); } -static TranslateToMLIRRegistration - fromLLVM("import-llvm", - [](llvm::SourceMgr &sourceMgr, MLIRContext *context) { - return translateLLVMIRToModule(sourceMgr, context); - }); +namespace mlir { +void registerFromLLVMIRTranslation() { + TranslateToMLIRRegistration fromLLVM( + "import-llvm", [](llvm::SourceMgr &sourceMgr, MLIRContext *context) { + return ::translateLLVMIRToModule(sourceMgr, context); + }); +} +} // namespace mlir diff --git a/mlir/lib/Target/LLVMIR/ConvertToLLVMIR.cpp b/mlir/lib/Target/LLVMIR/ConvertToLLVMIR.cpp index 45afde2..75bcf38 100644 --- a/mlir/lib/Target/LLVMIR/ConvertToLLVMIR.cpp +++ b/mlir/lib/Target/LLVMIR/ConvertToLLVMIR.cpp @@ -25,12 +25,16 @@ std::unique_ptr mlir::translateModuleToLLVMIR(ModuleOp m) { return LLVM::ModuleTranslation::translateModule<>(m); } -static TranslateFromMLIRRegistration - registration("mlir-to-llvmir", [](ModuleOp module, raw_ostream &output) { - auto llvmModule = LLVM::ModuleTranslation::translateModule<>(module); - if (!llvmModule) - return failure(); +namespace mlir { +void registerToLLVMIRTranslation() { + TranslateFromMLIRRegistration registration( + "mlir-to-llvmir", [](ModuleOp module, raw_ostream &output) { + auto llvmModule = LLVM::ModuleTranslation::translateModule<>(module); + if (!llvmModule) + return failure(); - llvmModule->print(output, nullptr); - return success(); - }); + llvmModule->print(output, nullptr); + return success(); + }); +} +} // namespace mlir diff --git a/mlir/lib/Target/LLVMIR/ConvertToNVVMIR.cpp b/mlir/lib/Target/LLVMIR/ConvertToNVVMIR.cpp index 8e194d9..51686ad 100644 --- a/mlir/lib/Target/LLVMIR/ConvertToNVVMIR.cpp +++ b/mlir/lib/Target/LLVMIR/ConvertToNVVMIR.cpp @@ -94,12 +94,16 @@ std::unique_ptr mlir::translateModuleToNVVMIR(Operation *m) { return llvmModule; } -static TranslateFromMLIRRegistration - registration("mlir-to-nvvmir", [](ModuleOp module, raw_ostream &output) { - auto llvmModule = mlir::translateModuleToNVVMIR(module); - if (!llvmModule) - return failure(); - - llvmModule->print(output, nullptr); - return success(); - }); +namespace mlir { +void registerToNVVMIRTranslation() { + TranslateFromMLIRRegistration registration( + "mlir-to-nvvmir", [](ModuleOp module, raw_ostream &output) { + auto llvmModule = mlir::translateModuleToNVVMIR(module); + if (!llvmModule) + return failure(); + + llvmModule->print(output, nullptr); + return success(); + }); +} +} // namespace mlir diff --git a/mlir/lib/Target/LLVMIR/ConvertToROCDLIR.cpp b/mlir/lib/Target/LLVMIR/ConvertToROCDLIR.cpp index 8e9478b..1b511f4 100644 --- a/mlir/lib/Target/LLVMIR/ConvertToROCDLIR.cpp +++ b/mlir/lib/Target/LLVMIR/ConvertToROCDLIR.cpp @@ -97,12 +97,16 @@ std::unique_ptr mlir::translateModuleToROCDLIR(Operation *m) { return llvmModule; } -static TranslateFromMLIRRegistration - registration("mlir-to-rocdlir", [](ModuleOp module, raw_ostream &output) { - auto llvmModule = mlir::translateModuleToROCDLIR(module); - if (!llvmModule) - return failure(); - - llvmModule->print(output, nullptr); - return success(); - }); +namespace mlir { +void registerToROCLDIRTranslation() { + TranslateFromMLIRRegistration registration( + "mlir-to-rocdlir", [](ModuleOp module, raw_ostream &output) { + auto llvmModule = mlir::translateModuleToROCDLIR(module); + if (!llvmModule) + return failure(); + + llvmModule->print(output, nullptr); + return success(); + }); +} +} // namespace mlir diff --git a/mlir/lib/Target/LLVMIR/LLVMAVX512Intr.cpp b/mlir/lib/Target/LLVMIR/LLVMAVX512Intr.cpp index 216ae86..7335fab 100644 --- a/mlir/lib/Target/LLVMIR/LLVMAVX512Intr.cpp +++ b/mlir/lib/Target/LLVMIR/LLVMAVX512Intr.cpp @@ -40,12 +40,16 @@ std::unique_ptr translateLLVMAVX512ModuleToLLVMIR(Operation *m) { } } // end namespace -static TranslateFromMLIRRegistration - reg("avx512-mlir-to-llvmir", [](ModuleOp module, raw_ostream &output) { - auto llvmModule = translateLLVMAVX512ModuleToLLVMIR(module); - if (!llvmModule) - return failure(); - - llvmModule->print(output, nullptr); - return success(); - }); +namespace mlir { +void registerAVX512ToLLVMIRTranslation() { + TranslateFromMLIRRegistration reg( + "avx512-mlir-to-llvmir", [](ModuleOp module, raw_ostream &output) { + auto llvmModule = translateLLVMAVX512ModuleToLLVMIR(module); + if (!llvmModule) + return failure(); + + llvmModule->print(output, nullptr); + return success(); + }); +} +} // namespace mlir diff --git a/mlir/test/EDSC/CMakeLists.txt b/mlir/test/EDSC/CMakeLists.txt index 6c2f5f9..6592f8c 100644 --- a/mlir/test/EDSC/CMakeLists.txt +++ b/mlir/test/EDSC/CMakeLists.txt @@ -20,12 +20,3 @@ target_link_libraries(mlir-edsc-builder-api-test ) target_include_directories(mlir-edsc-builder-api-test PRIVATE ..) - -whole_archive_link(mlir-edsc-builder-api-test - MLIRAffine - MLIRLinalgOps - MLIRLoopOps - MLIRStandardOps - MLIRVector - MLIRTransforms -) diff --git a/mlir/test/SDBM/CMakeLists.txt b/mlir/test/SDBM/CMakeLists.txt index c7ab71e..9e00237 100644 --- a/mlir/test/SDBM/CMakeLists.txt +++ b/mlir/test/SDBM/CMakeLists.txt @@ -14,7 +14,3 @@ target_link_libraries(mlir-sdbm-api-test ) target_include_directories(mlir-sdbm-api-test PRIVATE ..) - -whole_archive_link(mlir-sdbm-api-test - MLIRSDBM -) diff --git a/mlir/tools/mlir-shlib/CMakeLists.txt b/mlir/tools/mlir-shlib/CMakeLists.txt index e9b2963..d0e2e95 100644 --- a/mlir/tools/mlir-shlib/CMakeLists.txt +++ b/mlir/tools/mlir-shlib/CMakeLists.txt @@ -38,5 +38,4 @@ if(LLVM_BUILD_LLVM_DYLIB) mlir-shlib.cpp ) target_link_libraries(MLIR PRIVATE LLVM ${LLVM_PTHREAD_LIB}) - whole_archive_link(MLIR ${mlir_libs}) endif() diff --git a/mlir/tools/mlir-translate/CMakeLists.txt b/mlir/tools/mlir-translate/CMakeLists.txt index ff81324..be6840c 100644 --- a/mlir/tools/mlir-translate/CMakeLists.txt +++ b/mlir/tools/mlir-translate/CMakeLists.txt @@ -12,16 +12,8 @@ set(LIBS MLIRTranslation MLIRSupport ) -set(FULL_LIBS - MLIRSPIRVSerialization - MLIRTargetAVX512 - MLIRTargetLLVMIR - MLIRTargetNVVMIR - MLIRTargetROCDLIR -) add_llvm_tool(mlir-translate mlir-translate.cpp ) llvm_update_compile_flags(mlir-translate) -whole_archive_link(mlir-translate ${FULL_LIBS}) target_link_libraries(mlir-translate PRIVATE MLIRIR MLIRTranslation ${LIBS} LLVMSupport) diff --git a/mlir/tools/mlir-translate/mlir-translate.cpp b/mlir/tools/mlir-translate/mlir-translate.cpp index c9240bb..13fa07e 100644 --- a/mlir/tools/mlir-translate/mlir-translate.cpp +++ b/mlir/tools/mlir-translate/mlir-translate.cpp @@ -14,6 +14,7 @@ #include "mlir/IR/Diagnostics.h" #include "mlir/IR/MLIRContext.h" #include "mlir/InitAllDialects.h" +#include "mlir/InitAllTranslations.h" #include "mlir/Support/FileUtilities.h" #include "mlir/Support/LogicalResult.h" #include "mlir/Support/ToolUtilities.h" @@ -45,8 +46,17 @@ static llvm::cl::opt verifyDiagnostics( "expected-* lines on the corresponding line"), llvm::cl::init(false)); +namespace mlir { +// Defined in the test directory, no public header. +void registerTestRoundtripSPIRV(); +} // namespace mlir + +static void registerTestTranslations() { registerTestRoundtripSPIRV(); } + int main(int argc, char **argv) { registerAllDialects(); + registerAllTranslations(); + registerTestTranslations(); llvm::InitLLVM y(argc, argv); // Add flags for all the registered translations. diff --git a/mlir/unittests/Dialect/SPIRV/CMakeLists.txt b/mlir/unittests/Dialect/SPIRV/CMakeLists.txt index b444b5c..2d9b60a 100644 --- a/mlir/unittests/Dialect/SPIRV/CMakeLists.txt +++ b/mlir/unittests/Dialect/SPIRV/CMakeLists.txt @@ -6,6 +6,3 @@ target_link_libraries(MLIRSPIRVTests PRIVATE MLIRSPIRV MLIRSPIRVSerialization) - -whole_archive_link(MLIRSPIRVTests MLIRSPIRV) - diff --git a/mlir/unittests/SDBM/CMakeLists.txt b/mlir/unittests/SDBM/CMakeLists.txt index 3d832ec..d86f9dd 100644 --- a/mlir/unittests/SDBM/CMakeLists.txt +++ b/mlir/unittests/SDBM/CMakeLists.txt @@ -5,4 +5,3 @@ target_link_libraries(MLIRSDBMTests PRIVATE MLIRSDBM ) -whole_archive_link(MLIRSDBMTests MLIRSDBM) -- 2.7.4