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 | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 24 | #include "mlir/IR/MLIRContext.h" |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 25 | #include "mlir/IR/Module.h" |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 26 | #include "mlir/Parser.h" |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 27 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 28 | #include "llvm/Support/SourceMgr.h" |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 29 | #include "llvm/Support/FileUtilities.h" |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 30 | #include "llvm/Support/InitLLVM.h" |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 31 | #include "llvm/Support/ToolOutputFile.h" |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 32 | using namespace mlir; |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 33 | using namespace llvm; |
| 34 | |
| 35 | static cl::opt<std::string> |
| 36 | inputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-")); |
| 37 | |
| 38 | static cl::opt<std::string> |
| 39 | outputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), |
| 40 | cl::init("-")); |
| 41 | |
Jacques Pienaar | bae4051 | 2018-06-24 09:10:36 -0700 | [diff] [blame] | 42 | static cl::opt<bool> |
| 43 | checkParserErrors("check-parser-errors", cl::desc("Check for parser errors"), |
| 44 | cl::init(false)); |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 45 | |
| 46 | /// Open the specified output file and return it, exiting if there is any I/O or |
| 47 | /// other errors. |
| 48 | static std::unique_ptr<ToolOutputFile> getOutputStream() { |
| 49 | std::error_code error; |
| 50 | auto result = make_unique<ToolOutputFile>(outputFilename, error, |
| 51 | sys::fs::F_None); |
| 52 | if (error) { |
| 53 | llvm::errs() << error.message() << '\n'; |
| 54 | exit(1); |
| 55 | } |
| 56 | |
| 57 | return result; |
| 58 | } |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 59 | |
Jacques Pienaar | bae4051 | 2018-06-24 09:10:36 -0700 | [diff] [blame] | 60 | /// Parses the memory buffer and, if successfully parsed, prints the parsed |
| 61 | /// output. Returns whether parsing succeeded. |
| 62 | bool parseAndPrintMemoryBuffer(std::unique_ptr<MemoryBuffer> buffer) { |
| 63 | // Tell sourceMgr about this buffer, which is what the parser will pick up. |
| 64 | SourceMgr sourceMgr; |
| 65 | sourceMgr.AddNewSourceBuffer(std::move(buffer), SMLoc()); |
| 66 | |
| 67 | // Parse the input file and emit any errors. |
| 68 | MLIRContext context; |
| 69 | std::unique_ptr<Module> module(parseSourceFile(sourceMgr, &context)); |
| 70 | if (!module) return false; |
| 71 | |
| 72 | // Print the output. |
| 73 | auto output = getOutputStream(); |
| 74 | module->print(output->os()); |
| 75 | output->keep(); |
| 76 | |
| 77 | // Success. |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | /// Split the memory buffer into multiple buffers using the marker -----. |
| 82 | bool splitMemoryBufferForErrorChecking(std::unique_ptr<MemoryBuffer> buffer) { |
| 83 | const char marker[] = "-----"; |
| 84 | SmallVector<StringRef, 2> sourceBuffers; |
| 85 | buffer->getBuffer().split(sourceBuffers, marker); |
| 86 | for (auto& subbuffer : sourceBuffers) |
| 87 | parseAndPrintMemoryBuffer(MemoryBuffer::getMemBufferCopy(subbuffer)); |
| 88 | |
| 89 | // Ignore errors returned by parseAndPrintMemoryBuffer when checking parse |
| 90 | // errors reported. |
| 91 | return true; |
| 92 | } |
| 93 | |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 94 | int main(int argc, char **argv) { |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 95 | InitLLVM x(argc, argv); |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 96 | |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 97 | cl::ParseCommandLineOptions(argc, argv, "MLIR modular optimizer driver\n"); |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 98 | |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 99 | // Set up the input file. |
| 100 | auto fileOrErr = MemoryBuffer::getFileOrSTDIN(inputFilename); |
| 101 | if (std::error_code error = fileOrErr.getError()) { |
| 102 | llvm::errs() << argv[0] << ": could not open input file '" << inputFilename |
| 103 | << "': " << error.message() << "\n"; |
| 104 | return 1; |
| 105 | } |
| 106 | |
Jacques Pienaar | bae4051 | 2018-06-24 09:10:36 -0700 | [diff] [blame] | 107 | if (checkParserErrors) |
| 108 | return !splitMemoryBufferForErrorChecking(std::move(*fileOrErr)); |
| 109 | return !parseAndPrintMemoryBuffer(std::move(*fileOrErr)); |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 110 | } |