18 #include "mlir/Dialect/MemRef/IR/MemRef.h"
19 #include "mlir/Dialect/SCF/IR/SCF.h"
20 #include "mlir/IR/PatternMatch.h"
21 #include "mlir/Rewrite/FrozenRewritePatternSet.h"
22 #include "mlir/Transforms/DialectConversion.h"
24 using namespace circt;
25 using namespace handshake;
30 struct HandshakeLegalizeMemrefsPass
31 :
public HandshakeLegalizeMemrefsBase<HandshakeLegalizeMemrefsPass> {
32 void runOnOperation()
override {
33 func::FuncOp op = getOperation();
42 llvm::make_early_inc_range(op.getOps<memref::DeallocOp>()))
45 auto b = OpBuilder(op);
49 for (
auto copy : llvm::make_early_inc_range(op.getOps<memref::CopyOp>())) {
50 b.setInsertionPoint(copy);
51 auto loc = copy.getLoc();
52 auto src = copy.getSource();
53 auto dst = copy.getTarget();
54 auto memrefType = src.getType().cast<MemRefType>();
56 llvm::errs() <<
"Cannot legalize multi-dimensional memref operation "
58 <<
". Please run the memref flattening pass before this "
64 auto emitLoadStore = [&](Value index) {
65 llvm::SmallVector<Value> indices = {index};
66 auto loadValue = b.create<memref::LoadOp>(loc, src, indices);
67 b.create<memref::StoreOp>(loc, loadValue, dst, indices);
70 auto n = memrefType.getShape()[0];
73 auto lb = b.create<arith::ConstantIndexOp>(loc, 0).getResult();
74 auto ub = b.create<arith::ConstantIndexOp>(loc, n).getResult();
75 auto step = b.create<arith::ConstantIndexOp>(loc, 1).getResult();
78 loc, lb, ub, step, llvm::SmallVector<Value>(),
79 [&](OpBuilder &b, Location loc, Value iv, ValueRange loopState) {
81 b.create<scf::YieldOp>(loc);
84 emitLoadStore(b.create<arith::ConstantIndexOp>(loc, 0));
92 std::unique_ptr<mlir::Pass>
94 return std::make_unique<HandshakeLegalizeMemrefsPass>();
std::unique_ptr< mlir::Pass > createHandshakeLegalizeMemrefsPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
bool isUniDimensional(mlir::MemRefType memref)