blob: 6eb7a68f4740ad55f676812dabb38ca44a3c4e09 [file] [log] [blame]
Chris Lattner21e67f62018-07-06 10:46:19 -07001//===- Verifier.cpp - MLIR Verifier Implementation ------------------------===//
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 file implements the verify() methods on the various IR types, performing
19// (potentially expensive) checks on the holistic structure of the code. This
20// can be used for detecting bugs in compiler transformations and hand written
21// .mlir files.
22//
23// The checks in this file are only for things that can occur as part of IR
24// transformations: e.g. violation of dominance information, malformed operation
25// attributes, etc. MLIR supports transformations moving IR through locally
26// invalid states (e.g. unlinking an instruction from an instruction before
27// re-inserting it in a new place), but each transformation must complete with
28// the IR in a valid form.
29//
30// This should not check for things that are always wrong by construction (e.g.
31// affine maps or other immutable structures that are incorrect), because those
32// are not mutable and can be checked at time of construction.
33//
34//===----------------------------------------------------------------------===//
35
36#include "mlir/IR/CFGFunction.h"
37#include "mlir/IR/MLFunction.h"
38#include "mlir/IR/Module.h"
39#include "mlir/IR/OperationSet.h"
40#include "llvm/ADT/Twine.h"
41#include "llvm/Support/raw_ostream.h"
42using namespace mlir;
43
Chris Lattner40746442018-07-21 14:32:09 -070044namespace {
45/// Base class for the verifiers in this file. It is a pervasive truth that
46/// this file treats "true" as an error that needs to be recovered from, and
47/// "false" as success.
48///
49class Verifier {
50public:
51 template <typename T>
52 static void failure(const Twine &message, const T &value, raw_ostream &os) {
53 // Print the error message and flush the stream in case printing the value
54 // causes a crash.
55 os << "MLIR verification failure: " + message + "\n";
56 os.flush();
57 value.print(os);
58 }
59
60 template <typename T>
61 bool failure(const Twine &message, const T &value) {
62 // If the caller isn't trying to collect failure information, just print
63 // the result and abort.
64 if (!errorResult) {
65 failure(message, value, llvm::errs());
66 abort();
67 }
68
69 // Otherwise, emit the error into the string and return true.
70 llvm::raw_string_ostream os(*errorResult);
71 failure(message, value, os);
72 os.flush();
73 return true;
74 }
75
Chris Lattner95865062018-08-01 10:18:59 -070076 bool opFailure(const Twine &message, const Operation &value) {
77 value.emitError(message);
78 return true;
79 }
80
Chris Lattner40746442018-07-21 14:32:09 -070081protected:
82 explicit Verifier(std::string *errorResult) : errorResult(errorResult) {}
83
84private:
85 std::string *errorResult;
86};
87} // end anonymous namespace
Chris Lattner21e67f62018-07-06 10:46:19 -070088
89//===----------------------------------------------------------------------===//
90// CFG Functions
91//===----------------------------------------------------------------------===//
92
93namespace {
Chris Lattner40746442018-07-21 14:32:09 -070094class CFGFuncVerifier : public Verifier {
Chris Lattner21e67f62018-07-06 10:46:19 -070095public:
96 const CFGFunction &fn;
97 OperationSet &operationSet;
98
Chris Lattner40746442018-07-21 14:32:09 -070099 CFGFuncVerifier(const CFGFunction &fn, std::string *errorResult)
100 : Verifier(errorResult), fn(fn),
101 operationSet(OperationSet::get(fn.getContext())) {}
Chris Lattner21e67f62018-07-06 10:46:19 -0700102
Chris Lattner40746442018-07-21 14:32:09 -0700103 bool verify();
104 bool verifyBlock(const BasicBlock &block);
105 bool verifyOperation(const OperationInst &inst);
106 bool verifyTerminator(const TerminatorInst &term);
107 bool verifyReturn(const ReturnInst &inst);
Chris Lattner1604e472018-07-23 08:42:19 -0700108 bool verifyBranch(const BranchInst &inst);
James Molloy4f788372018-07-24 15:01:27 -0700109 bool verifyCondBranch(const CondBranchInst &inst);
110
111 // Given a list of "operands" and "arguments" that are the same length, verify
112 // that the types of operands pointwise match argument types. The iterator
113 // types must expose the "getType()" function when dereferenced twice; that
114 // is, the iterator's value_type must be equivalent to SSAValue*.
115 template <typename OperandIteratorTy, typename ArgumentIteratorTy>
116 bool verifyOperandsMatchArguments(OperandIteratorTy opBegin,
117 OperandIteratorTy opEnd,
118 ArgumentIteratorTy argBegin,
119 const Instruction &instContext);
Chris Lattner21e67f62018-07-06 10:46:19 -0700120};
121} // end anonymous namespace
122
Chris Lattner40746442018-07-21 14:32:09 -0700123bool CFGFuncVerifier::verify() {
Chris Lattner21e67f62018-07-06 10:46:19 -0700124 // TODO: Lots to be done here, including verifying dominance information when
125 // we have uses and defs.
James Molloy61a656c2018-07-22 15:45:24 -0700126 // TODO: Verify the first block has no predecessors.
127
128 if (fn.empty())
129 return failure("cfgfunc must have at least one basic block", fn);
130
131 // Verify that the argument list of the function and the arg list of the first
132 // block line up.
133 auto *firstBB = &fn.front();
134 auto fnInputTypes = fn.getType()->getInputs();
135 if (fnInputTypes.size() != firstBB->getNumArguments())
136 return failure("first block of cfgfunc must have " +
137 Twine(fnInputTypes.size()) +
138 " arguments to match function signature",
139 fn);
140 for (unsigned i = 0, e = firstBB->getNumArguments(); i != e; ++i)
141 if (fnInputTypes[i] != firstBB->getArgument(i)->getType())
142 return failure(
143 "type of argument #" + Twine(i) +
144 " must match corresponding argument in function signature",
145 fn);
Chris Lattner21e67f62018-07-06 10:46:19 -0700146
147 for (auto &block : fn) {
Chris Lattner40746442018-07-21 14:32:09 -0700148 if (verifyBlock(block))
149 return true;
Chris Lattner21e67f62018-07-06 10:46:19 -0700150 }
Chris Lattner40746442018-07-21 14:32:09 -0700151 return false;
Chris Lattner21e67f62018-07-06 10:46:19 -0700152}
153
Chris Lattner40746442018-07-21 14:32:09 -0700154bool CFGFuncVerifier::verifyBlock(const BasicBlock &block) {
Chris Lattner21e67f62018-07-06 10:46:19 -0700155 if (!block.getTerminator())
Chris Lattner40746442018-07-21 14:32:09 -0700156 return failure("basic block with no terminator", block);
157
158 if (verifyTerminator(*block.getTerminator()))
159 return true;
Chris Lattner21e67f62018-07-06 10:46:19 -0700160
James Molloy61a656c2018-07-22 15:45:24 -0700161 for (auto *arg : block.getArguments()) {
162 if (arg->getOwner() != &block)
163 return failure("basic block argument not owned by block", block);
164 }
165
Chris Lattner21e67f62018-07-06 10:46:19 -0700166 for (auto &inst : block) {
Chris Lattner40746442018-07-21 14:32:09 -0700167 if (verifyOperation(inst))
168 return true;
Chris Lattner21e67f62018-07-06 10:46:19 -0700169 }
Chris Lattner40746442018-07-21 14:32:09 -0700170 return false;
Chris Lattner21e67f62018-07-06 10:46:19 -0700171}
172
Chris Lattner40746442018-07-21 14:32:09 -0700173bool CFGFuncVerifier::verifyTerminator(const TerminatorInst &term) {
Chris Lattner21e67f62018-07-06 10:46:19 -0700174 if (term.getFunction() != &fn)
Chris Lattner40746442018-07-21 14:32:09 -0700175 return failure("terminator in the wrong function", term);
Chris Lattner21e67f62018-07-06 10:46:19 -0700176
177 // TODO: Check that operands are structurally ok.
178 // TODO: Check that successors are in the right function.
Chris Lattner40746442018-07-21 14:32:09 -0700179
180 if (auto *ret = dyn_cast<ReturnInst>(&term))
181 return verifyReturn(*ret);
182
Chris Lattner1604e472018-07-23 08:42:19 -0700183 if (auto *br = dyn_cast<BranchInst>(&term))
184 return verifyBranch(*br);
185
James Molloy4f788372018-07-24 15:01:27 -0700186 if (auto *br = dyn_cast<CondBranchInst>(&term))
187 return verifyCondBranch(*br);
188
Chris Lattner40746442018-07-21 14:32:09 -0700189 return false;
Chris Lattner21e67f62018-07-06 10:46:19 -0700190}
191
Chris Lattner40746442018-07-21 14:32:09 -0700192bool CFGFuncVerifier::verifyReturn(const ReturnInst &inst) {
193 // Verify that the return operands match the results of the function.
194 auto results = fn.getType()->getResults();
195 if (inst.getNumOperands() != results.size())
196 return failure("return has " + Twine(inst.getNumOperands()) +
197 " operands, but enclosing function returns " +
198 Twine(results.size()),
199 inst);
200
Chris Lattner1604e472018-07-23 08:42:19 -0700201 for (unsigned i = 0, e = results.size(); i != e; ++i)
202 if (inst.getOperand(i)->getType() != results[i])
203 return failure("type of return operand " + Twine(i) +
204 " doesn't match result function result type",
205 inst);
206
207 return false;
208}
209
210bool CFGFuncVerifier::verifyBranch(const BranchInst &inst) {
211 // Verify that the number of operands lines up with the number of BB arguments
212 // in the successor.
213 auto dest = inst.getDest();
214 if (inst.getNumOperands() != dest->getNumArguments())
215 return failure("branch has " + Twine(inst.getNumOperands()) +
216 " operands, but target block has " +
217 Twine(dest->getNumArguments()),
218 inst);
219
220 for (unsigned i = 0, e = inst.getNumOperands(); i != e; ++i)
221 if (inst.getOperand(i)->getType() != dest->getArgument(i)->getType())
222 return failure("type of branch operand " + Twine(i) +
223 " doesn't match target bb argument type",
224 inst);
225
Chris Lattner40746442018-07-21 14:32:09 -0700226 return false;
227}
228
James Molloy4f788372018-07-24 15:01:27 -0700229template <typename OperandIteratorTy, typename ArgumentIteratorTy>
230bool CFGFuncVerifier::verifyOperandsMatchArguments(
231 OperandIteratorTy opBegin, OperandIteratorTy opEnd,
232 ArgumentIteratorTy argBegin, const Instruction &instContext) {
233 OperandIteratorTy opIt = opBegin;
234 ArgumentIteratorTy argIt = argBegin;
235 for (; opIt != opEnd; ++opIt, ++argIt) {
236 if ((*opIt)->getType() != (*argIt)->getType())
237 return failure("type of operand " + Twine(std::distance(opBegin, opIt)) +
238 " doesn't match argument type",
239 instContext);
240 }
241 return false;
242}
243
244bool CFGFuncVerifier::verifyCondBranch(const CondBranchInst &inst) {
245 // Verify that the number of operands lines up with the number of BB arguments
246 // in the true successor.
247 auto trueDest = inst.getTrueDest();
248 if (inst.getNumTrueOperands() != trueDest->getNumArguments())
249 return failure("branch has " + Twine(inst.getNumTrueOperands()) +
250 " true operands, but true target block has " +
251 Twine(trueDest->getNumArguments()),
252 inst);
253
254 if (verifyOperandsMatchArguments(inst.true_operand_begin(),
255 inst.true_operand_end(),
256 trueDest->args_begin(), inst))
257 return true;
258
259 // And the false successor.
260 auto falseDest = inst.getFalseDest();
261 if (inst.getNumFalseOperands() != falseDest->getNumArguments())
262 return failure("branch has " + Twine(inst.getNumFalseOperands()) +
263 " false operands, but false target block has " +
264 Twine(falseDest->getNumArguments()),
265 inst);
266
267 if (verifyOperandsMatchArguments(inst.false_operand_begin(),
268 inst.false_operand_end(),
269 falseDest->args_begin(), inst))
270 return true;
271
272 if (inst.getCondition()->getType() != Type::getInteger(1, fn.getContext()))
273 return failure("type of condition is not boolean (i1)", inst);
274
275 return false;
276}
277
Chris Lattner40746442018-07-21 14:32:09 -0700278bool CFGFuncVerifier::verifyOperation(const OperationInst &inst) {
Chris Lattner21e67f62018-07-06 10:46:19 -0700279 if (inst.getFunction() != &fn)
Chris Lattner95865062018-08-01 10:18:59 -0700280 return opFailure("operation in the wrong function", inst);
Chris Lattner21e67f62018-07-06 10:46:19 -0700281
282 // TODO: Check that operands are structurally ok.
283
284 // See if we can get operation info for this.
Chris Lattner95865062018-08-01 10:18:59 -0700285 if (auto *opInfo = inst.getAbstractOperation()) {
Chris Lattner21e67f62018-07-06 10:46:19 -0700286 if (auto errorMessage = opInfo->verifyInvariants(&inst))
Chris Lattner95865062018-08-01 10:18:59 -0700287 return opFailure(
288 Twine("'") + inst.getName().str() + "' op " + errorMessage, inst);
Chris Lattner21e67f62018-07-06 10:46:19 -0700289 }
Chris Lattner40746442018-07-21 14:32:09 -0700290
291 return false;
Chris Lattner21e67f62018-07-06 10:46:19 -0700292}
293
294//===----------------------------------------------------------------------===//
295// ML Functions
296//===----------------------------------------------------------------------===//
297
298namespace {
Chris Lattner40746442018-07-21 14:32:09 -0700299class MLFuncVerifier : public Verifier {
Chris Lattner21e67f62018-07-06 10:46:19 -0700300public:
301 const MLFunction &fn;
302
Chris Lattner40746442018-07-21 14:32:09 -0700303 MLFuncVerifier(const MLFunction &fn, std::string *errorResult)
304 : Verifier(errorResult), fn(fn) {}
Chris Lattner21e67f62018-07-06 10:46:19 -0700305
Chris Lattner40746442018-07-21 14:32:09 -0700306 bool verify() {
Chris Lattner21e67f62018-07-06 10:46:19 -0700307 // TODO.
Chris Lattner40746442018-07-21 14:32:09 -0700308 return false;
Chris Lattner21e67f62018-07-06 10:46:19 -0700309 }
310};
311} // end anonymous namespace
312
313//===----------------------------------------------------------------------===//
314// Entrypoints
315//===----------------------------------------------------------------------===//
316
317/// Perform (potentially expensive) checks of invariants, used to detect
Chris Lattner40746442018-07-21 14:32:09 -0700318/// compiler bugs. On error, this fills in the string and return true,
319/// or aborts if the string was not provided.
320bool Function::verify(std::string *errorResult) const {
Chris Lattner21e67f62018-07-06 10:46:19 -0700321 switch (getKind()) {
322 case Kind::ExtFunc:
323 // No body, nothing can be wrong here.
Chris Lattner40746442018-07-21 14:32:09 -0700324 return false;
Chris Lattner21e67f62018-07-06 10:46:19 -0700325 case Kind::CFGFunc:
Chris Lattner40746442018-07-21 14:32:09 -0700326 return CFGFuncVerifier(*cast<CFGFunction>(this), errorResult).verify();
Chris Lattner21e67f62018-07-06 10:46:19 -0700327 case Kind::MLFunc:
Chris Lattner40746442018-07-21 14:32:09 -0700328 return MLFuncVerifier(*cast<MLFunction>(this), errorResult).verify();
Chris Lattner21e67f62018-07-06 10:46:19 -0700329 }
330}
331
332/// Perform (potentially expensive) checks of invariants, used to detect
Chris Lattner40746442018-07-21 14:32:09 -0700333/// compiler bugs. On error, this fills in the string and return true,
334/// or aborts if the string was not provided.
335bool Module::verify(std::string *errorResult) const {
336
Chris Lattner21e67f62018-07-06 10:46:19 -0700337 /// Check that each function is correct.
Chris Lattnera8e47672018-07-25 14:08:16 -0700338 for (auto &fn : *this) {
339 if (fn.verify(errorResult))
Chris Lattner40746442018-07-21 14:32:09 -0700340 return true;
Chris Lattner21e67f62018-07-06 10:46:19 -0700341 }
Chris Lattner40746442018-07-21 14:32:09 -0700342
343 // Make sure the error string is empty on success.
344 if (errorResult)
345 errorResult->clear();
346 return false;
Chris Lattner21e67f62018-07-06 10:46:19 -0700347}