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 | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame^] | 24 | #include "mlir/IR/Attributes.h" |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 25 | #include "mlir/IR/MLFunction.h" |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 26 | #include "mlir/IR/MLIRContext.h" |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 27 | #include "mlir/IR/Module.h" |
Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 28 | #include "mlir/IR/Pass.h" |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 29 | #include "mlir/Parser.h" |
Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 30 | #include "mlir/TensorFlow/ControlFlowOps.h" |
| 31 | #include "mlir/TensorFlow/Passes.h" |
| 32 | #include "mlir/Transforms/Passes.h" |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 33 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 34 | #include "llvm/Support/FileUtilities.h" |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 35 | #include "llvm/Support/InitLLVM.h" |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame^] | 36 | #include "llvm/Support/PrettyStackTrace.h" |
Jacques Pienaar | ca4c4a0 | 2018-06-25 08:10:46 -0700 | [diff] [blame] | 37 | #include "llvm/Support/Regex.h" |
Jacques Pienaar | 39ffa10 | 2018-07-07 19:12:22 -0700 | [diff] [blame] | 38 | #include "llvm/Support/SourceMgr.h" |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 39 | #include "llvm/Support/ToolOutputFile.h" |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 40 | using namespace mlir; |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 41 | using namespace llvm; |
| 42 | |
| 43 | static cl::opt<std::string> |
| 44 | inputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-")); |
| 45 | |
| 46 | static cl::opt<std::string> |
| 47 | outputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), |
| 48 | cl::init("-")); |
| 49 | |
Jacques Pienaar | bae4051 | 2018-06-24 09:10:36 -0700 | [diff] [blame] | 50 | static cl::opt<bool> |
| 51 | checkParserErrors("check-parser-errors", cl::desc("Check for parser errors"), |
| 52 | cl::init(false)); |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 53 | |
Chris Lattner | dc3ba38 | 2018-07-29 14:13:03 -0700 | [diff] [blame] | 54 | enum Passes { |
| 55 | ConvertToCFG, |
| 56 | UnrollInnermostLoops, |
| 57 | TFRaiseControlFlow, |
| 58 | }; |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 59 | |
Chris Lattner | dc3ba38 | 2018-07-29 14:13:03 -0700 | [diff] [blame] | 60 | static cl::list<Passes> passList( |
| 61 | "", cl::desc("Compiler passes to run"), |
| 62 | cl::values(clEnumValN(ConvertToCFG, "convert-to-cfg", |
| 63 | "Convert all ML functions in the module to CFG ones"), |
| 64 | clEnumValN(UnrollInnermostLoops, "unroll-innermost-loops", |
| 65 | "Unroll innermost loops"), |
| 66 | clEnumValN(TFRaiseControlFlow, "tf-raise-control-flow", |
| 67 | "Dynamic TensorFlow Switch/Match nodes to a CFG"))); |
Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 68 | |
Jacques Pienaar | 7b82970 | 2018-07-03 13:24:09 -0700 | [diff] [blame] | 69 | enum OptResult { OptSuccess, OptFailure }; |
| 70 | |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 71 | /// Open the specified output file and return it, exiting if there is any I/O or |
| 72 | /// other errors. |
| 73 | static std::unique_ptr<ToolOutputFile> getOutputStream() { |
| 74 | std::error_code error; |
MLIR Team | 61eadaa | 2018-07-30 15:00:47 -0700 | [diff] [blame] | 75 | auto result = |
| 76 | llvm::make_unique<ToolOutputFile>(outputFilename, error, sys::fs::F_None); |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 77 | if (error) { |
| 78 | llvm::errs() << error.message() << '\n'; |
| 79 | exit(1); |
| 80 | } |
| 81 | |
| 82 | return result; |
| 83 | } |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 84 | |
Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 85 | static void initializeMLIRContext(MLIRContext &ctx) { |
| 86 | TFControlFlow::registerOperations(ctx); |
| 87 | } |
| 88 | |
Jacques Pienaar | bae4051 | 2018-06-24 09:10:36 -0700 | [diff] [blame] | 89 | /// Parses the memory buffer and, if successfully parsed, prints the parsed |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 90 | /// output. Optionally, convert ML functions into CFG functions. |
| 91 | /// TODO: pull parsing and printing into separate functions. |
Jacques Pienaar | 7b82970 | 2018-07-03 13:24:09 -0700 | [diff] [blame] | 92 | OptResult parseAndPrintMemoryBuffer(std::unique_ptr<MemoryBuffer> buffer) { |
Jacques Pienaar | bae4051 | 2018-06-24 09:10:36 -0700 | [diff] [blame] | 93 | // Tell sourceMgr about this buffer, which is what the parser will pick up. |
| 94 | SourceMgr sourceMgr; |
| 95 | sourceMgr.AddNewSourceBuffer(std::move(buffer), SMLoc()); |
| 96 | |
Jacques Pienaar | 9c411be | 2018-06-24 19:17:35 -0700 | [diff] [blame] | 97 | // Parse the input file. |
Jacques Pienaar | bae4051 | 2018-06-24 09:10:36 -0700 | [diff] [blame] | 98 | MLIRContext context; |
Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 99 | initializeMLIRContext(context); |
Jacques Pienaar | 7b82970 | 2018-07-03 13:24:09 -0700 | [diff] [blame] | 100 | std::unique_ptr<Module> module(parseSourceFile(sourceMgr, &context)); |
| 101 | if (!module) |
| 102 | return OptFailure; |
Jacques Pienaar | bae4051 | 2018-06-24 09:10:36 -0700 | [diff] [blame] | 103 | |
Chris Lattner | dc3ba38 | 2018-07-29 14:13:03 -0700 | [diff] [blame] | 104 | // Run each of the passes that were selected. |
| 105 | for (auto passKind : passList) { |
| 106 | Pass *pass = nullptr; |
| 107 | switch (passKind) { |
| 108 | case ConvertToCFG: |
| 109 | pass = createConvertToCFGPass(); |
| 110 | break; |
| 111 | case UnrollInnermostLoops: |
| 112 | pass = createLoopUnrollPass(); |
| 113 | break; |
| 114 | case TFRaiseControlFlow: |
| 115 | pass = createRaiseTFControlFlowPass(); |
| 116 | break; |
| 117 | } |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 118 | |
Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 119 | pass->runOnModule(module.get()); |
| 120 | delete pass; |
| 121 | module->verify(); |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Jacques Pienaar | bae4051 | 2018-06-24 09:10:36 -0700 | [diff] [blame] | 124 | // Print the output. |
| 125 | auto output = getOutputStream(); |
| 126 | module->print(output->os()); |
| 127 | output->keep(); |
| 128 | |
Jacques Pienaar | 7b82970 | 2018-07-03 13:24:09 -0700 | [diff] [blame] | 129 | return OptSuccess; |
Jacques Pienaar | bae4051 | 2018-06-24 09:10:36 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /// Split the memory buffer into multiple buffers using the marker -----. |
Jacques Pienaar | 7b82970 | 2018-07-03 13:24:09 -0700 | [diff] [blame] | 133 | OptResult |
| 134 | splitMemoryBufferForErrorChecking(std::unique_ptr<MemoryBuffer> buffer) { |
Jacques Pienaar | bae4051 | 2018-06-24 09:10:36 -0700 | [diff] [blame] | 135 | const char marker[] = "-----"; |
| 136 | SmallVector<StringRef, 2> sourceBuffers; |
| 137 | buffer->getBuffer().split(sourceBuffers, marker); |
Jacques Pienaar | bae4051 | 2018-06-24 09:10:36 -0700 | [diff] [blame] | 138 | |
Jacques Pienaar | ca4c4a0 | 2018-06-25 08:10:46 -0700 | [diff] [blame] | 139 | // Error reporter that verifies error reports matches expected error |
| 140 | // substring. |
| 141 | // TODO: Only checking for error cases below. Could be expanded to other kinds |
| 142 | // of diagnostics. |
| 143 | // TODO: Enable specifying errors on different lines (@-1). |
| 144 | // TODO: Currently only checking if substring matches, enable regex checking. |
Jacques Pienaar | 7b82970 | 2018-07-03 13:24:09 -0700 | [diff] [blame] | 145 | OptResult opt_result = OptSuccess; |
Jacques Pienaar | b2ddbb6 | 2018-06-26 08:56:55 -0700 | [diff] [blame] | 146 | SourceMgr fileSourceMgr; |
| 147 | fileSourceMgr.AddNewSourceBuffer(std::move(buffer), SMLoc()); |
| 148 | |
Jacques Pienaar | 39ffa10 | 2018-07-07 19:12:22 -0700 | [diff] [blame] | 149 | // Record the expected errors's position, substring and whether it was seen. |
| 150 | struct ExpectedError { |
| 151 | int lineNo; |
| 152 | StringRef substring; |
| 153 | SMLoc fileLoc; |
| 154 | bool matched; |
| 155 | }; |
| 156 | |
Jacques Pienaar | b2ddbb6 | 2018-06-26 08:56:55 -0700 | [diff] [blame] | 157 | // Tracks offset of subbuffer into original buffer. |
| 158 | const char *fileOffset = |
| 159 | fileSourceMgr.getMemoryBuffer(fileSourceMgr.getMainFileID()) |
| 160 | ->getBufferStart(); |
| 161 | |
Jacques Pienaar | 39ffa10 | 2018-07-07 19:12:22 -0700 | [diff] [blame] | 162 | for (auto &subbuffer : sourceBuffers) { |
Jacques Pienaar | b2ddbb6 | 2018-06-26 08:56:55 -0700 | [diff] [blame] | 163 | SourceMgr sourceMgr; |
| 164 | // Tell sourceMgr about this buffer, which is what the parser will pick up. |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame^] | 165 | auto bufferId = sourceMgr.AddNewSourceBuffer( |
| 166 | MemoryBuffer::getMemBufferCopy(subbuffer), SMLoc()); |
Jacques Pienaar | b2ddbb6 | 2018-06-26 08:56:55 -0700 | [diff] [blame] | 167 | |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame^] | 168 | // Extract the expected errors. |
James Molloy | 61a656c | 2018-07-22 15:45:24 -0700 | [diff] [blame] | 169 | llvm::Regex expected("expected-error(@[+-][0-9]+)? *{{(.*)}}"); |
Jacques Pienaar | 39ffa10 | 2018-07-07 19:12:22 -0700 | [diff] [blame] | 170 | SmallVector<ExpectedError, 2> expectedErrors; |
| 171 | SmallVector<StringRef, 100> lines; |
| 172 | subbuffer.split(lines, '\n'); |
| 173 | size_t bufOffset = 0; |
| 174 | for (int lineNo = 0; lineNo < lines.size(); ++lineNo) { |
| 175 | SmallVector<StringRef, 3> matches; |
| 176 | if (expected.match(lines[lineNo], &matches)) { |
| 177 | // Point to the start of expected-error. |
| 178 | SMLoc errorStart = |
| 179 | SMLoc::getFromPointer(fileOffset + bufOffset + |
| 180 | lines[lineNo].size() - matches[2].size() - 2); |
| 181 | ExpectedError expErr{lineNo + 1, matches[2], errorStart, false}; |
| 182 | int offset; |
| 183 | if (!matches[1].empty() && |
| 184 | !matches[1].drop_front().getAsInteger(0, offset)) { |
| 185 | expErr.lineNo += offset; |
| 186 | } |
| 187 | expectedErrors.push_back(expErr); |
| 188 | } |
| 189 | bufOffset += lines[lineNo].size() + 1; |
Jacques Pienaar | ca4c4a0 | 2018-06-25 08:10:46 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Jacques Pienaar | 39ffa10 | 2018-07-07 19:12:22 -0700 | [diff] [blame] | 192 | // Error checker that verifies reported error was expected. |
| 193 | auto checker = [&](const SMDiagnostic &err) { |
| 194 | for (auto &e : expectedErrors) { |
| 195 | if (err.getLineNo() == e.lineNo && |
| 196 | err.getMessage().contains(e.substring)) { |
| 197 | e.matched = true; |
| 198 | return; |
| 199 | } |
| 200 | } |
| 201 | // Report error if no match found. |
| 202 | const auto &sourceMgr = *err.getSourceMgr(); |
| 203 | const char *bufferStart = |
| 204 | sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID()) |
| 205 | ->getBufferStart(); |
| 206 | |
| 207 | size_t offset = err.getLoc().getPointer() - bufferStart; |
| 208 | SMLoc loc = SMLoc::getFromPointer(fileOffset + offset); |
| 209 | fileSourceMgr.PrintMessage(loc, SourceMgr::DK_Error, |
| 210 | "unexpected error: " + err.getMessage()); |
| 211 | opt_result = OptFailure; |
| 212 | }; |
| 213 | |
Jacques Pienaar | b2ddbb6 | 2018-06-26 08:56:55 -0700 | [diff] [blame] | 214 | // Parse the input file. |
| 215 | MLIRContext context; |
Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 216 | initializeMLIRContext(context); |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame^] | 217 | |
| 218 | // TODO: refactor into initializeMLIRContext so the normal parser pass |
| 219 | // gets to use this. |
| 220 | context.registerDiagnosticHandler([&](Attribute *location, |
| 221 | StringRef message, |
| 222 | MLIRContext::DiagnosticKind kind) { |
| 223 | auto offset = cast<IntegerAttr>(location)->getValue(); |
| 224 | auto ptr = sourceMgr.getMemoryBuffer(bufferId)->getBufferStart() + offset; |
| 225 | SourceMgr::DiagKind diagKind; |
| 226 | switch (kind) { |
| 227 | case MLIRContext::DiagnosticKind::Error: |
| 228 | diagKind = SourceMgr::DK_Error; |
| 229 | break; |
| 230 | case MLIRContext::DiagnosticKind::Warning: |
| 231 | diagKind = SourceMgr::DK_Warning; |
| 232 | break; |
| 233 | case MLIRContext::DiagnosticKind::Note: |
| 234 | diagKind = SourceMgr::DK_Note; |
| 235 | break; |
| 236 | } |
| 237 | checker( |
| 238 | sourceMgr.GetMessage(SMLoc::getFromPointer(ptr), diagKind, message)); |
| 239 | }); |
| 240 | |
Jacques Pienaar | b2ddbb6 | 2018-06-26 08:56:55 -0700 | [diff] [blame] | 241 | std::unique_ptr<Module> module( |
| 242 | parseSourceFile(sourceMgr, &context, checker)); |
Jacques Pienaar | b2ddbb6 | 2018-06-26 08:56:55 -0700 | [diff] [blame] | 243 | |
Jacques Pienaar | 39ffa10 | 2018-07-07 19:12:22 -0700 | [diff] [blame] | 244 | // Verify that all expected errors were seen. |
| 245 | for (auto err : expectedErrors) { |
| 246 | if (!err.matched) { |
| 247 | SMRange range(err.fileLoc, |
| 248 | SMLoc::getFromPointer(err.fileLoc.getPointer() + |
| 249 | err.substring.size())); |
| 250 | fileSourceMgr.PrintMessage( |
| 251 | err.fileLoc, SourceMgr::DK_Error, |
| 252 | "expected error \"" + err.substring + "\" was not produced", range); |
| 253 | opt_result = OptFailure; |
| 254 | } |
Jacques Pienaar | ca4c4a0 | 2018-06-25 08:10:46 -0700 | [diff] [blame] | 255 | } |
Jacques Pienaar | b2ddbb6 | 2018-06-26 08:56:55 -0700 | [diff] [blame] | 256 | |
| 257 | fileOffset += subbuffer.size() + strlen(marker); |
Jacques Pienaar | ca4c4a0 | 2018-06-25 08:10:46 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Jacques Pienaar | 7b82970 | 2018-07-03 13:24:09 -0700 | [diff] [blame] | 260 | return opt_result; |
Jacques Pienaar | bae4051 | 2018-06-24 09:10:36 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 263 | int main(int argc, char **argv) { |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame^] | 264 | llvm::PrettyStackTraceProgram x(argc, argv); |
| 265 | InitLLVM y(argc, argv); |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 266 | |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 267 | cl::ParseCommandLineOptions(argc, argv, "MLIR modular optimizer driver\n"); |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 268 | |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 269 | // Set up the input file. |
| 270 | auto fileOrErr = MemoryBuffer::getFileOrSTDIN(inputFilename); |
| 271 | if (std::error_code error = fileOrErr.getError()) { |
| 272 | llvm::errs() << argv[0] << ": could not open input file '" << inputFilename |
| 273 | << "': " << error.message() << "\n"; |
| 274 | return 1; |
| 275 | } |
| 276 | |
Jacques Pienaar | bae4051 | 2018-06-24 09:10:36 -0700 | [diff] [blame] | 277 | if (checkParserErrors) |
Jacques Pienaar | 7b82970 | 2018-07-03 13:24:09 -0700 | [diff] [blame] | 278 | return splitMemoryBufferForErrorChecking(std::move(*fileOrErr)); |
Jacques Pienaar | ca4c4a0 | 2018-06-25 08:10:46 -0700 | [diff] [blame] | 279 | |
Jacques Pienaar | 7b82970 | 2018-07-03 13:24:09 -0700 | [diff] [blame] | 280 | return parseAndPrintMemoryBuffer(std::move(*fileOrErr)); |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 281 | } |