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" |
Jacques Pienaar | ca4c4a0 | 2018-06-25 08:10:46 -0700 | [diff] [blame] | 31 | #include "llvm/Support/Regex.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 | |
| 101 | // Tracks offset of subbuffer into original buffer. |
| 102 | const char *fileOffset = |
| 103 | fileSourceMgr.getMemoryBuffer(fileSourceMgr.getMainFileID()) |
| 104 | ->getBufferStart(); |
| 105 | |
| 106 | // Create error checker that uses the helper function to relate the reported |
| 107 | // error to the file being parsed. |
Jacques Pienaar | 7b82970 | 2018-07-03 13:24:09 -0700 | [diff] [blame] | 108 | SMDiagnosticHandlerTy checker = [&](const SMDiagnostic &err) { |
Jacques Pienaar | b2ddbb6 | 2018-06-26 08:56:55 -0700 | [diff] [blame] | 109 | const auto &sourceMgr = *err.getSourceMgr(); |
| 110 | const char *bufferStart = |
| 111 | sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID())->getBufferStart(); |
| 112 | |
Jacques Pienaar | ca4c4a0 | 2018-06-25 08:10:46 -0700 | [diff] [blame] | 113 | StringRef line = err.getLineContents(); |
Jacques Pienaar | b2ddbb6 | 2018-06-26 08:56:55 -0700 | [diff] [blame] | 114 | size_t offset = err.getLoc().getPointer() - bufferStart; |
| 115 | SMLoc loc = SMLoc::getFromPointer(fileOffset + offset); |
| 116 | |
Jacques Pienaar | ca4c4a0 | 2018-06-25 08:10:46 -0700 | [diff] [blame] | 117 | // Extract expected substring using regex and check simple containment in |
| 118 | // error message. |
| 119 | llvm::Regex expected("expected-error {{(.*)}}"); |
| 120 | SmallVector<StringRef, 2> matches; |
| 121 | bool matched = expected.match(line, &matches); |
| 122 | if (matches.size() != 2) { |
Jacques Pienaar | b2ddbb6 | 2018-06-26 08:56:55 -0700 | [diff] [blame] | 123 | fileSourceMgr.PrintMessage( |
| 124 | loc, SourceMgr::DK_Error, |
| 125 | "unexpected error: " + err.getMessage()); |
Jacques Pienaar | 7b82970 | 2018-07-03 13:24:09 -0700 | [diff] [blame] | 126 | opt_result = OptFailure; |
Jacques Pienaar | ca4c4a0 | 2018-06-25 08:10:46 -0700 | [diff] [blame] | 127 | return; |
| 128 | } |
| 129 | |
| 130 | matched = err.getMessage().contains(matches[1]); |
| 131 | if (!matched) { |
Jacques Pienaar | b2ddbb6 | 2018-06-26 08:56:55 -0700 | [diff] [blame] | 132 | const char checkPrefix[] = "expected-error {{"; |
| 133 | loc = SMLoc::getFromPointer(fileOffset + offset + line.find(checkPrefix) - |
| 134 | err.getColumnNo() + strlen(checkPrefix)); |
Jacques Pienaar | 7b82970 | 2018-07-03 13:24:09 -0700 | [diff] [blame] | 135 | fileSourceMgr.PrintMessage( |
| 136 | loc, SourceMgr::DK_Error, |
| 137 | "\"" + err.getMessage() + "\" did not contain expected substring \"" + |
| 138 | matches[1] + "\""); |
| 139 | opt_result = OptFailure; |
Jacques Pienaar | b2ddbb6 | 2018-06-26 08:56:55 -0700 | [diff] [blame] | 140 | return; |
Jacques Pienaar | ca4c4a0 | 2018-06-25 08:10:46 -0700 | [diff] [blame] | 141 | } |
| 142 | }; |
| 143 | |
| 144 | for (auto& subbuffer : sourceBuffers) { |
Jacques Pienaar | b2ddbb6 | 2018-06-26 08:56:55 -0700 | [diff] [blame] | 145 | SourceMgr sourceMgr; |
| 146 | // Tell sourceMgr about this buffer, which is what the parser will pick up. |
| 147 | sourceMgr.AddNewSourceBuffer(MemoryBuffer::getMemBufferCopy(subbuffer), |
| 148 | SMLoc()); |
| 149 | |
Jacques Pienaar | ca4c4a0 | 2018-06-25 08:10:46 -0700 | [diff] [blame] | 150 | int expectedCount = subbuffer.count("expected-error"); |
| 151 | if (expectedCount > 1) { |
Jacques Pienaar | b2ddbb6 | 2018-06-26 08:56:55 -0700 | [diff] [blame] | 152 | size_t expectedOffset = subbuffer.find("expected-error"); |
| 153 | expectedOffset = subbuffer.find("expected-error", expectedOffset); |
| 154 | SMLoc loc = SMLoc::getFromPointer(fileOffset + expectedOffset); |
| 155 | fileSourceMgr.PrintMessage(loc, SourceMgr::DK_Error, |
| 156 | "too many errors expected: unable to verify " |
| 157 | "more than one error per group"); |
| 158 | fileOffset += subbuffer.size() + strlen(marker); |
Jacques Pienaar | 7b82970 | 2018-07-03 13:24:09 -0700 | [diff] [blame] | 159 | opt_result = OptFailure; |
Jacques Pienaar | ca4c4a0 | 2018-06-25 08:10:46 -0700 | [diff] [blame] | 160 | continue; |
| 161 | } |
| 162 | |
Jacques Pienaar | b2ddbb6 | 2018-06-26 08:56:55 -0700 | [diff] [blame] | 163 | // Parse the input file. |
| 164 | MLIRContext context; |
| 165 | std::unique_ptr<Module> module( |
| 166 | parseSourceFile(sourceMgr, &context, checker)); |
| 167 | bool parsed = module != nullptr; |
Jacques Pienaar | ca4c4a0 | 2018-06-25 08:10:46 -0700 | [diff] [blame] | 168 | if (parsed && expectedCount != 0) { |
Jacques Pienaar | b2ddbb6 | 2018-06-26 08:56:55 -0700 | [diff] [blame] | 169 | llvm::Regex expected(".*expected-error {{(.*)}}"); |
Jacques Pienaar | ca4c4a0 | 2018-06-25 08:10:46 -0700 | [diff] [blame] | 170 | SmallVector<StringRef, 2> matches; |
| 171 | expected.match(subbuffer, &matches); |
Jacques Pienaar | b2ddbb6 | 2018-06-26 08:56:55 -0700 | [diff] [blame] | 172 | |
| 173 | // Highlight expected-error clause of unexpectedly passing test case. |
| 174 | size_t expectedOffset = subbuffer.find("expected-error"); |
| 175 | size_t endOffset = matches[0].size(); |
| 176 | SMLoc loc = SMLoc::getFromPointer(fileOffset + expectedOffset); |
| 177 | SMRange range(loc, SMLoc::getFromPointer(fileOffset + endOffset)); |
| 178 | fileSourceMgr.PrintMessage( |
| 179 | loc, SourceMgr::DK_Error, |
| 180 | "expected error \"" + matches[1] + "\" was not produced", range); |
Jacques Pienaar | 7b82970 | 2018-07-03 13:24:09 -0700 | [diff] [blame] | 181 | opt_result = OptFailure; |
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 | } |