1//===- InferTypeOpInterface.cpp - Infer Type Interfaces ---------*- C++ -*-===//
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 contains the definitions of the infer op interfaces defined in
10// `InferTypeOpInterface.td`.
11//
12//===----------------------------------------------------------------------===//
13
14#include "mlir/Interfaces/InferTypeOpInterface.h"
15#include "mlir/IR/BuiltinTypes.h"
16#include "mlir/IR/Matchers.h"
17#include "llvm/Support/FormatVariadic.h"
18#include "llvm/Support/InterleavedRange.h"
19
20using namespace mlir;
21
22namespace mlir {
23#include "mlir/Interfaces/InferTypeOpInterface.cpp.inc"
24} // namespace mlir
25
26LogicalResult
27mlir::reifyResultShapes(OpBuilder &b, Operation *op,
28 ReifiedRankedShapedTypeDims &reifiedReturnShapes) {
29 auto reifiableOp = dyn_cast<ReifyRankedShapedTypeOpInterface>(Val: op);
30 if (!reifiableOp)
31 return failure();
32 LogicalResult status = reifiableOp.reifyResultShapes(builder&: b, reifiedReturnShapes);
33#ifndef NDEBUG
34 if (failed(status))
35 return failure();
36 // Assert that ReifyRankedShapedTypeOpInterface::reifyResultShapes produced
37 // a correct result.
38 int64_t resultIdx = 0;
39 for (OpResult result : op->getResults()) {
40 auto shapedType = dyn_cast<ShapedType>(result.getType());
41 if (!shapedType)
42 continue;
43 if (!shapedType.hasRank()) {
44 // Nothing to check for unranked shaped values.
45 ++resultIdx;
46 continue;
47 }
48 // Assert one OpFoldResult per dimension.
49 assert(shapedType.getRank() ==
50 static_cast<int64_t>(reifiedReturnShapes[resultIdx].size()) &&
51 "incorrect implementation of ReifyRankedShapedTypeOpInterface");
52 ++resultIdx;
53 }
54 // Assert that every shaped value result was reified.
55 assert(resultIdx == static_cast<int64_t>(reifiedReturnShapes.size()) &&
56 "incorrect implementation of ReifyRankedShapedTypeOpInterface");
57#endif // NDEBUG
58 return status;
59}
60
61FailureOr<SmallVector<OpFoldResult>>
62mlir::reifyShapeOfResult(OpBuilder &b, Operation *op, int resultIndex) {
63 auto reifiableOp = dyn_cast<ReifyRankedShapedTypeOpInterface>(Val: op);
64 if (!reifiableOp)
65 return failure();
66 return reifiableOp.reifyShapeOfResult(builder&: b, resultIndex);
67}
68
69FailureOr<OpFoldResult> mlir::reifyDimOfResult(OpBuilder &b, Operation *op,
70 int resultIndex, int dim) {
71 auto reifiableOp = dyn_cast<ReifyRankedShapedTypeOpInterface>(Val: op);
72 if (!reifiableOp)
73 return failure();
74 return reifiableOp.reifyDimOfResult(builder&: b, resultIndex, dim);
75}
76
77bool ShapeAdaptor::hasRank() const {
78 if (val.isNull())
79 return false;
80 if (auto t = llvm::dyn_cast_if_present<Type>(Val: val))
81 return cast<ShapedType>(Val&: t).hasRank();
82 if (isa<Attribute>(Val: val))
83 return true;
84 return cast<ShapedTypeComponents *>(Val: val)->hasRank();
85}
86
87Type ShapeAdaptor::getElementType() const {
88 if (val.isNull())
89 return nullptr;
90 if (auto t = llvm::dyn_cast_if_present<Type>(Val: val))
91 return cast<ShapedType>(Val&: t).getElementType();
92 if (isa<Attribute>(Val: val))
93 return nullptr;
94 return cast<ShapedTypeComponents *>(Val: val)->getElementType();
95}
96
97void ShapeAdaptor::getDims(SmallVectorImpl<int64_t> &res) const {
98 assert(hasRank());
99 if (auto t = llvm::dyn_cast_if_present<Type>(Val: val)) {
100 ArrayRef<int64_t> vals = cast<ShapedType>(Val&: t).getShape();
101 res.assign(in_start: vals.begin(), in_end: vals.end());
102 } else if (auto attr = llvm::dyn_cast_if_present<Attribute>(Val: val)) {
103 auto dattr = cast<DenseIntElementsAttr>(Val&: attr);
104 res.clear();
105 res.reserve(N: dattr.size());
106 for (auto it : dattr.getValues<APInt>())
107 res.push_back(Elt: it.getSExtValue());
108 } else {
109 auto vals = cast<ShapedTypeComponents *>(Val: val)->getDims();
110 res.assign(in_start: vals.begin(), in_end: vals.end());
111 }
112}
113
114void ShapeAdaptor::getDims(ShapedTypeComponents &res) const {
115 assert(hasRank());
116 res.ranked = true;
117 getDims(res&: res.dims);
118}
119
120int64_t ShapeAdaptor::getDimSize(int index) const {
121 assert(hasRank());
122 if (auto t = llvm::dyn_cast_if_present<Type>(Val: val))
123 return cast<ShapedType>(Val&: t).getDimSize(idx: index);
124 if (auto attr = llvm::dyn_cast_if_present<Attribute>(Val: val))
125 return cast<DenseIntElementsAttr>(Val&: attr)
126 .getValues<APInt>()[index]
127 .getSExtValue();
128 auto *stc = cast<ShapedTypeComponents *>(Val: val);
129 return stc->getDims()[index];
130}
131
132int64_t ShapeAdaptor::getRank() const {
133 assert(hasRank());
134 if (auto t = llvm::dyn_cast_if_present<Type>(Val: val))
135 return cast<ShapedType>(Val&: t).getRank();
136 if (auto attr = llvm::dyn_cast_if_present<Attribute>(Val: val))
137 return cast<DenseIntElementsAttr>(Val&: attr).size();
138 return cast<ShapedTypeComponents *>(Val: val)->getDims().size();
139}
140
141bool ShapeAdaptor::hasStaticShape() const {
142 if (!hasRank())
143 return false;
144
145 if (auto t = llvm::dyn_cast_if_present<Type>(Val: val))
146 return cast<ShapedType>(Val&: t).hasStaticShape();
147 if (auto attr = llvm::dyn_cast_if_present<Attribute>(Val: val)) {
148 auto dattr = cast<DenseIntElementsAttr>(Val&: attr);
149 for (auto index : dattr.getValues<APInt>())
150 if (ShapedType::isDynamic(dValue: index.getSExtValue()))
151 return false;
152 return true;
153 }
154 auto *stc = cast<ShapedTypeComponents *>(Val: val);
155 return llvm::none_of(Range: stc->getDims(), P: ShapedType::isDynamic);
156}
157
158int64_t ShapeAdaptor::getNumElements() const {
159 assert(hasStaticShape() && "cannot get element count of dynamic shaped type");
160
161 if (auto t = llvm::dyn_cast_if_present<Type>(Val: val))
162 return cast<ShapedType>(Val&: t).getNumElements();
163
164 if (auto attr = llvm::dyn_cast_if_present<Attribute>(Val: val)) {
165 auto dattr = cast<DenseIntElementsAttr>(Val&: attr);
166 int64_t num = 1;
167 for (auto index : dattr.getValues<APInt>()) {
168 num *= index.getZExtValue();
169 assert(num >= 0 && "integer overflow in element count computation");
170 }
171 return num;
172 }
173
174 auto *stc = cast<ShapedTypeComponents *>(Val: val);
175 int64_t num = 1;
176 for (int64_t dim : stc->getDims()) {
177 num *= dim;
178 assert(num >= 0 && "integer overflow in element count computation");
179 }
180 return num;
181}
182
183#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
184void ShapeAdaptor::dump() const {
185 if (!hasRank()) {
186 llvm::errs() << "<<unranked>>\n";
187 return;
188 }
189
190 SmallVector<int64_t> dims;
191 getDims(dims);
192 auto mapped = llvm::map_range(dims, [](int64_t dim) -> std::string {
193 if (ShapedType::isDynamic(dim))
194 return "?";
195 return llvm::formatv("{0}", dim).str();
196 });
197 llvm::errs() << "rank = " << getRank()
198 << " dims = " << llvm::interleaved_array(mapped, "x") << "\n";
199}
200#endif
201
202ShapeAdaptor ValueShapeRange::getValueAsShape(int index) {
203 Value val = operator[](Index: index);
204 if (valueToShape)
205 if (ShapeAdaptor ret = valueToShape(val))
206 return ret;
207
208 DenseIntElementsAttr attr;
209 if (!matchPattern(value: val, pattern: m_Constant(bind_value: &attr)))
210 return nullptr;
211 if (attr.getType().getRank() != 1)
212 return nullptr;
213 return attr;
214}
215
216ShapeAdaptor ValueShapeRange::getShape(Value val) const {
217 if (operandShape)
218 if (ShapeAdaptor ret = operandShape(val))
219 return ret;
220 return val.getType();
221}
222
223ShapeAdaptor ValueShapeRange::getShape(int index) const {
224 if (index < 0 || static_cast<size_t>(index) >= size())
225 return nullptr;
226 return getShape(val: operator[](Index: index));
227}
228
229LogicalResult mlir::detail::inferReturnTensorTypes(
230 ArrayRef<ShapedTypeComponents> retComponents,
231 SmallVectorImpl<Type> &inferredReturnTypes) {
232 for (const auto &shapeAndType : retComponents) {
233 Type elementTy = shapeAndType.getElementType();
234 assert(elementTy && "element type required to construct tensor");
235
236 Attribute attr = shapeAndType.getAttribute();
237 if (shapeAndType.hasRank()) {
238 inferredReturnTypes.push_back(
239 Elt: RankedTensorType::get(shape: shapeAndType.getDims(), elementType: elementTy, encoding: attr));
240 } else {
241 assert(attr == nullptr && "attribute not supported");
242 inferredReturnTypes.push_back(Elt: UnrankedTensorType::get(elementType: elementTy));
243 }
244 }
245 return success();
246}
247
248LogicalResult mlir::detail::verifyInferredResultTypes(Operation *op) {
249 SmallVector<Type, 4> inferredReturnTypes(op->getResultTypes());
250 auto retTypeFn = cast<InferTypeOpInterface>(Val: op);
251 auto result = retTypeFn.refineReturnTypes(
252 context: op->getContext(), location: op->getLoc(), operands: op->getOperands(),
253 attributes: op->getRawDictionaryAttrs(), properties: op->getPropertiesStorage(), regions: op->getRegions(),
254 returnTypes&: inferredReturnTypes);
255 if (failed(Result: result))
256 op->emitOpError() << "failed to infer returned types";
257
258 return result;
259}
260
261void mlir::detail::reportFatalInferReturnTypesError(OperationState &state) {
262 std::string buffer;
263 llvm::raw_string_ostream os(buffer);
264 os << "Failed to infer result type(s):\n"
265 << "\"" << state.name << "\"(...) "
266 << state.attributes.getDictionary(context: state.location.getContext()) << " : ("
267 << llvm::interleaved(R: llvm::map_range(
268 C&: state.operands, F: [](Value val) { return val.getType(); }))
269 << ") -> ( ??? )";
270 emitRemark(loc: state.location, message: "location of op");
271 llvm::report_fatal_error(reason: llvm::StringRef(buffer));
272}
273