Skip to content

Commit be75020

Browse files
authored
[MLIR] Fix unchecked use of memref memory space attr in affine data copy generate (#116763)
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 4a0b8c3 commit be75020

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
@@ -2304,21 +2304,26 @@ mlir::affine::affineDataCopyGenerate(Block::iterator begin, Block::iterator end,
23042304

23052305
// Walk this range of operations to gather all memory regions.
23062306
block->walk(begin, end, [&](Operation *opInst) {
2307+
Value memref;
2308+
MemRefType memrefType;
23072309
// Gather regions to allocate to buffers in faster memory space.
23082310
if (auto loadOp = dyn_cast<AffineLoadOp>(opInst)) {
2309-
if ((filterMemRef.has_value() && filterMemRef != loadOp.getMemRef()) ||
2310-
(loadOp.getMemRefType().getMemorySpaceAsInt() !=
2311-
copyOptions.slowMemorySpace))
2312-
return;
2311+
memref = loadOp.getMemRef();
2312+
memrefType = loadOp.getMemRefType();
23132313
} else if (auto storeOp = dyn_cast<AffineStoreOp>(opInst)) {
2314-
if ((filterMemRef.has_value() && filterMemRef != storeOp.getMemRef()) ||
2315-
storeOp.getMemRefType().getMemorySpaceAsInt() !=
2316-
copyOptions.slowMemorySpace)
2317-
return;
2318-
} else {
2319-
// Neither load nor a store op.
2320-
return;
2314+
memref = storeOp.getMemRef();
2315+
memrefType = storeOp.getMemRefType();
23212316
}
2317+
// Neither load nor a store op.
2318+
if (!memref)
2319+
return;
2320+
2321+
auto memorySpaceAttr =
2322+
dyn_cast_or_null<IntegerAttr>(memrefType.getMemorySpace());
2323+
if ((filterMemRef.has_value() && filterMemRef != memref) ||
2324+
(memorySpaceAttr &&
2325+
memrefType.getMemorySpaceAsInt() != copyOptions.slowMemorySpace))
2326+
return;
23222327

23232328
// Compute the MemRefRegion accessed.
23242329
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)