1 //===- Verifier.cpp - MLIR Verifier Implementation ------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the verify() methods on the various IR types, performing
10 // (potentially expensive) checks on the holistic structure of the code.  This
11 // can be used for detecting bugs in compiler transformations and hand written
12 // .mlir files.
13 //
14 // The checks in this file are only for things that can occur as part of IR
15 // transformations: e.g. violation of dominance information, malformed operation
16 // attributes, etc.  MLIR supports transformations moving IR through locally
17 // invalid states (e.g. unlinking an operation from a block before re-inserting
18 // it in a new place), but each transformation must complete with the IR in a
19 // valid form.
20 //
21 // This should not check for things that are always wrong by construction (e.g.
22 // attributes or other immutable structures that are incorrect), because those
23 // are not mutable and can be checked at time of construction.
24 //
25 //===----------------------------------------------------------------------===//
26 
27 #include "mlir/IR/Verifier.h"
28 #include "mlir/IR/Attributes.h"
29 #include "mlir/IR/Dialect.h"
30 #include "mlir/IR/Dominance.h"
31 #include "mlir/IR/Operation.h"
32 #include "mlir/IR/RegionKindInterface.h"
33 #include "llvm/ADT/StringMap.h"
34 #include "llvm/Support/FormatVariadic.h"
35 #include "llvm/Support/PrettyStackTrace.h"
36 #include "llvm/Support/Regex.h"
37 
38 using namespace mlir;
39 
40 namespace {
41 /// This class encapsulates all the state used to verify an operation region.
42 class OperationVerifier {
43 public:
44   explicit OperationVerifier() {}
45 
46   /// Verify the given operation.
47   LogicalResult verifyOpAndDominance(Operation &op);
48 
49 private:
50   /// Verify the given potentially nested region or block.
51   LogicalResult verifyRegion(Region &region);
52   LogicalResult verifyBlock(Block &block);
53   LogicalResult verifyOperation(Operation &op);
54 
55   /// Verify the dominance property of operations within the given Region.
56   LogicalResult verifyDominance(Region &region);
57 
58   /// Verify the dominance property of regions contained within the given
59   /// Operation.
60   LogicalResult verifyDominanceOfContainedRegions(Operation &op);
61 
62   /// Emit an error for the given block.
63   InFlightDiagnostic emitError(Block &bb, const Twine &message) {
64     // Take the location information for the first operation in the block.
65     if (!bb.empty())
66       return bb.front().emitError(message);
67 
68     // Worst case, fall back to using the parent's location.
69     return mlir::emitError(bb.getParent()->getLoc(), message);
70   }
71 
72   /// Dominance information for this operation, when checking dominance.
73   DominanceInfo *domInfo = nullptr;
74 };
75 } // end anonymous namespace
76 
77 /// Verify the given operation.
78 LogicalResult OperationVerifier::verifyOpAndDominance(Operation &op) {
79   // Verify the operation first.
80   if (failed(verifyOperation(op)))
81     return failure();
82 
83   // Since everything looks structurally ok to this point, we do a dominance
84   // check for any nested regions. We do this as a second pass since malformed
85   // CFG's can cause dominator analysis constructure to crash and we want the
86   // verifier to be resilient to malformed code.
87   DominanceInfo theDomInfo(&op);
88   domInfo = &theDomInfo;
89   if (failed(verifyDominanceOfContainedRegions(op)))
90     return failure();
91 
92   domInfo = nullptr;
93   return success();
94 }
95 
96 LogicalResult OperationVerifier::verifyRegion(Region &region) {
97   if (region.empty())
98     return success();
99 
100   // Verify the first block has no predecessors.
101   auto *firstBB = &region.front();
102   if (!firstBB->hasNoPredecessors())
103     return mlir::emitError(region.getLoc(),
104                            "entry block of region may not have predecessors");
105 
106   // Verify each of the blocks within the region.
107   for (Block &block : region)
108     if (failed(verifyBlock(block)))
109       return failure();
110   return success();
111 }
112 
113 /// Returns true if this block may be valid without terminator. That is if:
114 /// - it does not have a parent region.
115 /// - Or the parent region have a single block and:
116 ///    - This region does not have a parent op.
117 ///    - Or the parent op is unregistered.
118 ///    - Or the parent op has the NoTerminator trait.
119 static bool mayBeValidWithoutTerminator(Block *block) {
120   if (!block->getParent())
121     return true;
122   if (!llvm::hasSingleElement(*block->getParent()))
123     return false;
124   Operation *op = block->getParentOp();
125   return !op || op->mightHaveTrait<OpTrait::NoTerminator>();
126 }
127 
128 LogicalResult OperationVerifier::verifyBlock(Block &block) {
129   for (auto arg : block.getArguments())
130     if (arg.getOwner() != &block)
131       return emitError(block, "block argument not owned by block");
132 
133   // Verify that this block has a terminator.
134   if (block.empty()) {
135     if (mayBeValidWithoutTerminator(&block))
136       return success();
137     return emitError(block, "empty block: expect at least a terminator");
138   }
139 
140   // Check each operation, and make sure there are no branches out of the
141   // middle of this block.
142   for (auto &op : llvm::make_range(block.begin(), block.end())) {
143     // Only the last instructions is allowed to have successors.
144     if (op.getNumSuccessors() != 0 && &op != &block.back())
145       return op.emitError(
146           "operation with block successors must terminate its parent block");
147 
148     if (failed(verifyOperation(op)))
149       return failure();
150   }
151 
152   // Verify that this block is not branching to a block of a different
153   // region.
154   for (Block *successor : block.getSuccessors())
155     if (successor->getParent() != block.getParent())
156       return block.back().emitOpError(
157           "branching to block of a different region");
158 
159   // If this block doesn't have to have a terminator, don't require it.
160   if (mayBeValidWithoutTerminator(&block))
161     return success();
162 
163   Operation &terminator = block.back();
164   if (!terminator.mightHaveTrait<OpTrait::IsTerminator>())
165     return block.back().emitError("block with no terminator, has ")
166            << terminator;
167 
168   return success();
169 }
170 
171 LogicalResult OperationVerifier::verifyOperation(Operation &op) {
172   // Check that operands are non-nil and structurally ok.
173   for (auto operand : op.getOperands())
174     if (!operand)
175       return op.emitError("null operand found");
176 
177   /// Verify that all of the attributes are okay.
178   for (auto attr : op.getAttrs()) {
179     // Check for any optional dialect specific attributes.
180     if (auto *dialect = attr.first.getDialect())
181       if (failed(dialect->verifyOperationAttribute(&op, attr)))
182         return failure();
183   }
184 
185   // If we can get operation info for this, check the custom hook.
186   OperationName opName = op.getName();
187   auto *opInfo = opName.getAbstractOperation();
188   if (opInfo && failed(opInfo->verifyInvariants(&op)))
189     return failure();
190 
191   if (unsigned numRegions = op.getNumRegions()) {
192     auto kindInterface = dyn_cast<mlir::RegionKindInterface>(op);
193 
194     // Verify that all child regions are ok.
195     for (unsigned i = 0; i < numRegions; ++i) {
196       Region &region = op.getRegion(i);
197       RegionKind kind =
198           kindInterface ? kindInterface.getRegionKind(i) : RegionKind::SSACFG;
199       // Check that Graph Regions only have a single basic block. This is
200       // similar to the code in SingleBlockImplicitTerminator, but doesn't
201       // require the trait to be specified. This arbitrary limitation is
202       // designed to limit the number of cases that have to be handled by
203       // transforms and conversions.
204       if (op.isRegistered() && kind == RegionKind::Graph) {
205         // Empty regions are fine.
206         if (region.empty())
207           continue;
208 
209         // Non-empty regions must contain a single basic block.
210         if (std::next(region.begin()) != region.end())
211           return op.emitOpError("expects graph region #")
212                  << i << " to have 0 or 1 blocks";
213       }
214       if (failed(verifyRegion(region)))
215         return failure();
216     }
217   }
218 
219   // If this is a registered operation, there is nothing left to do.
220   if (opInfo)
221     return success();
222 
223   // Otherwise, verify that the parent dialect allows un-registered operations.
224   Dialect *dialect = opName.getDialect();
225   if (!dialect) {
226     if (!op.getContext()->allowsUnregisteredDialects()) {
227       return op.emitOpError()
228              << "created with unregistered dialect. If this is "
229                 "intended, please call allowUnregisteredDialects() on the "
230                 "MLIRContext, or use -allow-unregistered-dialect with "
231                 "mlir-opt";
232     }
233     return success();
234   }
235 
236   if (!dialect->allowsUnknownOperations()) {
237     return op.emitError("unregistered operation '")
238            << op.getName() << "' found in dialect ('" << dialect->getNamespace()
239            << "') that does not allow unknown operations";
240   }
241 
242   return success();
243 }
244 
245 //===----------------------------------------------------------------------===//
246 // Dominance Checking
247 //===----------------------------------------------------------------------===//
248 
249 /// Emit an error when the specified operand of the specified operation is an
250 /// invalid use because of dominance properties.
251 static void diagnoseInvalidOperandDominance(Operation &op, unsigned operandNo) {
252   InFlightDiagnostic diag = op.emitError("operand #")
253                             << operandNo << " does not dominate this use";
254 
255   Value operand = op.getOperand(operandNo);
256 
257   /// Attach a note to an in-flight diagnostic that provide more information
258   /// about where an op operand is defined.
259   if (auto *useOp = operand.getDefiningOp()) {
260     Diagnostic &note = diag.attachNote(useOp->getLoc());
261     note << "operand defined here";
262     Block *block1 = op.getBlock();
263     Block *block2 = useOp->getBlock();
264     Region *region1 = block1->getParent();
265     Region *region2 = block2->getParent();
266     if (block1 == block2)
267       note << " (op in the same block)";
268     else if (region1 == region2)
269       note << " (op in the same region)";
270     else if (region2->isProperAncestor(region1))
271       note << " (op in a parent region)";
272     else if (region1->isProperAncestor(region2))
273       note << " (op in a child region)";
274     else
275       note << " (op is neither in a parent nor in a child region)";
276     return;
277   }
278   // Block argument case.
279   Block *block1 = op.getBlock();
280   Block *block2 = operand.cast<BlockArgument>().getOwner();
281   Region *region1 = block1->getParent();
282   Region *region2 = block2->getParent();
283   Location loc = UnknownLoc::get(op.getContext());
284   if (block2->getParentOp())
285     loc = block2->getParentOp()->getLoc();
286   Diagnostic &note = diag.attachNote(loc);
287   if (!region2) {
288     note << " (block without parent)";
289     return;
290   }
291   if (block1 == block2)
292     llvm::report_fatal_error("Internal error in dominance verification");
293   int index = std::distance(region2->begin(), block2->getIterator());
294   note << "operand defined as a block argument (block #" << index;
295   if (region1 == region2)
296     note << " in the same region)";
297   else if (region2->isProperAncestor(region1))
298     note << " in a parent region)";
299   else if (region1->isProperAncestor(region2))
300     note << " in a child region)";
301   else
302     note << " neither in a parent nor in a child region)";
303 }
304 
305 LogicalResult OperationVerifier::verifyDominance(Region &region) {
306   // Verify the dominance of each of the held operations.
307   for (Block &block : region) {
308     // Dominance is only meaningful inside reachable blocks.
309     bool isReachable = domInfo->isReachableFromEntry(&block);
310 
311     for (Operation &op : block) {
312       if (isReachable) {
313         // Check that operands properly dominate this use.
314         for (unsigned operandNo = 0, e = op.getNumOperands(); operandNo != e;
315              ++operandNo) {
316           if (domInfo->properlyDominates(op.getOperand(operandNo), &op))
317             continue;
318 
319           diagnoseInvalidOperandDominance(op, operandNo);
320           return failure();
321         }
322       }
323 
324       // Recursively verify dominance within each operation in the
325       // block, even if the block itself is not reachable, or we are in
326       // a region which doesn't respect dominance.
327       if (failed(verifyDominanceOfContainedRegions(op)))
328         return failure();
329     }
330   }
331   return success();
332 }
333 
334 /// Verify the dominance of each of the nested blocks within the given operation
335 LogicalResult
336 OperationVerifier::verifyDominanceOfContainedRegions(Operation &op) {
337   for (Region &region : op.getRegions()) {
338     if (failed(verifyDominance(region)))
339       return failure();
340   }
341   return success();
342 }
343 
344 //===----------------------------------------------------------------------===//
345 // Entrypoint
346 //===----------------------------------------------------------------------===//
347 
348 /// Perform (potentially expensive) checks of invariants, used to detect
349 /// compiler bugs.  On error, this reports the error through the MLIRContext and
350 /// returns failure.
351 LogicalResult mlir::verify(Operation *op) {
352   return OperationVerifier().verifyOpAndDominance(*op);
353 }
354