diff --git a/mlir/examples/standalone/standalone-translate/standalone-translate.cpp b/mlir/examples/standalone/standalone-translate/standalone-translate.cpp --- a/mlir/examples/standalone/standalone-translate/standalone-translate.cpp +++ b/mlir/examples/standalone/standalone-translate/standalone-translate.cpp @@ -1,27 +1,27 @@ -//===- standalone-translate.cpp ---------------------------------*- C++ -*-===// -// -// This file is licensed 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 is a command line utility that translates a file from/to MLIR using one -// of the registered translations. -// -//===----------------------------------------------------------------------===// - -#include "mlir/InitAllTranslations.h" -#include "mlir/Support/LogicalResult.h" -#include "mlir/Tools/mlir-translate/MlirTranslateMain.h" - -#include "Standalone/StandaloneDialect.h" - -int main(int argc, char **argv) { - mlir::registerAllTranslations(); - - // TODO: Register standalone translations here. - - return failed( - mlir::mlirTranslateMain(argc, argv, "MLIR Translation Testing Tool")); -} +//===- standalone-translate.cpp ---------------------------------*- C++ -*-===// +// +// This file is licensed 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 is a command line utility that translates a file from/to MLIR using one +// of the registered translations. +// +//===----------------------------------------------------------------------===// + +#include "mlir/InitAllTranslations.h" +#include "mlir/Support/LogicalResult.h" +#include "mlir/Tools/mlir-translate/MlirTranslateMain.h" + +#include "Standalone/StandaloneDialect.h" + +int main(int argc, char **argv) { + mlir::registerAllTranslations(); + + // TODO: Register standalone translations here. + + return failed( + mlir::mlirTranslateMain(argc, argv, "MLIR Translation Testing Tool")); +} diff --git a/mlir/include/mlir/Tools/mlir-translate/Translation.h b/mlir/include/mlir/Tools/mlir-translate/Translation.h --- a/mlir/include/mlir/Tools/mlir-translate/Translation.h +++ b/mlir/include/mlir/Tools/mlir-translate/Translation.h @@ -1,102 +1,102 @@ -//===- Translation.h - Translation registry ---------------------*- 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 -// -//===----------------------------------------------------------------------===// -// -// Registry for user-provided translations. -// -//===----------------------------------------------------------------------===// - -#ifndef MLIR_TOOLS_MLIRTRANSLATE_TRANSLATION_H -#define MLIR_TOOLS_MLIRTRANSLATE_TRANSLATION_H - -#include "llvm/Support/CommandLine.h" - -namespace llvm { -class MemoryBuffer; -class SourceMgr; -class StringRef; -} // namespace llvm - -namespace mlir { -class DialectRegistry; -struct LogicalResult; -class MLIRContext; -class ModuleOp; -template -class OwningOpRef; - -/// Interface of the function that translates the sources managed by `sourceMgr` -/// to MLIR. The source manager has at least one buffer. The implementation -/// should create a new MLIR ModuleOp in the given context and return a pointer -/// to it, or a nullptr in case of any error. -using TranslateSourceMgrToMLIRFunction = std::function( - llvm::SourceMgr &sourceMgr, MLIRContext *)>; - -/// Interface of the function that translates the given string to MLIR. The -/// implementation should create a new MLIR ModuleOp in the given context. If -/// source-related error reporting is required from within the function, use -/// TranslateSourceMgrToMLIRFunction instead. -using TranslateStringRefToMLIRFunction = - std::function(llvm::StringRef, MLIRContext *)>; - -/// Interface of the function that translates MLIR to a different format and -/// outputs the result to a stream. It is allowed to modify the module. -using TranslateFromMLIRFunction = - std::function; - -/// Interface of the function that performs file-to-file translation involving -/// MLIR. The input file is held in the given MemoryBuffer; the output file -/// should be written to the given raw_ostream. The implementation should create -/// all MLIR constructs needed during the process inside the given context. This -/// can be used for round-tripping external formats through the MLIR system. -using TranslateFunction = std::function; - -/// 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 file scope. -/// namespace mlir { -/// void registerTRexToMLIRRegistration() { -/// TranslateToMLIRRegistration Unused(&MySubCommand, [] { ... }); -/// } -/// } // namespace mlir -/// -/// \{ -struct TranslateToMLIRRegistration { - TranslateToMLIRRegistration(llvm::StringRef name, - const TranslateSourceMgrToMLIRFunction &function); - TranslateToMLIRRegistration(llvm::StringRef name, - const TranslateStringRefToMLIRFunction &function); -}; - -struct TranslateFromMLIRRegistration { - TranslateFromMLIRRegistration( - llvm::StringRef name, const TranslateFromMLIRFunction &function, - const std::function &dialectRegistration = - [](DialectRegistry &) {}); -}; -struct TranslateRegistration { - TranslateRegistration(llvm::StringRef name, - const TranslateFunction &function); -}; -/// \} - -/// A command line parser for translation functions. -struct TranslationParser : public llvm::cl::parser { - TranslationParser(llvm::cl::Option &opt); - - void printOptionInfo(const llvm::cl::Option &o, - size_t globalWidth) const override; -}; - -} // namespace mlir - -#endif // MLIR_TOOLS_MLIRTRANSLATE_TRANSLATION_H +//===- Translation.h - Translation registry ---------------------*- 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 +// +//===----------------------------------------------------------------------===// +// +// Registry for user-provided translations. +// +//===----------------------------------------------------------------------===// + +#ifndef MLIR_TOOLS_MLIRTRANSLATE_TRANSLATION_H +#define MLIR_TOOLS_MLIRTRANSLATE_TRANSLATION_H + +#include "llvm/Support/CommandLine.h" + +namespace llvm { +class MemoryBuffer; +class SourceMgr; +class StringRef; +} // namespace llvm + +namespace mlir { +class DialectRegistry; +struct LogicalResult; +class MLIRContext; +class ModuleOp; +template +class OwningOpRef; + +/// Interface of the function that translates the sources managed by `sourceMgr` +/// to MLIR. The source manager has at least one buffer. The implementation +/// should create a new MLIR ModuleOp in the given context and return a pointer +/// to it, or a nullptr in case of any error. +using TranslateSourceMgrToMLIRFunction = std::function( + llvm::SourceMgr &sourceMgr, MLIRContext *)>; + +/// Interface of the function that translates the given string to MLIR. The +/// implementation should create a new MLIR ModuleOp in the given context. If +/// source-related error reporting is required from within the function, use +/// TranslateSourceMgrToMLIRFunction instead. +using TranslateStringRefToMLIRFunction = + std::function(llvm::StringRef, MLIRContext *)>; + +/// Interface of the function that translates MLIR to a different format and +/// outputs the result to a stream. It is allowed to modify the module. +using TranslateFromMLIRFunction = + std::function; + +/// Interface of the function that performs file-to-file translation involving +/// MLIR. The input file is held in the given MemoryBuffer; the output file +/// should be written to the given raw_ostream. The implementation should create +/// all MLIR constructs needed during the process inside the given context. This +/// can be used for round-tripping external formats through the MLIR system. +using TranslateFunction = std::function; + +/// 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 file scope. +/// namespace mlir { +/// void registerTRexToMLIRRegistration() { +/// TranslateToMLIRRegistration Unused(&MySubCommand, [] { ... }); +/// } +/// } // namespace mlir +/// +/// \{ +struct TranslateToMLIRRegistration { + TranslateToMLIRRegistration(llvm::StringRef name, + const TranslateSourceMgrToMLIRFunction &function); + TranslateToMLIRRegistration(llvm::StringRef name, + const TranslateStringRefToMLIRFunction &function); +}; + +struct TranslateFromMLIRRegistration { + TranslateFromMLIRRegistration( + llvm::StringRef name, const TranslateFromMLIRFunction &function, + const std::function &dialectRegistration = + [](DialectRegistry &) {}); +}; +struct TranslateRegistration { + TranslateRegistration(llvm::StringRef name, + const TranslateFunction &function); +}; +/// \} + +/// A command line parser for translation functions. +struct TranslationParser : public llvm::cl::parser { + TranslationParser(llvm::cl::Option &opt); + + void printOptionInfo(const llvm::cl::Option &o, + size_t globalWidth) const override; +}; + +} // namespace mlir + +#endif // MLIR_TOOLS_MLIRTRANSLATE_TRANSLATION_H diff --git a/mlir/lib/Tools/mlir-translate/Translation.cpp b/mlir/lib/Tools/mlir-translate/Translation.cpp --- a/mlir/lib/Tools/mlir-translate/Translation.cpp +++ b/mlir/lib/Tools/mlir-translate/Translation.cpp @@ -1,126 +1,126 @@ -//===- Translation.cpp - Translation registry -----------------------------===// -// -// 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 -// -//===----------------------------------------------------------------------===// -// -// Definitions of the translation registry. -// -//===----------------------------------------------------------------------===// - -#include "mlir/Tools/mlir-translate/Translation.h" -#include "mlir/IR/AsmState.h" -#include "mlir/IR/BuiltinOps.h" -#include "mlir/IR/Dialect.h" -#include "mlir/IR/Verifier.h" -#include "mlir/Parser/Parser.h" -#include "llvm/Support/SourceMgr.h" - -using namespace mlir; - -//===----------------------------------------------------------------------===// -// Translation Registry -//===----------------------------------------------------------------------===// - -/// Get the mutable static map between registered file-to-file MLIR translations -/// and the TranslateFunctions that perform those translations. -static llvm::StringMap &getTranslationRegistry() { - static llvm::StringMap translationRegistry; - return translationRegistry; -} - -/// Register the given translation. -static void registerTranslation(StringRef name, - const TranslateFunction &function) { - auto &translationRegistry = getTranslationRegistry(); - if (translationRegistry.find(name) != translationRegistry.end()) - llvm::report_fatal_error( - "Attempting to overwrite an existing function"); - assert(function && - "Attempting to register an empty translate function"); - translationRegistry[name] = function; -} - -TranslateRegistration::TranslateRegistration( - StringRef name, const TranslateFunction &function) { - registerTranslation(name, function); -} - -//===----------------------------------------------------------------------===// -// Translation to MLIR -//===----------------------------------------------------------------------===// - -// Puts `function` into the to-MLIR translation registry unless there is already -// a function registered for the same name. -static void registerTranslateToMLIRFunction( - StringRef name, const TranslateSourceMgrToMLIRFunction &function) { - auto wrappedFn = [function](llvm::SourceMgr &sourceMgr, raw_ostream &output, - MLIRContext *context) { - OwningOpRef module = function(sourceMgr, context); - if (!module || failed(verify(*module))) - return failure(); - module->print(output); - return success(); - }; - registerTranslation(name, wrappedFn); -} - -TranslateToMLIRRegistration::TranslateToMLIRRegistration( - StringRef name, const TranslateSourceMgrToMLIRFunction &function) { - registerTranslateToMLIRFunction(name, function); -} - -/// Wraps `function` with a lambda that extracts a StringRef from a source -/// manager and registers the wrapper lambda as a to-MLIR conversion. -TranslateToMLIRRegistration::TranslateToMLIRRegistration( - StringRef name, const TranslateStringRefToMLIRFunction &function) { - registerTranslateToMLIRFunction( - name, [function](llvm::SourceMgr &sourceMgr, MLIRContext *ctx) { - const llvm::MemoryBuffer *buffer = - sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID()); - return function(buffer->getBuffer(), ctx); - }); -} - -//===----------------------------------------------------------------------===// -// Translation from MLIR -//===----------------------------------------------------------------------===// - -TranslateFromMLIRRegistration::TranslateFromMLIRRegistration( - StringRef name, const TranslateFromMLIRFunction &function, - const std::function &dialectRegistration) { - registerTranslation(name, [function, dialectRegistration]( - llvm::SourceMgr &sourceMgr, raw_ostream &output, - MLIRContext *context) { - DialectRegistry registry; - dialectRegistration(registry); - context->appendDialectRegistry(registry); - auto module = parseSourceFile(sourceMgr, context); - if (!module || failed(verify(*module))) - return failure(); - return function(module.get(), output); - }); -} - -//===----------------------------------------------------------------------===// -// Translation Parser -//===----------------------------------------------------------------------===// - -TranslationParser::TranslationParser(llvm::cl::Option &opt) - : llvm::cl::parser(opt) { - for (const auto &kv : getTranslationRegistry()) - addLiteralOption(kv.first(), &kv.second, kv.first()); -} - -void TranslationParser::printOptionInfo(const llvm::cl::Option &o, - size_t globalWidth) const { - TranslationParser *tp = const_cast(this); - llvm::array_pod_sort(tp->Values.begin(), tp->Values.end(), - [](const TranslationParser::OptionInfo *lhs, - const TranslationParser::OptionInfo *rhs) { - return lhs->Name.compare(rhs->Name); - }); - llvm::cl::parser::printOptionInfo(o, globalWidth); -} +//===- Translation.cpp - Translation registry -----------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// Definitions of the translation registry. +// +//===----------------------------------------------------------------------===// + +#include "mlir/Tools/mlir-translate/Translation.h" +#include "mlir/IR/AsmState.h" +#include "mlir/IR/BuiltinOps.h" +#include "mlir/IR/Dialect.h" +#include "mlir/IR/Verifier.h" +#include "mlir/Parser/Parser.h" +#include "llvm/Support/SourceMgr.h" + +using namespace mlir; + +//===----------------------------------------------------------------------===// +// Translation Registry +//===----------------------------------------------------------------------===// + +/// Get the mutable static map between registered file-to-file MLIR translations +/// and the TranslateFunctions that perform those translations. +static llvm::StringMap &getTranslationRegistry() { + static llvm::StringMap translationRegistry; + return translationRegistry; +} + +/// Register the given translation. +static void registerTranslation(StringRef name, + const TranslateFunction &function) { + auto &translationRegistry = getTranslationRegistry(); + if (translationRegistry.find(name) != translationRegistry.end()) + llvm::report_fatal_error( + "Attempting to overwrite an existing function"); + assert(function && + "Attempting to register an empty translate function"); + translationRegistry[name] = function; +} + +TranslateRegistration::TranslateRegistration( + StringRef name, const TranslateFunction &function) { + registerTranslation(name, function); +} + +//===----------------------------------------------------------------------===// +// Translation to MLIR +//===----------------------------------------------------------------------===// + +// Puts `function` into the to-MLIR translation registry unless there is already +// a function registered for the same name. +static void registerTranslateToMLIRFunction( + StringRef name, const TranslateSourceMgrToMLIRFunction &function) { + auto wrappedFn = [function](llvm::SourceMgr &sourceMgr, raw_ostream &output, + MLIRContext *context) { + OwningOpRef module = function(sourceMgr, context); + if (!module || failed(verify(*module))) + return failure(); + module->print(output); + return success(); + }; + registerTranslation(name, wrappedFn); +} + +TranslateToMLIRRegistration::TranslateToMLIRRegistration( + StringRef name, const TranslateSourceMgrToMLIRFunction &function) { + registerTranslateToMLIRFunction(name, function); +} + +/// Wraps `function` with a lambda that extracts a StringRef from a source +/// manager and registers the wrapper lambda as a to-MLIR conversion. +TranslateToMLIRRegistration::TranslateToMLIRRegistration( + StringRef name, const TranslateStringRefToMLIRFunction &function) { + registerTranslateToMLIRFunction( + name, [function](llvm::SourceMgr &sourceMgr, MLIRContext *ctx) { + const llvm::MemoryBuffer *buffer = + sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID()); + return function(buffer->getBuffer(), ctx); + }); +} + +//===----------------------------------------------------------------------===// +// Translation from MLIR +//===----------------------------------------------------------------------===// + +TranslateFromMLIRRegistration::TranslateFromMLIRRegistration( + StringRef name, const TranslateFromMLIRFunction &function, + const std::function &dialectRegistration) { + registerTranslation(name, [function, dialectRegistration]( + llvm::SourceMgr &sourceMgr, raw_ostream &output, + MLIRContext *context) { + DialectRegistry registry; + dialectRegistration(registry); + context->appendDialectRegistry(registry); + auto module = parseSourceFile(sourceMgr, context); + if (!module || failed(verify(*module))) + return failure(); + return function(module.get(), output); + }); +} + +//===----------------------------------------------------------------------===// +// Translation Parser +//===----------------------------------------------------------------------===// + +TranslationParser::TranslationParser(llvm::cl::Option &opt) + : llvm::cl::parser(opt) { + for (const auto &kv : getTranslationRegistry()) + addLiteralOption(kv.first(), &kv.second, kv.first()); +} + +void TranslationParser::printOptionInfo(const llvm::cl::Option &o, + size_t globalWidth) const { + TranslationParser *tp = const_cast(this); + llvm::array_pod_sort(tp->Values.begin(), tp->Values.end(), + [](const TranslationParser::OptionInfo *lhs, + const TranslationParser::OptionInfo *rhs) { + return lhs->Name.compare(rhs->Name); + }); + llvm::cl::parser::printOptionInfo(o, globalWidth); +}