public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/98641] New: Feature request: implement pointer alignment builtins
@ 2021-01-12 17:01 ktkachov at gcc dot gnu.org
  2021-01-12 17:02 ` [Bug c++/98641] " ktkachov at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: ktkachov at gcc dot gnu.org @ 2021-01-12 17:01 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 98641
           Summary: Feature request: implement pointer alignment builtins
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ktkachov at gcc dot gnu.org
  Target Milestone: ---

We received reports that users found the pointer alignment builtins provided by
LLVM useful in avoiding error-prone casting to and from intptr_t:
https://clang.llvm.org/docs/LanguageExtensions.html#alignment-builtins

It would be great if we could support them in GCC as well.

This would involve implementing:
Type __builtin_align_up(Type value, size_t alignment);
Type __builtin_align_down(Type value, size_t alignment);
bool __builtin_is_aligned(Type value, size_t alignment);

Using these builtins the compiler can also preserve pointer provenance
information  more easily.

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

* [Bug c++/98641] Feature request: implement pointer alignment builtins
  2021-01-12 17:01 [Bug c++/98641] New: Feature request: implement pointer alignment builtins ktkachov at gcc dot gnu.org
@ 2021-01-12 17:02 ` ktkachov at gcc dot gnu.org
  2021-01-13  8:11 ` rguenth at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: ktkachov at gcc dot gnu.org @ 2021-01-12 17:02 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from ktkachov at gcc dot gnu.org ---
The component is marked as C++, but it would be good to have these in C as
well.

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

* [Bug c++/98641] Feature request: implement pointer alignment builtins
  2021-01-12 17:01 [Bug c++/98641] New: Feature request: implement pointer alignment builtins ktkachov at gcc dot gnu.org
  2021-01-12 17:02 ` [Bug c++/98641] " ktkachov at gcc dot gnu.org
@ 2021-01-13  8:11 ` rguenth at gcc dot gnu.org
  2021-01-13 10:07 ` redi at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-01-13  8:11 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
            Version|unknown                     |11.0

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

* [Bug c++/98641] Feature request: implement pointer alignment builtins
  2021-01-12 17:01 [Bug c++/98641] New: Feature request: implement pointer alignment builtins ktkachov at gcc dot gnu.org
  2021-01-12 17:02 ` [Bug c++/98641] " ktkachov at gcc dot gnu.org
  2021-01-13  8:11 ` rguenth at gcc dot gnu.org
@ 2021-01-13 10:07 ` redi at gcc dot gnu.org
  2021-01-13 10:18 ` jakub at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2021-01-13 10:07 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
We could make immediate use of these in libstdc++. We have std::align and
std::assume_aligned in include/bits/align.h, and aligned_ceil in
src/c++17/memory_resource.cc, and a couple of places in libsupc++/new_opa.cc
and maybe other places too.

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

* [Bug c++/98641] Feature request: implement pointer alignment builtins
  2021-01-12 17:01 [Bug c++/98641] New: Feature request: implement pointer alignment builtins ktkachov at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2021-01-13 10:07 ` redi at gcc dot gnu.org
@ 2021-01-13 10:18 ` jakub at gcc dot gnu.org
  2021-01-13 10:21 ` redi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-01-13 10:18 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
assume_aligned is something different, I guess __builtin_is_aligned expands to
an actual runtime check (perhaps optimized away if the pointer can't be
misaligned).
So I guess __builtin_is_aligned (__builtin_assume_aligned (ptr, 32), 16) should
optimize to true...

I guess I have big questions on what exactly will it do during constexpr
evaluation - if it is on a pointer to object with certain alignment, for
smaller alignment arguments guess it is just like pointer arithmetics then, but
if it asks for larger alignment, shall it make it non-constant expression?

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

* [Bug c++/98641] Feature request: implement pointer alignment builtins
  2021-01-12 17:01 [Bug c++/98641] New: Feature request: implement pointer alignment builtins ktkachov at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2021-01-13 10:18 ` jakub at gcc dot gnu.org
