[mlir] Remove need for static global ctors from mlir-translate
authorJonathan Roelofs <jroelofs@jroelofs.com>
Sun, 5 Apr 2020 20:42:16 +0000 (14:42 -0600)
committerJonathan Roelofs <jroelofs@jroelofs.com>
Wed, 8 Apr 2020 22:52:33 +0000 (16:52 -0600)
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

16 files changed:
mlir/cmake/modules/AddMLIR.cmake
mlir/include/mlir/InitAllTranslations.h [new file with mode: 0644]
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

index 7449f54..2a71b2d 100644 (file)
@@ -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 (file)
index 0000000..4952713
--- /dev/null
@@ -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
index 72cbee8..a7682ca 100644 (file)
@@ -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 {
index 555bf95..4850be5 100644 (file)
@@ -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
index dfe95f3..5f1ae73 100644 (file)
@@ -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
index 45afde2..75bcf38 100644 (file)
@@ -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
index 8e194d9..51686ad 100644 (file)
@@ -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
index 8e9478b..1b511f4 100644 (file)
@@ -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
index 216ae86..7335fab 100644 (file)
@@ -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
index 6c2f5f9..6592f8c 100644 (file)
@@ -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
-)
index c7ab71e..9e00237 100644 (file)
@@ -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
-)
index e9b2963..d0e2e95 100644 (file)
@@ -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()
index ff81324..be6840c 100644 (file)
@@ -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)
index c9240bb..13fa07e 100644 (file)
@@ -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.
index b444b5c..2d9b60a 100644 (file)
@@ -6,6 +6,3 @@ target_link_libraries(MLIRSPIRVTests
   PRIVATE
   MLIRSPIRV
   MLIRSPIRVSerialization)
-
-whole_archive_link(MLIRSPIRVTests MLIRSPIRV)
-
index 3d832ec..d86f9dd 100644 (file)
@@ -5,4 +5,3 @@ target_link_libraries(MLIRSDBMTests
   PRIVATE
   MLIRSDBM
 )
-whole_archive_link(MLIRSDBMTests MLIRSDBM)