Dear all,

the attached proposed patch fixes PR120099 by modifying
gfc_return_by_reference so that it returns true with -ff2c
also for intrinsics returning complex numbers, as these are
not pure in the GCC IR sense, and wrapper functions for the
intrinsics were optimized out by DCE.

The change only affects compilation with -ff2c, so I guess
there will be only few people able to test any performance
impact on real-world code...

Regtested on x86_64-pc-linux-gnu with testcases only that
use the -ff2c flag explicitly.

OK for mainline?

Thanks,
Harald

From 65d7c6efe51371ba4d0681fc2fa0e732b70b70d7 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anl...@gmx.de>
Date: Sun, 18 May 2025 22:42:26 +0200
Subject: [PATCH] Fortran: fix FAIL of gfortran.dg/specifics_1.f90 after
 r16-372 [PR120099]

After commit r16-372, testcase gfortran.dg/specifics_1.f90 started to
FAIL at -O2 and higher, as DCE lead to elimination of evaluations of
Fortran specific intrinsics returning complex results and with -ff2c.
As the Fortran runtime library is compiled with -fno-f2c, the frontend
generates calls to wrapper subroutines _gfortran_f2c_specific_* that
return their result by reference via their first argument when this is
needed.  This is e.g. the case when specific names of the intrinsics are
used for passing as actual argument to procedures.  These wrappers are
not pure in the GCC IR sense, even if the Fortran intrinsics are.
Therefore gfc_return_by_reference must return true for these.

	PR fortran/120099

gcc/fortran/ChangeLog:

	* trans-types.cc (gfc_return_by_reference): Intrinsic functions
	returning complex numbers may return their result by reference
	with -ff2c.
---
 gcc/fortran/trans-types.cc | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/gcc/fortran/trans-types.cc b/gcc/fortran/trans-types.cc
index f8980754685..e15b1bb89f0 100644
--- a/gcc/fortran/trans-types.cc
+++ b/gcc/fortran/trans-types.cc
@@ -3231,13 +3231,14 @@ gfc_return_by_reference (gfc_symbol * sym)
 
   /* Possibly return complex numbers by reference for g77 compatibility.
      We don't do this for calls to intrinsics (as the library uses the
-     -fno-f2c calling convention), nor for calls to functions which always
+     -fno-f2c calling convention) except for calls to specific wrappers
+     (_gfortran_f2c_specific_*), nor for calls to functions which always
      require an explicit interface, as no compatibility problems can
      arise there.  */
   if (flag_f2c && sym->ts.type == BT_COMPLEX
       && !sym->attr.pointer
       && !sym->attr.allocatable
-      && !sym->attr.intrinsic && !sym->attr.always_explicit)
+      && !sym->attr.always_explicit)
     return 1;
 
   return 0;
-- 
2.43.0

Reply via email to