| 1 | //===- VerificationUtils.cpp - Common verification utilities --------------===// |
| 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 | #include "mlir/Dialect/Utils/VerificationUtils.h" |
| 10 | |
| 11 | using namespace mlir; |
| 12 | |
| 13 | LogicalResult mlir::verifyDynamicDimensionCount(Operation *op, ShapedType type, |
| 14 | ValueRange dynamicSizes) { |
| 15 | int64_t expectedCount = type.getNumDynamicDims(); |
| 16 | int64_t actualCount = dynamicSizes.size(); |
| 17 | if (expectedCount != actualCount) { |
| 18 | return op->emitOpError(message: "incorrect number of dynamic sizes, has " ) |
| 19 | << actualCount << ", expected " << expectedCount; |
| 20 | } |
| 21 | return success(); |
| 22 | } |
| 23 | |
| 24 | LogicalResult mlir::verifyRanksMatch(Operation *op, ShapedType lhs, |
| 25 | ShapedType rhs, StringRef lhsName, |
| 26 | StringRef rhsName) { |
| 27 | if (!lhs.hasRank() || !rhs.hasRank()) |
| 28 | return success(); // Unranked types are considered compatible |
| 29 | |
| 30 | int64_t rank1 = lhs.getRank(); |
| 31 | int64_t rank2 = rhs.getRank(); |
| 32 | if (rank1 != rank2) { |
| 33 | return op->emitOpError() |
| 34 | << lhsName << " rank (" << rank1 << ") does not match " << rhsName |
| 35 | << " rank (" << rank2 << ")" ; |
| 36 | } |
| 37 | return success(); |
| 38 | } |
| 39 | |
| 40 | LogicalResult mlir::verifyElementTypesMatch(Operation *op, ShapedType lhs, |
| 41 | ShapedType rhs, StringRef lhsName, |
| 42 | StringRef rhsName) { |
| 43 | Type lhsElementType = lhs.getElementType(); |
| 44 | Type rhsElementType = rhs.getElementType(); |
| 45 | if (lhsElementType != rhsElementType) { |
| 46 | return op->emitOpError() << lhsName << " element type (" << lhsElementType |
| 47 | << ") does not match " << rhsName |
| 48 | << " element type (" << rhsElementType << ")" ; |
| 49 | } |
| 50 | return success(); |
| 51 | } |
| 52 | |