2018-06-22 00:49:33 +08:00
|
|
|
//===- mlir-opt.cpp - MLIR Optimizer Driver -------------------------------===//
|
|
|
|
//
|
|
|
|
// Copyright 2019 The MLIR Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
// =============================================================================
|
|
|
|
//
|
|
|
|
// This is a command line utility that parses an MLIR file, runs an optimization
|
|
|
|
// pass, then prints the result back out. It is designed to support unit
|
|
|
|
// testing.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-06-23 13:03:48 +08:00
|
|
|
#include "mlir/IR/MLIRContext.h"
|
2018-06-22 06:22:42 +08:00
|
|
|
#include "mlir/IR/Module.h"
|
2018-06-23 01:39:19 +08:00
|
|
|
#include "mlir/Parser.h"
|
2018-07-25 01:15:13 +08:00
|
|
|
#include "mlir/Transforms/ConvertToCFG.h"
|
2018-06-22 00:49:33 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2018-06-22 06:22:42 +08:00
|
|
|
#include "llvm/Support/FileUtilities.h"
|
2018-06-22 00:49:33 +08:00
|
|
|
#include "llvm/Support/InitLLVM.h"
|
2018-06-25 23:10:46 +08:00
|
|
|
#include "llvm/Support/Regex.h"
|
2018-07-08 10:12:22 +08:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2018-06-22 06:22:42 +08:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2018-06-22 00:49:33 +08:00
|
|
|
using namespace mlir;
|
2018-06-22 06:22:42 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
inputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
|
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
outputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
|
|
|
|
cl::init("-"));
|
|
|
|
|
2018-06-25 00:10:36 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
checkParserErrors("check-parser-errors", cl::desc("Check for parser errors"),
|
|
|
|
cl::init(false));
|
2018-06-22 06:22:42 +08:00
|
|
|
|
2018-07-25 01:15:13 +08:00
|
|
|
static cl::opt<bool> convertToCFGOpt(
|
|
|
|
"convert-to-cfg",
|
|
|
|
cl::desc("Convert all ML functions in the module to CFG ones"));
|
|
|
|
|
2018-07-04 04:24:09 +08:00
|
|
|
enum OptResult { OptSuccess, OptFailure };
|
|
|
|
|
2018-06-22 06:22:42 +08:00
|
|
|
/// Open the specified output file and return it, exiting if there is any I/O or
|
|
|
|
/// other errors.
|
|
|
|
static std::unique_ptr<ToolOutputFile> getOutputStream() {
|
|
|
|
std::error_code error;
|
|
|
|
auto result = make_unique<ToolOutputFile>(outputFilename, error,
|
|
|
|
sys::fs::F_None);
|
|
|
|
if (error) {
|
|
|
|
llvm::errs() << error.message() << '\n';
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2018-06-22 00:49:33 +08:00
|
|
|
|
2018-06-25 00:10:36 +08:00
|
|
|
/// Parses the memory buffer and, if successfully parsed, prints the parsed
|
2018-07-25 01:15:13 +08:00
|
|
|
/// output. Optionally, convert ML functions into CFG functions.
|
|
|
|
/// TODO: pull parsing and printing into separate functions.
|
2018-07-04 04:24:09 +08:00
|
|
|
OptResult parseAndPrintMemoryBuffer(std::unique_ptr<MemoryBuffer> buffer) {
|
2018-06-25 00:10:36 +08:00
|
|
|
// Tell sourceMgr about this buffer, which is what the parser will pick up.
|
|
|
|
SourceMgr sourceMgr;
|
|
|
|
sourceMgr.AddNewSourceBuffer(std::move(buffer), SMLoc());
|
2018-06-22 00:49:33 +08:00
|
|
|
|
2018-06-25 10:17:35 +08:00
|
|
|
// Parse the input file.
|
2018-06-23 13:03:48 +08:00
|
|
|
MLIRContext context;
|
2018-07-04 04:24:09 +08:00
|
|
|
std::unique_ptr<Module> module(parseSourceFile(sourceMgr, &context));
|
|
|
|
if (!module)
|
|
|
|
return OptFailure;
|
2018-06-25 00:10:36 +08:00
|
|
|
|
2018-07-25 01:15:13 +08:00
|
|
|
// Convert ML functions into CFG functions
|
|
|
|
if (convertToCFGOpt)
|
|
|
|
convertToCFG(module.get());
|
|
|
|
|
2018-06-25 00:10:36 +08:00
|
|
|
// Print the output.
|
|
|
|
auto output = getOutputStream();
|
|
|
|
module->print(output->os());
|
|
|
|
output->keep();
|
|
|
|
|
2018-07-04 04:24:09 +08:00
|
|
|
return OptSuccess;
|
2018-06-25 00:10:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Split the memory buffer into multiple buffers using the marker -----.
|
2018-07-04 04:24:09 +08:00
|
|
|
OptResult
|
|
|
|
splitMemoryBufferForErrorChecking(std::unique_ptr<MemoryBuffer> buffer) {
|
2018-06-25 00:10:36 +08:00
|
|
|
const char marker[] = "-----";
|
|
|
|
SmallVector<StringRef, 2> sourceBuffers;
|
|
|
|
buffer->getBuffer().split(sourceBuffers, marker);
|
|
|
|
|
2018-06-25 23:10:46 +08:00
|
|
|
// Error reporter that verifies error reports matches expected error
|
|
|
|
// substring.
|
|
|
|
// TODO: Only checking for error cases below. Could be expanded to other kinds
|
|
|
|
// of diagnostics.
|
|
|
|
// TODO: Enable specifying errors on different lines (@-1).
|
|
|
|
// TODO: Currently only checking if substring matches, enable regex checking.
|
2018-07-04 04:24:09 +08:00
|
|
|
OptResult opt_result = OptSuccess;
|
2018-06-26 23:56:55 +08:00
|
|
|
SourceMgr fileSourceMgr;
|
|
|
|
fileSourceMgr.AddNewSourceBuffer(std::move(buffer), SMLoc());
|
|
|
|
|
2018-07-08 10:12:22 +08:00
|
|
|
// Record the expected errors's position, substring and whether it was seen.
|
|
|
|
struct ExpectedError {
|
|
|
|
int lineNo;
|
|
|
|
StringRef substring;
|
|
|
|
SMLoc fileLoc;
|
|
|
|
bool matched;
|
|
|
|
};
|
|
|
|
|
2018-06-26 23:56:55 +08:00
|
|
|
// Tracks offset of subbuffer into original buffer.
|
|
|
|
const char *fileOffset =
|
|
|
|
fileSourceMgr.getMemoryBuffer(fileSourceMgr.getMainFileID())
|
|
|
|
->getBufferStart();
|
|
|
|
|
2018-07-08 10:12:22 +08:00
|
|
|
for (auto &subbuffer : sourceBuffers) {
|
2018-06-26 23:56:55 +08:00
|
|
|
SourceMgr sourceMgr;
|
|
|
|
// Tell sourceMgr about this buffer, which is what the parser will pick up.
|
|
|
|
sourceMgr.AddNewSourceBuffer(MemoryBuffer::getMemBufferCopy(subbuffer),
|
|
|
|
SMLoc());
|
|
|
|
|
2018-07-08 10:12:22 +08:00
|
|
|
// Extracing the expected errors.
|
2018-07-23 06:45:24 +08:00
|
|
|
llvm::Regex expected("expected-error(@[+-][0-9]+)? *{{(.*)}}");
|
2018-07-08 10:12:22 +08:00
|
|
|
SmallVector<ExpectedError, 2> expectedErrors;
|
|
|
|
SmallVector<StringRef, 100> lines;
|
|
|
|
subbuffer.split(lines, '\n');
|
|
|
|
size_t bufOffset = 0;
|
|
|
|
for (int lineNo = 0; lineNo < lines.size(); ++lineNo) {
|
|
|
|
SmallVector<StringRef, 3> matches;
|
|
|
|
if (expected.match(lines[lineNo], &matches)) {
|
|
|
|
// Point to the start of expected-error.
|
|
|
|
SMLoc errorStart =
|
|
|
|
SMLoc::getFromPointer(fileOffset + bufOffset +
|
|
|
|
lines[lineNo].size() - matches[2].size() - 2);
|
|
|
|
ExpectedError expErr{lineNo + 1, matches[2], errorStart, false};
|
|
|
|
int offset;
|
|
|
|
if (!matches[1].empty() &&
|
|
|
|
!matches[1].drop_front().getAsInteger(0, offset)) {
|
|
|
|
expErr.lineNo += offset;
|
|
|
|
}
|
|
|
|
expectedErrors.push_back(expErr);
|
|
|
|
}
|
|
|
|
bufOffset += lines[lineNo].size() + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error checker that verifies reported error was expected.
|
|
|
|
auto checker = [&](const SMDiagnostic &err) {
|
|
|
|
for (auto &e : expectedErrors) {
|
|
|
|
if (err.getLineNo() == e.lineNo &&
|
|
|
|
err.getMessage().contains(e.substring)) {
|
|
|
|
e.matched = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Report error if no match found.
|
|
|
|
const auto &sourceMgr = *err.getSourceMgr();
|
|
|
|
const char *bufferStart =
|
|
|
|
sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID())
|
|
|
|
->getBufferStart();
|
|
|
|
|
|
|
|
size_t offset = err.getLoc().getPointer() - bufferStart;
|
|
|
|
SMLoc loc = SMLoc::getFromPointer(fileOffset + offset);
|
2018-06-26 23:56:55 +08:00
|
|
|
fileSourceMgr.PrintMessage(loc, SourceMgr::DK_Error,
|
2018-07-08 10:12:22 +08:00
|
|
|
"unexpected error: " + err.getMessage());
|
2018-07-04 04:24:09 +08:00
|
|
|
opt_result = OptFailure;
|
2018-07-08 10:12:22 +08:00
|
|
|
};
|
2018-06-25 23:10:46 +08:00
|
|
|
|
2018-06-26 23:56:55 +08:00
|
|
|
// Parse the input file.
|
|
|
|
MLIRContext context;
|
|
|
|
std::unique_ptr<Module> module(
|
|
|
|
parseSourceFile(sourceMgr, &context, checker));
|
2018-07-08 10:12:22 +08:00
|
|
|
|
|
|
|
// Verify that all expected errors were seen.
|
|
|
|
for (auto err : expectedErrors) {
|
|
|
|
if (!err.matched) {
|
|
|
|
SMRange range(err.fileLoc,
|
|
|
|
SMLoc::getFromPointer(err.fileLoc.getPointer() +
|
|
|
|
err.substring.size()));
|
|
|
|
fileSourceMgr.PrintMessage(
|
|
|
|
err.fileLoc, SourceMgr::DK_Error,
|
|
|
|
"expected error \"" + err.substring + "\" was not produced", range);
|
|
|
|
opt_result = OptFailure;
|
|
|
|
}
|
2018-06-25 23:10:46 +08:00
|
|
|
}
|
2018-06-26 23:56:55 +08:00
|
|
|
|
|
|
|
fileOffset += subbuffer.size() + strlen(marker);
|
2018-06-25 23:10:46 +08:00
|
|
|
}
|
|
|
|
|
2018-07-04 04:24:09 +08:00
|
|
|
return opt_result;
|
2018-06-25 00:10:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
InitLLVM x(argc, argv);
|
2018-06-23 13:03:48 +08:00
|
|
|
|
2018-06-22 06:22:42 +08:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, "MLIR modular optimizer driver\n");
|
2018-06-22 00:49:33 +08:00
|
|
|
|
2018-06-23 01:39:19 +08:00
|
|
|
// Set up the input file.
|
|
|
|
auto fileOrErr = MemoryBuffer::getFileOrSTDIN(inputFilename);
|
|
|
|
if (std::error_code error = fileOrErr.getError()) {
|
|
|
|
llvm::errs() << argv[0] << ": could not open input file '" << inputFilename
|
|
|
|
<< "': " << error.message() << "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-06-25 00:10:36 +08:00
|
|
|
if (checkParserErrors)
|
2018-07-04 04:24:09 +08:00
|
|
|
return splitMemoryBufferForErrorChecking(std::move(*fileOrErr));
|
2018-06-25 23:10:46 +08:00
|
|
|
|
2018-07-04 04:24:09 +08:00
|
|
|
return parseAndPrintMemoryBuffer(std::move(*fileOrErr));
|
2018-06-22 00:49:33 +08:00
|
|
|
}
|