Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Flang] Support passing a function that returns procedure pointer as actual corresponding to a procedure dummy. #80891

Merged
merged 2 commits into from
Feb 10, 2024

Conversation

DanielCChen
Copy link
Contributor

@DanielCChen DanielCChen commented Feb 6, 2024

Flang crashes with the following case. The problem is we missed the case when passing a reference to a function that returns a procedure pointer as actual that corresponds to a procedure dummy. This PR is to fix that.

  PROGRAM main
  IMPLICIT NONE
  INTERFACE
    FUNCTION IntF(Arg)
      integer :: Arg, IntF
    END FUNCTION
  END INTERFACE

  INTERFACE
    FUNCTION RetPtr(Arg)
      IMPORT
      PROCEDURE(IntF) :: Arg
      PROCEDURE(IntF), POINTER :: RetPtr
    END FUNCTION
  END INTERFACE

  CALL ModSub(RetPtr(IntF))
  contains
    SUBROUTINE ModSub(Fun1)
    PROCEDURE(IntF) :: Fun1
    END SUBROUTINE
  END

@DanielCChen DanielCChen self-assigned this Feb 6, 2024
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Feb 6, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Feb 6, 2024

@llvm/pr-subscribers-flang-fir-hlfir

Author: Daniel Chen (DanielCChen)

Changes

Flang crashes a with the following case. The problem is we missed the case when passing a reference to a function that returns a procedure pointer as actual that corresponds to a procedure dummy. This PR is to fix that.

  PROGRAM main
  IMPLICIT NONE
  INTERFACE
    FUNCTION IntF(Arg)
      integer :: Arg, IntF
    END FUNCTION
  END INTERFACE

  INTERFACE
    FUNCTION RetPtr(Arg)
      IMPORT
      PROCEDURE(IntF) :: Arg
      PROCEDURE(IntF), POINTER :: RetPtr
    END FUNCTION
  END INTERFACE

  CALL ModSub(RetPtr(IntF))
  contains
    SUBROUTINE ModSub(Fun1)
    PROCEDURE(IntF) :: Fun1
    END SUBROUTINE
  END

Full diff: https://github.com/llvm/llvm-project/pull/80891.diff

1 Files Affected:

  • (modified) flang/lib/Lower/ConvertCall.cpp (+4-2)
diff --git a/flang/lib/Lower/ConvertCall.cpp b/flang/lib/Lower/ConvertCall.cpp
index bb8fd2e945f43..bd6bd81117966 100644
--- a/flang/lib/Lower/ConvertCall.cpp
+++ b/flang/lib/Lower/ConvertCall.cpp
@@ -915,8 +915,10 @@ static PreparedDummyArgument preparePresentUserCallActualArgument(
   // Handle procedure arguments (procedure pointers should go through
   // prepareProcedurePointerActualArgument).
   if (hlfir::isFortranProcedureValue(dummyType)) {
-    // Procedure pointer actual to procedure dummy.
-    if (actual.isProcedurePointer()) {
+    // Procedure pointer or function returns procedure pointer actual to
+    // procedure dummy.
+    if (actual.isProcedurePointer() ||
+        actual.getType().isa<fir::BoxProcType>()) {
       actual = hlfir::derefPointersAndAllocatables(loc, builder, actual);
       return PreparedDummyArgument{actual, /*cleanups=*/{}};
     }

@clementval clementval changed the title [Flag] Support passing a function that returns procedure pointer as actual corresponding to a procedure dummy. [Flang] Support passing a function that returns procedure pointer as actual corresponding to a procedure dummy. Feb 6, 2024
Copy link
Contributor

@jeanPerier jeanPerier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this, nit but otherwise LGTM.

flang/lib/Lower/ConvertCall.cpp Show resolved Hide resolved
@DanielCChen
Copy link
Contributor Author

@jeanPerier Please let me know if you have some other cases in mind. I can try those to confirm if the fix of this PR is good or not. Thanks!

Copy link
Contributor

@jeanPerier jeanPerier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank, LGTM

@DanielCChen DanielCChen merged commit a4ac099 into llvm:main Feb 10, 2024
4 checks passed
@DanielCChen DanielCChen deleted the daniel_pptr branch February 10, 2024 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants