MLIR 24.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
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, alloc.getLoc(), alloc)
26 .getOperation();
27 }
28 static std::optional<Value> buildClone(OpBuilder &builder, Value alloc) {
29 return bufferization::CloneOp::create(builder, alloc.getLoc(), 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>(alloc.getDefiningOp());
38 memref::AllocaOp allocaOp = memref::AllocaOp::create(
39 builder, allocOp.getLoc(), allocOp.getType(), allocOp.getDynamicSizes(),
40 allocOp.getSymbolOperands(), 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 DefaultAllocationInterface, memref::ReallocOp> {
55 static std::optional<Operation *> buildDealloc(OpBuilder &builder,
56 Value realloc) {
57 return memref::DeallocOp::create(builder, realloc.getLoc(), realloc)
58 .getOperation();
59 }
60};
61} // namespace
62
64 DialectRegistry &registry) {
65 registry.addExtension(+[](MLIRContext *ctx, memref::MemRefDialect *dialect) {
66 memref::AllocOp::attachInterface<DefaultAllocationInterface>(*ctx);
67 memref::AllocaOp::attachInterface<
68 DefaultAutomaticAllocationHoistingInterface>(*ctx);
69 memref::ReallocOp::attachInterface<DefaultReallocationInterface>(*ctx);
70 });
71}
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:63
Location getLoc() const
Return the location of this value.
Definition Value.cpp:24
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
Definition Value.cpp:18
void registerAllocationOpInterfaceExternalModels(DialectRegistry &registry)
Include the generated interface declarations.