blob: 48f3643bb9b05dab5992578a743382f07651a3a4 [file] [log] [blame]
Chris Lattner21e67f62018-07-06 10:46:19 -07001//===- 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 Lattnerea5c3dc2018-08-21 08:42:19 -070036#include "mlir/IR/Attributes.h"
Chris Lattner21e67f62018-07-06 10:46:19 -070037#include "mlir/IR/CFGFunction.h"
38#include "mlir/IR/MLFunction.h"
39#include "mlir/IR/Module.h"
40#include "mlir/IR/OperationSet.h"
Chris Lattnerf7bdf952018-08-05 21:12:29 -070041#include "mlir/IR/Statements.h"
Chris Lattnerea5c3dc2018-08-21 08:42:19 -070042#include "mlir/IR/StmtVisitor.h"
Chris Lattnerf7bdf952018-08-05 21:12:29 -070043#include "llvm/ADT/ScopedHashTable.h"
Chris Lattner21e67f62018-07-06 10:46:19 -070044#include "llvm/ADT/Twine.h"
Chris Lattnerf7bdf952018-08-05 21:12:29 -070045#include "llvm/Support/PrettyStackTrace.h"
Chris Lattner21e67f62018-07-06 10:46:19 -070046#include "llvm/Support/raw_ostream.h"
47using namespace mlir;
48
Chris Lattner40746442018-07-21 14:32:09 -070049namespace {
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///
54class Verifier {
55public:
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 Lattner95865062018-08-01 10:18:59 -070081 bool opFailure(const Twine &message, const Operation &value) {
82 value.emitError(message);
83 return true;
84 }
85
Chris Lattnerea5c3dc2018-08-21 08:42:19 -070086 bool verifyOperation(const Operation &op);
87 bool verifyAttribute(Attribute *attr, const Operation &op);
88
Chris Lattner40746442018-07-21 14:32:09 -070089protected:
Chris Lattnerea5c3dc2018-08-21 08:42:19 -070090 explicit Verifier(std::string *errorResult, const Function &fn)
91 : errorResult(errorResult), fn(fn),
92 operationSet(OperationSet::get(fn.getContext())) {}
Chris Lattner40746442018-07-21 14:32:09 -070093
94private:
Chris Lattnerea5c3dc2018-08-21 08:42:19 -070095 /// If the verifier is returning errors back to a client, this is the error to
96 /// fill in.
Chris Lattner40746442018-07-21 14:32:09 -070097 std::string *errorResult;
Chris Lattnerea5c3dc2018-08-21 08:42:19 -070098
99 /// The function being checked.
100 const Function &fn;
101
102 /// The operation set installed in the current MLIR context.
103 OperationSet &operationSet;
Chris Lattner40746442018-07-21 14:32:09 -0700104};
105} // end anonymous namespace
Chris Lattner21e67f62018-07-06 10:46:19 -0700106
Chris Lattnerea5c3dc2018-08-21 08:42:19 -0700107// Check that function attributes are all well formed.
108bool 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.
136bool 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 Lattner21e67f62018-07-06 10:46:19 -0700160//===----------------------------------------------------------------------===//
161// CFG Functions
162//===----------------------------------------------------------------------===//
163
164namespace {
Chris Lattnerea5c3dc2018-08-21 08:42:19 -0700165struct CFGFuncVerifier : public Verifier {
Chris Lattner21e67f62018-07-06 10:46:19 -0700166 const CFGFunction &fn;
Chris Lattner21e67f62018-07-06 10:46:19 -0700167
Chris Lattner40746442018-07-21 14:32:09 -0700168 CFGFuncVerifier(const CFGFunction &fn, std::string *errorResult)
Chris Lattnerea5c3dc2018-08-21 08:42:19 -0700169 : Verifier(errorResult, fn), fn(fn) {}
Chris Lattner21e67f62018-07-06 10:46:19 -0700170
Chris Lattner40746442018-07-21 14:32:09 -0700171 bool verify();
172 bool verifyBlock(const BasicBlock &block);
Chris Lattner40746442018-07-21 14:32:09 -0700173 bool verifyTerminator(const TerminatorInst &term);
174 bool verifyReturn(const ReturnInst &inst);
Chris Lattner1604e472018-07-23 08:42:19 -0700175 bool verifyBranch(const BranchInst &inst);
James Molloy4f788372018-07-24 15:01:27 -0700176 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 Lattner21e67f62018-07-06 10:46:19 -0700187};
188} // end anonymous namespace
189
Chris Lattner40746442018-07-21 14:32:09 -0700190bool CFGFuncVerifier::verify() {
Chris Lattnerf7bdf952018-08-05 21:12:29 -0700191 llvm::PrettyStackTraceFormat fmt("MLIR Verifier: cfgfunc @%s",
192 fn.getName().c_str());
193
Chris Lattner21e67f62018-07-06 10:46:19 -0700194 // TODO: Lots to be done here, including verifying dominance information when
195 // we have uses and defs.
James Molloy61a656c2018-07-22 15:45:24 -0700196 // 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 Lattner21e67f62018-07-06 10:46:19 -0700216
217 for (auto &block : fn) {
Chris Lattner40746442018-07-21 14:32:09 -0700218 if (verifyBlock(block))
219 return true;
Chris Lattner21e67f62018-07-06 10:46:19 -0700220 }
Chris Lattner40746442018-07-21 14:32:09 -0700221 return false;
Chris Lattner21e67f62018-07-06 10:46:19 -0700222}
223
Chris Lattner40746442018-07-21 14:32:09 -0700224bool CFGFuncVerifier::verifyBlock(const BasicBlock &block) {
Chris Lattner21e67f62018-07-06 10:46:19 -0700225 if (!block.getTerminator())
Chris Lattner40746442018-07-21 14:32:09 -0700226 return failure("basic block with no terminator", block);
227
228 if (verifyTerminator(*block.getTerminator()))
229 return true;
Chris Lattner21e67f62018-07-06 10:46:19 -0700230
James Molloy61a656c2018-07-22 15:45:24 -0700231 for (auto *arg : block.getArguments()) {
232 if (arg->getOwner() != &block)
233 return failure("basic block argument not owned by block", block);
234 }
235
Chris Lattner21e67f62018-07-06 10:46:19 -0700236 for (auto &inst : block) {
Chris Lattner40746442018-07-21 14:32:09 -0700237 if (verifyOperation(inst))
238 return true;
Chris Lattner21e67f62018-07-06 10:46:19 -0700239 }
Chris Lattner40746442018-07-21 14:32:09 -0700240 return false;
Chris Lattner21e67f62018-07-06 10:46:19 -0700241}
242
Chris Lattner40746442018-07-21 14:32:09 -0700243bool CFGFuncVerifier::verifyTerminator(const TerminatorInst &term) {
Chris Lattner21e67f62018-07-06 10:46:19 -0700244 if (term.getFunction() != &fn)
Chris Lattner40746442018-07-21 14:32:09 -0700245 return failure("terminator in the wrong function", term);
Chris Lattner21e67f62018-07-06 10:46:19 -0700246
247 // TODO: Check that operands are structurally ok.
248 // TODO: Check that successors are in the right function.
Chris Lattner40746442018-07-21 14:32:09 -0700249
250 if (auto *ret = dyn_cast<ReturnInst>(&term))
251 return verifyReturn(*ret);
252
Chris Lattner1604e472018-07-23 08:42:19 -0700253 if (auto *br = dyn_cast<BranchInst>(&term))
254 return verifyBranch(*br);
255
James Molloy4f788372018-07-24 15:01:27 -0700256 if (auto *br = dyn_cast<CondBranchInst>(&term))
257 return verifyCondBranch(*br);
258
Chris Lattner40746442018-07-21 14:32:09 -0700259 return false;
Chris Lattner21e67f62018-07-06 10:46:19 -0700260}
261
Chris Lattner40746442018-07-21 14:32:09 -0700262bool 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 Lattner1604e472018-07-23 08:42:19 -0700271 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 Shpeismand9b1d862018-08-09 12:28:58 -0700274 " doesn't match function result type",
Chris Lattner1604e472018-07-23 08:42:19 -0700275 inst);
276
277 return false;
278}
279
280bool 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 Lattner40746442018-07-21 14:32:09 -0700296 return false;
297}
298
James Molloy4f788372018-07-24 15:01:27 -0700299template <typename OperandIteratorTy, typename ArgumentIteratorTy>
300bool 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
314bool 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 Lattner21e67f62018-07-06 10:46:19 -0700348//===----------------------------------------------------------------------===//
349// ML Functions
350//===----------------------------------------------------------------------===//
351
352namespace {
Chris Lattnerea5c3dc2018-08-21 08:42:19 -0700353struct MLFuncVerifier : public Verifier, public StmtWalker<MLFuncVerifier> {
Chris Lattner21e67f62018-07-06 10:46:19 -0700354 const MLFunction &fn;
Chris Lattnerea5c3dc2018-08-21 08:42:19 -0700355 bool hadError = false;
Chris Lattner21e67f62018-07-06 10:46:19 -0700356
Chris Lattner40746442018-07-21 14:32:09 -0700357 MLFuncVerifier(const MLFunction &fn, std::string *errorResult)
Chris Lattnerea5c3dc2018-08-21 08:42:19 -0700358 : Verifier(errorResult, fn), fn(fn) {}
359
360 void visitOperationStmt(OperationStmt *opStmt) {
361 hadError |= verifyOperation(*opStmt);
362 }
Chris Lattner21e67f62018-07-06 10:46:19 -0700363
Chris Lattner40746442018-07-21 14:32:09 -0700364 bool verify() {
Chris Lattnerf7bdf952018-08-05 21:12:29 -0700365 llvm::PrettyStackTraceFormat fmt("MLIR Verifier: mlfunc @%s",
366 fn.getName().c_str());
367
Chris Lattnerea5c3dc2018-08-21 08:42:19 -0700368 // Check basic structural properties.
369 walk(const_cast<MLFunction *>(&fn));
370 if (hadError)
371 return true;
372
Tatiana Shpeismand9b1d862018-08-09 12:28:58 -0700373 // TODO: check that operation is not a return statement unless it's
374 // the last one in the function.
Tatiana Shpeismanc6aa35b2018-08-28 15:26:20 -0700375 // TODO: check that loop bounds and if conditions are properly formed.
Tatiana Shpeismand9b1d862018-08-09 12:28:58 -0700376 if (verifyReturn())
377 return true;
Chris Lattnerf7bdf952018-08-05 21:12:29 -0700378
379 return verifyDominance();
Chris Lattner21e67f62018-07-06 10:46:19 -0700380 }
Chris Lattnerf7bdf952018-08-05 21:12:29 -0700381
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 Shpeismand9b1d862018-08-09 12:28:58 -0700385
386 /// Verify that function has a return statement that matches its signature.
387 bool verifyReturn();
Chris Lattner21e67f62018-07-06 10:46:19 -0700388};
389} // end anonymous namespace
390
Chris Lattnerf7bdf952018-08-05 21:12:29 -0700391/// Walk all of the code in this MLFunc and verify that the operands of any
392/// operations are properly dominated by their definitions.
393bool 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 Shpeismanbc3c7492018-08-06 11:54:39 -0700399 for (auto *arg : fn.getArguments())
400 liveValues.insert(arg, true);
Chris Lattnerf7bdf952018-08-05 21:12:29 -0700401
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 Shpeismande8829f2018-08-24 23:38:14 -0700413 // 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 Lattnerf7bdf952018-08-05 21:12:29 -0700422 }
Tatiana Shpeismande8829f2018-08-24 23:38:14 -0700423 ++operandNo;
424 }
Chris Lattnerf7bdf952018-08-05 21:12:29 -0700425
Tatiana Shpeismande8829f2018-08-24 23:38:14 -0700426 if (auto *opStmt = dyn_cast<OperationStmt>(&stmt)) {
Chris Lattnerf7bdf952018-08-05 21:12:29 -0700427 // 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 Lattnere787b322018-08-08 11:14:57 -0700435 if (walkBlock(*ifStmt->getThen()))
Chris Lattnerf7bdf952018-08-05 21:12:29 -0700436 return true;
437
Chris Lattnere787b322018-08-08 11:14:57 -0700438 if (auto *elseClause = ifStmt->getElse())
Chris Lattnerf7bdf952018-08-05 21:12:29 -0700439 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 Shpeismand9b1d862018-08-09 12:28:58 -0700454bool 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 Lattner21e67f62018-07-06 10:46:19 -0700485//===----------------------------------------------------------------------===//
486// Entrypoints
487//===----------------------------------------------------------------------===//
488
489/// Perform (potentially expensive) checks of invariants, used to detect
Chris Lattner40746442018-07-21 14:32:09 -0700490/// compiler bugs. On error, this fills in the string and return true,
491/// or aborts if the string was not provided.
492bool Function::verify(std::string *errorResult) const {
Chris Lattner21e67f62018-07-06 10:46:19 -0700493 switch (getKind()) {
494 case Kind::ExtFunc:
495 // No body, nothing can be wrong here.
Chris Lattner40746442018-07-21 14:32:09 -0700496 return false;
Chris Lattner21e67f62018-07-06 10:46:19 -0700497 case Kind::CFGFunc:
Chris Lattner40746442018-07-21 14:32:09 -0700498 return CFGFuncVerifier(*cast<CFGFunction>(this), errorResult).verify();
Chris Lattner21e67f62018-07-06 10:46:19 -0700499 case Kind::MLFunc:
Chris Lattner40746442018-07-21 14:32:09 -0700500 return MLFuncVerifier(*cast<MLFunction>(this), errorResult).verify();
Chris Lattner21e67f62018-07-06 10:46:19 -0700501 }
502}
503
504/// Perform (potentially expensive) checks of invariants, used to detect
Chris Lattner40746442018-07-21 14:32:09 -0700505/// compiler bugs. On error, this fills in the string and return true,
506/// or aborts if the string was not provided.
507bool Module::verify(std::string *errorResult) const {
508
Chris Lattner21e67f62018-07-06 10:46:19 -0700509 /// Check that each function is correct.
Chris Lattnera8e47672018-07-25 14:08:16 -0700510 for (auto &fn : *this) {
511 if (fn.verify(errorResult))
Chris Lattner40746442018-07-21 14:32:09 -0700512 return true;
Chris Lattner21e67f62018-07-06 10:46:19 -0700513 }
Chris Lattner40746442018-07-21 14:32:09 -0700514
515 // Make sure the error string is empty on success.
516 if (errorResult)
517 errorResult->clear();
518 return false;
Chris Lattner21e67f62018-07-06 10:46:19 -0700519}