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 | |
Chris Lattner | ea5c3dc | 2018-08-21 08:42:19 -0700 | [diff] [blame] | 36 | #include "mlir/IR/Attributes.h" |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 37 | #include "mlir/IR/CFGFunction.h" |
| 38 | #include "mlir/IR/MLFunction.h" |
| 39 | #include "mlir/IR/Module.h" |
| 40 | #include "mlir/IR/OperationSet.h" |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 41 | #include "mlir/IR/Statements.h" |
Chris Lattner | ea5c3dc | 2018-08-21 08:42:19 -0700 | [diff] [blame] | 42 | #include "mlir/IR/StmtVisitor.h" |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 43 | #include "llvm/ADT/ScopedHashTable.h" |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 44 | #include "llvm/ADT/Twine.h" |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 45 | #include "llvm/Support/PrettyStackTrace.h" |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 46 | #include "llvm/Support/raw_ostream.h" |
| 47 | using namespace mlir; |
| 48 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 49 | namespace { |
| 50 | /// Base class for the verifiers in this file. It is a pervasive truth that |
| 51 | /// this file treats "true" as an error that needs to be recovered from, and |
| 52 | /// "false" as success. |
| 53 | /// |
| 54 | class Verifier { |
| 55 | public: |
| 56 | template <typename T> |
| 57 | static void failure(const Twine &message, const T &value, raw_ostream &os) { |
| 58 | // Print the error message and flush the stream in case printing the value |
| 59 | // causes a crash. |
| 60 | os << "MLIR verification failure: " + message + "\n"; |
| 61 | os.flush(); |
| 62 | value.print(os); |
| 63 | } |
| 64 | |
| 65 | template <typename T> |
| 66 | bool failure(const Twine &message, const T &value) { |
| 67 | // If the caller isn't trying to collect failure information, just print |
| 68 | // the result and abort. |
| 69 | if (!errorResult) { |
| 70 | failure(message, value, llvm::errs()); |
| 71 | abort(); |
| 72 | } |
| 73 | |
| 74 | // Otherwise, emit the error into the string and return true. |
| 75 | llvm::raw_string_ostream os(*errorResult); |
| 76 | failure(message, value, os); |
| 77 | os.flush(); |
| 78 | return true; |
| 79 | } |
| 80 | |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 81 | bool opFailure(const Twine &message, const Operation &value) { |
| 82 | value.emitError(message); |
| 83 | return true; |
| 84 | } |
| 85 | |
Chris Lattner | ea5c3dc | 2018-08-21 08:42:19 -0700 | [diff] [blame] | 86 | bool verifyOperation(const Operation &op); |
| 87 | bool verifyAttribute(Attribute *attr, const Operation &op); |
| 88 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 89 | protected: |
Chris Lattner | ea5c3dc | 2018-08-21 08:42:19 -0700 | [diff] [blame] | 90 | explicit Verifier(std::string *errorResult, const Function &fn) |
| 91 | : errorResult(errorResult), fn(fn), |
| 92 | operationSet(OperationSet::get(fn.getContext())) {} |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 93 | |
| 94 | private: |
Chris Lattner | ea5c3dc | 2018-08-21 08:42:19 -0700 | [diff] [blame] | 95 | /// If the verifier is returning errors back to a client, this is the error to |
| 96 | /// fill in. |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 97 | std::string *errorResult; |
Chris Lattner | ea5c3dc | 2018-08-21 08:42:19 -0700 | [diff] [blame] | 98 | |
| 99 | /// The function being checked. |
| 100 | const Function &fn; |
| 101 | |
| 102 | /// The operation set installed in the current MLIR context. |
| 103 | OperationSet &operationSet; |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 104 | }; |
| 105 | } // end anonymous namespace |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 106 | |
Chris Lattner | ea5c3dc | 2018-08-21 08:42:19 -0700 | [diff] [blame] | 107 | // Check that function attributes are all well formed. |
| 108 | bool Verifier::verifyAttribute(Attribute *attr, const Operation &op) { |
| 109 | if (!attr->isOrContainsFunction()) |
| 110 | return false; |
| 111 | |
| 112 | // If we have a function attribute, check that it is non-null and in the |
| 113 | // same module as the operation that refers to it. |
| 114 | if (auto *fnAttr = dyn_cast<FunctionAttr>(attr)) { |
| 115 | if (!fnAttr->getValue()) |
| 116 | return opFailure("attribute refers to deallocated function!", op); |
| 117 | |
| 118 | if (fnAttr->getValue()->getModule() != fn.getModule()) |
| 119 | return opFailure("attribute refers to function '" + |
| 120 | Twine(fnAttr->getValue()->getName()) + |
| 121 | "' defined in another module!", |
| 122 | op); |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | // Otherwise, we must have an array attribute, remap the elements. |
| 127 | for (auto *elt : cast<ArrayAttr>(attr)->getValue()) { |
| 128 | if (verifyAttribute(elt, op)) |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | /// Check the invariants of the specified operation instruction or statement. |
| 136 | bool Verifier::verifyOperation(const Operation &op) { |
| 137 | if (op.getOperationFunction() != &fn) |
| 138 | return opFailure("operation in the wrong function", op); |
| 139 | |
| 140 | // TODO: Check that operands are non-nil and structurally ok. |
| 141 | |
| 142 | // Verify all attributes are ok. We need to check Function attributes, since |
| 143 | // they are actually mutable (the function they refer to can be deleted), and |
| 144 | // we have to check array attributes that can refer to them. |
| 145 | for (auto attr : op.getAttrs()) { |
| 146 | if (verifyAttribute(attr.second, op)) |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | // If we can get operation info for this, check the custom hook. |
| 151 | if (auto *opInfo = op.getAbstractOperation()) { |
| 152 | if (auto *errorMessage = opInfo->verifyInvariants(&op)) |
| 153 | return opFailure(Twine("'") + op.getName().str() + "' op " + errorMessage, |
| 154 | op); |
| 155 | } |
| 156 | |
| 157 | return false; |
| 158 | } |
| 159 | |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 160 | //===----------------------------------------------------------------------===// |
| 161 | // CFG Functions |
| 162 | //===----------------------------------------------------------------------===// |
| 163 | |
| 164 | namespace { |
Chris Lattner | ea5c3dc | 2018-08-21 08:42:19 -0700 | [diff] [blame] | 165 | struct CFGFuncVerifier : public Verifier { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 166 | const CFGFunction &fn; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 167 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 168 | CFGFuncVerifier(const CFGFunction &fn, std::string *errorResult) |
Chris Lattner | ea5c3dc | 2018-08-21 08:42:19 -0700 | [diff] [blame] | 169 | : Verifier(errorResult, fn), fn(fn) {} |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 170 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 171 | bool verify(); |
| 172 | bool verifyBlock(const BasicBlock &block); |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 173 | bool verifyTerminator(const TerminatorInst &term); |
| 174 | bool verifyReturn(const ReturnInst &inst); |
Chris Lattner | 1604e47 | 2018-07-23 08:42:19 -0700 | [diff] [blame] | 175 | bool verifyBranch(const BranchInst &inst); |
James Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 176 | bool verifyCondBranch(const CondBranchInst &inst); |
| 177 | |
| 178 | // Given a list of "operands" and "arguments" that are the same length, verify |
| 179 | // that the types of operands pointwise match argument types. The iterator |
| 180 | // types must expose the "getType()" function when dereferenced twice; that |
| 181 | // is, the iterator's value_type must be equivalent to SSAValue*. |
| 182 | template <typename OperandIteratorTy, typename ArgumentIteratorTy> |
| 183 | bool verifyOperandsMatchArguments(OperandIteratorTy opBegin, |
| 184 | OperandIteratorTy opEnd, |
| 185 | ArgumentIteratorTy argBegin, |
| 186 | const Instruction &instContext); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 187 | }; |
| 188 | } // end anonymous namespace |
| 189 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 190 | bool CFGFuncVerifier::verify() { |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 191 | llvm::PrettyStackTraceFormat fmt("MLIR Verifier: cfgfunc @%s", |
| 192 | fn.getName().c_str()); |
| 193 | |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 194 | // TODO: Lots to be done here, including verifying dominance information when |
| 195 | // we have uses and defs. |
James Molloy | 61a656c | 2018-07-22 15:45:24 -0700 | [diff] [blame] | 196 | // TODO: Verify the first block has no predecessors. |
| 197 | |
| 198 | if (fn.empty()) |
| 199 | return failure("cfgfunc must have at least one basic block", fn); |
| 200 | |
| 201 | // Verify that the argument list of the function and the arg list of the first |
| 202 | // block line up. |
| 203 | auto *firstBB = &fn.front(); |
| 204 | auto fnInputTypes = fn.getType()->getInputs(); |
| 205 | if (fnInputTypes.size() != firstBB->getNumArguments()) |
| 206 | return failure("first block of cfgfunc must have " + |
| 207 | Twine(fnInputTypes.size()) + |
| 208 | " arguments to match function signature", |
| 209 | fn); |
| 210 | for (unsigned i = 0, e = firstBB->getNumArguments(); i != e; ++i) |
| 211 | if (fnInputTypes[i] != firstBB->getArgument(i)->getType()) |
| 212 | return failure( |
| 213 | "type of argument #" + Twine(i) + |
| 214 | " must match corresponding argument in function signature", |
| 215 | fn); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 216 | |
| 217 | for (auto &block : fn) { |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 218 | if (verifyBlock(block)) |
| 219 | return true; |
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 | return false; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 224 | bool CFGFuncVerifier::verifyBlock(const BasicBlock &block) { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 225 | if (!block.getTerminator()) |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 226 | return failure("basic block with no terminator", block); |
| 227 | |
| 228 | if (verifyTerminator(*block.getTerminator())) |
| 229 | return true; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 230 | |
James Molloy | 61a656c | 2018-07-22 15:45:24 -0700 | [diff] [blame] | 231 | for (auto *arg : block.getArguments()) { |
| 232 | if (arg->getOwner() != &block) |
| 233 | return failure("basic block argument not owned by block", block); |
| 234 | } |
| 235 | |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 236 | for (auto &inst : block) { |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 237 | if (verifyOperation(inst)) |
| 238 | return true; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 239 | } |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 240 | return false; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 243 | bool CFGFuncVerifier::verifyTerminator(const TerminatorInst &term) { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 244 | if (term.getFunction() != &fn) |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 245 | return failure("terminator in the wrong function", term); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 246 | |
| 247 | // TODO: Check that operands are structurally ok. |
| 248 | // TODO: Check that successors are in the right function. |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 249 | |
| 250 | if (auto *ret = dyn_cast<ReturnInst>(&term)) |
| 251 | return verifyReturn(*ret); |
| 252 | |
Chris Lattner | 1604e47 | 2018-07-23 08:42:19 -0700 | [diff] [blame] | 253 | if (auto *br = dyn_cast<BranchInst>(&term)) |
| 254 | return verifyBranch(*br); |
| 255 | |
James Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 256 | if (auto *br = dyn_cast<CondBranchInst>(&term)) |
| 257 | return verifyCondBranch(*br); |
| 258 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 259 | return false; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 262 | bool CFGFuncVerifier::verifyReturn(const ReturnInst &inst) { |
| 263 | // Verify that the return operands match the results of the function. |
| 264 | auto results = fn.getType()->getResults(); |
| 265 | if (inst.getNumOperands() != results.size()) |
| 266 | return failure("return has " + Twine(inst.getNumOperands()) + |
| 267 | " operands, but enclosing function returns " + |
| 268 | Twine(results.size()), |
| 269 | inst); |
| 270 | |
Chris Lattner | 1604e47 | 2018-07-23 08:42:19 -0700 | [diff] [blame] | 271 | for (unsigned i = 0, e = results.size(); i != e; ++i) |
| 272 | if (inst.getOperand(i)->getType() != results[i]) |
| 273 | return failure("type of return operand " + Twine(i) + |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 274 | " doesn't match function result type", |
Chris Lattner | 1604e47 | 2018-07-23 08:42:19 -0700 | [diff] [blame] | 275 | inst); |
| 276 | |
| 277 | return false; |
| 278 | } |
| 279 | |
| 280 | bool CFGFuncVerifier::verifyBranch(const BranchInst &inst) { |
| 281 | // Verify that the number of operands lines up with the number of BB arguments |
| 282 | // in the successor. |
| 283 | auto dest = inst.getDest(); |
| 284 | if (inst.getNumOperands() != dest->getNumArguments()) |
| 285 | return failure("branch has " + Twine(inst.getNumOperands()) + |
| 286 | " operands, but target block has " + |
| 287 | Twine(dest->getNumArguments()), |
| 288 | inst); |
| 289 | |
| 290 | for (unsigned i = 0, e = inst.getNumOperands(); i != e; ++i) |
| 291 | if (inst.getOperand(i)->getType() != dest->getArgument(i)->getType()) |
| 292 | return failure("type of branch operand " + Twine(i) + |
| 293 | " doesn't match target bb argument type", |
| 294 | inst); |
| 295 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 296 | return false; |
| 297 | } |
| 298 | |
James Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 299 | template <typename OperandIteratorTy, typename ArgumentIteratorTy> |
| 300 | bool CFGFuncVerifier::verifyOperandsMatchArguments( |
| 301 | OperandIteratorTy opBegin, OperandIteratorTy opEnd, |
| 302 | ArgumentIteratorTy argBegin, const Instruction &instContext) { |
| 303 | OperandIteratorTy opIt = opBegin; |
| 304 | ArgumentIteratorTy argIt = argBegin; |
| 305 | for (; opIt != opEnd; ++opIt, ++argIt) { |
| 306 | if ((*opIt)->getType() != (*argIt)->getType()) |
| 307 | return failure("type of operand " + Twine(std::distance(opBegin, opIt)) + |
| 308 | " doesn't match argument type", |
| 309 | instContext); |
| 310 | } |
| 311 | return false; |
| 312 | } |
| 313 | |
| 314 | bool CFGFuncVerifier::verifyCondBranch(const CondBranchInst &inst) { |
| 315 | // Verify that the number of operands lines up with the number of BB arguments |
| 316 | // in the true successor. |
| 317 | auto trueDest = inst.getTrueDest(); |
| 318 | if (inst.getNumTrueOperands() != trueDest->getNumArguments()) |
| 319 | return failure("branch has " + Twine(inst.getNumTrueOperands()) + |
| 320 | " true operands, but true target block has " + |
| 321 | Twine(trueDest->getNumArguments()), |
| 322 | inst); |
| 323 | |
| 324 | if (verifyOperandsMatchArguments(inst.true_operand_begin(), |
| 325 | inst.true_operand_end(), |
| 326 | trueDest->args_begin(), inst)) |
| 327 | return true; |
| 328 | |
| 329 | // And the false successor. |
| 330 | auto falseDest = inst.getFalseDest(); |
| 331 | if (inst.getNumFalseOperands() != falseDest->getNumArguments()) |
| 332 | return failure("branch has " + Twine(inst.getNumFalseOperands()) + |
| 333 | " false operands, but false target block has " + |
| 334 | Twine(falseDest->getNumArguments()), |
| 335 | inst); |
| 336 | |
| 337 | if (verifyOperandsMatchArguments(inst.false_operand_begin(), |
| 338 | inst.false_operand_end(), |
| 339 | falseDest->args_begin(), inst)) |
| 340 | return true; |
| 341 | |
| 342 | if (inst.getCondition()->getType() != Type::getInteger(1, fn.getContext())) |
| 343 | return failure("type of condition is not boolean (i1)", inst); |
| 344 | |
| 345 | return false; |
| 346 | } |
| 347 | |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 348 | //===----------------------------------------------------------------------===// |
| 349 | // ML Functions |
| 350 | //===----------------------------------------------------------------------===// |
| 351 | |
| 352 | namespace { |
Chris Lattner | ea5c3dc | 2018-08-21 08:42:19 -0700 | [diff] [blame] | 353 | struct MLFuncVerifier : public Verifier, public StmtWalker<MLFuncVerifier> { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 354 | const MLFunction &fn; |
Chris Lattner | ea5c3dc | 2018-08-21 08:42:19 -0700 | [diff] [blame] | 355 | bool hadError = false; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 356 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 357 | MLFuncVerifier(const MLFunction &fn, std::string *errorResult) |
Chris Lattner | ea5c3dc | 2018-08-21 08:42:19 -0700 | [diff] [blame] | 358 | : Verifier(errorResult, fn), fn(fn) {} |
| 359 | |
| 360 | void visitOperationStmt(OperationStmt *opStmt) { |
| 361 | hadError |= verifyOperation(*opStmt); |
| 362 | } |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 363 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 364 | bool verify() { |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 365 | llvm::PrettyStackTraceFormat fmt("MLIR Verifier: mlfunc @%s", |
| 366 | fn.getName().c_str()); |
| 367 | |
Chris Lattner | ea5c3dc | 2018-08-21 08:42:19 -0700 | [diff] [blame] | 368 | // Check basic structural properties. |
| 369 | walk(const_cast<MLFunction *>(&fn)); |
| 370 | if (hadError) |
| 371 | return true; |
| 372 | |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 373 | // TODO: check that operation is not a return statement unless it's |
| 374 | // the last one in the function. |
Tatiana Shpeisman | de8829f | 2018-08-24 23:38:14 -0700 | [diff] [blame^] | 375 | // TODO: check that loop bounds are properly formed. |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 376 | if (verifyReturn()) |
| 377 | return true; |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 378 | |
| 379 | return verifyDominance(); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 380 | } |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 381 | |
| 382 | /// Walk all of the code in this MLFunc and verify that the operands of any |
| 383 | /// operations are properly dominated by their definitions. |
| 384 | bool verifyDominance(); |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 385 | |
| 386 | /// Verify that function has a return statement that matches its signature. |
| 387 | bool verifyReturn(); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 388 | }; |
| 389 | } // end anonymous namespace |
| 390 | |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 391 | /// Walk all of the code in this MLFunc and verify that the operands of any |
| 392 | /// operations are properly dominated by their definitions. |
| 393 | bool MLFuncVerifier::verifyDominance() { |
| 394 | using HashTable = llvm::ScopedHashTable<const SSAValue *, bool>; |
| 395 | HashTable liveValues; |
| 396 | HashTable::ScopeTy topScope(liveValues); |
| 397 | |
| 398 | // All of the arguments to the function are live for the whole function. |
Tatiana Shpeisman | bc3c749 | 2018-08-06 11:54:39 -0700 | [diff] [blame] | 399 | for (auto *arg : fn.getArguments()) |
| 400 | liveValues.insert(arg, true); |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 401 | |
| 402 | // This recursive function walks the statement list pushing scopes onto the |
| 403 | // stack as it goes, and popping them to remove them from the table. |
| 404 | std::function<bool(const StmtBlock &block)> walkBlock; |
| 405 | walkBlock = [&](const StmtBlock &block) -> bool { |
| 406 | HashTable::ScopeTy blockScope(liveValues); |
| 407 | |
| 408 | // The induction variable of a for statement is live within its body. |
| 409 | if (auto *forStmt = dyn_cast<ForStmt>(&block)) |
| 410 | liveValues.insert(forStmt, true); |
| 411 | |
| 412 | for (auto &stmt : block) { |
Tatiana Shpeisman | de8829f | 2018-08-24 23:38:14 -0700 | [diff] [blame^] | 413 | // Verify that each of the operands are live. |
| 414 | unsigned operandNo = 0; |
| 415 | for (auto *opValue : stmt.getOperands()) { |
| 416 | if (!liveValues.count(opValue)) { |
| 417 | stmt.emitError("operand #" + Twine(operandNo) + |
| 418 | " does not dominate this use"); |
| 419 | if (auto *useStmt = opValue->getDefiningStmt()) |
| 420 | useStmt->emitNote("operand defined here"); |
| 421 | return true; |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 422 | } |
Tatiana Shpeisman | de8829f | 2018-08-24 23:38:14 -0700 | [diff] [blame^] | 423 | ++operandNo; |
| 424 | } |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 425 | |
Tatiana Shpeisman | de8829f | 2018-08-24 23:38:14 -0700 | [diff] [blame^] | 426 | if (auto *opStmt = dyn_cast<OperationStmt>(&stmt)) { |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 427 | // Operations define values, add them to the hash table. |
| 428 | for (auto *result : opStmt->getResults()) |
| 429 | liveValues.insert(result, true); |
| 430 | continue; |
| 431 | } |
| 432 | |
| 433 | // If this is an if or for, recursively walk the block they contain. |
| 434 | if (auto *ifStmt = dyn_cast<IfStmt>(&stmt)) { |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 435 | if (walkBlock(*ifStmt->getThen())) |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 436 | return true; |
| 437 | |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 438 | if (auto *elseClause = ifStmt->getElse()) |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 439 | if (walkBlock(*elseClause)) |
| 440 | return true; |
| 441 | } |
| 442 | if (auto *forStmt = dyn_cast<ForStmt>(&stmt)) |
| 443 | if (walkBlock(*forStmt)) |
| 444 | return true; |
| 445 | } |
| 446 | |
| 447 | return false; |
| 448 | }; |
| 449 | |
| 450 | // Check the whole function out. |
| 451 | return walkBlock(fn); |
| 452 | } |
| 453 | |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 454 | bool MLFuncVerifier::verifyReturn() { |
| 455 | // TODO: fold return verification in the pass that verifies all statements. |
| 456 | const char missingReturnMsg[] = "ML function must end with return statement"; |
| 457 | if (fn.getStatements().empty()) |
| 458 | return failure(missingReturnMsg, fn); |
| 459 | |
| 460 | const auto &stmt = fn.getStatements().back(); |
| 461 | if (const auto *op = dyn_cast<OperationStmt>(&stmt)) { |
| 462 | if (!op->isReturn()) |
| 463 | return failure(missingReturnMsg, fn); |
| 464 | |
| 465 | // The operand number and types must match the function signature. |
| 466 | // TODO: move this verification in ReturnOp::verify() if printing |
| 467 | // of the error messages below can be made to work there. |
| 468 | const auto &results = fn.getType()->getResults(); |
| 469 | if (op->getNumOperands() != results.size()) |
| 470 | return failure("return has " + Twine(op->getNumOperands()) + |
| 471 | " operands, but enclosing function returns " + |
| 472 | Twine(results.size()), |
| 473 | *op); |
| 474 | |
| 475 | for (unsigned i = 0, e = results.size(); i != e; ++i) |
| 476 | if (op->getOperand(i)->getType() != results[i]) |
| 477 | return failure("type of return operand " + Twine(i) + |
| 478 | " doesn't match function result type", |
| 479 | *op); |
| 480 | return false; |
| 481 | } |
| 482 | return failure(missingReturnMsg, fn); |
| 483 | } |
| 484 | |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 485 | //===----------------------------------------------------------------------===// |
| 486 | // Entrypoints |
| 487 | //===----------------------------------------------------------------------===// |
| 488 | |
| 489 | /// Perform (potentially expensive) checks of invariants, used to detect |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 490 | /// compiler bugs. On error, this fills in the string and return true, |
| 491 | /// or aborts if the string was not provided. |
| 492 | bool Function::verify(std::string *errorResult) const { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 493 | switch (getKind()) { |
| 494 | case Kind::ExtFunc: |
| 495 | // No body, nothing can be wrong here. |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 496 | return false; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 497 | case Kind::CFGFunc: |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 498 | return CFGFuncVerifier(*cast<CFGFunction>(this), errorResult).verify(); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 499 | case Kind::MLFunc: |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 500 | return MLFuncVerifier(*cast<MLFunction>(this), errorResult).verify(); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | |
| 504 | /// Perform (potentially expensive) checks of invariants, used to detect |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 505 | /// compiler bugs. On error, this fills in the string and return true, |
| 506 | /// or aborts if the string was not provided. |
| 507 | bool Module::verify(std::string *errorResult) const { |
| 508 | |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 509 | /// Check that each function is correct. |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 510 | for (auto &fn : *this) { |
| 511 | if (fn.verify(errorResult)) |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 512 | return true; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 513 | } |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 514 | |
| 515 | // Make sure the error string is empty on success. |
| 516 | if (errorResult) |
| 517 | errorResult->clear(); |
| 518 | return false; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 519 | } |