public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Janne Blomqvist <blomqvist.janne@gmail.com>
To: Tobias Burnus <burnus@net-b.de>
Cc: gcc patches <gcc-patches@gcc.gnu.org>, gfortran <fortran@gcc.gnu.org>
Subject: Re: [Patch, Fortran] PR55758 - Non-C_Bool handling with BIND(C)
Date: Tue, 08 Jan 2013 20:07:00 -0000	[thread overview]
Message-ID: <CAO9iq9H_Q8vX--MuDYZbV2ym5s2ur3uh_cJfQY+N=Ax5-vgrzg@mail.gmail.com> (raw)
In-Reply-To: <50E9ABC7.4040705@net-b.de>

On Sun, Jan 6, 2013 at 6:52 PM, Tobias Burnus <burnus@net-b.de> wrote:
> ** ping **
>
> Attached is a small variation, which in addition handles the case that a
> non-BOOL_C LOGICAL, Bind(C) dummy argument (or result variable) is used in a
> procedure call. In that case, the variable is now converted to a
> TYPE_PRECISION == 1 variable. -- The updated patch was build and regtested
> successfully.

Nice, this should fix a pitfall with the previous patch. I still worry
about these almost-but-not-quite logicals causing weird and very hard
to track down bugs. A slightly safer variant of the approach you
describe above would be to convert the variable directly after the
bind(c) procedure call; that should make it pretty fool-proof, AFAICS?
(in some cases that would be a bit of extra useless work, but I doubt
it would matter performance-wise).

> As written before, I believe that the patch avoids some pitfalls with C
> interoperability of logical variables:  On one hand, it improves
> cross-compiler portability by rejecting non C_BOOL ones with
> -std=f2003/f2008/f2008ts;

This part is certainly ok.

> on the other hand, it makes wrong-code issues due
> to using non-0/1 integers from C much less likely. In both cases, the
> type-precision==1 handling for non-BIND(C) Fortran LOGICALs or for Bind(C)
> LOGICAL(kind=C_BOOL) remains the same; hence, no optimization issue is
> caused.
>
>
> OK for the trunk?
>
> Tobias
>
> PS: If there is consensus that this patch is a bad idea, I propose to reject
> non-C_BOOL LOGICALs unconditionally as dummy argument or result variable of
> BIND(C) procedures. Or do you have a better suggestion?
>
>
>
> On December 30, 2012, Tobias Burnus wrote:
>>
>> Janne Blomqvist wrote:
>>>
>>> On Fri, Dec 28, 2012 at 12:31 AM, Tobias Burnus <burnus@net-b.de> wrote:
>>>>
>>>> a) The Fortran standard only defines LOGICAL(kind=C_Bool) as being
>>>> interoperable with C - no other LOGICAL type. That matches GCC: With gcc
>>>> (the C compiler) only _Bool is a BOOLEAN_TYPE with TYPE_PRECISION == 1.
>>>> Hence, this patch rejects other logical kinds as dummy argument/result
>>>> variable in BIND(C) procedures if -std=f2003/f2008/f2008ts is specified
>>>> (using -pedantic, one gets a warning).
>>>
>>> Sorry, I don't understand, what is the -pedantic warning about if it's
>>> already rejected? Or do you mean std=gnu -pedantic?
>>
>>
>> The latter. Actually, I use "gfc_notify_std(GFC_STD_GNU, ..." and just
>> observed the -pedantic result. (I have to admit that I never quite
>> understood - and still don't - what -pedantic exactly does.)
>>
>>>> b) As GNU extension, other logical kinds are accepted in BIND(C)
>>>> procedures;
>>>> however, as the main use of "LOGICAL(kind=4)" (for BIND(C) procedures)
>>>> is to
>>>> handle logical expressions which use C's int, one has to deal with all
>>>> integer values and not only 0 and 1. Hence, a normal integer type is
>>>> used
>>>> internally in that case. That has been done to avoid surprises of users
>>>> and
>>>> hard to trace bugs.
>>>
>>> Does this actually work robustly?
>>
>>
>> I think it does in the sense that it mitigates the problems related to
>> LOGICAL(kind=4) and BIND(C) procedures. No, if one thinks of it as full cure
>> for the problem. The only way to ensure this is to turn all of gfortran's
>> LOGICALs into integers - and even that won't prevent issues related to
>> interoperability with C's _Bool as that one expects only 0 and 1. Thus,
>> either C<->Fortran or Fortran <-> Fortran logical(kind=C_Bool) could still
>> lead to problems.
>>
>>> E.g. if you have a logical but really integer under the covers, what
>>> happens if you equivalence it with a "normal" logical variable.
>>
>>
>> Well, equivalencing of actual arguments / result variables is not allowed
>> (I think, not checked). Besides, if you have equivalenced two variables, if
>> you have set one, you may not access the other, e.g.:
>>
>> logical :: A
>> integer :: B
>> equivalence (A,B)
>> A = .true.
>> B = 1
>> if (A) ...
>>
>> is invalid as "A" is not defined, even if A = .true. and B = 1 have
>> exactly the same storage size and bit patterns and, hence, in practice "A"
>> would be a well defined .true.
>>
>>> Or pass it as an argument to a procedure expecting a normal logical etc.
>>
>>
>> If the value is only 1 or 0, there shouldn't be any problems. Only if one
>> uses in turn ".not. dummy" there might be one.
>>
>> The idea of the patch was only to mitigate the problems - a full cure is
>> not possible (cf. above). I think the most likely problematic code is
>>    if (.not. c_function())
>> which is fixed by the patch. And the hope is that fold-converting to a
>> type-precision=1, Boolean-type logical fixes most of the remaining issues.
>>
>> I think the current solution which only affects non-C_BOOL-kind actual
>> arguments and result variables of BIND(C) procedures is a good compromise.
>>
>> * * *
>>
>> But if others do not like this approach, one could turn the gfc_notify_std
>> into a gfc_error are completely reject logicals with kinds /= C_Bool for
>> dummy arguments/result variables in BIND(C) procedures. Would you prefer
>> that approach?
>>
>> (Doing so will break user code (e.g. Open MPI) and make users unhappy but
>> it will be a tad safer as the current patch.)
>>
>> Tobias
>>
>



-- 
Janne Blomqvist

  parent reply	other threads:[~2013-01-08 20:07 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-27 22:31 Tobias Burnus
2012-12-29 22:11 ` Janne Blomqvist
2012-12-30 10:42   ` Tobias Burnus
2013-01-06 16:52     ` Tobias Burnus
2013-01-06 18:31       ` Steve Kargl
2013-01-08 20:07       ` Janne Blomqvist [this message]
2013-01-08 22:34         ` Tobias Burnus
2013-01-10 19:42           ` Janne Blomqvist
2013-02-26 14:58             ` Tobias Burnus
2013-01-09 16:22         ` Tobias Burnus
2013-01-09 17:23           ` Mikael Morin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAO9iq9H_Q8vX--MuDYZbV2ym5s2ur3uh_cJfQY+N=Ax5-vgrzg@mail.gmail.com' \
    --to=blomqvist.janne@gmail.com \
    --cc=burnus@net-b.de \
    --cc=fortran@gcc.gnu.org \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).