<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/130886>130886</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
tosa::ReduceSum Op goes wrong with type Float16.
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
CBalaa
</td>
</tr>
</table>
<pre>
My mlir code output wrongly because of tosa::ReduceOp, I try to lower tosa::ReduceOp to linalg.Reduce and scf.for, and they all go wrong!
Platform: X86 Ubuntu 22.04
I lower tosa::ReduceOp to scf::for op, I think it is logically correct. But my code expected to output [5120] on x86 platform, but got [2048], I wonder why and how to fix it:
```mlir
module attributes {test.weight = dense<1.000000e+00> : tensor<5120xf16>} {
memref.global "private" constant @__constant_5120xf16 : memref<5120xf16> = dense<1.000000e+00> {alignment = 64 : i64}
func.func private @printMemrefF16(memref<*xf16>)
func.func private @main() {
%c1 = arith.constant 1 : index
%c5120 = arith.constant 5120 : index
%c0 = arith.constant 0 : index
%cst = arith.constant 0.000000e+00 : f16
%0 = memref.get_global @__constant_5120xf16 : memref<5120xf16>
%alloc = memref.alloc() {alignment = 64 : i64} : memref<f16>
memref.store %cst, %alloc[] : memref<f16>
scf.for %arg0 = %c0 to %c5120 step %c1 {
%1 = memref.load %0[%arg0] : memref<5120xf16>
%2 = memref.load %alloc[] : memref<f16>
%3 = arith.addf %1, %2 : f16
memref.store %3, %alloc[] : memref<f16>
}
%expand_shape = memref.expand_shape %alloc [] output_shape [1] : memref<f16> into memref<1xf16>
%cast = memref.cast %expand_shape : memref<1xf16> to memref<*xf16>
call @printMemrefF16(%cast) : (memref<*xf16>) -> ()
return
}
}
```
makefile: (variables like MLIR_OPT should change to your executable.)
```makefile
#!/bin/bash
MLIR_OPT := ../../thirdparty/llvm/build/bin/mlir-opt
MLIR_TRANSLATE := ../../thirdparty/llvm/build/bin/mlir-translate
MLIR_CPU_RUNNER := ../../thirdparty/llvm/build/bin/mlir-cpu-runner
LLC := ../../thirdparty/llvm/build/bin/llc
OPT_FLAG := -O0
MLIR_LIB := ../../thirdparty/llvm/build/lib
AIPILER_PY_PATH := ../../python
MLIR_RUNNER_UTILS := ../../thirdparty/llvm/build/lib/libmlir_runner_utils.so
MLIR_C_RUNNER_UTILS := ../../thirdparty/llvm/build/lib/libmlir_c_runner_utils.so
MTRIPLE := x86_64-unknown-linux-gnu
test: test.mlir
${MLIR_OPT} $^ \
-pass-pipeline="builtin.module(func.func(tosa-to-linalg-named),func.func(tosa-to-linalg),func.func(tosa-to-tensor),func.func(tosa-to-arith), \
eliminate-empty-tensors, empty-tensor-to-alloc-tensor, one-shot-bufferize{bufferize-function-boundaries}, \
convert-linalg-to-loops, convert-vector-to-scf, expand-strided-metadata, convert-vector-to-llvm, \
arith-expand, lower-affine, convert-arith-to-llvm, convert-math-to-llvm, convert-math-to-libm, \
finalize-memref-to-llvm, convert-scf-to-cf)" --mlir-print-ir-after-failure | \
${MLIR_OPT} -llvm-request-c-wrappers | \
${MLIR_OPT} -pass-pipeline="builtin.module(convert-arith-to-llvm, convert-math-to-llvm, convert-math-to-libm, convert-func-to-llvm, reconcile-unrealized-casts)" | \
${MLIR_CPU_RUNNER} ${OPT_FLAG} -e main -entry-point-result=void \
-shared-libs=${MLIR_RUNNER_UTILS} -shared-libs=${MLIR_C_RUNNER_UTILS}
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJysV0Fv2zwS_TXMhZAgUZatHHyQ7Xg3gNsEaQrsngxKGlncUqSWpGJ7f_2CpGQ7sdOvLb6iMCJy5r2ZN6MhRbVmOwEwR-kCpas72ptGqvlyQTmld4WsjvMvR9xypnApK8CyN11v8F5JseNHXEBJew1Y1thITVGSoyR_gaov4alDZIkfsVFHbCTmcg_qhpHbY4LyXeiXMBUV1mUd1lJZBPtoGjhiyjneSU-NSIxRlKMof-bU1FK1KMnxv7Ip_l70wvSYkDCae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