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.133.124]) by sourceware.org (Postfix) with ESMTPS id 81BAE3858C52 for ; Fri, 14 Jul 2023 21:17:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 81BAE3858C52 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=1689369428; 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=lsQXOafcJOL+YreUKp0semh5lQgCkKdFg6uygC2vm8M=; b=SjJxrHutccucBEHOUzgVyF3QSipqYAPAxd2wsiOPxYzxxkVxxE03EYE74bO8HSY4tz2tvW OFktxOGZDZo3+Fz9bC6Uw8upuLJ1b1d3hiuWudIflP1WEU6A+GIcJ5PNFp0jDYWkYlgu4f qq6hLShYEvLInt7E3Kf05g8Vqzbb5Fc= Received: from mail-vk1-f199.google.com (mail-vk1-f199.google.com [209.85.221.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-592-WrGxTuvMMZOdt_kjgIX1IQ-1; Fri, 14 Jul 2023 17:17:07 -0400 X-MC-Unique: WrGxTuvMMZOdt_kjgIX1IQ-1 Received: by mail-vk1-f199.google.com with SMTP id 71dfb90a1353d-4816078ab05so481667e0c.1 for ; Fri, 14 Jul 2023 14:17:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689369427; x=1691961427; 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=lsQXOafcJOL+YreUKp0semh5lQgCkKdFg6uygC2vm8M=; b=Mz/+yOZ1tTvPnA7d2PUuMMQ5ih+jvqTSKkhSmIb1w8mzrvMuiX7CZGLs/UpeI0dzYK KdsYi08QCMSqUw8jgJ8lbpVqBjqwXLVbioFubT/rPIEw35TRdoJIWpvBc45WXqhwOq1R mvKzGchEXF3CP2wh55jlVfATROQ0v1M7G8pGoXDgvuTaVExM10dgQicNv/81q4RwKtq0 fJjsmUG9cA841PpTNck3tbe/r5Lo6fW2tmvt6H76ebpV8xOquw7nQpAnx7wmObyztqcC 1Xj3aqDOtafTTJ84HhYn2ai+9RQGiVmR3bZMMUljGPWHjcP24olPLHRlmMnTcGeARWp1 XbOQ== X-Gm-Message-State: ABy/qLbmVJuHecR6iE29kp960IObdPX+PkANMcX/Ug7LfjTdCbtrlPz1 kD7FuSmTi7/t02Zw+hgTPKxT3e6KmxGIZgKoQv6WolixWVE9MowLl86kLxT9Tapo9GOTLgkclyp USS4+QzXMOtofPFpBbIVtmflulw== X-Received: by 2002:a1f:bf08:0:b0:481:8049:591d with SMTP id p8-20020a1fbf08000000b004818049591dmr1142263vkf.13.1689369426789; Fri, 14 Jul 2023 14:17:06 -0700 (PDT) X-Google-Smtp-Source: APBJJlHZ65r2HgwAe7lKrAngt22K92xvi5Y10VY9V0DC3FRtTDFWFPqPbdGhH693oytHk8CMMc5WHA== X-Received: by 2002:a1f:bf08:0:b0:481:8049:591d with SMTP id p8-20020a1fbf08000000b004818049591dmr1142259vkf.13.1689369426458; Fri, 14 Jul 2023 14:17:06 -0700 (PDT) Received: from [192.168.1.108] (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id x3-20020a0ce243000000b0063019b482f8sm4267033qvl.85.2023.07.14.14.17.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 14 Jul 2023 14:17:05 -0700 (PDT) Message-ID: <5502feae-d5ef-092e-3f29-2b4fa9f38224@redhat.com> Date: Fri, 14 Jul 2023 17:17:05 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Subject: Re: [PATCH] c++: redundant targ coercion for var/alias tmpls To: Patrick Palka Cc: gcc-patches@gcc.gnu.org References: <20230621171920.1283054-1-ppalka@redhat.com> <94b29e42-dfa9-5c05-4f1d-3c5beb998fdf@redhat.com> <26258d18-5718-9387-087b-be58a25048c2@redhat.com> <4388a939-94bd-048c-f234-e54bab92d937@idea> <9e51de65-9910-445b-1ec7-8a3cd47114fc@idea> From: Jason Merrill In-Reply-To: <9e51de65-9910-445b-1ec7-8a3cd47114fc@idea> 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: 8bit 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,KAM_SHORT,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,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 List-Id: On 7/14/23 14:07, Patrick Palka wrote: > On Thu, 13 Jul 2023, Jason Merrill wrote: > >> On 7/13/23 11:48, Patrick Palka wrote: >>> On Wed, 28 Jun 2023, Patrick Palka wrote: >>> >>>> On Wed, Jun 28, 2023 at 11:50 AM Jason Merrill wrote: >>>>> >>>>> On 6/23/23 12:23, Patrick Palka wrote: >>>>>> On Fri, 23 Jun 2023, Jason Merrill wrote: >>>>>> >>>>>>> On 6/21/23 13:19, Patrick Palka wrote: >>>>>>>> When stepping through the variable/alias template specialization >>>>>>>> code >>>>>>>> paths, I noticed we perform template argument coercion twice: >>>>>>>> first from >>>>>>>> instantiate_alias_template / finish_template_variable and again >>>>>>>> from >>>>>>>> tsubst_decl (during instantiate_template). It should suffice to >>>>>>>> perform >>>>>>>> coercion once. >>>>>>>> >>>>>>>> To that end patch elides this second coercion from tsubst_decl >>>>>>>> when >>>>>>>> possible. We can't get rid of it completely because we don't >>>>>>>> always >>>>>>>> specialize a variable template from finish_template_variable: we >>>>>>>> could >>>>>>>> also be doing so directly from instantiate_template during >>>>>>>> variable >>>>>>>> template partial specialization selection, in which case the >>>>>>>> coercion >>>>>>>> from tsubst_decl would be the first and only coercion. >>>>>>> >>>>>>> Perhaps we should be coercing in lookup_template_variable rather >>>>>>> than >>>>>>> finish_template_variable? >>>>>> >>>>>> Ah yes, there's a patch for that at >>>>>> https://gcc.gnu.org/pipermail/gcc-patches/2023-May/617377.html :) >>>>> >>>>> So after that patch, can we get rid of the second coercion completely? >>>> >>>> On second thought it should be possible to get rid of it, if we >>>> rearrange things to always pass the primary arguments to tsubst_decl, >>>> and perform partial specialization selection from there instead of >>>> instantiate_template. Let me try... >>> >>> Like so? Bootstrapped and regtested on x86_64-pc-linux-gnu. >>> >>> -- >8 -- >>> >>> When stepping through the variable/alias template specialization code >>> paths, I noticed we perform template argument coercion twice: first from >>> instantiate_alias_template / finish_template_variable and again from >>> tsubst_decl (during instantiate_template). It'd be good to avoid this >>> redundant coercion. >>> >>> It turns out that this coercion could be safely elided whenever >>> specializing a primary variable/alias template, because we can rely on >>> lookup_template_variable and instantiate_alias_template to already have >>> coerced the arguments. >>> >>> The other situation to consider is when fully specializing a partial >>> variable template specialization (from instantiate_template), in which >>> case the passed 'args' are the (already coerced) arguments relative to >>> the partial template and 'argvec', the result of substitution into >>> DECL_TI_ARGS, are the (uncoerced) arguments relative to the primary >>> template, so coercion is still necessary. We can still avoid this >>> coercion however if we always pass the primary variable template to >>> tsubst_decl from instantiate_template, and instead perform partial >>> specialization selection directly from tsubst_decl. This patch >>> implements this approach. >> >> The relationship between instantiate_template and tsubst_decl is pretty >> tangled. We use the former to substitute (often deduced) template arguments >> into a template, and the latter to substitute template arguments into a use of >> a template...and also to implement the former. >> >> For substitution of uses of a template, we expect to need to coerce the >> arguments after substitution. But we avoid this issue for variable templates >> by keeping them as TEMPLATE_ID_EXPR until substitution time, so if we see a >> VAR_DECL in tsubst_decl it's either a non-template variable or under >> instantiate_template. > > FWIW it seems we could also be in tsubst_decl for a VAR_DECL if > > * we're partially instantiating a class-scope variable template > during instantiation of the class Hmm, why don't partial instantiations stay as TEMPLATE_ID_EXPR? > * we're substituting a use of an already non-dependent variable > template specialization Sure. >> So it seems like the current coercion for variable templates is only needed in >> this case to support the redundant hash table lookup that we just did in >> instantiate_template. Perhaps instead of doing coercion here or moving the >> partial spec lookup, we could skip the hash table lookup for the case of a >> variable template? > > It seems we'd then also have to make instantiate_template responsible > for registering the variable template specialization since tsubst_decl > no longer necessarily has the arguments relative to the primary template > ('args' could be relative to the partial template). > > Like so? The following makes us perform all the specialization table > manipulation in instantiate_template instead of tsubst_decl for variable > template specializations. Looks good. > I wonder if we might want to do this for alias template specializations too? That would make sense. > @@ -15222,20 +15230,21 @@ 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 > - && PRIMARY_TEMPLATE_P (gen_tmpl) > - && TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (argvec)) > - /* We're fully specializing a template declaration, so > - we need to coerce the innermost arguments corresponding to > - the template. */ > - argvec = (coerce_template_parms > - (DECL_TEMPLATE_PARMS (gen_tmpl), > - argvec, tmpl, complain)); > - if (argvec == error_mark_node) > - RETURN (error_mark_node); > - hash = spec_hasher::hash (gen_tmpl, argvec); > - spec = retrieve_specialization (gen_tmpl, argvec, hash); > + if (variable_template_p (tmpl) > + && (TMPL_ARGS_DEPTH (args) > + >= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))) Do we still need to compare depths? If not, we could also skip computing gen_tmpl in this case. Jason