[flang-commits] [flang] [RFC][flang] Trampolines for internal procedures. (PR #66157)

Slava Zakharin via flang-commits flang-commits at lists.llvm.org
Tue Sep 26 14:45:36 PDT 2023


================
@@ -0,0 +1,373 @@
+<!--===- docs/InternalProcedureTrampolines.md
+
+   Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+   See https://llvm.org/LICENSE.txt for license information.
+   SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+-->
+
+# Trampolines for pointers to internal procedures.
+
+## Overview
+
+```fortran
+subroutine host()
+  integer :: local = 10
+  call internal()
+  return
+
+  contains
+  subroutine internal()
+    print *, local
+  end subroutine internal
+end subroutine host
+```
+
+Procedure code generated for subprogram `inernal()` must have access to the scope of
+its host procedure, e.g. to access `local` variable. Flang achieves this by passing
+an extra argument to `internal()` that is a tuple of references to all variables
----------------
vzakhari wrote:

I think so.  In particular, it is possible inside an internal procedure to use a procedure pointer via a host association, where the procedure pointer resolves to an address of another (or the same) internal procedure.  For example:
```
module other
  abstract interface
     function callback()
       integer :: callback
     end function callback
  end interface
contains
  subroutine foo(fptr)
    procedure(callback), pointer :: fptr
    ! `fptr` is pointing to `callee1`, which needs the static chain link.
    print *, fptr()
  end subroutine foo
end module other

subroutine host(local)
  use other
  integer :: local
  procedure(callback), pointer :: fptr1
  procedure(callback), pointer :: fptr2
  fptr1 => callee1
  fptr2 => callee2
  call foo(fptr1)
  return
contains
  function callee1()
    integer :: callee1
    callee1 = fptr2()
  end function callee1
  function callee2()
    integer :: callee2
    callee2 = local
  end function callee2
end subroutine host
program main
  call host(10)
end program main
```

https://github.com/llvm/llvm-project/pull/66157


More information about the flang-commits mailing list