llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[mlir] Inconsistent output when executing MLIR program with `linalg-specialize-generic-ops`

Open Emilyaxe opened this issue 1 year ago • 7 comments

git version: https://github.com/llvm/llvm-project/commit/2172a5edcf9357d24006a3c1032f2d52e199915d

system: Ubuntu 18.04.6 LTS

Description:

I am experiencing an inconsistent result when executing the same MLIR program with and without linalg-specialize-generic-ops.

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 @main() -> () {
    %arg0 = index.constant 0
    %6 = "tosa.const"() <{values = dense<-132> : tensor<1x2x1xi32>}> : () -> tensor<1x2x1xi32>
    %11 = tosa.cast %6 : (tensor<1x2x1xi32>) -> tensor<1x2x1xf32>
    %15 = "tosa.const"() <{values = dense<0> : tensor<1x2x1xi32>}> : () -> tensor<1x2x1xi32>
    %16 = tosa.while_loop (%arg1 = %15) : (tensor<1x2x1xi32>) -> tensor<1x2x1xi32> {
      %40 = "tosa.const"() <{values = dense<3> : tensor<1x2x1xi32>}> : () -> tensor<1x2x1xi32>
      %41 = tosa.greater %40, %arg1 : (tensor<1x2x1xi32>, tensor<1x2x1xi32>) -> tensor<1x2x1xi1>
      %extracted = tensor.extract %41[%arg0, %arg0, %arg0] : tensor<1x2x1xi1>
      %from_elements = tensor.from_elements %extracted : tensor<i1>
      tosa.yield %from_elements : tensor<i1>
    } do {
    ^bb0(%arg1: tensor<1x2x1xi32>):
      %45 = "tosa.const"() <{values = dense<1> : tensor<1x2x1xi32>}> : () -> tensor<1x2x1xi32>
      %46 = tosa.add %arg1, %45 : (tensor<1x2x1xi32>, tensor<1x2x1xi32>) -> tensor<1x2x1xi32>
      tosa.yield %46 : tensor<1x2x1xi32>
    }
    %17 = tosa.clamp %16 {max_val = 1073741823 : i32, min_val = -1073741824 : i32} : (tensor<1x2x1xi32>) -> tensor<1x2x1xi32>
    %18 = tosa.clamp %16 {max_val = 1073741823 : i32, min_val = -1073741824 : i32} : (tensor<1x2x1xi32>) -> tensor<1x2x1xi32>
    %19 = tosa.sub %17, %18 : (tensor<1x2x1xi32>, tensor<1x2x1xi32>) -> tensor<1x2x1xi32>
    %cast19 = tensor.cast %19 : tensor<1x2x1xi32> to tensor<*xi32>
    call @printMemrefI32(%cast19) : (tensor<*xi32>) -> ()
    return 
  }

}


2. Command to Run without linalg-specialize-generic-ops :

/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))" \
| /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt -tosa-to-scf  \
| /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt -pass-pipeline="builtin.module(func.func(tosa-to-linalg))" \
| /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt -tosa-to-tensor  -tosa-to-arith  -convert-scf-to-cf  -convert-math-to-llvm   \
--linalg-fuse-elementwise-ops --cse --linalg-generalize-named-ops  -convert-arith-to-llvm  \
-one-shot-bufferize="bufferize-function-boundaries"  --linalg-fold-unit-extent-dims   -finalize-memref-to-llvm  --expand-strided-metadata  --linalg-specialize-generic-ops \
-convert-linalg-to-affine-loops   -convert-index-to-llvm  -finalize-memref-to-llvm  -lower-affine  -convert-scf-to-cf   \
-convert-arith-to-llvm -convert-cf-to-llvm  -finalize-memref-to-llvm   -convert-func-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 linalg-specialize-generic-ops ::

[[[0],
  [0]]]

4. Command to Run with linalg-specialize-generic-ops :

