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
9 #include "mlir/Dialect/Arith/Transforms/BufferDeallocationOpInterfaceImpl.h"
10 #include "mlir/Dialect/Arith/IR/Arith.h"
11 #include "mlir/Dialect/Bufferization/IR/BufferDeallocationOpInterface.h"
12 #include "mlir/Dialect/MemRef/IR/MemRef.h"
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> {
process__anon78b22bd10111::SelectOpInterface51 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>
materializeUniqueOwnershipForMemref__anon78b22bd10111::SelectOpInterface57 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 = 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
registerBufferDeallocationOpInterfaceExternalModels(DialectRegistry & registry)80 void mlir::arith::registerBufferDeallocationOpInterfaceExternalModels(
81 DialectRegistry ®istry) {
82 registry.addExtension(+[](MLIRContext *ctx, ArithDialect *dialect) {
83 SelectOp::attachInterface<SelectOpInterface>(*ctx);
84 });
85 }
86