33static LogicalResult
lockRegion(Region &r, OpBuilder &rewriter) {
34 Block *entry = &r.front();
35 Location loc = r.getLoc();
37 if (entry->getNumArguments() == 0)
38 return r.getParentOp()->emitError(
"cannot lock a region without arguments");
40 auto *ret = r.front().getTerminator();
41 if (ret->getNumOperands() == 0)
42 return r.getParentOp()->emitError(
"cannot lock a region without results");
44 rewriter.setInsertionPointToStart(entry);
46 auto backEdge = bebuilder.
get(rewriter.getNoneType());
48 auto buff = rewriter.create<handshake::BufferOp>(loc, backEdge, 1,
53 buff->setAttr(
"initValues", rewriter.getI64ArrayAttr({0}));
55 SmallVector<Value> inSyncOperands =
56 llvm::to_vector_of<Value>(entry->getArguments());
57 inSyncOperands.push_back(buff);
58 auto sync = rewriter.create<SyncOp>(loc, inSyncOperands);
62 for (
auto &&[arg, synced] :
63 llvm::drop_end(llvm::zip(inSyncOperands, sync.getResults())))
64 arg.replaceAllUsesExcept(synced, sync);
66 rewriter.setInsertionPoint(ret);
67 SmallVector<Value> endJoinOperands = llvm::to_vector(ret->getOperands());
69 endJoinOperands.push_back(sync.getResults().back());
70 auto endJoin = rewriter.create<JoinOp>(loc, endJoinOperands);
72 backEdge.setValue(endJoin);