11 #include "mlir/Pass/Pass.h"
17 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
19 #include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
22 #include "mlir/Transforms/DialectConversion.h"
28 #define GEN_PASS_DEF_KANAGAWAINLINESBLOCKS
29 #include "circt/Dialect/Kanagawa/KanagawaPasses.h.inc"
34 using namespace circt;
35 using namespace kanagawa;
39 struct InlineSBlocksPass
40 :
public circt::kanagawa::impl::KanagawaInlineSBlocksBase<
42 void runOnOperation()
override;
45 class InlineSBlocksPattern
53 matchAndRewrite(kanagawa::StaticBlockOp op, OpAdaptor adaptor,
54 ConversionPatternRewriter &rewriter)
const override {
55 Location loc = op.getLoc();
58 bool hasAttributes = !op->getAttrs().empty();
63 rewriter.create<kanagawa::InlineStaticBlockBeginOp>(loc);
64 inlineStart->setAttrs(op->getAttrs());
68 Block *sblockBody = op.getBodyBlock();
69 BlockReturnOp ret = cast<BlockReturnOp>(sblockBody->getTerminator());
70 rewriter.inlineBlockBefore(sblockBody, op->getNextNode());
74 for (
auto [res, val] : llvm::zip(op.getResults(), ret.getRetValues()))
75 rewriter.replaceAllUsesWith(res, val);
79 rewriter.setInsertionPoint(ret);
80 rewriter.create<kanagawa::InlineStaticBlockEndOp>(loc);
83 rewriter.eraseOp(ret);
91 void InlineSBlocksPass::runOnOperation() {
92 MethodOp parent = getOperation();
93 ConversionTarget target(getContext());
94 target.addIllegalOp<kanagawa::StaticBlockOp>();
97 target.markUnknownOpDynamicallyLegal([](Operation *op) {
return true; });
98 RewritePatternSet
patterns(&getContext());
99 patterns.add<InlineSBlocksPattern>(&getContext());
101 if (failed(applyPartialConversion(parent, target, std::move(
patterns))))
102 return signalPassFailure();
106 return std::make_unique<InlineSBlocksPass>();
std::unique_ptr< mlir::Pass > createInlineSBlocksPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.