Issue 238199
Summary [MLIR][MemRef] `flatten-memref` creates an invalid symbol _expression_
Labels mlir
Assignees
Reporter pingshiyu
    Reproduced with the official LLVM/MLIR 23.1.4 optimized Linux x86-64 build.

This verifier-valid program uses an affine induction variable after a index to i64 conversion and back, as the indices of a prefetch:

```mlir
module {
 func.func @loop_local_index(%source: memref<1x1x2xi32>) {
    affine.for %iv = 0 to 1 {
      %wide = arith.index_cast %iv : index to i64
      %index = arith.index_cast %wide : i64 to index
      memref.prefetch %source[%index, %index, %index], read, locality<1>, data : memref<1x1x2xi32>
    }
 return
  }
}
```

The pass fails:

```console
mlir-opt reduced.mlir \
  '--pass-pipeline=builtin.module(func.func(flatten-memref))' \
  -o /dev/null
```

```text
error: dimensional operand cannot be used as a symbol
note: see current operation: %... = "affine.apply"(%...) <{map = affine_map<()[s0] -> (s0 * 5)>}> : (index) -> index
```

Using `%iv` directly for the three prefetch indices succeeds and produces the valid dimensional map `affine_map<(d0) -> (d0 * 5)>`. 

## LLM-Suggested Cause

`IndexedAccessOpFlattenPattern` passes arbitrary indexed-access operands to `getLinearizedMemRefOffsetAndSize`. That helper initially binds every
index/stride term as an affine symbol. In this case, composing away the two `arith.index_cast` operations exposes the enclosing affine induction variable but leaves it in a symbol position, yielding invalid IR. The relevant current `main` paths are the [flattening caller](https://github.com/llvm/llvm-project/blob/384a71b8a2d99b5deb82792c23202c2b82b8ed82/mlir/lib/Dialect/MemRef/Transforms/FlattenMemRefs.cpp#L295-L317) and the [symbol-only linearization _expression_](https://github.com/llvm/llvm-project/blob/384a71b8a2d99b5deb82792c23202c2b82b8ed82/mlir/lib/Dialect/MemRef/Utils/MemRefUtils.cpp#L56-L86).

## LLM Declaration

LLM was used to prepare the bug report, and the bug itself was found and verified by a human.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to