Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FCall macro and/or Fortran calling convention #38872

Open
vchuravy opened this issue Dec 14, 2020 · 10 comments
Open

FCall macro and/or Fortran calling convention #38872

vchuravy opened this issue Dec 14, 2020 · 10 comments
Labels
feature Indicates new feature / enhancement requests ffi foreign function interfaces, ccall, etc. help wanted Indicates that a maintainer wants help on an issue or pull request

Comments

@vchuravy
Copy link
Member

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: JuliaLang/LinearAlgebra.jl#650

@vchuravy vchuravy added help wanted Indicates that a maintainer wants help on an issue or pull request feature Indicates new feature / enhancement requests labels Dec 14, 2020
@ViralBShah
Copy link
Member

You mean as discussed in #2167 😂

@simonbyrne
Copy link
Contributor

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

@ViralBShah
Copy link
Member

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
Copy link
Contributor

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
Copy link
Member

@stevengj 's comment in a flexiblas issue is good to link here: mpimd-csc/flexiblas#12 (comment)

@stevengj
Copy link
Member

stevengj commented Dec 15, 2020

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
Copy link
Contributor

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

@stevengj
Copy link
Member

stevengj commented Dec 15, 2020

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
Copy link
Member

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

@StefanKarpinski StefanKarpinski added the ffi foreign function interfaces, ccall, etc. label Mar 31, 2021
@ViralBShah
Copy link
Member

This literally only comes up in the BLAS dot product:

JuliaLinearAlgebra/libblastrampoline#56

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Indicates new feature / enhancement requests ffi foreign function interfaces, ccall, etc. help wanted Indicates that a maintainer wants help on an issue or pull request
Projects
None yet
Development

No branches or pull requests

5 participants