MLIR 22.0.0git
BufferDeallocationOpInterfaceImpl.cpp
Go to the documentation of this file.
1//===- BufferDeallocationOpInterfaceImpl.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
13#include "mlir/IR/Dialect.h"
14#include "mlir/IR/Operation.h"
15
16using namespace mlir;
17using namespace mlir::bufferization;
18
19namespace {
20/// Provides custom logic to materialize ownership indicator values for the
21/// result value of 'arith.select'. Instead of cloning or runtime alias
22/// checking, this implementation inserts another `arith.select` to choose the
23/// ownership indicator of the operand in the same way the original
24/// `arith.select` chooses the MemRef operand. If at least one of the operand's
25/// ownerships is 'Unknown', fall back to the default implementation.
26///
27/// Example:
28/// ```mlir
29/// // let ownership(%m0) := %o0
30/// // let ownership(%m1) := %o1
31/// %res = arith.select %cond, %m0, %m1
32/// ```
33/// The default implementation would insert a clone and replace all uses of the
34/// result of `arith.select` with that clone:
35/// ```mlir
36/// %res = arith.select %cond, %m0, %m1
37/// %clone = bufferization.clone %res
38/// // let ownership(%res) := 'Unknown'
39/// // let ownership(%clone) := %true
40/// // replace all uses of %res with %clone
41/// ```
42/// This implementation, on the other hand, materializes the following:
43/// ```mlir
44/// %res = arith.select %cond, %m0, %m1
45/// %res_ownership = arith.select %cond, %o0, %o1
46/// // let ownership(%res) := %res_ownership
47/// ```
48struct SelectOpInterface
49 : public BufferDeallocationOpInterface::ExternalModel<SelectOpInterface,
50 arith::SelectOp> {
51 FailureOr<Operation *> process(Operation *op, DeallocationState &state,
52 const DeallocationOptions &options) const {
53 return op; // nothing to do
54 }
55
56 std::pair<Value, Value>
57 materializeUniqueOwnershipForMemref(Operation *op, DeallocationState &state,
58 const DeallocationOptions &options,
59 OpBuilder &builder, Value value) const {
60 auto selectOp = cast<arith::SelectOp>(op);
61 assert(value == selectOp.getResult() &&
62 "Value not defined by this operation");
63
64 Block *block = value.getParentBlock();
65 if (!state.getOwnership(selectOp.getTrueValue(), block).isUnique() ||
66 !state.getOwnership(selectOp.getFalseValue(), block).isUnique())
67 return state.getMemrefWithUniqueOwnership(builder, value,
68 value.getParentBlock());
69
70 Value ownership = arith::SelectOp::create(
71 builder, op->getLoc(), selectOp.getCondition(),
72 state.getOwnership(selectOp.getTrueValue(), block).getIndicator(),
73 state.getOwnership(selectOp.getFalseValue(), block).getIndicator());
74 return {selectOp.getResult(), ownership};
75 }
76};
77
78} // namespace
79
81 DialectRegistry &registry) {
82 registry.addExtension(+[](MLIRContext *ctx, ArithDialect *dialect) {
83 SelectOp::attachInterface<SelectOpInterface>(*ctx);
84 });
85}
static llvm::ManagedStatic< PassManagerOptions > options
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
bool addExtension(TypeID extensionID, std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
Location getLoc()
The source location the operation was defined or derived from.
Definition Operation.h:223
Block * getParentBlock()
Return the Block in which this Value is defined.
Definition Value.cpp:46
Ownership getOwnership(Value memref, Block *block) const
Returns the ownership of 'memref' for the given basic block.
std::pair< Value, Value > getMemrefWithUniqueOwnership(OpBuilder &builder, Value memref, Block *block)
Given an SSA value of MemRef type, this function queries the ownership and if it is not already in th...
bool isUnique() const
Check if this ownership value is in the 'Unique' state.
Value getIndicator() const
If this ownership value is in 'Unique' state, this function can be used to get the indicator paramete...
void registerBufferDeallocationOpInterfaceExternalModels(DialectRegistry &registry)
Include the generated interface declarations.