From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 2E2D4385737E; Wed, 2 Nov 2022 17:46:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2E2D4385737E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667411179; bh=6Na2wnwBiUWFE/izkFLcYP4lqoxJ/ud5nKDqV3gJ6oI=; h=From:To:Subject:Date:In-Reply-To:References:From; b=tqMEv3JjxpUgJcHvx8e98QBks/E71G5BUeMowQHRE/pMJ6Me0+kMD2zJJha43KWhq xTW6bgcAJenF2tISMalsyxr7T86H3wqM7v6Lt82DImP6cAenRbGZaznkkpQpDocKuq yP+NfmGKWBkglqJdyfBo37/S+xlryj0NAzSfvzYs= From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/107500] Useless atexit entry for ~constant_init in eh_globals.cc Date: Wed, 02 Nov 2022 17:46:15 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 12.2.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: RESOLVED X-Bugzilla-Resolution: DUPLICATE X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D107500 --- Comment #15 from Jonathan Wakely --- (In reply to R. Diez from comment #13) > From your comments about "constexpr constructor" and "constinit", I gather > that the "eh_globals" singleton is guaranteed then to be initialised very > early, earlier than anything else that might use it, right? But that does At compile time, before any code executes. > not guarantee that it will be destroyed later than anything that wants to > use it, is that correct? That is why we need the hack, to make it outlive > all potential users. Yes, the destruction is dynamic, and so not ordered relative to destructors= in other translation units. > Therefore, destroying this object should have real no effect. I wonder why > there was a problem to fix in 'eh_globals' then. Read the bug. There was another global with a non-trivial destructor, which= had problems due to destructor ordering. Without using the constant_init type to make the eh_globals variable immort= al, its destructor would still logically "run" at some point before or after destruction of the other globals in that translation unit. That is true eve= n if the destructor is trivial and no code actually "runs". The current solution avoids that, because its destructor never runs. Its lifetime would end when= the storage is reused, but it's a global so that doesn't happen. The object is effectively immortal, and we don't run foul of optimizers making assumptions about object lifetime. > This all feels iffy. If I understand this correctly, it is impossible for > GCC to guarantee the correct construction and destruction order of such > global objects, and that is why we are hacking our way out. The reason is > mainly, that not all targets support "__attribute__ constructor", so there > is no way to implement a deterministic initialisation and destruction ord= er > for everybody. Is that right? I don't consider this a hack, unlike using init priority. Constant initialization is better than dynamic initialization where possibl= e, so I see no reason to forego constant initialization for dynamic initializa= tion that then needs init priority to make it work reliably. Constant init is al= ways reliable.=