[flang] trampolines created inside internal procedures should have the lifetime of the host procedure
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
Currently, trampolines created when assigning an internal procedure to a pointer are created on the stack of the procedure that contains the pointer assignment.
When the procedure pointer assignment happens inside an internal procedure, this is not in line with the Fortran requirements that the lifetime of the pointer target is the one of the host procedure (not the internal procedure).
For instance, when compiled at -O0, the following program will segfault.
```fortran
module m
implicit none
procedure(), pointer :: proc_ptr
contains
subroutine host_procedure()
integer :: x
x = 1
call internal_proc1()
print *, "covering the stack from previous internal_proc1 call"
x = 2
call proc_ptr()
contains
subroutine internal_proc1()
proc_ptr => internal_proc2
end subroutine
subroutine internal_proc2()
print *, x
end subroutine
end subroutine
end module
use m
call host_procedure()
end
```
A potential solution is to create the trampolines used inside internal procedures in the host and to pass them via the hidden argument (as if they were extra variables from the host being addressed in internal procedures).
Contributor guide
Research direction
Start by compiling the Fortran reproducer at -O0 and confirm the procedure-pointer call can segfault after internal_proc1 returns. Then trace Flang's trampoline creation for assignments inside internal procedures; done means the trampoline remains valid for the host_procedure lifetime and the example prints the expected value without crashing.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- fortran
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100