blob: 3c3bb164a7aa484487a066a5279fa5261be7ca7d [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.
109
110 for (auto &block : fn) {
Chris Lattner40746442018-07-21 14:32:09 -0700111 if (verifyBlock(block))
112 return true;
Chris Lattner21e67f62018-07-06 10:46:19 -0700113 }
Chris Lattner40746442018-07-21 14:32:09 -0700114 return false;
Chris Lattner21e67f62018-07-06 10:46:19 -0700115}
116
Chris Lattner40746442018-07-21 14:32:09 -0700117bool CFGFuncVerifier::verifyBlock(const BasicBlock &block) {
Chris Lattner21e67f62018-07-06 10:46:19 -0700118 if (!block.getTerminator())
Chris Lattner40746442018-07-21 14:32:09 -0700119 return failure("basic block with no terminator", block);
120
121 if (verifyTerminator(*block.getTerminator()))
122 return true;
Chris Lattner21e67f62018-07-06 10:46:19 -0700123
124 for (auto &inst : block) {
Chris Lattner40746442018-07-21 14:32:09 -0700125 if (verifyOperation(inst))
126 return true;
Chris Lattner21e67f62018-07-06 10:46:19 -0700127 }
Chris Lattner40746442018-07-21 14:32:09 -0700128 return false;
Chris Lattner21e67f62018-07-06 10:46:19 -0700129}
130
Chris Lattner40746442018-07-21 14:32:09 -0700131bool CFGFuncVerifier::verifyTerminator(const TerminatorInst &term) {
Chris Lattner21e67f62018-07-06 10:46:19 -0700132 if (term.getFunction() != &fn)
Chris Lattner40746442018-07-21 14:32:09 -0700133 return failure("terminator in the wrong function", term);
Chris Lattner21e67f62018-07-06 10:46:19 -0700134
135 // TODO: Check that operands are structurally ok.
136 // TODO: Check that successors are in the right function.
Chris Lattner40746442018-07-21 14:32:09 -0700137
138 if (auto *ret = dyn_cast<ReturnInst>(&term))
139 return verifyReturn(*ret);
140
141 return false;
Chris Lattner21e67f62018-07-06 10:46:19 -0700142}
143
Chris Lattner40746442018-07-21 14:32:09 -0700144bool CFGFuncVerifier::verifyReturn(const ReturnInst &inst) {
145 // Verify that the return operands match the results of the function.
146 auto results = fn.getType()->getResults();
147 if (inst.getNumOperands() != results.size())
148 return failure("return has " + Twine(inst.getNumOperands()) +
149 " operands, but enclosing function returns " +
150 Twine(results.size()),
151 inst);
152
153 return false;
154}
155
156bool CFGFuncVerifier::verifyOperation(const OperationInst &inst) {
Chris Lattner21e67f62018-07-06 10:46:19 -0700157 if (inst.getFunction() != &fn)
Chris Lattner40746442018-07-21 14:32:09 -0700158 return failure("operation in the wrong function", inst);
Chris Lattner21e67f62018-07-06 10:46:19 -0700159
160 // TODO: Check that operands are structurally ok.
161
162 // See if we can get operation info for this.
163 if (auto *opInfo = inst.getAbstractOperation(fn.getContext())) {
164 if (auto errorMessage = opInfo->verifyInvariants(&inst))
Chris Lattner40746442018-07-21 14:32:09 -0700165 return failure(errorMessage, inst);
Chris Lattner21e67f62018-07-06 10:46:19 -0700166 }
Chris Lattner40746442018-07-21 14:32:09 -0700167
168 return false;
Chris Lattner21e67f62018-07-06 10:46:19 -0700169}
170
171//===----------------------------------------------------------------------===//
172// ML Functions
173//===----------------------------------------------------------------------===//
174
175namespace {
Chris Lattner40746442018-07-21 14:32:09 -0700176class MLFuncVerifier : public Verifier {
Chris Lattner21e67f62018-07-06 10:46:19 -0700177public:
178 const MLFunction &fn;
179
Chris Lattner40746442018-07-21 14:32:09 -0700180 MLFuncVerifier(const MLFunction &fn, std::string *errorResult)
181 : Verifier(errorResult), fn(fn) {}
Chris Lattner21e67f62018-07-06 10:46:19 -0700182
Chris Lattner40746442018-07-21 14:32:09 -0700183 bool verify() {
Chris Lattner21e67f62018-07-06 10:46:19 -0700184 // TODO.
Chris Lattner40746442018-07-21 14:32:09 -0700185 return false;
Chris Lattner21e67f62018-07-06 10:46:19 -0700186 }
187};
188} // end anonymous namespace
189
190//===----------------------------------------------------------------------===//
191// Entrypoints
192//===----------------------------------------------------------------------===//
193
194/// Perform (potentially expensive) checks of invariants, used to detect
Chris Lattner40746442018-07-21 14:32:09 -0700195/// compiler bugs. On error, this fills in the string and return true,
196/// or aborts if the string was not provided.
197bool Function::verify(std::string *errorResult) const {
Chris Lattner21e67f62018-07-06 10:46:19 -0700198 switch (getKind()) {
199 case Kind::ExtFunc:
200 // No body, nothing can be wrong here.
Chris Lattner40746442018-07-21 14:32:09 -0700201 return false;
Chris Lattner21e67f62018-07-06 10:46:19 -0700202 case Kind::CFGFunc:
Chris Lattner40746442018-07-21 14:32:09 -0700203 return CFGFuncVerifier(*cast<CFGFunction>(this), errorResult).verify();
Chris Lattner21e67f62018-07-06 10:46:19 -0700204 case Kind::MLFunc:
Chris Lattner40746442018-07-21 14:32:09 -0700205 return MLFuncVerifier(*cast<MLFunction>(this), errorResult).verify();
Chris Lattner21e67f62018-07-06 10:46:19 -0700206 }
207}
208
209/// Perform (potentially expensive) checks of invariants, used to detect
Chris Lattner40746442018-07-21 14:32:09 -0700210/// compiler bugs. On error, this fills in the string and return true,
211/// or aborts if the string was not provided.
212bool Module::verify(std::string *errorResult) const {
213
Chris Lattner21e67f62018-07-06 10:46:19 -0700214 /// Check that each function is correct.
215 for (auto fn : functionList) {
Chris Lattner40746442018-07-21 14:32:09 -0700216 if (fn->verify(errorResult))
217 return true;
Chris Lattner21e67f62018-07-06 10:46:19 -0700218 }
Chris Lattner40746442018-07-21 14:32:09 -0700219
220 // Make sure the error string is empty on success.
221 if (errorResult)
222 errorResult->clear();
223 return false;
Chris Lattner21e67f62018-07-06 10:46:19 -0700224}