public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/97132] New: assume_aligned is not constexpr
@ 2020-09-21  4:52 fsb4000 at yandex dot ru
  2020-09-21  7:39 ` [Bug libstdc++/97132] " redi at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: fsb4000 at yandex dot ru @ 2020-09-21  4:52 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 97132
           Summary: assume_aligned is not constexpr
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: fsb4000 at yandex dot ru
  Target Milestone: ---

failed test example: https://gcc.godbolt.org/z/KK6W8G

see your implementation:

```
return static_cast<_Tp*>(__builtin_assume_aligned(__ptr, _Align));
```

cast from void * to _Ty* is not allowed in a constant expression.

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

* [Bug libstdc++/97132] assume_aligned is not constexpr
  2020-09-21  4:52 [Bug libstdc++/97132] New: assume_aligned is not constexpr fsb4000 at yandex dot ru
@ 2020-09-21  7:39 ` redi at gcc dot gnu.org
  2020-09-21  8:01 ` jakub at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2020-09-21  7:39 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
          Component|c++                         |libstdc++
   Last reconfirmed|                            |2020-09-21
           Assignee|unassigned at gcc dot gnu.org      |redi at gcc dot gnu.org
             Status|UNCONFIRMED                 |ASSIGNED

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Huh, I thought I'd fixed this.

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

* [Bug libstdc++/97132] assume_aligned is not constexpr
  2020-09-21  4:52 [Bug libstdc++/97132] New: assume_aligned is not constexpr fsb4000 at yandex dot ru
  2020-09-21  7:39 ` [Bug libstdc++/97132] " redi at gcc dot gnu.org
@ 2020-09-21  8:01 ` jakub at gcc dot gnu.org
  2020-09-21  8:34 ` redi at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-09-21  8:01 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

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

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
I think there is no compliant way to test alignment assumptions during constant
evaluations and after all, one should always see the actual objects and
therefore can check the exact alignment, rather than needing any assumptions.
So, I think it doesn't really matter if this is done on the libstdc++ say
through not using __builtin_assume_aligned if std::is_constant_evaluated() or
on the C++ FE side (folding __builtin_assume_aligned to the first argument
during manifestly constant evaluation).

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

* [Bug libstdc++/97132] assume_aligned is not constexpr
  2020-09-21  4:52 [Bug libstdc++/97132] New: assume_aligned is not constexpr fsb4000 at yandex dot ru
  2020-09-21  7:39 ` [Bug libstdc++/97132] " redi at gcc dot gnu.org
  2020-09-21  8:01 ` jakub at gcc dot gnu.org
@ 2020-09-21  8:34 ` redi at gcc dot gnu.org
  2020-09-21  8:38 ` fsb4000 at yandex dot ru
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2020-09-21  8:34 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jakub Jelinek from comment #2)
> I think there is no compliant way to test alignment assumptions during
> constant evaluations and after all, one should always see the actual objects
> and therefore can check the exact alignment, rather than needing any
> assumptions.

Right, during CE unaligned objects are UB anyway and not allowed. And the
function is only supposed to benefit the optimiser, which isn't relevant during
CE.

> So, I think it doesn't really matter if this is done on the libstdc++ say
> through not using __builtin_assume_aligned if std::is_constant_evaluated()
> or on the C++ FE side (folding __builtin_assume_aligned to the first
> argument during manifestly constant evaluation).

I'll test this:

     {
       static_assert(std::has_single_bit(_Align));
       _GLIBCXX_DEBUG_ASSERT((std::uintptr_t)__ptr % _Align == 0);
-      return static_cast<_Tp*>(__builtin_assume_aligned(__ptr, _Align));
+      if (std::is_constant_evaluated())
+       return __ptr;
+      else
+       return static_cast<_Tp*>(__builtin_assume_aligned(__ptr, _Align));
     }
 #endif // C++2a

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

* [Bug libstdc++/97132] assume_aligned is not constexpr
  2020-09-21  4:52 [Bug libstdc++/97132] New: assume_aligned is not constexpr fsb4000 at yandex dot ru
                   ` (2 preceding siblings ...)
  2020-09-21  8:34 ` redi at gcc dot gnu.org
@ 2020-09-21  8:38 ` fsb4000 at yandex dot ru
  2020-09-21 13:29 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: fsb4000 at yandex dot ru @ 2020-09-21  8:38 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from fsb4000 at yandex dot ru ---
(In reply to Jonathan Wakely from comment #3)
> I'll test this:
> 
>      {
>        static_assert(std::has_single_bit(_Align));
>        _GLIBCXX_DEBUG_ASSERT((std::uintptr_t)__ptr % _Align == 0);
> -      return static_cast<_Tp*>(__builtin_assume_aligned(__ptr, _Align));
> +      if (std::is_constant_evaluated())
> +       return __ptr;
> +      else
> +       return static_cast<_Tp*>(__builtin_assume_aligned(__ptr, _Align));
>      }
>  #endif // C++2a

Yes, should be fine. 
We do same for Microsoft STL.

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

* [Bug libstdc++/97132] assume_aligned is not constexpr
  2020-09-21  4:52 [Bug libstdc++/97132] New: assume_aligned is not constexpr fsb4000 at yandex dot ru
                   ` (3 preceding siblings ...)
  2020-09-21  8:38 ` fsb4000 at yandex dot ru
@ 2020-09-21 13:29 ` cvs-commit at gcc dot gnu.org
  2020-10-19 21:25 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-09-21 13:29 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:f10ed928e2f8ecc2c859abff8f2f9296b11b8d95

