Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 1 | //===- 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" |
| 42 | using namespace mlir; |
| 43 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 44 | namespace { |
| 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 | /// |
| 49 | class Verifier { |
| 50 | public: |
| 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 | |
| 76 | protected: |
| 77 | explicit Verifier(std::string *errorResult) : errorResult(errorResult) {} |
| 78 | |
| 79 | private: |
| 80 | std::string *errorResult; |
| 81 | }; |
| 82 | } // end anonymous namespace |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 83 | |
| 84 | //===----------------------------------------------------------------------===// |
| 85 | // CFG Functions |
| 86 | //===----------------------------------------------------------------------===// |
| 87 | |
| 88 | namespace { |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 89 | class CFGFuncVerifier : public Verifier { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 90 | public: |
| 91 | const CFGFunction &fn; |
| 92 | OperationSet &operationSet; |
| 93 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 94 | CFGFuncVerifier(const CFGFunction &fn, std::string *errorResult) |
| 95 | : Verifier(errorResult), fn(fn), |
| 96 | operationSet(OperationSet::get(fn.getContext())) {} |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 97 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 98 | 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 Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 103 | }; |
| 104 | } // end anonymous namespace |
| 105 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 106 | bool CFGFuncVerifier::verify() { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 107 | // TODO: Lots to be done here, including verifying dominance information when |
| 108 | // we have uses and defs. |
James Molloy | 61a656c | 2018-07-22 15:45:24 -0700 | [diff] [blame] | 109 | // 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 Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 129 | |
| 130 | for (auto &block : fn) { |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 131 | if (verifyBlock(block)) |
| 132 | return true; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 133 | } |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 134 | return false; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 137 | bool CFGFuncVerifier::verifyBlock(const BasicBlock &block) { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 138 | if (!block.getTerminator()) |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 139 | return failure("basic block with no terminator", block); |
| 140 | |
| 141 | if (verifyTerminator(*block.getTerminator())) |
| 142 | return true; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 143 | |
James Molloy | 61a656c | 2018-07-22 15:45:24 -0700 | [diff] [blame] | 144 | for (auto *arg : block.getArguments()) { |
| 145 | if (arg->getOwner() != &block) |
| 146 | return failure("basic block argument not owned by block", block); |
| 147 | } |
| 148 | |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 149 | for (auto &inst : block) { |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 150 | if (verifyOperation(inst)) |
| 151 | return true; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 152 | } |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 153 | return false; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 156 | bool CFGFuncVerifier::verifyTerminator(const TerminatorInst &term) { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 157 | if (term.getFunction() != &fn) |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 158 | return failure("terminator in the wrong function", term); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 159 | |
| 160 | // TODO: Check that operands are structurally ok. |
| 161 | // TODO: Check that successors are in the right function. |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 162 | |
| 163 | if (auto *ret = dyn_cast<ReturnInst>(&term)) |
| 164 | return verifyReturn(*ret); |
| 165 | |
| 166 | return false; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 169 | bool 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 | |
| 181 | bool CFGFuncVerifier::verifyOperation(const OperationInst &inst) { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 182 | if (inst.getFunction() != &fn) |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 183 | return failure("operation in the wrong function", inst); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 184 | |
| 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 Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 190 | return failure(errorMessage, inst); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 191 | } |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 192 | |
| 193 | return false; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | //===----------------------------------------------------------------------===// |
| 197 | // ML Functions |
| 198 | //===----------------------------------------------------------------------===// |
| 199 | |
| 200 | namespace { |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 201 | class MLFuncVerifier : public Verifier { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 202 | public: |
| 203 | const MLFunction &fn; |
| 204 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 205 | MLFuncVerifier(const MLFunction &fn, std::string *errorResult) |
| 206 | : Verifier(errorResult), fn(fn) {} |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 207 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 208 | bool verify() { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 209 | // TODO. |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 210 | return false; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 211 | } |
| 212 | }; |
| 213 | } // end anonymous namespace |
| 214 | |
| 215 | //===----------------------------------------------------------------------===// |
| 216 | // Entrypoints |
| 217 | //===----------------------------------------------------------------------===// |
| 218 | |
| 219 | /// Perform (potentially expensive) checks of invariants, used to detect |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 220 | /// compiler bugs. On error, this fills in the string and return true, |
| 221 | /// or aborts if the string was not provided. |
| 222 | bool Function::verify(std::string *errorResult) const { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 223 | switch (getKind()) { |
| 224 | case Kind::ExtFunc: |
| 225 | // No body, nothing can be wrong here. |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 226 | return false; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 227 | case Kind::CFGFunc: |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 228 | return CFGFuncVerifier(*cast<CFGFunction>(this), errorResult).verify(); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 229 | case Kind::MLFunc: |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 230 | return MLFuncVerifier(*cast<MLFunction>(this), errorResult).verify(); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
| 234 | /// Perform (potentially expensive) checks of invariants, used to detect |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 235 | /// compiler bugs. On error, this fills in the string and return true, |
| 236 | /// or aborts if the string was not provided. |
| 237 | bool Module::verify(std::string *errorResult) const { |
| 238 | |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 239 | /// Check that each function is correct. |
| 240 | for (auto fn : functionList) { |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 241 | if (fn->verify(errorResult)) |
| 242 | return true; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 243 | } |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 244 | |
| 245 | // Make sure the error string is empty on success. |
| 246 | if (errorResult) |
| 247 | errorResult->clear(); |
| 248 | return false; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 249 | } |