public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/105682] New: Both `-Wsuggest-attribute=pure` and `-Wsuggest-attribute=const` on same function
@ 2022-05-21  8:39 sagebar at web dot de
  2022-05-21 21:34 ` [Bug ipa/105682] 12/13 Regression] " pinskia at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: sagebar at web dot de @ 2022-05-21  8:39 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 105682
           Summary: Both `-Wsuggest-attribute=pure` and
                    `-Wsuggest-attribute=const` on same function
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: sagebar at web dot de
  Target Milestone: ---

The following example causes both a `-Wsuggest-attribute=pure` and
`-Wsuggest-attribute=const` warning for the same function. Additionally, in
this example, only `-Wsuggest-attribute=pure` would be correct (the
`-Wsuggest-attribute=const` is completely bogus):

Compile with `gcc -c -O2 -Wsuggest-attribute=const -Wsuggest-attribute=pure
input.c`:

```
__attribute__((pure))
int callee(int x) {
        unsigned int a;
        __asm__ __volatile__("");
        __asm__ __volatile__("" : "=X" (a));
        x &= 1;
        if (a & 2)
                __asm__ __volatile__("" : "=m" (x));
        return x & 4;
}

int caller(int x) {
        return callee(x);
}
```

Output (gcc-12.1.0):
>input.c: In function 'caller':
>input.c:12:5: warning: function might be candidate for attribute 'pure' [-Wsuggest-attribute=pure]
>   12 | int caller(int x) {
>      |     ^~~~~~
>input.c:12:5: warning: function might be candidate for attribute 'const' [-Wsuggest-attribute=const]

Output (gcc-11.2.0; expected behavior):
>input.c: In function 'caller':
>input.c:12:5: warning: function might be candidate for attribute 'pure' [-Wsuggest-attribute=pure]
>   12 | int caller(int x) {
>      |     ^~~~~~


=== After adding `__attribute__((pure))` to `caller` ===

```
__attribute__((pure))
int caller(int x) {
        return callee(x);
}
```

gcc-12.1.0 (bogus warning: `caller()` has no right to be const; it calls a pure
function, and that function even contains inline assembly):
>infile.c: In function 'caller':
>infile.c:13:5: warning: function might be candidate for attribute 'const' [-Wsuggest-attribute=const]
>   13 | int caller(int x) {
>      |     ^~~~~~

gcc-11.2.0 (expected behavior):
> (No warnings)

=======================

NOTES:
- Not only does gcc suggest to add both pure & const to the same function, the
later wouldn't even make any sense in this scenario (though suggesting `pure`
is correct).
- The strange-looking body of `callee()` is required to reproduce the bug. -
Trying to further simplify it tends to make the warning go away, suggesting a
problem with some kind of heuristic.
- I know I also just reported
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105676, but that one is about a
bogus `-Wsuggest-attribute=pure` warning, while this one is about a bogus
`-Wsuggest-attribute=const` warning. The two bugs may be related, but simply
suppressing `-Wsuggest-attribute=pure` on const-functions wouldn't be enough to
fix the bug addressed in this report.

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

* [Bug ipa/105682] 12/13 Regression] Both `-Wsuggest-attribute=pure` and `-Wsuggest-attribute=const` on same function
  2022-05-21  8:39 [Bug c/105682] New: Both `-Wsuggest-attribute=pure` and `-Wsuggest-attribute=const` on same function sagebar at web dot de
@ 2022-05-21 21:34 ` pinskia at gcc dot gnu.org
  2022-05-23  6:52 ` [Bug ipa/105682] [12/13 " rguenth at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-05-21 21:34 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
           Keywords|                            |needs-bisection
   Target Milestone|---                         |12.2
      Known to fail|                            |12.1.0
   Last reconfirmed|                            |2022-05-21
      Known to work|                            |11.3.0
             Status|UNCONFIRMED                 |NEW
            Summary|Both                        |12/13 Regression] Both
                   |`-Wsuggest-attribute=pure`  |`-Wsuggest-attribute=pure`
                   |and                         |and
                   |`-Wsuggest-attribute=const` |`-Wsuggest-attribute=const`
                   |on same function            |on same function

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed. A regression from GCC 11.3.0.

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

* [Bug ipa/105682] [12/13 Regression] Both `-Wsuggest-attribute=pure` and `-Wsuggest-attribute=const` on same function
  2022-05-21  8:39 [Bug c/105682] New: Both `-Wsuggest-attribute=pure` and `-Wsuggest-attribute=const` on same function sagebar at web dot de
  2022-05-21 21:34 ` [Bug ipa/105682] 12/13 Regression] " pinskia at gcc dot gnu.org
