LLVM pointer type to memref type?

I have some code which is imported from llvm ir and is transferred to llvm dialect. As I know, for example, value with type memref<2x1xi8> would be lowered to llvm dialect with type llvm.struct<ptr, ptr, int64, array<2xi64>, array<2xi64>>.
I want to use “llvm i8 ptr” type buffer as input for operations such as vector load, mask store, etc… However these operation seems only available for the value with memref type. Is there any way to convert “llvm i8 ptr” type value to “memref” type value?

Memref is not just a pointer so in general you cannot use pointers as memrefs. There are two possible workarounds though:

  1. Populate the memref descriptor structure (https://mlir.llvm.org/docs/ConversionToLLVMDialect/#ranked-memref-types) with relevant data (allocated + aligned pointer, offset, sizes, strides) and use llvm.mlir.cast to convert that structure to memref. These casts will be lowered out by standard-to-llvm conversion if you call it.
  2. Use unrealized_conversion_cast that lets you cast any type to any other type. Using this means your code has a mechanism of resolving such casts later, i.e., either memrefs get lowered to pointers or there is a transformation somewhere that will retrofit your pointer into the expected descriptor format. You cannot lower unrealized_conversion_cast back to LLVM.

Thanks for reply! I constructed the llvm.struct type and used llvm.mlir.cast to convert to memref type, finally it worked!