llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.98k stars 11.95k forks source link

[mlir] Inconsistent results for affine.load and arith.minui #114652

Open wangyongj1a opened 6 days ago

wangyongj1a commented 6 days ago

I have the following MLIR program: test.mlir:

func.func @func1() -> f32 {
  %idx0 = index.constant 0
  %idx2 = index.constant 2
  %c1396717254_i64 = arith.constant 1396717254 : i64
  %c792_i64 = arith.constant 792 : i64

  %alloc_115 = memref.alloc() : memref<1x5xi64>
  linalg.fill ins(%c792_i64 : i64) outs(%alloc_115 : memref<1x5xi64>)

  %99 = affine.load %alloc_115[%idx0, %idx2] : memref<1x5xi64>

  // can not replace %99 with %c792_i64
  %105 = arith.xori %99, %c1396717254_i64 : i64

  %158 = arith.minui %105, %c1396717254_i64 : i64
  vector.print %158 : i64

  %159 = arith.subi %158, %c1396717254_i64 : i64
  vector.print %159 : i64
  %0 = arith.sitofp %159 : i64 to f32
  return %0 : f32
}

When I ran /data/tmp/v1102/llvm-project/build/bin/mlir-opt --lower-affine --convert-vector-to-llvm --convert-index-to-llvm --convert-linalg-to-loops --convert-scf-to-cf --finalize-memref-to-llvm --convert-arith-to-llvm --convert-func-to-llvm --reconcile-unrealized-casts test.mlir | /data/tmp/v1102/llvm-project/build/bin/mlir-cpu-runner -e func1 --shared-libs=/data/tmp/v1102/llvm-project/build/lib/libmlir_runner_utils.so,/data/tmp/v1102/llvm-project/build/lib/libmlir_c_runner_utils.so on the program, I got the result of:

1396717022
-232
-2.320000e+02

However, when I ran /data/tmp/v1102/llvm-project/build/bin/mlir-opt --int-range-optimizations --lower-affine --convert-vector-to-llvm --convert-index-to-llvm --convert-linalg-to-loops --convert-scf-to-cf --finalize-memref-to-llvm --convert-arith-to-llvm --convert-func-to-llvm --reconcile-unrealized-casts test.mlir | /data/tmp/v1102/llvm-project/build/bin/mlir-cpu-runner -e func1 --shared-libs=/data/tmp/v1102/llvm-project/build/lib/libmlir_runner_utils.so,/data/tmp/v1102/llvm-project/build/lib/libmlir_c_runner_utils.so on the program, I got the result of:

1396717254
0
0.000000e+00

The above two results seem to be inconsistent. I'm not sure if there is any bug in my program or if the wrong usage of the above passes caused these results.

My git version is 33bdb53d864e3e244d8fd5649062f17b7d4c958d.

llvmbot commented 6 days ago

@llvm/issue-subscribers-mlir-affine

Author: None (wangyongj1a)

I have the following MLIR program: test.mlir: ``` func.func @func1() -> f32 { %idx0 = index.constant 0 %idx2 = index.constant 2 %c1396717254_i64 = arith.constant 1396717254 : i64 %c792_i64 = arith.constant 792 : i64 %alloc_115 = memref.alloc() : memref<1x5xi64> linalg.fill ins(%c792_i64 : i64) outs(%alloc_115 : memref<1x5xi64>) %99 = affine.load %alloc_115[%idx0, %idx2] : memref<1x5xi64> // can not replace %99 with %c792_i64 %105 = arith.xori %99, %c1396717254_i64 : i64 %158 = arith.minui %105, %c1396717254_i64 : i64 vector.print %158 : i64 %159 = arith.subi %158, %c1396717254_i64 : i64 vector.print %159 : i64 %0 = arith.sitofp %159 : i64 to f32 return %0 : f32 } ``` When I ran ```/data/tmp/v1102/llvm-project/build/bin/mlir-opt --lower-affine --convert-vector-to-llvm --convert-index-to-llvm --convert-linalg-to-loops --convert-scf-to-cf --finalize-memref-to-llvm --convert-arith-to-llvm --convert-func-to-llvm --reconcile-unrealized-casts test.mlir | /data/tmp/v1102/llvm-project/build/bin/mlir-cpu-runner -e func1 --shared-libs=/data/tmp/v1102/llvm-project/build/lib/libmlir_runner_utils.so,/data/tmp/v1102/llvm-project/build/lib/libmlir_c_runner_utils.so``` on the program, I got the result of: ``` 1396717022 -232 -2.320000e+02 ``` However, when I ran ```/data/tmp/v1102/llvm-project/build/bin/mlir-opt --int-range-optimizations --lower-affine --convert-vector-to-llvm --convert-index-to-llvm --convert-linalg-to-loops --convert-scf-to-cf --finalize-memref-to-llvm --convert-arith-to-llvm --convert-func-to-llvm --reconcile-unrealized-casts test.mlir | /data/tmp/v1102/llvm-project/build/bin/mlir-cpu-runner -e func1 --shared-libs=/data/tmp/v1102/llvm-project/build/lib/libmlir_runner_utils.so,/data/tmp/v1102/llvm-project/build/lib/libmlir_c_runner_utils.so``` on the program, I got the result of: ``` 1396717254 0 0.000000e+00 ``` The above two results seem to be inconsistent. I'm not sure if there is any bug in my program or if the wrong usage of the above passes caused these results. My git version is 33bdb53d864e3e244d8fd5649062f17b7d4c958d.