public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/113713] New: static_assert result depends on optimization settings
@ 2024-02-02  8:08 fchelnokov at gmail dot com
  2024-02-02  8:17 ` [Bug c++/113713] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: fchelnokov at gmail dot com @ 2024-02-02  8:08 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 113713
           Summary: static_assert result depends on optimization settings
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: fchelnokov at gmail dot com
  Target Milestone: ---

This program

struct A{};

constexpr bool p(auto) { return false; }
constexpr bool f(auto v) { return p(v); }
constexpr bool g() { return f(A()); }
constexpr bool p(A) { return true; }

static_assert( f(A{}) );

The static_assert passes in GCC only with -O0 command line option, and it fails
with -O1 and higher optimization options, which looks wrong. Online demo:
https://godbolt.org/z/vWq8G7rn4

Related discussion: https://stackoverflow.com/q/77923182/7325599

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

* [Bug c++/113713] static_assert result depends on optimization settings
  2024-02-02  8:08 [Bug c++/113713] New: static_assert result depends on optimization settings fchelnokov at gmail dot com
@ 2024-02-02  8:17 ` pinskia at gcc dot gnu.org
  2024-02-02  8:28 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-02-02  8:17 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
C++11 testcase so it is easier to test against older versions of GCC:
```

struct A{};

template<class T>
constexpr bool p(T) { return false; }
template<class T>
constexpr bool f(T v) { return p(v); }

constexpr bool g() { return f(A()); }
constexpr bool p(A) { return true; }


void ff() {
  static_assert( f(A{}) , "");
}
```


GCC 6 and before causes the static_assert to always to fail at all
optimizations level. GCC 7 and afterwards has the current behavior of true for
-O0 and false for -O1 and above.

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

* [Bug c++/113713] static_assert result depends on optimization settings
  2024-02-02  8:08 [Bug c++/113713] New: static_assert result depends on optimization settings fchelnokov at gmail dot com
  2024-02-02  8:17 ` [Bug c++/113713] " pinskia at gcc dot gnu.org
@ 2024-02-02  8:28 ` pinskia at gcc dot gnu.org
  2024-02-02  8:34 ` [Bug c++/113713] constexpr function values (incorrectly?) depend on optimization level pinskia at gcc dot gnu.org
  2024-02-02 15:11 ` mpolacek at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-02-02  8:28 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2024-02-02
             Status|UNCONFIRMED                 |NEW

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed, this is a constexpr issue in both clang and GCC (unless there is
some unspecified behavior I don't know of).

Take:
```
#if defined(CE)
#define CONSTEXPR constexpr
#else
#define CONSTEXPR
#endif
struct A{};
template<class T>
CONSTEXPR
bool p(T) { return false; }
template<class T>
CONSTEXPR
bool f(T v) { return p(v); }
CONSTEXPR
bool g() { return f(A()); }
CONSTEXPR
bool p(A) { return true; }

int main()
{
        A a;
        if (!f(a))
          __builtin_abort();
}
```

Without CE being defined, this works at all optimizations level. With CE being
defined this works at -O0 only (like there is some [incorrect?] caching going
on at -O1 and above and clang is doing the caching at all levels).

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

* [Bug c++/113713] constexpr function values (incorrectly?) depend on optimization level
  2024-02-02  8:08 [Bug c++/113713] New: static_assert result depends on optimization settings fchelnokov at gmail dot com
  2024-02-02  8:17 ` [Bug c++/113713] " pinskia at gcc dot gnu.org
  2024-02-02  8:28 ` pinskia at gcc dot gnu.org
@ 2024-02-02  8:34 ` pinskia at gcc dot gnu.org
  2024-02-02 15:11 ` mpolacek at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-02-02  8:34 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
https://gcc.gnu.org/pipermail/gcc-patches/2016-April/446612.html might be
related to the whole caching situtation but I could be wrong.

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

* [Bug c++/113713] constexpr function values (incorrectly?) depend on optimization level
  2024-02-02  8:08 [Bug c++/113713] New: static_assert result depends on optimization settings fchelnokov at gmail dot com
                   ` (2 preceding siblings ...)
  2024-02-02  8:34 ` [Bug c++/113713] constexpr function values (incorrectly?) depend on optimization level pinskia at gcc dot gnu.org
@ 2024-02-02 15:11 ` mpolacek at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2024-02-02 15:11 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

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

--- Comment #4 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Looks like this changed with r11-4230-g46fdced6a9f936

commit 46fdced6a9f936ae4d5b42347d7d87f69875683a
Author: Patrick Palka <ppalka@redhat.com>
Date:   Thu Oct 22 07:33:58 2020 -0400

    c++: constexpr evaluation and bare EMPTY_CLASS_EXPR [PR96575]

but only when A is an empty class.

Using struct A { int i; };, the test has compiled with -O0 since
r7-4458-gf065303fcf9aa2.

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

end of thread, other threads:[~2024-02-02 15:11 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-02  8:08 [Bug c++/113713] New: static_assert result depends on optimization settings fchelnokov at gmail dot com
2024-02-02  8:17 ` [Bug c++/113713] " pinskia at gcc dot gnu.org
2024-02-02  8:28 ` pinskia at gcc dot gnu.org
2024-02-02  8:34 ` [Bug c++/113713] constexpr function values (incorrectly?) depend on optimization level pinskia at gcc dot gnu.org
2024-02-02 15:11 ` mpolacek 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).