Bug 106658 - [C++23] P2590 - Explicit lifetime management
Summary: [C++23] P2590 - Explicit lifetime management
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 16.0
: P3 normal
Target Milestone: 16.0
Assignee: Jakub Jelinek
URL:
Keywords: c++23
Depends on:
Blocks: c++23-core 117982
  Show dependency tree / graph
 
Reported: 2022-08-16 17:33 UTC by Marek Polacek
Modified: 2026-02-16 13:39 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2022-12-09 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Marek Polacek 2022-08-16 17:33:30 UTC
See <https://wg21.link/p2590>.
Comment 1 Richard Biener 2022-08-17 10:18:32 UTC
1.1 can be done without any code but 1.2 makes std::start_lifetime_at <T> (p)
retain the memory bit representation as initial value of the object?  The
paper mentions std::launder (ick), but I'm not sure how this is implementable
at all if not sufficiently restricted.
Comment 2 Richard Biener 2022-08-17 11:09:55 UTC
See for example PR101641 where even existing cases are currently impossible to get right.  So the only way to make std::start_lifetime_at <T> is to somehow
emit a hard memory barrier for the compiler _and_ make sure the underlying
storage is address-taken.  Note this barrier has to prevail in RTL as well
(I think std::launder is broken in this regard).
Comment 3 Drea Pinski 2022-12-09 15:27:58 UTC
.
Comment 4 Jakub Jelinek 2024-08-14 15:36:15 UTC
Do we really need to do anything at all (except defining the new stuff in a header), or possibly using __builtin_launder in it?
I mean, my understanding of the start lifetime as something is as if with -flifetime-dse=1 there was a call to a constructor which just did nothing at all (like e.g. the struct X { int a, b; }; constructor does).  And when we inline such an constructor, there is no CLOBBER and no code at all, so in the end nothing.
__builtin_launder is just an optimization barrier (and indeed it can look like the address escapes).

I think PR101641 is just bad example, we do allow type punning through union, but only if we can see it being done.  The testcase is trying to change active union member through just a store through a pointer to union member, that is not what we support I believe.
Comment 6 GCC Commits 2025-09-18 05:48:15 UTC
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

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

commit r16-3944-gc1e1691b95e65c53a2b9c25de70c56655215b905
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Thu Sep 18 07:44:54 2025 +0200

    libstdc++: Implement C++23 P2590R2 - Explicit lifetime management [PR106658]
    
    As I can't think of how the middle-end would treat
    __builtin_start_lifetime_as other than a blackbox and probably would
    need to be implemented as such inline asm in RTL, this patch
    just implements it using inline asm in the library.
    If not anything else, it can serve as fallback before we and/or clang
    get some builtin for it.
    
    Right now the inline asms pretend (potential) read from and write to the whole
    memory region and make optimizers forget where the return value points to.
    If the optimizers don't know where it points to, I think that should be
    good enough, but I'm a little bit afraid of possibly future optimizations
    trying to optimize
      q->c = 1;
      q->d = 2;
      auto p = std::start_lifetime_as<S>(q);
      if (p == reinterpret_cast<decltype (p)>(q))
        return p->a + p->b;
    that because of the guarding condition or perhaps assertion we could
    simply use the q pointer in MEM_REFs with S type and be surprised by TBAA.
    Though if it is a must-alias case, then we should be fine as well.
    Though guess that would be the same case with a builtin.
    
    2025-09-18  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/106658
            * include/bits/version.def: Implement C++23 P2590R2 - Explicit
            lifetime management.
            (start_lifetime_as): New.
            * include/bits/version.h: Regenerate.
            * include/std/memory (std::start_lifetime_as,
            std::start_lifetime_as_array): New function templates.
            * src/c++23/std.cc.in (std::start_lifetime_as,
            std::start_lifetime_as_array): Export.
            * testsuite/std/memory/start_lifetime_as/start_lifetime_as.cc: New test.
Comment 7 Jakub Jelinek 2025-09-18 05:51:56 UTC
Implemented for GCC 16.
Comment 8 GCC Commits 2025-09-22 13:59:46 UTC
The master branch has been updated by Tomasz Kaminski <tkaminsk@gcc.gnu.org>:

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

commit r16-4023-gf8681ec8d0a778c62cf6d921e00a8714529ecca8
Author: Tomasz KamiÅski <tkaminsk@redhat.com>
Date:   Mon Sep 22 13:31:17 2025 +0200

    libstdc++: Move start_lifetime_as functions to bits/stl_construct.h [PR106658]
    
    This allows inplace_vector to use these functions without including the entire
    <memory> header.
    
    Preprocessor checks are changed to use __glibcxx macros, so new functions are
    available outside memory header, that exports __cpp_lib macros.
    
            PR libstdc++/106658
    
    libstdc++-v3/ChangeLog:
    
            * include/bits/stl_construct.h (std::start_lifetime_as_array)
            (std::start_lifetime_as): Moved from std/memory, with update
            to guard macros.
            * include/std/memory (std::start_lifetime_as_array)
            (std::start_lifetime_as): Moved to bits/stl_construct.h.
    
    Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
    Signed-off-by: Tomasz KamiÅski <tkaminsk@redhat.com>