public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [Patch][libstdc++] Add constexpr specifier to function
@ 2024-06-06  2:36 Deev Patel
  2024-06-06  6:49 ` Jonathan Wakely
  0 siblings, 1 reply; 4+ messages in thread
From: Deev Patel @ 2024-06-06  2:36 UTC (permalink / raw)
  To: gcc-patches, libstdc++

[-- Attachment #1: Type: text/plain, Size: 1873 bytes --]

Things like std::atomic<double> are currently unable to be created in a
constexpr context with clang 18 and c++20. Example compilation error
```
external/com_google_tcmalloc/tcmalloc/parameters.cc:223:17: error: variable
does not have a constant initializer
  223 |     Parameters::peak_sampling_heap_growth_fraction_(1.1);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
external/com_google_tcmalloc/tcmalloc/parameters.cc:222:1: note: required
by 'constinit' specifier here
  222 | ABSL_CONST_INIT std::atomic<double>
      | ^~~~~~~~~~~~~~~
external/com_google_absl/absl/base/attributes.h:743:25: note: expanded from
macro 'ABSL_CONST_INIT'
  743 | #define ABSL_CONST_INIT constinit
      |                         ^~~~~~~~~
external/local_config_clang/crosstool/extra_tools/lib/gcc/x86_64-linux-gnu/14.1.0/../../../../include/c++/14.1.0/bits/atomic_base.h:1286:9:
note: non-constexpr function '__clear_padding<double>' cannot be used in a
constant expression
 1286 |       { __atomic_impl::__clear_padding(_M_fp); }
      |         ^
external/local_config_clang/crosstool/extra_tools/lib/gcc/x86_64-linux-gnu/14.1.0/../../../../include/c++/14.1.0/atomic:1644:38:
note: in call to '__atomic_float(1.100000e+00)'
 1644 |       atomic(double __fp) noexcept : __atomic_float<double>(__fp)
```

This patch adds the necessary constexpr specifier.

- Deev Patel

diff --git a/libstdc++-v3/include/bits/atomic_base.h
b/libstdc++-v3/include/bits/atomic_base.h
index 062f1549740..5e89f66f620 100644
--- a/libstdc++-v3/include/bits/atomic_base.h
+++ b/libstdc++-v3/include/bits/atomic_base.h
@@ -968,7 +968,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       }

     template<typename _Tp>
-      _GLIBCXX_ALWAYS_INLINE _Tp*
+      constexpr _GLIBCXX_ALWAYS_INLINE _Tp*
       __clear_padding(_Tp& __val) noexcept
       {
        auto* __ptr = std::__addressof(__val);

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

* Re: [Patch][libstdc++] Add constexpr specifier to function
  2024-06-06  2:36 [Patch][libstdc++] Add constexpr specifier to function Deev Patel
@ 2024-06-06  6:49 ` Jonathan Wakely
  2024-06-06  6:51   ` Jonathan Wakely
  0 siblings, 1 reply; 4+ messages in thread
From: Jonathan Wakely @ 2024-06-06  6:49 UTC (permalink / raw)
  To: Deev Patel; +Cc: gcc-patches, libstdc++

On Thu, 6 Jun 2024 at 03:37, Deev Patel <dkp10000@gmail.com> wrote:
>
> Things like std::atomic<double> are currently unable to be created in a constexpr context with clang 18 and c++20. Example compilation error
> ```
> external/com_google_tcmalloc/tcmalloc/parameters.cc:223:17: error: variable does not have a constant initializer
>   223 |     Parameters::peak_sampling_heap_growth_fraction_(1.1);
>       |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> external/com_google_tcmalloc/tcmalloc/parameters.cc:222:1: note: required by 'constinit' specifier here
>   222 | ABSL_CONST_INIT std::atomic<double>
>       | ^~~~~~~~~~~~~~~
> external/com_google_absl/absl/base/attributes.h:743:25: note: expanded from macro 'ABSL_CONST_INIT'
>   743 | #define ABSL_CONST_INIT constinit
>       |                         ^~~~~~~~~
> external/local_config_clang/crosstool/extra_tools/lib/gcc/x86_64-linux-gnu/14.1.0/../../../../include/c++/14.1.0/bits/atomic_base.h:1286:9: note: non-constexpr function '__clear_padding<double>' cannot be used in a constant expression
>  1286 |       { __atomic_impl::__clear_padding(_M_fp); }
>       |         ^
> external/local_config_clang/crosstool/extra_tools/lib/gcc/x86_64-linux-gnu/14.1.0/../../../../include/c++/14.1.0/atomic:1644:38: note: in call to '__atomic_float(1.100000e+00)'
>  1644 |       atomic(double __fp) noexcept : __atomic_float<double>(__fp)
> ```
>
> This patch adds the necessary constexpr specifier.
>
> - Deev Patel
>
> diff --git a/libstdc++-v3/include/bits/atomic_base.h b/libstdc++-v3/include/bits/atomic_base.h
> index 062f1549740..5e89f66f620 100644
> --- a/libstdc++-v3/include/bits/atomic_base.h
> +++ b/libstdc++-v3/include/bits/atomic_base.h
> @@ -968,7 +968,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>        }
>
>      template<typename _Tp>
> -      _GLIBCXX_ALWAYS_INLINE _Tp*
> +      constexpr _GLIBCXX_ALWAYS_INLINE _Tp*

Thanks. I'm pretty sure the constexpr should go after the
always_inline attribute, but I'll take care of it.

>        __clear_padding(_Tp& __val) noexcept
>        {
>         auto* __ptr = std::__addressof(__val);


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

* Re: [Patch][libstdc++] Add constexpr specifier to function
  2024-06-06  6:49 ` Jonathan Wakely
