C interoperable multiplatform representation of complex type in LLVM

As part of our LFortran compiler development, we need to be able to interface with C, including the complex type. In LLVM the complex type is represented as {float, float} and {double, double}. However, when passing these to C functions and returning from C functions, one must rewrite them into one of <2 x float>, i64, pass by reference, etc., and it is different on every platform (different on Windows / MSVC, Linux, macOS Intel and macOS ARM).

We currently do this in the frontend, based on the target architecture. This design adds complexity to the front end. It seems this is a really low level detail that would be best handled by LLVM itself.

Is there another solution that would be simpler? Would it make sense to try to write an LLVM pass that takes {float, float} and converts into one of the target specific C ABI conventions for passing structures by value?

Related issues and discussions:

Isn’t this akin to address the long lasting issue of handling C ABI in general in LLVM instead of Clang? (but it is highly coupled to Clang at the moment I believe).

1 Like

I think this is exactly the issue. I am not directly involved in the LLVM development, so I didn’t know it was a long lasting issue. Are there some pointers/links to this issue, besides the ones I sent above?

And more importantly, what is the latest best idea how to fix that?