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 | |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame^] | 76 | bool opFailure(const Twine &message, const Operation &value) { |
| 77 | value.emitError(message); |
| 78 | return true; |
| 79 | } |
| 80 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 81 | protected: |
| 82 | explicit Verifier(std::string *errorResult) : errorResult(errorResult) {} |
| 83 | |
| 84 | private: |
| 85 | std::string *errorResult; |
| 86 | }; |
| 87 | } // end anonymous namespace |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 88 | |
| 89 | //===----------------------------------------------------------------------===// |
| 90 | // CFG Functions |
| 91 | //===----------------------------------------------------------------------===// |
| 92 | |
| 93 | namespace { |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 94 | class CFGFuncVerifier : public Verifier { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 95 | public: |
| 96 | const CFGFunction &fn; |
| 97 | OperationSet &operationSet; |
| 98 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 99 | CFGFuncVerifier(const CFGFunction &fn, std::string *errorResult) |
| 100 | : Verifier(errorResult), fn(fn), |
| 101 | operationSet(OperationSet::get(fn.getContext())) {} |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 102 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 103 | 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 Lattner | 1604e47 | 2018-07-23 08:42:19 -0700 | [diff] [blame] | 108 | bool verifyBranch(const BranchInst &inst); |
James Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 109 | 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 Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 120 | }; |
| 121 | } // end anonymous namespace |
| 122 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 123 | bool CFGFuncVerifier::verify() { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 124 | // TODO: Lots to be done here, including verifying dominance information when |
| 125 | // we have uses and defs. |
James Molloy | 61a656c | 2018-07-22 15:45:24 -0700 | [diff] [blame] | 126 | // 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 Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 146 | |
| 147 | for (auto &block : fn) { |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 148 | if (verifyBlock(block)) |
| 149 | return true; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 150 | } |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 151 | return false; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 154 | bool CFGFuncVerifier::verifyBlock(const BasicBlock &block) { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 155 | if (!block.getTerminator()) |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 156 | return failure("basic block with no terminator", block); |
| 157 | |
| 158 | if (verifyTerminator(*block.getTerminator())) |
| 159 | return true; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 160 | |
James Molloy | 61a656c | 2018-07-22 15:45:24 -0700 | [diff] [blame] | 161 | for (auto *arg : block.getArguments()) { |
| 162 | if (arg->getOwner() != &block) |
| 163 | return failure("basic block argument not owned by block", block); |
| 164 | } |
| 165 | |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 166 | for (auto &inst : block) { |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 167 | if (verifyOperation(inst)) |
| 168 | return true; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 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::verifyTerminator(const TerminatorInst &term) { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 174 | if (term.getFunction() != &fn) |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 175 | return failure("terminator in the wrong function", term); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 176 | |
| 177 | // TODO: Check that operands are structurally ok. |
| 178 | // TODO: Check that successors are in the right function. |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 179 | |
| 180 | if (auto *ret = dyn_cast<ReturnInst>(&term)) |
| 181 | return verifyReturn(*ret); |
| 182 | |
Chris Lattner | 1604e47 | 2018-07-23 08:42:19 -0700 | [diff] [blame] | 183 | if (auto *br = dyn_cast<BranchInst>(&term)) |
| 184 | return verifyBranch(*br); |
| 185 | |
James Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 186 | if (auto *br = dyn_cast<CondBranchInst>(&term)) |
| 187 | return verifyCondBranch(*br); |
| 188 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 189 | return false; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 192 | bool 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 Lattner | 1604e47 | 2018-07-23 08:42:19 -0700 | [diff] [blame] | 201 | 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 | |
| 210 | bool 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 Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 226 | return false; |
| 227 | } |
| 228 | |
James Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 229 | template <typename OperandIteratorTy, typename ArgumentIteratorTy> |
| 230 | bool 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 | |
| 244 | bool 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 Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 278 | bool CFGFuncVerifier::verifyOperation(const OperationInst &inst) { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 279 | if (inst.getFunction() != &fn) |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame^] | 280 | return opFailure("operation in the wrong function", inst); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 281 | |
| 282 | // TODO: Check that operands are structurally ok. |
| 283 | |
| 284 | // See if we can get operation info for this. |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame^] | 285 | if (auto *opInfo = inst.getAbstractOperation()) { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 286 | if (auto errorMessage = opInfo->verifyInvariants(&inst)) |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame^] | 287 | return opFailure( |
| 288 | Twine("'") + inst.getName().str() + "' op " + errorMessage, inst); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 289 | } |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 290 | |
| 291 | return false; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | //===----------------------------------------------------------------------===// |
| 295 | // ML Functions |
| 296 | //===----------------------------------------------------------------------===// |
| 297 | |
| 298 | namespace { |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 299 | class MLFuncVerifier : public Verifier { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 300 | public: |
| 301 | const MLFunction &fn; |
| 302 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 303 | MLFuncVerifier(const MLFunction &fn, std::string *errorResult) |
| 304 | : Verifier(errorResult), fn(fn) {} |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 305 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 306 | bool verify() { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 307 | // TODO. |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 308 | return false; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 309 | } |
| 310 | }; |
| 311 | } // end anonymous namespace |
| 312 | |
| 313 | //===----------------------------------------------------------------------===// |
| 314 | // Entrypoints |
| 315 | //===----------------------------------------------------------------------===// |
| 316 | |
| 317 | /// Perform (potentially expensive) checks of invariants, used to detect |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 318 | /// compiler bugs. On error, this fills in the string and return true, |
| 319 | /// or aborts if the string was not provided. |
| 320 | bool Function::verify(std::string *errorResult) const { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 321 | switch (getKind()) { |
| 322 | case Kind::ExtFunc: |
| 323 | // No body, nothing can be wrong here. |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 324 | return false; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 325 | case Kind::CFGFunc: |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 326 | return CFGFuncVerifier(*cast<CFGFunction>(this), errorResult).verify(); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 327 | case Kind::MLFunc: |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 328 | return MLFuncVerifier(*cast<MLFunction>(this), errorResult).verify(); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
| 332 | /// Perform (potentially expensive) checks of invariants, used to detect |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 333 | /// compiler bugs. On error, this fills in the string and return true, |
| 334 | /// or aborts if the string was not provided. |
| 335 | bool Module::verify(std::string *errorResult) const { |
| 336 | |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 337 | /// Check that each function is correct. |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 338 | for (auto &fn : *this) { |
| 339 | if (fn.verify(errorResult)) |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 340 | return true; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 341 | } |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 342 | |
| 343 | // Make sure the error string is empty on success. |
| 344 | if (errorResult) |
| 345 | errorResult->clear(); |
| 346 | return false; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 347 | } |