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 09190385828D for ; Thu, 2 Mar 2023 16:35:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 09190385828D 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=1677774919; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ray1lzY09yJQo/0Yf4WVNQUVXdX4kdVRUyTtTbLfAIU=; b=eQJAoGpr3dP3XD2l7wzXv8xqFEM4SrM7BArSEXgnK/06iWRyRqC6W/BxDwDtmba8ebH79e cC+K0W6lFx7M05bQIcp/d9zZ+HC35x18vQvlxTlBqH17DmYa4o9PYBCwIDeQpoxLNCoYkm KdzgvDZjuG0H99HvWupKg6uM7/4M1UA= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-76-wAeQ1BfbNjKm0wihYenJQg-1; Thu, 02 Mar 2023 11:35:09 -0500 X-MC-Unique: wAeQ1BfbNjKm0wihYenJQg-1 Received: by mail-qk1-f200.google.com with SMTP id d4-20020a05620a166400b00742859d0d4fso10075496qko.15 for ; Thu, 02 Mar 2023 08:35:09 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677774909; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ray1lzY09yJQo/0Yf4WVNQUVXdX4kdVRUyTtTbLfAIU=; b=h6PnQjatdayLgR1ujAhKT9Tqn6HukHHPtCket4mcj62sdqFOBQJkUCub4kzYt9sjlb 1wnCts4SCvWYDG1q/seUjt7UBtBNacY/SUeg+aAJRwmJu0kToQk6BbLDqK8KDH3c1Vr9 wjdH6cHs7uJ83RDWXMUxac9cnNR3wCGtqm+yF3AvrVF1K/6XvXQlAjKTADfJNx3egpO/ T2JTzc2Bra2/V78rzEKJziLgDLWbNts+X4DSfxGpIgrOKrM8USr9vwZvfhCPnFttjMHu FlJOKGeniJePGKUryi0LiYHU4H7OZyxf0jFYJM/9/ZsHLpFLAculoPsd9+JUwBcsqMCD 3Z5A== X-Gm-Message-State: AO0yUKWGIsiMiftnpV/Y6wFZa4mav+MT2xD4iZnSidCP+DIKxtrrvwpu w/GexWMGpgn9R8d3omQnrdh0hyinFrPgwwE4JekOAPb4IRpRzYn+esu1hr+r1LaQ4fZY5AyW50n UlIdcCpDSBaav4+9Zsw== X-Received: by 2002:ac8:4e88:0:b0:3bf:ca3d:6757 with SMTP id 8-20020ac84e88000000b003bfca3d6757mr19789401qtp.66.1677774909337; Thu, 02 Mar 2023 08:35:09 -0800 (PST) X-Google-Smtp-Source: AK7set/gjbemrFyLGKOVwREy7BBO/aKfJ4W3Elq6U9IfiMXo/gimffkRb+p8Qne5TeKIqDgcgCfc2w== X-Received: by 2002:ac8:4e88:0:b0:3bf:ca3d:6757 with SMTP id 8-20020ac84e88000000b003bfca3d6757mr19789354qtp.66.1677774908974; Thu, 02 Mar 2023 08:35:08 -0800 (PST) Received: from [192.168.1.108] (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id s67-20020a374546000000b007424239e4casm11220774qka.117.2023.03.02.08.35.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 02 Mar 2023 08:35:08 -0800 (PST) Message-ID: <7a297fa5-ed10-d938-3693-2d5fd45f7365@redhat.com> Date: Thu, 2 Mar 2023 11:35:06 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.8.0 Subject: Re: [PATCH] c++: constant non-copy-init is manifestly constant [PR108243] To: Patrick Palka Cc: gcc-patches@gcc.gnu.org References: <20230220194641.4172416-1-ppalka@redhat.com> <44cf5ccc-e822-70c5-84dd-2fe5aefa9492@idea> From: Jason Merrill In-Reply-To: <44cf5ccc-e822-70c5-84dd-2fe5aefa9492@idea> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,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 2/21/23 15:18, Patrick Palka wrote: > On Mon, 20 Feb 2023, Patrick Palka wrote: > >> According to [basic.start.static]/2 and [expr.const]/2, a variable >> with static storage duration initialized with a constant initializer >> has constant initialization, and such an initializer is manifestly >> constant-evaluated. >> >> We're already getting this right with copy initialization because in >> that case check_initializer would consistently call store_init_value >> (which for TREE_STATIC variables calls fold_non_dependent_init with >> m_c_e=true). >> >> But for direct (or default) initialization, we don't always call >> store_init_value. We instead however always call maybe_constant_init >> from expand_default_init[1], albeit with m_c_e=false which means we >> don't always get the "manifestly constant-evaluated" part right for >> copy-init. >> >> This patch fixes this by simply passing m_c_e=true to this call to >> maybe_constant_init for static storage duration variables, mirroring >> what store_init_value basically does. >> >> [1]: this maybe_constant_init call isn't reached in the copy-init >> case because there init is a CONSTRUCTOR rather than a TREE_LIST so >> expand_default_init exits early returning an INIT_EXPR. This INIT_EXPR >> is ultimately what causes us to consistently hit the store_init_value >> code path from check_initializer in the copy-init case. >> >> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for >> trunk? Would it be suitable to backport this to the 12 branch since >> it should only affect C++20 code? >> >> PR c++/108243 >> >> gcc/cp/ChangeLog: >> >> * init.cc (expand_default_init): Pass m_c_e=true instead of >> =false to maybe_constant_init when initializing a variable >> with static storage duration. >> >> gcc/testsuite/ChangeLog: >> >> * g++.dg/cpp2a/is-constant-evaluated14.C: New test. >> --- >> gcc/cp/init.cc | 5 +- >> .../g++.dg/cpp2a/is-constant-evaluated14.C | 140 ++++++++++++++++++ >> 2 files changed, 144 insertions(+), 1 deletion(-) >> create mode 100644 gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated14.C >> >> diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc >> index 52e96fbe590..705a5b3bdb6 100644 >> --- a/gcc/cp/init.cc >> +++ b/gcc/cp/init.cc >> @@ -2203,7 +2203,10 @@ expand_default_init (tree binfo, tree true_exp, tree exp, tree init, int flags, >> tree fn = get_callee_fndecl (rval); >> if (fn && DECL_DECLARED_CONSTEXPR_P (fn)) >> { >> - tree e = maybe_constant_init (rval, exp); >> + bool manifestly_const_eval = false; >> + if (VAR_P (exp) && TREE_STATIC (exp)) >> + manifestly_const_eval = true; >> + tree e = maybe_constant_init (rval, exp, manifestly_const_eval); >> if (TREE_CONSTANT (e)) >> rval = cp_build_init_expr (exp, e); >> } > > Hmm, alternatively we could just override manifestly_const_eval to true > from maybe_constant_init for static storage duration variables, like so. > I guess this approach much be preferable since it potentially benefits > all maybe_constant_init callers? That does look better. OK (perhaps with a local variable to hold the mce_value). > -- >8 -- > > gcc/cp/ChangeLog: > > * constexpr.cc (maybe_constant_init_1): Override > manifestly_const_eval to true if is_static. > > diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc > index aa2c14355f8..8ae83a6eadf 100644 > --- a/gcc/cp/constexpr.cc > +++ b/gcc/cp/constexpr.cc > @@ -8760,7 +8760,8 @@ maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant, > bool is_static = (decl && DECL_P (decl) > && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))); > t = cxx_eval_outermost_constant_expr (t, allow_non_constant, !is_static, > - mce_value (manifestly_const_eval), > + (is_static ? mce_true > + : mce_value (manifestly_const_eval)), > false, decl); > } > if (TREE_CODE (t) == TARGET_EXPR) > >> diff --git a/gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated14.C b/gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated14.C >> new file mode 100644 >> index 00000000000..365bca3fd9a >> --- /dev/null >> +++ b/gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated14.C >> @@ -0,0 +1,140 @@ >> +// PR c++/108243 >> +// Verify a variable with static storage duration initialized with a >> +// constant initializer has constant initialization, and the initializer >> +// is manifestly constant-evaluated. >> +// { dg-do run { target c++11 } } >> +// { dg-additional-options "-fdump-tree-original" } >> + >> +#include >> + >> +struct A { >> + constexpr A(int n) : n(n), m(__builtin_is_constant_evaluated()) { } >> + constexpr A() : A(42) { } >> + void verify_mce() const { >> + if (m != 1) __builtin_abort(); >> + } >> + int n; >> + int m; >> +}; >> + >> +A a1 = {42}; >> +A a2{42}; >> +A a3(42); >> +A a4; >> +A a5{}; >> + >> +void f() { >> + static A a1 = {42}; >> + static A a2{42}; >> + static A a3(42); >> + static A a4; >> + static A a5{}; >> + for (auto& a : {a1, a2, a3, a4, a5}) >> + a.verify_mce(); >> +} >> + >> +template >> +void g() { >> + static A a1 = {42}; >> + static A a2{42}; >> + static A a3(42); >> + static A a4; >> + static A a5{}; >> + static A a6 = {N...}; >> + static A a7{N...}; >> + static A a8(N...); >> + for (auto& a : {a1, a2, a3, a4, a5, a6, a7, a8}) >> + a.verify_mce(); >> +} >> + >> +struct B { >> + static A a1; >> + static A a2; >> + static A a3; >> + static A a4; >> + static A a5; >> + static void verify_mce() { >> + for (auto& a : {a1, a2, a3, a4, a5}) >> + a.verify_mce(); >> + } >> +}; >> + >> +A B::a1 = {42}; >> +A B::a2{42}; >> +A B::a3(42); >> +A B::a4; >> +A B::a5{}; >> + >> +template >> +struct BT { >> + static A a1; >> + static A a2; >> + static A a3; >> + static A a4; >> + static A a5; >> + static A a6; >> + static A a7; >> + static A a8; >> + static void verify_mce() { >> + for (auto& a : {a1, a2, a3, a4, a5}) >> + a.verify_mce(); >> + } >> +}; >> + >> +template A BT::a1 = {42}; >> +template A BT::a2{42}; >> +template A BT::a3(42); >> +template A BT::a4; >> +template A BT::a5{}; >> +template A BT::a6 = {N...}; >> +template A BT::a7{N...}; >> +template A BT::a8(N...); >> + >> +#if __cpp_inline_variables >> +struct BI { >> + static inline A a1 = {42}; >> + static inline A a2{42}; >> + static inline A a3; >> + static inline A a4{}; >> + static void verify_mce() { >> + for (auto& a : {a1, a2, a3, a4}) >> + a.verify_mce(); >> + } >> +}; >> + >> +template >> +struct BIT { >> + static inline A a1 = {42}; >> + static inline A a2{42}; >> + static inline A a3; >> + static inline A a4{}; >> + static inline A a5 = {N...}; >> + static inline A a6{N...}; >> + static void verify_mce() { >> + for (auto& a : {a1, a2, a3, a4, a5, a6}) >> + a.verify_mce(); >> + } >> +}; >> +#endif >> + >> +int main() { >> + for (auto& a : {a1, a2, a3, a4, a5}) >> + a.verify_mce(); >> + >> + f(); >> + g<42>(); >> + g<>(); >> + >> + B::verify_mce(); >> + BT<42>::verify_mce(); >> + BT<>::verify_mce(); >> + >> +#if __cpp_inline_variables >> + BI::verify_mce(); >> + BIT<42>::verify_mce(); >> + BIT<>::verify_mce(); >> +#endif >> +} >> + >> +// { dg-final { scan-tree-dump-not "static initializers for" "original" } } >> +// { dg-final { scan-tree-dump-not "cxa_guard_acquire" "original" } } >> -- >> 2.39.2.501.gd9d677b2d8 >> >> >