@ 2024-06-06  6:51   ` Jonathan Wakely
  2024-06-08 15:07     ` [committed v2][libstdc++] Add constexpr specifier to function __atomic_impl::__clear_padding Jonathan Wakely
  0 siblings, 1 reply; 4+ messages in thread
From: Jonathan Wakely @ 2024-06-06  6:51 UTC (permalink / raw)
  To: Deev Patel; +Cc: gcc-patches, libstdc++

On Thu, 6 Jun 2024 at 07:49, Jonathan Wakely <jwakely@redhat.com> wrote:
>
> On Thu, 6 Jun 2024 at 03:37, Deev Patel <dkp10000@gmail.com> wrote:
> >
> > Things like std::atomic<double> are currently unable to be created in a constexpr context with clang 18 and c++20. Example compilation error
> > ```
> > external/com_google_tcmalloc/tcmalloc/parameters.cc:223:17: error: variable does not have a constant initializer
> >   223 |     Parameters::peak_sampling_heap_growth_fraction_(1.1);
> >       |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > external/com_google_tcmalloc/tcmalloc/parameters.cc:222:1: note: required by 'constinit' specifier here
> >   222 | ABSL_CONST_INIT std::atomic<double>
> >       | ^~~~~~~~~~~~~~~
> > external/com_google_absl/absl/base/attributes.h:743:25: note: expanded from macro 'ABSL_CONST_INIT'
> >   743 | #define ABSL_CONST_INIT constinit
> >       |                         ^~~~~~~~~
> > external/local_config_clang/crosstool/extra_tools/lib/gcc/x86_64-linux-gnu/14.1.0/../../../../include/c++/14.1.0/bits/atomic_base.h:1286:9: note: non-constexpr function '__clear_padding<double>' cannot be used in a constant expression
> >  1286 |       { __atomic_impl::__clear_padding(_M_fp); }
> >       |         ^
> > external/local_config_clang/crosstool/extra_tools/lib/gcc/x86_64-linux-gnu/14.1.0/../../../../include/c++/14.1.0/atomic:1644:38: note: in call to '__atomic_float(1.100000e+00)'
> >  1644 |       atomic(double __fp) noexcept : __atomic_float<double>(__fp)
> > ```
> >
> > This patch adds the necessary constexpr specifier.
> >
> > - Deev Patel
> >
> > diff --git a/libstdc++-v3/include/bits/atomic_base.h b/libstdc++-v3/include/bits/atomic_base.h
> > index 062f1549740..5e89f66f620 100644
> > --- a/libstdc++-v3/include/bits/atomic_base.h
> > +++ b/libstdc++-v3/include/bits/atomic_base.h
> > @@ -968,7 +968,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> >        }
> >
> >      template<typename _Tp>
> > -      _GLIBCXX_ALWAYS_INLINE _Tp*
> > +      constexpr _GLIBCXX_ALWAYS_INLINE _Tp*
>
> Thanks. I'm pretty sure the constexpr should go after the
> always_inline attribute, but I'll take care of it.

