Issue 132155
Summary [MLIR] Inconsistent output when executing MLIR program with and without `-affine-loop-fusion`
Labels mlir
Assignees
Reporter Lambor24
    My git version is [6003c30](https://github.com/llvm/llvm-project/commit/6003c3055a4630be31cc3d459cdbb88248a007b9).

## Description:
I am experiencing an inconsistent result when executing the same MLIR program with and without the `-affine-loop-fusion`.

## Steps to Reproduce:

### 1. **MLIR Program (test.mlir)**:

test.mlir:

```
module {
  func.func private @printMemrefF32(tensor<*xf32>)
  func.func @main() {
    %0 = "tosa.const"() <{values = dense<2.000000e+00> : tensor<1x3x9x4xf32>}> : () -> tensor<1x3x9x4xf32>
    %1 = "tosa.const"() <{values = dense<3.000000e+00> : tensor<12x2x5x4xf32>}> : () -> tensor<12x2x5x4xf32>
 %2 = "tosa.const"() <{values = dense<4.000000e+00> : tensor<12xf32>}> : () -> tensor<12xf32>
    %3 = "tosa.const"() <{values = dense<0.000000e+00> : tensor<1xf32>}> : () -> tensor<1xf32>
    %4 = "tosa.const"() <{values = dense<0.000000e+00> : tensor<1xf32>}> : () -> tensor<1xf32>
    %5 = tosa.conv2d %0, %1, %2, %3, %4 {acc_type = f32, dilation = array<i64: 1, 2>, pad = array<i64: 1, 1, 1, 1>, stride = array<i64: 1, 1>} : (tensor<1x3x9x4xf32>, tensor<12x2x5x4xf32>, tensor<12xf32>, tensor<1xf32>, tensor<1xf32>) -> tensor<1x4x3x12xf32>
    %6 = tosa.reduce_sum %5 {axis = 1 : i32} : (tensor<1x4x3x12xf32>) -> tensor<1x1x3x12xf32>
    %cast = tensor.cast %6 : tensor<1x1x3x12xf32> to tensor<*xf32>
    call @printMemrefF32(%cast) : (tensor<*xf32>) -> ()
    return
 }
}
```

### 2. **Command to Run Without `-affine-loop-fusion`:**

```
/path/llvm-project/build/bin/mlir-opt test.mlir -pass-pipeline='builtin.module(func.func(tosa-to-linalg-named))' | \
/path/llvm-project/build/bin/mlir-opt -pass-pipeline='builtin.module(func.func(tosa-to-linalg))' | \
/path/llvm-project/build/bin/mlir-opt -tosa-to-arith -one-shot-bufferize="bufferize-function-boundaries" -convert-linalg-to-affine-loops -lower-affine -convert-scf-to-cf -expand-strided-metadata -convert-cf-to-llvm -convert-arith-to-llvm -convert-math-to-llvm -canonicalize -finalize-memref-to-llvm -convert-func-to-llvm | \
/path/llvm-project/build/bin/mlir-runner -e main -entry-point-result=void \
-shared-libs=/path/llvm-project/build/lib/libmlir_runner_utils.so \
-shared-libs=/path/llvm-project/build/lib/libmlir_c_runner_utils.so \
-shared-libs=/path/llvm-project/build/lib/libmlir_async_runtime.so
```

### 3. **Output Without `-affine-loop-fusion`:**

```
[[[[592,     592, 592,     592,     592,     592,     592,     592,     592,     592,     592, 592], 
   [736,     736,     736,     736,     736,     736,     736, 736,     736,     736,     736,     736], 
   [592,     592,     592, 592,     592,     592,     592,     592,     592,     592,     592, 592]]]]
```

### 4. **Command to Run With `-affine-loop-fusion`:**

```
/path/llvm-project/build/bin/mlir-opt test.mlir -pass-pipeline='builtin.module(func.func(tosa-to-linalg-named))' | \
/path/llvm-project/build/bin/mlir-opt -pass-pipeline='builtin.module(func.func(tosa-to-linalg))' | \
/path/llvm-project/build/bin/mlir-opt -tosa-to-arith -one-shot-bufferize="bufferize-function-boundaries" -convert-linalg-to-affine-loops -affine-loop-fusion -lower-affine -convert-scf-to-cf -expand-strided-metadata -convert-cf-to-llvm -convert-arith-to-llvm -convert-math-to-llvm -canonicalize -finalize-memref-to-llvm -convert-func-to-llvm | \
/path/llvm-project/build/bin/mlir-runner -e main -entry-point-result=void \
-shared-libs=/path/llvm-project/build/lib/libmlir_runner_utils.so \
-shared-libs=/path/llvm-project/build/lib/libmlir_c_runner_utils.so \
-shared-libs=/path/llvm-project/build/lib/libmlir_async_runtime.so
```

### 5. **Output With `-affine-loop-fusion`:**

```
[[[[16,     16,     16, 16,     16,     16,     16,     16,     16,     16,     16,     16], 
 [16,     16,     16,     16,     16,     16,     16,     16,     16,     16, 16,     16], 
   [16,     16,     16,     16,     16,     16,     16, 16,     16,     16,     16,     16]]]]
```

I'm not sure if there is any bug in my program or if the wrong usage of the above passes caused this result.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to