llvm / llvm/llvm-project

[flang] Companion processor can differ from C

Open
#172,456 12 comments 0 reactions 0 assignees View on GitHub
flang
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

In some cases, it may be desirable to provide direct bindings to C++ or other languages that use mangling schemes different from C. Another use case is binding to specific symbol versions that include additional prefixes or suffixes. For example, Apple's framework Accelerate has the following declaration for `ddot` function in ILP64 mode:
```fortran
interface
pure function ddot(n,dx,incx,dy,incy) bind(C, name="ddot$NEWLAPACK$ILP64")
import
integer(c_int64_t), intent(in) :: n
real(c_double), intent(in) :: dx(*)
integer(c_int64_t), intent(in) :: incx
real(c_double), intent(in) :: dy(*)
integer(c_int64_t), intent(in) :: incy
real(c_double) :: ddot
end function ddot
end interface
```
while traditional interface is:
```fortran
interface
pure function ddot(n,dx,incx,dy,incy) bind(C, name="")
import
integer(c_int64_t), intent(in) :: n
real(c_double), intent(in) :: dx(*)
integer(c_int64_t), intent(in) :: incx
real(c_double), intent(in) :: dy(*)
integer(c_int64_t), intent(in) :: incy
real(c_double) :: ddot
end function ddot
end interface
```

At the current moment, flang does not accept such names: https://godbolt.org/z/YvM5nYExz

8.5.5 from Fortran 2023 standard specifies that
> If the value of the `scalar-default-char-constant-expr` after discarding leading and trailing blanks has nonzero length, it shall be valid as an identifier on the `companion processor`.

And `companion processor`, according to 5.5.7,
> might or might not be a mechanism that conforms to the requirements of ISO/IEC 9899:2018.

Therefore, enforcing conformance to valid C identifier rules is too restrictive.

Note: let me fix this, it should be straightforward.

Contributor guide

Open the contributing guide

Research direction

Start with the linked Compiler Explorer reproducer and the Fortran 2023 requirements in 8.5.5 and 5.5.7. Trace flang's handling of the BIND(C, NAME=...) character expression; done means it accepts valid companion-processor names that are not valid C identifiers, including the shown Accelerate symbol.

Written by the indexing model from the issue text.

Assessment

Tech stack
fortran
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.