/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))" \
| /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt -tosa-to-scf  \
| /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt -pass-pipeline="builtin.module(func.func(tosa-to-linalg))" \
| /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt -tosa-to-tensor  -tosa-to-arith  -convert-scf-to-cf  -convert-math-to-llvm   \
--linalg-fuse-elementwise-ops --cse --linalg-generalize-named-ops  -convert-arith-to-llvm  \
-one-shot-bufferize="bufferize-function-boundaries"  --linalg-fold-unit-extent-dims   -finalize-memref-to-llvm  --expand-strided-metadata  \
-convert-linalg-to-affine-loops   -convert-index-to-llvm  -finalize-memref-to-llvm  -lower-affine  -convert-scf-to-cf   \
-convert-arith-to-llvm -convert-cf-to-llvm  -finalize-memref-to-llvm   -convert-func-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 linalg-specialize-generic-ops :

[[[3],
  [3]]]

6. Analysis for this case :

I debug this issue and find the faulty pass is linalg-specialize-generic-ops The input IR (ir before running the linalg-specialize-generic-ops) can be found in input.txt The output IR (ir after running thelinalg-specialize-generic-ops) can be found in output.txt

Please change file from .txt to .mlir

This MLIR program is expected to correctly output [0, 0] for %19 = tosa.sub %17, %18, given that %17 and %18 are both equal to %16. However, instead of the expected result, it incorrectly outputs [3, 3], which is the value of %16. To debug this issue, I printed the IR after each pass and found that the input IR input.txt is correct before applying the --linalg-specialize-generic-ops pass. As shown in the first image, %reinterpret_cast_24 (the final result) is stored the value of %9, which is a constant with the value 0. However, after running --linalg-specialize-generic-ops output.txt , in the second image the linalg.generic operation is mistakenly optimized into linalg.copy, propagating the value 3 from %reinterpret_cast_19 to %reinterpret_cast_24, ultimately leading to the incorrect final result.

Image

Image

Emilyaxe avatar Mar 06 '25 07:03 Emilyaxe

@llvm/issue-subscribers-mlir

Author: None (Emilyaxe)

git version: 953838d

system: Ubuntu 18.04.6 LTS

Description:

I am experiencing an inconsistent result when executing the same MLIR program with and without linalg-specialize-generic-ops.

Steps to Reproduce:

1. MLIR Program (a.mlir):

a.mlir:

module {
  func.func private @<!-- -->printMemrefI32(tensor&lt;*xi32&gt;)
  func.func private @<!-- -->printMemrefF32(tensor&lt;*xf32&gt;)
  func.func @<!-- -->main() -&gt; () {
    %arg0 = index.constant 0
    %6 = "tosa.const"() &lt;{value = dense&lt;-132&gt; : tensor&lt;1x2x1xi32&gt;}&gt; : () -&gt; tensor&lt;1x2x1xi32&gt;
    %11 = tosa.cast %6 : (tensor&lt;1x2x1xi32&gt;) -&gt; tensor&lt;1x2x1xf32&gt;
    %15 = "tosa.const"() &lt;{value = dense&lt;0&gt; : tensor&lt;1x2x1xi32&gt;}&gt; : () -&gt; tensor&lt;1x2x1xi32&gt;
    %16 = tosa.while_loop (%arg1 = %15) : (tensor&lt;1x2x1xi32&gt;) -&gt; tensor&lt;1x2x1xi32&gt; {
      %40 = "tosa.const"() &lt;{value = dense&lt;3&gt; : tensor&lt;1x2x1xi32&gt;}&gt; : () -&gt; tensor&lt;1x2x1xi32&gt;
      %41 = tosa.greater %40, %arg1 : (tensor&lt;1x2x1xi32&gt;, tensor&lt;1x2x1xi32&gt;) -&gt; tensor&lt;1x2x1xi1&gt;
      %extracted = tensor.extract %41[%arg0, %arg0, %arg0] : tensor&lt;1x2x1xi1&gt;
      %from_elements = tensor.from_elements %extracted : tensor&lt;i1&gt;
      tosa.yield %from_elements : tensor&lt;i1&gt;
    } do {
    ^bb0(%arg1: tensor&lt;1x2x1xi32&gt;):
      %40 = tosa.sin %11 : (tensor&lt;1x2x1xf32&gt;) -&gt; tensor&lt;1x2x1xf32&gt;
      %41 = tosa.slice %11 {size = array&lt;i64: 1, 5, 6&gt;, start = array&lt;i64: 0, 9, 25&gt;} : (tensor&lt;1x2x1xf32&gt;) -&gt; tensor&lt;1x5x6xf32&gt;
      %42 = tosa.erf %11 : (tensor&lt;1x2x1xf32&gt;) -&gt; tensor&lt;1x2x1xf32&gt;
      %43 = tosa.reverse %11 {axis = 0 : i32} : (tensor&lt;1x2x1xf32&gt;) -&gt; tensor&lt;1x2x1xf32&gt;
      %44 = tosa.greater_equal %41, %41 : (tensor&lt;1x5x6xf32&gt;, tensor&lt;1x5x6xf32&gt;) -&gt; tensor&lt;1x5x6xi1&gt;
      %45 = "tosa.const"() &lt;{value = dense&lt;1&gt; : tensor&lt;1x2x1xi32&gt;}&gt; : () -&gt; tensor&lt;1x2x1xi32&gt;
      %46 = tosa.add %arg1, %45 : (tensor&lt;1x2x1xi32&gt;, tensor&lt;1x2x1xi32&gt;) -&gt; tensor&lt;1x2x1xi32&gt;
      tosa.yield %46 : tensor&lt;1x2x1xi32&gt;
    }
    %17 = tosa.clamp %16 {max_fp = 1.07374182E+9 : f32, max_int = 1073741823 : i64, min_fp = -1.07374182E+9 : f32, min_int = -1073741824 : i64} : (tensor&lt;1x2x1xi32&gt;) -&gt; tensor&lt;1x2x1xi32&gt;
    %18 = tosa.clamp %16 {max_fp = 1.07374182E+9 : f32, max_int = 1073741823 : i64, min_fp = -1.07374182E+9 : f32, min_int = -1073741824 : i64} : (tensor&lt;1x2x1xi32&gt;) -&gt; tensor&lt;1x2x1xi32&gt;
    %19 = tosa.sub %17, %18 : (tensor&lt;1x2x1xi32&gt;, tensor&lt;1x2x1xi32&gt;) -&gt; tensor&lt;1x2x1xi32&gt;
    %cast19 = tensor.cast %19 : tensor&lt;1x2x1xi32&gt; to tensor&lt;*xi32&gt;
    call @<!-- -->printMemrefI32(%cast19) : (tensor&lt;*xi32&gt;) -&gt; ()
    return 
  }

}

