-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathMulToAdd.cpp
More file actions
101 lines (80 loc) · 3.16 KB
/
Copy pathMulToAdd.cpp
File metadata and controls
101 lines (80 loc) · 3.16 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
91
92
93
94
95
96
97
98
99
100
101
#include "lib/Transform/Arith/MulToAdd.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "mlir/include/mlir/Pass/Pass.h"
namespace mlir {
namespace tutorial {
#define GEN_PASS_DEF_MULTOADD
#include "lib/Transform/Arith/Passes.h.inc"
using arith::AddIOp;
using arith::ConstantOp;
using arith::MulIOp;
// Replace y = C*x with y = C/2*x + C/2*x, when C is a power of 2, otherwise do
// nothing.
struct PowerOfTwoExpand : public OpRewritePattern<MulIOp> {
PowerOfTwoExpand(mlir::MLIRContext *context)
: OpRewritePattern<MulIOp>(context, /*benefit=*/2) {}
LogicalResult matchAndRewrite(MulIOp op,
PatternRewriter &rewriter) const override {
Value lhs = op.getOperand(0);
// canonicalization patterns ensure the constant is on the right, if there
// is a constant See
// https://mlir.llvm.org/docs/Canonicalization/#globally-applied-rules
Value rhs = op.getOperand(1);
auto rhsDefiningOp = rhs.getDefiningOp<arith::ConstantIntOp>();
if (!rhsDefiningOp) {
return failure();
}
int64_t value = rhsDefiningOp.value();
bool is_power_of_two = (value & (value - 1)) == 0;
if (!is_power_of_two) {
return failure();
}
ConstantOp newConstant = rewriter.create<ConstantOp>(
rhsDefiningOp.getLoc(),
rewriter.getIntegerAttr(rhs.getType(), value / 2));
MulIOp newMul = rewriter.create<MulIOp>(op.getLoc(), lhs, newConstant);
AddIOp newAdd = rewriter.create<AddIOp>(op.getLoc(), newMul, newMul);
rewriter.replaceOp(op, newAdd);
rewriter.eraseOp(rhsDefiningOp);
return success();
}
};
// Replace y = 9*x with y = 8*x + x
struct PeelFromMul : public OpRewritePattern<MulIOp> {
PeelFromMul(mlir::MLIRContext *context)
: OpRewritePattern<MulIOp>(context, /*benefit=*/1) {}
LogicalResult matchAndRewrite(MulIOp op,
PatternRewriter &rewriter) const override {
Value lhs = op.getOperand(0);
Value rhs = op.getOperand(1);
auto rhsDefiningOp = rhs.getDefiningOp<arith::ConstantIntOp>();
if (!rhsDefiningOp) {
return failure();
}
int64_t value = rhsDefiningOp.value();
// We are guaranteed `value` is not a power of two, because the greedy
// rewrite engine ensures the PowerOfTwoExpand pattern is run first, since
// it has higher benefit.
ConstantOp newConstant = rewriter.create<ConstantOp>(
rhsDefiningOp.getLoc(),
rewriter.getIntegerAttr(rhs.getType(), value - 1));
MulIOp newMul = rewriter.create<MulIOp>(op.getLoc(), lhs, newConstant);
AddIOp newAdd = rewriter.create<AddIOp>(op.getLoc(), newMul, lhs);
rewriter.replaceOp(op, newAdd);
rewriter.eraseOp(rhsDefiningOp);
return success();
}
};
struct MulToAdd : impl::MulToAddBase<MulToAdd> {
using MulToAddBase::MulToAddBase;
void runOnOperation() {
mlir::RewritePatternSet patterns(&getContext());
patterns.add<PowerOfTwoExpand>(&getContext());
patterns.add<PeelFromMul>(&getContext());
(void)applyPatternsGreedily(getOperation(), std::move(patterns));
}
};
} // namespace tutorial
} // namespace mlir