Issue with Converting memref Parameters to emitc Types in MLIR

Hi everyone,

I’ve been working with MLIR’s --convert-func-to-emitc and --convert-memref-to-emitc passes to translate MLIR functions to EmitC. However, I encountered an issue where these passes do not convert function parameters of type memref to emitc types. This lack of conversion causes the entire func to remain incompatible with EmitC, preventing a successful translation into C++.

MLIR is a highly capable toolchain, so I was surprised to find that memref parameter conversion is unsupported. Without the ability to handle memref arguments, it seems that func cannot be properly converted to EmitC for further translation into C++. This limitation is quite significant, as it hinders the completion of EmitC-based workflows that rely on memref structures.

I’m also wondering if perhaps I misunderstood how this conversion should work. Is it possible that this issue could be handled at a higher abstraction level, rather than directly within func?

To address this, I tried implementing a conversion that maps memref types to emitc.array during the func to EmitC transformation. Would this approach be correct, or is there a better way to handle memref conversions in EmitC?

Could someone provide guidance on:

  1. Why this functionality might be missing from the current MLIR toolchain?
  2. Whether mapping memref to emitc.array is a feasible solution or if there are better alternatives within EmitC for handling memref parameters.

Thank you!

1 Like

I have the same issue currently, I hand rolled an type converter that has a conversion role for FunctionType that converts input and result types, the memref → array conversion is already there upstream but this is missing and I don’t understand why (omission? legitimate reason?), i.e. I have:

struct EmitCTypeConverter : TypeConverter {
  explicit EmitCTypeConverter(MLIRContext *ctx) {

    addConversion([this, ctx](FunctionType type) -> std::optional<Type> { ... });
  }

imo, emitc is sorely missing an equivalent to LLVMTypeConverter, it seems like currently everyone trying to use this dialect has to supply this themselves. There is e.g. populateMemRefToEmitCTypeConversion but this does not cover this case and breaks style with the Type conversion mechanism used by LLVM (although there is some precedent elsewhere). Maybe someone else more familiar with emitc can weight in here.

There is a PR that combines all upstream patterns and type conversions. This has to be refactored though.