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 @@ -11,16 +11,23 @@ // //===----------------------------------------------------------------------===// +#include "Standalone/StandaloneDialect.h" +#include "mlir/IR/BuiltinOps.h" #include "mlir/InitAllTranslations.h" #include "mlir/Support/LogicalResult.h" #include "mlir/Tools/mlir-translate/MlirTranslateMain.h" - -#include "Standalone/StandaloneDialect.h" +#include "mlir/Tools/mlir-translate/Translation.h" int main(int argc, char **argv) { mlir::registerAllTranslations(); // TODO: Register standalone translations here. + mlir::TranslateFromMLIRRegistration withdescription( + "option", "different from option", + [](mlir::ModuleOp op, llvm::raw_ostream &output) { + return mlir::LogicalResult::success(); + }, + [](mlir::DialectRegistry &a) {}); 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 @@ -71,20 +71,21 @@ /// /// \{ struct TranslateToMLIRRegistration { - TranslateToMLIRRegistration(llvm::StringRef name, + TranslateToMLIRRegistration(llvm::StringRef name, llvm::StringRef description, const TranslateSourceMgrToMLIRFunction &function); - TranslateToMLIRRegistration(llvm::StringRef name, + TranslateToMLIRRegistration(llvm::StringRef name, llvm::StringRef description, const TranslateStringRefToMLIRFunction &function); }; struct TranslateFromMLIRRegistration { TranslateFromMLIRRegistration( - llvm::StringRef name, const TranslateFromMLIRFunction &function, + llvm::StringRef name, llvm::StringRef description, + const TranslateFromMLIRFunction &function, const std::function &dialectRegistration = [](DialectRegistry &) {}); }; struct TranslateRegistration { - TranslateRegistration(llvm::StringRef name, + TranslateRegistration(llvm::StringRef name, llvm::StringRef description, const TranslateFunction &function); }; /// \} diff --git a/mlir/lib/Target/Cpp/TranslateRegistration.cpp b/mlir/lib/Target/Cpp/TranslateRegistration.cpp --- a/mlir/lib/Target/Cpp/TranslateRegistration.cpp +++ b/mlir/lib/Target/Cpp/TranslateRegistration.cpp @@ -33,7 +33,7 @@ llvm::cl::init(false)); TranslateFromMLIRRegistration reg( - "mlir-to-cpp", + "mlir-to-cpp", "translate mlir to cpp", [](ModuleOp module, raw_ostream &output) { return emitc::translateToCpp( module, output, 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 @@ -1408,7 +1408,8 @@ namespace mlir { void registerFromLLVMIRTranslation() { TranslateToMLIRRegistration fromLLVM( - "import-llvm", [](llvm::SourceMgr &sourceMgr, MLIRContext *context) { + "import-llvm", "from llvm to mlir", + [](llvm::SourceMgr &sourceMgr, MLIRContext *context) { return ::translateLLVMIRToModule(sourceMgr, context); }); } 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 @@ -24,7 +24,7 @@ namespace mlir { void registerToLLVMIRTranslation() { TranslateFromMLIRRegistration registration( - "mlir-to-llvmir", + "mlir-to-llvmir", "translate mlir to llvmir", [](ModuleOp module, raw_ostream &output) { llvm::LLVMContext llvmContext; auto llvmModule = translateModuleToLLVMIR(module, llvmContext); diff --git a/mlir/lib/Target/SPIRV/TranslateRegistration.cpp b/mlir/lib/Target/SPIRV/TranslateRegistration.cpp --- a/mlir/lib/Target/SPIRV/TranslateRegistration.cpp +++ b/mlir/lib/Target/SPIRV/TranslateRegistration.cpp @@ -67,7 +67,7 @@ namespace mlir { void registerFromSPIRVTranslation() { TranslateToMLIRRegistration fromBinary( - "deserialize-spirv", + "deserialize-spirv", "deserializes the SPIR-V module", [](llvm::SourceMgr &sourceMgr, MLIRContext *context) { assert(sourceMgr.getNumBuffers() == 1 && "expected one buffer"); return deserializeModule( @@ -107,7 +107,7 @@ namespace mlir { void registerToSPIRVTranslation() { TranslateFromMLIRRegistration toBinary( - "serialize-spirv", + "serialize-spirv", "serialize SPIR-V dialect", [](ModuleOp module, raw_ostream &output) { return serializeModule(module, output); }, @@ -162,7 +162,7 @@ namespace mlir { void registerTestRoundtripSPIRV() { TranslateFromMLIRRegistration roundtrip( - "test-spirv-roundtrip", + "test-spirv-roundtrip", "test roundtrip in SPIR-V dialect", [](ModuleOp module, raw_ostream &output) { return roundTripModule(module, /*emitDebugInfo=*/false, output); }, @@ -173,7 +173,7 @@ void registerTestRoundtripDebugSPIRV() { TranslateFromMLIRRegistration roundtrip( - "test-spirv-roundtrip-debug", + "test-spirv-roundtrip-debug", "test roundtrip debug in SPIR-V", [](ModuleOp module, raw_ostream &output) { return roundTripModule(module, /*emitDebugInfo=*/true, output); }, 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 @@ -24,15 +24,20 @@ // Translation Registry //===----------------------------------------------------------------------===// +struct TranslationBundle { + TranslateFunction translateFunction; + StringRef translateDescription; +}; + /// 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; +/// and TranslateFunctions with its description that perform those translations. +static llvm::StringMap &getTranslationRegistry() { + static llvm::StringMap translationBundle; + return translationBundle; } /// Register the given translation. -static void registerTranslation(StringRef name, +static void registerTranslation(StringRef name, StringRef description, const TranslateFunction &function) { auto &translationRegistry = getTranslationRegistry(); if (translationRegistry.find(name) != translationRegistry.end()) @@ -40,12 +45,13 @@ "Attempting to overwrite an existing function"); assert(function && "Attempting to register an empty translate function"); - translationRegistry[name] = function; + translationRegistry[name].translateFunction = function; + translationRegistry[name].translateDescription = description; } TranslateRegistration::TranslateRegistration( - StringRef name, const TranslateFunction &function) { - registerTranslation(name, function); + StringRef name, StringRef description, const TranslateFunction &function) { + registerTranslation(name, description, function); } //===----------------------------------------------------------------------===// @@ -55,7 +61,8 @@ // 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) { + StringRef name, StringRef description, + const TranslateSourceMgrToMLIRFunction &function) { auto wrappedFn = [function](llvm::SourceMgr &sourceMgr, raw_ostream &output, MLIRContext *context) { OwningOpRef module = function(sourceMgr, context); @@ -64,20 +71,22 @@ module->print(output); return success(); }; - registerTranslation(name, wrappedFn); + registerTranslation(name, description, wrappedFn); } TranslateToMLIRRegistration::TranslateToMLIRRegistration( - StringRef name, const TranslateSourceMgrToMLIRFunction &function) { - registerTranslateToMLIRFunction(name, function); + StringRef name, StringRef description, + const TranslateSourceMgrToMLIRFunction &function) { + registerTranslateToMLIRFunction(name, description, 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) { + StringRef name, StringRef description, + const TranslateStringRefToMLIRFunction &function) { registerTranslateToMLIRFunction( - name, [function](llvm::SourceMgr &sourceMgr, MLIRContext *ctx) { + name, description, + [function](llvm::SourceMgr &sourceMgr, MLIRContext *ctx) { const llvm::MemoryBuffer *buffer = sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID()); return function(buffer->getBuffer(), ctx); @@ -89,19 +98,22 @@ //===----------------------------------------------------------------------===// TranslateFromMLIRRegistration::TranslateFromMLIRRegistration( - StringRef name, const TranslateFromMLIRFunction &function, + StringRef name, StringRef description, + 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); - }); + registerTranslation(name, description, + [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); + }); } //===----------------------------------------------------------------------===// @@ -110,8 +122,10 @@ TranslationParser::TranslationParser(llvm::cl::Option &opt) : llvm::cl::parser(opt) { - for (const auto &kv : getTranslationRegistry()) - addLiteralOption(kv.first(), &kv.second, kv.first()); + for (const auto &kv : getTranslationRegistry()) { + addLiteralOption(kv.first(), &kv.second.translateFunction, + kv.second.translateDescription); + } } void TranslationParser::printOptionInfo(const llvm::cl::Option &o,