[PATCH] libstdc++: Enable _GLIBCXX_ASSERTIONS by default for -O0 [PR112808]

Jonathan Wakely jwakely@redhat.com
Thu Oct 3 17:42:16 GMT 2024


On Thu, 3 Oct 2024 at 18:07, François Dumont <frs.dumont@gmail.com> wrote:
>
>
> On 02/10/2024 20:40, Arsen Arsenović wrote:
> > Patrick Palka <ppalka@redhat.com> writes:
> >
> >> On Wed, 2 Oct 2024, Jonathan Wakely wrote:
> >>
> >>> I think we should do this.
> >>>
> >>> Tested x86_64-linux.
> >>>
> >>> -- >8 --
> >>>
> >>> Too many users don't know about -D_GLIBCXX_ASSERTIONS and so are missing
> >>> valuable checks for C++ standard library preconditions. This change
> >>> enables libstdc++ assertions by default when compiling with -O0 so that
> >>> we diagnose more bugs by default.
> >>>
> >>> When users enable optimization we don't add the assertions by default
> >>> (because they have non-zero overhead) so they still need to enable them
> >>> manually.
> >>>
> >>> For users who really don't want the assertions even in unoptimized
> >>> builds, defining _GLIBCXX_NO_ASSERTIONS will prevent them from being
> >>> enabled automatically.
> >>>
> >>> libstdc++-v3/ChangeLog:
> >>>
> >>>     PR libstdc++/112808
> >>>     * doc/xml/manual/using.xml (_GLIBCXX_ASSERTIONS): Document
> >>>     implicit definition for -O0 compilation.
> >>>     (_GLIBCXX_NO_ASSERTIONS): Document.
> >>>     * doc/html/manual/using_macros.html: Regenerate.
> >>>     * include/bits/c++config [!__OPTIMIZE__] (_GLIBCXX_ASSERTIONS):
> >>>     Define for unoptimized builds.
> >> LGTM.  At -O0 the additional overhead of the assertions would be
> >> relatively small compared to the overhead of -O0 itself.
> > On that note, maybe we could add a __OPTIMIZE_DEBUG__ or similar so that
> > we can do a similar thing for -Og (which some use for their development
> > cycle).
>
> LGTM too.

Thanks.

> Maybe one step toward replacing those assertions with
> __builtin_unreachable if -O2/3 and some macro defined !

https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p2064r0.pdf
explains why replacing assertions with unreachable annotations is
unsafe. If users can ever get it wrong, then it's not unreachable. And
if users can't get it wrong, we don't need to waste time checking it
with an assertion. So if we have an assertion there today, we don't
want to make it unreachable.

I think allowing users to turn those assertions into go-faster
annotations would be a trap and make code less safe.



More information about the Libstdc++ mailing list