From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 5B6373858426 for ; Fri, 4 Nov 2022 15:25:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5B6373858426 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1667575513; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=B4iYnbVKgaipKZWOT8hX9wvCIqRK69JdIgxFKPNuMtc=; b=R3NzkqXtuZt/SUucHrqBvej09xK79CbRqtazHLVKR8/kWM+nWnp53xxqrD/t9pBTI21KUk qThvHMtES5CkaPiYxUB9JhsZ2Lyo5Q4GQWD0nkNwv+E0EN6lM7y+iEgRtVp9oqo9vatA2j ZokSowwrGaMbi3zf1IwmtUbWHlOciCw= Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-218-iRS19ImSMb2NauCfdG668g-1; Fri, 04 Nov 2022 11:25:12 -0400 X-MC-Unique: iRS19ImSMb2NauCfdG668g-1 Received: by mail-ej1-f69.google.com with SMTP id xj11-20020a170906db0b00b0077b6ecb23fcso3509161ejb.5 for ; Fri, 04 Nov 2022 08:25:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=B4iYnbVKgaipKZWOT8hX9wvCIqRK69JdIgxFKPNuMtc=; b=jxxdfu4JeQB80um+9fvzIrz33R91EshekVgTvbqpjztYHQRysRMXRZbS11QOO/tgm6 m9pPmTGkuwGaSX6zO6f/w/VDOmukZ6apU3waLhWEQcvzGeEVJY88FLf/RbmUeisogFI0 bTbJhHEOGmmJsGqDCpuk+IFkgowckZGwONDJ5S/EDRjo39ugGW3fCoH4JksYp9aBKCM5 qhlThhBUfQmkkYJWpJzkfnwbcIp5RpWNdsTHO/N2dJxKRs2HW+n86co1xz/TqmWXvF59 GfVwDgqalPPiEMsNGkWMVvj+/r64Tu6fVaQF+rkd+jh2e4XYG1m76AEFDPLbfyOualUN sx0A== X-Gm-Message-State: ACrzQf1AdjMwjwGfiX6O+iLaG4VpmR6Jp9Gd/2tA4eff7gA437iw1j+i QYVPtBq4hHFEWmfdqDGAGv03IGA1gTu3W8qpk0QtyW7xQO5qZjJBD3Oz1aJWdzM+LuxeIqgtd+M hszWkj0ph3G2DBv8uMbNODqrNjIh2wWc= X-Received: by 2002:a05:6402:13d9:b0:463:398a:75af with SMTP id a25-20020a05640213d900b00463398a75afmr29942202edx.328.1667575510797; Fri, 04 Nov 2022 08:25:10 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7OPLejfIBJyvkPemOcBQOSjue/5G7kWavhqLhAsjARtx1iX/nij6WOZZktQBakv3BsJc5TOQUH6SPL1uvxlhw= X-Received: by 2002:a05:6402:13d9:b0:463:398a:75af with SMTP id a25-20020a05640213d900b00463398a75afmr29942184edx.328.1667575510564; Fri, 04 Nov 2022 08:25:10 -0700 (PDT) MIME-Version: 1.0 References: <20221104150525.2968778-1-ppalka@redhat.com> <20221104150525.2968778-2-ppalka@redhat.com> In-Reply-To: <20221104150525.2968778-2-ppalka@redhat.com> From: Jonathan Wakely Date: Fri, 4 Nov 2022 15:24:59 +0000 Message-ID: Subject: Re: [PATCH 2/2] libstdc++: Move stream initialization into compiled library [PR44952] To: Patrick Palka Cc: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org, jason@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Fri, 4 Nov 2022 at 15:08, Patrick Palka via Libstdc++ wrote: > > This patch moves the global object for constructing the standard streams > out from and into the compiled library on targets that support > the init_priority attribute. This means that no longer > introduces a separate global constructor in each TU that includes it. > > We can do this only if the init_priority attribute is supported because > we need to force that the stream initialization runs first before any > user-defined global initializer in programs that that use a static > libstdc++.a. > > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look right? > Unfortunately I don't have access to a system that truly doesn't support > init priorities, so I instead tested that situation by artificially > disabling the init_priority attribute on x86_64. > > PR libstdc++/44952 > PR libstdc++/98108 > > libstdc++-v3/ChangeLog: > > * include/bits/c++config (_GLIBCXX_HAS_ATTRIBUTE): Define. > (_GLIBCXX_HAVE_ATTRIBUTE_INIT_PRIORITY): Define. > * include/std/iostream (__ioinit): Define only if init_priority > attribute isn't usable. > * src/c++98/ios_init.cc (__ioinit): Define here instead if > the init_priority is usable. > * src/c++98/ios_base_init.h: New file. > --- > libstdc++-v3/include/bits/c++config | 12 ++++++++++++ > libstdc++-v3/include/std/iostream | 4 ++++ > libstdc++-v3/src/c++98/ios_base_init.h | 9 +++++++++ > libstdc++-v3/src/c++98/ios_init.cc | 4 ++++ > 4 files changed, 29 insertions(+) > create mode 100644 libstdc++-v3/src/c++98/ios_base_init.h > > diff --git a/libstdc++-v3/include/bits/c++config b/libstdc++-v3/include/bits/c++config > index 50406066afe..f93076191d9 100644 > --- a/libstdc++-v3/include/bits/c++config > +++ b/libstdc++-v3/include/bits/c++config > @@ -837,6 +837,18 @@ namespace __gnu_cxx > > #undef _GLIBCXX_HAS_BUILTIN > > +#ifdef __has_attribute Do we need this? I think all the compilers we support implemented this long ago (clang had it before gcc, and Intel has it for gcc compat, and any others had better have it by now too). So we can just use #if __has_attribute directly, instead of defining these extra macros. > +# define _GLIBCXX_HAS_ATTRIBUTE(B) __has_attribute(B) > +#else > +# define _GLIBCXX_HAS_ATTRIBUTE(B) 0 > +#endif > + > +#if _GLIBCXX_HAS_ATTRIBUTE(init_priority) > +# define _GLIBCXX_HAVE_ATTRIBUTE_INIT_PRIORITY 1 > +#endif > + > +#undef _GLIBCXX_HAS_ATTRIBUTE > + > // Mark code that should be ignored by the compiler, but seen by Doxygen. > #define _GLIBCXX_DOXYGEN_ONLY(X) > > diff --git a/libstdc++-v3/include/std/iostream b/libstdc++-v3/include/std/iostream > index 70318a45891..5eaa9755d9a 100644 > --- a/libstdc++-v3/include/std/iostream > +++ b/libstdc++-v3/include/std/iostream > @@ -73,7 +73,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION > ///@} > > // For construction of filebuffers for cout, cin, cerr, clog et. al. > + // When the init_priority attribute is usable, we do this initialization > + // in the compiled library instead (see src/c++98/ios_init.cc). > +#if !_GLIBCXX_HAVE_ATTRIBUTE_INIT_PRIORITY > static ios_base::Init __ioinit; > +#endif > > _GLIBCXX_END_NAMESPACE_VERSION > } // namespace > diff --git a/libstdc++-v3/src/c++98/ios_base_init.h b/libstdc++-v3/src/c++98/ios_base_init.h > new file mode 100644 > index 00000000000..f3087d1da3c > --- /dev/null > +++ b/libstdc++-v3/src/c++98/ios_base_init.h > @@ -0,0 +1,9 @@ > +// This is only in a header so we can use the system_header pragma, > +// to suppress the warning caused by using a reserved init_priority. > +#pragma GCC system_header Ugh, that's annoying. > + > +#if !_GLIBCXX_HAVE_ATTRIBUTE_INIT_PRIORITY > +# error "This file should not be included for this build" > +#endif > + > +static ios_base::Init __ioinit __attribute__((init_priority(90))); > diff --git a/libstdc++-v3/src/c++98/ios_init.cc b/libstdc++-v3/src/c++98/ios_init.cc > index 1b5132f1c2d..954fa9f29cf 100644 > --- a/libstdc++-v3/src/c++98/ios_init.cc > +++ b/libstdc++-v3/src/c++98/ios_init.cc > @@ -75,6 +75,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION > extern wostream wclog; > #endif > > +#if _GLIBCXX_HAVE_ATTRIBUTE_INIT_PRIORITY > +# include "ios_base_init.h" > +#endif > + > ios_base::Init::Init() > { > if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, 1) == 0) > -- > 2.38.1.385.g3b08839926 >