llvm / llvm-project

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

incorrect lowering of vector.contract #58409

Open ftynse opened 2 years ago

ftynse commented 2 years ago

vector.contract lowers to add/mul contraction regardless of the contraction kind specified. Repro (expected to find compares or mins in the output, but add/mul are generated instead):

cat <<EOF | mlir-opt -convert-vector-to-llvm | grep cmp
#map0 = affine_map<(i, j, k) -> (i, k)>
#map1 = affine_map<(i, j, k) -> (k, j)>
#map2 = affine_map<(i, j, k) -> (i, j)> 

func.func @main() -> (i32) {
  %c0 = arith.constant 0 : i32
  %v0 = arith.constant dense<[[1., 2., 3., 4.], 
                              [5., 6., 7., 8.], 
                              [9., 10., 11., 12.]]> : vector<3x4xf32>

  %v1 = arith.constant dense<[[1., 2., 3.], 
                              [4., 5., 6.], 
                              [7., 8., 9.], 
                              [10., 11., 12.]]> : vector<4x3xf32>

  %v2 = arith.constant dense<[[0., 0., 0.], 
                              [0., 0., 0.], 
                              [0., 0., 0.]]> : vector<3x3xf32>

  %v3 = vector.contract {indexing_maps = [#map0, #map1, #map2], iterator_types = ["parallel", "parallel", "reduction"], kind = #vector.kind<minf>} %v0, %v1, %v2 : vector<3x4xf32>, vector<4x3xf32> into vector<3x3xf32>
  vector.print %v3 : vector<3x3xf32>
  return %c0 : i32
}
EOF

https://reviews.llvm.org/D136079 adds a stopgap, but a proper lowering may be desired.

On a side note, the modeling of contraction kind by a single selector isn't very clear to me. There are two primitive operations involved: per-element combination (multiplication) and accumulation (summation), which of them is being controlled by the kind?

llvmbot commented 2 years ago

@llvm/issue-subscribers-mlir-vectorops