public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Martin Sebor <msebor@gmail.com>
To: Joseph Myers <joseph@codesourcery.com>
Cc: Marek Polacek <polacek@redhat.com>,
	Gcc Patch List <gcc-patches@gcc.gnu.org>,
	Jason Merrill <jason@redhat.com>
Subject: Re: [PATCH 1/3] improve detection of attribute conflicts (PR 81544)
Date: Thu, 10 Aug 2017 04:47:00 -0000	[thread overview]
Message-ID: <0fc9f24b-f474-3ab5-e660-b206486cc644@gmail.com> (raw)
In-Reply-To: <alpine.DEB.2.20.1708091949180.9262@digraph.polyomino.org.uk>

On 08/09/2017 01:55 PM, Joseph Myers wrote:
> On Wed, 9 Aug 2017, Martin Sebor wrote:
>
>> the same function with the other of this pair attributes.  I'd
>> also be okay with not diagnosing this combination if I could
>> convince myself that it's safe (or can be made safe) and treated
>> consistently.
>
> I'd expect it to be safe; it might simply happen that some calls are
> optimized based on incomplete information (and even that is doubtful, as
> all optimizations except front-end folding should be taking place after
> all the declarations have been seen).

The following is the example I have in mind.  It compiles silently
without the patch but aborts at runtime.  By warning. the patch
makes it clear that the pure attribute is ignored.  (If/when GCC
starts warning about attribute const violations it will also point
out the global read, but that will help only if the definition sees
the const declaration.)

The problem isn't that the declarations aren't merged at the call
site but rather that the middle-end gives const precedence over
pure so when both attributes are provided the former wins.

   int x;

   int __attribute__ ((const, noclone, noinline)) f (int);

   int main (void)
   {
       int i = f (0);
       x = 7;
       i += f (0);
       if (i != 7) __builtin_abort ();
   }

   int __attribute__ ((pure)) f (int i) { return x + i; }

>
>> The problem whose subset the diagnostic detects is declaring
>> the function const and calling it before the pure declaration
>> that corresponds to its definition is seen.  I.e., the inverse
>> of what the ssa-ccp-2.c test does.  I think a mismatch between
>> the attributes is as suspect as a mismatch in the function's
>> signature.
>
> In the ssa-ccp-2.c case, it's not clear to me the test intends to use the
> same function at all; maybe one of the foo9 functions should be renamed.

I think you're right.  It's likely that the intent is to verify
that a call to either a pure or a const function doesn't defeat
constant propagation.  Let me fix the test.  (This also seems
to be an example where the warning has already proved to be
useful -- it has pointed out a weakness in the test.)

> I think it's actually like having a non-prototype declaration and a
> prototype one, where a composite type is formed from two compatible types.
> (We have a warning in the very specific case of a non-prototype
> *definition* followed by a prototype declaration.)
>
>> In any event, it sounds to me that conceptually, it might be
>> useful to be able to specify which of a pair of mutually
>> exclusive (or redundant) attributes to retain and/or accept:
>> the first or the second, and not just whether to accept or
>> drop the most recent one.  That will make it possible to make
>> the most appropriate decision about each specific pair of
>> attributes without impacting any of the others.
>
> If they conflict, I'm not sure there's any basis for making a choice
> specific to a particular pair of attributes.

I'm not sure either because I haven't done a complete survey.
What I do know is that some conflicts are currently ignored
with both attributes accepted (sometimes leading to confusing
symptoms later), others cause hard errors, and others cause
warnings.  This varies depending on whether the conflict
is on the same declaration or on two distinct ones.  So I'm
thinking that generalizing the machinery to make it possible
to express some or most of these scenarios will make it easy
to apply the patch and use it as is most appropriate in each
situation.

>
> In cases like const and pure where one is a subset of the other, it makes
> sense to choose based on the pair of attributes - and not necessarily to
> warn under the same conditions as the warnings for conflicting attributes.

One way to deal with this case might be to warn only if pure
is seen on an already used const function.

Let me think about it some more, work up a new patch, and post
it for consideration.

Martin

  reply	other threads:[~2017-08-10  4:18 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-08 16:14 Martin Sebor
2017-08-09 11:53 ` Marek Polacek
2017-08-09 14:19   ` Martin Sebor
2017-08-09 17:13     ` Joseph Myers
2017-08-09 19:26       ` Martin Sebor
2017-08-09 20:47         ` Joseph Myers
2017-08-10  4:47           ` Martin Sebor [this message]
2017-08-10 23:51             ` Joseph Myers
2017-08-11 16:46               ` Martin Sebor
2017-08-11 16:49                 ` Joseph Myers
2017-08-17 18:23                 ` Martin Sebor
2017-08-24 21:10                   ` Martin Sebor
2017-08-29  5:21                     ` Martin Sebor
2017-09-06 23:30                   ` Joseph Myers
2017-09-19 19:48                     ` Martin Sebor
2017-09-19 21:00                       ` Joseph Myers
2017-09-20 18:04                         ` Martin Sebor
2017-10-02 22:24                           ` Jeff Law
2018-04-04 16:16                             ` Andreas Krebbel
2018-04-04 16:51                               ` Andreas Krebbel
2018-04-04 17:18                                 ` Martin Sebor
2018-04-04 17:35                                 ` Jakub Jelinek
2018-04-05  7:01                                   ` Andreas Krebbel
2018-04-05 10:35                                     ` Jakub Jelinek
2017-09-12 17:06                   ` Jeff Law
2017-09-19 20:00                     ` Martin Sebor
2017-09-12 15:44               ` Jeff Law
2017-09-12 15:50           ` Jeff Law

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=0fc9f24b-f474-3ab5-e660-b206486cc644@gmail.com \
    --to=msebor@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    --cc=joseph@codesourcery.com \
    --cc=polacek@redhat.com \
    /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).