Edit: sorry, I read the wrong post as the original question.
But it would be helpful to see how exactly you call the mlir_ciface wrapper from your C++ code
-llvm-request-c-wrappers does not invoke MLIR, it only sets the llvm.emit_c_interface attribute on the function(s) so that the proper interface and entry point is emitted.
If you build a .o from that, you should be able to link it and call it from C++ using similar code to what you can see around the invoke method.
This is my c++ code to call mlir. But in my case, I can’t use mlir_ciface_forward to define this mlir function. It will be undefined reference. When I use forward to name this mlir function, it will be right. Are there any errors in my lower pipeline or c++ code? Thank you for your help.
It looks like you’re calling the forward function directly instead of calling a c-wrapper around it. When you look at the generated LLVMIR, your function signature looks something like this: llvm.func @forward(%arg0: !llvm.ptr, %arg1: !llvm.ptr, %arg2: i64, %arg3: i64, %arg4: i64, %arg5: i64, %arg6: i64, %arg7: !llvm.ptr, %arg8: !llvm.ptr, %arg9: i64, %arg10: i64, %arg11: i64, %arg12: i64, %arg13: i64) -> !llvm.struct<(ptr, ptr, i64, array<3 x i64>, array<3 x i64>)>
So all of the struct members of the memref descriptors have been promoted to function arguments. Your extern declaration looks very different.
That’s because no wrapper is generated for your function. When you add the attributes {llvm.emit_c_interface} to your func::FuncOp, that should happen. The last caveat is that because your func::FuncOp returns a memref, this will not show up in the same way in c-wrapper. Instead, the pointer to the corresponding descriptor gets turned into the first function argument.
OK, I see. Thank you for your help. To add this attributes {llvm.emit_c_interface} to mlir function, I should how to change my lower pipeline. It seems like my current lower pipeline can’t add this to my mlir function
Yes, you could either add it by hand or use the --llvm-request-c-wrappers pass that you are already using. But you have to use it before --convert-func-to-llvm otherwise it will add the attribute after the lowering and you will not get a wrapper.