From patchwork Tue Sep 19 15:04:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 141946 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp3457341vqi; Tue, 19 Sep 2023 08:05:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF4dPa5Rtxd03m+uPwOepF0jfBK5P88zNuW7vkVRgK10+Jg0OpFNBm10TiaX9VpdSW17AxO X-Received: by 2002:a05:6512:3caa:b0:503:fee:5849 with SMTP id h42-20020a0565123caa00b005030fee5849mr7780698lfv.53.1695135924744; Tue, 19 Sep 2023 08:05:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695135924; cv=none; d=google.com; s=arc-20160816; b=lDnacKO9Sj/tzrucRhqtZ/HibqMaea2g33H96YHgtROyw/AWZD7xg5OxJ2hDOy/y5u fd+LlaWyh8c/wHF/Fk3f5DBU5fuezDHTblOs+jWIx+knSmG7oGCtAyYeRvFMWMzxBU/1 Lxt9g5p9PnEO5N8LXmo2DsiOSnPB2BPGfeWpVnbWwTJgKzXcTl/CzfTLLwPDh6qNiDjh ot0We+aiSuIdq0gpXLNeRMmDflCO/yOpivAIUP79vp9SlXOUpeyjnNzRcbVk4TiJbJv8 1IxBGE6hNGEbW2B2JvW0CSbhNvIFoM9rjCOy/mMRASRkxjihUNE8K5wWFY0feP2UV3wN neLw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :dmarc-filter:delivered-to; bh=qa65OEctZDRZFSX2nINN54XjOc/eWxpRzpe7BCWktd0=; fh=1Hi9m88IqcWZGtfSxUrMGG+GBgL26N02X1SANXM6iTs=; b=lABoc/1c/dRvCYDXu+pGAAB+FS83ST7HIgIqyJL7hGyNNgBsEPxaTTjR341XTo3583 PM6nAipUelJrxoulxsiiyCFi2P4JJjp8RWh6RvMaXK5QEK0WUpyYVf11kjieT31208IQ 9oR6TXVZxpToCPfnRPy90vq6UsSnQtp+RgCpJDdEG2awky+xmwIkaWF0s8BJF6MNlgZM Dp4oANbv1yJVTCi0UP/Hvd/OdynTQuW8Vgnm5Y1kurkOesnz8XtzcVL0NBdcpdbtZptM M8FubknAIKDI4BYA6KVjlWJLnDky7EqoCsx4FMA4Gf6/sKFoMV2oPsOmvy6OTZZr9YNq phFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=aEQIs5Bk; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id s14-20020a056402164e00b005313e896bd9si2466440edx.522.2023.09.19.08.05.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Sep 2023 08:05:24 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=aEQIs5Bk; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E7B8E385558F for ; Tue, 19 Sep 2023 15:05:16 +0000 (GMT) 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 49D5F3858C2F for ; Tue, 19 Sep 2023 15:04:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 49D5F3858C2F 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=1695135889; 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; bh=qa65OEctZDRZFSX2nINN54XjOc/eWxpRzpe7BCWktd0=; b=aEQIs5BktySqwphouscLJc4r+XbNJecRMzWJLaBwhDAH9/XZTvq95IR1La9TaNr32o5Juk VC2rYg5XNA4WyP4n3R3Ik/v3vXkMvj8WdqrglvzSmykSAHy82NAU/SoNcN5wgK72/Wd9cn VQb11sLeBq9KKxUdBIJqpU5n/lk5dCs= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-260-uWVZ_MnWOxGJidItrMKt7Q-1; Tue, 19 Sep 2023 11:04:47 -0400 X-MC-Unique: uWVZ_MnWOxGJidItrMKt7Q-1 Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-6586292396eso4672686d6.1 for ; Tue, 19 Sep 2023 08:04:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695135887; x=1695740687; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=qa65OEctZDRZFSX2nINN54XjOc/eWxpRzpe7BCWktd0=; b=a/EiyrZonA+VX1L8SorTnwnT3dgNVYo0oxDYRTSHOy/T4jNobM5Ywx4Fx//208mqls w2XcRxExSFLLZlow2IJmvDSvRCC6vMfizYY1GRMdUp7YS3wjzkVBXVlJNPoPAz3iI2IZ 7cKOCRlOiqsw/tHEZttHsqZ1KPm1rFCo0s+X13zWFJ/7rZD4WpdBF0tBrvZZr1v9ebBF 5rKeHFvcvlQuLmufRfNie4nErMQwXXOMGYHPJ5CvaeD6MOayBa2bDIyIButdhSmrSeH/ eRT4UXZE7zYpKXUa0NRIlI5r5AI3vo6CgSdvM00/aUhZv7D/ogeJdgLo0sMPfeXtxWsq 316w== X-Gm-Message-State: AOJu0YzB8O0UOxwYaxlbPqcB+5kE26GVxz/JN4IAe2/2Rq0nORUJzLuk 710+zaD0hl7FwxcQ16kyvLsog3UmyxoIvqiKkmmZQ9FTYsc6L5FTpm88JdNen+2g2P8FfBmPsR5 X1c5FPGKP9UCncJThMagmee5n4883P8hYPs9onlWlX3OpwnevL8DMf540+DWQktNn1UB2EEyfip c= X-Received: by 2002:ad4:5a11:0:b0:656:5535:ef27 with SMTP id ei17-20020ad45a11000000b006565535ef27mr7730953qvb.48.1695135886791; Tue, 19 Sep 2023 08:04:46 -0700 (PDT) X-Received: by 2002:ad4:5a11:0:b0:656:5535:ef27 with SMTP id ei17-20020ad45a11000000b006565535ef27mr7730916qvb.48.1695135886366; Tue, 19 Sep 2023 08:04:46 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id f6-20020a0ccc86000000b0064906cfe430sm2808813qvl.135.2023.09.19.08.04.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Sep 2023 08:04:46 -0700 (PDT) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: further optimize tsubst_template_decl Date: Tue, 19 Sep 2023 11:04:44 -0400 Message-ID: <20230919150444.356437-1-ppalka@redhat.com> X-Mailer: git-send-email 2.42.0.216.gbda494f404 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.6 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_H3, RCVD_IN_MSPIKE_WL, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777478847224641050 X-GMAIL-MSGID: 1777478847224641050 Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? -- >8 -- This patch makes tsubst_template_decl use use_spec_table=false also in the non-class non-function template case, to avoid computing 'argvec' and doing a hash table lookup from tsubst_decl (when partially instantiating a member variable or alias template). This change reveals that for function templates, tsubst_template_decl registers the partially instantiated TEMPLATE_DECL, whereas for other non-class templates it registers the corresponding DECL_TEMPLATE_RESULT which is an interesting inconsistency that I decided to preserve for now. Trying to consistently register the TEMPLATE_DECL (or FUNCTION_DECL) causes modules crashes, but I haven't looked into why. In passing, I noticed in tsubst_function_decl that its 'argvec' goes unused when 'lambda_fntype' is set (since lambdas aren't recorded in the specializations table), so we can avoid computing it in that case. gcc/cp/ChangeLog: * pt.cc (tsubst_function_decl): Don't bother computing 'argvec' when 'lambda_fntype' is set. (tsubst_template_decl): Make sure we return a TEMPLATE_DECL after specialization lookup. In the non-class non-function template case, use tsubst_decl directly with use_spec_table=false, update DECL_TI_ARGS and call register_specialization like tsubst_decl would have done if use_spec_table=true. --- gcc/cp/pt.cc | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 777ff592789..cc8ba21d6fd 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -14370,7 +14370,7 @@ tsubst_function_decl (tree t, tree args, tsubst_flags_t complain, /* Calculate the complete set of arguments used to specialize R. */ - if (use_spec_table) + if (use_spec_table && !lambda_fntype) { argvec = tsubst_template_args (DECL_TI_ARGS (DECL_TEMPLATE_RESULT @@ -14380,14 +14380,11 @@ tsubst_function_decl (tree t, tree args, tsubst_flags_t complain, return error_mark_node; /* Check to see if we already have this specialization. */ - if (!lambda_fntype) - { - hash = spec_hasher::hash (gen_tmpl, argvec); - if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash)) - /* The spec for these args might be a partial instantiation of the - template, but here what we want is the FUNCTION_DECL. */ - return STRIP_TEMPLATE (spec); - } + hash = spec_hasher::hash (gen_tmpl, argvec); + if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash)) + /* The spec for these args might be a partial instantiation of the + template, but here what we want is the FUNCTION_DECL. */ + return STRIP_TEMPLATE (spec); } else argvec = args; @@ -14704,6 +14701,8 @@ tsubst_template_decl (tree t, tree args, tsubst_flags_t complain, /* Type partial instantiations are stored as the type by lookup_template_class_1, not here as the template. */ spec = CLASSTYPE_TI_TEMPLATE (spec); + else if (TREE_CODE (spec) != TEMPLATE_DECL) + spec = DECL_TI_TEMPLATE (spec); return spec; } } @@ -14754,7 +14753,7 @@ tsubst_template_decl (tree t, tree args, tsubst_flags_t complain, inner = tsubst_aggr_type (inner, args, complain, in_decl, /*entering*/1); else - inner = tsubst (inner, args, complain, in_decl); + inner = tsubst_decl (inner, args, complain, /*use_spec_table=*/false); } --processing_template_decl; if (inner == error_mark_node) @@ -14780,12 +14779,11 @@ tsubst_template_decl (tree t, tree args, tsubst_flags_t complain, } else { - if (TREE_CODE (inner) == FUNCTION_DECL) - /* Set DECL_TI_ARGS to the full set of template arguments, which - tsubst_function_decl didn't do due to use_spec_table=false. */ - DECL_TI_ARGS (inner) = full_args; - DECL_TI_TEMPLATE (inner) = r; + /* Set DECL_TI_ARGS to the full set of template arguments, + which tsubst_function_decl / tsubst_decl didn't do due to + use_spec_table=false. */ + DECL_TI_ARGS (inner) = full_args; DECL_TI_ARGS (r) = DECL_TI_ARGS (inner); } @@ -14813,9 +14811,14 @@ tsubst_template_decl (tree t, tree args, tsubst_flags_t complain, if (PRIMARY_TEMPLATE_P (t)) DECL_PRIMARY_TEMPLATE (r) = r; - if (TREE_CODE (decl) == FUNCTION_DECL && !lambda_fntype) - /* Record this non-type partial instantiation. */ - register_specialization (r, t, full_args, false, hash); + if (!lambda_fntype && !class_p) + { + /* Record this non-type partial instantiation. */ + if (TREE_CODE (inner) == FUNCTION_DECL) + register_specialization (r, t, full_args, false, hash); + else + register_specialization (inner, t, full_args, false, hash); + } return r; }