public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug fortran/110825] New: TYPE(*) dummy argument to generate an unused hidden argument
@ 2023-07-26 20:17 kargl at gcc dot gnu.org
  2023-07-26 20:17 ` [Bug fortran/110825] " kargl at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: kargl at gcc dot gnu.org @ 2023-07-26 20:17 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110825

            Bug ID: 110825
           Summary: TYPE(*) dummy argument to generate an unused hidden
                    argument
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kargl at gcc dot gnu.org
  Target Milestone: ---

The was pointed out in Fortran Discourse,

https://fortran-lang.discourse.group/t/strange-behaviour-when-using-type-and-character-in-gfortran/6233

that gfortran generates wrong code for a valid program.  Here's a slightly
modified version of the program.

  program foo

    implicit none

    character(100) :: not_used
    call sub(not_used, "123")

    contains

    subroutine sub(useless_var, print_this)
        type(*), intent(in)      :: useless_var
        character(*), intent(in) :: print_this
        if (len_trim(print_this) /= 3) stop 1
    end subroutine sub

  end

The subroutine reference in the main program is generating a call with two
hidden arguments.  From -fdump-tree-original, 

void MAIN__ ()
{
  static void sub (void * & restrict, character(kind=1)[1:] & restrict,
integer(kind=8));
  character(kind=1) not_used[1:100];

  sub (&not_used, &"123"[1]{lb: 1 sz: 1}, 100, 3);
}

we have hidden arguments 100 and 3.  However, the code generated for the
contained subroutine is only expecting one hidden argument.

__attribute__((fn spec (". r r ")))
void sub (void * & restrict useless_var,
   character(kind=1)[1:_print_this] & restrict print_this,
   integer(kind=8) _print_this)
{
...
}

Thus, when compiled and executed the program hits 'STOP 1'.  Likely, gfortran
needs to add a possibly unused hidden argument to the argument list for a
TYPE(*) dummy argument.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug fortran/110825] TYPE(*) dummy argument to generate an unused hidden argument
  2023-07-26 20:17 [Bug fortran/110825] New: TYPE(*) dummy argument to generate an unused hidden argument kargl at gcc dot gnu.org
@ 2023-07-26 20:17 ` kargl at gcc dot gnu.org
  2023-07-26 20:54 ` anlauf at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: kargl at gcc dot gnu.org @ 2023-07-26 20:17 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110825

kargl at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
           Priority|P3                          |P4
     Ever confirmed|0                           |1
      Known to fail|                            |13.1.1, 14.0
   Last reconfirmed|                            |2023-07-26

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug fortran/110825] TYPE(*) dummy argument to generate an unused hidden argument
  2023-07-26 20:17 [Bug fortran/110825] New: TYPE(*) dummy argument to generate an unused hidden argument kargl at gcc dot gnu.org
  2023-07-26 20:17 ` [Bug fortran/110825] " kargl at gcc dot gnu.org
@ 2023-07-26 20:54 ` anlauf at gcc dot gnu.org
  2023-07-26 21:18 ` anlauf at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: anlauf at gcc dot gnu.org @ 2023-07-26 20:54 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110825

--- Comment #1 from anlauf at gcc dot gnu.org ---
(In reply to kargl from comment #0)
> Thus, when compiled and executed the program hits 'STOP 1'.  Likely,
> gfortran needs to add a possibly unused hidden argument to the argument list
> for a TYPE(*) dummy argument.

I guess it should be the other way around: if the dummy argument is TPYE(*),
we only pass a reference to the actual argument, but no hidden argument,
as is the case for bind(c).  This is also what the called procedure expects.
What if the caller passed a type other than character?

So we just need to update the caller (the "monster" gfc_conv_procedure_call).

Do you have an idea what would be the use of the hidden argument here anyway?

As TYPE(*) is rather new, and I did not find any mentioning of it in the
ABI specification, we should clarify what we want and also document it at

https://gcc.gnu.org/onlinedocs/gfortran/Argument-passing-conventions.html

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug fortran/110825] TYPE(*) dummy argument to generate an unused hidden argument
  2023-07-26 20:17 [Bug fortran/110825] New: TYPE(*) dummy argument to generate an unused hidden argument kargl at gcc dot gnu.org
  2023-07-26 20:17 ` [Bug fortran/110825] " kargl at gcc dot gnu.org
  2023-07-26 20:54 ` anlauf at gcc dot gnu.org
