Skip to content

Commit 5faa5f8

Browse files
authored
[MLIR][Affine] Fix copy generation for missing memref definition depth check (#129187)
Fixes: #122210
1 parent 8c5cd77 commit 5faa5f8

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

mlir/lib/Dialect/Affine/Utils/LoopUtils.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2332,17 +2332,21 @@ mlir::affine::affineDataCopyGenerate(Block::iterator begin, Block::iterator end,
23322332
memref = storeOp.getMemRef();
23332333
memrefType = storeOp.getMemRefType();
23342334
}
2335-
// Neither load nor a store op.
2335+
// Not an affine.load/store op.
23362336
if (!memref)
23372337
return;
23382338

2339-
auto memorySpaceAttr =
2340-
dyn_cast_or_null<IntegerAttr>(memrefType.getMemorySpace());
23412339
if ((filterMemRef.has_value() && filterMemRef != memref) ||
2342-
(memorySpaceAttr &&
2340+
(isa_and_nonnull<IntegerAttr>(memrefType.getMemorySpace()) &&
23432341
memrefType.getMemorySpaceAsInt() != copyOptions.slowMemorySpace))
23442342
return;
23452343

2344+
if (!memref.getParentRegion()->isAncestor(block->getParent())) {
2345+
LLVM_DEBUG(llvm::dbgs() << "memref definition is inside of the depth at "
2346+
"which copy-in/copy-out would happen\n");
2347+
return;
2348+
}
2349+
23462350
// Compute the MemRefRegion accessed.
23472351
auto region = std::make_unique<MemRefRegion>(opInst->getLoc());
23482352
if (failed(region->compute(opInst, copyDepth, /*sliceState=*/nullptr,

mlir/test/Dialect/Affine/affine-data-copy.mlir

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,3 +419,15 @@ func.func @scalar_memref_copy_in_loop(%3:memref<480xi1>) {
419419
// CHECK: memref.dealloc %[[FAST_MEMREF]] : memref<480xi1>
420420
return
421421
}
422+
423+
// CHECK-LABEL: func @memref_def_inside
424+
func.func @memref_def_inside(%arg0: index) {
425+
%0 = llvm.mlir.constant(1.000000e+00 : f32) : f32
426+
// No copy generation can happen at this depth given the definition inside.
427+
affine.for %arg1 = 0 to 29 {
428+
%alloc_7 = memref.alloc() : memref<1xf32>
429+
// CHECK: affine.store {{.*}} : memref<1xf32>
430+
affine.store %0, %alloc_7[0] : memref<1xf32>
431+
}
432+
return
433+
}

0 commit comments

Comments
 (0)