blob: d1bb2ac5e29bfc7313520c20d0f55bea326bba21 [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
76protected:
77 explicit Verifier(std::string *errorResult) : errorResult(errorResult) {}
78
79private:
80 std::string *errorResult;
81};
82} // end anonymous namespace
Chris Lattner21e67f62018-07-06 10:46:19 -070083
84//===----------------------------------------------------------------------===//
85// CFG Functions
86//===----------------------------------------------------------------------===//
87
88namespace {
Chris Lattner40746442018-07-21 14:32:09 -070089class CFGFuncVerifier : public Verifier {
Chris Lattner21e67f62018-07-06 10:46:19 -070090public:
91 const CFGFunction &fn;
92 OperationSet &operationSet;
93
Chris Lattner40746442018-07-21 14:32:09 -070094 CFGFuncVerifier(const CFGFunction &fn, std::string *errorResult)
95 : Verifier(errorResult), fn(fn),
96 operationSet(OperationSet::get(fn.getContext())) {}
Chris Lattner21e67f62018-07-06 10:46:19 -070097
Chris Lattner40746442018-07-21 14:32:09 -070098 bool verify();
99 bool verifyBlock(const BasicBlock &block);
100 bool verifyOperation(const OperationInst &inst);
101 bool verifyTerminator(const TerminatorInst &term);
102 bool verifyReturn(const ReturnInst &inst);
Chris Lattner21e67f62018-07-06 10:46:19 -0700103};
104} // end anonymous namespace
105
Chris Lattner40746442018-07-21 14:32:09 -0700106bool CFGFuncVerifier::verify() {
Chris Lattner21e67f62018-07-06 10:46:19 -0700107 // TODO: Lots to be done here, including verifying dominance information when
108 // we have uses and defs.
James Molloy61a656c2018-07-22 15:45:24 -0700109 // TODO: Verify the first block has no predecessors.
110
111 if (fn.empty())
112 return failure("cfgfunc must have at least one basic block", fn);
113
114 // Verify that the argument list of the function and the arg list of the first
115 // block line up.
116 auto *firstBB = &fn.front();
117 auto fnInputTypes = fn.getType()->getInputs();
118 if (fnInputTypes.size() != firstBB->getNumArguments())
119 return failure("first block of cfgfunc must have " +
120 Twine(fnInputTypes.size()) +
121 " arguments to match function signature",
122 fn);
123 for (unsigned i = 0, e = firstBB->getNumArguments(); i != e; ++i)
124 if (fnInputTypes[i] != firstBB->getArgument(i)->getType())
125 return failure(
126 "type of argument #" + Twine(i) +
127 " must match corresponding argument in function signature",
128 fn);
Chris Lattner21e67f62018-07-06 10:46:19 -0700129
130 for (auto &block : fn) {
Chris Lattner40746442018-07-21 14:32:09 -0700131 if (verifyBlock(block))
132 return true;
Chris Lattner21e67f62018-07-06 10:46:19 -0700133 }
Chris Lattner40746442018-07-21 14:32:09 -0700134 return false;
Chris Lattner21e67f62018-07-06 10:46:19 -0700135}
136
Chris Lattner40746442018-07-21 14:32:09 -0700137bool CFGFuncVerifier::verifyBlock(const BasicBlock &block) {
Chris Lattner21e67f62018-07-06 10:46:19 -0700138 if (!block.getTerminator())
Chris Lattner40746442018-07-21 14:32:09 -0700139 return failure("basic block with no terminator", block);
140
141 if (verifyTerminator(*block.getTerminator()))
142 return true;
Chris Lattner21e67f62018-07-06 10:46:19 -0700143
James Molloy61a656c2018-07-22 15:45:24 -0700144 for (auto *arg : block.getArguments()) {
145 if (arg->getOwner() != &block)
146 return failure("basic block argument not owned by block", block);
147 }
148
Chris Lattner21e67f62018-07-06 10:46:19 -0700149 for (auto &inst : block) {
Chris Lattner40746442018-07-21 14:32:09 -0700150 if (verifyOperation(inst))
151 return true;
Chris Lattner21e67f62018-07-06 10:46:19 -0700152 }
Chris Lattner40746442018-07-21 14:32:09 -0700153 return false;
Chris Lattner21e67f62018-07-06 10:46:19 -0700154}
155
Chris Lattner40746442018-07-21 14:32:09 -0700156bool CFGFuncVerifier::verifyTerminator(const TerminatorInst &term) {
Chris Lattner21e67f62018-07-06 10:46:19 -0700157 if (term.getFunction() != &fn)
Chris Lattner40746442018-07-21 14:32:09 -0700158 return failure("terminator in the wrong function", term);
Chris Lattner21e67f62018-07-06 10:46:19 -0700159
160 // TODO: Check that operands are structurally ok.
161 // TODO: Check that successors are in the right function.
Chris Lattner40746442018-07-21 14:32:09 -0700162
163 if (auto *ret = dyn_cast<ReturnInst>(&term))
164 return verifyReturn(*ret);
165
166 return false;
Chris Lattner21e67f62018-07-06 10:46:19 -0700167}
168
Chris Lattner40746442018-07-21 14:32:09 -0700169bool CFGFuncVerifier::verifyReturn(const ReturnInst &inst) {
170 // Verify that the return operands match the results of the function.
171 auto results = fn.getType()->getResults();
172 if (inst.getNumOperands() != results.size())
173 return failure("return has " + Twine(inst.getNumOperands()) +
174 " operands, but enclosing function returns " +
175 Twine(results.size()),
176 inst);
177
178 return false;
179}
180
181bool CFGFuncVerifier::verifyOperation(const OperationInst &inst) {
Chris Lattner21e67f62018-07-06 10:46:19 -0700182 if (inst.getFunction() != &fn)
Chris Lattner40746442018-07-21 14:32:09 -0700183 return failure("operation in the wrong function", inst);
Chris Lattner21e67f62018-07-06 10:46:19 -0700184
185 // TODO: Check that operands are structurally ok.
186
187 // See if we can get operation info for this.
188 if (auto *opInfo = inst.getAbstractOperation(fn.getContext())) {
189 if (auto errorMessage = opInfo->verifyInvariants(&inst))
Chris Lattner40746442018-07-21 14:32:09 -0700190 return failure(errorMessage, inst);
Chris Lattner21e67f62018-07-06 10:46:19 -0700191 }
Chris Lattner40746442018-07-21 14:32:09 -0700192
193 return false;
Chris Lattner21e67f62018-07-06 10:46:19 -0700194}
195
196//===----------------------------------------------------------------------===//
197// ML Functions
198//===----------------------------------------------------------------------===//
199
200namespace {
Chris Lattner40746442018-07-21 14:32:09 -0700201class MLFuncVerifier : public Verifier {
Chris Lattner21e67f62018-07-06 10:46:19 -0700202public:
203 const MLFunction &fn;
204
Chris Lattner40746442018-07-21 14:32:09 -0700205 MLFuncVerifier(const MLFunction &fn, std::string *errorResult)
206 : Verifier(errorResult), fn(fn) {}
Chris Lattner21e67f62018-07-06 10:46:19 -0700207
Chris Lattner40746442018-07-21 14:32:09 -0700208 bool verify() {
Chris Lattner21e67f62018-07-06 10:46:19 -0700209 // TODO.
Chris Lattner40746442018-07-21 14:32:09 -0700210 return false;
Chris Lattner21e67f62018-07-06 10:46:19 -0700211 }
212};
213} // end anonymous namespace
214
215//===----------------------------------------------------------------------===//
216// Entrypoints
217//===----------------------------------------------------------------------===//
218
219/// Perform (potentially expensive) checks of invariants, used to detect
Chris Lattner40746442018-07-21 14:32:09 -0700220/// compiler bugs. On error, this fills in the string and return true,
221/// or aborts if the string was not provided.
222bool Function::verify(std::string *errorResult) const {
Chris Lattner21e67f62018-07-06 10:46:19 -0700223 switch (getKind()) {
224 case Kind::ExtFunc:
225 // No body, nothing can be wrong here.
Chris Lattner40746442018-07-21 14:32:09 -0700226 return false;
Chris Lattner21e67f62018-07-06 10:46:19 -0700227 case Kind::CFGFunc:
Chris Lattner40746442018-07-21 14:32:09 -0700228 return CFGFuncVerifier(*cast<CFGFunction>(this), errorResult).verify();
Chris Lattner21e67f62018-07-06 10:46:19 -0700229 case Kind::MLFunc:
Chris Lattner40746442018-07-21 14:32:09 -0700230 return MLFuncVerifier(*cast<MLFunction>(this), errorResult).verify();
Chris Lattner21e67f62018-07-06 10:46:19 -0700231 }
232}
233
234/// Perform (potentially expensive) checks of invariants, used to detect
Chris Lattner40746442018-07-21 14:32:09 -0700235/// compiler bugs. On error, this fills in the string and return true,
236/// or aborts if the string was not provided.
237bool Module::verify(std::string *errorResult) const {
238
Chris Lattner21e67f62018-07-06 10:46:19 -0700239 /// Check that each function is correct.
240 for (auto fn : functionList) {
Chris Lattner40746442018-07-21 14:32:09 -0700241 if (fn->verify(errorResult))
242 return true;
Chris Lattner21e67f62018-07-06 10:46:19 -0700243 }
Chris Lattner40746442018-07-21 14:32:09 -0700244
245 // Make sure the error string is empty on success.
246 if (errorResult)
247 errorResult->clear();
248 return false;
Chris Lattner21e67f62018-07-06 10:46:19 -0700249}