@ 2023-07-26 21:18 ` anlauf at gcc dot gnu.org
  2023-07-26 22:00 ` sgk at troutmask dot apl.washington.edu
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: anlauf at gcc dot gnu.org @ 2023-07-26 21:18 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110825

anlauf at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |anlauf at gcc dot gnu.org

--- Comment #2 from anlauf at gcc dot gnu.org ---
Untested fix:

diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc
index ef3e6d08f78..a1eac8e611e 100644
--- a/gcc/fortran/trans-expr.cc
+++ b/gcc/fortran/trans-expr.cc
@@ -7521,6 +7521,7 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
          && !(fsym && fsym->ts.type == BT_DERIVED && fsym->ts.u.derived
               && fsym->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR
               && fsym->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING )
+         && !(fsym && fsym->ts.type == BT_ASSUMED)
          && !(fsym && UNLIMITED_POLY (fsym)))
        vec_safe_push (stringargs, parmse.string_length);

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug fortran/110825] TYPE(*) dummy argument to generate an unused hidden argument
  2023-07-26 20:17 [Bug fortran/110825] New: TYPE(*) dummy argument to generate an unused hidden argument kargl at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2023-07-26 21:18 ` anlauf at gcc dot gnu.org
@ 2023-07-26 22:00 ` sgk at troutmask dot apl.washington.edu
  2023-07-27 19:41 ` anlauf at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: sgk at troutmask dot apl.washington.edu @ 2023-07-26 22:00 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110825

--- Comment #3 from Steve Kargl <sgk at troutmask dot apl.washington.edu> ---
On Wed, Jul 26, 2023 at 08:54:01PM +0000, anlauf at gcc dot gnu.org wrote:
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110825
> 
> --- Comment #1 from anlauf at gcc dot gnu.org ---
> (In reply to kargl from comment #0)
> > Thus, when compiled and executed the program hits 'STOP 1'.  Likely,
> > gfortran needs to add a possibly unused hidden argument to the argument list
> > for a TYPE(*) dummy argument.
> 
> I guess it should be the other way around: if the dummy argument is TPYE(*),
> we only pass a reference to the actual argument, but no hidden argument,
> as is the case for bind(c).  This is also what the called procedure expects.

I had not thought about how gfortran handles bind(c).  In my codes that
use bind(c), I use an array with character(len=1) with the last element
set to c_null_char.  On the C side, it looks like a C null-terminated
string. 

I don't use type(*) in my codes, so I'm unsure how, or even if,
the length type parameter would/(need to) be passed along.

> What if the caller passed a type other than character?

That's why I wrote '***possibly unused*** hidden argument'

> So we just need to update the caller (the "monster" gfc_conv_procedure_call).
> 
> Do you have an idea what would be the use of the hidden argument here anyway?

Hmmm, a scan of F2018 suggest that type(*) has rather limited uses

   C710  An assumed-type variable name shall not appear in a designator
      or expression except as an actual argument corresponding to a dummy
      argument that is assumed-type, or as the first argument to the
      intrinsic function IS_CONTIGUOUS, LBOUND, PRESENT, RANK, SHAPE, SIZE,
      or UBOUND, or the function C_LOC from the intrinsic module ISO_C_BINDING.

I suspect that a hidden argument isn't needed.  This means
that gfc_conv_procedure_call will need to suppress a hidden
argument for a type(*) dummy argument when the actual argument
is of character type.

> As TYPE(*) is rather new, and I did not find any mentioning of it in the
> ABI specification, we should clarify what we want and also document it at
> 
> https://gcc.gnu.org/onlinedocs/gfortran/Argument-passing-conventions.html

Sorry, I cannot be of much help.  type(*) is well off my radar for
my owns codes.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug fortran/110825] TYPE(*) dummy argument to generate an unused hidden argument
  2023-07-26 20:17 [Bug fortran/110825] New: TYPE(*) dummy argument to generate an unused hidden argument kargl at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2023-07-26 22:00 ` sgk at troutmask dot apl.washington.edu
