llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-mlir-bufferization Author: Zhijie Wang (aeft) <details> <summary>Changes</summary> The planner can erase shared deallocs still needed by a skipped allocation, causing a memory leak. Check actual candidate eligibility and propagate exclusions through shared deallocs, preserving groups that cannot be rewritten together. reproducer: https://godbolt.org/z/aT54ddej1 Assisted-by: Codex --- Full diff: https://github.com/llvm/llvm-project/pull/237124.diff 3 Files Affected: - (modified) mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.td (+9) - (modified) mlir/lib/Dialect/Bufferization/Transforms/StaticMemoryPlannerAnalysis.cpp (+57-12) - (modified) mlir/test/Dialect/Bufferization/Transforms/static-memory-planner-analysis.mlir (+101) ``````````diff diff --git a/mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.td b/mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.td index ec503217a1227..4fc38310adfaf 100644 --- a/mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.td +++ b/mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.td @@ -203,6 +203,12 @@ def StaticMemoryPlannerAnalysisPass (e.g. an `scf.if` body) is accepted and bounds the lifetime conservatively. + If a dealloc may also free an ineligible allocation, all candidates that + share that dealloc are skipped to preserve the ineligible allocation's + release. This exclusion propagates through other shared deallocs until + the remaining candidates can be rewritten together safely. Independent + eligible allocations can still be planned. + Eligible allocations are packed into a single arena using a configurable planning algorithm (see the `algorithm` option). The arena is an i8 byte buffer (`memref<Nxi8>`) that can hold allocations of different element @@ -267,6 +273,9 @@ def StaticMemoryPlannerAnalysisPass Statistic< "numSkipNested", "num-skipped-nested", "Number of allocations skipped: nested in loop or conditional">, + Statistic<"numSkipSharedDealloc", "num-skipped-shared-dealloc", + "Number of allocations skipped: dealloc shared with an " + "ineligible allocation">, Statistic<"numSkipNoDealloc", "num-skipped-no-dealloc", "Number of allocations skipped: no unique same-block dealloc">, Statistic< diff --git a/mlir/lib/Dialect/Bufferization/Transforms/StaticMemoryPlannerAnalysis.cpp b/mlir/lib/Dialect/Bufferization/Transforms/StaticMemoryPlannerAnalysis.cpp index a819e37d6fe00..9f3301a6e6f29 100644 --- a/mlir/lib/Dialect/Bufferization/Transforms/StaticMemoryPlannerAnalysis.cpp +++ b/mlir/lib/Dialect/Bufferization/Transforms/StaticMemoryPlannerAnalysis.cpp @@ -19,6 +19,8 @@ #include "mlir/Dialect/MemRef/IR/MemRef.h" #include "mlir/IR/Builders.h" #include "mlir/Interfaces/FunctionInterfaces.h" +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallPtrSet.h" #include "llvm/Support/Debug.h" #include <numeric> @@ -132,6 +134,49 @@ static int64_t buildAllocInfos( return arenaAlignment; } +/// Remove candidates that share a dealloc with an ineligible allocation. +/// Exclusion propagates through shared deallocs: removing one candidate may +/// prevent erasing another dealloc, which in turn excludes more candidates. +static void filterSharedDeallocCandidates( + const BufferViewFlowAnalysis &analysis, + SmallVectorImpl<AllocationCandidate> &candidates) { + SmallPtrSet<Operation *, 16> eligible; + DenseMap<Operation *, SmallVector<AllocationCandidate *>> deallocCandidates; + for (auto &candidate : candidates) { + eligible.insert(candidate.alloc.getOperation()); + for (memref::DeallocOp dealloc : candidate.deallocs) + deallocCandidates[dealloc.getOperation()].push_back(&candidate); + } + + // Seed deallocs that may release an allocation rejected by the initial + // eligibility checks, such as a dynamic or nested allocation. + SmallVector<memref::DeallocOp> worklist; + for (const auto &entry : deallocCandidates) { + auto dealloc = cast<memref::DeallocOp>(entry.first); + if (llvm::any_of(findFreedAllocs(dealloc, analysis), + [&](memref::AllocOp alloc) { + return !eligible.contains(alloc.getOperation()); + })) + worklist.push_back(dealloc); + } + + SmallPtrSet<Operation *, 16> visited; + while (!worklist.empty()) { + memref::DeallocOp dealloc = worklist.pop_back_val(); + if (!visited.insert(dealloc.getOperation()).second) + continue; + for (AllocationCandidate *candidate : + deallocCandidates[dealloc.getOperation()]) { + if (eligible.erase(candidate->alloc.getOperation())) + worklist.append(candidate->deallocs.begin(), candidate->deallocs.end()); + } + } + + llvm::erase_if(candidates, [&](AllocationCandidate &candidate) { + return !eligible.contains(candidate.alloc.getOperation()); + }); +} + /// Collect alloc/dealloc groups eligible for arena placement. /// /// Eligibility uses the shared `BufferViewFlowAnalysis` so that buffers flowing @@ -154,7 +199,8 @@ static int64_t buildAllocInfos( static LogicalResult collectCandidates( FunctionOpInterface funcOp, const BufferViewFlowAnalysis &analysis, llvm::Statistic &numSkipDynamic, llvm::Statistic &numSkipUnsupportedType, - llvm::Statistic &numSkipNested, llvm::Statistic &numEligible, + 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()) @@ -200,17 +246,8 @@ static LogicalResult collectCandidates( walkFailed = true; return WalkResult::interrupt(); } - // Every alloc that this dealloc may free must also be an entry-block - // candidate; otherwise erasing it during the rewrite is unsafe. - for (memref::AllocOp freed : findFreedAllocs(d, analysis)) { - if (freed->getBlock() != planBlock) { - ++numSkipNested; - return WalkResult::advance(); - } - } } - ++numEligible; AllocationCandidate candidate; candidate.alloc = allocOp; candidate.deallocs = deallocs; @@ -220,7 +257,15 @@ static LogicalResult collectCandidates( return WalkResult::advance(); }); - return failure(walkFailed); + if (walkFailed) + return failure(); + + size_t initialCandidates = candidates.size(); + filterSharedDeallocCandidates(analysis, candidates); + numSkipSharedDealloc += initialCandidates - candidates.size(); + + numEligible += candidates.size(); + return success(); } /// Create or obtain the arena buffer based on the arena mode. @@ -330,7 +375,7 @@ void StaticMemoryPlannerAnalysisPass::runOnOperation() { SmallVector<AllocationCandidate> candidates; if (failed(collectCandidates(funcOp, analysis, numSkipDynamic, numSkipUnsupportedType, numSkipNested, - numEligible, candidates))) + 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 1171d28178fcc..1b151ced47a8f 100644 --- a/mlir/test/Dialect/Bufferization/Transforms/static-memory-planner-analysis.mlir +++ b/mlir/test/Dialect/Bufferization/Transforms/static-memory-planner-analysis.mlir @@ -625,3 +625,104 @@ func.func @unsupported_element_types() { memref.dealloc %supported : memref<8xi32> return } + +// ----- + +// A dynamic allocation is in the entry block but cannot enter the arena. +// Preserve both allocations and both releases when they share deallocs. +// CHECK-LABEL: func.func @shared_dynamic_shape( +// CHECK-NEXT: %[[A:.*]] = memref.alloc() : memref<4xf32> +// CHECK-NEXT: %[[B:.*]] = memref.alloc(%{{.*}}) : memref<?xf32> +// CHECK-NEXT: %[[AC:.*]] = memref.cast %[[A]] : memref<4xf32> to memref<?xf32> +// CHECK-NEXT: %[[S0:.*]] = arith.select %{{.*}}, %[[AC]], %[[B]] : memref<?xf32> +// CHECK-NEXT: %[[S1:.*]] = arith.select %{{.*}}, %[[B]], %[[AC]] : memref<?xf32> +// CHECK-NEXT: memref.dealloc %[[S0]] : memref<?xf32> +// CHECK-NEXT: memref.dealloc %[[S1]] : memref<?xf32> +// CHECK-NEXT: return +func.func @shared_dynamic_shape(%n: index, %cond: i1) { + %a = memref.alloc() : memref<4xf32> + %b = memref.alloc(%n) : memref<?xf32> + %ac = memref.cast %a : memref<4xf32> to memref<?xf32> + %s0 = arith.select %cond, %ac, %b : memref<?xf32> + %s1 = arith.select %cond, %b, %ac : memref<?xf32> + memref.dealloc %s0 : memref<?xf32> + memref.dealloc %s1 : memref<?xf32> + return +} + +// ----- + +// A shared-dealloc group can only be rewritten if all its allocations are +// eligible; otherwise preserve its allocs and deallocs. Nested C excludes B +// through dealloc(R), and excluding B must also exclude A through the +// select deallocs. +// CHECK-LABEL: func.func @shared_dealloc_skip_chain( +// CHECK-NEXT: %[[A:.*]] = memref.alloc() : memref<4xf32> +// CHECK-NEXT: %[[B:.*]] = memref.alloc() : memref<4xf32> +// CHECK-NEXT: %[[R:.*]] = scf.if %{{.*}} -> (memref<4xf32>) { +// CHECK-NEXT: %[[C:.*]] = memref.alloc() : memref<4xf32> +// CHECK-NEXT: scf.yield %[[C]] : memref<4xf32> +// CHECK-NEXT: } else { +// CHECK-NEXT: scf.yield %[[B]] : memref<4xf32> +// CHECK-NEXT: } +// CHECK-NEXT: scf.if %{{.*}} { +// CHECK-NEXT: %[[S0:.*]] = arith.select %{{.*}}, %[[A]], %[[B]] : memref<4xf32> +// CHECK-NEXT: %[[S1:.*]] = arith.select %{{.*}}, %[[B]], %[[A]] : memref<4xf32> +// CHECK-NEXT: memref.dealloc %[[S0]] : memref<4xf32> +// CHECK-NEXT: memref.dealloc %[[S1]] : memref<4xf32> +// CHECK-NEXT: memref.dealloc %[[R]] : memref<4xf32> +// CHECK-NEXT: } else { +// CHECK-NEXT: memref.dealloc %[[A]] : memref<4xf32> +// CHECK-NEXT: memref.dealloc %[[R]] : memref<4xf32> +// CHECK-NEXT: } +// CHECK-NEXT: return +func.func @shared_dealloc_skip_chain(%cond: i1, %shuffle: i1) { + %a = memref.alloc() : memref<4xf32> + %b = memref.alloc() : memref<4xf32> + %r = scf.if %cond -> memref<4xf32> { + %c = memref.alloc() : memref<4xf32> + scf.yield %c : memref<4xf32> + } else { + scf.yield %b : memref<4xf32> + } + scf.if %cond { + %s0 = arith.select %shuffle, %a, %b : memref<4xf32> + %s1 = arith.select %shuffle, %b, %a : memref<4xf32> + memref.dealloc %s0 : memref<4xf32> + memref.dealloc %s1 : memref<4xf32> + memref.dealloc %r : memref<4xf32> + } else { + memref.dealloc %a : memref<4xf32> + memref.dealloc %r : memref<4xf32> + } + return +} + +// ----- + +// Pruning a shared-dealloc group must not prevent an independent allocation +// from entering the arena. +// CHECK-LABEL: func.func @independent_allocation( +// CHECK-NEXT: %[[A:.*]] = memref.alloc() : memref<4xf32> +// CHECK-NEXT: %[[B:.*]] = memref.alloc(%{{.*}}) : memref<?xf32> +// CHECK-NEXT: %[[AC:.*]] = memref.cast %[[A]] : memref<4xf32> to memref<?xf32> +// CHECK-NEXT: %[[S0:.*]] = arith.select %{{.*}}, %[[AC]], %[[B]] : memref<?xf32> +// CHECK-NEXT: %[[S1:.*]] = arith.select %{{.*}}, %[[B]], %[[AC]] : memref<?xf32> +// CHECK-NEXT: memref.dealloc %[[S0]] : memref<?xf32> +// CHECK-NEXT: memref.dealloc %[[S1]] : memref<?xf32> +// CHECK-NEXT: %[[ARENA:.*]] = memref.alloc() alignment = 1 : memref<32xi8> +// CHECK-NEXT: %[[C0:.*]] = arith.constant 0 : index +// CHECK-NEXT: %{{.*}} = memref.view %[[ARENA]][%[[C0]]][] : memref<32xi8> to memref<8xi32> +// CHECK-NEXT: return +func.func @independent_allocation(%n: index, %cond: i1) { + %a = memref.alloc() : memref<4xf32> + %b = memref.alloc(%n) : memref<?xf32> + %ac = memref.cast %a : memref<4xf32> to memref<?xf32> + %s0 = arith.select %cond, %ac, %b : memref<?xf32> + %s1 = arith.select %cond, %b, %ac : memref<?xf32> + memref.dealloc %s0 : memref<?xf32> + memref.dealloc %s1 : memref<?xf32> + %independent = memref.alloc() : memref<8xi32> + memref.dealloc %independent : memref<8xi32> + return +} `````````` </details> https://github.com/llvm/llvm-project/pull/237124 _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
