Memref of vector type

Hi

How to convert memref type to vector type.

memref<3x1xf32> to vector<3x1xf32>

I tried vector::TypeCastOp, but it is expecting the result type as memref<vector<3x1xf3>>. With this type, I am not able to use this operand in any other vector operations like ShapeCast op.

Getting below error:

error: ‘vector.shape_cast’ op operand #0 must be vector of any type values or tuple with any combination of vector of any type values values, but got ‘memref<vector<1x3xf32>>’

  • M

You can use ops such as vector.transfer_read and vector.transfer_write to read from/write to memory. The vector dialect documentation is a good starting point.

E.g.:

%padding = arith.constant 0.0 : f32
%1 = vector.transfer_read %arg0[%c3, %c3], %padding : memref<?x?xf32>, vector<3x7xf32>

There are also passes/transforms that vectorize higher-level ops. E.g., the transform.structured.vectorize transform op vectorizes ops from the linalg dialect.

Thank Mattias.
To transfer from memref<3x2xf32> to vector<3x2xf32>, do we need to do the transfer controlled by affine loops or just a single transfer_read instruction is sufficient?

I have gone through vector.transfer_read operation. But its functioning is not very clear to me. My requirement is to convert memref type (say memref<3x2xf32>) to vector type (say vector<3x2xf32>). How can I achieve it? Do I need to read each element one by one and transfer to vector?

You do not just “convert” from memref to a value type, you have to load from a memref.

The Memref dialect has a load operation, which would allow load each element of the memref individually (here you have 6 memref.load to pull all the f32 value pointed by the memref). Now the point of the vector.transfer_read is to read a slice (potentially the slice can cover the entire memref) directly into a vector (this is literally the first two sentence of the doc).
Since the slice is multi-dimensional, the transfer_read may not read consecutive elements in memory: it acts as a gather. In your case the slice is contiguous (since it covers the entire memref).

Please read the documentation carefully and ask detailed question if some specific aspects aren’t clear.

(actually the lowering example in the doc does not seem correct to me, trying to improve in ⚙ D151037 [MLIR][doc] Improve/fix the doc on mlir.vector.transfer_read (NFC) )

This is the important part. A memref is a reference to memory or an address. A vector is a value. You can load and store vectors with a memref as the address. Converting an memref or address to a vector sounds odd.