2. Command to Run without linalg-specialize-generic-ops :

/data/szy/MLIR/llvm-release/llvm-project/install/mlir-opt /data/szy/workspace/mlir-inconsistent/a.mlir -pass-pipeline="builtin.module(func.func(tosa-to-linalg-named))" \
| /data/szy/MLIR/llvm-release/llvm-project/install/mlir-opt -tosa-to-scf  \
| /data/szy/MLIR/llvm-release/llvm-project/install/mlir-opt -pass-pipeline="builtin.module(func.func(tosa-to-linalg))" \
| /data/szy/MLIR/llvm-release/llvm-project/install/mlir-opt -tosa-to-tensor  -tosa-to-arith  -convert-scf-to-cf  -convert-math-to-llvm   \
--linalg-fuse-elementwise-ops --cse --linalg-generalize-named-ops  -convert-arith-to-llvm  \
-one-shot-bufferize="bufferize-function-boundaries"  --linalg-fold-unit-extent-dims   -finalize-memref-to-llvm  --expand-strided-metadata  \
-convert-linalg-to-affine-loops  -convert-cf-to-llvm  -convert-index-to-llvm  -finalize-memref-to-llvm  -lower-affine  -convert-scf-to-cf   \
-convert-arith-to-llvm  -finalize-memref-to-llvm   -convert-func-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 linalg-specialize-generic-ops ::

[[[0],
  [0]]]

4. Command to Run with linalg-specialize-generic-ops :

