blob: a75ba76fafc773dc387e7b7a42e1f0ce9a86e04e [file] [log] [blame]
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -07001//===- mlir-opt.cpp - MLIR Optimizer Driver -------------------------------===//
2//
3// Copyright 2019 The MLIR Authors.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16// =============================================================================
17//
18// This is a command line utility that parses an MLIR file, runs an optimization
19// pass, then prints the result back out. It is designed to support unit
20// testing.
21//
22//===----------------------------------------------------------------------===//
23
Chris Lattnerf7e22732018-06-22 22:03:48 -070024#include "mlir/IR/MLIRContext.h"
Chris Lattnere2259872018-06-21 15:22:42 -070025#include "mlir/IR/Module.h"
Chris Lattnere79379a2018-06-22 10:39:19 -070026#include "mlir/Parser.h"
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -070027#include "llvm/Support/CommandLine.h"
Chris Lattnere79379a2018-06-22 10:39:19 -070028#include "llvm/Support/SourceMgr.h"
Chris Lattnere2259872018-06-21 15:22:42 -070029#include "llvm/Support/FileUtilities.h"
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -070030#include "llvm/Support/InitLLVM.h"
Chris Lattnere2259872018-06-21 15:22:42 -070031#include "llvm/Support/ToolOutputFile.h"
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -070032using namespace mlir;
Chris Lattnere2259872018-06-21 15:22:42 -070033using namespace llvm;
34
35static cl::opt<std::string>
36inputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
37
38static cl::opt<std::string>
39outputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
40 cl::init("-"));
41
42
43/// Open the specified output file and return it, exiting if there is any I/O or
44/// other errors.
45static std::unique_ptr<ToolOutputFile> getOutputStream() {
46 std::error_code error;
47 auto result = make_unique<ToolOutputFile>(outputFilename, error,
48 sys::fs::F_None);
49 if (error) {
50 llvm::errs() << error.message() << '\n';
51 exit(1);
52 }
53
54 return result;
55}
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -070056
57int main(int argc, char **argv) {
Chris Lattnere2259872018-06-21 15:22:42 -070058 InitLLVM x(argc, argv);
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -070059
Chris Lattnerf7e22732018-06-22 22:03:48 -070060 MLIRContext context;
61
Chris Lattnere2259872018-06-21 15:22:42 -070062 cl::ParseCommandLineOptions(argc, argv, "MLIR modular optimizer driver\n");
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -070063
Chris Lattnere79379a2018-06-22 10:39:19 -070064 // Set up the input file.
65 auto fileOrErr = MemoryBuffer::getFileOrSTDIN(inputFilename);
66 if (std::error_code error = fileOrErr.getError()) {
67 llvm::errs() << argv[0] << ": could not open input file '" << inputFilename
68 << "': " << error.message() << "\n";
69 return 1;
70 }
71
72 // Tell sourceMgr about this buffer, which is what the parser will pick up.
73 SourceMgr sourceMgr;
74 sourceMgr.AddNewSourceBuffer(std::move(*fileOrErr), SMLoc());
75
76 // Parse the input file and emit any errors.
Chris Lattnerf7e22732018-06-22 22:03:48 -070077 std::unique_ptr<Module> module(parseSourceFile(sourceMgr, &context));
Chris Lattnere79379a2018-06-22 10:39:19 -070078 if (!module) return 1;
Chris Lattnere2259872018-06-21 15:22:42 -070079
80 // Print the output.
81 auto output = getOutputStream();
Chris Lattnere79379a2018-06-22 10:39:19 -070082 module->print(output->os());
Chris Lattnere2259872018-06-21 15:22:42 -070083 output->keep();
Chris Lattnere79379a2018-06-22 10:39:19 -070084
85 // Success.
86 return 0;
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -070087}