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 | 1604e47 | 2018-07-23 08:42:19 -0700 | [diff] [blame^] | 103 | bool verifyBranch(const BranchInst &inst); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 104 | }; |
| 105 | } // end anonymous namespace |
| 106 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 107 | bool CFGFuncVerifier::verify() { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 108 | // TODO: Lots to be done here, including verifying dominance information when |
| 109 | // we have uses and defs. |
James Molloy | 61a656c | 2018-07-22 15:45:24 -0700 | [diff] [blame] | 110 | // TODO: Verify the first block has no predecessors. |
| 111 | |
| 112 | if (fn.empty()) |
| 113 | return failure("cfgfunc must have at least one basic block", fn); |
| 114 | |
| 115 | // Verify that the argument list of the function and the arg list of the first |
| 116 | // block line up. |
| 117 | auto *firstBB = &fn.front(); |
| 118 | auto fnInputTypes = fn.getType()->getInputs(); |
| 119 | if (fnInputTypes.size() != firstBB->getNumArguments()) |
| 120 | return failure("first block of cfgfunc must have " + |
| 121 | Twine(fnInputTypes.size()) + |
| 122 | " arguments to match function signature", |
| 123 | fn); |
| 124 | for (unsigned i = 0, e = firstBB->getNumArguments(); i != e; ++i) |
| 125 | if (fnInputTypes[i] != firstBB->getArgument(i)->getType()) |
| 126 | return failure( |
| 127 | "type of argument #" + Twine(i) + |
| 128 | " must match corresponding argument in function signature", |
| 129 | fn); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 130 | |
| 131 | for (auto &block : fn) { |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 132 | if (verifyBlock(block)) |
| 133 | return true; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 134 | } |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 135 | return false; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 138 | bool CFGFuncVerifier::verifyBlock(const BasicBlock &block) { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 139 | if (!block.getTerminator()) |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 140 | return failure("basic block with no terminator", block); |
| 141 | |
| 142 | if (verifyTerminator(*block.getTerminator())) |
| 143 | return true; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 144 | |
James Molloy | 61a656c | 2018-07-22 15:45:24 -0700 | [diff] [blame] | 145 | for (auto *arg : block.getArguments()) { |
| 146 | if (arg->getOwner() != &block) |
| 147 | return failure("basic block argument not owned by block", block); |
| 148 | } |
| 149 | |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 150 | for (auto &inst : block) { |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 151 | if (verifyOperation(inst)) |
| 152 | return true; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 153 | } |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 154 | return false; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 157 | bool CFGFuncVerifier::verifyTerminator(const TerminatorInst &term) { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 158 | if (term.getFunction() != &fn) |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 159 | return failure("terminator in the wrong function", term); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 160 | |
| 161 | // TODO: Check that operands are structurally ok. |
| 162 | // TODO: Check that successors are in the right function. |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 163 | |
| 164 | if (auto *ret = dyn_cast<ReturnInst>(&term)) |
| 165 | return verifyReturn(*ret); |
| 166 | |
Chris Lattner | 1604e47 | 2018-07-23 08:42:19 -0700 | [diff] [blame^] | 167 | if (auto *br = dyn_cast<BranchInst>(&term)) |
| 168 | return verifyBranch(*br); |
| 169 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 170 | return false; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 173 | bool CFGFuncVerifier::verifyReturn(const ReturnInst &inst) { |
| 174 | // Verify that the return operands match the results of the function. |
| 175 | auto results = fn.getType()->getResults(); |
| 176 | if (inst.getNumOperands() != results.size()) |
| 177 | return failure("return has " + Twine(inst.getNumOperands()) + |
| 178 | " operands, but enclosing function returns " + |
| 179 | Twine(results.size()), |
| 180 | inst); |
| 181 | |
Chris Lattner | 1604e47 | 2018-07-23 08:42:19 -0700 | [diff] [blame^] | 182 | for (unsigned i = 0, e = results.size(); i != e; ++i) |
| 183 | if (inst.getOperand(i)->getType() != results[i]) |
| 184 | return failure("type of return operand " + Twine(i) + |
| 185 | " doesn't match result function result type", |
| 186 | inst); |
| 187 | |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | bool CFGFuncVerifier::verifyBranch(const BranchInst &inst) { |
| 192 | // Verify that the number of operands lines up with the number of BB arguments |
| 193 | // in the successor. |
| 194 | auto dest = inst.getDest(); |
| 195 | if (inst.getNumOperands() != dest->getNumArguments()) |
| 196 | return failure("branch has " + Twine(inst.getNumOperands()) + |
| 197 | " operands, but target block has " + |
| 198 | Twine(dest->getNumArguments()), |
| 199 | inst); |
| 200 | |
| 201 | for (unsigned i = 0, e = inst.getNumOperands(); i != e; ++i) |
| 202 | if (inst.getOperand(i)->getType() != dest->getArgument(i)->getType()) |
| 203 | return failure("type of branch operand " + Twine(i) + |
| 204 | " doesn't match target bb argument type", |
| 205 | inst); |
| 206 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 207 | return false; |
| 208 | } |
| 209 | |
| 210 | bool CFGFuncVerifier::verifyOperation(const OperationInst &inst) { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 211 | if (inst.getFunction() != &fn) |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 212 | return failure("operation in the wrong function", inst); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 213 | |
| 214 | // TODO: Check that operands are structurally ok. |
| 215 | |
| 216 | // See if we can get operation info for this. |
| 217 | if (auto *opInfo = inst.getAbstractOperation(fn.getContext())) { |
| 218 | if (auto errorMessage = opInfo->verifyInvariants(&inst)) |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 219 | return failure(errorMessage, inst); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 220 | } |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 221 | |
| 222 | return false; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | //===----------------------------------------------------------------------===// |
| 226 | // ML Functions |
| 227 | //===----------------------------------------------------------------------===// |
| 228 | |
| 229 | namespace { |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 230 | class MLFuncVerifier : public Verifier { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 231 | public: |
| 232 | const MLFunction &fn; |
| 233 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 234 | MLFuncVerifier(const MLFunction &fn, std::string *errorResult) |
| 235 | : Verifier(errorResult), fn(fn) {} |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 236 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 237 | bool verify() { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 238 | // TODO. |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 239 | return false; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 240 | } |
| 241 | }; |
| 242 | } // end anonymous namespace |
| 243 | |
| 244 | //===----------------------------------------------------------------------===// |
| 245 | // Entrypoints |
| 246 | //===----------------------------------------------------------------------===// |
| 247 | |
| 248 | /// Perform (potentially expensive) checks of invariants, used to detect |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 249 | /// compiler bugs. On error, this fills in the string and return true, |
| 250 | /// or aborts if the string was not provided. |
| 251 | bool Function::verify(std::string *errorResult) const { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 252 | switch (getKind()) { |
| 253 | case Kind::ExtFunc: |
| 254 | // No body, nothing can be wrong here. |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 255 | return false; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 256 | case Kind::CFGFunc: |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 257 | return CFGFuncVerifier(*cast<CFGFunction>(this), errorResult).verify(); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 258 | case Kind::MLFunc: |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 259 | return MLFuncVerifier(*cast<MLFunction>(this), errorResult).verify(); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
| 263 | /// Perform (potentially expensive) checks of invariants, used to detect |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 264 | /// compiler bugs. On error, this fills in the string and return true, |
| 265 | /// or aborts if the string was not provided. |
| 266 | bool Module::verify(std::string *errorResult) const { |
| 267 | |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 268 | /// Check that each function is correct. |
| 269 | for (auto fn : functionList) { |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 270 | if (fn->verify(errorResult)) |
| 271 | return true; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 272 | } |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 273 | |
| 274 | // Make sure the error string is empty on success. |
| 275 | if (errorResult) |
| 276 | errorResult->clear(); |
| 277 | return false; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 278 | } |