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: 1836749 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=aEQIs5Bk; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RqlMR09Wbz1ynZ for ; Wed, 20 Sep 2023 01:05:06 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id F2BE23856DC7 for ; Tue, 19 Sep 2023 15:05:03 +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-Google-Smtp-Source: AGHT+IG8biFHOk1izMh56tEYy1HYKzfuMClRJawQvVIWORwmJYgVH0uwFAXMRFGErp9IpaEjr4tTWg== 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+incoming=patchwork.ozlabs.org@gcc.gnu.org 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; }