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.129.124]) by sourceware.org (Postfix) with ESMTPS id EA73F3858CDB for ; Thu, 2 Mar 2023 16:48:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EA73F3858CDB 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=1677775689; 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=b0O6myzQ/Q/Tg9dFlI72/no0I9+8uMRliVllORNWR2E=; b=X4TaGHQ3OrPmLTGHqo6TyqG+MhKsFpzZFekXAh2Oy5nFDperg3wrbKrtpzOylA2BT615cj ggHv5OuXWM/Q8U9GmELDZRquNv/j6EclTtLNQ5uXpUdrv+RqfKGNzQWOZTgBaIBAEOWgsi HQ8Nn8HLwtXsynVgjwdahDnwMOy/I0k= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-245-wx4Hqo-lPdC0Dw99MWqSgQ-1; Thu, 02 Mar 2023 11:48:08 -0500 X-MC-Unique: wx4Hqo-lPdC0Dw99MWqSgQ-1 Received: by mail-qk1-f199.google.com with SMTP id eb13-20020a05620a480d00b00742be8a1a17so6978074qkb.18 for ; Thu, 02 Mar 2023 08:48:08 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677775688; 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=b0O6myzQ/Q/Tg9dFlI72/no0I9+8uMRliVllORNWR2E=; b=SyPDElS0XaEu1KjaDX5Ejv+Eg+xma5o1IQqj2xoU5bDncpkInJ0V1XyiHUsR2gAa1E uIQCZ6Z2scfZWbmZU2tN7Vr2Dj8fb4kBD6BngLTtMSry5DPqap256S82Nm1afOMtnKdM 023ONzcvQPn9enWkiWOdq5WLk3bJldanXu+ZnH1AlY8ELf19tCivPNpRXHicCU0i9484 N9t+NwJvZG1W5fsgaSxWutOCQ+VDfj7S5J6dNSV5oxhsYhpC44IAs/e2xGETJff1P4X+ VU9EBjtKM8MPy63fvFqiJvih52dcEfMP/yVKO+U5rk5nR147X63QZabIkhXxASNGsaMU AQWg== X-Gm-Message-State: AO0yUKVaQcWI4Ykcj7IAcO4FfOiBboGeFy8yiFIFktFt1Yissk0m3f4b smpw/8Th89Cqiz9FTPahd0opNKDoYZipAZmcMkXaSnB+AcTxvOAeg79l4AE63HlZBX9PT6jl+wH h+TTocSQ+fOAfxZIrDz0c4oA= X-Received: by 2002:a05:622a:609:b0:3bd:1a02:60c8 with SMTP id z9-20020a05622a060900b003bd1a0260c8mr17922289qta.37.1677775687728; Thu, 02 Mar 2023 08:48:07 -0800 (PST) X-Google-Smtp-Source: AK7set93JCNQGurSmUJIbG46LbyBZ4rH7DxUhFNIoKNgrj2vqNKhHuEc616UvOL7s3igNj/kxiLP1w== X-Received: by 2002:a05:622a:609:b0:3bd:1a02:60c8 with SMTP id z9-20020a05622a060900b003bd1a0260c8mr17922263qta.37.1677775687335; Thu, 02 Mar 2023 08:48:07 -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 h21-20020ac87155000000b003b85ed59fa2sm10489068qtp.50.2023.03.02.08.48.06 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 02 Mar 2023 08:48:06 -0800 (PST) Message-ID: <5aff4f89-5f00-c71a-5681-3d481a354c9b@redhat.com> Date: Thu, 2 Mar 2023 11:48:04 -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++: Don't defer local statics initialized with constant expressions [PR108702] To: Jakub Jelinek Cc: gcc-patches@gcc.gnu.org References: From: Jason Merrill In-Reply-To: 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=-6.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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/9/23 11:14, Jakub Jelinek wrote: > Hi! > > The stmtexpr19.C testcase used to be rejected as it has a static > variable in statement expression in constexpr context, but as that > static variable is initialized by constant expression, when P2647R1 > was implemented we agreed to make it valid. > > Now, as reported, the testcase compiles fine, but doesn't actually link > because the static variable isn't defined anywhere, and with -flto ICEs > because of this problem. This is because we never > varpool_node::finalize_decl those vars, the constant expression in which > the DECL_EXPR is present for the static VAR_DECL is folded (constant > evaluated) into just the address of the VAR_DECL. Would it make sense to define it when we see the DECL_EXPR in constant evaluation? > Now, similar testcase included below (do we want to include it in the > testsuite too?) works fine, because in > cp_finish_decl -> make_rtl_for_nonlocal_decl > we have since PR70353 fix: > /* We defer emission of local statics until the corresponding > DECL_EXPR is expanded. But with constexpr its function might never > be expanded, so go ahead and tell cgraph about the variable now. */ > defer_p = ((DECL_FUNCTION_SCOPE_P (decl) > && !var_in_maybe_constexpr_fn (decl)) > || DECL_VIRTUAL_P (decl)); > and so don't defer them in constexpr/consteval functions. The following > patch extends that and doesn't defer vars initialized by constant > expressions either, because otherwise there is nothing to finalize those. > It is true that e.g. with -O0 > int foo (int x) { > if (x) { static int y = 1; ++y; } > if (0) { static int z = 1; ++z; } > return sizeof (({ static int w = 1; w; })); > } > we used to emit just y and z and with the patch emit also w, but with > optimizations that is optimized away properly. > > Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? > > The testcase I was talking above that works because of the > && !var_in_maybe_constexpr_fn (decl) case is: > > extern "C" void abort (); > > constexpr const int * > foo () > { > static constexpr int a = 1; > return &a; > } > > consteval const int * > bar () > { > static constexpr int a = 1; > return &a; > } > > [[gnu::noipa]] void > baz (const int *x) > { > if (*x != 1) > abort (); > } > > int > main () > { > constexpr const int *p = foo (); > constexpr const int *q = bar (); > baz (p); > baz (q); > if (p == q) > abort (); > } > > 2023-02-09 Jakub Jelinek > > PR c++/108702 > * decl.cc (make_rtl_for_nonlocal_decl): Don't defer local statics > initialized by constant expressions. > > * g++.dg/ext/stmtexpr19.C: Use dg-do link rather than dg-do compile. > > --- gcc/cp/decl.cc.jj 2023-01-24 11:10:13.151076134 +0100 > +++ gcc/cp/decl.cc 2023-02-09 13:29:50.527083618 +0100 > @@ -7731,9 +7731,12 @@ make_rtl_for_nonlocal_decl (tree decl, t > > /* We defer emission of local statics until the corresponding > DECL_EXPR is expanded. But with constexpr its function might never > - be expanded, so go ahead and tell cgraph about the variable now. */ > + be expanded, so go ahead and tell cgraph about the variable now. > + Also don't defer local statics initialized by constant expressions, > + see PR108702. */ > defer_p = ((DECL_FUNCTION_SCOPE_P (decl) > - && !var_in_maybe_constexpr_fn (decl)) > + && !var_in_maybe_constexpr_fn (decl) > + && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)) > || DECL_VIRTUAL_P (decl)); > > /* Defer template instantiations. */ > --- gcc/testsuite/g++.dg/ext/stmtexpr19.C.jj 2022-11-19 09:26:30.168061316 +0100 > +++ gcc/testsuite/g++.dg/ext/stmtexpr19.C 2023-02-09 13:32:48.887453520 +0100 > @@ -1,6 +1,6 @@ > // PR c++/81073 > // { dg-options "" } > -// { dg-do compile { target c++11 } } > +// { dg-do link { target c++11 } } > > struct test { const int *addr; }; > > > Jakub >