MLIR  20.0.0git
AllocationOpInterfaceImpl.cpp
Go to the documentation of this file.
1 //===- AllocationOpInterfaceImpl.cpp - Impl. of AllocationOpInterface -----===//
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 
10 
14 #include "mlir/IR/Dialect.h"
15 #include "mlir/IR/Operation.h"
16 
17 using namespace mlir;
18 
19 namespace {
20 struct DefaultAllocationInterface
21  : public bufferization::AllocationOpInterface::ExternalModel<
22  DefaultAllocationInterface, memref::AllocOp> {
23  static std::optional<Operation *> buildDealloc(OpBuilder &builder,
24  Value alloc) {
25  return builder.create<memref::DeallocOp>(alloc.getLoc(), alloc)
26  .getOperation();
27  }
28  static std::optional<Value> buildClone(OpBuilder &builder, Value alloc) {
29  return builder.create<bufferization::CloneOp>(alloc.getLoc(), alloc)
30  .getResult();
31  }
32  static ::mlir::HoistingKind getHoistingKind() {
34  }
35  static ::std::optional<::mlir::Operation *>
36  buildPromotedAlloc(OpBuilder &builder, Value alloc) {
37  Operation *definingOp = alloc.getDefiningOp();
38  return builder.create<memref::AllocaOp>(
39  definingOp->getLoc(), cast<MemRefType>(definingOp->getResultTypes()[0]),
40  definingOp->getOperands(), definingOp->getAttrs());
41  }
42 };
43 
44 struct DefaultAutomaticAllocationHoistingInterface
45  : public bufferization::AllocationOpInterface::ExternalModel<
46  DefaultAutomaticAllocationHoistingInterface, memref::AllocaOp> {
47  static ::mlir::HoistingKind getHoistingKind() { return HoistingKind::Loop; }
48 };
49 
50 struct DefaultReallocationInterface
51  : public bufferization::AllocationOpInterface::ExternalModel<
52  DefaultAllocationInterface, memref::ReallocOp> {
53  static std::optional<Operation *> buildDealloc(OpBuilder &builder,
54  Value realloc) {
55  return builder.create<memref::DeallocOp>(realloc.getLoc(), realloc)
56  .getOperation();
57  }
58 };
59 } // namespace
60 
62  DialectRegistry &registry) {
63  registry.addExtension(+[](MLIRContext *ctx, memref::MemRefDialect *dialect) {
64  memref::AllocOp::attachInterface<DefaultAllocationInterface>(*ctx);
65  memref::AllocaOp::attachInterface<
66  DefaultAutomaticAllocationHoistingInterface>(*ctx);
67  memref::ReallocOp::attachInterface<DefaultReallocationInterface>(*ctx);
68  });
69 }
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:60
This class helps build Operations.
Definition: Builders.h:215
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:488
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
Location getLoc()
The source location the operation was defined or derived from.
Definition: Operation.h:223
ArrayRef< NamedAttribute > getAttrs()
Return all of the attributes on this operation.
Definition: Operation.h:507
result_type_range getResultTypes()
Definition: Operation.h:423
operand_range getOperands()
Returns an iterator on the underlying Value's.
Definition: Operation.h:373
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
Location getLoc() const
Return the location of this value.
Definition: Value.cpp:26
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
Definition: Value.cpp:20
void registerAllocationOpInterfaceExternalModels(DialectRegistry &registry)
Include the generated interface declarations.