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 | 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" |
Jacques Pienaar | ca4c4a0 | 2018-06-25 08:10:46 -0700 | [diff] [blame] | 30 | #include "llvm/Support/Regex.h" |
Jacques Pienaar | 39ffa10 | 2018-07-07 19:12:22 -0700 | [diff] [blame^] | 31 | #include "llvm/Support/SourceMgr.h" |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 32 | #include "llvm/Support/ToolOutputFile.h" |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 33 | using namespace mlir; |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 34 | using namespace llvm; |
| 35 | |
| 36 | static cl::opt<std::string> |
| 37 | inputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-")); |
| 38 | |
| 39 | static cl::opt<std::string> |
| 40 | outputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), |
| 41 | cl::init("-")); |
| 42 | |
Jacques Pienaar | bae4051 | 2018-06-24 09:10:36 -0700 | [diff] [blame] | 43 | static cl::opt<bool> |
| 44 | checkParserErrors("check-parser-errors", cl::desc("Check for parser errors"), |
| 45 | cl::init(false)); |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 46 | |
Jacques Pienaar | 7b82970 | 2018-07-03 13:24:09 -0700 | [diff] [blame] | 47 | enum OptResult { OptSuccess, OptFailure }; |
| 48 | |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 49 | /// Open the specified output file and return it, exiting if there is any I/O or |
| 50 | /// other errors. |
| 51 | static std::unique_ptr<ToolOutputFile> getOutputStream() { |
| 52 | std::error_code error; |
| 53 | auto result = make_unique<ToolOutputFile>(outputFilename, error, |
| 54 | sys::fs::F_None); |
| 55 | if (error) { |
| 56 | llvm::errs() << error.message() << '\n'; |
| 57 | exit(1); |
| 58 | } |
| 59 | |
| 60 | return result; |
| 61 | } |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 62 | |
Jacques Pienaar | bae4051 | 2018-06-24 09:10:36 -0700 | [diff] [blame] | 63 | /// Parses the memory buffer and, if successfully parsed, prints the parsed |
Jacques Pienaar | 7b82970 | 2018-07-03 13:24:09 -0700 | [diff] [blame] | 64 | /// output. |
| 65 | OptResult parseAndPrintMemoryBuffer(std::unique_ptr<MemoryBuffer> buffer) { |
Jacques Pienaar | bae4051 | 2018-06-24 09:10:36 -0700 | [diff] [blame] | 66 | // Tell sourceMgr about this buffer, which is what the parser will pick up. |
| 67 | SourceMgr sourceMgr; |
| 68 | sourceMgr.AddNewSourceBuffer(std::move(buffer), SMLoc()); |
| 69 | |
Jacques Pienaar | 9c411be | 2018-06-24 19:17:35 -0700 | [diff] [blame] | 70 | // Parse the input file. |
Jacques Pienaar | bae4051 | 2018-06-24 09:10:36 -0700 | [diff] [blame] | 71 | MLIRContext context; |
Jacques Pienaar | 7b82970 | 2018-07-03 13:24:09 -0700 | [diff] [blame] | 72 | std::unique_ptr<Module> module(parseSourceFile(sourceMgr, &context)); |
| 73 | if (!module) |
| 74 | return OptFailure; |
Jacques Pienaar | bae4051 | 2018-06-24 09:10:36 -0700 | [diff] [blame] | 75 | |
| 76 | // Print the output. |
| 77 | auto output = getOutputStream(); |
| 78 | module->print(output->os()); |
| 79 | output->keep(); |
| 80 | |
Jacques Pienaar | 7b82970 | 2018-07-03 13:24:09 -0700 | [diff] [blame] | 81 | return OptSuccess; |
Jacques Pienaar | bae4051 | 2018-06-24 09:10:36 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /// Split the memory buffer into multiple buffers using the marker -----. |
Jacques Pienaar | 7b82970 | 2018-07-03 13:24:09 -0700 | [diff] [blame] | 85 | OptResult |
| 86 | splitMemoryBufferForErrorChecking(std::unique_ptr<MemoryBuffer> buffer) { |
Jacques Pienaar | bae4051 | 2018-06-24 09:10:36 -0700 | [diff] [blame] | 87 | const char marker[] = "-----"; |
| 88 | SmallVector<StringRef, 2> sourceBuffers; |
| 89 | buffer->getBuffer().split(sourceBuffers, marker); |
Jacques Pienaar | bae4051 | 2018-06-24 09:10:36 -0700 | [diff] [blame] | 90 | |
Jacques Pienaar | ca4c4a0 | 2018-06-25 08:10:46 -0700 | [diff] [blame] | 91 | // Error reporter that verifies error reports matches expected error |
| 92 | // substring. |
| 93 | // TODO: Only checking for error cases below. Could be expanded to other kinds |
| 94 | // of diagnostics. |
| 95 | // TODO: Enable specifying errors on different lines (@-1). |
| 96 | // TODO: Currently only checking if substring matches, enable regex checking. |
Jacques Pienaar | 7b82970 | 2018-07-03 13:24:09 -0700 | [diff] [blame] | 97 | OptResult opt_result = OptSuccess; |
Jacques Pienaar | b2ddbb6 | 2018-06-26 08:56:55 -0700 | [diff] [blame] | 98 | SourceMgr fileSourceMgr; |
| 99 | fileSourceMgr.AddNewSourceBuffer(std::move(buffer), SMLoc()); |
| 100 | |
Jacques Pienaar | 39ffa10 | 2018-07-07 19:12:22 -0700 | [diff] [blame^] | 101 | // Record the expected errors's position, substring and whether it was seen. |
| 102 | struct ExpectedError { |
| 103 | int lineNo; |
| 104 | StringRef substring; |
| 105 | SMLoc fileLoc; |
| 106 | bool matched; |
| 107 | }; |
| 108 | |
Jacques Pienaar | b2ddbb6 | 2018-06-26 08:56:55 -0700 | [diff] [blame] | 109 | // Tracks offset of subbuffer into original buffer. |
| 110 | const char *fileOffset = |
| 111 | fileSourceMgr.getMemoryBuffer(fileSourceMgr.getMainFileID()) |
| 112 | ->getBufferStart(); |
| 113 | |
Jacques Pienaar | 39ffa10 | 2018-07-07 19:12:22 -0700 | [diff] [blame^] | 114 | for (auto &subbuffer : sourceBuffers) { |
Jacques Pienaar | b2ddbb6 | 2018-06-26 08:56:55 -0700 | [diff] [blame] | 115 | SourceMgr sourceMgr; |
| 116 | // Tell sourceMgr about this buffer, which is what the parser will pick up. |
| 117 | sourceMgr.AddNewSourceBuffer(MemoryBuffer::getMemBufferCopy(subbuffer), |
| 118 | SMLoc()); |
| 119 | |
Jacques Pienaar | 39ffa10 | 2018-07-07 19:12:22 -0700 | [diff] [blame^] | 120 | // Extracing the expected errors. |
| 121 | llvm::Regex expected("expected-error(@[+-][0-9]+)? {{(.*)}}"); |
| 122 | SmallVector<ExpectedError, 2> expectedErrors; |
| 123 | SmallVector<StringRef, 100> lines; |
| 124 | subbuffer.split(lines, '\n'); |
| 125 | size_t bufOffset = 0; |
| 126 | for (int lineNo = 0; lineNo < lines.size(); ++lineNo) { |
| 127 | SmallVector<StringRef, 3> matches; |
| 128 | if (expected.match(lines[lineNo], &matches)) { |
| 129 | // Point to the start of expected-error. |
| 130 | SMLoc errorStart = |
| 131 | SMLoc::getFromPointer(fileOffset + bufOffset + |
| 132 | lines[lineNo].size() - matches[2].size() - 2); |
| 133 | ExpectedError expErr{lineNo + 1, matches[2], errorStart, false}; |
| 134 | int offset; |
| 135 | if (!matches[1].empty() && |
| 136 | !matches[1].drop_front().getAsInteger(0, offset)) { |
| 137 | expErr.lineNo += offset; |
| 138 | } |
| 139 | expectedErrors.push_back(expErr); |
| 140 | } |
| 141 | bufOffset += lines[lineNo].size() + 1; |
Jacques Pienaar | ca4c4a0 | 2018-06-25 08:10:46 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Jacques Pienaar | 39ffa10 | 2018-07-07 19:12:22 -0700 | [diff] [blame^] | 144 | // Error checker that verifies reported error was expected. |
| 145 | auto checker = [&](const SMDiagnostic &err) { |
| 146 | for (auto &e : expectedErrors) { |
| 147 | if (err.getLineNo() == e.lineNo && |
| 148 | err.getMessage().contains(e.substring)) { |
| 149 | e.matched = true; |
| 150 | return; |
| 151 | } |
| 152 | } |
| 153 | // Report error if no match found. |
| 154 | const auto &sourceMgr = *err.getSourceMgr(); |
| 155 | const char *bufferStart = |
| 156 | sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID()) |
| 157 | ->getBufferStart(); |
| 158 | |
| 159 | size_t offset = err.getLoc().getPointer() - bufferStart; |
| 160 | SMLoc loc = SMLoc::getFromPointer(fileOffset + offset); |
| 161 | fileSourceMgr.PrintMessage(loc, SourceMgr::DK_Error, |
| 162 | "unexpected error: " + err.getMessage()); |
| 163 | opt_result = OptFailure; |
| 164 | }; |
| 165 | |
Jacques Pienaar | b2ddbb6 | 2018-06-26 08:56:55 -0700 | [diff] [blame] | 166 | // Parse the input file. |
| 167 | MLIRContext context; |
| 168 | std::unique_ptr<Module> module( |
| 169 | parseSourceFile(sourceMgr, &context, checker)); |
Jacques Pienaar | b2ddbb6 | 2018-06-26 08:56:55 -0700 | [diff] [blame] | 170 | |
Jacques Pienaar | 39ffa10 | 2018-07-07 19:12:22 -0700 | [diff] [blame^] | 171 | // Verify that all expected errors were seen. |
| 172 | for (auto err : expectedErrors) { |
| 173 | if (!err.matched) { |
| 174 | SMRange range(err.fileLoc, |
| 175 | SMLoc::getFromPointer(err.fileLoc.getPointer() + |
| 176 | err.substring.size())); |
| 177 | fileSourceMgr.PrintMessage( |
| 178 | err.fileLoc, SourceMgr::DK_Error, |
| 179 | "expected error \"" + err.substring + "\" was not produced", range); |
| 180 | opt_result = OptFailure; |
| 181 | } |
Jacques Pienaar | ca4c4a0 | 2018-06-25 08:10:46 -0700 | [diff] [blame] | 182 | } |
Jacques Pienaar | b2ddbb6 | 2018-06-26 08:56:55 -0700 | [diff] [blame] | 183 | |
| 184 | fileOffset += subbuffer.size() + strlen(marker); |
Jacques Pienaar | ca4c4a0 | 2018-06-25 08:10:46 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Jacques Pienaar | 7b82970 | 2018-07-03 13:24:09 -0700 | [diff] [blame] | 187 | return opt_result; |
Jacques Pienaar | bae4051 | 2018-06-24 09:10:36 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 190 | int main(int argc, char **argv) { |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 191 | InitLLVM x(argc, argv); |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 192 | |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 193 | cl::ParseCommandLineOptions(argc, argv, "MLIR modular optimizer driver\n"); |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 194 | |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 195 | // Set up the input file. |
| 196 | auto fileOrErr = MemoryBuffer::getFileOrSTDIN(inputFilename); |
| 197 | if (std::error_code error = fileOrErr.getError()) { |
| 198 | llvm::errs() << argv[0] << ": could not open input file '" << inputFilename |
| 199 | << "': " << error.message() << "\n"; |
| 200 | return 1; |
| 201 | } |
| 202 | |
Jacques Pienaar | bae4051 | 2018-06-24 09:10:36 -0700 | [diff] [blame] | 203 | if (checkParserErrors) |
Jacques Pienaar | 7b82970 | 2018-07-03 13:24:09 -0700 | [diff] [blame] | 204 | return splitMemoryBufferForErrorChecking(std::move(*fileOrErr)); |
Jacques Pienaar | ca4c4a0 | 2018-06-25 08:10:46 -0700 | [diff] [blame] | 205 | |
Jacques Pienaar | 7b82970 | 2018-07-03 13:24:09 -0700 | [diff] [blame] | 206 | return parseAndPrintMemoryBuffer(std::move(*fileOrErr)); |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 207 | } |