/data/szy/MLIR/llvm-release/llvm-project/install/mlir-opt /data/szy/workspace/mlir-inconsistent/a.mlir -pass-/data/szy/MLIR/llvm-release/llvm-project/install/mlir-opt /data/szy/workspace/mlir-inconsistent/a.mlir -pass-pipeline="builtin.module(func.func(tosa-to-linalg-named))" \
| /data/szy/MLIR/llvm-release/llvm-project/install/mlir-opt -tosa-to-scf  \
| /data/szy/MLIR/llvm-release/llvm-project/install/mlir-opt -pass-pipeline="builtin.module(func.func(tosa-to-linalg))" \
| /data/szy/MLIR/llvm-release/llvm-project/install/mlir-opt -tosa-to-tensor  -tosa-to-arith  -convert-scf-to-cf  -convert-math-to-llvm   \
--linalg-fuse-elementwise-ops --cse --linalg-generalize-named-ops  -convert-arith-to-llvm  \
-one-shot-bufferize="bufferize-function-boundaries"  --linalg-fold-unit-extent-dims   -finalize-memref-to-llvm  --expand-strided-metadata  \
--linalg-specialize-generic-ops  -convert-linalg-to-affine-loops  -convert-cf-to-llvm  -convert-index-to-llvm  -finalize-memref-to-llvm  -lower-affine  -convert-scf-to-cf   \
-convert-arith-to-llvm  -finalize-memref-to-llvm   -convert-func-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 linalg-specialize-generic-ops :

[[[3],
  [3]]]

6. Analysis for this case :

This MLIR program is expected to correctly output [0, 0] for %19 = tosa.sub %17, %18, given that %17 and %18 are both equal to %16. However, instead of the expected result, it incorrectly outputs [3, 3], which is the value of %16. To debug this issue, I printed the IR after each pass and found that the input IR input.txt is correct before applying the --linalg-specialize-generic-ops pass. As shown in the first image, %reinterpret_cast_24 (the final result) is stored the value of %9, which is a constant with the value 0. However, after running --linalg-specialize-generic-ops output.txt , in the second image the linalg.generic operation is mistakenly optimized into linalg.copy, propagating the value 3 from %reinterpret_cast_19 to %reinterpret_cast_24, ultimately leading to the incorrect final result.

Image

Image

llvmbot avatar Mar 06 '25 07:03 llvmbot

@llvm/issue-subscribers-mlir-linalg

Author: None (Emilyaxe)

git version: 953838dceaff

system: Ubuntu 18.04.6 LTS

Description:

I am experiencing an inconsistent result when executing the same MLIR program with and without linalg-specialize-generic-ops.

Steps to Reproduce:

1. MLIR Program (a.mlir):

a.mlir:

module {
  func.func private @<!-- -->printMemrefI32(tensor&lt;*xi32&gt;)
  func.func private @<!-- -->printMemrefF32(tensor&lt;*xf32&gt;)
  func.func @<!-- -->main() -&gt; () {
    %arg0 = index.constant 0
    %6 = "tosa.const"() &lt;{value = dense&lt;-132&gt; : tensor&lt;1x2x1xi32&gt;}&gt; : () -&gt; tensor&lt;1x2x1xi32&gt;
    %11 = tosa.cast %6 : (tensor&lt;1x2x1xi32&gt;) -&gt; tensor&lt;1x2x1xf32&gt;
    %15 = "tosa.const"() &lt;{value = dense&lt;0&gt; : tensor&lt;1x2x1xi32&gt;}&gt; : () -&gt; tensor&lt;1x2x1xi32&gt;
    %16 = tosa.while_loop (%arg1 = %15) : (tensor&lt;1x2x1xi32&gt;) -&gt; tensor&lt;1x2x1xi32&gt; {
      %40 = "tosa.const"() &lt;{value = dense&lt;3&gt; : tensor&lt;1x2x1xi32&gt;}&gt; : () -&gt; tensor&lt;1x2x1xi32&gt;
      %41 = tosa.greater %40, %arg1 : (tensor&lt;1x2x1xi32&gt;, tensor&lt;1x2x1xi32&gt;) -&gt; tensor&lt;1x2x1xi1&gt;
      %extracted = tensor.extract %41[%arg0, %arg0, %arg0] : tensor&lt;1x2x1xi1&gt;
      %from_elements = tensor.from_elements %extracted : tensor&lt;i1&gt;
      tosa.yield %from_elements : tensor&lt;i1&gt;
    } do {
    ^bb0(%arg1: tensor&lt;1x2x1xi32&gt;):
      %40 = tosa.sin %11 : (tensor&lt;1x2x1xf32&gt;) -&gt; tensor&lt;1x2x1xf32&gt;
      %41 = tosa.slice %11 {size = array&lt;i64: 1, 5, 6&gt;, start = array&lt;i64: 0, 9, 25&gt;} : (tensor&lt;1x2x1xf32&gt;) -&gt; tensor&lt;1x5x6xf32&gt;
      %42 = tosa.erf %11 : (tensor&lt;1x2x1xf32&gt;) -&gt; tensor&lt;1x2x1xf32&gt;
      %43 = tosa.reverse %11 {axis = 0 : i32} : (tensor&lt;1x2x1xf32&gt;) -&gt; tensor&lt;1x2x1xf32&gt;
      %44 = tosa.greater_equal %41, %41 : (tensor&lt;1x5x6xf32&gt;, tensor&lt;1x5x6xf32&gt;) -&gt; tensor&lt;1x5x6xi1&gt;
      %45 = "tosa.const"() &lt;{value = dense&lt;1&gt; : tensor&lt;1x2x1xi32&gt;}&gt; : () -&gt; tensor&lt;1x2x1xi32&gt;
      %46 = tosa.add %arg1, %45 : (tensor&lt;1x2x1xi32&gt;, tensor&lt;1x2x1xi32&gt;) -&gt; tensor&lt;1x2x1xi32&gt;
      tosa.yield %46 : tensor&lt;1x2x1xi32&gt;
    }
    %17 = tosa.clamp %16 {max_fp = 1.07374182E+9 : f32, max_int = 1073741823 : i64, min_fp = -1.07374182E+9 : f32, min_int = -1073741824 : i64} : (tensor&lt;1x2x1xi32&gt;) -&gt; tensor&lt;1x2x1xi32&gt;
    %18 = tosa.clamp %16 {max_fp = 1.07374182E+9 : f32, max_int = 1073741823 : i64, min_fp = -1.07374182E+9 : f32, min_int = -1073741824 : i64} : (tensor&lt;1x2x1xi32&gt;) -&gt; tensor&lt;1x2x1xi32&gt;
    %19 = tosa.sub %17, %18 : (tensor&lt;1x2x1xi32&gt;, tensor&lt;1x2x1xi32&gt;) -&gt; tensor&lt;1x2x1xi32&gt;
    %cast19 = tensor.cast %19 : tensor&lt;1x2x1xi32&gt; to tensor&lt;*xi32&gt;
    call @<!-- -->printMemrefI32(%cast19) : (tensor&lt;*xi32&gt;) -&gt; ()
    return 
  }

}

2. Command to Run without linalg-specialize-generic-ops :

/data/szy/MLIR/llvm-release/llvm-project/install/mlir-opt /data/szy/workspace/mlir-inconsistent/a.mlir -pass-pipeline="builtin.module(func.func(tosa-to-linalg-named))" \
| /data/szy/MLIR/llvm-release/llvm-project/install/mlir-opt -tosa-to-scf  \
| /data/szy/MLIR/llvm-release/llvm-project/install/mlir-opt -pass-pipeline="builtin.module(func.func(tosa-to-linalg))" \
| /data/szy/MLIR/llvm-release/llvm-project/install/mlir-opt -tosa-to-tensor  -tosa-to-arith  -convert-scf-to-cf  -convert-math-to-llvm   \
--linalg-fuse-elementwise-ops --cse --linalg-generalize-named-ops  -convert-arith-to-llvm  \
-one-shot-bufferize="bufferize-function-boundaries"  --linalg-fold-unit-extent-dims   -finalize-memref-to-llvm  --expand-strided-metadata  \
-convert-linalg-to-affine-loops  -convert-cf-to-llvm  -convert-index-to-llvm  -finalize-memref-to-llvm  -lower-affine  -convert-scf-to-cf   \
-convert-arith-to-llvm  -finalize-memref-to-llvm   -convert-func-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 linalg-specialize-generic-ops ::

[[[0],
  [0]]]

4. Command to Run with linalg-specialize-generic-ops :

