[Mlir-commits] [mlir] 223154d - [mlir] Remove need for static global ctors from mlir-translate
Jonathan Roelofs
llvmlistbot at llvm.org
Wed Apr 8 15:52:41 PDT 2020
Author: Jonathan Roelofs
Date: 2020-04-08T16:52:33-06:00
New Revision: 223154d267e2e935d1bbcba77fe222c8ef96e789
URL: https://github.com/llvm/llvm-project/commit/223154d267e2e935d1bbcba77fe222c8ef96e789
DIFF: https://github.com/llvm/llvm-project/commit/223154d267e2e935d1bbcba77fe222c8ef96e789.diff
LOG: [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
Added:
mlir/include/mlir/InitAllTranslations.h
Modified:
mlir/cmake/modules/AddMLIR.cmake
mlir/include/mlir/Translation.h
mlir/lib/Dialect/SPIRV/Serialization/TranslateRegistration.cpp
mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp
mlir/lib/Target/LLVMIR/ConvertToLLVMIR.cpp
mlir/lib/Target/LLVMIR/ConvertToNVVMIR.cpp
mlir/lib/Target/LLVMIR/ConvertToROCDLIR.cpp
mlir/lib/Target/LLVMIR/LLVMAVX512Intr.cpp
mlir/test/EDSC/CMakeLists.txt
mlir/test/SDBM/CMakeLists.txt
mlir/tools/mlir-shlib/CMakeLists.txt
mlir/tools/mlir-translate/CMakeLists.txt
mlir/tools/mlir-translate/mlir-translate.cpp
mlir/unittests/Dialect/SPIRV/CMakeLists.txt
mlir/unittests/SDBM/CMakeLists.txt
Removed:
################################################################################
diff --git a/mlir/cmake/modules/AddMLIR.cmake b/mlir/cmake/modules/AddMLIR.cmake
index 7449f54ea877..2a71b2dc013c 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 000000000000..4952713c3828
--- /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 72cbee8baf58..a7682cae530c 100644
--- a/mlir/include/mlir/Translation.h
+++ b/mlir/include/mlir/Translation.h
@@ -53,14 +53,18 @@ using TranslateFromMLIRFunction =
using TranslateFunction = std::function<LogicalResult(
llvm::SourceMgr &sourceMgr, llvm::raw_ostream &output, MLIRContext *)>;
-/// 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 555bf95f3c95..4850be5ceb48 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 dfe95f3e59d6..5f1ae738280a 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 45afde2f39d3..75bcf38aae04 100644
--- a/mlir/lib/Target/LLVMIR/ConvertToLLVMIR.cpp
+++ b/mlir/lib/Target/LLVMIR/ConvertToLLVMIR.cpp
@@ -25,12 +25,16 @@ std::unique_ptr<llvm::Module> 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 8e194d907479..51686ad73666 100644
--- a/mlir/lib/Target/LLVMIR/ConvertToNVVMIR.cpp
+++ b/mlir/lib/Target/LLVMIR/ConvertToNVVMIR.cpp
@@ -94,12 +94,16 @@ std::unique_ptr<llvm::Module> 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 8e9478b044ca..1b511f4b61f4 100644
--- a/mlir/lib/Target/LLVMIR/ConvertToROCDLIR.cpp
+++ b/mlir/lib/Target/LLVMIR/ConvertToROCDLIR.cpp
@@ -97,12 +97,16 @@ std::unique_ptr<llvm::Module> 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 216ae862d4b2..7335fab84bb9 100644
--- a/mlir/lib/Target/LLVMIR/LLVMAVX512Intr.cpp
+++ b/mlir/lib/Target/LLVMIR/LLVMAVX512Intr.cpp
@@ -40,12 +40,16 @@ std::unique_ptr<llvm::Module> 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 6c2f5f9fd0be..6592f8cccd1d 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 c7ab71e89eec..9e0023750e68 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 e9b2963d8bb8..d0e2e959652f 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 ff813245f7c8..be6840c8ddc2 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 c9240bb6b20c..13fa07e8affd 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<bool> 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 b444b5c0220a..2d9b60addbe7 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 3d832ec15488..d86f9dda3802 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)
More information about the Mlir-commits
mailing list