Skip to content

Commit

Permalink
[mlir][mlir-translation] patch for standalone-translation command lin…
Browse files Browse the repository at this point in the history
…e description missing.

Differential Revision: https://reviews.llvm.org/D134696

	modified:   mlir/examples/standalone/standalone-translate/standalone-translate.cpp
	modified:   mlir/include/mlir/Tools/mlir-translate/Translation.h
	modified:   mlir/lib/Target/Cpp/TranslateRegistration.cpp
	modified:   mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp
	modified:   mlir/lib/Target/LLVMIR/ConvertToLLVMIR.cpp
	modified:   mlir/lib/Target/SPIRV/TranslateRegistration.cpp
	modified:   mlir/lib/Tools/mlir-translate/Translation.cpp
  • Loading branch information
changkaiyan committed Oct 4, 2022
1 parent 852a4bd commit c4cc755
Show file tree
Hide file tree
Showing 8 changed files with 2,115 additions and 42 deletions.
2,050 changes: 2,050 additions & 0 deletions 202209301111.patch

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
9 changes: 5 additions & 4 deletions mlir/include/mlir/Tools/mlir-translate/Translation.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,21 @@ using TranslateFunction = std::function<LogicalResult(
///
/// \{
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<void(DialectRegistry &)> &dialectRegistration =
[](DialectRegistry &) {});
};
struct TranslateRegistration {
TranslateRegistration(llvm::StringRef name,
TranslateRegistration(llvm::StringRef name, llvm::StringRef description,
const TranslateFunction &function);
};
/// \}
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Target/Cpp/TranslateRegistration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void registerToCppTranslation() {
llvm::cl::init(false));

TranslateFromMLIRRegistration reg(
"mlir-to-cpp",
"mlir-to-cpp", "translate from mlir to cpp",
[](ModuleOp module, raw_ostream &output) {
return emitc::translateToCpp(
module, output,
Expand Down
3 changes: 2 additions & 1 deletion mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1402,7 +1402,8 @@ OwningOpRef<ModuleOp> translateLLVMIRToModule(llvm::SourceMgr &sourceMgr,
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);
});
}
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Target/LLVMIR/ConvertToLLVMIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ using namespace mlir;
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);
Expand Down
8 changes: 4 additions & 4 deletions mlir/lib/Target/SPIRV/TranslateRegistration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static OwningOpRef<ModuleOp> deserializeModule(const llvm::MemoryBuffer *input,
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(
Expand Down Expand Up @@ -107,7 +107,7 @@ static LogicalResult serializeModule(ModuleOp module, raw_ostream &output) {
namespace mlir {
void registerToSPIRVTranslation() {
TranslateFromMLIRRegistration toBinary(
"serialize-spirv",
"serialize-spirv", "serialize SPIR-V dialect",
[](ModuleOp module, raw_ostream &output) {
return serializeModule(module, output);
},
Expand Down Expand Up @@ -162,7 +162,7 @@ static LogicalResult roundTripModule(ModuleOp srcModule, bool emitDebugInfo,
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);
},
Expand All @@ -173,7 +173,7 @@ void registerTestRoundtripSPIRV() {

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);
},
Expand Down
72 changes: 43 additions & 29 deletions mlir/lib/Tools/mlir-translate/Translation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,34 @@ using namespace mlir;
// 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<TranslateFunction> &getTranslationRegistry() {
static llvm::StringMap<TranslateFunction> translationRegistry;
return translationRegistry;
/// and TranslateFunctions with its description that perform those translations.
static llvm::StringMap<TranslationBundle> &getTranslationRegistry() {
static llvm::StringMap<TranslationBundle> 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())
llvm::report_fatal_error(
"Attempting to overwrite an existing <file-to-file> function");
assert(function &&
"Attempting to register an empty translate <file-to-file> 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);
}

//===----------------------------------------------------------------------===//
Expand All @@ -55,7 +61,8 @@ TranslateRegistration::TranslateRegistration(
// 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<ModuleOp> module = function(sourceMgr, context);
Expand All @@ -64,20 +71,22 @@ static void registerTranslateToMLIRFunction(
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);
Expand All @@ -89,19 +98,22 @@ TranslateToMLIRRegistration::TranslateToMLIRRegistration(
//===----------------------------------------------------------------------===//

TranslateFromMLIRRegistration::TranslateFromMLIRRegistration(
StringRef name, const TranslateFromMLIRFunction &function,
StringRef name, StringRef description,
const TranslateFromMLIRFunction &function,
const std::function<void(DialectRegistry &)> &dialectRegistration) {
registerTranslation(name, [function, dialectRegistration](
llvm::SourceMgr &sourceMgr, raw_ostream &output,
MLIRContext *context) {
DialectRegistry registry;
dialectRegistration(registry);
context->appendDialectRegistry(registry);
auto module = parseSourceFile<ModuleOp>(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<ModuleOp>(sourceMgr, context);
if (!module || failed(verify(*module)))
return failure();
return function(module.get(), output);
});
}

//===----------------------------------------------------------------------===//
Expand All @@ -110,8 +122,10 @@ TranslateFromMLIRRegistration::TranslateFromMLIRRegistration(

TranslationParser::TranslationParser(llvm::cl::Option &opt)
: llvm::cl::parser<const TranslateFunction *>(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,
Expand Down

0 comments on commit c4cc755

Please sign in to comment.