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 44F963858D1E for ; Thu, 21 Mar 2024 03:29:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 44F963858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 44F963858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710991768; cv=none; b=TsRivvXOu5r6iH2ozk/OTPYKlU4SBvF8JAXgBiqkM1s64hZ6oF6UVf3se4Z2w8g0J98PJpmg9NvX72zkzoeLnaAo4R8uaB093kVAaR9x9CYDYg9QWrQn99TbBsTKjgY4snUispavM9ro3oMPGFNOLkV6yM1Zby33rEqHtB/1aSo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710991768; c=relaxed/simple; bh=sWGul9F3QViRt+K66UUQLVEE7IX1zDgHV9Qu6mCL6uc=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=tPdiX3rXmxUMuqzqWAMZ5bhtiwepxaA9Z8LULXQ7LJ09VRr0olELbM0/JF/dJAIb5WMqUapC0T3lsmm2aaqdQjbV1anNLAcYSRH4lCvqxjaoL6KA3g4q77X8e0k69QeTFfZqvHwDnHoxuvz/Y3R09R+pbRopE554BDm0hbwU5b8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710991765; 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=7IER5I2z4D5mgusOuRV63oIE8l1omAhFoQgN4ImNzeU=; b=ixkwp5A2cjIZ7Kk6JfwvxJem9yuTmwtAPP6IBZT34A5rhXNCO1wY1w+e2HQdd3ykj/EstS AnAhi/Af318Q5/xEldNl6BVMrs1634bytc6lMd0MzMt2HD1aCPf9aLM4at/vfNPUQzRd+H sPx4imsnYEnVKCLwicDIwzlnIyhYzZg= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-618-dcV6pCqWPCe_Spaj1O3Tpw-1; Wed, 20 Mar 2024 23:29:23 -0400 X-MC-Unique: dcV6pCqWPCe_Spaj1O3Tpw-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-78a280c9422so59277185a.2 for ; Wed, 20 Mar 2024 20:29:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710991763; x=1711596563; 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=7IER5I2z4D5mgusOuRV63oIE8l1omAhFoQgN4ImNzeU=; b=vkGuq45iAKDkGaURcWTXG2KIFp+Svce9iEKGkW1qkTC61FsmNeQh7EY2ZnqsvcAmkN 8ZSxS0rHBtOlFQy2LF7nRD+1xSxNUCHMlOGE8ekUq8nf5ARdDsF/ofkBR5el9jhgvJol URIdMu53XplrXjHg+9ph8gcqKL8NqwSz2oAFjIgG2T8VigoQq3kpGxrLLgBJImbHCB3W qpIDUh3P0iYR7BdAUp5ut4mF6tuadLv8hh7B24uorY72TbQSrfj+eF2piqIATI7ZdyCK EHBOWaqEPwldKg3iCa1MgIzC8S8QVjYH+8pDLlC3/9zdu6nZx/4bJ2rOlVFnC6Acdz+P sbKw== X-Gm-Message-State: AOJu0YwDKgFuanq7JvZdf0jx7M8j6IDsUnI22d27wuaYehnhCd5QAg3P TxEua5IjjOZGTBT3/fHIRK1o986i+iq3p6iAySQwWhz5Noq11SEpqLvuYEmKlww0XsxMCjBLwAg iVqrpCXXGtWs9E8wBOEqIoDHgo1bjPlYZEvp7/zFE14IpbhZMq0Ad/UvjQwYBFsQ= X-Received: by 2002:a05:620a:31a0:b0:788:3f49:1ca4 with SMTP id bi32-20020a05620a31a000b007883f491ca4mr918465qkb.43.1710991763003; Wed, 20 Mar 2024 20:29:23 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHlyDSK0FuS9zgSc7nn8oZXUkjBq6Y5m71WDM1r+8cDKuyyZnkRy75DeZNzPb3L6irvhQjRtQ== X-Received: by 2002:a05:620a:31a0:b0:788:3f49:1ca4 with SMTP id bi32-20020a05620a31a000b007883f491ca4mr918452qkb.43.1710991762582; Wed, 20 Mar 2024 20:29:22 -0700 (PDT) Received: from [192.168.1.130] (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 h4-20020a05620a13e400b00789e94cf424sm5246516qkl.108.2024.03.20.20.29.21 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 20 Mar 2024 20:29:21 -0700 (PDT) Message-ID: <8f998fef-9009-453e-9a3d-9c510ac8883c@redhat.com> Date: Wed, 20 Mar 2024 23:29:21 -0400 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] c++: explicit inst of template method not generated [PR110323] To: Marek Polacek Cc: GCC Patches References: <20240308170215.21919-1-polacek@redhat.com> From: Jason Merrill In-Reply-To: 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: 7bit X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,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 3/19/24 15:30, Marek Polacek wrote: > On Mon, Mar 18, 2024 at 09:10:27PM -0400, Jason Merrill wrote: >> On 3/15/24 13:48, Marek Polacek wrote: >>> On Thu, Mar 14, 2024 at 03:39:04PM -0400, Jason Merrill wrote: >>>> On 3/8/24 12:02, Marek Polacek wrote: >>>>> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? >>>>> >>>>> -- >8 -- >>>>> Consider >>>>> >>>>> constexpr int VAL = 1; >>>>> struct foo { >>>>> template >>>>> void bar(typename std::conditional::type arg) { } >>>>> }; >>>>> template void foo::bar<1>(int arg); >>>>> >>>>> where we since r11-291 fail to emit the code for the explicit >>>>> instantiation. That's because cp_walk_subtrees/TYPENAME_TYPE now >>>>> walks TYPE_CONTEXT ('conditional' here) as well, and in a template >>>>> finds the B==VAL template argument. VAL is constexpr, which implies const, >>>>> which in the global scope implies static. constrain_visibility_for_template >>>>> then makes "struct conditional<(B == VAL), int, float>" non-TREE_PUBLIC. >>>>> Then symtab_node::needed_p checks TREE_PUBLIC, sees it's 0, and we don't >>>>> emit any code. >>>>> >>>>> I thought the fix would be some ODR-esque check to not consider >>>>> constexpr variables/fns that are used just for their value. But >>>>> it turned out to be tricky. For instance, we can't skip >>>>> determine_visibility in a template; we can't even skip it for value-dep >>>>> expressions. For example, no-linkage-expr1.C has >>>>> >>>>> using P = struct {}*; >>>>> template >>>>> void f(int(*)[((P)0, N)]) {} >>>>> >>>>> where ((P)0, N) is value-dep, but N is not relevant here: we have to >>>>> ferret out the anonymous type. When instantiating, it's already gone. >>>> >>>> Hmm, how is that different from the B == VAL case? In both cases we're >>>> naming an internal entity that gets folded away. >>>> >>>> I guess the difference is that B == VAL falls under the special allowance in >>>> https://eel.is/c++draft/basic.def.odr#14.5.1 because it's a constant used as >>>> a prvalue, and therefore is not odr-used under >>>> https://eel.is/c++draft/basic.def.odr#5.2 >>>> >>>> So I would limit this change to decl_constant_var_p. Really we should also >>>> be checking that the lvalue-rvalue conversion is applied, but that's more >>>> complicated. >>> >>> Thanks. My previous version had it, but it didn't handle >>> >>> static constexpr int getval () { return 1; } >>> >>> template >>> void baz(typename conditional::type arg) { } >>> >>> I'd say that "getval()" is one of "manifestly constant-evaluated expressions that >>> are not value-dependent", so it should be treated the same as B == VAL. >> >> But it doesn't satisfy the 14.5 rule that corresponding names need to refer >> to the same entity; since getval names a function, it doesn't get the >> special exemption from that rule that VAL gets. >> >> So this should not be treated the same as B == VAL. > > Thanks for the explanation. > >>> I don't know if this is important to handle. Do you want me to poke further or >>> should we just go with decl_constant_var_p and leave it at that for now? >> >> Just decl_constant_var_p. > > Here it is: > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > -- >8 -- > Consider > > constexpr int VAL = 1; > struct foo { > template > void bar(typename std::conditional::type arg) { } > }; > template void foo::bar<1>(int arg); > > where we since r11-291 fail to emit the code for the explicit > instantiation. That's because cp_walk_subtrees/TYPENAME_TYPE now > walks TYPE_CONTEXT ('conditional' here) as well, and in a template > finds the B==VAL template argument. VAL is constexpr, which implies const, > which in the global scope implies static. constrain_visibility_for_template > then makes "struct conditional<(B == VAL), int, float>" non-TREE_PUBLIC. > Then symtab_node::needed_p checks TREE_PUBLIC, sees it's 0, and we don't > emit any code. > > I thought the fix would be some ODR-esque check to not consider > constexpr variables/fns that are used just for their value. But > it turned out to be tricky. For instance, we can't skip > determine_visibility in a template; we can't even skip it for value-dep > expressions. For example, no-linkage-expr1.C has > > using P = struct {}*; > template > void f(int(*)[((P)0, N)]) {} > > where ((P)0, N) is value-dep, but N is not relevant here: we have to > ferret out the anonymous type. When instantiating, it's already gone. > > This patch uses decl_constant_var_p. This is to implement (an > approximation) [basic.def.odr]#14.5.1 and [basic.def.odr]#5.2. > > PR c++/110323 > > gcc/cp/ChangeLog: > > * decl2.cc (min_vis_expr_r) : Do nothing for > decl_constant_var_p VAR_DECLs. > > gcc/testsuite/ChangeLog: > > * g++.dg/template/explicit-instantiation6.C: New test. > * g++.dg/template/explicit-instantiation7.C: New test. > --- > gcc/cp/decl2.cc | 6 ++- > .../g++.dg/template/explicit-instantiation6.C | 43 +++++++++++++++++++ > .../g++.dg/template/explicit-instantiation7.C | 22 ++++++++++ > 3 files changed, 70 insertions(+), 1 deletion(-) > create mode 100644 gcc/testsuite/g++.dg/template/explicit-instantiation6.C > create mode 100644 gcc/testsuite/g++.dg/template/explicit-instantiation7.C > > diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc > index 2562d8aeff6..753e45e56ad 100644 > --- a/gcc/cp/decl2.cc > +++ b/gcc/cp/decl2.cc > @@ -2718,7 +2718,11 @@ min_vis_expr_r (tree *tp, int */*walk_subtrees*/, void *data) > /* Fall through. */ > case VAR_DECL: > case FUNCTION_DECL: > - if (! TREE_PUBLIC (t)) > + if (decl_constant_var_p (t)) > + /* In a template, we can't trust VAR_DECLs, either. It's possible > + they won't be ODR-used, and we could wrongly think the linkage > + is internal (PR110323). */; Maybe "The ODR allows definitions in different TUs to refer to distinct constant variables with internal or no linkage, so such a reference shouldn't affect visibility (PR110323). FIXME but only if the lvalue-rvalue conversion is applied." OK with that change. > + else if (! TREE_PUBLIC (t)) > tpvis = VISIBILITY_ANON; > else > tpvis = DECL_VISIBILITY (t); > diff --git a/gcc/testsuite/g++.dg/template/explicit-instantiation6.C b/gcc/testsuite/g++.dg/template/explicit-instantiation6.C > new file mode 100644 > index 00000000000..8b77c9deb20 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/template/explicit-instantiation6.C > @@ -0,0 +1,43 @@ > +// PR c++/110323 > +// { dg-do compile { target c++14 } } > + > +template > +struct conditional { using type = T; }; > + > +template > +struct conditional { using type = F; }; > + > +constexpr int VAL = 1; > + > +static constexpr int getval () { return 1; } > + > +template > +constexpr int TVAL = 1; > + > +static struct S { > + constexpr operator bool() { return true; } > +} s; > + > +struct foo { > + template > + void bar(typename conditional::type arg) { } > + > + template > + void qux(typename conditional, int, float>::type arg) { } > + > + template > + void sox(typename conditional::type arg) { } > + > + template > + void nim(typename conditional::type arg) { } > +}; > + > +template void foo::bar<1>(int arg); > +template void foo::qux<1>(int arg); > +template void foo::sox<1>(int arg); > +template void foo::nim<1>(int arg); > + > +// { dg-final { scan-assembler "_ZN3foo3barILi1EEEvN11conditionalIXeqT_L_ZL3VALEEifE4typeE" } } > +// { dg-final { scan-assembler "_ZN3foo3quxILi1EEEvN11conditionalIXeqT_L_Z4TVALIiEEEifE4typeE" } } > +// { dg-final { scan-assembler "_ZN3foo3soxILi1EEEvN11conditionalIXeqT_nxL_ZL3VALEEifE4typeE" } } > +// { dg-final { scan-assembler "_ZN3foo3nimILi1EEEvN11conditionalIXneT_szL_ZL3VALEEifE4typeE" } } > diff --git a/gcc/testsuite/g++.dg/template/explicit-instantiation7.C b/gcc/testsuite/g++.dg/template/explicit-instantiation7.C > new file mode 100644 > index 00000000000..9a870e808fa > --- /dev/null > +++ b/gcc/testsuite/g++.dg/template/explicit-instantiation7.C > @@ -0,0 +1,22 @@ > +// PR c++/110323 > +// { dg-do compile { target c++11 } } > + > +using P = struct { }*; > +using N = struct A { }*; > + > +template > +struct conditional { using type = T; }; > + > +struct foo { > + template > + void bar(typename conditional<((P) 0, B), int, float>::type arg) { } > + > + template > + void baz(typename conditional<((N) 0, B), int, float>::type arg) { } > +}; > + > +template void foo::bar<1>(int arg); > +template void foo::baz<1>(int arg); > + > +// { dg-final { scan-assembler-not "_ZN3foo3barILi1EEEvN11conditionalIXcmcvP1XLi0EneT_Li0EEifE4typeE" } } > +// { dg-final { scan-assembler "_ZN3foo3bazILi1EEEvN11conditionalIXcmcvP1ALi0EneT_Li0EEifE4typeE" } } > > base-commit: bc91e3870e9c984c180b478a3449a9a2e56cd107