<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/57002>57002</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[flang] Derived type passed by value (bind(C) with value attribute: interoperable with C)
</td>
</tr>
<tr>
<th>Labels</th>
<td>
flang:ir
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
PeixinQiao
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
PeixinQiao
</td>
</tr>
</table>
<pre>
## Example
```
$ cat test.f90
program main
type, bind(c) :: t1
real :: i = -1.
integer :: j = -2
end type t1
type, bind(c) :: t2
type(t1) :: t1_obj1
integer(8) :: x = 1
end type t2
type, bind(c) :: t3
type(t1) :: t1_obj2
integer(8) :: x(2) = [1, 2]
end type t3
type(t1) :: my_t1
type(t2) :: my_t2
type(t3) :: my_t3
INTERFACE
subroutine c_func(c_t1, c_t2, c_t3) BIND(C, NAME='c_func_')
import :: t1, t2, t3
type(t1), value :: c_t1
type(t2), value :: c_t2
type(t3), value :: c_t3
END
END INTERFACE
call c_func(my_t1, my_t2, my_t3)
end
```
```
$ cat func.c
#include<stdio.h>
struct Ty1 {
float i;
int j;
};
struct Ty2 {
struct Ty1 bt1;
__int64_t x;
};
struct Ty3 {
struct Ty1 bt2;
__int64_t x[2];
};
void c_func_(struct Ty1 t1, struct Ty2 t2, struct Ty3 t3) {
printf("%.2f\t", t1.i);
printf("%d\n", t1.j);
printf("%.2f\t", t2.bt1.i);
printf("%d\t", t2.bt1.j);
printf("%ld\n", t2.x);
printf("%.2f\t", t3.bt2.i);
printf("%d\t", t3.bt2.j);
printf("%ld\t", t3.x[0]);
printf("%ld\t", t3.x[1]);
printf("\n");
}
```
```
$ cat run.sh
#!/bin/bash
clang $1 -c -o C-obj.o
flang-new $2 -c -o F-obj.o
flang-new -flang-experimental-exec C-obj.o F-obj.o -o a.exe
./a.exe && rm C-obj.o F-obj.o a.exe
$ bash run.sh func.c test.f90
-1.00 -2
-1.00 -2 1
-1.00 -2 1 2
```
## Initial support
https://reviews.llvm.org/D131391
## Problem
ABI is complex
architecture and OS dependent
## How to do it?
ABI in clang: (e.g.: AArch64) https://github.com/llvm/llvm-project/blob/bbf19005714b2f6f957cac25e44d381167843732/clang/lib/CodeGen/TargetInfo.cpp#L5489-L5533
Classify argument in AArch64: https://github.com/llvm/llvm-project/blob/bbf19005714b2f6f957cac25e44d381167843732/clang/lib/CodeGen/TargetInfo.cpp#L5750
## Questions
1. Is this the right direction?
2. What arch && OS are in high priority? AArch64 && X86_64 linux?
3. Are there any functions to compute the size and alignment of FIR Record Type (derived type)?
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzNV1uPqzYQ_jXkxQrCdrg95CGbbNpIp9vT0yO1b5EBE7wiGBmzm_TXd4yBkGx2u_tWCQVfvvnm4pnBSWR2XjqEwoMeT-xYl9zxNo63cgKvf-yULFDKNNK80W4e96u1kgfFjujIRGVXENLnmjtkjRJRZQ6JUofEyKEreJDGAwghxVk5rAsYbNAcu5dtUWl-4GpAPFsEGQC8yjpFE8qP9JILsYVFIDe1ay-TZ_xGO-CiCezUGYHf2kA-YwP9jA3kP22AObHzDXL8B2zUEcffvLWKXlt1re543t-GLtLkBkBuAfQG0KsYYLunn48_tqv148UL1LSJkq0WFUfpPm-r1MTF6Aa7U6PCvjvmh93TBrbXZu1p9dsj-OiQ0IrtYQSYCTOE6FhLpSfZBXKWcRrtm4Cb7RdWtnyQS_fTvETX4biHJnfR9B301JLHp_GcYPg2Xikry0uY7BkBpT2LfkDHKMBR36_V-6VrWN10WKOiSss24w5dNzoT0i0c2tvRaNWmGv08Y-SED4NteSmBRDh0XIEMRc_j3Ak343hkIFOGCW8Cjl2I9nugChZ7Den9IR19l468Q-c_dMVxj9X-vkiRoTHFogmpjf3EFXsGE2P6eriYVCvQnAONQwDruyR3_LXuJpCT2BXm7Oh78AzA1QX8_CH4hpu4yaf4bwQ-1lFeWUTc01csoqCAfMUiK_AJiyYS5o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