11 #include "mlir/Pass/Pass.h"
20 #include "mlir/Transforms/DialectConversion.h"
24 #define GEN_PASS_DEF_KANAGAWACONVERTMETHODSTOCONTAINERS
25 #include "circt/Dialect/Kanagawa/KanagawaPasses.h.inc"
29 using namespace circt;
30 using namespace kanagawa;
34 struct DataflowMethodOpConversion
37 using OpAdaptor =
typename DataflowMethodOp::Adaptor;
40 matchAndRewrite(DataflowMethodOp op, OpAdaptor adaptor,
41 ConversionPatternRewriter &rewriter)
const override {
43 auto newContainer = rewriter.create<ContainerOp>(
44 op.getLoc(), op.getInnerSym(),
false);
45 rewriter.setInsertionPointToStart(newContainer.getBodyBlock());
50 rewriter.create<ThisOp>(op.getLoc(), newContainer.getInnerRef());
53 llvm::SmallVector<Value> argValues;
54 for (
auto [arg, name] : llvm::zip_equal(
55 op.getArguments(), op.getArgNames().getAsRange<StringAttr>())) {
56 auto port = rewriter.create<InputPortOp>(
58 argValues.push_back(rewriter.create<PortReadOp>(arg.getLoc(), port));
61 ReturnOp returnOp = cast<ReturnOp>(op.getBodyBlock()->getTerminator());
62 for (
auto [idx, resType] : llvm::enumerate(
63 cast<MethodLikeOpInterface>(op.getOperation()).getResultTypes())) {
64 auto portName = rewriter.getStringAttr(
"out" + std::to_string(idx));
65 auto port = rewriter.create<OutputPortOp>(
67 rewriter.create<PortWriteOp>(op.getLoc(), port, returnOp.getOperand(idx));
70 rewriter.mergeBlocks(op.getBodyBlock(), newContainer.getBodyBlock(),
73 rewriter.eraseOp(returnOp);
78 struct MethodsToContainersPass
79 :
public circt::kanagawa::impl::KanagawaConvertMethodsToContainersBase<
80 MethodsToContainersPass> {
81 void runOnOperation()
override;
85 void MethodsToContainersPass::runOnOperation() {
86 auto *context = &getContext();
87 ConversionTarget target(*context);
88 target.addLegalDialect<KanagawaDialect>();
89 target.addIllegalOp<DataflowMethodOp>();
90 target.addIllegalOp<ReturnOp>();
93 patterns.insert<DataflowMethodOpConversion>(context);
95 applyPartialConversion(getOperation(), target, std::move(
patterns))))
100 return std::make_unique<MethodsToContainersPass>();
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
std::unique_ptr< mlir::Pass > createConvertMethodsToContainersPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.