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"
30using namespace kanagawa;
34struct 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());
48 llvm::SmallVector<Value> argValues;
49 for (
auto [arg, name] : llvm::zip_equal(
50 op.getArguments(), op.getArgNames().getAsRange<StringAttr>())) {
51 auto port = rewriter.create<InputPortOp>(
52 arg.getLoc(), hw::InnerSymAttr::get(name), arg.getType(), name);
53 argValues.push_back(rewriter.create<PortReadOp>(arg.getLoc(), port));
56 ReturnOp returnOp = cast<ReturnOp>(op.getBodyBlock()->getTerminator());
57 for (
auto [idx, resType] : llvm::enumerate(
58 cast<MethodLikeOpInterface>(op.getOperation()).getResultTypes())) {
59 auto portName = rewriter.getStringAttr(
"out" + std::to_string(idx));
60 auto port = rewriter.create<OutputPortOp>(
61 op.getLoc(), hw::InnerSymAttr::get(portName), resType, portName);
62 rewriter.create<PortWriteOp>(op.getLoc(), port, returnOp.getOperand(idx));
65 rewriter.mergeBlocks(op.getBodyBlock(), newContainer.getBodyBlock(),
68 rewriter.eraseOp(returnOp);
73struct MethodsToContainersPass
74 :
public circt::kanagawa::impl::KanagawaConvertMethodsToContainersBase<
75 MethodsToContainersPass> {
76 void runOnOperation()
override;
80void MethodsToContainersPass::runOnOperation() {
81 auto *context = &getContext();
82 ConversionTarget target(*context);
83 target.addLegalDialect<KanagawaDialect>();
84 target.addIllegalOp<DataflowMethodOp>();
85 target.addIllegalOp<ReturnOp>();
88 patterns.insert<DataflowMethodOpConversion>(context);
90 applyPartialConversion(getOperation(), target, std::move(
patterns))))
95 return std::make_unique<MethodsToContainersPass>();
std::unique_ptr< mlir::Pass > createConvertMethodsToContainersPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.