11 #include "mlir/Pass/Pass.h"
21 #include "mlir/Transforms/DialectConversion.h"
27 #define GEN_PASS_DEF_KANAGAWACONVERTHANDSHAKETODC
28 #include "circt/Dialect/Kanagawa/KanagawaPasses.h.inc"
33 using namespace circt;
34 using namespace kanagawa;
38 struct ConvertHandshakeToDCPass
39 :
public circt::kanagawa::impl::KanagawaConvertHandshakeToDCBase<
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<kanagawa::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 kanagawa::ClassOp classOp = getOperation();
90 auto targetModifier = [&](mlir::ConversionTarget &target) {
91 target.addDynamicallyLegalOp<kanagawa::DataflowMethodOp>(
92 [](kanagawa::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<kanagawa::ReturnOp>(
isDCTypedOp);
98 target.addLegalDialect<hw::HWDialect, kanagawa::KanagawaDialect>();
99 target.addIllegalOp<kanagawa::IsolatedStaticBlockOp>();
103 target.addLegalOp<kanagawa::DCBlockOp>();
104 target.markOpRecursivelyLegal<kanagawa::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.