Passing in a Unranked Memref to an LLVM function call

Hi all,

I am trying to write a simple MLIR file that calls an external C function that takes in a float array.

I am using the Memref dialect to create this array. However, I am getting a seg fault within my C function anytime I would like to access the array.

What is the right way to approach this?

My code structure looks like:

%input_ptr = memref.alloc() : memref<1x1x16xf32>
%cf1 = arith.constant 1.00000e+00 : f32
linalg.fill ins(%cf1 : f32) outs(%input_ptr : memref<1x1x16xf32>)
%input_ptr_ = memref.cast %input_ptr : memref<1x1x16xf32> to memref<*xf32>
%elems= arith.constant 16: i32
call @print_array(%input_ptr_, elems) : (memref<*xf32>, i32) -> ()

where print_array is defined in C as follows:

void print_array(float *arr, int elems) {
  printf("arr = [");
  for(int i=0; i<elems; i++)
    printf(" %f", arr[i]);

Thank you,

Memref is not a pointer. You cannot just pass a memref to a function that takes a pointer and expect that to work. You can check the documentation LLVM IR Target - MLIR and use the memref ABI.

If you want a pointer you can now memref.extract_aligned_pointer_as_index and pass that around.
If you want to use it at the LLVM dialect level you can additionally inttoptr.

I see. Thank you for the clarification. I am still in the learning process and this is very helpful.

I will look into the documentation on the LLVM IR Target.

I tried using memref.extract_aligned_pointer_as_index but mlir-opt is telling me that the op is unknown. Should I be using a specific commit for MLIR?

small/test_memref.mlir:11:10: error: custom op 'memref.extract_aligned_pointer_as_index' is unknown
    %0 = memref.extract_aligned_pointer_as_index %input_ptr_ : memref<*xf32> -> index

It’s no more than a week old, so probably just fetch the repo head as of now.

1 Like