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