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
9#include "mlir/Dialect/MemRef/Transforms/AllocationOpInterfaceImpl.h"
10
11#include "mlir/Dialect/Bufferization/IR/AllocationOpInterface.h"
12#include "mlir/Dialect/Bufferization/IR/Bufferization.h"
13#include "mlir/Dialect/MemRef/IR/MemRef.h"
14#include "mlir/IR/Dialect.h"
15#include "mlir/IR/Operation.h"
16
17using namespace mlir;
18
19namespace {
20struct DefaultAllocationInterface
21 : public bufferization::AllocationOpInterface::ExternalModel<
22 DefaultAllocationInterface, memref::AllocOp> {
23 static std::optional<Operation *> buildDealloc(OpBuilder &builder,
24 Value alloc) {
25 return memref::DeallocOp::create(builder, location: alloc.getLoc(), memref: alloc)
26 .getOperation();
27 }
28 static std::optional<Value> buildClone(OpBuilder &builder, Value alloc) {
29 return bufferization::CloneOp::create(builder, location: alloc.getLoc(), value: alloc)
30 .getResult();
31 }
32 static ::mlir::HoistingKind getHoistingKind() {
33 return HoistingKind::Loop | HoistingKind::Block;
34 }
35 static ::std::optional<::mlir::Operation *>
36 buildPromotedAlloc(OpBuilder &builder, Value alloc) {
37 auto allocOp = cast<memref::AllocOp>(Val: alloc.getDefiningOp());
38 memref::AllocaOp allocaOp = memref::AllocaOp::create(
39 builder, location: allocOp.getLoc(), memrefType: allocOp.getType(), dynamicSizes: allocOp.getDynamicSizes(),
40 symbolOperands: allocOp.getSymbolOperands(), alignment: allocOp.getAlignmentAttr());
41 allocaOp->setDiscardableAttrs(allocOp->getDiscardableAttrDictionary());
42 return allocaOp.getOperation();
43 }
44};
45
46struct DefaultAutomaticAllocationHoistingInterface
47 : public bufferization::AllocationOpInterface::ExternalModel<
48 DefaultAutomaticAllocationHoistingInterface, memref::AllocaOp> {
49 static ::mlir::HoistingKind getHoistingKind() { return HoistingKind::Loop; }
50};
51
52struct DefaultReallocationInterface
53 : public bufferization::AllocationOpInterface::ExternalModel<
54 DefaultReallocationInterface, memref::ReallocOp> {
55 static std::optional<Operation *> buildDealloc(OpBuilder &builder,
56 Value realloc) {
57 return memref::DeallocOp::create(builder, location: realloc.getLoc(), memref: realloc)
58 .getOperation();
59 }
60};
61} // namespace
62
63void mlir::memref::registerAllocationOpInterfaceExternalModels(
64 DialectRegistry &registry) {
65 registry.addExtension(extensionFn: +[](MLIRContext *ctx, memref::MemRefDialect *dialect) {
66 memref::AllocOp::attachInterface<DefaultAllocationInterface>(context&: *ctx);
67 memref::AllocaOp::attachInterface<
68 DefaultAutomaticAllocationHoistingInterface>(context&: *ctx);
69 memref::ReallocOp::attachInterface<DefaultReallocationInterface>(context&: *ctx);
70 });
71}
72