Skip to content

Commit b4f24be

Browse files
[mlir][bufferization] Simplify helper potentiallyAliasesMemref (#78690)
This commit simplifies a helper function in the ownership-based buffer deallocation pass. Fixes a potential double-free (depending on the scheduling of patterns).
1 parent 80ccc72 commit b4f24be

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,12 @@ static bool distinctAllocAndBlockArgument(Value v1, Value v2) {
7777
return areDistinct(v1Base, v2Base) || areDistinct(v2Base, v1Base);
7878
}
7979

80-
/// Checks if `memref` may or must alias a MemRef in `otherList`. It is often a
81-
/// requirement of optimization patterns that there cannot be any aliasing
82-
/// memref in order to perform the desired simplification. The `allowSelfAlias`
83-
/// argument indicates whether `memref` may be present in `otherList` which
84-
/// makes this helper function applicable to situations where we already know
85-
/// that `memref` is in the list but also when we don't want it in the list.
80+
/// Checks if `memref` may potentially alias a MemRef in `otherList`. It is
81+
/// often a requirement of optimization patterns that there cannot be any
82+
/// aliasing memref in order to perform the desired simplification.
8683
static bool potentiallyAliasesMemref(AliasAnalysis &analysis,
87-
ValueRange otherList, Value memref,
88-
bool allowSelfAlias) {
84+
ValueRange otherList, Value memref) {
8985
for (auto other : otherList) {
90-
if (allowSelfAlias && other == memref)
91-
continue;
9286
if (distinctAllocAndBlockArgument(other, memref))
9387
continue;
9488
if (!analysis.alias(other, memref).isNo())
@@ -243,7 +237,7 @@ struct RemoveRetainedMemrefsGuaranteedToNotAlias
243237

244238
for (auto retainedMemref : deallocOp.getRetained()) {
245239
if (potentiallyAliasesMemref(aliasAnalysis, deallocOp.getMemrefs(),
246-
retainedMemref, false)) {
240+
retainedMemref)) {
247241
newRetainedMemrefs.push_back(retainedMemref);
248242
replacements.push_back({});
249243
continue;
@@ -314,11 +308,13 @@ struct SplitDeallocWhenNotAliasingAnyOther
314308

315309
SmallVector<Value> remainingMemrefs, remainingConditions;
316310
SmallVector<SmallVector<Value>> updatedConditions;
317-
for (auto [memref, cond] :
318-
llvm::zip(deallocOp.getMemrefs(), deallocOp.getConditions())) {
311+
for (int64_t i = 0, e = deallocOp.getMemrefs().size(); i < e; ++i) {
312+
Value memref = deallocOp.getMemrefs()[i];
313+
Value cond = deallocOp.getConditions()[i];
314+
SmallVector<Value> otherMemrefs(deallocOp.getMemrefs());
315+
otherMemrefs.erase(otherMemrefs.begin() + i);
319316
// Check if `memref` can split off into a separate bufferization.dealloc.
320-
if (potentiallyAliasesMemref(aliasAnalysis, deallocOp.getMemrefs(),
321-
memref, true)) {
317+
if (potentiallyAliasesMemref(aliasAnalysis, otherMemrefs, memref)) {
322318
// `memref` alias with other memrefs, do not split off.
323319
remainingMemrefs.push_back(memref);
324320
remainingConditions.push_back(cond);

mlir/test/Dialect/Bufferization/Transforms/buffer-deallocation-simplification.mlir

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,14 @@ func.func @alloc_and_bbarg(%arg0: memref<5xf32>, %arg1: index, %arg2: index, %ar
160160
// CHECK: bufferization.dealloc (%[[view]] : memref<f32>)
161161
// CHECK-NOT: retain
162162
// CHECK: scf.yield %[[alloc]], %[[true]]
163+
164+
// -----
165+
166+
func.func @duplicate_memref(%arg0: memref<5xf32>, %arg1: memref<6xf32>, %c: i1) -> i1 {
167+
%0 = bufferization.dealloc (%arg0, %arg0 : memref<5xf32>, memref<5xf32>) if (%c, %c) retain (%arg1 : memref<6xf32>)
168+
return %0 : i1
169+
}
170+
171+
// CHECK-LABEL: func @duplicate_memref(
172+
// CHECK: %[[r:.*]] = bufferization.dealloc (%{{.*}} : memref<5xf32>) if (%{{.*}}) retain (%{{.*}} : memref<6xf32>)
173+
// CHECK: return %[[r]]

0 commit comments

Comments
 (0)