17 #include "mlir/IR/PatternMatch.h"
18 #include "mlir/Rewrite/FrozenRewritePatternSet.h"
19 #include "mlir/Transforms/DialectConversion.h"
21 using namespace circt;
22 using namespace handshake;
26 Block *entry = &r.front();
27 Location loc = r.getLoc();
29 if (entry->getNumArguments() == 0)
30 return r.getParentOp()->emitError(
"cannot lock a region without arguments");
32 auto *ret = r.front().getTerminator();
33 if (ret->getNumOperands() == 0)
34 return r.getParentOp()->emitError(
"cannot lock a region without results");
36 rewriter.setInsertionPointToStart(entry);
38 auto backEdge = bebuilder.
get(rewriter.getNoneType());
40 auto buff = rewriter.create<handshake::BufferOp>(loc, backEdge, 1,
45 buff->setAttr(
"initValues", rewriter.getI64ArrayAttr({0}));
47 SmallVector<Value> inSyncOperands =
48 llvm::to_vector_of<Value>(entry->getArguments());
49 inSyncOperands.push_back(buff);
50 auto sync = rewriter.create<SyncOp>(loc, inSyncOperands);
54 for (
auto &&[arg, synced] :
55 llvm::drop_end(llvm::zip(inSyncOperands, sync.getResults())))
56 arg.replaceAllUsesExcept(synced, sync);
58 rewriter.setInsertionPoint(ret);
59 SmallVector<Value> endJoinOperands = llvm::to_vector(ret->getOperands());
61 endJoinOperands.push_back(sync.getResults().back());
62 auto endJoin = rewriter.create<JoinOp>(loc, endJoinOperands);
64 backEdge.setValue(endJoin);
70 struct HandshakeLockFunctionsPass
71 :
public HandshakeLockFunctionsBase<HandshakeLockFunctionsPass> {
72 void runOnOperation()
override {
73 handshake::FuncOp op = getOperation();
84 std::unique_ptr<mlir::Pass>
86 return std::make_unique<HandshakeLockFunctionsPass>();
Instantiate one of these and use it to build typed backedges.
Backedge get(mlir::Type resultType, mlir::LocationAttr optionalLoc={})
Create a typed backedge.
std::unique_ptr< mlir::Pass > createHandshakeLockFunctionsPass()
LogicalResult lockRegion(Region &r, OpBuilder &rewriter)
This file defines an intermediate representation for circuits acting as an abstraction for constraint...