[Mlir-commits] [mlir] [mlir][memref] `memref.subview`: Verify result strides (PR #79865)

Han-Chung Wang llvmlistbot at llvm.org
Wed Jan 31 23:33:25 PST 2024


hanhanW wrote:

I think this is causing a weird issue in downstream projects (i.e., IREE), and I don't have a proper fix yet. We have `--iree-iree-hoist-statically-bound-allocations` pass [[implementation](https://github.com/openxla/iree/blob/8e1eca95f6c544e715d38642075759fc97308aac/compiler/src/iree/compiler/Codegen/Transforms/Transforms.cpp#L194-L246)], which hoists the allocation ops and replace the original one with `memref.subview`. Then it triggers a verification issue. Below is the input IR, and it passes IR verification.

```mlir
#map = affine_map<(d0) -> (d0, 16)>
func.func @nested_op_alloca_subview_use(%arg0 : index, %o0 : index, %o1 : index) {
  %c0 = arith.constant 0 : index
  %c1 = arith.constant 1 : index
  %c42 = arith.constant 42 : i32
  scf.for %iv = %c0 to %arg0 step %c1 {
    %0 = affine.min #map(%iv)
    %1 = memref.alloca(%0, %0) : memref<?x?xi32>
    %2 = memref.subview %1[%o0, %o1][%c1, %0][1, 1] : memref<?x?xi32> to memref<?x?xi32, strided<[?, 1], offset: ?>>
    scf.yield
  }
  return
}
```

After running the pass, we generate a alloca op outside of scf.for, and replace the old one with memref.subview. So we get

```mlir
func.func @nested_op_alloca_subview_use(%arg0 : index, %o0 : index, %o1 : index) {
  %c0 = arith.constant 0 : index
  %c1 = arith.constant 1 : index
  %c42 = arith.constant 42 : i32
  %alloca = memref.alloca() : memref<16x16xi32>
  scf.for %iv = %c0 to %arg0 step %c1 {
    %0 = affine.min #map(%iv)
    %1 = memref.subview %alloca[0, 0] [%0, %0] [1, 1] : memref<16x16xi32> to memref<?x?xi32, strided<[16, 1]>>
    %2 = memref.subview %1[%o0, %o1][%c1, %0][1, 1] : memref<?x?xi32, strided<[16, 1]>> to memref<?x?xi32, strided<[?, 1], offset: ?>>
    scf.yield
  }
  return
}
```

Then the verification issue happens. It infers a different type in the second subview, because the strides change. The old type is `memref<?x?xi32, strided<[?, 1], offset: ?>>` and the expected type is `memref<?x?xi32, strided<[16, 1], offset: ?>>`. I think this could happen in other passes. Do you have a suggestion about how we can fix it, or can we revert the commit?


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


More information about the Mlir-commits mailing list