blob: dee86ed2624b02927a81cccfe125c297a431e6f8 [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 Lattnere2259872018-06-21 15:22:42 -070024#include "mlir/IR/Module.h"
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -070025#include "llvm/Support/CommandLine.h"
Chris Lattnere2259872018-06-21 15:22:42 -070026#include "llvm/Support/FileUtilities.h"
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -070027#include "llvm/Support/InitLLVM.h"
Chris Lattnere2259872018-06-21 15:22:42 -070028#include "llvm/Support/ToolOutputFile.h"
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -070029using namespace mlir;
Chris Lattnere2259872018-06-21 15:22:42 -070030using namespace llvm;
31
32static cl::opt<std::string>
33inputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
34
35static cl::opt<std::string>
36outputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
37 cl::init("-"));
38
39
40/// Open the specified output file and return it, exiting if there is any I/O or
41/// other errors.
42static std::unique_ptr<ToolOutputFile> getOutputStream() {
43 std::error_code error;
44 auto result = make_unique<ToolOutputFile>(outputFilename, error,
45 sys::fs::F_None);
46 if (error) {
47 llvm::errs() << error.message() << '\n';
48 exit(1);
49 }
50
51 return result;
52}
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -070053
54int main(int argc, char **argv) {
Chris Lattnere2259872018-06-21 15:22:42 -070055 InitLLVM x(argc, argv);
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -070056
Chris Lattnere2259872018-06-21 15:22:42 -070057 cl::ParseCommandLineOptions(argc, argv, "MLIR modular optimizer driver\n");
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -070058
59 // Instantiate an IR object.
Chris Lattnere2259872018-06-21 15:22:42 -070060 Module m;
61 m.functionList.push_back(new Function("foo"));
62 m.functionList.push_back(new Function("bar"));
63
64 // Print the output.
65 auto output = getOutputStream();
66 m.print(output->os());
67 output->keep();
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -070068}