diff --git a/mlir/cmake/modules/AddMLIR.cmake b/mlir/cmake/modules/AddMLIR.cmake --- a/mlir/cmake/modules/AddMLIR.cmake +++ b/mlir/cmake/modules/AddMLIR.cmake @@ -4,33 +4,6 @@ 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 --- /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 --- a/mlir/include/mlir/Translation.h +++ b/mlir/include/mlir/Translation.h @@ -53,14 +53,18 @@ 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 --- a/mlir/lib/Dialect/SPIRV/Serialization/TranslateRegistration.cpp +++ b/mlir/lib/Dialect/SPIRV/Serialization/TranslateRegistration.cpp @@ -60,12 +60,17 @@ 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 @@ 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 @@ 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 --- a/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp +++ b/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp @@ -929,8 +929,11 @@ 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 --- a/mlir/lib/Target/LLVMIR/ConvertToLLVMIR.cpp +++ b/mlir/lib/Target/LLVMIR/ConvertToLLVMIR.cpp @@ -25,12 +25,16 @@ 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 --- a/mlir/lib/Target/LLVMIR/ConvertToNVVMIR.cpp +++ b/mlir/lib/Target/LLVMIR/ConvertToNVVMIR.cpp @@ -94,12 +94,16 @@ 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 --- a/mlir/lib/Target/LLVMIR/ConvertToROCDLIR.cpp +++ b/mlir/lib/Target/LLVMIR/ConvertToROCDLIR.cpp @@ -97,12 +97,16 @@ 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 --- a/mlir/lib/Target/LLVMIR/LLVMAVX512Intr.cpp +++ b/mlir/lib/Target/LLVMIR/LLVMAVX512Intr.cpp @@ -40,12 +40,16 @@ } } // 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 --- a/mlir/test/EDSC/CMakeLists.txt +++ b/mlir/test/EDSC/CMakeLists.txt @@ -20,12 +20,3 @@ ) 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 --- a/mlir/test/SDBM/CMakeLists.txt +++ b/mlir/test/SDBM/CMakeLists.txt @@ -14,7 +14,3 @@ ) 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 --- a/mlir/tools/mlir-shlib/CMakeLists.txt +++ b/mlir/tools/mlir-shlib/CMakeLists.txt @@ -38,5 +38,4 @@ 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 --- a/mlir/tools/mlir-translate/CMakeLists.txt +++ b/mlir/tools/mlir-translate/CMakeLists.txt @@ -12,16 +12,8 @@ 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 --- 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 @@ "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 --- a/mlir/unittests/Dialect/SPIRV/CMakeLists.txt +++ b/mlir/unittests/Dialect/SPIRV/CMakeLists.txt @@ -6,6 +6,3 @@ PRIVATE MLIRSPIRV MLIRSPIRVSerialization) - -whole_archive_link(MLIRSPIRVTests MLIRSPIRV) - diff --git a/mlir/unittests/SDBM/CMakeLists.txt b/mlir/unittests/SDBM/CMakeLists.txt --- a/mlir/unittests/SDBM/CMakeLists.txt +++ b/mlir/unittests/SDBM/CMakeLists.txt @@ -5,4 +5,3 @@ PRIVATE MLIRSDBM ) -whole_archive_link(MLIRSDBMTests MLIRSDBM)