public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/114900] New: Overaligned locals in coroutines are not properly aligned
@ 2024-04-30 19:39 jankodedic2 at gmail dot com
  2024-04-30 19:41 ` [Bug c++/114900] " pinskia at gcc dot gnu.org
  2024-04-30 19:42 ` pinskia at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: jankodedic2 at gmail dot com @ 2024-04-30 19:39 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 114900
           Summary: Overaligned locals in coroutines are not properly
                    aligned
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jankodedic2 at gmail dot com
  Target Milestone: ---

GCC 13.2.0 (x86-64)
Flags: -std=c++20
Godbolt link: https://godbolt.org/z/f3KGaGWW6

Overaligned locals in coroutines are not properly aligned. If a local variable
is marked with alignas(n), GCC seemingly does nothing about it. If a local
variable just has an overaligned type, GCC seems to overallocate to try and
align it correctly but alignment is still incorrect (overallocation is probably
the only solution until P2014 is resolved -
https://github.com/cplusplus/papers/issues/750).

Full example pasted below:

```
#include <coroutine>
#include <cstdlib>
#include <cstdint>
#include <iostream>

static constexpr std::size_t alignment = 1 << 13;

bool
is_aligned(void* p)
{
    return ((std::uintptr_t)p % alignment) == 0;
}

void* malloc_address = nullptr;
void* local_address = nullptr;

struct coro
{
    std::coroutine_handle<> coroutine_handle;

    struct promise_type
    {
        void*
        operator new(std::size_t n)
        {
            std::cout << "allocating " << n << " bytes\n";
            malloc_address = std::malloc(n);
            return malloc_address;
        }

        void
        operator delete(void* p)
        {
            std::free(p);
        }

        std::suspend_never
        initial_suspend()
        {
            return {};
        }

        coro
        get_return_object()
        {
            return {std::coroutine_handle<promise_type>::from_promise(*this)};
        }

        void
        return_void()
        {}

        void
        unhandled_exception()
        {}

        std::suspend_always
        final_suspend() noexcept
        {
            return {};
        }
    };
};

struct S
{
    alignas(alignment) int x = 42;
};

coro
foo()
{
#if 0
    alignas(alignment) int x = 42;
#else
    S x;
#endif

    local_address = &x;
    std::cout << "is aligned? " << is_aligned(local_address) << std::endl;
    co_return;
}

int
main()
{
    std::cout << "max align: " << alignof(std::max_align_t) << std::endl;
    auto c = foo();
    std::cout << "malloc_address: " << malloc_address << std::endl;
    std::cout << "local_address: " << local_address << std::endl;
    std::cout << "difference: " << ((char*)local_address -
(char*)malloc_address) << std::endl;
}
```

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

* [Bug c++/114900] Overaligned locals in coroutines are not properly aligned
  2024-04-30 19:39 [Bug c++/114900] New: Overaligned locals in coroutines are not properly aligned jankodedic2 at gmail dot com
@ 2024-04-30 19:41 ` pinskia at gcc dot gnu.org
  2024-04-30 19:42 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-30 19:41 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This is by design for C++20 ...

The support for P2014 is not in GCC yet, and IIRC it won't be enable for C++20
only C++23+.

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

* [Bug c++/114900] Overaligned locals in coroutines are not properly aligned
  2024-04-30 19:39 [Bug c++/114900] New: Overaligned locals in coroutines are not properly aligned jankodedic2 at gmail dot com
  2024-04-30 19:41 ` [Bug c++/114900] " pinskia at gcc dot gnu.org
@ 2024-04-30 19:42 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-30 19:42 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Anyways it is a dup of bug 104177.

*** This bug has been marked as a duplicate of bug 104177 ***

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

end of thread, other threads:[~2024-04-30 19:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-30 19:39 [Bug c++/114900] New: Overaligned locals in coroutines are not properly aligned jankodedic2 at gmail dot com
2024-04-30 19:41 ` [Bug c++/114900] " pinskia at gcc dot gnu.org
2024-04-30 19:42 ` pinskia 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).