Issue 131105
Summary [mlir] Inconsistent output when executing MLIR program with `--test-affine-parametric-tile`
Labels mlir
Assignees
Reporter Emilyaxe
    git version: 59fd2878fce47

system: `Ubuntu 18.04.6 LTS`

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


## Steps to Reproduce:

### 1. **MLIR Program (a.mlir)**:
a.mlir: 
``` 
module {
 func.func private @printMemrefI32(tensor<*xi32>)
  func.func private @printMemrefF32(tensor<*xf32>)
  func.func @entry(%arg0: index) -> () {
 %1 = "tosa.const"() <{values = dense<7> : tensor<1x4x5xi32>}> : () -> tensor<1x4x5xi32>
    %5 = "tosa.const"() <{values = dense<-2522> : tensor<1x4x5xi32>}> : () -> tensor<1x4x5xi32>
    %16 = tosa.clamp %5 { max_val = 2 : i32,  min_val = 0 : i32} : (tensor<1x4x5xi32>) -> tensor<1x4x5xi32>
    %17 = tosa.logical_right_shift %1, %16 : (tensor<1x4x5xi32>, tensor<1x4x5xi32>) -> tensor<1x4x5xi32>
    %19 = tosa.reduce_product %17 {axis = 1 : i32} : (tensor<1x4x5xi32>) -> tensor<1x1x5xi32>
    %cast_4 = tensor.cast %19 : tensor<1x1x5xi32> to tensor<*xi32>
    call @printMemrefI32(%cast_4) : (tensor<*xi32>) -> ()
 return 
  }
  func.func @main() {
    %idx0 = index.constant 0
    call @entry(%idx0) : (index) -> ()
    return
  }
}
``` 

 ### 2. **Command to Run without  `--test-affine-parametric-tile` :**

``` 
/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt /data/szy/workspace/mlir-inconsistent/a.mlir \
-pass-pipeline="builtin.module(func.func(tosa-to-linalg-named,tosa-to-linalg))" \
| /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt -tosa-to-arith  --convert-elementwise-to-linalg  -convert-linalg-to-loops \
 -one-shot-bufferize="bufferize-function-boundaries" --linalg-fold-unit-extent-dims -convert-linalg-to-affine-loops  \
| /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt -pass-pipeline="builtin.module(func.func(affine-loop-unroll,affine-loop-unroll))" \
| /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt --test-constant-fold  -convert-index-to-llvm   -convert-arith-to-llvm \
--expand-strided-metadata   -lower-affine   -convert-scf-to-cf -finalize-memref-to-llvm   -convert-arith-to-llvm   -convert-cf-to-llvm \
-convert-func-to-llvm   -convert-math-to-llvm -reconcile-unrealized-casts \
| timeout 10 /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-cpu-runner -e main -entry-point-result=void \
--shared-libs=/data/szy/MLIR/llvm-release/llvm-project/build/lib/libmlir_c_runner_utils.so \
--shared-libs=/data/szy/MLIR/llvm-release/llvm-project/build/lib/libmlir_runner_utils.so \
--shared-libs=/data/szy/MLIR/llvm-release/llvm-project/build/lib/libmlir_async_runtime.so
``` 

### 3. **Output  without  `--test-affine-parametric-tile` :**:

``` 
[[[2401,    2401,    2401,    2401,    2401]]]
``` 

### 4. **Command to Run with  `--test-affine-parametric-tile` :**


``` 
/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt /data/szy/workspace/mlir-inconsistent/a.mlir \
-pass-pipeline="builtin.module(func.func(tosa-to-linalg-named,tosa-to-linalg))" \
| /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt -tosa-to-arith  --convert-elementwise-to-linalg  -convert-linalg-to-loops \
 -one-shot-bufferize="bufferize-function-boundaries" --linalg-fold-unit-extent-dims -convert-linalg-to-affine-loops  \
| /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt -pass-pipeline="builtin.module(func.func(affine-loop-unroll,affine-loop-unroll))" \
| /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt --test-constant-fold  -convert-index-to-llvm   -convert-arith-to-llvm \
--expand-strided-metadata --test-affine-parametric-tile   -lower-affine -convert-scf-to-cf  -finalize-memref-to-llvm   -convert-arith-to-llvm \ -convert-cf-to-llvm -convert-func-to-llvm   -convert-math-to-llvm -reconcile-unrealized-casts \
| timeout 10 /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-cpu-runner -e main -entry-point-result=void \
--shared-libs=/data/szy/MLIR/llvm-release/llvm-project/build/lib/libmlir_c_runner_utils.so \
--shared-libs=/data/szy/MLIR/llvm-release/llvm-project/build/lib/libmlir_runner_utils.so \
--shared-libs=/data/szy/MLIR/llvm-release/llvm-project/build/lib/libmlir_async_runtime.so
``` 

### 5. **Output with `--test-affine-parametric-tile` :**

``` 
[[[712284096,    22092,    0,    0,    712725616]]]
``` 

### 6. **Analysis for this case :**

I debug this issue and find the faulty pass is `--test-affine-parametric-tile`  pass
The input IR (ir before running the `--test-affine-parametric-tile` ) can be found in [input.txt](https://github.com/user-attachments/files/19225730/input.txt)
The output IR (ir after running the `--test-affine-parametric-tile` ) can be found in [output.txt](https://github.com/user-attachments/files/19225735/output.txt)
Please change file from .txt to .mlir

The `--test-affine-parametric-tile` pass utilizes SSA values as tiling parameters (tile sizes); however, it does not account for the case where a tile size is 0, which can lead to undefined behavior.

![Image](https://github.com/user-attachments/assets/e26c4858-12d6-48e6-858e-d688fb9eb414)







_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to