llvm-project
698bb5f2 - [mlir][docs] Add C example for C-compatible wrapper for LLVM IR (#120955)

Commit
277 days ago
[mlir][docs] Add C example for C-compatible wrapper for LLVM IR (#120955) `TargetLLVMIR` documentation introduced the C-compatible wrapper function for a MLIR function and ways to generate it, but did not demonstrate the corresponding C function signature for them. The C function signature is not obvious, in that * `MemrefDescriptor` should be passed as _pointer_. + For example, MLIR function could return a new Descriptor, so pointer is a must. + Surprisingly, directly pass the struct, by C convention, is also a pointer so some function will work, but that is implicit and error-prone. * for `@foo() -> memref<>`, the return type becomes the first argument in `_mlir_ciface_foo(%arg0: !llvm.ptr)`. + This is described in https://github.com/llvm/llvm-project/blob/f70ab7d909d6861c7eec5ab40679bde16ab826c6/mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp#L110-L167 Especially by code `size_t argOffset = resultStructType ? 1 : 0;` saying the actual argument starts at 1 when result is a struct (memref) Users using the wrong signature will get incorrect results. LLVM discourse has some example of it * https://discourse.llvm.org/t/how-to-compile-and-link-with-other-c-c-programs/4835/10 * https://discourse.llvm.org/t/segmentation-fault-on-memref-store/80286/3 * https://discourse.llvm.org/t/memref-store-storing-a-memref-load/80307 Cc @ftynse for relevent commit history. Cc @charitha22 and @Wheest from discourse post.
Parents
Loading