1*65341b09SMartin Erhart //===- AllocationOpInterfaceImpl.cpp - Impl. of AllocationOpInterface -----===//
2*65341b09SMartin Erhart //
3*65341b09SMartin Erhart // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*65341b09SMartin Erhart // See https://llvm.org/LICENSE.txt for license information.
5*65341b09SMartin Erhart // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*65341b09SMartin Erhart //
7*65341b09SMartin Erhart //===----------------------------------------------------------------------===//
8*65341b09SMartin Erhart
9*65341b09SMartin Erhart #include "mlir/Dialect/MemRef/Transforms/AllocationOpInterfaceImpl.h"
10*65341b09SMartin Erhart
11*65341b09SMartin Erhart #include "mlir/Dialect/Bufferization/IR/AllocationOpInterface.h"
12*65341b09SMartin Erhart #include "mlir/Dialect/Bufferization/IR/Bufferization.h"
13*65341b09SMartin Erhart #include "mlir/Dialect/MemRef/IR/MemRef.h"
14*65341b09SMartin Erhart #include "mlir/IR/Dialect.h"
15*65341b09SMartin Erhart #include "mlir/IR/Operation.h"
16*65341b09SMartin Erhart
17*65341b09SMartin Erhart using namespace mlir;
18*65341b09SMartin Erhart
19*65341b09SMartin Erhart namespace {
20*65341b09SMartin Erhart struct DefaultAllocationInterface
21*65341b09SMartin Erhart : public bufferization::AllocationOpInterface::ExternalModel<
22*65341b09SMartin Erhart DefaultAllocationInterface, memref::AllocOp> {
buildDealloc__anoned7c1d120111::DefaultAllocationInterface23*65341b09SMartin Erhart static std::optional<Operation *> buildDealloc(OpBuilder &builder,
24*65341b09SMartin Erhart Value alloc) {
25*65341b09SMartin Erhart return builder.create<memref::DeallocOp>(alloc.getLoc(), alloc)
26*65341b09SMartin Erhart .getOperation();
27*65341b09SMartin Erhart }
buildClone__anoned7c1d120111::DefaultAllocationInterface28*65341b09SMartin Erhart static std::optional<Value> buildClone(OpBuilder &builder, Value alloc) {
29*65341b09SMartin Erhart return builder.create<bufferization::CloneOp>(alloc.getLoc(), alloc)
30*65341b09SMartin Erhart .getResult();
31*65341b09SMartin Erhart }
getHoistingKind__anoned7c1d120111::DefaultAllocationInterface32*65341b09SMartin Erhart static ::mlir::HoistingKind getHoistingKind() {
33*65341b09SMartin Erhart return HoistingKind::Loop | HoistingKind::Block;
34*65341b09SMartin Erhart }
35*65341b09SMartin Erhart static ::std::optional<::mlir::Operation *>
buildPromotedAlloc__anoned7c1d120111::DefaultAllocationInterface36*65341b09SMartin Erhart buildPromotedAlloc(OpBuilder &builder, Value alloc) {
37*65341b09SMartin Erhart Operation *definingOp = alloc.getDefiningOp();
38*65341b09SMartin Erhart return builder.create<memref::AllocaOp>(
39*65341b09SMartin Erhart definingOp->getLoc(), cast<MemRefType>(definingOp->getResultTypes()[0]),
40*65341b09SMartin Erhart definingOp->getOperands(), definingOp->getAttrs());
41*65341b09SMartin Erhart }
42*65341b09SMartin Erhart };
43*65341b09SMartin Erhart
44*65341b09SMartin Erhart struct DefaultAutomaticAllocationHoistingInterface
45*65341b09SMartin Erhart : public bufferization::AllocationOpInterface::ExternalModel<
46*65341b09SMartin Erhart DefaultAutomaticAllocationHoistingInterface, memref::AllocaOp> {
getHoistingKind__anoned7c1d120111::DefaultAutomaticAllocationHoistingInterface47*65341b09SMartin Erhart static ::mlir::HoistingKind getHoistingKind() { return HoistingKind::Loop; }
48*65341b09SMartin Erhart };
49*65341b09SMartin Erhart
50*65341b09SMartin Erhart struct DefaultReallocationInterface
51*65341b09SMartin Erhart : public bufferization::AllocationOpInterface::ExternalModel<
52*65341b09SMartin Erhart DefaultAllocationInterface, memref::ReallocOp> {
buildDealloc__anoned7c1d120111::DefaultReallocationInterface53*65341b09SMartin Erhart static std::optional<Operation *> buildDealloc(OpBuilder &builder,
54*65341b09SMartin Erhart Value realloc) {
55*65341b09SMartin Erhart return builder.create<memref::DeallocOp>(realloc.getLoc(), realloc)
56*65341b09SMartin Erhart .getOperation();
57*65341b09SMartin Erhart }
58*65341b09SMartin Erhart };
59*65341b09SMartin Erhart } // namespace
60*65341b09SMartin Erhart
registerAllocationOpInterfaceExternalModels(DialectRegistry & registry)61*65341b09SMartin Erhart void mlir::memref::registerAllocationOpInterfaceExternalModels(
62*65341b09SMartin Erhart DialectRegistry ®istry) {
63*65341b09SMartin Erhart registry.addExtension(+[](MLIRContext *ctx, memref::MemRefDialect *dialect) {
64*65341b09SMartin Erhart memref::AllocOp::attachInterface<DefaultAllocationInterface>(*ctx);
65*65341b09SMartin Erhart memref::AllocaOp::attachInterface<
66*65341b09SMartin Erhart DefaultAutomaticAllocationHoistingInterface>(*ctx);
67*65341b09SMartin Erhart memref::ReallocOp::attachInterface<DefaultReallocationInterface>(*ctx);
68*65341b09SMartin Erhart });
69*65341b09SMartin Erhart }
70