-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathLowerPolygeistOps.cpp
More file actions
90 lines (74 loc) · 2.95 KB
/
LowerPolygeistOps.cpp
File metadata and controls
90 lines (74 loc) · 2.95 KB
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
//===- LowerPolygeistOps.cpp - Lower polygeist ops to upstream MLIR ops -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements a pass which lowers any remaining Polygeist dialect
// operations (after canonicalization) to operations found in upstream MLIR
// dialects.
//
//===----------------------------------------------------------------------===//
#include "PassDetails.h"
#include "mlir/Dialect/Arithmetic/IR/Arithmetic.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/Rewrite/FrozenRewritePatternSet.h"
#include "mlir/Transforms/DialectConversion.h"
#include "polygeist/Dialect.h"
#include "polygeist/Ops.h"
using namespace mlir;
using namespace polygeist;
using namespace mlir::arith;
namespace {
struct SubIndexToReinterpretCast
: public OpConversionPattern<polygeist::SubIndexOp> {
using OpConversionPattern::OpConversionPattern;
LogicalResult
matchAndRewrite(polygeist::SubIndexOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
auto srcMemRefType = op.source().getType().cast<MemRefType>();
auto resMemRefType = op.result().getType().cast<MemRefType>();
auto inShape = srcMemRefType.getShape();
auto outShape = resMemRefType.getShape();
if (!resMemRefType.hasStaticShape())
return failure();
llvm::SmallVector<OpFoldResult> strides, sizes;
int64_t innerSize = resMemRefType.getNumElements();
auto offset = rewriter.create<arith::MulIOp>(
op.getLoc(), op.index(),
rewriter.create<ConstantIndexOp>(op.getLoc(), innerSize));
int64_t strideAcc = 1;
for (auto dim : llvm::reverse(outShape)) {
sizes.insert(sizes.begin(), rewriter.getIndexAttr(dim));
strides.insert(strides.begin(), rewriter.getIndexAttr(strideAcc));
strideAcc *= dim;
}
rewriter.replaceOpWithNewOp<memref::ReinterpretCastOp>(
op, resMemRefType, op.source(), offset.getResult(), sizes, strides);
return success();
}
};
struct LowerPolygeistOpsPass
: public LowerPolygeistOpsBase<LowerPolygeistOpsPass> {
void runOnOperation() override {
auto op = getOperation();
auto ctx = op->getContext();
RewritePatternSet patterns(ctx);
patterns.insert<SubIndexToReinterpretCast>(ctx);
ConversionTarget target(*ctx);
target.addIllegalDialect<polygeist::PolygeistDialect>();
target.addLegalDialect<arith::ArithmeticDialect, memref::MemRefDialect>();
if (failed(applyPartialConversion(op, target, std::move(patterns))))
return signalPassFailure();
}
};
} // namespace
namespace mlir {
namespace polygeist {
std::unique_ptr<Pass> createLowerPolygeistOpsPass() {
return std::make_unique<LowerPolygeistOpsPass>();
}
} // namespace polygeist
} // namespace mlir