@ 2021-01-13 10:21 ` redi at gcc dot gnu.org
  2021-01-13 12:44 ` Alexander.Richardson at cl dot cam.ac.uk
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2021-01-13 10:21 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jakub Jelinek from comment #3)
> assume_aligned is something different, I guess __builtin_is_aligned expands
> to an actual runtime check (perhaps optimized away if the pointer can't be
> misaligned).

But we could do this:

--- a/libstdc++-v3/include/bits/align.h
+++ b/libstdc++-v3/include/bits/align.h
@@ -98,7 +98,7 @@ align(size_t __align, size_t __size, void*& __ptr, size_t&
__space) noexcept
        {
          // This function is expected to be used in hot code, where
          // __glibcxx_assert would add unwanted overhead.
-         _GLIBCXX_DEBUG_ASSERT((uintptr_t)__ptr % _Align == 0);
+         _GLIBCXX_DEBUG_ASSERT(__builtin_is_aligned(__ptr, _Align));
          return static_cast<_Tp*>(__builtin_assume_aligned(__ptr, _Align));
        }
     }

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

* [Bug c++/98641] Feature request: implement pointer alignment builtins
  2021-01-12 17:01 [Bug c++/98641] New: Feature request: implement pointer alignment builtins ktkachov at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2021-01-13 10:21 ` redi at gcc dot gnu.org
@ 2021-01-13 12:44 ` Alexander.Richardson at cl dot cam.ac.uk
  2021-01-13 12:53 ` Alexander.Richardson at cl dot cam.ac.uk
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Alexander.Richardson at cl dot cam.ac.uk @ 2021-01-13 12:44 UTC (permalink / raw)
  To: gcc-bugs

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

Alex Richardson <Alexander.Richardson at cl dot cam.ac.uk> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |Alexander.Richardson at cl dot cam
                   |                            |.ac.uk

--- Comment #5 from Alex Richardson <Alexander.Richardson at cl dot cam.ac.uk> ---
In clang __builtin_assume_aligned will not be used by the constant expression
evaluator, but it will be used for code generation: for example,

_Bool check(void* x) {
    return __builtin_is_aligned(__builtin_assume_aligned(x, 16), 16);
}

is folded to "return true", but something like

extern int i;
_Static_assert(__builtin_is_aligned(__builtin_assume_aligned(&i, 16), 8), "");
generates an "alignment of the base pointee object (4 bytes) is less than the
asserted 16 byte" error when evaluating __builtin_assume_aligned().

https://godbolt.org/z/96h6j1

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

* [Bug c++/98641] Feature request: implement pointer alignment builtins
  2021-01-12 17:01 [Bug c++/98641] New: Feature request: implement pointer alignment builtins ktkachov at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2021-01-13 12:44 ` Alexander.Richardson at cl dot cam.ac.uk
