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-06-22 00:49:33 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2018-06-23 01:39:19 +08:00
|
|
|
#include "llvm/Support/SourceMgr.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-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("-"));
|
|
|
|
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2018-06-22 06:22:42 +08:00
|
|
|
InitLLVM x(argc, argv);
|
2018-06-22 00:49:33 +08:00
|
|
|
|
2018-06-23 13:03:48 +08:00
|
|
|
MLIRContext context;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tell sourceMgr about this buffer, which is what the parser will pick up.
|
|
|
|
SourceMgr sourceMgr;
|
|
|
|
sourceMgr.AddNewSourceBuffer(std::move(*fileOrErr), SMLoc());
|
|
|
|
|
|
|
|
// Parse the input file and emit any errors.
|
2018-06-23 13:03:48 +08:00
|
|
|
std::unique_ptr<Module> module(parseSourceFile(sourceMgr, &context));
|
2018-06-23 01:39:19 +08:00
|
|
|
if (!module) return 1;
|
2018-06-22 06:22:42 +08:00
|
|
|
|
|
|
|
// Print the output.
|
|
|
|
auto output = getOutputStream();
|
2018-06-23 01:39:19 +08:00
|
|
|
module->print(output->os());
|
2018-06-22 06:22:42 +08:00
|
|
|
output->keep();
|
2018-06-23 01:39:19 +08:00
|
|
|
|
|
|
|
// Success.
|
|
|
|
return 0;
|
2018-06-22 00:49:33 +08:00
|
|
|
}
|