-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReluIndexExtraction.cpp
More file actions
207 lines (156 loc) · 6.61 KB
/
Copy pathReluIndexExtraction.cpp
File metadata and controls
207 lines (156 loc) · 6.61 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
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
#include "Cei/CeiPasses.h"
#include "mlir/Dialect/Affine/Passes.h"
#include "mlir/Dialect/Affine/IR/AffineOps.h"
#include "mlir/Dialect/Affine/LoopUtils.h"
#include "mlir/IR/IntegerSet.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/Pass/Pass.h"
#include "llvm/ADT/Sequence.h"
#include "mlir/IR/IRMapping.h"
#include "llvm/Support/raw_ostream.h"
#include <iostream>
using namespace mlir;
using namespace mlir::affine;
using namespace mlir::func;
namespace {
class ReluIndexExtractionPass
: public mlir::PassWrapper<ReluIndexExtractionPass,
mlir::OperationPass<mlir::ModuleOp>> {
public:
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(ReluIndexExtractionPass)
void getDependentDialects(mlir::DialectRegistry ®istry) const override {
registry.insert<mlir::affine::AffineDialect,
mlir::scf::SCFDialect,
mlir::arith::ArithDialect>();
}
bool isReluInAffineFor(AffineForOp loop, Value &memrefIn, Value &memrefOut) {
Value iv = loop.getInductionVar();
bool found = false;
loop.getBody()->walk([&](scf::IfOp scfIf) {
if (found)
return;
// Verify that the scf if condition is given by an arith.cmpi sgt
auto cmp = scfIf.getCondition().getDefiningOp<arith::CmpIOp>();
if (!cmp || cmp.getPredicate() != arith::CmpIPredicate::sgt)
return;
// Verify that the comparison is done on a value loaded with affine.load
auto load = cmp.getLhs().getDefiningOp<AffineLoadOp>();
if (!load)
return;
// Verify that the load is done on a 1D vector at the induction variable position
if (load.getIndices().size() != 1 || load.getIndices()[0] != iv)
return;
Value inputMemref = load.getMemRef();
// Verify that the right-hand side of the comparison is a constant equal to 0
auto rhs = cmp.getRhs().getDefiningOp<arith::ConstantOp>();
if (!rhs)
return;
auto rhsAttr = rhs.getValue().dyn_cast<IntegerAttr>();
if (!rhsAttr || !rhsAttr.getValue().isZero())
return;
// Verify that the scf if has an else block
if (!scfIf.elseBlock())
return;
// Verify that the then block stores the loaded value into the output
AffineStoreOp thenStore = nullptr;
scfIf.thenBlock()->walk([&](AffineStoreOp store) {
thenStore = store;
});
if (!thenStore)
return;
if (thenStore.getValue() != load.getResult())
return;
// Verify that the store is done on a 1D vector at the induction variable position
if (thenStore.getIndices().size() != 1 || thenStore.getIndices()[0] != iv)
return;
Value outputMemref = thenStore.getMemRef();
// Verify that the else block stores a constant equal to 0 into the output
AffineStoreOp elseStore = nullptr;
scfIf.elseBlock()->walk([&](AffineStoreOp store) {
elseStore = store;
});
if (!elseStore)
return;
auto elseConstant = elseStore.getValue().getDefiningOp<arith::ConstantOp>();
if (!elseConstant)
return;
auto elseAttr = elseConstant.getValue().dyn_cast<IntegerAttr>();
if (!elseAttr || !elseAttr.getValue().isZero())
return;
if (elseStore.getIndices().size() != 1 || elseStore.getIndices()[0] != iv)
return;
if (elseStore.getMemRef() != outputMemref)
return;
memrefIn = inputMemref;
memrefOut = outputMemref;
found = true;
});
return found;
}
void runOnOperation() override {
ModuleOp module = getOperation();
for (FuncOp func : module.getOps<FuncOp>()) {
SmallVector<AffineForOp, 1> loopsToErase;
for (AffineForOp loop : func.getOps<AffineForOp>()) {
Value memrefIn, memrefOut;
if (!isReluInAffineFor(loop, memrefIn, memrefOut))
continue;
auto memrefType = memrefIn.getType().dyn_cast<MemRefType>();
if (!memrefType || memrefType.getRank() != 1)
continue;
auto numberElements = memrefType.getShape()[0];
constexpr int numberEntries = 4;
int fullBlocks = numberElements / numberEntries;
int remainder = numberElements % numberEntries;
OpBuilder builder(loop);
Location loc = loop.getLoc();
MLIRContext *ctx = builder.getContext();
AffineExpr d0 = getAffineDimExpr(0, ctx);
Value zero = builder.create<arith::ConstantIntOp>(loc, 0, memrefType.getElementType());
auto blockLoop = builder.create<AffineForOp>(loc, 0, fullBlocks);
builder.setInsertionPointToStart(blockLoop.getBody());
Value block = blockLoop.getInductionVar();
SmallVector<Value, 4> entries;
for (int i = 0; i < numberEntries; ++i) {
AffineMap map = AffineMap::get(1, 0, d0 * numberEntries + i, ctx);
entries.push_back(builder.create<AffineApplyOp>(loc, map, block));
}
for (Value entry : entries) {
Value val = builder.create<AffineLoadOp>(loc, memrefIn, entry);
Value cmp = builder.create<arith::CmpIOp>(loc, arith::CmpIPredicate::sgt, val, zero);
auto scfIf = builder.create<scf::IfOp>(loc, cmp, true);
builder.setInsertionPointToStart(scfIf.thenBlock());
builder.create<AffineStoreOp>(loc, val, memrefOut, entry);
builder.setInsertionPointToStart(scfIf.elseBlock());
builder.create<AffineStoreOp>(loc, zero, memrefOut, entry);
builder.setInsertionPointAfter(scfIf);
}
if (remainder == 0) {
loopsToErase.push_back(loop);
continue;
}
builder.setInsertionPointAfter(blockLoop);
for (int i = 0; i < remainder; ++i) {
Value entry = builder.create<arith::ConstantIndexOp>(loc, fullBlocks * numberEntries + i);
Value val = builder.create<AffineLoadOp>(loc, memrefIn, entry);
Value cmp = builder.create<arith::CmpIOp>(loc, arith::CmpIPredicate::sgt, val, zero);
auto scfIf = builder.create<scf::IfOp>(loc, cmp, true);
builder.setInsertionPointToStart(scfIf.thenBlock());
builder.create<AffineStoreOp>(loc, val, memrefOut, entry);
builder.setInsertionPointToStart(scfIf.elseBlock());
builder.create<AffineStoreOp>(loc, zero, memrefOut, entry);
builder.setInsertionPointAfter(scfIf);
}
loopsToErase.push_back(loop);
}
for (AffineForOp l : loopsToErase)
l.erase();
}
}
};
} // namespace
std::unique_ptr<mlir::Pass> cei::reluIndexExtractionPass() {
return std::make_unique<ReluIndexExtractionPass>();
}