[Mlir-commits] [mlir] [mlir][vector] Support multi-dimensional vectors in VectorFromElementsLowering (PR #151175)

Yang Bai llvmlistbot at llvm.org
Mon Aug 11 03:08:55 PDT 2025


yangtetris wrote:

> How we ideally want to structure conversion to backends is:
>
> Set of patterns to do unrolling from N-D vectors to 1-D vectors
Set of patterns to do flattening from N-D vectors to 1-D vectors (in case someone wants to do this, we dont have patterns for this today)
Set of patterns to cleanup boundary ops created by unrolling/flattening
Set of patterns to convert 1-D vector dialect operations to LLVM dialect
Set of patterns to convert 1-D vector (+ spirv restrictions) dialect operations to SPIRV dialect.

Thank you for your detailed explanation, this is very helpful for understanding why keeping the conversion logic simple is important.

> Unrolling will bloat the code size when unrolling a large dimension whereas linearization will generate fewer ops (best case a single op).

There could be some different cases to consider here. Here are the comparison between unrolling vs flattening, with the ordinary vector type `<2x2x3x4x7x11xi32>`. I was surprised that there's not much difference in the generated IR sequence lengths.

| |Unrolling | Flattening |
|------|------|-----|
|Process| <2x3x4x7x2xi32> -> 2 x vector<2x3x4x7x11xi32> ->  2x2 x vector<3x4x7x11xi32> -> ... -> 2x2x3x4x7 x vector<11xi32> | from_elements: vector<3696xi32> <br> shape_cast: vector<3696xi32> -> vector<2x2x3x4x7x11xi32> | 
| Count by Op Type | llvm.insertelement: 3696 <br> llvm.insertvalue: 402 <br> llvm.mlir.constant: 3696 <br> llvm.mlir.poison: 336 | llvm.insertelement: 3696 <br> llvm.insertvalue: 336 <br> llvm.mlir.constant: 3696 <br> llvm.mlir.poison: 336 |
| Total | 8130 | 8064 |

**What's more**, after CSE, the **unrolling** method will have a much shorter length, because there are only 11 unique constant ops for indices.

> For this PR specifically, my suggestion is that we:
>
> Add the shape cast implementation to VectorLinearize.cpp (one PR).
Add the unrolling implementation, using the direct implementation, similar to what other unrolling patterns do (one PR).

That makes sense to me. Users should be able to choose between unrolling or flattening (temporarily, we can choose one to integrate into convert-vector-to-llvm). Could you please further elaborate on what "using the direct implementation" refers to? My plan is to implement one following the example of [UnrollVectorGather](https://github.com/llvm/llvm-project/blob/5bd39a0060bed29e04db954eff77386473d38bb3/mlir/lib/Dialect/Vector/Transforms/LowerVectorGather.cpp#L52-L95).


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


More information about the Mlir-commits mailing list