@ 2021-01-13 12:53 ` Alexander.Richardson at cl dot cam.ac.uk
  2021-01-13 12:54 ` jakub at gcc dot gnu.org
  2021-01-13 14:31 ` Alexander.Richardson at cl dot cam.ac.uk
  8 siblings, 0 replies; 10+ messages in thread
From: Alexander.Richardson at cl dot cam.ac.uk @ 2021-01-13 12:53 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Alex Richardson <Alexander.Richardson at cl dot cam.ac.uk> ---
(In reply to Jakub Jelinek from comment #3)
> assume_aligned is something different, I guess __builtin_is_aligned expands
> to an actual runtime check (perhaps optimized away if the pointer can't be
> misaligned).
> So I guess __builtin_is_aligned (__builtin_assume_aligned (ptr, 32), 16)
> should optimize to true...
> 
> I guess I have big questions on what exactly will it do during constexpr
> evaluation - if it is on a pointer to object with certain alignment, for
> smaller alignment arguments guess it is just like pointer arithmetics then,
> but if it asks for larger alignment, shall it make it non-constant
> expression?

Regarding your second question: If folding is possible during constant
evaluation, clang will fold it even at -O0. If it's unknown, the LLVM IR
optimization passes will attempt to fold it (taking into account
__builtin_assume_aligned metadata if present).

For example, in:

static inline _Bool is_aligned_wrapper(void* x) {
    return __builtin_is_aligned(x, 4);
}

_Bool check_unknown(void* x) {
    return is_aligned_wrapper(x);
}

_Bool check_known_4_byte_alignment(void) {
    int i;
    return is_aligned_wrapper(&i);
}

_Bool check_assume_4_byte_alignment(void* x) {
    return is_aligned_wrapper(__builtin_assume_aligned(x, 4));
}

The check_unknown() function will perform a run-time check, the other two
simply return true.

https://godbolt.org/z/v4Gaaj

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

* [Bug c++/98641] Feature request: implement pointer alignment builtins
  2021-01-12 17:01 [Bug c++/98641] New: Feature request: implement pointer alignment builtins ktkachov at gcc dot gnu.org
                   ` (6 preceding siblings ...)
  2021-01-13 12:53 ` Alexander.Richardson at cl dot cam.ac.uk
@ 2021-01-13 12:54 ` jakub at gcc dot gnu.org
  2021-01-13 14:31 ` Alexander.Richardson at cl dot cam.ac.uk
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-01-13 12:54 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
(In reply to Alex Richardson from comment #5)
> extern int i;
> _Static_assert(__builtin_is_aligned(__builtin_assume_aligned(&i, 16), 8),
> "");
> generates an "alignment of the base pointee object (4 bytes) is less than
> the asserted 16 byte" error when evaluating __builtin_assume_aligned().

in constant expression contexts perhaps, though I'd certainly not word it that
way, if it was evaluated at runtime, it could have been true if one is lucky
(or ensures it manually, e.g. by
int alignas(16) i;
at the variable definition).

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

* [Bug c++/98641] Feature request: implement pointer alignment builtins
  2021-01-12 17:01 [Bug c++/98641] New: Feature request: implement pointer alignment builtins ktkachov at gcc dot gnu.org
                   ` (7 preceding siblings ...)
  2021-01-13 12:54 ` jakub at gcc dot gnu.org
@ 2021-01-13 14:31 ` Alexander.Richardson at cl dot cam.ac.uk
  8 siblings, 0 replies; 10+ messages in thread
From: Alexander.Richardson at cl dot cam.ac.uk @ 2021-01-13 14:31 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Alex Richardson <Alexander.Richardson at cl dot cam.ac.uk> ---
(In reply to Jakub Jelinek from comment #3)
> I guess I have big questions on what exactly will it do during constexpr
> evaluation - if it is on a pointer to object with certain alignment, for
> smaller alignment arguments guess it is just like pointer arithmetics then,
> but if it asks for larger alignment, shall it make it non-constant
> expression?

When I implemented this for clang, I made the expression no longer constant if
__builtin_is_aligned() cannot be evaluated to true (since it might actually be
true at run time).
Therefore the following gives a "non a constant expression error rather than
returning false":

extern int x;
_Static_assert(__builtin_is_aligned(&x, 8), "");

See
https://github.com/llvm/llvm-project/commit/8c387cbea76b169f1f8ecc7693797e96567ed896#diff-b69ce3cbe3c48b9fac72f4c22fe7bb425c8757b144a87c44347ff1522360460fR211

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

end of thread, other threads:[~2021-01-13 14:31 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-12 17:01 [Bug c++/98641] New: Feature request: implement pointer alignment builtins ktkachov at gcc dot gnu.org
2021-01-12 17:02 ` [Bug c++/98641] " ktkachov at gcc dot gnu.org
2021-01-13  8:11 ` rguenth at gcc dot gnu.org
2021-01-13 10:07 ` redi at gcc dot gnu.org
2021-01-13 10:18 ` jakub at gcc dot gnu.org
2021-01-13 10:21 ` redi at gcc dot gnu.org
2021-01-13 12:44 ` Alexander.Richardson at cl dot cam.ac.uk
2021-01-13 12:53 ` Alexander.Richardson at cl dot cam.ac.uk
2021-01-13 12:54 ` jakub at gcc dot gnu.org
2021-01-13 14:31 ` Alexander.Richardson at cl dot cam.ac.uk

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