MLIR 22.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 Operation *definingOp = alloc.getDefiningOp();
38 return memref::AllocaOp::create(
39 builder, definingOp->getLoc(),
40 cast<MemRefType>(definingOp->getResultTypes()[0]),
41 definingOp->getOperands(), definingOp->getAttrs());
42 }
43};
44
45struct DefaultAutomaticAllocationHoistingInterface
46 : public bufferization::AllocationOpInterface::ExternalModel<
47 DefaultAutomaticAllocationHoistingInterface, memref::AllocaOp> {
48 static ::mlir::HoistingKind getHoistingKind() { return HoistingKind::Loop; }
49};
50
51struct DefaultReallocationInterface
52 : public bufferization::AllocationOpInterface::ExternalModel<
53 DefaultAllocationInterface, memref::ReallocOp> {
54 static std::optional<Operation *> buildDealloc(OpBuilder &builder,
55 Value realloc) {
56 return memref::DeallocOp::create(builder, realloc.getLoc(), realloc)
57 .getOperation();
58 }
59};
60} // namespace
61
63 DialectRegistry &registry) {
64 registry.addExtension(+[](MLIRContext *ctx, memref::MemRefDialect *dialect) {
65 memref::AllocOp::attachInterface<DefaultAllocationInterface>(*ctx);
66 memref::AllocaOp::attachInterface<
67 DefaultAutomaticAllocationHoistingInterface>(*ctx);
68 memref::ReallocOp::attachInterface<DefaultReallocationInterface>(*ctx);
69 });
70}
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
ArrayRef< NamedAttribute > getAttrs()
Return all of the attributes on this operation.
Definition Operation.h:512
Location getLoc()
The source location the operation was defined or derived from.
Definition Operation.h:223
result_type_range getResultTypes()
Definition Operation.h:428
operand_range getOperands()
Returns an iterator on the underlying Value's.
Definition Operation.h:378
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.