CIRCT  20.0.0git
KanagawaConvertHandshakeToDC.cpp
Go to the documentation of this file.
1 //===- KanagawaConvertHandshakeToDCPass.cpp -------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
11 #include "mlir/Pass/Pass.h"
12 
19 
21 #include "mlir/Transforms/DialectConversion.h"
22 
24 
25 namespace circt {
26 namespace kanagawa {
27 #define GEN_PASS_DEF_KANAGAWACONVERTHANDSHAKETODC
28 #include "circt/Dialect/Kanagawa/KanagawaPasses.h.inc"
29 } // namespace kanagawa
30 } // namespace circt
31 
32 using namespace mlir;
33 using namespace circt;
34 using namespace kanagawa;
35 
36 namespace {
37 
38 struct ConvertHandshakeToDCPass
39  : public circt::kanagawa::impl::KanagawaConvertHandshakeToDCBase<
40  ConvertHandshakeToDCPass> {
41  void runOnOperation() override;
42 };
43 
44 class ReturnOpConversion : public OpConversionPattern<ReturnOp> {
46  using OpAdaptor = typename ReturnOp::Adaptor;
47 
48  LogicalResult
49  matchAndRewrite(ReturnOp op, OpAdaptor adaptor,
50  ConversionPatternRewriter &rewriter) const override {
51  rewriter.replaceOpWithNewOp<kanagawa::ReturnOp>(op, adaptor.getOperands());
52  return success();
53  }
54 };
55 
56 struct StaticBlockOpConversion
57  : public OpConversionPattern<IsolatedStaticBlockOp> {
59  using OpAdaptor = typename IsolatedStaticBlockOp::Adaptor;
60 
61  LogicalResult
62  matchAndRewrite(IsolatedStaticBlockOp op, OpAdaptor adaptor,
63  ConversionPatternRewriter &rewriter) const override {
64  llvm::SmallVector<Type> resultTypes;
65  if (failed(
66  getTypeConverter()->convertTypes(op.getResultTypes(), resultTypes)))
67  return failure();
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());
73 
74  rewriter.replaceOp(op, dcBlock.getResults());
75  return success();
76  }
77 };
78 
79 } // anonymous namespace
80 
81 static bool isDCType(Type t) { return isa<dc::ValueType, dc::TokenType>(t); }
82 
83 static bool isDCTypedOp(Operation *op) {
84  return llvm::all_of(op->getOperandTypes(), isDCType) &&
85  llvm::all_of(op->getResultTypes(), isDCType);
86 }
87 
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);
96  });
97  target.addDynamicallyLegalOp<kanagawa::ReturnOp>(isDCTypedOp);
98  target.addLegalDialect<hw::HWDialect, kanagawa::KanagawaDialect>();
99  target.addIllegalOp<kanagawa::IsolatedStaticBlockOp>();
100 
101  // kanagawa.sblock.dc ops are recursively legal - we're only considering the
102  // DataflowMethodOp's region for conversion.
103  target.addLegalOp<kanagawa::DCBlockOp>();
104  target.markOpRecursivelyLegal<kanagawa::DCBlockOp>();
105  };
106 
107  auto patternBuilder = [&](TypeConverter &typeConverter,
108  handshaketodc::ConvertedOps &convertedOps,
109  RewritePatternSet &patterns) {
110  patterns
113  TypeOpConversionPattern<StaticBlockOp>, StaticBlockOpConversion>(
114  typeConverter, classOp.getContext());
115  };
116 
117  LogicalResult res = handshaketodc::runHandshakeToDC(
118  classOp, {"clk", "rst"}, patternBuilder, targetModifier);
119  if (failed(res))
120  signalPassFailure();
121 }
122 
124  return std::make_unique<ConvertHandshakeToDCPass>();
125 }
static bool isDCType(Type t)
static bool isDCTypedOp(Operation *op)
DenseSet< Operation * > ConvertedOps
Definition: HandshakeToDC.h:31
LogicalResult runHandshakeToDC(mlir::Operation *op, HandshakeToDCOptions options, 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.
Definition: DebugAnalysis.h:21