CIRCT  19.0.0git
IbisConvertHandshakeToDC.cpp
Go to the documentation of this file.
1 //===- IbisConvertHandshakeToDCPass.cpp -----------------------------------===//
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 
9 #include "PassDetails.h"
10 
18 
20 #include "mlir/Transforms/DialectConversion.h"
21 
23 
24 using namespace mlir;
25 using namespace circt;
26 using namespace ibis;
27 
28 namespace {
29 
30 struct ConvertHandshakeToDCPass
31  : public IbisConvertHandshakeToDCBase<ConvertHandshakeToDCPass> {
32  void runOnOperation() override;
33 };
34 
35 class ReturnOpConversion : public OpConversionPattern<ReturnOp> {
37  using OpAdaptor = typename ReturnOp::Adaptor;
38 
39  LogicalResult
40  matchAndRewrite(ReturnOp op, OpAdaptor adaptor,
41  ConversionPatternRewriter &rewriter) const override {
42  rewriter.replaceOpWithNewOp<ibis::ReturnOp>(op, adaptor.getOperands());
43  return success();
44  }
45 };
46 
47 struct StaticBlockOpConversion
48  : public OpConversionPattern<IsolatedStaticBlockOp> {
50  using OpAdaptor = typename IsolatedStaticBlockOp::Adaptor;
51 
52  LogicalResult
53  matchAndRewrite(IsolatedStaticBlockOp op, OpAdaptor adaptor,
54  ConversionPatternRewriter &rewriter) const override {
55  llvm::SmallVector<Type> resultTypes;
56  if (failed(
57  getTypeConverter()->convertTypes(op.getResultTypes(), resultTypes)))
58  return failure();
59  auto dcBlock = rewriter.create<DCBlockOp>(op.getLoc(), resultTypes,
60  adaptor.getOperands());
61  rewriter.eraseOp(dcBlock.getBodyBlock()->getTerminator());
62  rewriter.mergeBlocks(op.getBodyBlock(), dcBlock.getBodyBlock(),
63  dcBlock.getBodyBlock()->getArguments());
64 
65  rewriter.replaceOp(op, dcBlock.getResults());
66  return success();
67  }
68 };
69 
70 } // anonymous namespace
71 
72 static bool isDCType(Type t) { return t.isa<dc::ValueType, dc::TokenType>(); }
73 
74 static bool isDCTypedOp(Operation *op) {
75  return llvm::all_of(op->getOperandTypes(), isDCType) &&
76  llvm::all_of(op->getResultTypes(), isDCType);
77 }
78 
79 void ConvertHandshakeToDCPass::runOnOperation() {
80  ibis::ClassOp classOp = getOperation();
81  auto targetModifier = [&](mlir::ConversionTarget &target) {
82  target.addDynamicallyLegalOp<ibis::DataflowMethodOp>(
83  [](ibis::DataflowMethodOp op) {
84  auto methodLikeOp = cast<MethodLikeOpInterface>(op.getOperation());
85  return llvm::all_of(methodLikeOp.getArgumentTypes(), isDCType) &&
86  llvm::all_of(methodLikeOp.getResultTypes(), isDCType);
87  });
88  target.addDynamicallyLegalOp<ibis::ReturnOp>(isDCTypedOp);
89  target.addLegalDialect<hw::HWDialect, ibis::IbisDialect>();
90  target.addIllegalOp<ibis::IsolatedStaticBlockOp>();
91 
92  // ibis.sblock.dc ops are recursively legal - we're only considering the
93  // DataflowMethodOp's region for conversion.
94  target.addLegalOp<ibis::DCBlockOp>();
95  target.markOpRecursivelyLegal<ibis::DCBlockOp>();
96  };
97 
98  auto patternBuilder = [&](TypeConverter &typeConverter,
99  handshaketodc::ConvertedOps &convertedOps,
100  RewritePatternSet &patterns) {
101  patterns
104  TypeOpConversionPattern<StaticBlockOp>, StaticBlockOpConversion>(
105  typeConverter, classOp.getContext());
106  };
107 
108  LogicalResult res =
109  handshaketodc::runHandshakeToDC(classOp, patternBuilder, targetModifier);
110  if (failed(res))
111  signalPassFailure();
112 }
113 
115  return std::make_unique<ConvertHandshakeToDCPass>();
116 }
static bool isDCType(Type t)
static bool isDCTypedOp(Operation *op)
DenseSet< Operation * > ConvertedOps
Definition: HandshakeToDC.h:30
LogicalResult runHandshakeToDC(mlir::Operation *op, llvm::function_ref< void(TypeConverter &typeConverter, ConvertedOps &convertedOps, RewritePatternSet &patterns)> patternBuilder, llvm::function_ref< void(mlir::ConversionTarget &)> configureTarget={})
std::unique_ptr< mlir::Pass > createConvertHandshakeToDCPass()
This file defines an intermediate representation for circuits acting as an abstraction for constraint...
Definition: DebugAnalysis.h:21