Can we read data from input file to fill the content of an alloc object?


Does MLIR support read data from input file? Can the data be used to fill the content of an “alloc” object, e.g. %1 = alloc() : memref<5xi32>. Can the values in %1 be read from file?

Thank you for your help in advance.

Have you gone through the tutorial yet?
MLIR is an extensible framework, there is little that you can’t do in MLIR (CIRCT is a good example).

Loading data from a file and populating memory is trivially implement through calling a runtime function (that you implement in C) though.
In general, you get into IR design when you want to model something where the semantics is important for further transformations on the IR.

There is a way to print memrefs in MLIR (a call to print_memref via its runtime utils) - you could similarly design a way to scan in memrefs from a file. Allocate the memref and have a similar interface/ABI scan_memref.

Hi Uday,

Thank you so much! I make it. it works.


Hi Mehdi,

Thank you for your information. It helps me!


Hi Ruiqin, how did you do that? Would you share the solution here?

As others stated, MLIR is extensible, so you can add such functionality easily.

Having said that, the sparse tensor dialect already provides a very convenient new operator to read in sparse tensors from file in one of the common external formats, such as FROSTT and the MatrixMarket.

You will find several examples of this in the integration tests, but the general form is as shown below. By following the lowering of this operator to actual LLMV code with calls into a runtime support library for file I/O, you may get some inspiration on how to implement your own reader and writer.

!Filename = !llvm.ptr<i8>

#CSR = #sparse_tensor.encoding<{
  dimLevelType = [ "compressed", "compressed" ],
  dimOrdering = affine_map<(i,j) -> (i,j)>


// Read the sparse input matrix A from a file.
%A = %fileName : !Filename to tensor<?x?xf64, #CSR>
1 Like