@ 2022-05-23  6:52 ` rguenth at gcc dot gnu.org
  2022-07-15 10:40 ` [Bug ipa/105682] [12/13 Regression] Both `-Wsuggest-attribute=pure` and `-Wsuggest-attribute=const` on same function since r10-3542-g0b92cf305dcf3438 marxin at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-05-23  6:52 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P3                          |P2
            Summary|12/13 Regression] Both      |[12/13 Regression] Both
                   |`-Wsuggest-attribute=pure`  |`-Wsuggest-attribute=pure`
                   |and                         |and
                   |`-Wsuggest-attribute=const` |`-Wsuggest-attribute=const`
                   |on same function            |on same function

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

* [Bug ipa/105682] [12/13 Regression] Both `-Wsuggest-attribute=pure` and `-Wsuggest-attribute=const` on same function since r10-3542-g0b92cf305dcf3438
  2022-05-21  8:39 [Bug c/105682] New: Both `-Wsuggest-attribute=pure` and `-Wsuggest-attribute=const` on same function sagebar at web dot de
  2022-05-21 21:34 ` [Bug ipa/105682] 12/13 Regression] " pinskia at gcc dot gnu.org
  2022-05-23  6:52 ` [Bug ipa/105682] [12/13 " rguenth at gcc dot gnu.org
@ 2022-07-15 10:40 ` marxin at gcc dot gnu.org
  2022-07-15 19:22 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: marxin at gcc dot gnu.org @ 2022-07-15 10:40 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Liška <marxin at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|needs-bisection             |
            Summary|[12/13 Regression] Both     |[12/13 Regression] Both
                   |`-Wsuggest-attribute=pure`  |`-Wsuggest-attribute=pure`
                   |and                         |and
                   |`-Wsuggest-attribute=const` |`-Wsuggest-attribute=const`
                   |on same function            |on same function since
                   |                            |r10-3542-g0b92cf305dcf3438
                 CC|                            |hubicka at gcc dot gnu.org

--- Comment #2 from Martin Liška <marxin at gcc dot gnu.org> ---
Started with r10-3542-g0b92cf305dcf3438.

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

* [Bug ipa/105682] [12/13 Regression] Both `-Wsuggest-attribute=pure` and `-Wsuggest-attribute=const` on same function since r10-3542-g0b92cf305dcf3438
  2022-05-21  8:39 [Bug c/105682] New: Both `-Wsuggest-attribute=pure` and `-Wsuggest-attribute=const` on same function sagebar at web dot de
                   ` (2 preceding siblings ...)
  2022-07-15 10:40 ` [Bug ipa/105682] [12/13 Regression] Both `-Wsuggest-attribute=pure` and `-Wsuggest-attribute=const` on same function since r10-3542-g0b92cf305dcf3438 marxin at gcc dot gnu.org
@ 2022-07-15 19:22 ` pinskia at gcc dot gnu.org
  2022-07-15 19:24 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-07-15 19:22 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |needs-bisection

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Martin Liška from comment #2)
> Started with r10-3542-g0b92cf305dcf3438.

I think that is when the warning for pure attribute missing started but not
when the warnings for both const and pure attribute started.

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

* [Bug ipa/105682] [12/13 Regression] Both `-Wsuggest-attribute=pure` and `-Wsuggest-attribute=const` on same function since r10-3542-g0b92cf305dcf3438
  2022-05-21  8:39 [Bug c/105682] New: Both `-Wsuggest-attribute=pure` and `-Wsuggest-attribute=const` on same function sagebar at web dot de
                   ` (3 preceding siblings ...)
  2022-07-15 19:22 ` pinskia at gcc dot gnu.org
@ 2022-07-15 19:24 ` pinskia at gcc dot gnu.org
  2022-07-18  8:30 ` [Bug ipa/105682] [12/13 Regression] Both `-Wsuggest-attribute=pure` and `-Wsuggest-attribute=const` on same function since r12-5177-g494bdadf28d0fb35 marxin at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-07-15 19:24 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #3)
> (In reply to Martin Liška from comment #2)
> > Started with r10-3542-g0b92cf305dcf3438.
> 
> I think that is when the warning for pure attribute missing started but not
> when the warnings for both const and pure attribute started.

My bet it is the same issue as PR 105676 really.

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

* [Bug ipa/105682] [12/13 Regression] Both `-Wsuggest-attribute=pure` and `-Wsuggest-attribute=const` on same function since r12-5177-g494bdadf28d0fb35
  2022-05-21  8:39 [Bug c/105682] New: Both `-Wsuggest-attribute=pure` and `-Wsuggest-attribute=const` on same function sagebar at web dot de
                   ` (4 preceding siblings ...)
  2022-07-15 19:24 ` pinskia at gcc dot gnu.org
@ 2022-07-18  8:30 ` marxin at gcc dot gnu.org
  2022-07-18  9:53 ` hubicka at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: marxin at gcc dot gnu.org @ 2022-07-18  8:30 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Liška <marxin at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[12/13 Regression] Both     |[12/13 Regression] Both
                   |`-Wsuggest-attribute=pure`  |`-Wsuggest-attribute=pure`
                   |and                         |and
                   |`-Wsuggest-attribute=const` |`-Wsuggest-attribute=const`
                   |on same function since      |on same function since
                   |r10-3542-g0b92cf305dcf3438  |r12-5177-g494bdadf28d0fb35

--- Comment #5 from Martin Liška <marxin at gcc dot gnu.org> ---
Oh, then it started with r12-5177-g494bdadf28d0fb35.

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

* [Bug ipa/105682] [12/13 Regression] Both `-Wsuggest-attribute=pure` and `-Wsuggest-attribute=const` on same function since r12-5177-g494bdadf28d0fb35
  2022-05-21  8:39 [Bug c/105682] New: Both `-Wsuggest-attribute=pure` and `-Wsuggest-attribute=const` on same function sagebar at web dot de
                   ` (5 preceding siblings ...)
  2022-07-18  8:30 ` [Bug ipa/105682] [12/13 Regression] Both `-Wsuggest-attribute=pure` and `-Wsuggest-attribute=const` on same function since r12-5177-g494bdadf28d0fb35 marxin at gcc dot gnu.org
@ 2022-07-18  9:53 ` hubicka at gcc dot gnu.org
  2022-09-05 17:15 ` sagebar at web dot de
  2023-05-08 12:24 ` [Bug ipa/105682] [12/13/14 " rguenth at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: hubicka at gcc dot gnu.org @ 2022-07-18  9:53 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Jan Hubicka <hubicka at gcc dot gnu.org> ---
gcc-12.1.0 (bogus warning: `caller()` has no right to be const; it calls a pure
function, and that function even contains inline assembly):

I think the conlcusion here is correct.  callee has pure attribute and that
means that it has no side effects except for reading global memory. So whatever
volatile assembly does has to satisfy this.

Now since assembly is not declared as reading memory, GCC concludes that the
function has no side effects and does not read global memory and this can be
uprgraded to const.

Now as optimization goes we first realize that function is pure and only later
realize that it is also const. This could have happened with older GCC versions
as well. This triggers the duplicated warning on older compilers as well with 
-Wsuggest-attribute=pure -Wsuggest-attribute=const -O2 -fno-early-inlining
:

__attribute__ ((pure)) int q(int);
__attribute__ ((pure)) int t(int a)
{
        if (a)
                return q(2);
}
__attribute__ ((pure)) int q(int a)
{
        if (!a)
                return t(1);
}
int a;
int
test()
{
        return t(a);
}

 I guess if we want to avoid such warnings we need to hold all the information
until the end of late ltrans optimization.

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

* [Bug ipa/105682] [12/13 Regression] Both `-Wsuggest-attribute=pure` and `-Wsuggest-attribute=const` on same function since r12-5177-g494bdadf28d0fb35
  2022-05-21  8:39 [Bug c/105682] New: Both `-Wsuggest-attribute=pure` and `-Wsuggest-attribute=const` on same function sagebar at web dot de
                   ` (6 preceding siblings ...)
  2022-07-18  9:53 ` hubicka at gcc dot gnu.org
@ 2022-09-05 17:15 ` sagebar at web dot de
  2023-05-08 12:24 ` [Bug ipa/105682] [12/13/14 " rguenth at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: sagebar at web dot de @ 2022-09-05 17:15 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from sagebar at web dot de ---
(In reply to Jan Hubicka from comment #6)
> I think the conlcusion here is correct.  callee has pure attribute and that
> means that it has no side effects except for reading global memory. So
> whatever volatile assembly does has to satisfy this.
> 
> Now since assembly is not declared as reading memory, GCC concludes that the
> function has no side effects and does not read global memory and this can be
> uprgraded to const.

This assumes that reading memory is the only thing inline assembly can do to
access (part of) the global state. But consider the following example where the
ARM FPU control word is read:

```
#include <fpu_control.h> // HINT: this is arm assembly
//#define _FPU_GETCW(cw) __asm__ __volatile__("vmrs %0, fpscr" : "=r" (cw))
//#define _FPU_SETCW(cw) __asm__ __volatile__("vmsr fpscr, %0" : : "r" (cw))

__attribute__((pure))
fpu_control_t getcw() {
        fpu_control_t result;
        _FPU_GETCW(result);
        return result;
}

int main() {
        fpu_control_t before, after;
        before = getcw();
        _FPU_SETCW(0x1234);
        after = getcw()
        printf("oldcw: %d\n", before);
        printf("newcw: %d\n", after);
}
```

If you're saying that a `__asm__ __volatile__` that doesn't access memory
should be considered as `const`, then gcc should be allowed to remove the first
`getcw()` and simply assign the same value to `before` and `after` (since a
`const` function's return value only depends on its arguments, meaning it calls
can be removed and re-ordered however gcc pleases).

I think you can see how that would be a problem in the above.

However: I would understand gcc doing this if `_FPU_GETCW` was implemented
using `__asm__` instead of `__asm__ __volatile__`.

(If I'm misunderstanding how `pure` is meant to work, please correct me. But as
far as I understand it, the above is a correct usage examle)

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

* [Bug ipa/105682] [12/13/14 Regression] Both `-Wsuggest-attribute=pure` and `-Wsuggest-attribute=const` on same function since r12-5177-g494bdadf28d0fb35
  2022-05-21  8:39 [Bug c/105682] New: Both `-Wsuggest-attribute=pure` and `-Wsuggest-attribute=const` on same function sagebar at web dot de
                   ` (7 preceding siblings ...)
  2022-09-05 17:15 ` sagebar at web dot de
@ 2023-05-08 12:24 ` rguenth at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-05-08 12:24 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|12.3                        |12.4

--- Comment #9 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 12.3 is being released, retargeting bugs to GCC 12.4.

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

end of thread, other threads:[~2023-05-08 12:24 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-21  8:39 [Bug c/105682] New: Both `-Wsuggest-attribute=pure` and `-Wsuggest-attribute=const` on same function sagebar at web dot de
2022-05-21 21:34 ` [Bug ipa/105682] 12/13 Regression] " pinskia at gcc dot gnu.org
2022-05-23  6:52 ` [Bug ipa/105682] [12/13 " rguenth at gcc dot gnu.org
2022-07-15 10:40 ` [Bug ipa/105682] [12/13 Regression] Both `-Wsuggest-attribute=pure` and `-Wsuggest-attribute=const` on same function since r10-3542-g0b92cf305dcf3438 marxin at gcc dot gnu.org
2022-07-15 19:22 ` pinskia at gcc dot gnu.org
2022-07-15 19:24 ` pinskia at gcc dot gnu.org
2022-07-18  8:30 ` [Bug ipa/105682] [12/13 Regression] Both `-Wsuggest-attribute=pure` and `-Wsuggest-attribute=const` on same function since r12-5177-g494bdadf28d0fb35 marxin at gcc dot gnu.org
2022-07-18  9:53 ` hubicka at gcc dot gnu.org
2022-09-05 17:15 ` sagebar at web dot de
2023-05-08 12:24 ` [Bug ipa/105682] [12/13/14 " rguenth 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).