Skip to content

[flang] handle intrinsic interfaces in FunctionRef::GetType #89583

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

Merged
merged 2 commits into from
Apr 23, 2024

Conversation

jeanPerier
Copy link
Contributor

User functions may be declared with an interface that is a specific intrinsic. In such case, there is no result type available from the procedure symbol (at least without using evaluate::Probe), and FunctionRef::GetType() returned nullopt. This caused lowering to crash.
The result type of specific intrinsic procedures is always a lengthless intrinsic type, so it is fully defined in the template argument of FunctionRef. Use it.

User functions may be declared with an interface that is a specific intrinsic.
In such case, there is no result type available from the procedure
symbol (at least without using evaluate::Probe), and FunctionRef::GetType()
returned nullopt. This caused lowering to crash.
The result type of specific intrinsic procedures is always a lengthless
intrinsic type, so it is fully defined in the template argument of
FunctionRef. Use it.
@jeanPerier jeanPerier requested a review from klausler April 22, 2024 09:34
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir flang:semantics labels Apr 22, 2024
@llvmbot
Copy link
Member

llvmbot commented Apr 22, 2024

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

@llvm/pr-subscribers-flang-semantics

Author: None (jeanPerier)

Changes

User functions may be declared with an interface that is a specific intrinsic. In such case, there is no result type available from the procedure symbol (at least without using evaluate::Probe), and FunctionRef::GetType() returned nullopt. This caused lowering to crash.
The result type of specific intrinsic procedures is always a lengthless intrinsic type, so it is fully defined in the template argument of FunctionRef. Use it.


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

2 Files Affected:

  • (modified) flang/include/flang/Evaluate/call.h (+12-8)
  • (modified) flang/test/Lower/HLFIR/calls-f77.f90 (+13)
diff --git a/flang/include/flang/Evaluate/call.h b/flang/include/flang/Evaluate/call.h
index 3d766bc08e58d4..7317ab099c1cc4 100644
--- a/flang/include/flang/Evaluate/call.h
+++ b/flang/include/flang/Evaluate/call.h
@@ -287,15 +287,19 @@ template <typename A> class FunctionRef : public ProcedureRef {
       : ProcedureRef{std::move(p), std::move(a)} {}
 
   std::optional<DynamicType> GetType() const {
-    if (auto type{proc_.GetType()}) {
-      // TODO: Non constant explicit length parameters of PDTs result should
-      // likely be dropped too. This is not as easy as for characters since some
-      // long lived DerivedTypeSpec pointer would need to be created here. It is
-      // not clear if this is causing any issue so far since the storage size of
-      // PDTs is independent of length parameters.
-      return type->DropNonConstantCharacterLength();
+    if constexpr (IsLengthlessIntrinsicType<A>) {
+      return A::GetType();
+    } else {
+      if (auto type{proc_.GetType()}) {
+        // TODO: Non constant explicit length parameters of PDTs result should
+        // likely be dropped too. This is not as easy as for characters since
+        // some long lived DerivedTypeSpec pointer would need to be created
+        // here. It is not clear if this is causing any issue so far since the
+        // storage size of PDTs is independent of length parameters.
+        return type->DropNonConstantCharacterLength();
+      }
+      return std::nullopt;
     }
-    return std::nullopt;
   }
 };
 } // namespace Fortran::evaluate
diff --git a/flang/test/Lower/HLFIR/calls-f77.f90 b/flang/test/Lower/HLFIR/calls-f77.f90
index ac5be007eb838c..cefe379a45d353 100644
--- a/flang/test/Lower/HLFIR/calls-f77.f90
+++ b/flang/test/Lower/HLFIR/calls-f77.f90
@@ -186,3 +186,16 @@ subroutine alternate_return_call(n1, n2, k)
   ! CHECK: ^[[block2]]: // pred: ^bb0
 7 k =  1; return
 end
+
+! -----------------------------------------------------------------------------
+!     Test calls to user procedures with intrinsic interfaces
+! -----------------------------------------------------------------------------
+
+! CHECK-NAME: func.func @_QPintrinsic_iface()
+subroutine intrinsic_iface()
+  intrinsic acos
+  real :: x
+  procedure(acos) :: proc
+  x = proc(1.0)
+end subroutine
+! CHECK" fir.call @_QPproc(%{{.*}}) {{.*}}: (!fir.ref<f32>) -> f32

@jeanPerier jeanPerier merged commit 35159c2 into llvm:main Apr 23, 2024
@jeanPerier jeanPerier deleted the jp-funcref-gettype branch April 23, 2024 07:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants