11 #include "mlir/Dialect/Func/IR/FuncOps.h"
12 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
13 #include "mlir/Transforms/DialectConversion.h"
14 #include "llvm/Support/Debug.h"
16 #define DEBUG_TYPE "arc-lower-arcs-to-funcs"
20 #define GEN_PASS_DEF_LOWERARCSTOFUNCS
21 #include "circt/Dialect/Arc/ArcPasses.h.inc"
26 using namespace circt;
33 struct LowerArcsToFuncsPass
34 :
public arc::impl::LowerArcsToFuncsBase<LowerArcsToFuncsPass> {
36 LogicalResult lowerToFuncs();
37 void runOnOperation()
override;
41 using OpConversionPattern::OpConversionPattern;
43 matchAndRewrite(arc::DefineOp op, OpAdaptor adaptor,
44 ConversionPatternRewriter &rewriter)
const final {
45 auto func = rewriter.create<mlir::func::FuncOp>(op.getLoc(), op.getName(),
46 op.getFunctionType());
50 rewriter.inlineRegionBefore(op.getRegion(), func.getBody(), func.end());
57 using OpConversionPattern::OpConversionPattern;
59 matchAndRewrite(arc::OutputOp op, OpAdaptor adaptor,
60 ConversionPatternRewriter &rewriter)
const final {
61 rewriter.replaceOpWithNewOp<func::ReturnOp>(op, adaptor.getOutputs());
67 using OpConversionPattern::OpConversionPattern;
69 matchAndRewrite(arc::CallOp op, OpAdaptor adaptor,
70 ConversionPatternRewriter &rewriter)
const final {
71 SmallVector<Type> newResultTypes;
73 typeConverter->convertTypes(op.getResultTypes(), newResultTypes)))
75 rewriter.replaceOpWithNewOp<func::CallOp>(
76 op, newResultTypes, op.getArcAttr(), adaptor.getInputs());
82 using OpConversionPattern::OpConversionPattern;
84 matchAndRewrite(arc::StateOp op, OpAdaptor adaptor,
85 ConversionPatternRewriter &rewriter)
const final {
86 SmallVector<Type> newResultTypes;
88 typeConverter->convertTypes(op.getResultTypes(), newResultTypes)))
90 rewriter.replaceOpWithNewOp<func::CallOp>(
91 op, newResultTypes, op.getArcAttr(), adaptor.getInputs());
99 target.addLegalDialect<func::FuncDialect>();
100 target.addLegalDialect<LLVM::LLVMDialect>();
102 target.addIllegalOp<arc::CallOp>();
103 target.addIllegalOp<arc::DefineOp>();
104 target.addIllegalOp<arc::OutputOp>();
105 target.addIllegalOp<arc::StateOp>();
109 TypeConverter &typeConverter) {
110 auto *context =
patterns.getContext();
112 .add<CallOpLowering, DefineOpLowering, OutputOpLowering, StateOpLowering>(
113 typeConverter, context);
117 typeConverter.addConversion([&](Type type) {
return type; });
120 LogicalResult LowerArcsToFuncsPass::lowerToFuncs() {
121 LLVM_DEBUG(llvm::dbgs() <<
"Lowering arcs to funcs\n");
122 ConversionTarget target(getContext());
123 TypeConverter converter;
124 RewritePatternSet
patterns(&getContext());
128 return applyPartialConversion(getOperation(), target, std::move(
patterns));
131 void LowerArcsToFuncsPass::runOnOperation() {
132 if (failed(lowerToFuncs()))
133 return signalPassFailure();
137 return std::make_unique<LowerArcsToFuncsPass>();
static void populateLegality(ConversionTarget &target)
static void populateOpConversion(RewritePatternSet &patterns, TypeConverter &typeConverter)
static void populateTypeConversion(TypeConverter &typeConverter)
std::unique_ptr< mlir::Pass > createLowerArcsToFuncsPass()
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.