-
Notifications
You must be signed in to change notification settings - Fork 27
/
Utils.cpp
288 lines (246 loc) · 11.8 KB
/
Utils.cpp
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
#include "lib/Conversion/Utils.h"
#include <cstddef>
#include <cstdint>
#include <memory>
#include "mlir/include/mlir/Dialect/Affine/IR/AffineOps.h" // from @llvm-project
#include "mlir/include/mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project
#include "mlir/include/mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project
#include "mlir/include/mlir/Dialect/Func/Transforms/FuncConversions.h" // from @llvm-project
#include "mlir/include/mlir/Dialect/SCF/Transforms/Patterns.h" // from @llvm-project
#include "mlir/include/mlir/Dialect/Tensor/IR/Tensor.h" // from @llvm-project
#include "mlir/include/mlir/IR/BuiltinTypes.h" // from @llvm-project
#include "mlir/include/mlir/IR/IRMapping.h" // from @llvm-project
#include "mlir/include/mlir/IR/OpDefinition.h" // from @llvm-project
#include "mlir/include/mlir/IR/OperationSupport.h" // from @llvm-project
#include "mlir/include/mlir/IR/PatternMatch.h" // from @llvm-project
#include "mlir/include/mlir/IR/Region.h" // from @llvm-project
#include "mlir/include/mlir/Support/LLVM.h" // from @llvm-project
#include "mlir/include/mlir/Support/LogicalResult.h" // from @llvm-project
#include "mlir/include/mlir/Transforms/DialectConversion.h" // from @llvm-project
namespace mlir {
namespace heir {
using ::mlir::func::CallOp;
using ::mlir::func::FuncOp;
using ::mlir::func::ReturnOp;
struct ConvertAny : public ConversionPattern {
ConvertAny(const TypeConverter &typeConverter, MLIRContext *context)
: ConversionPattern(typeConverter, RewritePattern::MatchAnyOpTypeTag(),
/*benefit=*/1, context) {
setDebugName("ConvertAny");
setHasBoundedRewriteRecursion(true);
}
// generate a new op where all operands have been replaced with their
// materialized/typeconverted versions
LogicalResult matchAndRewrite(
Operation *op, ArrayRef<Value> operands,
ConversionPatternRewriter &rewriter) const override {
SmallVector<Type> newOperandTypes;
if (failed(getTypeConverter()->convertTypes(op->getOperandTypes(),
newOperandTypes)))
return failure();
SmallVector<Type> newResultTypes;
if (failed(getTypeConverter()->convertTypes(op->getResultTypes(),
newResultTypes)))
return failure();
SmallVector<std::unique_ptr<Region>, 1> regions;
IRMapping mapping;
for (auto &r : op->getRegions()) {
Region *newRegion = new Region();
rewriter.cloneRegionBefore(r, *newRegion, newRegion->end(), mapping);
if (failed(rewriter.convertRegionTypes(newRegion, *this->typeConverter)))
return failure();
regions.emplace_back(newRegion);
}
Operation *newOp = rewriter.create(OperationState(
op->getLoc(), op->getName().getStringRef(), operands, newResultTypes,
op->getAttrs(), op->getSuccessors(), regions));
rewriter.replaceOp(op, newOp);
return success();
}
};
struct ConvertExtract : public OpConversionPattern<tensor::ExtractOp> {
ConvertExtract(mlir::MLIRContext *context)
: OpConversionPattern<tensor::ExtractOp>(context) {}
using OpConversionPattern::OpConversionPattern;
// Convert a tensor.extract that would type-convert to extracting a tensor to
// a tensor.extract_slice operation instead. Specifically, this targets
// extracting SourceType from tensor<...xSourceType> when SourceType would be
// type converted to tensor<...>.
LogicalResult matchAndRewrite(
tensor::ExtractOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
// replace tensor.extract %t[%i] from tensor<shape x SourceType>
// with an equivalent tensor.slice from tensor<shape x resultshape>
auto shape = op.getTensor().getType().getShape();
auto resultType = getTypeConverter()
->convertType(op.getResult().getType())
.cast<RankedTensorType>();
auto resultShape = resultType.getShape();
// expand op's list of indices by appending as many zeros as there are
// dimension in resultShape
SmallVector<OpFoldResult> offsets;
offsets.append(op.getIndices().begin(), op.getIndices().end());
for (size_t i = 0; i < resultShape.size(); ++i) {
offsets.push_back(rewriter.getIndexAttr(0));
}
// expand resultShape by prepending as many ones as there are dimensions in
// shape
SmallVector<OpFoldResult> sizes;
for (size_t i = 0; i < shape.size(); ++i) {
sizes.push_back(rewriter.getIndexAttr(1));
}
for (int64_t i : resultShape) {
sizes.push_back(rewriter.getIndexAttr(i));
}
// strides are all 1, and we need as many as there are dimensions in
// both shape and resultShape together
SmallVector<OpFoldResult> strides;
for (size_t i = 0; i < shape.size() + resultShape.size(); ++i) {
strides.push_back(rewriter.getIndexAttr(1));
}
rewriter.replaceOpWithNewOp<tensor::ExtractSliceOp>(
op, resultType, adaptor.getTensor(), offsets, sizes, strides);
return success();
}
};
struct ConvertInsert : public OpConversionPattern<tensor::InsertOp> {
ConvertInsert(mlir::MLIRContext *context)
: OpConversionPattern<tensor::InsertOp>(context) {}
using OpConversionPattern::OpConversionPattern;
// Convert a tensor.insert that would type-convert to inserting a tensor to
// a tensor.insert_slice operation instead. Specifically, this targets
// inserting SourceType into tensor<...xSourceType> when SourceType would be
// type converted to tensor<...>.
LogicalResult matchAndRewrite(
tensor::InsertOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
// replace tensor.insert %s into %t[%i] with tensor<shape x SourceType>
// with an equivalent tensor.insert_slice with tensor<shape x resultshape>
auto shape = op.getDest().getType().getShape();
auto resultType = getTypeConverter()
->convertType(op.getScalar().getType())
.cast<RankedTensorType>();
auto resultShape = resultType.getShape();
// expand op's list of indices by appending as many zeros as there are
// dimension in resultShape
SmallVector<OpFoldResult> offsets;
offsets.append(op.getIndices().begin(), op.getIndices().end());
for (size_t i = 0; i < resultShape.size(); ++i) {
offsets.push_back(rewriter.getIndexAttr(0));
}
// expand resultShape by prepending as many ones as there are dimensions in
// shape
SmallVector<OpFoldResult> sizes;
for (size_t i = 0; i < shape.size(); ++i) {
sizes.push_back(rewriter.getIndexAttr(1));
}
for (int64_t i : resultShape) {
sizes.push_back(rewriter.getIndexAttr(i));
}
// strides are all 1, and we need as many as there are dimensions in
// both shape and resultShape together
SmallVector<OpFoldResult> strides;
for (size_t i = 0; i < shape.size() + resultShape.size(); ++i) {
strides.push_back(rewriter.getIndexAttr(1));
}
rewriter.replaceOpWithNewOp<tensor::InsertSliceOp>(
op, adaptor.getScalar(), adaptor.getDest(), offsets, sizes, strides);
return success();
}
};
struct ConvertFromElements
: public OpConversionPattern<tensor::FromElementsOp> {
ConvertFromElements(mlir::MLIRContext *context)
: OpConversionPattern<tensor::FromElementsOp>(context) {}
using OpConversionPattern::OpConversionPattern;
// Converts a tensor.from_elements %s0, %s1, ... : tensor<...xSourceType>
// where SourceType would be type-converted to tensor<...> to
// a concatenation of the converted operands (with appropriate reshape)
LogicalResult matchAndRewrite(
tensor::FromElementsOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
// Expand each of the (converted) operands:
SmallVector<Value> newOperands;
for (auto o : adaptor.getElements()) {
// extend tensor<...xT> to tensor<1x...xT>
if (auto tensorType = o.getType().dyn_cast<RankedTensorType>()) {
auto shape = tensorType.getShape();
SmallVector<int64_t> newShape(1, 1);
newShape.append(shape.begin(), shape.end());
// Create a dense constant for targetShape
auto shapeOp = rewriter.create<arith::ConstantOp>(
op.getLoc(),
RankedTensorType::get(newShape.size(), rewriter.getIndexType()),
rewriter.getIndexTensorAttr(newShape));
auto reshapeOp = rewriter.create<tensor::ReshapeOp>(
op.getLoc(),
RankedTensorType::get(newShape, tensorType.getElementType()), o,
shapeOp);
newOperands.push_back(reshapeOp);
} else {
newOperands.push_back(o);
}
}
// Create the final tensor.concat operation
rewriter.replaceOpWithNewOp<tensor::ConcatOp>(op, 0, newOperands);
return success();
}
};
void addTensorOfTensorConversionPatterns(TypeConverter &typeConverter,
RewritePatternSet &patterns,
ConversionTarget &target) {
target.addDynamicallyLegalDialect<tensor::TensorDialect>(
[&](Operation *op) { return typeConverter.isLegal(op); });
typeConverter.addConversion([&](TensorType type) -> Type {
if (!typeConverter.isLegal(type.getElementType())) {
typeConverter.convertType(type.getElementType()).dump();
if (auto convertedType =
typeConverter.convertType(type.getElementType())) {
if (auto castConvertedType =
convertedType.dyn_cast<RankedTensorType>()) {
// Create the combined shape
auto polyShape = castConvertedType.getShape();
auto tensorShape = type.getShape();
SmallVector<int64_t, 4> combinedShape(tensorShape.begin(),
tensorShape.end());
combinedShape.append(polyShape.begin(), polyShape.end());
auto combinedType = RankedTensorType::get(
combinedShape, castConvertedType.getElementType());
return combinedType;
}
}
}
return type;
});
target.addDynamicallyLegalDialect<affine::AffineDialect>(
[&](Operation *op) { return typeConverter.isLegal(op); });
patterns.add<ConvertAny, ConvertExtract, ConvertInsert, ConvertFromElements>(
typeConverter, patterns.getContext());
}
void addStructuralConversionPatterns(TypeConverter &typeConverter,
RewritePatternSet &patterns,
ConversionTarget &target) {
populateFunctionOpInterfaceTypeConversionPattern<FuncOp>(patterns,
typeConverter);
target.addDynamicallyLegalOp<func::FuncOp>([&](func::FuncOp op) {
return typeConverter.isSignatureLegal(op.getFunctionType()) &&
typeConverter.isLegal(&op.getBody());
});
populateReturnOpTypeConversionPattern(patterns, typeConverter);
target.addDynamicallyLegalOp<func::ReturnOp>(
[&](func::ReturnOp op) { return typeConverter.isLegal(op); });
populateCallOpTypeConversionPattern(patterns, typeConverter);
target.addDynamicallyLegalOp<func::CallOp>(
[&](func::CallOp op) { return typeConverter.isLegal(op); });
populateBranchOpInterfaceTypeConversionPattern(patterns, typeConverter);
target.markUnknownOpDynamicallyLegal([&](Operation *op) {
return isNotBranchOpInterfaceOrReturnLikeOp(op) ||
isLegalForBranchOpInterfaceTypeConversionPattern(op,
typeConverter) ||
isLegalForReturnOpTypeConversionPattern(op, typeConverter);
});
scf::populateSCFStructuralTypeConversionsAndLegality(typeConverter, patterns,
target);
}
} // namespace heir
} // namespace mlir