forked from EPFL-LAP/dynamatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHandshakeAnalyzeLSQUsage.cpp
313 lines (280 loc) · 12 KB
/
HandshakeAnalyzeLSQUsage.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
//===- HandshakeAnalyzeLSQUsage.cpp - LSQ flow analysis ---------*- C++ -*-===//
//
// Dynamatic is 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
//
//===----------------------------------------------------------------------===//
//
// Implements the --handshake-analyze-lsq-usage pass, using the logic
// introduced in https://ieeexplore.ieee.org/document/8977873.
//
//===----------------------------------------------------------------------===//
#include "dynamatic/Transforms/HandshakeAnalyzeLSQUsage.h"
#include "dynamatic/Analysis/NameAnalysis.h"
#include "dynamatic/Dialect/Handshake/HandshakeAttributes.h"
#include "dynamatic/Dialect/Handshake/HandshakeOps.h"
#include "dynamatic/Support/Attribute.h"
#include "dynamatic/Support/CFG.h"
#include "dynamatic/Support/DynamaticPass.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/IR/Visitors.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/Debug.h"
#define DEBUG_TYPE "handshake-analyze-lsq-usage"
using namespace mlir;
using namespace dynamatic;
using namespace dynamatic::handshake;
namespace {
/// Simple pass driver for the LSQ usage analysis pass. Does not modify the IR
/// beyong setting `handshake::MemInterfaceAttr` attributes on memory ports.
struct HandshakeAnalyzeLSQUsagePass
: public dynamatic::impl::HandshakeAnalyzeLSQUsageBase<
HandshakeAnalyzeLSQUsagePass> {
void runDynamaticPass() override;
/// Analyzes all memory regions inside a Handshake functions and marks all
/// operations representing memory accesses to it with the
/// `handshake::MemInterfaceAttr` attribute.
void analyzeFunction(handshake::FuncOp funcOp);
/// Analyzes a specific memory region inside a Handshake function and
/// determines whether each of its access port should go through an LSQ.
void analyzeMemRef(handshake::FuncOp funcOp,
TypedValue<mlir::MemRefType> memref, HandshakeCFG &cfg);
};
} // namespace
void HandshakeAnalyzeLSQUsagePass::runDynamaticPass() {
mlir::ModuleOp modOp = getOperation();
// Check that memory access ports are named
NameAnalysis &namer = getAnalysis<NameAnalysis>();
WalkResult res = modOp.walk([&](Operation *op) {
if (!isa<handshake::LoadOp, handshake::StoreOp>(op))
return WalkResult::advance();
if (!namer.hasName(op)) {
op->emitError() << "Memory access port must be named.";
return WalkResult::interrupt();
}
return WalkResult::advance();
});
if (res.wasInterrupted())
return signalPassFailure();
// Check that all eligible operations within Handshake function belon to a
// basic block
for (handshake::FuncOp funcOp : modOp.getOps<handshake::FuncOp>()) {
for (Operation &op : funcOp.getOps()) {
if (!cannotBelongToCFG(&op) && !getLogicBB(&op)) {
op.emitError() << "Operation should have basic block attribute.";
return signalPassFailure();
}
}
}
for (handshake::FuncOp funcOp : modOp.getOps<handshake::FuncOp>())
analyzeFunction(funcOp);
}
void HandshakeAnalyzeLSQUsagePass::analyzeFunction(handshake::FuncOp funcOp) {
for (BlockArgument arg : funcOp.getArguments()) {
HandshakeCFG cfg(funcOp);
if (auto memref = dyn_cast<TypedValue<mlir::MemRefType>>(arg))
analyzeMemRef(funcOp, memref, cfg);
}
}
/// Determines whether there exists any RAW dependency between the load and the
/// stores.
static bool hasRAW(handshake::LoadOp loadOp,
DenseSet<handshake::StoreOp> &storeOps) {
StringRef loadName = getUniqueName(loadOp);
for (handshake::StoreOp storeOp : storeOps) {
if (auto deps = getDialectAttr<MemDependenceArrayAttr>(storeOp)) {
for (MemDependenceAttr dependency : deps.getDependencies()) {
if (dependency.getDstAccess() == loadName) {
LLVM_DEBUG({
llvm::dbgs() << "\tKeeping '" << loadName
<< "': RAW dependency with '" << getUniqueName(storeOp)
<< "'\n";
});
return true;
}
}
}
}
return false;
}
/// Determines whether the load is globally in-order independent (GIID) on the
/// store along all non-cyclic CFG paths between them.
static bool isStoreGIIDOnLoad(handshake::LoadOp loadOp,
handshake::StoreOp storeOp, HandshakeCFG &cfg) {
// Identify all CFG paths from the block containing the load to the block
// containing the store
handshake::FuncOp funcOp = loadOp->getParentOfType<handshake::FuncOp>();
assert(funcOp && "parent of load access must be handshake function");
SmallVector<CFGPath> allPaths;
std::optional<unsigned> loadBB = getLogicBB(loadOp);
std::optional<unsigned> storeBB = getLogicBB(storeOp);
assert(loadBB && storeBB && "memory accesses must belong to blocks");
cfg.getNonCyclicPaths(*loadBB, *storeBB, allPaths);
// There must be a dependence between any operand of the store with the load
// data result on all CFG paths between them
Value loadData = loadOp.getDataResult();
return llvm::all_of(allPaths, [&](CFGPath &path) {
return isGIID(loadData, storeOp->getOpOperand(0), path) ||
isGIID(loadData, storeOp->getOpOperand(1), path);
});
}
/// Determines whether the load is globally in-order independent (GIID) on all
/// stores with which it has a WAR dependency along all non-cyclic CFG paths
/// between them.
static bool hasEnforcedWARs(handshake::LoadOp loadOp,
DenseSet<handshake::StoreOp> &storeOps,
HandshakeCFG &cfg) {
DenseMap<StringRef, handshake::StoreOp> storesByName;
for (handshake::StoreOp storeOp : storeOps)
storesByName.insert({getUniqueName(storeOp), storeOp});
// We only need to check stores that depend on the load (WAR dependencies) as
// others are already provably independent. We may check a single store
// multiple times if it depends on the load at multiple loop depths
if (auto deps = getDialectAttr<MemDependenceArrayAttr>(loadOp)) {
for (MemDependenceAttr dependency : deps.getDependencies()) {
auto storeOp = storesByName.at(dependency.getDstAccess());
if (!isStoreGIIDOnLoad(loadOp, storeOp, cfg)) {
LLVM_DEBUG({
llvm::dbgs() << "\tKeeping '" << getUniqueName(loadOp)
<< "': non-enforced WAR with '" << getUniqueName(storeOp)
<< "'\n";
});
return false;
}
}
}
return true;
}
/// Mark all accesses with the `MemInterfaceAttr`, indicating whether they
/// should connect to an MC or LSQ depending on their dependencies with other
/// accesses.
template <typename Op>
static void markLSQPorts(const DenseSet<Op> &accesses,
const DenseSet<Op> &dependentAccesses,
const DenseMap<Operation *, unsigned> &groupMap,
MLIRContext *ctx) {
for (Op accessOp : accesses) {
if (dependentAccesses.contains(accessOp))
setDialectAttr<MemInterfaceAttr>(accessOp, ctx, groupMap.at(accessOp));
else
setDialectAttr<MemInterfaceAttr>(accessOp, ctx);
}
};
void HandshakeAnalyzeLSQUsagePass::analyzeMemRef(
handshake::FuncOp funcOp, TypedValue<mlir::MemRefType> memref,
HandshakeCFG &cfg) {
LLVM_DEBUG({
unsigned idx = cast<BlockArgument>(memref).getArgNumber();
StringRef argName = funcOp.getArgName(idx);
llvm::dbgs() << "Analyzing interfaces for region '" << argName << "'\n";
});
// There should be at most one memref user in any well-formed function
auto memrefUsers = memref.getUsers();
assert(std::distance(memrefUsers.begin(), memrefUsers.end()) <= 1 &&
"expected at most one memref user");
if (memrefUsers.empty()) {
LLVM_DEBUG(llvm::dbgs() << "\tNo interfaces\n");
return;
}
MLIRContext *ctx = &getContext();
// Identify all memory interfaces (master and potential slaves) for the region
Operation *memOp = *memrefUsers.begin();
handshake::LSQOp lsqOp;
if (lsqOp = dyn_cast<handshake::LSQOp>(memOp); !lsqOp) {
// The master memory interface must be an MC
auto mcOp = cast<handshake::MemoryControllerOp>(memOp);
// Ports to memory controllers will always remain connected to a memory
// controller, mark them as such with the memory interface attribute
MCPorts mcPorts = mcOp.getPorts();
for (MCBlock &block : mcPorts.getBlocks()) {
for (MemoryPort &port : block->accessPorts)
setDialectAttr<MemInterfaceAttr>(port.portOp, ctx);
}
// Nothing else to do if the region has no LSQ
if (!mcPorts.connectsToLSQ()) {
LLVM_DEBUG(llvm::dbgs() << "\tNo LSQ interface for the region\n");
return;
}
lsqOp = mcPorts.getLSQPort().getLSQOp();
}
// Identify load and store accesses to the LSQ
DenseSet<handshake::LoadOp> lsqLoadOps;
DenseSet<handshake::StoreOp> lsqStoreOps;
DenseMap<Operation *, unsigned> groupMap;
LSQPorts lsqPorts = lsqOp.getPorts();
for (LSQGroup &group : lsqPorts.getGroups()) {
for (MemoryPort &port : group->accessPorts) {
groupMap.insert({port.portOp, group.groupID});
if (auto loadOp = dyn_cast<handshake::LoadOp>(port.portOp))
lsqLoadOps.insert(loadOp);
else
lsqStoreOps.insert(cast<handshake::StoreOp>(port.portOp));
}
}
NameAnalysis &namer = getAnalysis<NameAnalysis>();
// Check whether we can prove independence of some loads w.r.t. other accesses
DenseSet<handshake::LoadOp> dependentLoads;
DenseSet<handshake::StoreOp> dependentStores;
for (handshake::LoadOp loadOp : lsqLoadOps) {
// Loads with no RAW dependencies and which satisfy the GIID property with
// all stores they have a dependency with may be removed
if (hasRAW(loadOp, lsqStoreOps) ||
!hasEnforcedWARs(loadOp, lsqStoreOps, cfg)) {
dependentLoads.insert(loadOp);
// All stores involved in a WAR with the load are still dependent
if (auto deps = getDialectAttr<MemDependenceArrayAttr>(loadOp)) {
for (MemDependenceAttr dependency : deps.getDependencies()) {
Operation *dstOp = namer.getOp(dependency.getDstAccess());
if (auto storeOp = dyn_cast<StoreOp>(dstOp))
dependentStores.insert(storeOp);
}
}
} else {
LLVM_DEBUG({
llvm::dbgs() << "\tRerouting '" << getUniqueName(loadOp) << "' to MC\n";
});
}
}
// Stores involed in a RAW ar WAW dependency with another operation are sill
// dependent
for (handshake::StoreOp storeOp : lsqStoreOps) {
auto deps = getDialectAttr<MemDependenceArrayAttr>(storeOp);
if (!deps)
continue;
// Iterate over all RAW and WAW dependencies to determine those which must
// still be honored by an LSQ
StringRef storeName = getUniqueName(storeOp);
for (MemDependenceAttr dependency : deps.getDependencies()) {
StringRef dstName = dependency.getDstAccess();
// WAW dependencies on the same operation can be ignored, they are
// enforced automatically by the dataflow circuit's construction
if (storeName == dstName)
continue;
// The dependency must still be honored
Operation *dstOp = namer.getOp(dstName);
dependentStores.insert(storeOp);
if (auto dstStoreOp = dyn_cast<handshake::StoreOp>(dstOp))
dependentStores.insert(dstStoreOp);
}
}
LLVM_DEBUG({
for (handshake::StoreOp storeOp : lsqStoreOps) {
if (dependentStores.contains(storeOp)) {
llvm::dbgs() << "\tKeeping '" << getUniqueName(storeOp)
<< "': WAW or RAW dependency with other access\n";
} else {
llvm::dbgs() << "\tRerouting '" << getUniqueName(storeOp)
<< "' to MC\n";
}
}
});
// Tag LSQ access ports with the interface they should actually connect to,
// which may be different from the one they currently connect to
markLSQPorts(lsqLoadOps, dependentLoads, groupMap, ctx);
markLSQPorts(lsqStoreOps, dependentStores, groupMap, ctx);
}
std::unique_ptr<dynamatic::DynamaticPass>
dynamatic::createHandshakeAnalyzeLSQUsage() {
return std::make_unique<HandshakeAnalyzeLSQUsagePass>();
}