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)
--- /dev/null
+//===- 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
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 {
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
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
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
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
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
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
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
}
} // 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
)
target_include_directories(mlir-edsc-builder-api-test PRIVATE ..)
-
-whole_archive_link(mlir-edsc-builder-api-test
- MLIRAffine
- MLIRLinalgOps
- MLIRLoopOps
- MLIRStandardOps
- MLIRVector
- MLIRTransforms
-)
)
target_include_directories(mlir-sdbm-api-test PRIVATE ..)
-
-whole_archive_link(mlir-sdbm-api-test
- MLIRSDBM
-)
mlir-shlib.cpp
)
target_link_libraries(MLIR PRIVATE LLVM ${LLVM_PTHREAD_LIB})
- whole_archive_link(MLIR ${mlir_libs})
endif()
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)
#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"
"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.
PRIVATE
MLIRSPIRV
MLIRSPIRVSerialization)
-
-whole_archive_link(MLIRSPIRVTests MLIRSPIRV)
-
PRIVATE
MLIRSDBM
)
-whole_archive_link(MLIRSDBMTests MLIRSDBM)