[Mlir-commits] [mlir] [mlir][vector]advance support extract insert under dynamic case. (PR #121631)

Kunwar Grover llvmlistbot at llvm.org
Sat Jan 4 10:39:58 PST 2025


Groverkss wrote:

> > Do we really need this? It's reasonable for the lowering to expect a canonical form of the operation. This seems to just add support for a non canonical form where the constant positions arent folded in.
> 
> At least for me it is useful that the patch is more favorable to the concept of MLIR multilayered intermediate representations.
> 
> ```
> $ mlir-opt vector-extract-insert.mlir -affine-loop-unroll="unroll-full=true unroll-num-reps=10" -convert-arith-to-llvm -convert-arith-to-llvm  -convert-vector-to-llvm
> ```
> 
> With this PR you can use the above pipeline lower the code below.(Honestly, the code below should theoretically be able to manually codegen out the llvm dialect IR. since the loop count is clear. However, you need to take into account the level where the current IR is located, so there are times when it's better to use a loop instead of generating the llvm dialect directly.)
> 
> ```
> func.func @matmul() -> i32 {
>   %c = arith.constant dense<0> : vector<32x1xi32>
>   %a = arith.constant dense<1> : vector<32x1xi32>
>   %b = arith.constant dense<1> : vector<1x1xi32>
>   %c_0 = arith.constant 0 : index
>   %y = affine.for %arg_0 = 0 to 32 iter_args(%it_0 = %c) -> vector<32x1xi32> {
>     %y_0 = affine.for %arg_1 = 0 to 1 iter_args(%it_1 = %it_0) -> vector<32x1xi32> {
>       %c_elem = vector.extract %c[%arg_0, %arg_1] : i32 from vector<32x1xi32>
>       %a_elem = vector.extract %a[%arg_0, %c_0] : i32 from vector<32x1xi32>
>       %b_elem = vector.extract %b[%arg_1, %c_0] : i32 from vector<1x1xi32>
>       %mul = arith.muli %a_elem, %b_elem : i32
>       %ret = arith.addi %mul, %c_elem : i32 
>       %y_1 = vector.insert %ret, %c[%arg_0, %arg_1] : i32 into vector<32x1xi32>
>       affine.yield %y_1 : vector<32x1xi32>
>     }
>     affine.yield %y_0 : vector<32x1xi32>
>   } 
>   vector.print %c : vector<32x1xi32>
>   vector.print %y : vector<32x1xi32>
>   %ret = arith.constant 0 : i32
>   return %ret : i32
> }
> ```
> 
> This is why this PR came up.

Have you tried inserting -canonicalize before -convert-vector-to-llvm

https://github.com/llvm/llvm-project/pull/121631


More information about the Mlir-commits mailing list