11 #include "mlir/Pass/Pass.h"
21 #include "mlir/Transforms/DialectConversion.h"
27 #define GEN_PASS_DEF_IBISCONVERTHANDSHAKETODC
28 #include "circt/Dialect/Ibis/IbisPasses.h.inc"
33 using namespace circt;
38 struct ConvertHandshakeToDCPass
39 :
public circt::ibis::impl::IbisConvertHandshakeToDCBase<
40 ConvertHandshakeToDCPass> {
41 void runOnOperation()
override;
46 using OpAdaptor =
typename ReturnOp::Adaptor;
49 matchAndRewrite(ReturnOp op, OpAdaptor adaptor,
50 ConversionPatternRewriter &rewriter)
const override {
51 rewriter.replaceOpWithNewOp<ibis::ReturnOp>(op, adaptor.getOperands());
56 struct StaticBlockOpConversion
59 using OpAdaptor =
typename IsolatedStaticBlockOp::Adaptor;
62 matchAndRewrite(IsolatedStaticBlockOp op, OpAdaptor adaptor,
63 ConversionPatternRewriter &rewriter)
const override {
64 llvm::SmallVector<Type> resultTypes;
66 getTypeConverter()->convertTypes(op.getResultTypes(), resultTypes)))
68 auto dcBlock = rewriter.create<DCBlockOp>(op.getLoc(), resultTypes,
69 adaptor.getOperands());
70 rewriter.eraseOp(dcBlock.getBodyBlock()->getTerminator());
71 rewriter.mergeBlocks(op.getBodyBlock(), dcBlock.getBodyBlock(),
72 dcBlock.getBodyBlock()->getArguments());
74 rewriter.replaceOp(op, dcBlock.getResults());
81 static bool isDCType(Type t) {
return isa<dc::ValueType, dc::TokenType>(t); }
84 return llvm::all_of(op->getOperandTypes(),
isDCType) &&
85 llvm::all_of(op->getResultTypes(),
isDCType);
88 void ConvertHandshakeToDCPass::runOnOperation() {
89 ibis::ClassOp classOp = getOperation();
90 auto targetModifier = [&](mlir::ConversionTarget &target) {
91 target.addDynamicallyLegalOp<ibis::DataflowMethodOp>(
92 [](ibis::DataflowMethodOp op) {
93 auto methodLikeOp = cast<MethodLikeOpInterface>(op.getOperation());
94 return llvm::all_of(methodLikeOp.getArgumentTypes(),
isDCType) &&
95 llvm::all_of(methodLikeOp.getResultTypes(),
isDCType);
97 target.addDynamicallyLegalOp<ibis::ReturnOp>(
isDCTypedOp);
98 target.addLegalDialect<hw::HWDialect, ibis::IbisDialect>();
99 target.addIllegalOp<ibis::IsolatedStaticBlockOp>();
103 target.addLegalOp<ibis::DCBlockOp>();
104 target.markOpRecursivelyLegal<ibis::DCBlockOp>();
107 auto patternBuilder = [&](TypeConverter &typeConverter,
114 typeConverter, classOp.getContext());
124 return std::make_unique<ConvertHandshakeToDCPass>();
static bool isDCType(Type t)
static bool isDCTypedOp(Operation *op)
DenseSet< Operation * > ConvertedOps
LogicalResult runHandshakeToDC(mlir::Operation *op, llvm::function_ref< void(TypeConverter &typeConverter, ConvertedOps &convertedOps, RewritePatternSet &patterns)> patternBuilder, llvm::function_ref< void(mlir::ConversionTarget &)> configureTarget={})
std::unique_ptr< mlir::Pass > createConvertHandshakeToDCPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.