Skip to content

Commit c515c78

Browse files
authored
[mlir][Bufferization] castOrReallocMemRefValue: Use BufferizationOptions (#89175)
This allows to configure both the op used for allocation and copy of memrefs. It also changes the default behavior because the default allocation in `BufferizationOptions` creates `memref.alloc` with `alignment = 64` where we used to create `memref.alloca` without any alignment before. Fixes ``` // TODO: Use alloc/memcpy callback from BufferizationOptions if called via // BufferizableOpInterface impl of ToMemrefOp. ```
1 parent 4c3514f commit c515c78

File tree

5 files changed

+29
-20
lines changed

5 files changed

+29
-20
lines changed

mlir/include/mlir/Dialect/Bufferization/IR/Bufferization.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,14 @@ void populateDynamicDimSizes(OpBuilder &b, Location loc, Value shapedValue,
5353
/// This function returns `failure()` in case of unsupported casts. E.g., casts
5454
/// with differing element types or memory spaces.
5555
FailureOr<Value> castOrReallocMemRefValue(OpBuilder &b, Value value,
56-
MemRefType type);
56+
MemRefType type,
57+
const BufferizationOptions &options);
5758

5859
/// Try to fold to_memref(to_tensor(x)). If x's type and the result type of the
5960
/// to_memref op are different, a memref.cast is needed.
6061
LogicalResult foldToMemrefToTensorPair(RewriterBase &rewriter,
61-
ToMemrefOp toMemref);
62+
ToMemrefOp toMemref,
63+
const BufferizationOptions &options);
6264

6365
/// Add the canonicalization patterns for bufferization.dealloc to the given
6466
/// pattern set to make them available to other passes (such as

mlir/lib/Dialect/Bufferization/IR/BufferizationOps.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ using namespace mlir::bufferization;
2323
// Helper functions
2424
//===----------------------------------------------------------------------===//
2525

