1 //===- ViewLikeInterface.cpp - View-like operations in MLIR ---------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "mlir/Interfaces/ViewLikeInterface.h"
13 //===----------------------------------------------------------------------===//
14 // ViewLike Interfaces
15 //===----------------------------------------------------------------------===//
17 /// Include the definitions of the loop-like interfaces.
18 #include "mlir/Interfaces/ViewLikeInterface.cpp.inc"
20 LogicalResult
mlir::verifyListOfOperandsOrIntegers(Operation
*op
,
23 ArrayRef
<int64_t> staticVals
,
25 // Check static and dynamic offsets/sizes/strides does not overflow type.
26 if (staticVals
.size() != numElements
)
27 return op
->emitError("expected ")
28 << numElements
<< " " << name
<< " values";
29 unsigned expectedNumDynamicEntries
=
30 llvm::count_if(staticVals
, [&](int64_t staticVal
) {
31 return ShapedType::isDynamic(staticVal
);
33 if (values
.size() != expectedNumDynamicEntries
)
34 return op
->emitError("expected ")
35 << expectedNumDynamicEntries
<< " dynamic " << name
<< " values";
40 mlir::detail::verifyOffsetSizeAndStrideOp(OffsetSizeAndStrideOpInterface op
) {
41 std::array
<unsigned, 3> maxRanks
= op
.getArrayAttrMaxRanks();
42 // Offsets can come in 2 flavors:
43 // 1. Either single entry (when maxRanks == 1).
44 // 2. Or as an array whose rank must match that of the mixed sizes.
45 // So that the result type is well-formed.
46 if (!(op
.getMixedOffsets().size() == 1 && maxRanks
[0] == 1) && // NOLINT
47 op
.getMixedOffsets().size() != op
.getMixedSizes().size())
49 "expected mixed offsets rank to match mixed sizes rank (")
50 << op
.getMixedOffsets().size() << " vs " << op
.getMixedSizes().size()
51 << ") so the rank of the result type is well-formed.";
52 // Ranks of mixed sizes and strides must always match so the result type is
54 if (op
.getMixedSizes().size() != op
.getMixedStrides().size())
56 "expected mixed sizes rank to match mixed strides rank (")
57 << op
.getMixedSizes().size() << " vs " << op
.getMixedStrides().size()
58 << ") so the rank of the result type is well-formed.";
60 if (failed(verifyListOfOperandsOrIntegers(op
, "offset", maxRanks
[0],
61 op
.static_offsets(), op
.offsets())))
63 if (failed(verifyListOfOperandsOrIntegers(op
, "size", maxRanks
[1],
64 op
.static_sizes(), op
.sizes())))
66 if (failed(verifyListOfOperandsOrIntegers(op
, "stride", maxRanks
[2],
67 op
.static_strides(), op
.strides())))
72 void mlir::printDynamicIndexList(OpAsmPrinter
&printer
, Operation
*op
,
74 ArrayRef
<int64_t> integers
) {
76 if (integers
.empty()) {
81 llvm::interleaveComma(integers
, printer
, [&](int64_t integer
) {
82 if (ShapedType::isDynamic(integer
))
83 printer
<< values
[idx
++];
90 ParseResult
mlir::parseDynamicIndexList(
92 SmallVectorImpl
<OpAsmParser::UnresolvedOperand
> &values
,
93 DenseI64ArrayAttr
&integers
) {
95 SmallVector
<int64_t, 4> integerVals
;
96 auto parseIntegerOrValue
= [&]() {
97 OpAsmParser::UnresolvedOperand operand
;
98 auto res
= parser
.parseOptionalOperand(operand
);
99 if (res
.has_value() && succeeded(res
.value())) {
100 values
.push_back(operand
);
101 integerVals
.push_back(ShapedType::kDynamic
);
104 if (failed(parser
.parseInteger(integer
)))
106 integerVals
.push_back(integer
);
110 if (parser
.parseCommaSeparatedList(OpAsmParser::Delimiter::Square
,
112 " in dynamic index list"))
113 return parser
.emitError(parser
.getNameLoc())
114 << "expected SSA value or integer";
115 integers
= parser
.getBuilder().getDenseI64ArrayAttr(integerVals
);
119 bool mlir::detail::sameOffsetsSizesAndStrides(
120 OffsetSizeAndStrideOpInterface a
, OffsetSizeAndStrideOpInterface b
,
121 llvm::function_ref
<bool(OpFoldResult
, OpFoldResult
)> cmp
) {
122 if (a
.static_offsets().size() != b
.static_offsets().size())
124 if (a
.static_sizes().size() != b
.static_sizes().size())
126 if (a
.static_strides().size() != b
.static_strides().size())
128 for (auto it
: llvm::zip(a
.getMixedOffsets(), b
.getMixedOffsets()))
129 if (!cmp(std::get
<0>(it
), std::get
<1>(it
)))
131 for (auto it
: llvm::zip(a
.getMixedSizes(), b
.getMixedSizes()))
132 if (!cmp(std::get
<0>(it
), std::get
<1>(it
)))
134 for (auto it
: llvm::zip(a
.getMixedStrides(), b
.getMixedStrides()))
135 if (!cmp(std::get
<0>(it
), std::get
<1>(it
)))