CIRCT 20.0.0git
Loading...
Searching...
No Matches
KanagawaMethodsToContainers.cpp
Go to the documentation of this file.
1//===- KanagawaMethodsToContainers.cpp - Implementation of containerizing -===//
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
17
20#include "mlir/Transforms/DialectConversion.h"
21
22namespace circt {
23namespace kanagawa {
24#define GEN_PASS_DEF_KANAGAWACONVERTMETHODSTOCONTAINERS
25#include "circt/Dialect/Kanagawa/KanagawaPasses.h.inc"
26} // namespace kanagawa
27} // namespace circt
28
29using namespace circt;
30using namespace kanagawa;
31
32namespace {
33
34struct DataflowMethodOpConversion
35 : public OpConversionPattern<DataflowMethodOp> {
36 using OpConversionPattern<DataflowMethodOp>::OpConversionPattern;
37 using OpAdaptor = typename DataflowMethodOp::Adaptor;
38
39 LogicalResult
40 matchAndRewrite(DataflowMethodOp op, OpAdaptor adaptor,
41 ConversionPatternRewriter &rewriter) const override {
42 // Replace the class by a container of the same name.
43 auto newContainer = rewriter.create<ContainerOp>(
44 op.getLoc(), op.getInnerSym(), /*isTopLevel=*/false);
45 rewriter.setInsertionPointToStart(newContainer.getBodyBlock());
46
47 // Create in- and output ports.
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));
54 }
55
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));
63 }
64
65 rewriter.mergeBlocks(op.getBodyBlock(), newContainer.getBodyBlock(),
66 argValues);
67 rewriter.eraseOp(op);
68 rewriter.eraseOp(returnOp);
69 return success();
70 }
71};
72
73struct MethodsToContainersPass
74 : public circt::kanagawa::impl::KanagawaConvertMethodsToContainersBase<
75 MethodsToContainersPass> {
76 void runOnOperation() override;
77};
78} // anonymous namespace
79
80void MethodsToContainersPass::runOnOperation() {
81 auto *context = &getContext();
82 ConversionTarget target(*context);
83 target.addLegalDialect<KanagawaDialect>();
84 target.addIllegalOp<DataflowMethodOp>();
85 target.addIllegalOp<ReturnOp>();
86 RewritePatternSet patterns(context);
87
88 patterns.insert<DataflowMethodOpConversion>(context);
89 if (failed(
90 applyPartialConversion(getOperation(), target, std::move(patterns))))
91 signalPassFailure();
92}
93
95 return std::make_unique<MethodsToContainersPass>();
96}
std::unique_ptr< mlir::Pass > createConvertMethodsToContainersPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.