@ 2023-07-27 19:41 ` anlauf at gcc dot gnu.org
  2023-07-28 18:40 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: anlauf at gcc dot gnu.org @ 2023-07-27 19:41 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110825

anlauf at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |anlauf at gcc dot gnu.org

--- Comment #4 from anlauf at gcc dot gnu.org ---
Submitted: https://gcc.gnu.org/pipermail/fortran/2023-July/059658.html

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug fortran/110825] TYPE(*) dummy argument to generate an unused hidden argument
  2023-07-26 20:17 [Bug fortran/110825] New: TYPE(*) dummy argument to generate an unused hidden argument kargl at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2023-07-27 19:41 ` anlauf at gcc dot gnu.org
@ 2023-07-28 18:40 ` cvs-commit at gcc dot gnu.org
  2023-07-30 18:28 ` cvs-commit at gcc dot gnu.org
  2023-08-17 20:28 ` anlauf at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-07-28 18:40 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110825

--- Comment #5 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:02f4ca0df2d69b922a622e7cc9b396cf686d5a0f

commit r14-2856-g02f4ca0df2d69b922a622e7cc9b396cf686d5a0f
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Thu Jul 27 21:30:26 2023 +0200

    Fortran: do not pass hidden character length for TYPE(*) dummy [PR110825]

    gcc/fortran/ChangeLog:

            PR fortran/110825
            * gfortran.texi: Clarify argument passing convention.
            * trans-expr.cc (gfc_conv_procedure_call): Do not pass the
character
            length as hidden argument when the declared dummy argument is
            assumed-type.

    gcc/testsuite/ChangeLog:

            PR fortran/110825
            * gfortran.dg/assumed_type_18.f90: New test.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug fortran/110825] TYPE(*) dummy argument to generate an unused hidden argument
  2023-07-26 20:17 [Bug fortran/110825] New: TYPE(*) dummy argument to generate an unused hidden argument kargl at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2023-07-28 18:40 ` cvs-commit at gcc dot gnu.org
@ 2023-07-30 18:28 ` cvs-commit at gcc dot gnu.org
  2023-08-17 20:28 ` anlauf at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-07-30 18:28 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110825

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-13 branch has been updated by Harald Anlauf
<anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:63d18dffeb34e58a8ef79ca614f854e055355043

commit r13-7656-g63d18dffeb34e58a8ef79ca614f854e055355043
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Thu Jul 27 21:30:26 2023 +0200

    Fortran: do not pass hidden character length for TYPE(*) dummy [PR110825]

    gcc/fortran/ChangeLog:

            PR fortran/110825
            * gfortran.texi: Clarify argument passing convention.
            * trans-expr.cc (gfc_conv_procedure_call): Do not pass the
character
            length as hidden argument when the declared dummy argument is
            assumed-type.

    gcc/testsuite/ChangeLog:

            PR fortran/110825
            * gfortran.dg/assumed_type_18.f90: New test.

    (cherry picked from commit 02f4ca0df2d69b922a622e7cc9b396cf686d5a0f)

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug fortran/110825] TYPE(*) dummy argument to generate an unused hidden argument
  2023-07-26 20:17 [Bug fortran/110825] New: TYPE(*) dummy argument to generate an unused hidden argument kargl at gcc dot gnu.org
                   ` (6 preceding siblings ...)
  2023-07-30 18:28 ` cvs-commit at gcc dot gnu.org
@ 2023-08-17 20:28 ` anlauf at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: anlauf at gcc dot gnu.org @ 2023-08-17 20:28 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110825

anlauf at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|ASSIGNED                    |RESOLVED
   Target Milestone|---                         |13.3

--- Comment #7 from anlauf at gcc dot gnu.org ---
Fixed on mainline for gcc-14, and backported to 13-branch.  Closing.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2023-08-17 20:28 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-26 20:17 [Bug fortran/110825] New: TYPE(*) dummy argument to generate an unused hidden argument kargl at gcc dot gnu.org
2023-07-26 20:17 ` [Bug fortran/110825] " kargl at gcc dot gnu.org
2023-07-26 20:54 ` anlauf at gcc dot gnu.org
2023-07-26 21:18 ` anlauf at gcc dot gnu.org
2023-07-26 22:00 ` sgk at troutmask dot apl.washington.edu
2023-07-27 19:41 ` anlauf at gcc dot gnu.org
2023-07-28 18:40 ` cvs-commit at gcc dot gnu.org
2023-07-30 18:28 ` cvs-commit at gcc dot gnu.org
2023-08-17 20:28 ` anlauf at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).