From patchwork Wed Sep 7 19:41:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1079 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5044:0:0:0:0:0 with SMTP id h4csp1295502wrt; Wed, 7 Sep 2022 12:41:52 -0700 (PDT) X-Google-Smtp-Source: AA6agR4wuVii3CrG/2akm/nHA3hpX8De5BTocXmzebib5zQMqY/ydaCIWbPglvgf9i/gNgdXUEal X-Received: by 2002:a17:907:2e0d:b0:741:a3ec:7f92 with SMTP id ig13-20020a1709072e0d00b00741a3ec7f92mr3387724ejc.309.1662579712752; Wed, 07 Sep 2022 12:41:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662579712; cv=none; d=google.com; s=arc-20160816; b=Hl8GSvnD+qA5BQ2pD7bW0i0QkyvmgJkfEpR+tLLXX3U0GEvmW4zrGxla9VD8qLkdhD 7QyhFsyYzOnDPWqorA5/PPL+DeXMAN4+1PYB9+nZK06Pe2trdKUj3Fi+xs+2uYQS5RZd J7CGxc+LJfGVIewbn1D5Ci313687AnTV5OcFDQTkyy+dj8Icufe17HzvIapw0d6mM+lK OxrJHLGmhqDJq8n4/tgnrcOIVllfWItv3EoLPi4R4T7bwKvQDD/1pvE3R2hH1kWdscx6 ycQkbcDhD40qoNcMsf/hrcwMAmc6YEMXR6SKl8dAotN8hHsGuM7dLrSY7BbUGeqpmF9s CQWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=WANv7QioYmBAW9sxeV6jQKQikP4pywgIk6OsyRb3Gk8=; b=q8u4zP80B8DzOwgyVI9SQdlGquZb4sxjMafqr94KGt1e1Bewj5f4yLNHk1kUbFs6sT pZgDMqTiHxhat5+mMskwmaKDeyonv65QVEDRfeE8at6ToI4tD4ZiOW2Re/YF+0YB8BQU +4lIykY7znggNBnrjyor59/f1AMEXAYvFDtclnOf4N2sFUe4xGBlZpCZBjfz7sy4yPrn w/GwJBYHuwPrlx9VBjSRotQcXIC7KzehpSM/GfQXpywyfB/pvLG20aTJ4E7jfXqHx4o8 fsCfaZTOM+2ZkNPgba1DOmZSQMe0bK2Ufo9k71SLSdYlCa54REWGswlQfydLFS/rkiBz dpjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=uUx6BxTN; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id h9-20020a170906590900b0073d611d4265si237755ejq.687.2022.09.07.12.41.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Sep 2022 12:41:52 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=uUx6BxTN; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8D211385841A for ; Wed, 7 Sep 2022 19:41:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8D211385841A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1662579711; bh=WANv7QioYmBAW9sxeV6jQKQikP4pywgIk6OsyRb3Gk8=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=uUx6BxTNiMZGeRiHVSAKJ9wZDIZUQy29bhQbROjG06oZ+3fAVelWM0H7VrhFeW46S 6YpneoxSqXN9pL5ZZYyD/DLpawjn7wsJ+/l37r0xknao4xxmm5JIjo7NZdmE/Bx1Y4 j+2YT/HWNWswW91L6/9oyMcxvv39T22X77odQMS0= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 188373858D28 for ; Wed, 7 Sep 2022 19:41:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 188373858D28 Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-232-Q-GVwTR1PRmxsNbV_Pg73w-1; Wed, 07 Sep 2022 15:41:05 -0400 X-MC-Unique: Q-GVwTR1PRmxsNbV_Pg73w-1 Received: by mail-qv1-f69.google.com with SMTP id y16-20020a0cec10000000b004a5df9e16c6so6739572qvo.1 for ; Wed, 07 Sep 2022 12:41:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date; bh=WANv7QioYmBAW9sxeV6jQKQikP4pywgIk6OsyRb3Gk8=; b=CrXxWEa7qoWeyoyRLbpEQVYJ2V+vy5aq3dFWK3u0bs8PKjcOavDk3waVYm31fBQ6pn 6wzONjTFcYilhEBpoQpwf/vhQVaRXx0szlyKMFe4LHSPyiUJYCQnDw06Ww//FtEPeeKx q/IqFKCS+SC03umygwn3jW8bEDvZR7e+Z2pgcE4dwfF5YzEwgXTbjePFDkTWwHl3y5EP jPA9KYrwBmrPQnLD1CH2j/MEVC+JYk0wW59jmeE+sp4uyBiJAW6zBuGD+fOqwgEitsOy Omprlcd8pUNt9y9V0wdSPy4jUkxhjxq8mU0HA1r7HzQNRMBOjrj6cgWsrmjwLVj4I9lS Kq9A== X-Gm-Message-State: ACgBeo1yo1v7bJsDMGwI5nrgX4fzqnehFrPfWriyiT+3lCfrFTy6GeSZ NYQ+lrkycSHVXyvfO51/YIj6CDR84UI/+srSbSsvy9l1edVjOzuyDkEUwMI81TjHLpG9ILuhhgD HFt6U/FoLg8Fq/TQdLxiqgmV34wulj0fnMKcrqL8gdr++fKdUuUvvDXKKOoWqYKN7GFc= X-Received: by 2002:a05:6214:260a:b0:498:f11f:2945 with SMTP id gu10-20020a056214260a00b00498f11f2945mr4552488qvb.69.1662579664347; Wed, 07 Sep 2022 12:41:04 -0700 (PDT) X-Received: by 2002:a05:6214:260a:b0:498:f11f:2945 with SMTP id gu10-20020a056214260a00b00498f11f2945mr4552461qvb.69.1662579664028; Wed, 07 Sep 2022 12:41:04 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id k19-20020a05620a143300b006bb83e2e65fsm13690181qkj.42.2022.09.07.12.41.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Sep 2022 12:41:03 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: unnecessary instantiation of constexpr var [PR99130] Date: Wed, 7 Sep 2022 15:41:00 -0400 Message-Id: <20220907194100.879066-1-ppalka@redhat.com> X-Mailer: git-send-email 2.37.3.518.g79f2338b37 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.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_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1743341184916781682?= X-GMAIL-MSGID: =?utf-8?q?1743341184916781682?= Here the use of the constexpr member/variable specialization 'value' from within an unevaluated context causes us to overeagerly instantiate it, via maybe_instantiate_decl called from mark_used, despite only its declaration not its definition being needed. We used to have the same issue for constexpr function specializations until r6-1309-g81371eff9bc7ef made us delay their instantiation until necessary during constexpr evaluation. So this patch makes us avoid unnecessarily instantiating constexpr variable template specializations from mark_used as well. To that end this patch pulls out the test in maybe_instantiate_decl (decl_maybe_constant_var_p (decl) || (TREE_CODE (decl) == FUNCTION_DECL && DECL_OMP_DECLARE_REDUCTION_P (decl)) || undeduced_auto_decl (decl)) into each of its three callers (including mark_used) and refines the test appropriately. The net result is that only mark_used is changed, because the other two callers, resolve_address_of_overloaded_function and decl_constant_var_p, already guard the call appropriately. And presumably decl_constant_var_p will take care of instantiation when needed for e.g. constexpr evaluation. Bootstrapped and regteste on x86_64-pc-linux-gnu, does this look OK for trunk? PR c++/99130 gcc/cp/ChangeLog: * decl2.cc (maybe_instantiate_decl): Adjust function comment. Check VAR_OR_FUNCTION_DECL_P. Pull out the disjunction into ... (mark_used): ... here, removing the decl_maybe_constant_var_p part of it. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/var-templ70.C: New test. --- gcc/cp/decl2.cc | 33 ++++++++---------------- gcc/testsuite/g++.dg/cpp1y/var-templ70.C | 19 ++++++++++++++ 2 files changed, 30 insertions(+), 22 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1y/var-templ70.C diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc index 89ab2545d64..cd188813bee 100644 --- a/gcc/cp/decl2.cc +++ b/gcc/cp/decl2.cc @@ -5381,24 +5381,15 @@ possibly_inlined_p (tree decl) return true; } -/* Normally, we can wait until instantiation-time to synthesize DECL. - However, if DECL is a static data member initialized with a constant - or a constexpr function, we need it right now because a reference to - such a data member or a call to such function is not value-dependent. - For a function that uses auto in the return type, we need to instantiate - it to find out its type. For OpenMP user defined reductions, we need - them instantiated for reduction clauses which inline them by hand - directly. */ +/* If DECL is a function or variable template specialization, instantiate + its definition now. */ void maybe_instantiate_decl (tree decl) { - if (DECL_LANG_SPECIFIC (decl) + if (VAR_OR_FUNCTION_DECL_P (decl) + && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl) - && (decl_maybe_constant_var_p (decl) - || (TREE_CODE (decl) == FUNCTION_DECL - && DECL_OMP_DECLARE_REDUCTION_P (decl)) - || undeduced_auto_decl (decl)) && !DECL_DECLARED_CONCEPT_P (decl) && !uses_template_parms (DECL_TI_ARGS (decl))) { @@ -5700,15 +5691,13 @@ mark_used (tree decl, tsubst_flags_t complain) return false; } - /* Normally, we can wait until instantiation-time to synthesize DECL. - However, if DECL is a static data member initialized with a constant - or a constexpr function, we need it right now because a reference to - such a data member or a call to such function is not value-dependent. - For a function that uses auto in the return type, we need to instantiate - it to find out its type. For OpenMP user defined reductions, we need - them instantiated for reduction clauses which inline them by hand - directly. */ - maybe_instantiate_decl (decl); + /* If DECL has a deduced return type, we need to instantiate it now to + find out its type. For OpenMP user defined reductions, we need them + instantiated for reduction clauses which inline them by hand directly. */ + if (undeduced_auto_decl (decl) + || (TREE_CODE (decl) == FUNCTION_DECL + && DECL_OMP_DECLARE_REDUCTION_P (decl))) + maybe_instantiate_decl (decl); if (processing_template_decl || in_template_function ()) return true; diff --git a/gcc/testsuite/g++.dg/cpp1y/var-templ70.C b/gcc/testsuite/g++.dg/cpp1y/var-templ70.C new file mode 100644 index 00000000000..80965657c32 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/var-templ70.C @@ -0,0 +1,19 @@ +// PR c++/99130 +// { dg-do compile { target c++14 } } + +template +struct A { + static constexpr int value = T::value; +}; + +struct B { + template + static constexpr int value = T::value; +}; + +template +constexpr int value = T::value; + +using ty1 = decltype(A::value); +using ty2 = decltype(B::value); +using ty3 = decltype(value);