1 //===- ViewLikeInterface.cpp - View-like operations in MLIR ---------------===// 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/Interfaces/ViewLikeInterface.h" 10 11 #include "mlir/IR/StandardTypes.h" 12 13 using namespace mlir; 14 15 //===----------------------------------------------------------------------===// 16 // ViewLike Interfaces 17 //===----------------------------------------------------------------------===// 18 19 /// Include the definitions of the loop-like interfaces. 20 #include "mlir/Interfaces/ViewLikeInterface.cpp.inc" 21 22 static LogicalResult verifyOpWithOffsetSizesAndStridesPart( 23 OffsetSizeAndStrideOpInterface op, StringRef name, 24 unsigned expectedNumElements, StringRef attrName, ArrayAttr attr, 25 llvm::function_ref<bool(int64_t)> isDynamic, ValueRange values) { 26 /// Check static and dynamic offsets/sizes/strides breakdown. 27 if (attr.size() != expectedNumElements) 28 return op.emitError("expected ") 29 << expectedNumElements << " " << name << " values"; 30 unsigned expectedNumDynamicEntries = 31 llvm::count_if(attr.getValue(), [&](Attribute attr) { 32 return isDynamic(attr.cast<IntegerAttr>().getInt()); 33 }); 34 if (values.size() != expectedNumDynamicEntries) 35 return op.emitError("expected ") 36 << expectedNumDynamicEntries << " dynamic " << name << " values"; 37 return success(); 38 } 39 40 LogicalResult mlir::verify(OffsetSizeAndStrideOpInterface op) { 41 std::array<unsigned, 3> ranks = op.getArrayAttrRanks(); 42 if (failed(verifyOpWithOffsetSizesAndStridesPart( 43 op, "offset", ranks[0], 44 OffsetSizeAndStrideOpInterface::getStaticOffsetsAttrName(), 45 op.static_offsets(), ShapedType::isDynamicStrideOrOffset, 46 op.offsets()))) 47 return failure(); 48 if (failed(verifyOpWithOffsetSizesAndStridesPart( 49 op, "size", ranks[1], 50 OffsetSizeAndStrideOpInterface::getStaticSizesAttrName(), 51 op.static_sizes(), ShapedType::isDynamic, op.sizes()))) 52 return failure(); 53 if (failed(verifyOpWithOffsetSizesAndStridesPart( 54 op, "stride", ranks[2], 55 OffsetSizeAndStrideOpInterface::getStaticStridesAttrName(), 56 op.static_strides(), ShapedType::isDynamicStrideOrOffset, 57 op.strides()))) 58 return failure(); 59 return success(); 60 } 61