MLIR  19.0.0git
FuncBufferize.cpp
Go to the documentation of this file.
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 
14 
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 
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, typeConverter);
48  target.addDynamicallyLegalOp<CallOp>(
49  [&](CallOp op) { return typeConverter.isLegal(op); });
50 
51  populateBranchOpInterfaceTypeConversionPattern(patterns, typeConverter);
52  populateReturnOpTypeConversionPattern(patterns, typeConverter);
53  target.addLegalOp<ModuleOp, bufferization::ToTensorOp,
54  bufferization::ToMemrefOp>();
55 
59  typeConverter) ||
60  isLegalForReturnOpTypeConversionPattern(op, 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 }
static MLIRContext * getContext(OpFoldResult val)
This class describes a specific conversion target.
void addLegalOp(OperationName op)
Register the given operations as legal.
void markUnknownOpDynamicallyLegal(const DynamicLegalityCallbackFn &fn)
Register unknown operations as dynamically legal.
void addDynamicallyLegalOp(OperationName op, const DynamicLegalityCallbackFn &callback)
Register the given operation as dynamically legal and set the dynamic legalization callback to the on...
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
bool isLegal(Type type) const
Return true if the given type is legal for this type converter, i.e.
bool isSignatureLegal(FunctionType ty) const
Return true if the inputs and outputs of the given function type are legal.
A helper type converter class that automatically populates the relevant materializations and type con...
Definition: Bufferize.h:43
std::unique_ptr< Pass > createFuncBufferizePass()
Creates an instance of func bufferization pass.
Include the generated interface declarations.
void populateCallOpTypeConversionPattern(RewritePatternSet &patterns, TypeConverter &converter)
Add a pattern to the given pattern list to convert the operand and result types of a CallOp with the ...
void populateReturnOpTypeConversionPattern(RewritePatternSet &patterns, TypeConverter &converter)
Add a pattern to the given pattern list to rewrite return ops to use operands that have been legalize...
void populateBranchOpInterfaceTypeConversionPattern(RewritePatternSet &patterns, TypeConverter &converter, function_ref< bool(BranchOpInterface branchOp, int idx)> shouldConvertBranchOperand=nullptr)
Add a pattern to the given pattern list to rewrite branch operations to use operands that have been l...
LogicalResult applyFullConversion(ArrayRef< Operation * > ops, const ConversionTarget &target, const FrozenRewritePatternSet &patterns, ConversionConfig config=ConversionConfig())
Apply a complete conversion on the given operations, and all nested operations.
bool isNotBranchOpInterfaceOrReturnLikeOp(Operation *op)
Return true if op is neither BranchOpInterface nor ReturnLike.
bool isLegalForReturnOpTypeConversionPattern(Operation *op, TypeConverter &converter, bool returnOpAlwaysLegal=false)
For ReturnLike ops (except return), return True.
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
Definition: LogicalResult.h:72
bool isLegalForBranchOpInterfaceTypeConversionPattern(Operation *op, TypeConverter &converter)
Return true if op is a BranchOpInterface op whose operands are all legal according to converter.