MLIR  19.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 
16 using namespace mlir;
17 using namespace mlir::bufferization;
18 
19 namespace {
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 /// ```
48 struct SelectOpInterface
49  : public BufferDeallocationOpInterface::ExternalModel<SelectOpInterface,
50  arith::SelectOp> {
52  const DeallocationOptions &options) const {
53  return op; // nothing to do
54  }
55 
56  std::pair<Value, Value>
57  materializeUniqueOwnershipForMemref(Operation *op, DeallocationState &state,
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 = builder.create<arith::SelectOp>(
71  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
Block represents an ordered list of Operations.
Definition: Block.h:31
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
void addExtension(std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
This class provides support for representing a failure result, or a valid value of type T.
Definition: LogicalResult.h:78
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
This class helps build Operations.
Definition: Builders.h:209
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:464
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
Definition: Operation.h:402
Location getLoc()
The source location the operation was defined or derived from.
Definition: Operation.h:223
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
Block * getParentBlock()
Return the Block in which this Value is defined.
Definition: Value.cpp:48
This class collects all the state that we need to perform the buffer deallocation pass with associate...
void registerBufferDeallocationOpInterfaceExternalModels(DialectRegistry &registry)
Include the generated interface declarations.
Options for BufferDeallocationOpInterface-based buffer deallocation.