11 #include "mlir/Pass/Pass.h"
18 #include "mlir/Transforms/DialectConversion.h"
22 #define GEN_PASS_DEF_KANAGAWAARGIFYBLOCKS
23 #include "circt/Dialect/Kanagawa/KanagawaPasses.h.inc"
27 using namespace circt;
28 using namespace kanagawa;
34 using ValueMapping = llvm::MapVector<Value, llvm::SmallVector<OpOperand *>>;
38 static void getExternallyDefinedOperands(StaticBlockOp blockOp,
39 ValueMapping &mapping) {
40 Block *blockBodyBlock = blockOp.getBodyBlock();
41 for (Operation &op : *blockBodyBlock) {
42 for (OpOperand &operand : op.getOpOperands()) {
43 Value v = operand.get();
44 if (v.getParentBlock() != blockBodyBlock)
45 mapping[v].push_back(&operand);
54 matchAndRewrite(StaticBlockOp blockOp, OpAdaptor adaptor,
55 ConversionPatternRewriter &rewriter)
const override {
57 getExternallyDefinedOperands(blockOp, mapping);
58 Block *bodyBlock = blockOp.getBodyBlock();
60 auto isolatedBlock = rewriter.create<IsolatedStaticBlockOp>(
61 blockOp.getLoc(), blockOp.getResultTypes(), blockOp.getOperands(),
62 blockOp.getMaxThreadsAttr());
64 Block *isolatedBlockBody = isolatedBlock.getBodyBlock();
65 rewriter.eraseOp(isolatedBlockBody->getTerminator());
66 llvm::SmallVector<Value> preAddBArgs;
67 llvm::copy(blockOp.getBodyBlock()->getArguments(),
68 std::back_inserter(preAddBArgs));
72 for (
auto &[value, uses] : mapping) {
73 isolatedBlock.getInputsMutable().append({value});
75 isolatedBlockBody->addArgument(value.getType(), value.getLoc());
76 for (OpOperand *operand : uses)
80 rewriter.mergeBlocks(bodyBlock, isolatedBlockBody, preAddBArgs);
81 rewriter.replaceOp(blockOp, isolatedBlock.getResults());
86 struct ArgifyBlocksPass
87 :
public circt::kanagawa::impl::KanagawaArgifyBlocksBase<ArgifyBlocksPass> {
88 void runOnOperation()
override;
92 void ArgifyBlocksPass::runOnOperation() {
93 auto *ctx = &getContext();
94 ConversionTarget target(*ctx);
95 target.addIllegalOp<StaticBlockOp>();
96 target.addLegalDialect<KanagawaDialect>();
99 patterns.add<BlockConversionPattern>(ctx);
102 applyPartialConversion(getOperation(), target, std::move(
patterns))))
107 return std::make_unique<ArgifyBlocksPass>();
std::unique_ptr< mlir::Pass > createArgifyBlocksPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.