[LLVM] Fix Maintainers.md formatting (NFC)
[llvm-project.git] / mlir / lib / IR / Verifier.cpp
blob90ff8ef3b497fe2f1c17ced7619299e8da0de919
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.
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.
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.
25 //===----------------------------------------------------------------------===//
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 "mlir/IR/Threading.h"
34 #include "llvm/ADT/DenseMapInfoVariant.h"
35 #include "llvm/ADT/PointerIntPair.h"
36 #include "llvm/ADT/StringMap.h"
37 #include "llvm/Support/FormatVariadic.h"
38 #include "llvm/Support/PrettyStackTrace.h"
39 #include "llvm/Support/Regex.h"
40 #include <atomic>
41 #include <optional>
43 using namespace mlir;
45 namespace {
46 /// This class encapsulates all the state used to verify an operation region.
47 class OperationVerifier {
48 public:
49 /// If `verifyRecursively` is true, then this will also recursively verify
50 /// nested operations.
51 explicit OperationVerifier(bool verifyRecursively)
52 : verifyRecursively(verifyRecursively) {}
54 /// Verify the given operation.
55 LogicalResult verifyOpAndDominance(Operation &op);
57 private:
58 using WorkItem = llvm::PointerUnion<Operation *, Block *>;
59 using WorkItemEntry = llvm::PointerIntPair<WorkItem, 1, bool>;
61 /// This verifier uses a DFS of the tree of operations/blocks. The method
62 /// verifyOnEntrance is invoked when we visit a node for the first time, i.e.
63 /// before visiting its children. The method verifyOnExit is invoked
64 /// upon exit from the subtree, i.e. when we visit a node for the second time.
65 LogicalResult verifyOnEntrance(Block &block);
66 LogicalResult verifyOnEntrance(Operation &op);
68 LogicalResult verifyOnExit(Block &block);
69 LogicalResult verifyOnExit(Operation &op);
71 /// Verify the properties and dominance relationships of this operation.
72 LogicalResult verifyOperation(Operation &op);
74 /// Verify the dominance property of regions contained within the given
75 /// Operation.
76 LogicalResult verifyDominanceOfContainedRegions(Operation &op,
77 DominanceInfo &domInfo);
79 /// A flag indicating if this verifier should recursively verify nested
80 /// operations.
81 bool verifyRecursively;
83 } // namespace
85 LogicalResult OperationVerifier::verifyOpAndDominance(Operation &op) {
86 // Verify the operation first, collecting any IsolatedFromAbove operations.
87 if (failed(verifyOperation(op)))
88 return failure();
90 // Since everything looks structurally ok to this point, we do a dominance
91 // check for any nested regions. We do this as a second pass since malformed
92 // CFG's can cause dominator analysis construction to crash and we want the
93 // verifier to be resilient to malformed code.
94 if (op.getNumRegions() != 0) {
95 DominanceInfo domInfo;
96 if (failed(verifyDominanceOfContainedRegions(op, domInfo)))
97 return failure();
100 return success();
103 /// Returns true if this block may be valid without terminator. That is if:
104 /// - it does not have a parent region.
105 /// - Or the parent region have a single block and:
106 /// - This region does not have a parent op.
107 /// - Or the parent op is unregistered.
108 /// - Or the parent op has the NoTerminator trait.
109 static bool mayBeValidWithoutTerminator(Block *block) {
110 if (!block->getParent())
111 return true;
112 if (!llvm::hasSingleElement(*block->getParent()))
113 return false;
114 Operation *op = block->getParentOp();
115 return !op || op->mightHaveTrait<OpTrait::NoTerminator>();
118 LogicalResult OperationVerifier::verifyOnEntrance(Block &block) {
119 for (auto arg : block.getArguments())
120 if (arg.getOwner() != &block)
121 return emitError(arg.getLoc(), "block argument not owned by block");
123 // Verify that this block has a terminator.
124 if (block.empty()) {
125 if (mayBeValidWithoutTerminator(&block))
126 return success();
127 return emitError(block.getParent()->getLoc(),
128 "empty block: expect at least a terminator");
131 // Check each operation, and make sure there are no branches out of the
132 // middle of this block.
133 for (Operation &op : block) {
134 // Only the last instructions is allowed to have successors.
135 if (op.getNumSuccessors() != 0 && &op != &block.back())
136 return op.emitError(
137 "operation with block successors must terminate its parent block");
140 return success();
143 LogicalResult OperationVerifier::verifyOnExit(Block &block) {
144 // Verify that this block is not branching to a block of a different
145 // region.
146 for (Block *successor : block.getSuccessors())
147 if (successor->getParent() != block.getParent())
148 return block.back().emitOpError(
149 "branching to block of a different region");
151 // If this block doesn't have to have a terminator, don't require it.
152 if (mayBeValidWithoutTerminator(&block))
153 return success();
155 Operation &terminator = block.back();
156 if (!terminator.mightHaveTrait<OpTrait::IsTerminator>())
157 return block.back().emitError("block with no terminator, has ")
158 << terminator;
160 return success();
163 LogicalResult OperationVerifier::verifyOnEntrance(Operation &op) {
164 // Check that operands are non-nil and structurally ok.
165 for (auto operand : op.getOperands())
166 if (!operand)
167 return op.emitError("null operand found");
169 /// Verify that all of the attributes are okay.
170 for (auto attr : op.getDiscardableAttrDictionary()) {
171 // Check for any optional dialect specific attributes.
172 if (auto *dialect = attr.getNameDialect())
173 if (failed(dialect->verifyOperationAttribute(&op, attr)))
174 return failure();
177 // If we can get operation info for this, check the custom hook.
178 OperationName opName = op.getName();
179 std::optional<RegisteredOperationName> registeredInfo =
180 opName.getRegisteredInfo();
181 if (registeredInfo && failed(registeredInfo->verifyInvariants(&op)))
182 return failure();
184 unsigned numRegions = op.getNumRegions();
185 if (!numRegions)
186 return success();
187 auto kindInterface = dyn_cast<RegionKindInterface>(&op);
188 SmallVector<Operation *> opsWithIsolatedRegions;
189 // Verify that all child regions are ok.
190 MutableArrayRef<Region> regions = op.getRegions();
191 for (unsigned i = 0; i < numRegions; ++i) {
192 Region &region = regions[i];
193 RegionKind kind =
194 kindInterface ? kindInterface.getRegionKind(i) : RegionKind::SSACFG;
195 // Check that Graph Regions only have a single basic block. This is
196 // similar to the code in SingleBlockImplicitTerminator, but doesn't
197 // require the trait to be specified. This arbitrary limitation is
198 // designed to limit the number of cases that have to be handled by
199 // transforms and conversions.
200 if (op.isRegistered() && kind == RegionKind::Graph) {
201 // Non-empty regions must contain a single basic block.
202 if (!region.empty() && !region.hasOneBlock())
203 return op.emitOpError("expects graph region #")
204 << i << " to have 0 or 1 blocks";
207 if (region.empty())
208 continue;
210 // Verify the first block has no predecessors.
211 Block *firstBB = &region.front();
212 if (!firstBB->hasNoPredecessors())
213 return emitError(op.getLoc(),
214 "entry block of region may not have predecessors");
216 return success();
219 LogicalResult OperationVerifier::verifyOnExit(Operation &op) {
220 SmallVector<Operation *> opsWithIsolatedRegions;
221 if (verifyRecursively) {
222 for (Region &region : op.getRegions())
223 for (Block &block : region)
224 for (Operation &o : block)
225 if (o.getNumRegions() != 0 &&
226 o.hasTrait<OpTrait::IsIsolatedFromAbove>())
227 opsWithIsolatedRegions.push_back(&o);
229 if (failed(failableParallelForEach(
230 op.getContext(), opsWithIsolatedRegions,
231 [&](Operation *o) { return verifyOpAndDominance(*o); })))
232 return failure();
233 OperationName opName = op.getName();
234 std::optional<RegisteredOperationName> registeredInfo =
235 opName.getRegisteredInfo();
236 // After the region ops are verified, run the verifiers that have additional
237 // region invariants need to veirfy.
238 if (registeredInfo && failed(registeredInfo->verifyRegionInvariants(&op)))
239 return failure();
241 // If this is a registered operation, there is nothing left to do.
242 if (registeredInfo)
243 return success();
245 // Otherwise, verify that the parent dialect allows un-registered operations.
246 Dialect *dialect = opName.getDialect();
247 if (!dialect) {
248 if (!op.getContext()->allowsUnregisteredDialects()) {
249 return op.emitOpError()
250 << "created with unregistered dialect. If this is "
251 "intended, please call allowUnregisteredDialects() on the "
252 "MLIRContext, or use -allow-unregistered-dialect with "
253 "the MLIR opt tool used";
255 return success();
258 if (!dialect->allowsUnknownOperations()) {
259 return op.emitError("unregistered operation '")
260 << op.getName() << "' found in dialect ('" << dialect->getNamespace()
261 << "') that does not allow unknown operations";
264 return success();
267 /// Verify the properties and dominance relationships of this operation,
268 /// stopping region "recursion" at any "isolated from above operations".
269 /// Such ops are collected separately and verified inside
270 /// verifyBlockPostChildren.
271 LogicalResult OperationVerifier::verifyOperation(Operation &op) {
272 SmallVector<WorkItemEntry> worklist{{&op, false}};
273 while (!worklist.empty()) {
274 WorkItemEntry &top = worklist.back();
276 auto visit = [](auto &&visitor, WorkItem w) {
277 if (w.is<Operation *>())
278 return visitor(w.get<Operation *>());
279 return visitor(w.get<Block *>());
282 const bool isExit = top.getInt();
283 top.setInt(true);
284 auto item = top.getPointer();
286 // 2nd visit of this work item ("exit").
287 if (isExit) {
288 if (failed(
289 visit([this](auto *workItem) { return verifyOnExit(*workItem); },
290 item)))
291 return failure();
292 worklist.pop_back();
293 continue;
296 // 1st visit of this work item ("entrance").
297 if (failed(visit(
298 [this](auto *workItem) { return verifyOnEntrance(*workItem); },
299 item)))
300 return failure();
302 if (item.is<Block *>()) {
303 Block &currentBlock = *item.get<Block *>();
304 // Skip "isolated from above operations".
305 for (Operation &o : llvm::reverse(currentBlock)) {
306 if (o.getNumRegions() == 0 ||
307 !o.hasTrait<OpTrait::IsIsolatedFromAbove>())
308 worklist.emplace_back(&o);
310 continue;
313 Operation &currentOp = *item.get<Operation *>();
314 if (verifyRecursively)
315 for (Region &region : llvm::reverse(currentOp.getRegions()))
316 for (Block &block : llvm::reverse(region))
317 worklist.emplace_back(&block);
319 return success();
322 //===----------------------------------------------------------------------===//
323 // Dominance Checking
324 //===----------------------------------------------------------------------===//
326 /// Emit an error when the specified operand of the specified operation is an
327 /// invalid use because of dominance properties.
328 static void diagnoseInvalidOperandDominance(Operation &op, unsigned operandNo) {
329 InFlightDiagnostic diag = op.emitError("operand #")
330 << operandNo << " does not dominate this use";
332 Value operand = op.getOperand(operandNo);
334 /// Attach a note to an in-flight diagnostic that provide more information
335 /// about where an op operand is defined.
336 if (auto *useOp = operand.getDefiningOp()) {
337 Diagnostic &note = diag.attachNote(useOp->getLoc());
338 note << "operand defined here";
339 Block *block1 = op.getBlock();
340 Block *block2 = useOp->getBlock();
341 Region *region1 = block1->getParent();
342 Region *region2 = block2->getParent();
343 if (block1 == block2)
344 note << " (op in the same block)";
345 else if (region1 == region2)
346 note << " (op in the same region)";
347 else if (region2->isProperAncestor(region1))
348 note << " (op in a parent region)";
349 else if (region1->isProperAncestor(region2))
350 note << " (op in a child region)";
351 else
352 note << " (op is neither in a parent nor in a child region)";
353 return;
355 // Block argument case.
356 Block *block1 = op.getBlock();
357 Block *block2 = llvm::cast<BlockArgument>(operand).getOwner();
358 Region *region1 = block1->getParent();
359 Region *region2 = block2->getParent();
360 Location loc = UnknownLoc::get(op.getContext());
361 if (block2->getParentOp())
362 loc = block2->getParentOp()->getLoc();
363 Diagnostic &note = diag.attachNote(loc);
364 if (!region2) {
365 note << " (block without parent)";
366 return;
368 if (block1 == block2)
369 llvm::report_fatal_error("Internal error in dominance verification");
370 int index = std::distance(region2->begin(), block2->getIterator());
371 note << "operand defined as a block argument (block #" << index;
372 if (region1 == region2)
373 note << " in the same region)";
374 else if (region2->isProperAncestor(region1))
375 note << " in a parent region)";
376 else if (region1->isProperAncestor(region2))
377 note << " in a child region)";
378 else
379 note << " neither in a parent nor in a child region)";
382 /// Verify the dominance of each of the nested blocks within the given operation
383 LogicalResult
384 OperationVerifier::verifyDominanceOfContainedRegions(Operation &op,
385 DominanceInfo &domInfo) {
386 llvm::SmallVector<Operation *, 8> worklist{&op};
387 while (!worklist.empty()) {
388 auto *op = worklist.pop_back_val();
389 for (auto &region : op->getRegions())
390 for (auto &block : region.getBlocks()) {
391 // Dominance is only meaningful inside reachable blocks.
392 bool isReachable = domInfo.isReachableFromEntry(&block);
393 for (auto &op : block) {
394 if (isReachable) {
395 // Check that operands properly dominate this use.
396 for (const auto &operand : llvm::enumerate(op.getOperands())) {
397 if (domInfo.properlyDominates(operand.value(), &op))
398 continue;
400 diagnoseInvalidOperandDominance(op, operand.index());
401 return failure();
405 // Recursively verify dominance within each operation in the block,
406 // even if the block itself is not reachable, or we are in a region
407 // which doesn't respect dominance.
408 if (verifyRecursively && op.getNumRegions() != 0) {
409 // If this operation is IsolatedFromAbove, then we'll handle it in
410 // the outer verification loop.
411 if (op.hasTrait<OpTrait::IsIsolatedFromAbove>())
412 continue;
413 worklist.push_back(&op);
419 return success();
422 //===----------------------------------------------------------------------===//
423 // Entrypoint
424 //===----------------------------------------------------------------------===//
426 LogicalResult mlir::verify(Operation *op, bool verifyRecursively) {
427 OperationVerifier verifier(verifyRecursively);
428 return verifier.verifyOpAndDominance(*op);