Skip to content

Commit 9fe28ba

Browse files
committed
[MLIR][Affine] Fix copy generation for missing memref definition depth check
Fixes: #122210
1 parent da5b8ff commit 9fe28ba

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
@@ -2330,17 +2330,21 @@ mlir::affine::affineDataCopyGenerate(Block::iterator begin, Block::iterator end,
23302330
memref = storeOp.getMemRef();
23312331
memrefType = storeOp.getMemRefType();
23322332
}
2333-
// Neither load nor a store op.
2333+
// Not an affine.load/store op.
23342334
if (!memref)
23352335
return;
23362336

2337-
auto memorySpaceAttr =
2338-
dyn_cast_or_null<IntegerAttr>(memrefType.getMemorySpace());
23392337
if ((filterMemRef.has_value() && filterMemRef != memref) ||
2340-
(memorySpaceAttr &&
2338+
(isa_and_nonnull<IntegerAttr>(memrefType.getMemorySpace()) &&
23412339
memrefType.getMemorySpaceAsInt() != copyOptions.slowMemorySpace))
23422340
return;
23432341

2342+
if (!memref.getParentRegion()->isAncestor(block->getParent())) {
2343+
LLVM_DEBUG(llvm::dbgs() << "memref definition is inside of the depth at "
2344+
"which copy-in/copy-out would happen\n");
2345+
return;
2346+
}
2347+
23442348
// Compute the MemRefRegion accessed.
23452349
auto region = std::make_unique<MemRefRegion>(opInst->getLoc());
23462350
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)