<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/131105>131105</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [mlir] Inconsistent output when executing MLIR program with `--test-affine-parametric-tile`
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            mlir
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          Emilyaxe
      </td>
    </tr>
</table>

<pre>
    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)







</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsWV9vozoW_zTOy5EjMBDIQx7S5EaqdK_2amb2uXLgELxrbGSbNJ1Pv7KBNG1ze7OdmdU-VEIJwcfn_M7fHwFurTgoxBXJ7ki2nfHeNdqsfmuFfOInnO119bQ6CAdHNFZoRZI1ZMu6YkVe1CWmOYnWJFrbJ-uw9YtkEf1z3yvXQ1zMo3S-gN-_fSWLaJAjLCEsgS3a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