Skip to content

Commit 73480da

Browse files
committed
[MLIR] Fix unchecked use of memref memory space attr in affine data copy generate
Fix unchecked use of memref memory space attr in affine data copy generate. In the case of memory accesses without a memory space attribute or those other than integer attributes, the pass treats them as slow memory spaces. Fixes #116536
1 parent 70b9440 commit 73480da

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2298,21 +2298,26 @@ mlir::affine::affineDataCopyGenerate(Block::iterator begin, Block::iterator end,
22982298

22992299
// Walk this range of operations to gather all memory regions.
23002300
block->walk(begin, end, [&](Operation *opInst) {
2301+
Value memref;
2302+
MemRefType memrefType;
23012303
// Gather regions to allocate to buffers in faster memory space.
23022304
if (auto loadOp = dyn_cast<AffineLoadOp>(opInst)) {
2303-
if ((filterMemRef.has_value() && filterMemRef != loadOp.getMemRef()) ||
2304-
(loadOp.getMemRefType().getMemorySpaceAsInt() !=
2305-
copyOptions.slowMemorySpace))
2306-
return;
2305+
memref = loadOp.getMemRef();
2306+
memrefType = loadOp.getMemRefType();
23072307
} else if (auto storeOp = dyn_cast<AffineStoreOp>(opInst)) {
2308-
if ((filterMemRef.has_value() && filterMemRef != storeOp.getMemRef()) ||
2309-
storeOp.getMemRefType().getMemorySpaceAsInt() !=
2310-
copyOptions.slowMemorySpace)
2311-
return;
2312-
} else {
2313-
// Neither load nor a store op.
2314-
return;
2308+
memref = storeOp.getMemRef();
2309+
memrefType = storeOp.getMemRefType();
23152310
}
2311+
// Neither load nor a store op.
2312+
if (!memref)
2313+
return;
2314+
2315+
auto memorySpaceAttr =
2316+
dyn_cast_or_null<IntegerAttr>(memrefType.getMemorySpace());
2317+
if ((filterMemRef.has_value() && filterMemRef != memref) ||
2318+
(memorySpaceAttr &&
2319+
memrefType.getMemorySpaceAsInt() != copyOptions.slowMemorySpace))
2320+
return;
23162321

23172322
// Compute the MemRefRegion accessed.
23182323
auto region = std::make_unique<MemRefRegion>(opInst->getLoc());

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,23 @@ func.func @index_elt_type(%arg0: memref<1x2x4x8xindex>) {
333333
// CHECK-NEXT: affine.for %{{.*}} = 0 to 8
334334
return
335335
}
336+
337+
#map = affine_map<(d0) -> (d0 + 1)>
338+
339+
// CHECK-LABEL: func @arbitrary_memory_space
340+
func.func @arbitrary_memory_space() {
341+
%alloc = memref.alloc() : memref<256x8xi8, #spirv.storage_class<StorageBuffer>>
342+
affine.for %arg0 = 0 to 32 step 4 {
343+
%0 = affine.apply #map(%arg0)
344+
affine.for %arg1 = 0 to 8 step 2 {
345+
%1 = affine.apply #map(%arg1)
346+
affine.for %arg2 = 0 to 8 step 2 {
347+
// CHECK: memref.alloc() : memref<1x7xi8>
348+
%2 = affine.apply #map(%arg2)
349+
%3 = affine.load %alloc[%0, %1] : memref<256x8xi8, #spirv.storage_class<StorageBuffer>>
350+
affine.store %3, %alloc[%0, %2] : memref<256x8xi8, #spirv.storage_class<StorageBuffer>>
351+
}
352+
}
353+
}
354+
return
355+
}

0 commit comments

Comments
 (0)