[MLIR] Inconsistent output when executing MLIR program with and without `-linalg-inline-scalar-operands`
My git version is 35619c7.
Description:
I am experiencing an inconsistent result when executing the same MLIR program with and without the -linalg-inline-scalar-operands.
Steps to Reproduce:
1. MLIR Program (test.mlir):
test.mlir:
module {
func.func private @printMemrefI32(tensor<*xi32>)
func.func @main() -> () {
%arg0 = "tosa.const"() {value = dense<83> : tensor<i32>} : () -> tensor<i32>
%arg1 = "tosa.const"() {value = dense<36> : tensor<i32>} : () -> tensor<i32>
%0 = tosa.arithmetic_right_shift %arg0, %arg1 {round = true} : (tensor<i32>, tensor<i32>) -> tensor<i32>
%rtn1 = tensor.cast %0 : tensor<i32> to tensor<*xi32>
call @printMemrefI32(%rtn1) : (tensor<*xi32>) -> ()
return
}
}
2. Command to Run Without -linalg-inline-scalar-operands:
/path/llvm-project/build/bin/mlir-opt test.mlir -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-loops -finalize-memref-to-llvm -convert-func-to-llvm -reconcile-unrealized-casts | \
/path/llvm-project/build/bin/mlir-cpu-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 -linalg-inline-scalar-operands:
[5]
4. Command to Run With -linalg-inline-scalar-operands:
/path/llvm-project/build/bin/mlir-opt test.mlir -pass-pipeline='builtin.module(func.func(tosa-to-linalg))' | \
/path/llvm-project/build/bin/mlir-opt -linalg-inline-scalar-operands -tosa-to-arith -one-shot-bufferize='bufferize-function-boundaries' -convert-linalg-to-loops -finalize-memref-to-llvm -convert-func-to-llvm -reconcile-unrealized-casts | \
/path/llvm-project/build/bin/mlir-cpu-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 -linalg-inline-scalar-operands:
[0]
I'm not sure if there is any bug in my program or if the wrong usage of the above passes caused this result.
Arithmetic right-shift where the RHS (shift amount) is greater than the bitwidth of the LHS operand is undefined behavior (in LLVM as well as in the TOSA spec).
You are observing undefined behavior, so it doesn't look like a bug.
What appears to be happening is that in the first case (without the scalar inlining pass), the tosa.const ... tensor<i32> are lowered into llvm.global buffers (of a single scalar) rather than as a llvm.constant. Therefore, the generated LLVM code doesn't see the constant values and the operands of the llvm.ashr are results of loads. The downstream LLVM compiler won't notice that the operands will result in UB. Instead, you are seeing the result of what ashr 83, 36 happens to be on your machine. On x86, this happens to be 83 >> (36 %32) which is 5 .
In the second case, the constants will be converted t o llvm.constant due to the inlining. The LLVM compiler is producing a poison value (see https://llvm.org/docs/LangRef.html#ashr-instruction). If you run the second case multiple times, you will see that the result actually changes randomly.