forked from EPFL-LAP/dynamatic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForceMemoryInterface.cpp
More file actions
84 lines (71 loc) · 2.98 KB
/
ForceMemoryInterface.cpp
File metadata and controls
84 lines (71 loc) · 2.98 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
//===- ForceMemoryInterface.cpp - Force interface in Handshake --*- 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 --force-memory-interface pass, internally adding/modifying the
// `handshake::MemInterfaceAttr` to/on all memory operations to force placement
// of a specific type of memory interface.
//
//===----------------------------------------------------------------------===//
#include "dynamatic/Transforms/ForceMemoryInterface.h"
#include "dynamatic/Dialect/Handshake/HandshakeAttributes.h"
#include "dynamatic/Support/Attribute.h"
#include "mlir/Dialect/Affine/IR/AffineOps.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
using namespace mlir;
using namespace dynamatic;
namespace {
/// Simple driver for memory interface forcing pass.
struct ForceMemoryInterfacePass
: public dynamatic::impl::ForceMemoryInterfaceBase<
ForceMemoryInterfacePass> {
ForceMemoryInterfacePass(bool forceLSQ, bool forceMC) {
this->forceLSQ = forceLSQ;
this->forceMC = forceMC;
}
void runDynamaticPass() override {
// Exactly one of the two pass options need to have been set
if (forceLSQ && forceMC)
llvm::errs() << "Both " << forceLSQ.ArgStr << " and " << forceMC.ArgStr
<< " flags were provided. However, only one can be set at "
"the same time.";
if (!forceLSQ && !forceMC)
llvm::errs()
<< "Neither " << forceLSQ.ArgStr << " and " << forceMC.ArgStr
<< " flags were provided. However, exactly one needs to be set.";
MLIRContext *ctx = &getContext();
DenseMap<Block *, unsigned> lsqGroups;
unsigned nextGroupID = 0;
// Find all memory operations and adds/modifies the
// handshake::MemInterfaceAttr on them depending on the pass parameters
getOperation()->walk([&](Operation *op) {
// This only makes sense on load/store-like operations
if (!isa<memref::LoadOp, memref::StoreOp, affine::AffineLoadOp,
affine::AffineStoreOp>(op))
return;
if (forceMC) {
setDialectAttr<handshake::MemInterfaceAttr>(op, ctx);
return;
}
// Make every block its own LSQ group
Block *block = op->getBlock();
unsigned groupID;
// Try to find the block's group ID. Failing that, assign a new group ID
// to the block
if (auto groupIt = lsqGroups.find(block); groupIt != lsqGroups.end())
groupID = groupIt->second;
else
lsqGroups[block] = groupID = nextGroupID++;
setDialectAttr<handshake::MemInterfaceAttr>(op, ctx, groupID);
});
}
};
} // namespace
std::unique_ptr<mlir::OperationPass<mlir::ModuleOp>>
dynamatic::createForceMemoryInterface(bool forceLSQ, bool forceMC) {
return std::make_unique<ForceMemoryInterfacePass>(forceLSQ, forceMC);
}