Issue 174517
Summary [MLIR] `-linalg-specialize-generic-ops` causes wrong-code for `linalg.matmul`
Labels mlir
Assignees
Reporter BealZephyr
    **test commit**: [5cfd02](https://github.com/llvm/llvm-project/commit/5cfd02f44a43a2e2a085a633b022a62f64ba2b93)

## Description:
I found a miscompilation issue in MLIR where adding -linalg-specialize-generic-ops to an otherwise identical lowering pipeline changes the numerical result of a linalg.matmul.


## test case
```
#map = affine_map<(d0, d1, d2) -> (d0, d2)>
#map1 = affine_map<(d0, d1, d2) -> (d2, d1)>
#map2 = affine_map<(d0, d1, d2) -> (d0, d1)>
module {
 memref.global "private" constant @__constant_16x32xi32 : memref<16x32xi32> = dense<130> {alignment = 64 : i64}
  memref.global "private" constant @__constant_8x32xi64 : memref<8x32xi64> = dense<159> {alignment = 64 : i64}
 memref.global "private" constant @__constant_16x8xi16 : memref<16x8xi16> = dense<-50> {alignment = 64 : i64}
  func.func private @printMemrefI32(memref<*xi32>)
  func.func @main() {
    %0 = memref.get_global @__constant_16x8xi16 : memref<16x8xi16>
    %1 = memref.get_global @__constant_8x32xi64 : memref<8x32xi64>
    %2 = memref.get_global @__constant_16x32xi32 : memref<16x32xi32>
    %alloc = memref.alloc() {alignment = 64 : i64} : memref<16x32xi32>
    memref.copy %2, %alloc : memref<16x32xi32> to memref<16x32xi32>
    linalg.generic {indexing_maps = [#map, #map1, #map2], iterator_types = ["parallel", "parallel", "reduction"]} ins(%0, %1 : memref<16x8xi16>, memref<8x32xi64>) outs(%alloc : memref<16x32xi32>) {
    ^bb0(%in: i16, %in_0: i64, %out: i32):
      %3 = arith.extui %in : i16 to i32
      %4 = arith.trunci %in_0 : i64 to i32
      %5 = arith.muli %3, %4 : i32
      %6 = arith.addi %out, %5 : i32
      linalg.yield %6 : i32
    }
    %cast = memref.cast %alloc : memref<16x32xi32> to memref<*xi32>
    call @printMemrefI32(%cast) : (memref<*xi32>) -> ()
    return
  }
}
```

## Command:
### 1. without -linalg-specialize-generic-ops

```
mlir-opt test.mlir -convert-linalg-to-affine-loops -lower-affine -convert-scf-to-cf -convert-to-llvm |  mlir-runner -e main -entry-point-result=void -shared-libs=/home/workdir/llvm-project-latest/build/lib/libmlir_runner_utils.so,/home/workdir/llvm-project-latest/build/lib/libmlir_c_runner_utils.so,/home/workdir/llvm-project-latest/build/lib/libmlir_async_runtime.so
```

### output:
```
[83298322,   83298322,   83298322,   83298322,   83298322, 83298322,   83298322,   83298322,   ...]]
```
### 2. With -linalg-specialize-generic-ops:

```
mlir-opt test.mlir -linalg-specialize-generic-ops -convert-linalg-to-affine-loops -lower-affine -convert-scf-to-cf -convert-to-llvm |  mlir-runner -e main -entry-point-result=void -shared-libs=/home/workdir/llvm-project-latest/build/lib/libmlir_runner_utils.so,/home/workdir/llvm-project-latest/build/lib/libmlir_c_runner_utils.so,/home/workdir/llvm-project-latest/build/lib/libmlir_async_runtime.so
```
### output:
```
[-63470,   -63470,   -63470,   -63470,   -63470,   -63470, -63470,   -63470,   -63470,   -63470,   ...]]
```

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

Reply via email to