1 | //===- Bufferize.cpp - Bufferization for func ops -------------------------===// |
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 | // This file implements bufferization of func.func's and func.call's. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "mlir/Dialect/Func/Transforms/Passes.h" |
14 | |
15 | #include "mlir/Dialect/Bufferization/IR/Bufferization.h" |
16 | #include "mlir/Dialect/Bufferization/Transforms/Bufferize.h" |
17 | #include "mlir/Dialect/Func/IR/FuncOps.h" |
18 | #include "mlir/Dialect/Func/Transforms/FuncConversions.h" |
19 | #include "mlir/Dialect/MemRef/IR/MemRef.h" |
20 | #include "mlir/Transforms/DialectConversion.h" |
21 | |
22 | namespace mlir { |
23 | #define GEN_PASS_DEF_FUNCBUFFERIZE |
24 | #include "mlir/Dialect/Func/Transforms/Passes.h.inc" |
25 | } // namespace mlir |
26 | |
27 | using namespace mlir; |
28 | using namespace mlir::func; |
29 | |
30 | namespace { |
31 | struct FuncBufferizePass : public impl::FuncBufferizeBase<FuncBufferizePass> { |
32 | using FuncBufferizeBase<FuncBufferizePass>::FuncBufferizeBase; |
33 | void runOnOperation() override { |
34 | auto module = getOperation(); |
35 | auto *context = &getContext(); |
36 | |
37 | bufferization::BufferizeTypeConverter typeConverter; |
38 | RewritePatternSet patterns(context); |
39 | ConversionTarget target(*context); |
40 | |
41 | populateFunctionOpInterfaceTypeConversionPattern<FuncOp>(patterns, |
42 | typeConverter); |
43 | target.addDynamicallyLegalOp<FuncOp>([&](FuncOp op) { |
44 | return typeConverter.isSignatureLegal(op.getFunctionType()) && |
45 | typeConverter.isLegal(&op.getBody()); |
46 | }); |
47 | populateCallOpTypeConversionPattern(patterns, converter&: typeConverter); |
48 | target.addDynamicallyLegalOp<CallOp>( |
49 | [&](CallOp op) { return typeConverter.isLegal(op); }); |
50 | |
51 | populateBranchOpInterfaceTypeConversionPattern(patterns, converter&: typeConverter); |
52 | populateReturnOpTypeConversionPattern(patterns, converter&: typeConverter); |
53 | target.addLegalOp<ModuleOp, bufferization::ToTensorOp, |
54 | bufferization::ToMemrefOp>(); |
55 | |
56 | target.markUnknownOpDynamicallyLegal([&](Operation *op) { |
57 | return isNotBranchOpInterfaceOrReturnLikeOp(op) || |
58 | isLegalForBranchOpInterfaceTypeConversionPattern(op, |
59 | converter&: typeConverter) || |
60 | isLegalForReturnOpTypeConversionPattern(op, converter&: typeConverter); |
61 | }); |
62 | |
63 | if (failed(applyFullConversion(module, target, std::move(patterns)))) |
64 | signalPassFailure(); |
65 | } |
66 | }; |
67 | } // namespace |
68 | |
69 | std::unique_ptr<Pass> mlir::func::createFuncBufferizePass() { |
70 | return std::make_unique<FuncBufferizePass>(); |
71 | } |
72 | |