Skip to content

Commit 7c6419b

Browse files
authored
[NFC][mlir][bufferization] Move AllocationOpInterface implementations (#65578)
The new Buffer Deallocation pass introduced in D158421 will not need the AllocationOpInterface anymore, thus it is better to move those default implementations to a place where they will still be used.
1 parent 98062d8 commit 7c6419b

File tree

2 files changed

+56
-50
lines changed

2 files changed

+56
-50
lines changed

mlir/lib/Dialect/Bufferization/Transforms/BufferDeallocation.cpp

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -625,46 +625,6 @@ class BufferDeallocation : public BufferPlacementTransformationBase {
625625
// BufferDeallocationPass
626626
//===----------------------------------------------------------------------===//
627627

628-
struct DefaultAllocationInterface
629-
: public bufferization::AllocationOpInterface::ExternalModel<
630-
DefaultAllocationInterface, memref::AllocOp> {
631-
static std::optional<Operation *> buildDealloc(OpBuilder &builder,
632-
Value alloc) {
633-
return builder.create<memref::DeallocOp>(alloc.getLoc(), alloc)
634-
.getOperation();
635-
}
636-
static std::optional<Value> buildClone(OpBuilder &builder, Value alloc) {
637-
return builder.create<bufferization::CloneOp>(alloc.getLoc(), alloc)
638-
.getResult();
639-
}
640-
static ::mlir::HoistingKind getHoistingKind() {
641-
return HoistingKind::Loop | HoistingKind::Block;
642-
}
643-
static ::std::optional<::mlir::Operation *>
644-
buildPromotedAlloc(OpBuilder &builder, Value alloc) {
645-
Operation *definingOp = alloc.getDefiningOp();
646-
return builder.create<memref::AllocaOp>(
647-
definingOp->getLoc(), cast<MemRefType>(definingOp->getResultTypes()[0]),
648-
definingOp->getOperands(), definingOp->getAttrs());
649-
}
650-
};
651-
652-
struct DefaultAutomaticAllocationHoistingInterface
653-
: public bufferization::AllocationOpInterface::ExternalModel<
654-
DefaultAutomaticAllocationHoistingInterface, memref::AllocaOp> {
655-
static ::mlir::HoistingKind getHoistingKind() { return HoistingKind::Loop; }
656-
};
657-
658-
struct DefaultReallocationInterface
659-
: public bufferization::AllocationOpInterface::ExternalModel<
660-
DefaultAllocationInterface, memref::ReallocOp> {
661-
static std::optional<Operation *> buildDealloc(OpBuilder &builder,
662-
Value realloc) {
663-
return builder.create<memref::DeallocOp>(realloc.getLoc(), realloc)
664-
.getOperation();
665-
}
666-
};
667-
668628
/// The actual buffer deallocation pass that inserts and moves dealloc nodes
669629
/// into the right positions. Furthermore, it inserts additional clones if
670630
/// necessary. It uses the algorithm described at the top of the file.
@@ -725,16 +685,6 @@ LogicalResult bufferization::deallocateBuffers(Operation *op) {
725685
return success();
726686
}
727687

728-
void bufferization::registerAllocationOpInterfaceExternalModels(
729-
DialectRegistry &registry) {
730-
registry.addExtension(+[](MLIRContext *ctx, memref::MemRefDialect *dialect) {
731-
memref::AllocOp::attachInterface<DefaultAllocationInterface>(*ctx);
732-
memref::AllocaOp::attachInterface<
733-
DefaultAutomaticAllocationHoistingInterface>(*ctx);
734-
memref::ReallocOp::attachInterface<DefaultReallocationInterface>(*ctx);
735-
});
736-
}
737-
738688
//===----------------------------------------------------------------------===//
739689
// BufferDeallocationPass construction
740690
//===----------------------------------------------------------------------===//

mlir/lib/Dialect/Bufferization/Transforms/Bufferize.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,3 +682,59 @@ BufferizationOptions bufferization::getPartialBufferizationOptions() {
682682
options.opFilter.allowDialect<BufferizationDialect>();
683683
return options;
684684
}
685+
686+
//===----------------------------------------------------------------------===//
687+
// Default AllocationOpInterface implementation and registration
688+
//===----------------------------------------------------------------------===//
689+
690+
namespace {
691+
struct DefaultAllocationInterface
692+
: public bufferization::AllocationOpInterface::ExternalModel<
693+
DefaultAllocationInterface, memref::AllocOp> {
694+
static std::optional<Operation *> buildDealloc(OpBuilder &builder,
695+
Value alloc) {
696+
return builder.create<memref::DeallocOp>(alloc.getLoc(), alloc)
697+
.getOperation();
698+
}
699+
static std::optional<Value> buildClone(OpBuilder &builder, Value alloc) {
700+
return builder.create<bufferization::CloneOp>(alloc.getLoc(), alloc)
701+
.getResult();
702+
}
703+
static ::mlir::HoistingKind getHoistingKind() {
704+
return HoistingKind::Loop | HoistingKind::Block;
705+
}
706+
static ::std::optional<::mlir::Operation *>
707+
buildPromotedAlloc(OpBuilder &builder, Value alloc) {
708+
Operation *definingOp = alloc.getDefiningOp();
709+
return builder.create<memref::AllocaOp>(
710+
definingOp->getLoc(), cast<MemRefType>(definingOp->getResultTypes()[0]),
711+
definingOp->getOperands(), definingOp->getAttrs());
712+
}
713+
};
714+
715+
struct DefaultAutomaticAllocationHoistingInterface
716+
: public bufferization::AllocationOpInterface::ExternalModel<
717+
DefaultAutomaticAllocationHoistingInterface, memref::AllocaOp> {
718+
static ::mlir::HoistingKind getHoistingKind() { return HoistingKind::Loop; }
719+
};
720+
721+
struct DefaultReallocationInterface
722+
: public bufferization::AllocationOpInterface::ExternalModel<
723+
DefaultAllocationInterface, memref::ReallocOp> {
724+
static std::optional<Operation *> buildDealloc(OpBuilder &builder,
725+
Value realloc) {
726+
return builder.create<memref::DeallocOp>(realloc.getLoc(), realloc)
727+
.getOperation();
728+
}
729+
};
730+
} // namespace
731+
732+
void bufferization::registerAllocationOpInterfaceExternalModels(
733+
DialectRegistry &registry) {
734+
registry.addExtension(+[](MLIRContext *ctx, memref::MemRefDialect *dialect) {
735+
memref::AllocOp::attachInterface<DefaultAllocationInterface>(*ctx);
736+
memref::AllocaOp::attachInterface<
737+
DefaultAutomaticAllocationHoistingInterface>(*ctx);
738+
memref::ReallocOp::attachInterface<DefaultReallocationInterface>(*ctx);
739+
});
740+
}

0 commit comments

Comments
 (0)