And it might need to be _GLIBCXX14_CONSTEXPR because the function
isn't a valid constexpr function in C++11 mode (and doesn't need to
be, because atomic<floating-point-type> doesn't exist in C++11).

>
> >        __clear_padding(_Tp& __val) noexcept
> >        {
> >         auto* __ptr = std::__addressof(__val);


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

* [committed v2][libstdc++] Add constexpr specifier to function __atomic_impl::__clear_padding
  2024-06-06  6:51   ` Jonathan Wakely
@ 2024-06-08 15:07     ` Jonathan Wakely
  0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Wakely @ 2024-06-08 15:07 UTC (permalink / raw)
  To: libstdc++, gcc-patches

Here's what I pushed to trunk, using the macro instead of the plain
keyword, and with a testcase.

Thanks for the patch, Deev.

Tested x86_64-linux. Pushed to trunk. I'll backport this too.

-- >8 --

This is called from the std::atomic<floating-point-type> constructor,
which needs to be usable in constant expressions.

libstdc++-v3/ChangeLog:

	* include/bits/atomic_base.h (__atomic_impl::__clear_padding):
	Add missing constexpr specifier.
	* testsuite/29_atomics/atomic_float/constinit.cc: New test.

Co-authored-by: Jonathan Wakely <jwakely@redhat.com>
---
 libstdc++-v3/include/bits/atomic_base.h                     | 2 +-
 libstdc++-v3/testsuite/29_atomics/atomic_float/constinit.cc | 3 +++
 2 files changed, 4 insertions(+), 1 deletion(-)
 create mode 100644 libstdc++-v3/testsuite/29_atomics/atomic_float/constinit.cc

diff --git a/libstdc++-v3/include/bits/atomic_base.h b/libstdc++-v3/include/bits/atomic_base.h
index 062f1549740..20901b7fc06 100644
--- a/libstdc++-v3/include/bits/atomic_base.h
+++ b/libstdc++-v3/include/bits/atomic_base.h
@@ -968,7 +968,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       }
 
     template<typename _Tp>
-      _GLIBCXX_ALWAYS_INLINE _Tp*
+      _GLIBCXX_ALWAYS_INLINE _GLIBCXX14_CONSTEXPR _Tp*
       __clear_padding(_Tp& __val) noexcept
       {
 	auto* __ptr = std::__addressof(__val);
diff --git a/libstdc++-v3/testsuite/29_atomics/atomic_float/constinit.cc b/libstdc++-v3/testsuite/29_atomics/atomic_float/constinit.cc
new file mode 100644
index 00000000000..6b3f4f76b4c
--- /dev/null
+++ b/libstdc++-v3/testsuite/29_atomics/atomic_float/constinit.cc
@@ -0,0 +1,3 @@
+// { dg-do compile { target c++20 } }
+#include <atomic>
+constinit std::atomic<float> a(0.0f);
-- 
2.45.1


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

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

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-06  2:36 [Patch][libstdc++] Add constexpr specifier to function Deev Patel
2024-06-06  6:49 ` Jonathan Wakely
2024-06-06  6:51   ` Jonathan Wakely
2024-06-08 15:07     ` [committed v2][libstdc++] Add constexpr specifier to function __atomic_impl::__clear_padding Jonathan Wakely

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).