llvm / llvm-project

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

[mlir] Inconsistent results for affine.for and memref.store #116664

Open wangyongj1a opened 1 week ago

wangyongj1a commented 1 week ago

I have the following MLIR program: test.mlir:

module {
  func.func private @func1() -> f32{
    %idx0 = index.constant 0
    %c21848_i16 = arith.constant 21848 : i16
    %c1 = arith.constant 1 : index
    %c5 = arith.constant 5 : index

    %alloc_107 = memref.alloc(%c5, %c1) : memref<?x?x1xi16>
    %c-58_i16 = arith.constant -58 : i16
    linalg.fill ins(%c-58_i16 : i16) outs(%alloc_107 : memref<?x?x1xi16>)
    affine.for %arg0 = 0 to 61 {
      memref.store %c21848_i16, %alloc_107[%idx0, %idx0, %idx0] : memref<?x?x1xi16>
    }
    %1 = memref.load %alloc_107[%idx0, %idx0, %idx0] : memref<?x?x1xi16>

    vector.print %1 : i16
    %2 = arith.sitofp %1 : i16 to f32
    return %2  : f32
  }
}

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

21848
2.184800e+04

However, when I ran /data/tmp/v1118/llvm-project/build/bin/mlir-opt --affine-loop-tile="tile-size=32" --affine-loop-normalize --lower-affine --test-vector-sink-patterns --scf-for-loop-range-folding --convert-vector-to-llvm --convert-arith-to-llvm --convert-linalg-to-loops --convert-arith-to-llvm --convert-scf-to-cf --convert-arith-to-llvm --convert-index-to-llvm --finalize-memref-to-llvm --convert-func-to-llvm --reconcile-unrealized-casts test.mlir | /data/tmp/v1118/llvm-project/build/bin/mlir-cpu-runner -e func1 --shared-libs=/data/tmp/v1118/llvm-project/build/lib/libmlir_runner_utils.so,/data/tmp/v1118/llvm-project/build/lib/libmlir_c_runner_utils.so on the program, I got the result of:

-58
-5.800000e+01

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 c25c6c32494c8d1038438b6208d42ba40f25270e.

wangyongj1a commented 3 days ago

I tried to reproduce this issue on history commit versions, and l found these inconsistent results can be reproduced on the commit of pass --for-loop-range-folding(i.e., --scf-for-loop-range-folding pass in the previous command) that was first added(3f429e82d3ea1710ee0a841675acba9bb7b658d2). To satisfy the history syntax and pass usage, I made some changes to the program, and the adjusted test.mlir is:

module {
  func @func1() -> f32{
    %idx0 = constant 0 :index
    %c21848_i16 = constant 21848 : i16
    %c1 = constant 1 : index
    %c5 = constant 5 : index

    %alloc_107 = memref.alloc(%c5, %c1) : memref<?x?x1xi16>
    %c-58_i16 = constant -58 : i16
    linalg.fill(%c-58_i16, %alloc_107) : i16, memref<?x?x1xi16>
    affine.for %arg0 = 1 to 61 {
      memref.store %c21848_i16, %alloc_107[%idx0, %idx0, %idx0] : memref<?x?x1xi16>
    }
    %1 = memref.load %alloc_107[%idx0, %idx0, %idx0] : memref<?x?x1xi16>

    vector.print %1 : i16
    %2 = sitofp %1 : i16 to f32
    return %2  : f32
  }
}

When I ran

/data/tmp/3f429e82d3ea/llvm-project/build/bin/mlir-opt --affine-loop-tile="tile-size=32" --affine-loop-normalize --lower-affine --canonicalize \
--convert-scf-to-std  --convert-vector-to-llvm --convert-linalg-to-loops --convert-scf-to-std --convert-std-to-llvm test.mlir | /data/tmp/3f429e82d3ea/llvm-project/build/bin/mlir-cpu-runner -e func1 --shared-libs=/data/tmp/3f429e82d3ea/llvm-project/build/lib/libmlir_runner_utils.so,/data/tmp/3f429e82d3ea/llvm-project/build/lib/libmlir_c_runner_utils.so

on the program, I got the result of:

21848
2.184800e+04

However, when I ran

/data/tmp/3f429e82d3ea/llvm-project/build/bin/mlir-opt --affine-loop-tile="tile-size=32" --affine-loop-normalize --lower-affine --canonicalize \
--for-loop-range-folding \
--convert-scf-to-std  --convert-vector-to-llvm --convert-linalg-to-loops --convert-scf-to-std --convert-std-to-llvm test.mlir | /data/tmp/3f429e82d3ea/llvm-project/build/bin/mlir-cpu-runner -e func1 --shared-libs=/data/tmp/3f429e82d3ea/llvm-project/build/lib/libmlir_runner_utils.so,/data/tmp/3f429e82d3ea/llvm-project/build/lib/libmlir_c_runner_utils.so

on the program, I got the result of:

-58
-5.800000e+01

Note that @anthonycanino1 has stopped committing more than 2 years, @joker-eph sorry to disturb but I was wondering if you would mind taking a look at this problem?