2019-05-15 06:03:48 +08:00
|
|
|
//===- Translation.cpp - Translation registry -----------------------------===//
|
2018-11-21 22:21:19 +08:00
|
|
|
//
|
2020-01-26 11:58:30 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-24 01:35:36 +08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-11-21 22:21:19 +08:00
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-11-21 22:21:19 +08:00
|
|
|
//
|
|
|
|
// Definitions of the translation registry.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "mlir/Translation.h"
|
2020-08-23 08:57:39 +08:00
|
|
|
#include "mlir/IR/AsmState.h"
|
2019-07-08 23:04:40 +08:00
|
|
|
#include "mlir/IR/Module.h"
|
2020-05-01 04:09:13 +08:00
|
|
|
#include "mlir/IR/Verifier.h"
|
2020-04-06 07:21:07 +08:00
|
|
|
#include "mlir/Parser.h"
|
2020-08-23 08:57:39 +08:00
|
|
|
#include "mlir/Support/FileUtilities.h"
|
|
|
|
#include "mlir/Support/ToolUtilities.h"
|
|
|
|
#include "llvm/Support/InitLLVM.h"
|
2019-11-08 03:42:11 +08:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2020-08-23 08:57:39 +08:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2018-11-21 22:21:19 +08:00
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
|
2020-04-06 07:21:07 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Translation Registry
|
|
|
|
//===----------------------------------------------------------------------===//
|
2018-11-21 22:22:17 +08:00
|
|
|
|
2020-04-06 07:21:07 +08:00
|
|
|
/// Get the mutable static map between registered file-to-file MLIR translations
|
|
|
|
/// and the TranslateFunctions that perform those translations.
|
|
|
|
static llvm::StringMap<TranslateFunction> &getTranslationRegistry() {
|
2019-09-18 04:03:48 +08:00
|
|
|
static llvm::StringMap<TranslateFunction> translationRegistry;
|
|
|
|
return translationRegistry;
|
|
|
|
}
|
|
|
|
|
2020-04-06 07:21:07 +08:00
|
|
|
/// 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 <file-to-file> function");
|
|
|
|
assert(function &&
|
|
|
|
"Attempting to register an empty translate <file-to-file> function");
|
|
|
|
translationRegistry[name] = function;
|
|
|
|
}
|
|
|
|
|
|
|
|
TranslateRegistration::TranslateRegistration(
|
|
|
|
StringRef name, const TranslateFunction &function) {
|
|
|
|
registerTranslation(name, function);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Translation to MLIR
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-11-08 03:42:11 +08:00
|
|
|
// 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) {
|
2020-04-06 07:21:07 +08:00
|
|
|
auto wrappedFn = [function](llvm::SourceMgr &sourceMgr, raw_ostream &output,
|
|
|
|
MLIRContext *context) {
|
|
|
|
OwningModuleRef module = function(sourceMgr, context);
|
|
|
|
if (!module || failed(verify(*module)))
|
|
|
|
return failure();
|
|
|
|
module->print(output);
|
|
|
|
return success();
|
|
|
|
};
|
|
|
|
registerTranslation(name, wrappedFn);
|
2018-11-21 22:21:19 +08:00
|
|
|
}
|
|
|
|
|
2019-11-08 03:42:11 +08:00
|
|
|
TranslateToMLIRRegistration::TranslateToMLIRRegistration(
|
|
|
|
StringRef name, const TranslateSourceMgrToMLIRFunction &function) {
|
|
|
|
registerTranslateToMLIRFunction(name, function);
|
|
|
|
}
|
|
|
|
|
2020-04-06 07:21:07 +08:00
|
|
|
/// Wraps `function` with a lambda that extracts a StringRef from a source
|
|
|
|
/// manager and registers the wrapper lambda as a to-MLIR conversion.
|
2019-11-08 03:42:11 +08:00
|
|
|
TranslateToMLIRRegistration::TranslateToMLIRRegistration(
|
|
|
|
StringRef name, const TranslateStringRefToMLIRFunction &function) {
|
2020-04-06 07:21:07 +08:00
|
|
|
registerTranslateToMLIRFunction(
|
|
|
|
name, [function](llvm::SourceMgr &sourceMgr, MLIRContext *ctx) {
|
|
|
|
const llvm::MemoryBuffer *buffer =
|
|
|
|
sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID());
|
|
|
|
return function(buffer->getBuffer(), ctx);
|
|
|
|
});
|
2019-11-08 03:42:11 +08:00
|
|
|
}
|
|
|
|
|
2020-04-06 07:21:07 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Translation from MLIR
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-11-21 22:22:17 +08:00
|
|
|
TranslateFromMLIRRegistration::TranslateFromMLIRRegistration(
|
|
|
|
StringRef name, const TranslateFromMLIRFunction &function) {
|
2020-04-06 07:21:07 +08:00
|
|
|
registerTranslation(name, [function](llvm::SourceMgr &sourceMgr,
|
|
|
|
raw_ostream &output,
|
|
|
|
MLIRContext *context) {
|
|
|
|
auto module = OwningModuleRef(parseSourceFile(sourceMgr, context));
|
|
|
|
if (!module)
|
|
|
|
return failure();
|
|
|
|
return function(module.get(), output);
|
|
|
|
});
|
2019-09-18 04:03:48 +08:00
|
|
|
}
|
|
|
|
|
2020-04-06 07:21:07 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Translation Parser
|
|
|
|
//===----------------------------------------------------------------------===//
|
2018-11-21 22:22:17 +08:00
|
|
|
|
2020-04-06 07:21:07 +08:00
|
|
|
TranslationParser::TranslationParser(llvm::cl::Option &opt)
|
|
|
|
: llvm::cl::parser<const TranslateFunction *>(opt) {
|
|
|
|
for (const auto &kv : getTranslationRegistry())
|
|
|
|
addLiteralOption(kv.first(), &kv.second, kv.first());
|
2018-11-21 22:21:19 +08:00
|
|
|
}
|
2019-09-18 04:03:48 +08:00
|
|
|
|
2020-04-06 07:21:07 +08:00
|
|
|
void TranslationParser::printOptionInfo(const llvm::cl::Option &o,
|
|
|
|
size_t globalWidth) const {
|
|
|
|
TranslationParser *tp = const_cast<TranslationParser *>(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<const TranslateFunction *>::printOptionInfo(o, globalWidth);
|
2019-09-18 04:03:48 +08:00
|
|
|
}
|
2020-08-23 08:57:39 +08:00
|
|
|
|
|
|
|
LogicalResult mlir::mlirTranslateMain(int argc, char **argv,
|
|
|
|
llvm::StringRef toolName) {
|
|
|
|
|
|
|
|
static llvm::cl::opt<std::string> inputFilename(
|
|
|
|
llvm::cl::Positional, llvm::cl::desc("<input file>"),
|
|
|
|
llvm::cl::init("-"));
|
|
|
|
|
|
|
|
static llvm::cl::opt<std::string> outputFilename(
|
|
|
|
"o", llvm::cl::desc("Output filename"), llvm::cl::value_desc("filename"),
|
|
|
|
llvm::cl::init("-"));
|
|
|
|
|
|
|
|
static llvm::cl::opt<bool> splitInputFile(
|
|
|
|
"split-input-file",
|
|
|
|
llvm::cl::desc("Split the input file into pieces and "
|
|
|
|
"process each chunk independently"),
|
|
|
|
llvm::cl::init(false));
|
|
|
|
|
|
|
|
static llvm::cl::opt<bool> verifyDiagnostics(
|
|
|
|
"verify-diagnostics",
|
|
|
|
llvm::cl::desc("Check that emitted diagnostics match "
|
|
|
|
"expected-* lines on the corresponding line"),
|
|
|
|
llvm::cl::init(false));
|
|
|
|
|
|
|
|
llvm::InitLLVM y(argc, argv);
|
|
|
|
|
|
|
|
// Add flags for all the registered translations.
|
|
|
|
llvm::cl::opt<const TranslateFunction *, false, TranslationParser>
|
|
|
|
translationRequested("", llvm::cl::desc("Translation to perform"),
|
|
|
|
llvm::cl::Required);
|
|
|
|
registerAsmPrinterCLOptions();
|
|
|
|
registerMLIRContextCLOptions();
|
|
|
|
llvm::cl::ParseCommandLineOptions(argc, argv, "MLIR translation driver\n");
|
|
|
|
|
|
|
|
std::string errorMessage;
|
|
|
|
auto input = openInputFile(inputFilename, &errorMessage);
|
|
|
|
if (!input) {
|
|
|
|
llvm::errs() << errorMessage << "\n";
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto output = openOutputFile(outputFilename, &errorMessage);
|
|
|
|
if (!output) {
|
|
|
|
llvm::errs() << errorMessage << "\n";
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Processes the memory buffer with a new MLIRContext.
|
|
|
|
auto processBuffer = [&](std::unique_ptr<llvm::MemoryBuffer> ownedBuffer,
|
|
|
|
raw_ostream &os) {
|
|
|
|
MLIRContext context;
|
|
|
|
context.printOpOnDiagnostic(!verifyDiagnostics);
|
|
|
|
llvm::SourceMgr sourceMgr;
|
|
|
|
sourceMgr.AddNewSourceBuffer(std::move(ownedBuffer), llvm::SMLoc());
|
|
|
|
|
|
|
|
if (!verifyDiagnostics) {
|
|
|
|
SourceMgrDiagnosticHandler sourceMgrHandler(sourceMgr, &context);
|
|
|
|
return (*translationRequested)(sourceMgr, os, &context);
|
|
|
|
}
|
|
|
|
|
|
|
|
// In the diagnostic verification flow, we ignore whether the translation
|
|
|
|
// failed (in most cases, it is expected to fail). Instead, we check if the
|
|
|
|
// diagnostics were produced as expected.
|
|
|
|
SourceMgrDiagnosticVerifierHandler sourceMgrHandler(sourceMgr, &context);
|
|
|
|
(*translationRequested)(sourceMgr, os, &context);
|
|
|
|
return sourceMgrHandler.verify();
|
|
|
|
};
|
|
|
|
|
|
|
|
if (splitInputFile) {
|
|
|
|
if (failed(splitAndProcessBuffer(std::move(input), processBuffer,
|
|
|
|
output->os())))
|
|
|
|
return failure();
|
|
|
|
} else if (failed(processBuffer(std::move(input), output->os()))) {
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
|
|
|
|
output->keep();
|
|
|
|
return success();
|
|
|
|
}
|