| Issue |
240034
|
| Summary |
[flang] BIND(C) calls pass a hidden length argument for CHARACTER dummies
|
| Labels |
flang
|
| Assignees |
|
| Reporter |
mms-it-ch
|
**Version:** flang 23.1.2 (release) and `main` (34d6a1887f; `flang/lib/Optimizer/CodeGen/TargetRewrite.cpp` is unchanged up to 635f30899d).
### Reproducer
```fortran
subroutine s(h)
use iso_c_binding
interface
type(c_funptr) function dlsym(handle, name) bind(c, name='dlsym')
import :: c_ptr, c_funptr, c_char
type(c_ptr), value :: handle
character(kind=c_char), intent(in) :: name(*)
end function
end interface
type(c_ptr) :: h
type(c_funptr) :: fp
common /k/ fp
fp = dlsym(h, 'f' // c_null_char)
end subroutine
```
`flang -O1 -S -emit-llvm` on x86-64 gives
```llvm
%6 = call ptr @dlsym(ptr %4, ptr nonnull %2, i64 2)
declare ptr @dlsym(ptr, ptr, i64) local_unnamed_addr
```
The C function has two parameters; the third `i64` is the length of the CHARACTER actual argument. The same happens for `dlopen`, `setenv`, `strlen` etc. and for definitions: a BIND(C) function with a `character(kind=c_char) :: s(*)` dummy is defined with a trailing `i64` parameter that a C caller does not pass.
### Cause
Lowering passes every CHARACTER dummy that is not passed by descriptor as `!fir.boxchar`, also for BIND(C) procedures, and TargetRewrite splits each `boxchar` into the address and a trailing length, without looking at BIND(C).
For an interoperable CHARACTER dummy the length comes from its declaration (length 1, or a specification _expression_); assumed length is passed by descriptor. So the hidden length is never needed for a BIND(C) procedure.
With the usual C calling conventions the extra argument is harmless (a C callee ignores it, and a Fortran definition does not read it), but the calls and declarations do not match the C prototypes.
Related: #59881 (BIND(C) CHARACTER arguments in general).
I have a patch and will open a PR.
---
*Drafted with the help of Claude Code (Anthropic), in line with the LLVM AI Tool Use Policy. The reproducer was checked with flang 23.1.2 on x86-64 and with a build of `main` 34d6a1887f for s390x.*
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs