blob: f1ccddd3afb335483697c94aed1e27c6827ce3ff [file] [log] [blame]
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -07001//===- 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 Lattnerf7e22732018-06-22 22:03:48 -070024#include "mlir/IR/MLIRContext.h"
Chris Lattnere2259872018-06-21 15:22:42 -070025#include "mlir/IR/Module.h"
Chris Lattnere79379a2018-06-22 10:39:19 -070026#include "mlir/Parser.h"
Tatiana Shpeisman6708b452018-07-24 10:15:13 -070027#include "mlir/Transforms/ConvertToCFG.h"
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -070028#include "llvm/Support/CommandLine.h"
Chris Lattnere2259872018-06-21 15:22:42 -070029#include "llvm/Support/FileUtilities.h"
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -070030#include "llvm/Support/InitLLVM.h"
Jacques Pienaarca4c4a02018-06-25 08:10:46 -070031#include "llvm/Support/Regex.h"
Jacques Pienaar39ffa102018-07-07 19:12:22 -070032#include "llvm/Support/SourceMgr.h"
Chris Lattnere2259872018-06-21 15:22:42 -070033#include "llvm/Support/ToolOutputFile.h"
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -070034using namespace mlir;
Chris Lattnere2259872018-06-21 15:22:42 -070035using namespace llvm;
36
37static cl::opt<std::string>
38inputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
39
40static cl::opt<std::string>
41outputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
42 cl::init("-"));
43
Jacques Pienaarbae40512018-06-24 09:10:36 -070044static cl::opt<bool>
45checkParserErrors("check-parser-errors", cl::desc("Check for parser errors"),
46 cl::init(false));
Chris Lattnere2259872018-06-21 15:22:42 -070047
Tatiana Shpeisman6708b452018-07-24 10:15:13 -070048static cl::opt<bool> convertToCFGOpt(
49 "convert-to-cfg",
50 cl::desc("Convert all ML functions in the module to CFG ones"));
51
Jacques Pienaar7b829702018-07-03 13:24:09 -070052enum OptResult { OptSuccess, OptFailure };
53
Chris Lattnere2259872018-06-21 15:22:42 -070054/// Open the specified output file and return it, exiting if there is any I/O or
55/// other errors.
56static std::unique_ptr<ToolOutputFile> getOutputStream() {
57 std::error_code error;
58 auto result = make_unique<ToolOutputFile>(outputFilename, error,
59 sys::fs::F_None);
60 if (error) {
61 llvm::errs() << error.message() << '\n';
62 exit(1);
63 }
64
65 return result;
66}
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -070067
Jacques Pienaarbae40512018-06-24 09:10:36 -070068/// Parses the memory buffer and, if successfully parsed, prints the parsed
Tatiana Shpeisman6708b452018-07-24 10:15:13 -070069/// output. Optionally, convert ML functions into CFG functions.
70/// TODO: pull parsing and printing into separate functions.
Jacques Pienaar7b829702018-07-03 13:24:09 -070071OptResult parseAndPrintMemoryBuffer(std::unique_ptr<MemoryBuffer> buffer) {
Jacques Pienaarbae40512018-06-24 09:10:36 -070072 // Tell sourceMgr about this buffer, which is what the parser will pick up.
73 SourceMgr sourceMgr;
74 sourceMgr.AddNewSourceBuffer(std::move(buffer), SMLoc());
75
Jacques Pienaar9c411be2018-06-24 19:17:35 -070076 // Parse the input file.
Jacques Pienaarbae40512018-06-24 09:10:36 -070077 MLIRContext context;
Jacques Pienaar7b829702018-07-03 13:24:09 -070078 std::unique_ptr<Module> module(parseSourceFile(sourceMgr, &context));
79 if (!module)
80 return OptFailure;
Jacques Pienaarbae40512018-06-24 09:10:36 -070081
Tatiana Shpeisman6708b452018-07-24 10:15:13 -070082 // Convert ML functions into CFG functions
83 if (convertToCFGOpt)
84 convertToCFG(module.get());
85
Jacques Pienaarbae40512018-06-24 09:10:36 -070086 // Print the output.
87 auto output = getOutputStream();
88 module->print(output->os());
89 output->keep();
90
Jacques Pienaar7b829702018-07-03 13:24:09 -070091 return OptSuccess;
Jacques Pienaarbae40512018-06-24 09:10:36 -070092}
93
94/// Split the memory buffer into multiple buffers using the marker -----.
Jacques Pienaar7b829702018-07-03 13:24:09 -070095OptResult
96splitMemoryBufferForErrorChecking(std::unique_ptr<MemoryBuffer> buffer) {
Jacques Pienaarbae40512018-06-24 09:10:36 -070097 const char marker[] = "-----";
98 SmallVector<StringRef, 2> sourceBuffers;
99 buffer->getBuffer().split(sourceBuffers, marker);
Jacques Pienaarbae40512018-06-24 09:10:36 -0700100
Jacques Pienaarca4c4a02018-06-25 08:10:46 -0700101 // Error reporter that verifies error reports matches expected error
102 // substring.
103 // TODO: Only checking for error cases below. Could be expanded to other kinds
104 // of diagnostics.
105 // TODO: Enable specifying errors on different lines (@-1).
106 // TODO: Currently only checking if substring matches, enable regex checking.
Jacques Pienaar7b829702018-07-03 13:24:09 -0700107 OptResult opt_result = OptSuccess;
Jacques Pienaarb2ddbb62018-06-26 08:56:55 -0700108 SourceMgr fileSourceMgr;
109 fileSourceMgr.AddNewSourceBuffer(std::move(buffer), SMLoc());
110
Jacques Pienaar39ffa102018-07-07 19:12:22 -0700111 // Record the expected errors's position, substring and whether it was seen.
112 struct ExpectedError {
113 int lineNo;
114 StringRef substring;
115 SMLoc fileLoc;
116 bool matched;
117 };
118
Jacques Pienaarb2ddbb62018-06-26 08:56:55 -0700119 // Tracks offset of subbuffer into original buffer.
120 const char *fileOffset =
121 fileSourceMgr.getMemoryBuffer(fileSourceMgr.getMainFileID())
122 ->getBufferStart();
123
Jacques Pienaar39ffa102018-07-07 19:12:22 -0700124 for (auto &subbuffer : sourceBuffers) {
Jacques Pienaarb2ddbb62018-06-26 08:56:55 -0700125 SourceMgr sourceMgr;
126 // Tell sourceMgr about this buffer, which is what the parser will pick up.
127 sourceMgr.AddNewSourceBuffer(MemoryBuffer::getMemBufferCopy(subbuffer),
128 SMLoc());
129
Jacques Pienaar39ffa102018-07-07 19:12:22 -0700130 // Extracing the expected errors.
James Molloy61a656c2018-07-22 15:45:24 -0700131 llvm::Regex expected("expected-error(@[+-][0-9]+)? *{{(.*)}}");
Jacques Pienaar39ffa102018-07-07 19:12:22 -0700132 SmallVector<ExpectedError, 2> expectedErrors;
133 SmallVector<StringRef, 100> lines;
134 subbuffer.split(lines, '\n');
135 size_t bufOffset = 0;
136 for (int lineNo = 0; lineNo < lines.size(); ++lineNo) {
137 SmallVector<StringRef, 3> matches;
138 if (expected.match(lines[lineNo], &matches)) {
139 // Point to the start of expected-error.
140 SMLoc errorStart =
141 SMLoc::getFromPointer(fileOffset + bufOffset +
142 lines[lineNo].size() - matches[2].size() - 2);
143 ExpectedError expErr{lineNo + 1, matches[2], errorStart, false};
144 int offset;
145 if (!matches[1].empty() &&
146 !matches[1].drop_front().getAsInteger(0, offset)) {
147 expErr.lineNo += offset;
148 }
149 expectedErrors.push_back(expErr);
150 }
151 bufOffset += lines[lineNo].size() + 1;
Jacques Pienaarca4c4a02018-06-25 08:10:46 -0700152 }
153
Jacques Pienaar39ffa102018-07-07 19:12:22 -0700154 // Error checker that verifies reported error was expected.
155 auto checker = [&](const SMDiagnostic &err) {
156 for (auto &e : expectedErrors) {
157 if (err.getLineNo() == e.lineNo &&
158 err.getMessage().contains(e.substring)) {
159 e.matched = true;
160 return;
161 }
162 }
163 // Report error if no match found.
164 const auto &sourceMgr = *err.getSourceMgr();
165 const char *bufferStart =
166 sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID())
167 ->getBufferStart();
168
169 size_t offset = err.getLoc().getPointer() - bufferStart;
170 SMLoc loc = SMLoc::getFromPointer(fileOffset + offset);
171 fileSourceMgr.PrintMessage(loc, SourceMgr::DK_Error,
172 "unexpected error: " + err.getMessage());
173 opt_result = OptFailure;
174 };
175
Jacques Pienaarb2ddbb62018-06-26 08:56:55 -0700176 // Parse the input file.
177 MLIRContext context;
178 std::unique_ptr<Module> module(
179 parseSourceFile(sourceMgr, &context, checker));
Jacques Pienaarb2ddbb62018-06-26 08:56:55 -0700180
Jacques Pienaar39ffa102018-07-07 19:12:22 -0700181 // Verify that all expected errors were seen.
182 for (auto err : expectedErrors) {
183 if (!err.matched) {
184 SMRange range(err.fileLoc,
185 SMLoc::getFromPointer(err.fileLoc.getPointer() +
186 err.substring.size()));
187 fileSourceMgr.PrintMessage(
188 err.fileLoc, SourceMgr::DK_Error,
189 "expected error \"" + err.substring + "\" was not produced", range);
190 opt_result = OptFailure;
191 }
Jacques Pienaarca4c4a02018-06-25 08:10:46 -0700192 }
Jacques Pienaarb2ddbb62018-06-26 08:56:55 -0700193
194 fileOffset += subbuffer.size() + strlen(marker);
Jacques Pienaarca4c4a02018-06-25 08:10:46 -0700195 }
196
Jacques Pienaar7b829702018-07-03 13:24:09 -0700197 return opt_result;
Jacques Pienaarbae40512018-06-24 09:10:36 -0700198}
199
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -0700200int main(int argc, char **argv) {
Chris Lattnere2259872018-06-21 15:22:42 -0700201 InitLLVM x(argc, argv);
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -0700202
Chris Lattnere2259872018-06-21 15:22:42 -0700203 cl::ParseCommandLineOptions(argc, argv, "MLIR modular optimizer driver\n");
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -0700204
Chris Lattnere79379a2018-06-22 10:39:19 -0700205 // Set up the input file.
206 auto fileOrErr = MemoryBuffer::getFileOrSTDIN(inputFilename);
207 if (std::error_code error = fileOrErr.getError()) {
208 llvm::errs() << argv[0] << ": could not open input file '" << inputFilename
209 << "': " << error.message() << "\n";
210 return 1;
211 }
212
Jacques Pienaarbae40512018-06-24 09:10:36 -0700213 if (checkParserErrors)
Jacques Pienaar7b829702018-07-03 13:24:09 -0700214 return splitMemoryBufferForErrorChecking(std::move(*fileOrErr));
Jacques Pienaarca4c4a02018-06-25 08:10:46 -0700215
Jacques Pienaar7b829702018-07-03 13:24:09 -0700216 return parseAndPrintMemoryBuffer(std::move(*fileOrErr));
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -0700217}