Move return value to pointer argument


Is there a way to rewrite a function signature so that it returns void, and what was once the return value is written into a pointer argument? I’ve seen some bits scattered around (it looks like it is possible to clone a function in LLVM, and there is a pass that erases a return argument from an MLIR function), but I could appreciate some direction.

My end goal is that the function returns void when lowered to LLVM and then translated to LLVM IR. Does it make sense to think of a new MLIR transformation, or should I lower/translate everything to LLVM and work there?

You’ll likely have to write your own transformation for this, either at the LLVM dialect level or directly on LLVM IR. There is no notion of pointer above that level.

When lowering func.func to LLVM, we can optionally emit a wrapper function with a C-compatible interface for memrefs that may move results into a (leading) argument. The code for that is here for inspiration.

I think that is exactly what I need, thank you! Is the wrapper function supposed to be used by calling it from hand-written C code?

It works both ways: functions that have a body in mlir get a wrapper that is callable from C, functions that don’t have a body (are assumed to have an implementation in C) get a wrapper that makes them callable from MLIR. More details here: LLVM IR Target - MLIR

1 Like