1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
|
//===- Verifier.cpp - MLIR Verifier Implementation ------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements the verify() methods on the various IR types, performing
// (potentially expensive) checks on the holistic structure of the code. This
// can be used for detecting bugs in compiler transformations and hand written
// .mlir files.
//
// The checks in this file are only for things that can occur as part of IR
// transformations: e.g. violation of dominance information, malformed operation
// attributes, etc. MLIR supports transformations moving IR through locally
// invalid states (e.g. unlinking an operation from a block before re-inserting
// it in a new place), but each transformation must complete with the IR in a
// valid form.
//
// This should not check for things that are always wrong by construction (e.g.
// attributes or other immutable structures that are incorrect), because those
// are not mutable and can be checked at time of construction.
//
//===----------------------------------------------------------------------===//
#include "mlir/IR/Verifier.h"
#include "mlir/IR/Attributes.h"
#include "mlir/IR/Dialect.h"
#include "mlir/IR/Dominance.h"
#include "mlir/IR/Operation.h"
#include "mlir/IR/RegionKindInterface.h"
#include "mlir/IR/Threading.h"
#include "llvm/ADT/DenseMapInfoVariant.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/Regex.h"
#include <atomic>
#include <optional>
using namespace mlir;
namespace {
/// This class encapsulates all the state used to verify an operation region.
class OperationVerifier {
public:
/// If `verifyRecursively` is true, then this will also recursively verify
/// nested operations.
explicit OperationVerifier(bool verifyRecursively)
: verifyRecursively(verifyRecursively) {}
/// Verify the given operation.
LogicalResult verifyOpAndDominance(Operation &op);
private:
using WorkItem = llvm::PointerUnion<Operation *, Block *>;
/// This verifier uses a DFS of the tree of operations/blocks. The method
/// verifyOnEntrance is invoked when we visit a node for the first time, i.e.
/// before visiting its children. The method verifyOnExit is invoked
/// upon exit from the subtree, i.e. when we visit a node for the second time.
LogicalResult verifyOnEntrance(Block &block);
LogicalResult verifyOnEntrance(Operation &op);
LogicalResult verifyOnExit(Block &block);
LogicalResult verifyOnExit(Operation &op);
/// Verify the properties and dominance relationships of this operation.
LogicalResult verifyOperation(Operation &op);
/// Verify the dominance property of regions contained within the given
/// Operation.
LogicalResult verifyDominanceOfContainedRegions(Operation &op,
DominanceInfo &domInfo);
/// A flag indicating if this verifier should recursively verify nested
/// operations.
bool verifyRecursively;
};
} // namespace
LogicalResult OperationVerifier::verifyOpAndDominance(Operation &op) {
// Verify the operation first, collecting any IsolatedFromAbove operations.
if (failed(verifyOperation(op)))
return failure();
// Since everything looks structurally ok to this point, we do a dominance
// check for any nested regions. We do this as a second pass since malformed
// CFG's can cause dominator analysis construction to crash and we want the
// verifier to be resilient to malformed code.
if (op.getNumRegions() != 0) {
DominanceInfo domInfo;
if (failed(verifyDominanceOfContainedRegions(op, domInfo)))
return failure();
}
return success();
}
/// Returns true if this block may be valid without terminator. That is if:
/// - it does not have a parent region.
/// - Or the parent region have a single block and:
/// - This region does not have a parent op.
/// - Or the parent op is unregistered.
/// - Or the parent op has the NoTerminator trait.
static bool mayBeValidWithoutTerminator(Block *block) {
if (!block->getParent())
return true;
if (!llvm::hasSingleElement(*block->getParent()))
return false;
Operation *op = block->getParentOp();
return !op || op->mightHaveTrait<OpTrait::NoTerminator>();
}
LogicalResult OperationVerifier::verifyOnEntrance(Block &block) {
for (auto arg : block.getArguments())
if (arg.getOwner() != &block)
return emitError(arg.getLoc(), "block argument not owned by block");
// Verify that this block has a terminator.
if (block.empty()) {
if (mayBeValidWithoutTerminator(&block))
return success();
return emitError(block.getParent()->getLoc(),
"empty block: expect at least a terminator");
}
// Check each operation, and make sure there are no branches out of the
// middle of this block.
for (Operation &op : block) {
// Only the last instructions is allowed to have successors.
if (op.getNumSuccessors() != 0 && &op != &block.back())
return op.emitError(
"operation with block successors must terminate its parent block");
}
return success();
}
LogicalResult OperationVerifier::verifyOnExit(Block &block) {
// Verify that this block is not branching to a block of a different
// region.
for (Block *successor : block.getSuccessors())
if (successor->getParent() != block.getParent())
return block.back().emitOpError(
"branching to block of a different region");
// If this block doesn't have to have a terminator, don't require it.
if (mayBeValidWithoutTerminator(&block))
return success();
Operation &terminator = block.back();
if (!terminator.mightHaveTrait<OpTrait::IsTerminator>())
return block.back().emitError("block with no terminator, has ")
<< terminator;
return success();
}
LogicalResult OperationVerifier::verifyOnEntrance(Operation &op) {
// Check that operands are non-nil and structurally ok.
for (auto operand : op.getOperands())
if (!operand)
return op.emitError("null operand found");
/// Verify that all of the attributes are okay.
for (auto attr : op.getDiscardableAttrDictionary()) {
// Check for any optional dialect specific attributes.
if (auto *dialect = attr.getNameDialect())
if (failed(dialect->verifyOperationAttribute(&op, attr)))
return failure();
}
// If we can get operation info for this, check the custom hook.
OperationName opName = op.getName();
std::optional<RegisteredOperationName> registeredInfo =
opName.getRegisteredInfo();
if (registeredInfo && failed(registeredInfo->verifyInvariants(&op)))
return failure();
unsigned numRegions = op.getNumRegions();
if (!numRegions)
return success();
auto kindInterface = dyn_cast<RegionKindInterface>(&op);
SmallVector<Operation *> opsWithIsolatedRegions;
// Verify that all child regions are ok.
MutableArrayRef<Region> regions = op.getRegions();
for (unsigned i = 0; i < numRegions; ++i) {
Region ®ion = regions[i];
RegionKind kind =
kindInterface ? kindInterface.getRegionKind(i) : RegionKind::SSACFG;
// Check that Graph Regions only have a single basic block. This is
// similar to the code in SingleBlockImplicitTerminator, but doesn't
// require the trait to be specified. This arbitrary limitation is
// designed to limit the number of cases that have to be handled by
// transforms and conversions.
if (op.isRegistered() && kind == RegionKind::Graph) {
// Non-empty regions must contain a single basic block.
if (!region.empty() && !region.hasOneBlock())
return op.emitOpError("expects graph region #")
<< i << " to have 0 or 1 blocks";
}
if (region.empty())
continue;
// Verify the first block has no predecessors.
Block *firstBB = ®ion.front();
if (!firstBB->hasNoPredecessors())
return emitError(op.getLoc(),
"entry block of region may not have predecessors");
}
return success();
}
LogicalResult OperationVerifier::verifyOnExit(Operation &op) {
SmallVector<Operation *> opsWithIsolatedRegions;
if (verifyRecursively) {
for (Region ®ion : op.getRegions())
for (Block &block : region)
for (Operation &o : block)
if (o.getNumRegions() != 0 &&
o.hasTrait<OpTrait::IsIsolatedFromAbove>())
opsWithIsolatedRegions.push_back(&o);
}
if (failed(failableParallelForEach(
op.getContext(), opsWithIsolatedRegions,
[&](Operation *o) { return verifyOpAndDominance(*o); })))
return failure();
OperationName opName = op.getName();
std::optional<RegisteredOperationName> registeredInfo =
opName.getRegisteredInfo();
// After the region ops are verified, run the verifiers that have additional
// region invariants need to veirfy.
if (registeredInfo && failed(registeredInfo->verifyRegionInvariants(&op)))
return failure();
// If this is a registered operation, there is nothing left to do.
if (registeredInfo)
return success();
// Otherwise, verify that the parent dialect allows un-registered operations.
Dialect *dialect = opName.getDialect();
if (!dialect) {
if (!op.getContext()->allowsUnregisteredDialects()) {
return op.emitOpError()
<< "created with unregistered dialect. If this is "
"intended, please call allowUnregisteredDialects() on the "
"MLIRContext, or use -allow-unregistered-dialect with "
"the MLIR opt tool used";
}
return success();
}
if (!dialect->allowsUnknownOperations()) {
return op.emitError("unregistered operation '")
<< op.getName() << "' found in dialect ('" << dialect->getNamespace()
<< "') that does not allow unknown operations";
}
return success();
}
/// Verify the properties and dominance relationships of this operation,
/// stopping region "recursion" at any "isolated from above operations".
/// Such ops are collected separately and verified inside
/// verifyBlockPostChildren.
LogicalResult OperationVerifier::verifyOperation(Operation &op) {
SmallVector<WorkItem> worklist{{&op}};
DenseSet<WorkItem> seen;
while (!worklist.empty()) {
WorkItem top = worklist.back();
auto visit = [](auto &&visitor, WorkItem w) {
if (w.is<Operation *>())
return visitor(w.get<Operation *>());
return visitor(w.get<Block *>());
};
const bool isExit = !seen.insert(top).second;
// 2nd visit of this work item ("exit").
if (isExit) {
worklist.pop_back();
if (failed(visit(
[this](auto *workItem) { return verifyOnExit(*workItem); }, top)))
return failure();
continue;
}
// 1st visit of this work item ("entrance").
if (failed(visit(
[this](auto *workItem) { return verifyOnEntrance(*workItem); },
top)))
return failure();
if (top.is<Block *>()) {
Block ¤tBlock = *top.get<Block *>();
// Skip "isolated from above operations".
for (Operation &o : llvm::reverse(currentBlock)) {
if (o.getNumRegions() == 0 ||
!o.hasTrait<OpTrait::IsIsolatedFromAbove>())
worklist.emplace_back(&o);
}
continue;
}
Operation ¤tOp = *top.get<Operation *>();
if (verifyRecursively)
for (Region ®ion : llvm::reverse(currentOp.getRegions()))
for (Block &block : llvm::reverse(region))
worklist.emplace_back(&block);
}
return success();
}
//===----------------------------------------------------------------------===//
// Dominance Checking
//===----------------------------------------------------------------------===//
/// Emit an error when the specified operand of the specified operation is an
/// invalid use because of dominance properties.
static void diagnoseInvalidOperandDominance(Operation &op, unsigned operandNo) {
InFlightDiagnostic diag = op.emitError("operand #")
<< operandNo << " does not dominate this use";
Value operand = op.getOperand(operandNo);
/// Attach a note to an in-flight diagnostic that provide more information
/// about where an op operand is defined.
if (auto *useOp = operand.getDefiningOp()) {
Diagnostic ¬e = diag.attachNote(useOp->getLoc());
note << "operand defined here";
Block *block1 = op.getBlock();
Block *block2 = useOp->getBlock();
Region *region1 = block1->getParent();
Region *region2 = block2->getParent();
if (block1 == block2)
note << " (op in the same block)";
else if (region1 == region2)
note << " (op in the same region)";
else if (region2->isProperAncestor(region1))
note << " (op in a parent region)";
else if (region1->isProperAncestor(region2))
note << " (op in a child region)";
else
note << " (op is neither in a parent nor in a child region)";
return;
}
// Block argument case.
Block *block1 = op.getBlock();
Block *block2 = llvm::cast<BlockArgument>(operand).getOwner();
Region *region1 = block1->getParent();
Region *region2 = block2->getParent();
Location loc = UnknownLoc::get(op.getContext());
if (block2->getParentOp())
loc = block2->getParentOp()->getLoc();
Diagnostic ¬e = diag.attachNote(loc);
if (!region2) {
note << " (block without parent)";
return;
}
if (block1 == block2)
llvm::report_fatal_error("Internal error in dominance verification");
int index = std::distance(region2->begin(), block2->getIterator());
note << "operand defined as a block argument (block #" << index;
if (region1 == region2)
note << " in the same region)";
else if (region2->isProperAncestor(region1))
note << " in a parent region)";
else if (region1->isProperAncestor(region2))
note << " in a child region)";
else
note << " neither in a parent nor in a child region)";
}
/// Verify the dominance of each of the nested blocks within the given operation
LogicalResult
OperationVerifier::verifyDominanceOfContainedRegions(Operation &op,
DominanceInfo &domInfo) {
for (Region ®ion : op.getRegions()) {
// Verify the dominance of each of the held operations.
for (Block &block : region) {
// Dominance is only meaningful inside reachable blocks.
bool isReachable = domInfo.isReachableFromEntry(&block);
for (Operation &op : block) {
if (isReachable) {
// Check that operands properly dominate this use.
for (const auto &operand : llvm::enumerate(op.getOperands())) {
if (domInfo.properlyDominates(operand.value(), &op))
continue;
diagnoseInvalidOperandDominance(op, operand.index());
return failure();
}
}
// Recursively verify dominance within each operation in the block, even
// if the block itself is not reachable, or we are in a region which
// doesn't respect dominance.
if (verifyRecursively && op.getNumRegions() != 0) {
// If this operation is IsolatedFromAbove, then we'll handle it in the
// outer verification loop.
if (op.hasTrait<OpTrait::IsIsolatedFromAbove>())
continue;
if (failed(verifyDominanceOfContainedRegions(op, domInfo)))
return failure();
}
}
}
}
return success();
}
//===----------------------------------------------------------------------===//
// Entrypoint
//===----------------------------------------------------------------------===//
LogicalResult mlir::verify(Operation *op, bool verifyRecursively) {
OperationVerifier verifier(verifyRecursively);
return verifier.verifyOpAndDominance(*op);
}
|