26-
FailureOr<Value>
27-
mlir::bufferization::castOrReallocMemRefValue(OpBuilder &b, Value value,
28-
MemRefType destType) {
26+
FailureOr<Value> mlir::bufferization::castOrReallocMemRefValue(
27+
OpBuilder &b, Value value, MemRefType destType,
28+
const BufferizationOptions &options) {
2929
auto srcType = llvm::cast<MemRefType>(value.getType());
3030

3131
// Element type, rank and memory space must match.
@@ -73,18 +73,21 @@ mlir::bufferization::castOrReallocMemRefValue(OpBuilder &b, Value value,
7373
Value size = b.create<memref::DimOp>(loc, value, i);
7474
dynamicOperands.push_back(size);
7575
}
76-
// TODO: Use alloc/memcpy callback from BufferizationOptions if called via
77-
// BufferizableOpInterface impl of ToMemrefOp.
78-
Value copy = b.create<memref::AllocOp>(loc, destType, dynamicOperands);
79-
b.create<memref::CopyOp>(loc, value, copy);
76+
77+
FailureOr<Value> copy =
78+
options.createAlloc(b, loc, destType, dynamicOperands);
79+
if (failed(copy))
80+
return failure();
81+
if (failed(options.createMemCpy(b, loc, value, *copy)))
82+
return failure();
8083
return copy;
8184
}
8285

8386
/// Try to fold to_memref(to_tensor(x)). If x's type and the result type of the
8487
/// to_memref op are different, a memref.cast is needed.
85-
LogicalResult
86-
mlir::bufferization::foldToMemrefToTensorPair(RewriterBase &rewriter,
87-
ToMemrefOp toMemref) {
88+
LogicalResult mlir::bufferization::foldToMemrefToTensorPair(
89+
RewriterBase &rewriter, ToMemrefOp toMemref,
90+
const BufferizationOptions &options) {
8891
auto memrefToTensor = toMemref.getTensor().getDefiningOp<ToTensorOp>();
8992
if (!memrefToTensor)
9093
return failure();
@@ -105,7 +108,7 @@ mlir::bufferization::foldToMemrefToTensorPair(RewriterBase &rewriter,
105108
// Ranked memref -> Ranked memref cast.
106109
if (rankedSrcType && rankedDestType) {
107110
FailureOr<Value> replacement = castOrReallocMemRefValue(
108-
rewriter, memrefToTensor.getMemref(), rankedDestType);
111+
rewriter, memrefToTensor.getMemref(), rankedDestType, options);
109112
if (failed(replacement))
110113
return failure();
111114

@@ -795,7 +798,9 @@ struct ToMemrefToTensorFolding : public OpRewritePattern<ToMemrefOp> {
795798

796799
LogicalResult matchAndRewrite(ToMemrefOp toMemref,
797800
PatternRewriter &rewriter) const final {
798-
return foldToMemrefToTensorPair(rewriter, toMemref);
801+
BufferizationOptions options;
802+
options.bufferAlignment = 0;
803+
return foldToMemrefToTensorPair(rewriter, toMemref, options);
799804
}
800805
};
801806

@@ -843,7 +848,7 @@ void ToMemrefOp::getCanonicalizationPatterns(RewritePatternSet &results,
843848
LogicalResult ToMemrefOp::bufferize(RewriterBase &rewriter,
844849
const BufferizationOptions &options) {
845850
// Fold to_memref(to_tensor(x)) to x. Insert a cast if necessary.
846-
(void)foldToMemrefToTensorPair(rewriter, *this);
851+
(void)foldToMemrefToTensorPair(rewriter, *this, options);
847852
// Note: The return value of `bufferize` indicates whether there was an error
848853
// or not. (And not whether the pattern matched or not.)
849854
return success();

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ BufferizeTypeConverter::BufferizeTypeConverter() {
7474
auto rankedDestType = dyn_cast<MemRefType>(type);
7575
if (!rankedDestType)
7676
return nullptr;
77+
BufferizationOptions options;
78+
options.bufferAlignment = 0;
7779
FailureOr<Value> replacement =
78-
castOrReallocMemRefValue(builder, inputs[0], rankedDestType);
80+
castOrReallocMemRefValue(builder, inputs[0], rankedDestType, options);
7981
if (failed(replacement))
8082
return nullptr;
8183
return *replacement;
@@ -512,8 +514,8 @@ LogicalResult bufferization::bufferizeOp(Operation *op,
512514
// Fold all to_memref(to_tensor(x)) pairs.
513515
for (Operation *op : toMemrefOps) {
514516
rewriter.setInsertionPoint(op);
515-
(void)bufferization::foldToMemrefToTensorPair(rewriter,
516-
cast<ToMemrefOp>(op));
517+
(void)bufferization::foldToMemrefToTensorPair(
518+
rewriter, cast<ToMemrefOp>(op), options);
517519
}
518520

519521
// Remove all dead to_tensor ops.

mlir/test/Dialect/Bufferization/Transforms/one-shot-module-bufferize-out-params.mlir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func.func @main(%t: tensor<5xf32>) -> (f32, f32) {
8484
// Note: This alloc is not needed, but it is inserted before the returned buffer
8585
// is promoted to an out param to reconcile mismatching layout maps on return
8686
// value and function signature.
87-
// CHECK-NO-LAYOUT: %[[alloc2:.*]] = memref.alloc() : memref<2x5xf32>
87+
// CHECK-NO-LAYOUT: %[[alloc2:.*]] = memref.alloc() {{.*}} : memref<2x5xf32>
8888
// CHECK-NO-LAYOUT: memref.copy %[[subview]], %[[alloc2]]
8989
// CHECK-NO-LAYOUT: memref.copy %[[alloc2]], %[[r]]
9090

mlir/test/Dialect/Bufferization/Transforms/one-shot-module-bufferize.mlir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func.func private @external_func_with_return_val(tensor<4xi32>) -> f32
5252
// CHECK-NO-LAYOUT-MAP-LABEL: func @return_extract_slice(%{{.*}}) -> memref<2x?xf32>
5353
// CHECK-NO-LAYOUT-MAP: %[[alloc:.*]] = memref.alloc() {{.*}} : memref<20x10xf32>
5454
// CHECK-NO-LAYOUT-MAP: %[[subview:.*]] = memref.subview {{.*}} : memref<20x10xf32> to memref<2x?xf32, strided<[10, 1], offset: ?>>
55-
// CHECK-NO-LAYOUT-MAP: %[[alloc_no_layout:.*]] = memref.alloc(%{{.*}}) : memref<2x?xf32>
55+
// CHECK-NO-LAYOUT-MAP: %[[alloc_no_layout:.*]] = memref.alloc(%{{.*}}) {{.*}} : memref<2x?xf32>
5656
// CHECK-NO-LAYOUT-MAP: memref.copy %[[subview]], %[[alloc_no_layout]]
5757
// TODO: %alloc should be deallocated here, but we currently do not dealloc
5858
// buffers that are inserted due to to_tensor/to_memref canonicalization (when

0 commit comments

Comments
 (0)