commit r11-3324-gf10ed928e2f8ecc2c859abff8f2f9296b11b8d95
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Mon Sep 21 14:28:58 2020 +0100

    libstdc++: Make std::assume_aligned a constexpr function [PR 97132]

    The cast from void* to T* in std::assume_aligned is not valid in a
    constexpr function. The optimization hint is redundant during constant
    evaluation anyway (the compiler can see the object and knows its
    alignment). Simply return the original pointer without applying the
    __builtin_assume_aligned hint to it when doing constant evaluation.

    This change also removes the preprocessor branch that works around
    uintptr_t not being available. We already assume that type is present
    elsewhere in the library.

    libstdc++-v3/ChangeLog:

            PR libstdc++/97132
            * include/bits/align.h (align) [!_GLIBCXX_USE_C99_STDINT_TR1]:
            Remove unused code.
            (assume_aligned): Do not use __builtin_assume_aligned during
            constant evaluation.
            * testsuite/20_util/assume_aligned/1.cc: Improve test.
            * testsuite/20_util/assume_aligned/97132.cc: New test.

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

* [Bug libstdc++/97132] assume_aligned is not constexpr
  2020-09-21  4:52 [Bug libstdc++/97132] New: assume_aligned is not constexpr fsb4000 at yandex dot ru
                   ` (4 preceding siblings ...)
  2020-09-21 13:29 ` cvs-commit at gcc dot gnu.org
@ 2020-10-19 21:25 ` cvs-commit at gcc dot gnu.org
  2020-10-19 21:25 ` cvs-commit at gcc dot gnu.org
  2020-10-19 21:34 ` redi at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-10-19 21:25 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-10 branch has been updated by Jonathan Wakely
<redi@gcc.gnu.org>:

https://gcc.gnu.org/g:90c9484b12dd8a05b5314f5cb9847df46024a194

commit r10-8913-g90c9484b12dd8a05b5314f5cb9847df46024a194
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Mon Sep 21 14:28:58 2020 +0100

    libstdc++: Make std::assume_aligned a constexpr function [PR 97132]

    The cast from void* to T* in std::assume_aligned is not valid in a
    constexpr function. The optimization hint is redundant during constant
    evaluation anyway (the compiler can see the object and knows its
    alignment). Simply return the original pointer without applying the
    __builtin_assume_aligned hint to it when doing constant evaluation.

    libstdc++-v3/ChangeLog:

            PR libstdc++/97132
            * include/std/memory (assume_aligned): Do not use
            __builtin_assume_aligned during constant evaluation.
            * testsuite/20_util/assume_aligned/1.cc: Improve test.
            * testsuite/20_util/assume_aligned/97132.cc: New test.

    (cherry picked from commit f10ed928e2f8ecc2c859abff8f2f9296b11b8d95)

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

* [Bug libstdc++/97132] assume_aligned is not constexpr
  2020-09-21  4:52 [Bug libstdc++/97132] New: assume_aligned is not constexpr fsb4000 at yandex dot ru
                   ` (5 preceding siblings ...)
  2020-10-19 21:25 ` cvs-commit at gcc dot gnu.org
@ 2020-10-19 21:25 ` cvs-commit at gcc dot gnu.org
  2020-10-19 21:34 ` redi at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-10-19 21:25 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-9 branch has been updated by Jonathan Wakely
<redi@gcc.gnu.org>:

https://gcc.gnu.org/g:77923ad01415f6e72af844cbef5227f5b5a9fb4b

commit r9-9003-g77923ad01415f6e72af844cbef5227f5b5a9fb4b
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Mon Sep 21 14:28:58 2020 +0100

    libstdc++: Make std::assume_aligned a constexpr function [PR 97132]

    The cast from void* to T* in std::assume_aligned is not valid in a
    constexpr function. The optimization hint is redundant during constant
    evaluation anyway (the compiler can see the object and knows its
    alignment). Simply return the original pointer without applying the
    __builtin_assume_aligned hint to it when doing constant evaluation.

    libstdc++-v3/ChangeLog:

            PR libstdc++/97132
            * include/std/memory (assume_aligned): Do not use
            __builtin_assume_aligned during constant evaluation.
            * testsuite/20_util/assume_aligned/1.cc: Improve test.
            * testsuite/20_util/assume_aligned/97132.cc: New test.

    (cherry picked from commit f10ed928e2f8ecc2c859abff8f2f9296b11b8d95)

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

* [Bug libstdc++/97132] assume_aligned is not constexpr
  2020-09-21  4:52 [Bug libstdc++/97132] New: assume_aligned is not constexpr fsb4000 at yandex dot ru
                   ` (6 preceding siblings ...)
  2020-10-19 21:25 ` cvs-commit at gcc dot gnu.org
@ 2020-10-19 21:34 ` redi at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2020-10-19 21:34 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
   Target Milestone|---                         |9.4
             Status|ASSIGNED                    |RESOLVED

--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Fixed for 9.4 and 10.3

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

end of thread, other threads:[~2020-10-19 21:34 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-21  4:52 [Bug libstdc++/97132] New: assume_aligned is not constexpr fsb4000 at yandex dot ru
2020-09-21  7:39 ` [Bug libstdc++/97132] " redi at gcc dot gnu.org
2020-09-21  8:01 ` jakub at gcc dot gnu.org
2020-09-21  8:34 ` redi at gcc dot gnu.org
2020-09-21  8:38 ` fsb4000 at yandex dot ru
2020-09-21 13:29 ` cvs-commit at gcc dot gnu.org
2020-10-19 21:25 ` cvs-commit at gcc dot gnu.org
2020-10-19 21:25 ` cvs-commit at gcc dot gnu.org
2020-10-19 21:34 ` redi 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).