From patchwork Wed Jun 8 18:21:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 54951 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AD3F6388B5B5 for ; Wed, 8 Jun 2022 18:22:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AD3F6388B5B5 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1654712574; bh=LFiuKo0xM7ecTyxVzpQdiiIvAaIj9UlssxdAR6AtkUs=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=TkpSjM35nRB6BY/tgff+DD0IY+DuytoexBmhw/YC9WpzEqlJjTKR09Z/9Z5IezuaE rYLS0L0FTF5Us+q+GYtKDn3CdNE/eIRrAmAXnV0ovZ28xb25mHG7god+XnDv1OWCtE 2l+xzFrm7Of/feAYrSIfH07HywVzPI7ImXfyUmwk= 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 6B5FA388B5A2 for ; Wed, 8 Jun 2022 18:21:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6B5FA388B5A2 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.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-112-XVNfHzOQMhi6fOr5ASY79w-1; Wed, 08 Jun 2022 14:21:51 -0400 X-MC-Unique: XVNfHzOQMhi6fOr5ASY79w-1 Received: by mail-qv1-f69.google.com with SMTP id x17-20020a0cfe11000000b004645917e45cso13496550qvr.4 for ; Wed, 08 Jun 2022 11:21:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=LFiuKo0xM7ecTyxVzpQdiiIvAaIj9UlssxdAR6AtkUs=; b=Dvs1qJh5Sqv3h3+gAkNpqNtm2tNTF5EXfyDtLb+YJx4hMldb5AtPFtzHRsPWtpjOsp 7uIjQ3eIwsKfEy8zN3l5mizXboNSq7QaRHtY57bEhRiHX11NBdRMryWx+lkkUJPU9IJv 8ZFTgZwUdOIyAFffuOaHD9+NnyiuRX6yb3F8oBgdCVf21oXBobZoWBGIOW7zL55nAAuE yN3sM8Nm1GE/XeJHzB8T5UhXSspeloMASiAhKQxBYZV9hcipCHbm3wkYx8evxwwcgFhC Lo/ftkLww409WNYrUszS7JOPeNkl79QUEhOxPMX+Co7LD/ehPdl/uBDYCcFYBqdIxTVn WyTw== X-Gm-Message-State: AOAM530s95jbr10xfPvr7zNbSIAXmo7Xk3J2CqEz/CfnGnvWAycfqTQJ rm86n5vzqeFvY8mETsiSv5c/5ULBCMWsbL7dBO8VKfub58kqNs5NSKp6Zg3UqR+PmfLY0FoOj2x IEZ0ZLb+UTao087ILvYmoZ9OSmh8mgQAPXrScvaXasqGk2vLpqgGMzUH53EmU2bZKhIU= X-Received: by 2002:a05:6214:2aac:b0:46b:b44c:8e4d with SMTP id js12-20020a0562142aac00b0046bb44c8e4dmr9073308qvb.124.1654712510483; Wed, 08 Jun 2022 11:21:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxiVlGGlWZY06yWkFutG6vURJxj2q1JB3YXFN3zXfUetDOyDjByWV/+SAlmyfW0kCEM84VH4Q== X-Received: by 2002:a05:6214:2aac:b0:46b:b44c:8e4d with SMTP id js12-20020a0562142aac00b0046bb44c8e4dmr9073288qvb.124.1654712510178; Wed, 08 Jun 2022 11:21:50 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id bm32-20020a05620a19a000b006a6d20386f6sm5792921qkb.42.2022.06.08.11.21.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jun 2022 11:21:49 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: optimize specialization of nested class templates Date: Wed, 8 Jun 2022 14:21:47 -0400 Message-Id: <20220608182147.4123587-1-ppalka@redhat.com> X-Mailer: git-send-email 2.36.1.363.g9c897eef06 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.5 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, 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" When substituting a class template specialization, tsubst_aggr_type substitutes the TYPE_CONTEXT before passing it to lookup_template_class. This appears to be unnecessary, however, because the the initial value of lookup_template_class's context parameter is unused outside of the IDENTIFIER_NODE case, and l_t_c performs its own substitution of the context, anyway. So this patch removes the redundant substitution in tsubst_aggr_type. Doing so causes us to ICE on template/nested5.C because during lookup_template_class for A::C::D with T=E and S=S, we substitute and complete the context A::C with T=E, which in turn registers the desired dependent specialization of D for us and we end up trying to register it again. This patch fixes this by checking the specializations table again after completion of the context. This patch also implements a couple of other optimizations: * In lookup_template_class, if the context of the partially instantiated template is already non-dependent, then we could reuse that instead of substituting the context of the most general template. * When substituting the TYPE_DECL for an injected-class-name in tsubst_decl, we can avoid substituting its TREE_TYPE and DECL_TI_ARGS. Together these optimizations improve memory usage for the range-v3 testcase test/view/split.cc by about 5%. The improvement is probably more significant when dealing with deeply nested class templates. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? gcc/cp/ChangeLog: * pt.cc (lookup_template_class): Remove dead stores to context parameter. Don't substitute the context of the most general template if that of the partially instantiated template is non-dependent. Check the specializations table again after completing the context of a nested dependent specialization. (tsubst_aggr_type) : Don't substitute TYPE_CONTEXT or pass it to lookup_template_class. (tsubst_decl) : Avoid substituting the TREE_TYPE and DECL_TI_ARGS when DECL_SELF_REFERENCE_P. --- gcc/cp/pt.cc | 69 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 28 deletions(-) diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 59b94317e88..28023d60684 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -9840,8 +9840,6 @@ lookup_template_class (tree d1, tree arglist, tree in_decl, tree context, if (context) pop_decl_namespace (); } - if (templ) - context = DECL_CONTEXT (templ); } else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1))) { @@ -9868,7 +9866,6 @@ lookup_template_class (tree d1, tree arglist, tree in_decl, tree context, { templ = d1; d1 = DECL_NAME (templ); - context = DECL_CONTEXT (templ); } else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1)) { @@ -10059,8 +10056,25 @@ lookup_template_class (tree d1, tree arglist, tree in_decl, tree context, context = DECL_CONTEXT (gen_tmpl); if (context && TYPE_P (context)) { - context = tsubst_aggr_type (context, arglist, complain, in_decl, true); - context = complete_type (context); + if (!uses_template_parms (DECL_CONTEXT (templ))) + /* If the context of the partially instantiated template is + already non-dependent, then we might as well use it. */ + context = DECL_CONTEXT (templ); + else + { + context = tsubst_aggr_type (context, arglist, complain, in_decl, true); + context = complete_type (context); + if (is_dependent_type && arg_depth > 1) + { + /* If this is a dependent nested specialization such as + A::B, then completion of A might have + registered this specialization of B for us, so check + the table again (33959). */ + entry = type_specializations->find_with_hash (&elt, hash); + if (entry) + return entry->spec; + } + } } else context = tsubst (context, arglist, complain, in_decl); @@ -13711,25 +13725,12 @@ tsubst_aggr_type (tree t, if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t)) { tree argvec; - tree context; tree r; /* In "sizeof(X)" we need to evaluate "I". */ cp_evaluated ev; - /* First, determine the context for the type we are looking - up. */ - context = TYPE_CONTEXT (t); - if (context && TYPE_P (context)) - { - context = tsubst_aggr_type (context, args, complain, - in_decl, /*entering_scope=*/1); - /* If context is a nested class inside a class template, - it may still need to be instantiated (c++/33959). */ - context = complete_type (context); - } - - /* Then, figure out what arguments are appropriate for the + /* Figure out what arguments are appropriate for the type we are trying to find. For example, given: template struct S; @@ -13744,7 +13745,7 @@ tsubst_aggr_type (tree t, r = error_mark_node; else { - r = lookup_template_class (t, argvec, in_decl, context, + r = lookup_template_class (t, argvec, in_decl, NULL_TREE, entering_scope, complain); r = cp_build_qualified_type (r, cp_type_quals (t), complain); } @@ -14880,6 +14881,10 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain) ctx = tsubst_aggr_type (ctx, args, complain, in_decl, /*entering_scope=*/1); + if (DECL_SELF_REFERENCE_P (t)) + /* The context and type of a injected-class-name are + the same, so we don't need to substitute both. */ + type = ctx; /* If CTX is unchanged, then T is in fact the specialization we want. That situation occurs when referencing a static data member within in its own @@ -14900,14 +14905,22 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain) { tmpl = DECL_TI_TEMPLATE (t); gen_tmpl = most_general_template (tmpl); - argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl); - if (argvec != error_mark_node) - argvec = (coerce_innermost_template_parms - (DECL_TEMPLATE_PARMS (gen_tmpl), - argvec, t, complain, - /*all*/true, /*defarg*/true)); - if (argvec == error_mark_node) - RETURN (error_mark_node); + if (DECL_SELF_REFERENCE_P (t)) + /* The DECL_TI_ARGS for the injected-class-name are the + generic template arguments for the class template, so + substitution/coercion is just the identity mapping. */ + argvec = args; + else + { + argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl); + if (argvec != error_mark_node) + argvec = (coerce_innermost_template_parms + (DECL_TEMPLATE_PARMS (gen_tmpl), + argvec, t, complain, + /*all*/true, /*defarg*/true)); + if (argvec == error_mark_node) + RETURN (error_mark_node); + } hash = hash_tmpl_and_args (gen_tmpl, argvec); spec = retrieve_specialization (gen_tmpl, argvec, hash); } From patchwork Thu Jun 9 13:00:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 54982 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C279C38344DF for ; Thu, 9 Jun 2022 13:01:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C279C38344DF DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1654779666; bh=TFCY7ew+H82rH84vapqInR4SzQfHELRIxLgZEkgdHik=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=XsqOm9KyMQbTNYaOKyslonOffPt0s+D7Cyd0GJdffSJvqw0JMxzL04m4IMlDZYqwt 5ZAccEH/vc/3QAVq8yBtgZChIutd1LK2JuJZfKx3CuhkrwEMVmnyQoSfZdGHdAZqTT G9RHfcQoCchFK8bKP/zxquXRIWn8EgrZaHt08QFI= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id DABF23839406 for ; Thu, 9 Jun 2022 13:00:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DABF23839406 Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-633-VmPq7SsrOqu50cnsJv0DJw-1; Thu, 09 Jun 2022 09:00:34 -0400 X-MC-Unique: VmPq7SsrOqu50cnsJv0DJw-1 Received: by mail-qk1-f197.google.com with SMTP id bl27-20020a05620a1a9b00b0069994eeb30cso18943762qkb.11 for ; Thu, 09 Jun 2022 06:00:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=TFCY7ew+H82rH84vapqInR4SzQfHELRIxLgZEkgdHik=; b=IZscoOLlJW8RymeLz6PMFtUeDG4cQ1JF8LN510svnnqcKMr7V2SI/L9JdbbwjXiAld Xo2EkCDW1pVIwBzi5aMGTegek0pmD59Bck0FKg0Yh7ITfdndrx+Yw4KZMHo41k9cE/Ip 3lgfveN7nnfNizJ5NOdXujOhjq5ipQ+44WNgRvI6dqs3VDMADS8g/YUcan/0OHnw5M+t /a1cm7gQjQ7wmO8UQ8GAXC+Sr0I/MN4ealYnlfYqcAcylLsw5z3ABUuiwr2e+VX5vTFc EWvf6ggQ6ZwfgmQzNCkt6cJPsMww3rr9XigQOXWmlvpWROXa1syVk9BiIP143k32YvRX bUEA== X-Gm-Message-State: AOAM532+3FqmaWx/MrF66GdfPwnRWBAiBhmoikWIQqOJC2OsxvkfByvR PWsKCQNKOecprd/p/aaHf3E28ZqIQ7+1SpFWNSYY1Y5vdxkYuTOn+mD5tSsTAWo2bfxGT0L3SvU GODWJWh1RVTXQ1ei8Q7v0+FLZzvaVysyMHev4Au7yNEcRq/6GNM3vF/wRfkhh1IrEZzY= X-Received: by 2002:ac8:5c44:0:b0:305:165e:9f3 with SMTP id j4-20020ac85c44000000b00305165e09f3mr190277qtj.560.1654779628694; Thu, 09 Jun 2022 06:00:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw8SLU6+zcGvyrz9moFHuIiWC+Bwlk/t+lqz5FFmK4lOz6d1gvK3O28E9X4pmtIck5xQhsNuw== X-Received: by 2002:ac8:5c44:0:b0:305:165e:9f3 with SMTP id j4-20020ac85c44000000b00305165e09f3mr189929qtj.560.1654779625283; Thu, 09 Jun 2022 06:00:25 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id d5-20020a05620a240500b006a6b1630e95sm4447733qkn.45.2022.06.09.06.00.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Jun 2022 06:00:24 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH 2/1] c++: optimize specialization of templated member functions Date: Thu, 9 Jun 2022 09:00:13 -0400 Message-Id: <20220609130013.250243-1-ppalka@redhat.com> X-Mailer: git-send-email 2.36.1.363.g9c897eef06 In-Reply-To: <20220608182147.4123587-1-ppalka@redhat.com> References: <20220608182147.4123587-1-ppalka@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.5 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, 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" This performs one of the optimizations added by the previous patch to lookup_template_class, to instantiate_template as well. (For the libstdc++ ranges tests this optimization appears to be effective around 30% of the time, i.e. 30% of the time context of 'tmpl' is non-dependent while the context of 'gen_tmpl' is dependent.) gcc/cp/ChangeLog: * pt.cc (instantiate_template): Don't substitute the context of the most general template if that of the partially instantiated template is non-dependent. --- gcc/cp/pt.cc | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index e021c254872..208daad298a 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -21661,8 +21661,14 @@ instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain) ++processing_template_decl; if (DECL_CLASS_SCOPE_P (gen_tmpl)) { - tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr, - complain, gen_tmpl, true); + tree ctx; + if (!uses_template_parms (DECL_CONTEXT (tmpl))) + /* If the context of the partially instantiated template is already + non-dependent, then we might as well use it. */ + ctx = DECL_CONTEXT (tmpl); + else + ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr, + complain, gen_tmpl, true); push_nested_class (ctx); }