From patchwork Tue Mar 19 19:30:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1913764 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=dL9sEO3D; 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 4Tzhgr0Hy9z1yWs for ; Wed, 20 Mar 2024 06:32:22 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 867F8385840E for ; Tue, 19 Mar 2024 19:32:20 +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 43E233858C39 for ; Tue, 19 Mar 2024 19:30:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 43E233858C39 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 43E233858C39 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=1710876630; cv=none; b=RHZ3grpycPqmf1MIQYb9ul4KClDaWT7OxbdtVgAfcH0P/U76/pR5MNJnQxp1SYxaqDNjlqCzbkr7g61bqJipzhQSlRvPiWxfZbhj5dQukvxXzqn3HzKpMJks6O7JbhilCQ7VIBbU1kxGijbyAeB1hFixt/QdySwZhXeOvMFo/pM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710876630; c=relaxed/simple; bh=MU9pYZYa/aj/8EM4pbSkWmfiYs5WR70JDLeeigsARQw=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=H8FWToHroe64RYAIL/3uh91j+PYRDQDCJ0xg34k5XoSaTJIiER754IbNP00s2gl1rr+NNAEKImc/Xu2sYf4FqUXKTIOmqlHzE5QcwUS7aT2hJV71nAa7B/dy60subUK88YGhpojnRsNaCIBs0tc83rizE66J/xwR3ZYe4moxdZo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710876627; 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: in-reply-to:in-reply-to:references:references; bh=XhiuaD/JoIrb9JzhQxEslXc9HSSFAx3HZYvY42SEBdE=; b=dL9sEO3DwrfqGtkD/bnnwFAseGNKUgm0o6DucVk4R5LfmTe2CLYbbQJ7Y97SkHEfDamAzI 0rQb0Deh/22ltZaZAmY8D9O82MGA8VcXRDe0swZBnn5ZhuSqXL22ThOClC28fP9UTKHe5l Bqs+8y263h5+9jgJbluR9j6RjAWMO6c= Received: from mail-oi1-f199.google.com (mail-oi1-f199.google.com [209.85.167.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-570-a5J10peZMyuyiDpOF8rweg-1; Tue, 19 Mar 2024 15:30:26 -0400 X-MC-Unique: a5J10peZMyuyiDpOF8rweg-1 Received: by mail-oi1-f199.google.com with SMTP id 5614622812f47-3c24221f4efso4949708b6e.0 for ; Tue, 19 Mar 2024 12:30:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710876625; x=1711481425; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XhiuaD/JoIrb9JzhQxEslXc9HSSFAx3HZYvY42SEBdE=; b=X+bmeFhnWcCthpBwMwBq4twsBmhqWgF2YshEqU9MZ4ykO0F2krgWExY96UuRExO7QH YkzQhkoem2vcYtrqAKuTTFbUVoUYQFGq7Od4YvkJd31IednlpX7K2nYNQGxUhEc/RWTD 5q0mqoiB8UGgFsQeOOzVufY4uXQxW45OoVKHTsauRG5NMMJjc9VF6HWzSRXjQyuGWBxo zChvaE2rfEE8tlekYXCVLNPXivk0EUga8wmov6z9/EdPfCZZHRW5mn/4o+ADvfENlzBb sn9U5u1k2MIFNxJUXaoTl+Ya4JMYNAMwSRU/QF8ICApe98mqz/EsJQXBXzZkIMeHHJhW onNQ== X-Gm-Message-State: AOJu0YxYKfkT1i9FJW4UXYq6GvdLNsboFvtD7c1GA9hktOzMXGxATznh Xulrn0h+QzYeWTgMYB6Z/0dyiE13x4cq84LknrqpnFaiQVo2bV9aY6HmRxxdrNPA+fS54Kr8bdH D+CA5hqRM+6yH4sciCcpjxoOZRNsd6P3IJo5R7raSCN8zaAYH03RwscjzB5JoJPc= X-Received: by 2002:a05:6808:301e:b0:3c2:39d1:f111 with SMTP id ay30-20020a056808301e00b003c239d1f111mr17684249oib.48.1710876624985; Tue, 19 Mar 2024 12:30:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFY/EoOMTHZuFHijBf2ZNUmJ5uwwwuXZEOQnDa97CvuegjF7t3xENcJej4F4HfRKB8KlXSoVQ== X-Received: by 2002:a05:6808:301e:b0:3c2:39d1:f111 with SMTP id ay30-20020a056808301e00b003c239d1f111mr17684234oib.48.1710876624619; Tue, 19 Mar 2024 12:30:24 -0700 (PDT) Received: from redhat.com (2603-7000-9500-34a5-0000-0000-0000-1db4.res6.spectrum.com. [2603:7000:9500:34a5::1db4]) by smtp.gmail.com with ESMTPSA id l2-20020a05620a28c200b0078a1df9dae7sm176720qkp.104.2024.03.19.12.30.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Mar 2024 12:30:24 -0700 (PDT) Date: Tue, 19 Mar 2024 15:30:22 -0400 From: Marek Polacek To: Jason Merrill Cc: GCC Patches Subject: [PATCH v2] c++: explicit inst of template method not generated [PR110323] Message-ID: References: <20240308170215.21919-1-polacek@redhat.com> MIME-Version: 1.0 In-Reply-To: User-Agent: Mutt/2.2.12 (2023-09-09) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-11.7 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, RCVD_IN_SORBS_WEB, 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.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 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 base-commit: bc91e3870e9c984c180b478a3449a9a2e56cd107 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). */; + 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" } }