[Mlir-commits] [mlir] [mlir] remove folder on MemRef::ExtractStridedMetadataOp (PR #88043)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Apr 8 22:28:49 PDT 2024


MaheshRavishankar wrote:

The crux of the issue is you should never decouple the base pointer and the offset when using memrefs. Doing that leads to all sorts of badness. Here is an example, which afaics proves this folding is wrong

Lets start with this example

```
%0 = memref.alloc(%d0) : memref<?xf32>
%1 = memref.alloc(%d1) : memref<?xf32>
%base0, %offset0, %size0, %stride0 = memref.extract_strided_metadata %0 
%base1, %offset1, %size1, %stride1 = memref.extract_strided_metadata %1 
func.call(%base0, %offset0, ..., %base1, %offset1, ...)
```

So the caller will use the metadata info to access values in this buffer. The folder above with canonicalize this away to
```
%0 = memref.alloc(%d0) : memref<?xf32>
%1 = memref.alloc(%d1) : memref<?xf32>
%c0 = arith.constant 0 : index
%base0, %offset0, %size0, %stride0 = memref.extract_strided_metadata %0 
%base1, %offset1, %size1, %stride1 = memref.extract_strided_metadata %1 
func.call(%base0, %c0, .. %base1, %c0, ....)
```

Lets say later on there is a "buffer coalesing pass" (totally making up the name) which tries to create a single allocation for all the buffers. So rewrites the program to
```
%alloc = memref.alloc(%d0 + d1) : memref<?xf32>
%0 = memref.subview %alloc[0] [%d0] [1] : memref<?xf32> to memref<?xf32>
%1 = memref.subview %alloc[%d0] [%d1] [1] : memref<?xf32> to memref<?xf32>
%c0 = arith.constant 0 : index
%base0, %offset0, %size0, %stride0 = memref.extract_strided_metadata %0 
%base1, %offset1, %size1, %stride1 = memref.extract_strided_metadata %1 
func.call(%base0, %c0, .. %base1, %c0, ....)
```

Now the extract strided metadata does get the right values of `%offset0` and `%offset1` and is consistent, but becaise the link from the base pointer and offset has been broken due to folding, the offsets passed to the functions cannot be updated. So the caller now sees the same base pointer and offset, while the original program had different base pointers and offsets. If the folder wasnt there the code would be correct. 

```
%alloc = memref.alloc(%d0 + d1) : memref<?xf32>
%0 = memref.subview %alloc[0] [%d0] [1] : memref<?xf32> to memref<?xf32>
%1 = memref.subview %alloc[%d0] [%d1] [1] : memref<?xf32> to memref<?xf32>
%c0 = arith.constant 0 : index
%base0, %offset0, %size0, %stride0 = memref.extract_strided_metadata %0 
%base1, %offset1, %size1, %stride1 = memref.extract_strided_metadata %1 
func.call(%base0, %offset0, .. %base1, %offset1, ....)
```

Because the extract metadata couples the base pointer and offset the impact of the transformation of collapsing the allocations is resolved correctly. 

https://github.com/llvm/llvm-project/pull/88043


More information about the Mlir-commits mailing list