JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.04k stars 5.43k forks source link

FCall macro and/or Fortran calling convention #38872

Open vchuravy opened 3 years ago

vchuravy commented 3 years ago

In conversation with @vtjnash I mentioned that it might make sense to add a calling convention to ccall that implements the GFortran ABI so that we can target different Fortran ABI compilers (see #38836) instead of having it be explicit.

Alternatively a @fcall macro could make sense that converts the arguments to by-ref and handles, characters correctly.

x-ref: #32870

ViralBShah commented 3 years ago

You mean as discussed in #2167 😂

simonbyrne commented 3 years ago

It would also need to handle name mangling, though i'm not sure if that is documented anywhere.

ViralBShah commented 3 years ago

The name mangling tends to be compiler dependent. I guess you could look at the symbols in a shared library and guess the mangling scheme.

simonbyrne commented 3 years ago

I was wondering how CMake did it, and it looks like they literally compile a file with different combinations of names and see what resulting symbols are exported: https://github.com/Kitware/CMake/blob/fd8eef034168a4008d06ecc87b4522cad9636a28/Modules/FortranCInterface/Detect.cmake

ViralBShah commented 3 years ago

@stevengj 's comment in a flexiblas issue is good to link here: https://github.com/mpimd-csc/flexiblas/issues/12#issuecomment-745403270

stevengj commented 3 years ago

I was wondering how CMake did it, and it looks like they literally compile a file with different combinations of names and see what resulting symbols are exported:

Yes, that's also essentially what the AC_F77_WRAPPERS and AC_FC_WRAPPERS macros do in GNU autoconf. (I actually contributed these to autoconf decades ago.) Basically, you only have to check a few possibilities to cover all known compilers: upper/lowercase, with or without appended underscore, and whether to append a second underscore for identifiers containing an underscore.

simonbyrne commented 3 years ago

@stevengj do you know how they handle the string issue? Or is that up to the caller?

stevengj commented 3 years ago

The string issue is up to the caller AFAIK; it seemed pretty hard to write an autoconf-style compilation check to test for this IIRC, so you were stuck with a table of known compilers.

It wasn't a major issue in the past because the most common case of passing strings from C to Fortran was for single-character strings ala LAPACK/BLAS, and not passing the length arguments for single-character strings worked with all common compilers for decades AFAIK until gfortran decided it was important to disallow this (sigh).

I don't know how much diversity there is in string-passing conventions between Fortran compilers; you'd have to do some experiments. I'm guessing that for single-character strings it will be a bit more forgiving, since most compilers probably ignore any length arguments that you push onto the stack.

ViralBShah commented 3 years ago

Should we create a label for ccall or more generally ffi?

ViralBShah commented 2 years ago

This literally only comes up in the BLAS dot product:

https://github.com/JuliaLinearAlgebra/libblastrampoline/issues/56