-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathConversionUtils.cpp
411 lines (361 loc) · 15.6 KB
/
ConversionUtils.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include "lib/Utils/ConversionUtils.h"
#include <cstddef>
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include "lib/Dialect/LWE/IR/LWEAttributes.h"
#include "lib/Dialect/Secret/IR/SecretOps.h"
#include "llvm/include/llvm/ADT/TypeSwitch.h" // from @llvm-project
#include "llvm/include/llvm/Support/ErrorHandling.h" // from @llvm-project
#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/IR/Value.h" // from @llvm-project
#include "mlir/include/mlir/IR/Verifier.h" // from @llvm-project
#include "mlir/include/mlir/IR/Visitors.h" // from @llvm-project
#include "mlir/include/mlir/Interfaces/FunctionInterfaces.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;
LogicalResult convertAnyOperand(const TypeConverter *typeConverter,
Operation *op, ArrayRef<Value> operands,
ConversionPatternRewriter &rewriter) {
if (typeConverter->isLegal(op)) {
return failure();
}
SmallVector<Type> newOperandTypes;
if (failed(
typeConverter->convertTypes(op->getOperandTypes(), newOperandTypes)))
return failure();
SmallVector<Type> newResultTypes;
if (failed(typeConverter->convertTypes(op->getResultTypes(), newResultTypes)))
return failure();
SmallVector<std::unique_ptr<Region>, 1> regions;
IRMapping mapping;
for (auto &r : op->getRegions()) {
Region *newRegion = new Region(op);
rewriter.cloneRegionBefore(r, *newRegion, newRegion->end(), mapping);
if (failed(rewriter.convertRegionTypes(newRegion, *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 = mlir::cast<RankedTensorType>(
getTypeConverter()->convertType(op.getResult().getType()));
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 = mlir::cast<RankedTensorType>(
getTypeConverter()->convertType(op.getScalar().getType()));
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 = mlir::dyn_cast<RankedTensorType>(o.getType())) {
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<mlir::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())) {
if (auto convertedType =
typeConverter.convertType(type.getElementType())) {
if (auto castConvertedType =
mlir::dyn_cast<RankedTensorType>(convertedType)) {
// 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);
}
int widthFromEncodingAttr(Attribute encoding) {
return llvm::TypeSwitch<Attribute, int>(encoding)
.Case<lwe::BitFieldEncodingAttr, lwe::UnspecifiedBitFieldEncodingAttr>(
[](auto attr) -> int { return attr.getCleartextBitwidth(); })
.Default([](Attribute attr) -> int {
llvm_unreachable("Unsupported encoding attribute");
return 0;
});
}
Attribute TypeWithAttrTypeConverter::getValueAttr(Value value) const {
Attribute attr;
if (auto blockArg = dyn_cast<BlockArgument>(value)) {
auto *parentOp = blockArg.getOwner()->getParentOp();
auto funcOp = dyn_cast<FunctionOpInterface>(parentOp);
if (funcOp) {
attr = funcOp.getArgAttr(blockArg.getArgNumber(), attrName);
}
} else {
auto *parentOp = value.getDefiningOp();
attr = parentOp->getAttr(attrName);
}
return attr;
}
void TypeWithAttrTypeConverter::convertValueRangeTypes(
ValueRange values, SmallVectorImpl<Type> &newTypes) const {
newTypes.reserve(values.size());
for (auto value : values) {
Attribute attr = getValueAttr(value);
auto newType = convertTypeWithAttr(value.getType(), attr);
// this is actually unsafe...
// all the thing should be done through the rewriter,
// if we are using the rewriter
value.setType(newType);
newTypes.push_back(newType);
}
}
void TypeWithAttrTypeConverter::convertOpResultTypes(
Operation *op, SmallVectorImpl<Type> &newResultTypes) const {
newResultTypes.reserve(op->getResultTypes().size());
auto attr = op->getAttr(attrName);
for (auto resultType : op->getResultTypes()) {
auto newType = convertTypeWithAttr(resultType, attr);
newResultTypes.push_back(newType);
}
}
void TypeWithAttrTypeConverter::convertFuncArgumentAndResultTypes(
FunctionOpInterface funcOp, SmallVectorImpl<Type> &newArgTypes,
SmallVectorImpl<Type> &newResultTypes) const {
for (auto argument : funcOp.getArguments()) {
auto attr = funcOp.getArgAttr(argument.getArgNumber(), attrName);
auto newType = convertTypeWithAttr(argument.getType(), attr);
// this is actually unsafe...
// we should go through rewriter.convertRegionTypes,
// which will create unresolved_materializaton,
// and everything is safe.
argument.setType(newType);
newArgTypes.push_back(newType);
}
// did not convert block arg/signature though..
for (auto &block : funcOp.getBlocks()) {
for (auto result : block.getTerminator()->getOperands()) {
auto attr = getValueAttr(result);
auto newType = convertTypeWithAttr(result.getType(), attr);
result.setType(newType);
newResultTypes.push_back(newType);
}
}
}
bool TypeWithAttrTypeConverter::isValueLegal(Value value) {
auto attr = getValueAttr(value);
return value.getType() == convertTypeWithAttr(value.getType(), attr);
}
bool TypeWithAttrTypeConverter::isOperationLegal(Operation *op) {
for (auto operand : op->getOperands()) {
if (!isValueLegal(operand)) {
return false;
}
}
for (auto result : op->getResults()) {
if (!isValueLegal(result)) {
return false;
}
}
for (auto ®ion : op->getRegions()) {
for (auto &block : region) {
for (auto argument : block.getArguments()) {
if (!isValueLegal(argument)) {
return false;
}
}
for (auto result : block.getTerminator()->getOperands()) {
if (!isValueLegal(result)) {
return false;
}
}
}
}
return true;
}
bool TypeWithAttrTypeConverter::isFuncArgumentAndResultLegal(
FunctionOpInterface funcOp) {
for (auto argument : funcOp.getArguments()) {
if (!isValueLegal(argument)) {
return false;
}
}
for (auto &block : funcOp.getBlocks()) {
for (auto result : block.getTerminator()->getOperands()) {
if (!isValueLegal(result)) {
return false;
}
}
}
return true;
}
} // namespace heir
} // namespace mlir