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