Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 1 | //===- 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 Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 24 | #include "mlir/IR/Module.h" |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 25 | #include "mlir/Parser.h" |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 26 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 27 | #include "llvm/Support/SourceMgr.h" |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 28 | #include "llvm/Support/FileUtilities.h" |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 29 | #include "llvm/Support/InitLLVM.h" |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 30 | #include "llvm/Support/ToolOutputFile.h" |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 31 | using namespace mlir; |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 32 | using namespace llvm; |
| 33 | |
| 34 | static cl::opt<std::string> |
| 35 | inputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-")); |
| 36 | |
| 37 | static cl::opt<std::string> |
| 38 | outputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), |
| 39 | cl::init("-")); |
| 40 | |
| 41 | |
| 42 | /// Open the specified output file and return it, exiting if there is any I/O or |
| 43 | /// other errors. |
| 44 | static std::unique_ptr<ToolOutputFile> getOutputStream() { |
| 45 | std::error_code error; |
| 46 | auto result = make_unique<ToolOutputFile>(outputFilename, error, |
| 47 | sys::fs::F_None); |
| 48 | if (error) { |
| 49 | llvm::errs() << error.message() << '\n'; |
| 50 | exit(1); |
| 51 | } |
| 52 | |
| 53 | return result; |
| 54 | } |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 55 | |
| 56 | int main(int argc, char **argv) { |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 57 | InitLLVM x(argc, argv); |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 58 | |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 59 | cl::ParseCommandLineOptions(argc, argv, "MLIR modular optimizer driver\n"); |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 60 | |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 61 | // Set up the input file. |
| 62 | auto fileOrErr = MemoryBuffer::getFileOrSTDIN(inputFilename); |
| 63 | if (std::error_code error = fileOrErr.getError()) { |
| 64 | llvm::errs() << argv[0] << ": could not open input file '" << inputFilename |
| 65 | << "': " << error.message() << "\n"; |
| 66 | return 1; |
| 67 | } |
| 68 | |
| 69 | // Tell sourceMgr about this buffer, which is what the parser will pick up. |
| 70 | SourceMgr sourceMgr; |
| 71 | sourceMgr.AddNewSourceBuffer(std::move(*fileOrErr), SMLoc()); |
| 72 | |
| 73 | // Parse the input file and emit any errors. |
| 74 | std::unique_ptr<Module> module(parseSourceFile(sourceMgr)); |
| 75 | if (!module) return 1; |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 76 | |
| 77 | // Print the output. |
| 78 | auto output = getOutputStream(); |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 79 | module->print(output->os()); |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 80 | output->keep(); |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 81 | |
| 82 | // Success. |
| 83 | return 0; |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 84 | } |