/data/szy/MLIR/llvm-release/llvm-project/install/mlir-opt /data/szy/workspace/mlir-inconsistent/a.mlir -pass-/data/szy/MLIR/llvm-release/llvm-project/install/mlir-opt /data/szy/workspace/mlir-inconsistent/a.mlir -pass-pipeline="builtin.module(func.func(tosa-to-linalg-named))" \
| /data/szy/MLIR/llvm-release/llvm-project/install/mlir-opt -tosa-to-scf  \
| /data/szy/MLIR/llvm-release/llvm-project/install/mlir-opt -pass-pipeline="builtin.module(func.func(tosa-to-linalg))" \
| /data/szy/MLIR/llvm-release/llvm-project/install/mlir-opt -tosa-to-tensor  -tosa-to-arith  -convert-scf-to-cf  -convert-math-to-llvm   \
--linalg-fuse-elementwise-ops --cse --linalg-generalize-named-ops  -convert-arith-to-llvm  \
-one-shot-bufferize="bufferize-function-boundaries"  --linalg-fold-unit-extent-dims   -finalize-memref-to-llvm  --expand-strided-metadata  \
--linalg-specialize-generic-ops  -convert-linalg-to-affine-loops  -convert-cf-to-llvm  -convert-index-to-llvm  -finalize-memref-to-llvm  -lower-affine  -convert-scf-to-cf   \
-convert-arith-to-llvm  -finalize-memref-to-llvm   -convert-func-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 linalg-specialize-generic-ops :

[[[3],
  [3]]]

6. Analysis for this case :

This MLIR program is expected to correctly output [0, 0] for %19 = tosa.sub %17, %18, given that %17 and %18 are both equal to %16. However, instead of the expected result, it incorrectly outputs [3, 3], which is the value of %16. To debug this issue, I printed the IR after each pass and found that the input IR input.txt is correct before applying the --linalg-specialize-generic-ops pass. As shown in the first image, %reinterpret_cast_24 (the final result) is stored the value of %9, which is a constant with the value 0. However, after running --linalg-specialize-generic-ops output.txt , in the second image the linalg.generic operation is mistakenly optimized into linalg.copy, propagating the value 3 from %reinterpret_cast_19 to %reinterpret_cast_24, ultimately leading to the incorrect final result.

Image

Image

llvmbot avatar Mar 06 '25 08:03 llvmbot

@javedabsar , assigned this one to you as our expert in the area :)

banach-space avatar Mar 06 '25 10:03 banach-space

@Emilyaxe please could you share the linalg out of tosa. For me the above tosa IR gives error $ ./build/bin/mlir-opt -pass-pipeline="builtin.module(func.func(tosa-to-linalg-named))" tosa.mlir tosa.mlir:18:13: error: 'tosa.slice' op expected 3 operands, but found 1 %41 = tosa.slice %11 {size = array<i64: 1, 5, 6>, start = array<i64: 0, 9, 25>} : (tensor<1x2x1xf32>) -> tensor<1x5x6xf32> ^ tosa.mlir:18:13: note: see current operation: %10 = "tosa.slice"(%2) {size = array<i64: 1, 5, 6>, start = array<i64: 0, 9, 25>} : (tensor<1x2x1xf32>) -> tensor<1x5x6xf32>

javedabsar1 avatar Mar 06 '25 12:03 javedabsar1

Hi @javedabsar1,

Thank you for your quick reply.

Sorry for the confusion—the TOSA program cannot be compiled with the latest version of mlir-opt. The corresponding Linalg out of tosa ( input IR ) can be found in input-3.txt, and the output IR after running --linalg-specialize-generic-ops is available in output-3.txt. Please note that you should rename the files from .txt to .mlir before use.

The following commands can be used to generate the results. This command is compatible with the latest version (d6c0839c9c82375).

/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt input.mlir \
--linalg-specialize-generic-ops  -convert-linalg-to-affine-loops  -convert-cf-to-llvm  -convert-index-to-llvm  -finalize-memref-to-llvm  -lower-affine  -convert-scf-to-cf  -convert-cf-to-llvm  \
-convert-arith-to-llvm  -finalize-memref-to-llvm   -convert-func-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

Emilyaxe avatar Mar 06 '25 14:03 Emilyaxe

I have updated the case to reproduce the bug in the latest version (https://github.com/llvm/llvm-project/commit/2172a5edcf9357d24006a3c1032f2d52e199915d), and I have also provided the input and output IR before and after the suspicious pass.

Image

Emilyaxe avatar Mar 09 '25 08:03 Emilyaxe

Hi, @javedabsar1 , are you still check this issue? If not, I can open a PR to fix it.

CoTinker avatar Jul 17 '25 12:07 CoTinker