[llvm-branch-commits] [mlir] [mlir][bufferization] Skip non-identity layouts in static memory planning (PR #237125)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Fri Oct 30 02:04:51 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir-bufferization
Author: Zhijie Wang (aeft)
<details>
<summary>Changes</summary>
Skip allocations with non-identity layouts to avoid invalid `memref.view` operations, preserving affected alloc/dealloc groups.
reproducer: https://godbolt.org/z/cs5MEG18W
Assisted-by: Codex
---
Full diff: https://github.com/llvm/llvm-project/pull/237125.diff
3 Files Affected:
- (modified) mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.td (+3)
- (modified) mlir/lib/Dialect/Bufferization/Transforms/StaticMemoryPlannerAnalysis.cpp (+12-4)
- (modified) mlir/test/Dialect/Bufferization/Transforms/static-memory-planner-analysis.mlir (+52)
``````````diff
diff --git a/mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.td b/mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.td
index 4fc38310adfaf..34f50e31dc2b4 100644
--- a/mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.td
+++ b/mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.td
@@ -194,6 +194,7 @@ def StaticMemoryPlannerAnalysisPass
envelope:
- Static memref shape.
- Integer or floating-point element type.
+ - Identity layout.
- The allocation lives directly in the function's entry block
(allocations nested in a region are skipped for now).
- Every `memref.dealloc` that may free the buffer is anchored in the entry
@@ -270,6 +271,8 @@ def StaticMemoryPlannerAnalysisPass
"Number of allocations skipped: dynamic shape">,
Statistic<"numSkipUnsupportedType", "num-skipped-unsupported-type",
"Number of allocations skipped: unsupported element type">,
+ Statistic<"numSkipUnsupportedLayout", "num-skipped-unsupported-layout",
+ "Number of allocations skipped: unsupported layout">,
Statistic<
"numSkipNested", "num-skipped-nested",
"Number of allocations skipped: nested in loop or conditional">,
diff --git a/mlir/lib/Dialect/Bufferization/Transforms/StaticMemoryPlannerAnalysis.cpp b/mlir/lib/Dialect/Bufferization/Transforms/StaticMemoryPlannerAnalysis.cpp
index 9f3301a6e6f29..83d59ae2be439 100644
--- a/mlir/lib/Dialect/Bufferization/Transforms/StaticMemoryPlannerAnalysis.cpp
+++ b/mlir/lib/Dialect/Bufferization/Transforms/StaticMemoryPlannerAnalysis.cpp
@@ -184,6 +184,7 @@ static void filterSharedDeallocCandidates(
/// ops are handled uniformly. An allocation is eligible when:
/// - it has a static shape (dynamic shapes are silently skipped), and
/// - its element type is an integer or float, and
+/// - its layout is identity, and
/// - it lives directly in the function's entry block (allocs nested in a
/// region are skipped for now), and
/// - every dealloc that may free it is anchored in that same entry block --
@@ -199,8 +200,8 @@ static void filterSharedDeallocCandidates(
static LogicalResult collectCandidates(
FunctionOpInterface funcOp, const BufferViewFlowAnalysis &analysis,
llvm::Statistic &numSkipDynamic, llvm::Statistic &numSkipUnsupportedType,
- llvm::Statistic &numSkipNested, llvm::Statistic &numSkipSharedDealloc,
- llvm::Statistic &numEligible,
+ llvm::Statistic &numSkipUnsupportedLayout, llvm::Statistic &numSkipNested,
+ llvm::Statistic &numSkipSharedDealloc, llvm::Statistic &numEligible,
SmallVector<AllocationCandidate> &candidates) {
// All candidates are planned relative to the function's entry block.
if (funcOp.getFunctionBody().empty())
@@ -228,6 +229,12 @@ static LogicalResult collectCandidates(
return WalkResult::advance();
}
+ // memref.view requires an identity layout for its result.
+ if (!memrefType.getLayout().isIdentity()) {
+ ++numSkipUnsupportedLayout;
+ return WalkResult::advance();
+ }
+
SmallVector<memref::DeallocOp> deallocs;
collectDeallocs(allocOp.getResult(), analysis, deallocs);
@@ -374,8 +381,9 @@ void StaticMemoryPlannerAnalysisPass::runOnOperation() {
BufferViewFlowAnalysis analysis(funcOp);
SmallVector<AllocationCandidate> candidates;
if (failed(collectCandidates(funcOp, analysis, numSkipDynamic,
- numSkipUnsupportedType, numSkipNested,
- numSkipSharedDealloc, numEligible, candidates)))
+ numSkipUnsupportedType, numSkipUnsupportedLayout,
+ numSkipNested, numSkipSharedDealloc, numEligible,
+ candidates)))
return signalPassFailure();
if (candidates.empty())
diff --git a/mlir/test/Dialect/Bufferization/Transforms/static-memory-planner-analysis.mlir b/mlir/test/Dialect/Bufferization/Transforms/static-memory-planner-analysis.mlir
index 1b151ced47a8f..ee6c2f645f5bc 100644
--- a/mlir/test/Dialect/Bufferization/Transforms/static-memory-planner-analysis.mlir
+++ b/mlir/test/Dialect/Bufferization/Transforms/static-memory-planner-analysis.mlir
@@ -726,3 +726,55 @@ func.func @independent_allocation(%n: index, %cond: i1) {
memref.dealloc %independent : memref<8xi32>
return
}
+
+// -----
+
+// Non-identity strides and offsets are skipped. An explicit identity layout
+// remains eligible.
+// CHECK-LABEL: func.func @unsupported_layouts(
+// CHECK-NEXT: %[[A:.*]] = memref.alloc() : memref<4xf32, strided<[2]>>
+// CHECK-NEXT: %[[B:.*]] = memref.alloc()[%{{.*}}] : memref<4xf32, strided<[?]>>
+// CHECK-NEXT: %[[C:.*]] = memref.alloc() : memref<4xf32, strided<[1], offset: 1>>
+// CHECK-NEXT: memref.dealloc %[[A]] : memref<4xf32, strided<[2]>>
+// CHECK-NEXT: memref.dealloc %[[B]] : memref<4xf32, strided<[?]>>
+// CHECK-NEXT: memref.dealloc %[[C]] : memref<4xf32, strided<[1], offset: 1>>
+// CHECK-NEXT: %[[ARENA:.*]] = memref.alloc() alignment = 1 : memref<16xi8>
+// CHECK-NEXT: %[[C0:.*]] = arith.constant 0 : index
+// CHECK-NEXT: %{{.*}} = memref.view %[[ARENA]][%[[C0]]][] : memref<16xi8> to memref<4xf32, strided<[1]>>
+// CHECK-NEXT: return
+func.func @unsupported_layouts(%stride: index) {
+ %a = memref.alloc() : memref<4xf32, strided<[2]>>
+ %b = memref.alloc()[%stride] : memref<4xf32, strided<[?]>>
+ %c = memref.alloc() : memref<4xf32, strided<[1], offset: 1>>
+ memref.dealloc %a : memref<4xf32, strided<[2]>>
+ memref.dealloc %b : memref<4xf32, strided<[?]>>
+ memref.dealloc %c : memref<4xf32, strided<[1], offset: 1>>
+ %supported = memref.alloc() : memref<4xf32, strided<[1]>>
+ memref.dealloc %supported : memref<4xf32, strided<[1]>>
+ return
+}
+
+// -----
+
+// Preserve shared deallocs when one allocation has an unsupported layout.
+// CHECK-LABEL: func.func @shared_unsupported_layout(
+// CHECK-NEXT: %[[A:.*]] = memref.alloc() : memref<4xf32>
+// CHECK-NEXT: %[[B:.*]] = memref.alloc() : memref<4xf32, strided<[2]>>
+// CHECK-NEXT: %[[AC:.*]] = memref.cast %[[A]] : memref<4xf32> to memref<4xf32, strided<[?], offset: ?>>
+// CHECK-NEXT: %[[BC:.*]] = memref.cast %[[B]] : memref<4xf32, strided<[2]>> to memref<4xf32, strided<[?], offset: ?>>
+// CHECK-NEXT: %[[S0:.*]] = arith.select %{{.*}}, %[[AC]], %[[BC]] : memref<4xf32, strided<[?], offset: ?>>
+// CHECK-NEXT: %[[S1:.*]] = arith.select %{{.*}}, %[[BC]], %[[AC]] : memref<4xf32, strided<[?], offset: ?>>
+// CHECK-NEXT: memref.dealloc %[[S0]] : memref<4xf32, strided<[?], offset: ?>>
+// CHECK-NEXT: memref.dealloc %[[S1]] : memref<4xf32, strided<[?], offset: ?>>
+// CHECK-NEXT: return
+func.func @shared_unsupported_layout(%cond: i1) {
+ %a = memref.alloc() : memref<4xf32>
+ %b = memref.alloc() : memref<4xf32, strided<[2]>>
+ %ac = memref.cast %a : memref<4xf32> to memref<4xf32, strided<[?], offset: ?>>
+ %bc = memref.cast %b : memref<4xf32, strided<[2]>> to memref<4xf32, strided<[?], offset: ?>>
+ %s0 = arith.select %cond, %ac, %bc : memref<4xf32, strided<[?], offset: ?>>
+ %s1 = arith.select %cond, %bc, %ac : memref<4xf32, strided<[?], offset: ?>>
+ memref.dealloc %s0 : memref<4xf32, strided<[?], offset: ?>>
+ memref.dealloc %s1 : memref<4xf32, strided<[?], offset: ?>>
+ return
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/237125
More information about the llvm-branch-commits
mailing list