public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/110323] New: Code for explicit instantiation of template method of template class
@ 2023-06-20 15:57 stlim0727 at gmail dot com
2023-06-20 16:12 ` [Bug c++/110323] [11/12/13/14 Regression] Code for explicit instantiation of template method of template class not generated pinskia at gcc dot gnu.org
` (11 more replies)
0 siblings, 12 replies; 13+ messages in thread
From: stlim0727 at gmail dot com @ 2023-06-20 15:57 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110323
Bug ID: 110323
Summary: Code for explicit instantiation of template method of
template class
Product: gcc
Version: 11.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: stlim0727 at gmail dot com
Target Milestone: ---
Code for explicit instantiation of a template method of a class is not
generated with following code:
$ cat > test.cpp
#include <type_traits>
constexpr int VAL = 1;
struct foo {
template <int B>
void bar(typename std::conditional<B==VAL, int, float>::type arg) {
}
};
template void foo::bar<1>(int arg);
Code is not generated for g++11 and above versions:
$ g++12 -S -o- -c test.cpp
.file "test.cpp"
.text
.section .rodata
.align 4
.type _ZL3VAL, @object
.size _ZL3VAL, 4
_ZL3VAL:
.long 1
.ident "GCC: (GNU) 12.1.1 20220628 (Red Hat 12.1.1-3)"
.section .note.GNU-stack,"",@progbits
However, the code is generated if I replace "B==VAL" with "B==1" or if I use
g++10 and below versions:
$ g++8 -S -o- -c test.cpp
.file "test.cpp"
.text
.section .rodata
.align 4
.type _ZL3VAL, @object
.size _ZL3VAL, 4
_ZL3VAL:
.long 1
.section
.text._ZN3foo3barILi1EEEvNSt11conditionalIXeqT_L_ZL3VALEEifE4typeE,"axG",@progbits,_ZN3foo3barILi1EEEvNSt11conditionalIXeqT_L_ZL3VALEEifE4typeE,comdat
.align 2
.weak _ZN3foo3barILi1EEEvNSt11conditionalIXeqT_L_ZL3VALEEifE4typeE
.type _ZN3foo3barILi1EEEvNSt11conditionalIXeqT_L_ZL3VALEEifE4typeE,
@function
_ZN3foo3barILi1EEEvNSt11conditionalIXeqT_L_ZL3VALEEifE4typeE:
.LFB9:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movq %rdi, -8(%rbp)
movl %esi, -12(%rbp)
nop
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE9:
.size _ZN3foo3barILi1EEEvNSt11conditionalIXeqT_L_ZL3VALEEifE4typeE,
.-_ZN3foo3barILi1EEEvNSt11conditionalIXeqT_L_ZL3VALEEifE4typeE
.ident "GCC: (GNU) 8.5.0 20210514 (Red Hat 8.5.0-18)"
.section .note.GNU-stack,"",@progbits
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug c++/110323] [11/12/13/14 Regression] Code for explicit instantiation of template method of template class not generated
2023-06-20 15:57 [Bug c++/110323] New: Code for explicit instantiation of template method of template class stlim0727 at gmail dot com
@ 2023-06-20 16:12 ` pinskia at gcc dot gnu.org
2024-03-05 20:47 ` mpolacek at gcc dot gnu.org
` (10 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-20 16:12 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110323
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Keywords| |wrong-code
Target Milestone|--- |11.5
Summary|Code for explicit |[11/12/13/14 Regression]
|instantiation of template |Code for explicit
|method of template class |instantiation of template
|not generated |method of template class
| |not generated
Ever confirmed|0 |1
Last reconfirmed| |2023-06-20
Status|UNCONFIRMED |NEW
URL|https://godbolt.org/z/x4ao4 |
|dxdx |
Known to work|10.4.0 |10.1.0, 9.1.0
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed.
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug c++/110323] [11/12/13/14 Regression] Code for explicit instantiation of template method of template class not generated
2023-06-20 15:57 [Bug c++/110323] New: Code for explicit instantiation of template method of template class stlim0727 at gmail dot com
2023-06-20 16:12 ` [Bug c++/110323] [11/12/13/14 Regression] Code for explicit instantiation of template method of template class not generated pinskia at gcc dot gnu.org
@ 2024-03-05 20:47 ` mpolacek at gcc dot gnu.org
2024-03-05 20:49 ` mpolacek at gcc dot gnu.org
` (9 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2024-03-05 20:47 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110323
Marek Polacek <mpolacek at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Priority|P3 |P2
CC| |mpolacek at gcc dot gnu.org
--- Comment #2 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Started with r11-291-g0f50f6daa14018:
commit 0f50f6daa140186a048cbf33f54f4591eabf5f12
Author: Jason Merrill <jason@redhat.com>
Date: Mon May 11 15:46:59 2020 -0400
c++: tree walk into TYPENAME_TYPE.
```
template<bool B, class T, class F>
struct conditional { using type = T; };
template<class T, class F>
struct conditional<false, T, F> { using type = F; };
constexpr int VAL = 1;
struct foo {
template <int B>
void bar(typename conditional<B==VAL, int, float>::type arg) {
}
};
template void foo::bar<1>(int arg);
```
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug c++/110323] [11/12/13/14 Regression] Code for explicit instantiation of template method of template class not generated
2023-06-20 15:57 [Bug c++/110323] New: Code for explicit instantiation of template method of template class stlim0727 at gmail dot com
2023-06-20 16:12 ` [Bug c++/110323] [11/12/13/14 Regression] Code for explicit instantiation of template method of template class not generated pinskia at gcc dot gnu.org
2024-03-05 20:47 ` mpolacek at gcc dot gnu.org
@ 2024-03-05 20:49 ` mpolacek at gcc dot gnu.org
2024-03-05 21:39 ` mpolacek at gcc dot gnu.org
` (8 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2024-03-05 20:49 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110323
--- Comment #3 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
This makes a difference for some reason:
--- a/gcc/cp/tree.cc
+++ b/gcc/cp/tree.cc
@@ -5542,7 +5542,7 @@ cp_walk_subtrees (tree *tp, int *walk_subtrees_p,
walk_tree_fn func,
break;
case TYPENAME_TYPE:
- WALK_SUBTREE (TYPE_CONTEXT (t));
+ //WALK_SUBTREE (TYPE_CONTEXT (t));
WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
*walk_subtrees_p = 0;
break;
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug c++/110323] [11/12/13/14 Regression] Code for explicit instantiation of template method of template class not generated
2023-06-20 15:57 [Bug c++/110323] New: Code for explicit instantiation of template method of template class stlim0727 at gmail dot com
` (2 preceding siblings ...)
2024-03-05 20:49 ` mpolacek at gcc dot gnu.org
@ 2024-03-05 21:39 ` mpolacek at gcc dot gnu.org
2024-03-05 22:56 ` mpolacek at gcc dot gnu.org
` (7 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2024-03-05 21:39 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110323
--- Comment #4 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Ah -- if we walk into TYPE_CONTEXT (t) (here: struct conditional), then in
min_vis_r we determine the visibility as VISIBILITY_ANON. Without it, it
remains VISIBILITY_DEFAULT.
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug c++/110323] [11/12/13/14 Regression] Code for explicit instantiation of template method of template class not generated
2023-06-20 15:57 [Bug c++/110323] New: Code for explicit instantiation of template method of template class stlim0727 at gmail dot com
` (3 preceding siblings ...)
2024-03-05 21:39 ` mpolacek at gcc dot gnu.org
@ 2024-03-05 22:56 ` mpolacek at gcc dot gnu.org
2024-03-06 14:53 ` mpolacek at gcc dot gnu.org
` (6 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2024-03-05 22:56 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110323
--- Comment #5 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
VAL is constexpr, which implies const, which in the global scope implies
static. Then constrain_visibility_for_template makes "struct conditional<(B ==
VAL), int, float>" non-TREE_PUBLIC. So with
extern constexpr int VAL = 1;
the test works again.
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug c++/110323] [11/12/13/14 Regression] Code for explicit instantiation of template method of template class not generated
2023-06-20 15:57 [Bug c++/110323] New: Code for explicit instantiation of template method of template class stlim0727 at gmail dot com
` (4 preceding siblings ...)
2024-03-05 22:56 ` mpolacek at gcc dot gnu.org
@ 2024-03-06 14:53 ` mpolacek at gcc dot gnu.org
2024-03-07 15:23 ` mpolacek at gcc dot gnu.org
` (5 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2024-03-06 14:53 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110323
Marek Polacek <mpolacek at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |ASSIGNED
Assignee|unassigned at gcc dot gnu.org |mpolacek at gcc dot gnu.org
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug c++/110323] [11/12/13/14 Regression] Code for explicit instantiation of template method of template class not generated
2023-06-20 15:57 [Bug c++/110323] New: Code for explicit instantiation of template method of template class stlim0727 at gmail dot com
` (5 preceding siblings ...)
2024-03-06 14:53 ` mpolacek at gcc dot gnu.org
@ 2024-03-07 15:23 ` mpolacek at gcc dot gnu.org
2024-03-08 17:25 ` ppalka at gcc dot gnu.org
` (4 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2024-03-07 15:23 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110323
--- Comment #6 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Extended test. I think all 4 should be emitted.
```
// PR c++/110323
template<bool B, class T, class F>
struct conditional { using type = T; };
template<class T, class F>
struct conditional<false, T, F> { using type = F; };
constexpr int VAL = 1;
static constexpr int getval () { return 1; }
template<typename>
constexpr int TVAL = 1;
static struct S {
constexpr operator bool() { return true; }
} s;
struct foo {
template <int B>
void bar(typename conditional<B == VAL, int, float>::type arg) { }
template <int B>
void baz(typename conditional<B == getval (), int, float>::type arg) { }
template <int B>
void qux(typename conditional<B == TVAL<int>, int, float>::type arg) { }
template <int B>
void lox(typename conditional<B == s, int, float>::type arg) { }
};
template void foo::bar<1>(int arg);
template void foo::baz<1>(int arg);
template void foo::qux<1>(int arg);
template void foo::lox<1>(int arg);
```
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug c++/110323] [11/12/13/14 Regression] Code for explicit instantiation of template method of template class not generated
2023-06-20 15:57 [Bug c++/110323] New: Code for explicit instantiation of template method of template class stlim0727 at gmail dot com
` (6 preceding siblings ...)
2024-03-07 15:23 ` mpolacek at gcc dot gnu.org
@ 2024-03-08 17:25 ` ppalka at gcc dot gnu.org
2024-03-08 17:43 ` mpolacek at gcc dot gnu.org
` (3 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: ppalka at gcc dot gnu.org @ 2024-03-08 17:25 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110323
Patrick Palka <ppalka at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |ppalka at gcc dot gnu.org
--- Comment #7 from Patrick Palka <ppalka at gcc dot gnu.org> ---
I noticed we emit the function if we turn it into a non-member:
#include <type_traits>
constexpr int VAL = 1;
template <int B>
void bar(typename std::conditional<B==VAL, int, float>::type arg) {
}
template void bar<1>(int arg);
I wonder why this bug seems specific to member functions?
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug c++/110323] [11/12/13/14 Regression] Code for explicit instantiation of template method of template class not generated
2023-06-20 15:57 [Bug c++/110323] New: Code for explicit instantiation of template method of template class stlim0727 at gmail dot com
` (7 preceding siblings ...)
2024-03-08 17:25 ` ppalka at gcc dot gnu.org
@ 2024-03-08 17:43 ` mpolacek at gcc dot gnu.org
2024-03-08 17:47 ` mpolacek at gcc dot gnu.org
` (2 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2024-03-08 17:43 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110323
--- Comment #8 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
(In reply to Patrick Palka from comment #7)
> I noticed we emit the function if we turn it into a non-member:
>
> #include <type_traits>
>
> constexpr int VAL = 1;
>
> template <int B>
> void bar(typename std::conditional<B==VAL, int, float>::type arg) {
> }
>
> template void bar<1>(int arg);
>
> I wonder why this bug seems specific to member functions?
Good question. It's because in this case bar is marked as force_output here:
/* When not optimizing, also output the static functions. (see
PR24561), but don't do so for always_inline functions, functions
declared inline and nested functions. These were optimized out
in the original implementation and it is unclear whether we want
to change the behavior here. */
if (((!opt_for_fn (decl, optimize) || flag_keep_static_functions
|| node->no_reorder)
&& !node->cpp_implicit_alias
&& !DECL_DISREGARD_INLINE_LIMITS (decl)
&& !DECL_DECLARED_INLINE_P (decl)
&& !(DECL_CONTEXT (decl)
&& TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL))
&& !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
node->force_output = 1;
and when deciding if we ought to emit the fn in symtab_node::needed_p we do:
/* If the user told us it is used, then it must be so. */
if (force_output)
return true;
With -O the fn isn't emitted.
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug c++/110323] [11/12/13/14 Regression] Code for explicit instantiation of template method of template class not generated
2023-06-20 15:57 [Bug c++/110323] New: Code for explicit instantiation of template method of template class stlim0727 at gmail dot com
` (8 preceding siblings ...)
2024-03-08 17:43 ` mpolacek at gcc dot gnu.org
@ 2024-03-08 17:47 ` mpolacek at gcc dot gnu.org
2024-03-21 17:54 ` cvs-commit at gcc dot gnu.org
2024-03-21 17:55 ` [Bug c++/110323] [11/12/13 " mpolacek at gcc dot gnu.org
11 siblings, 0 replies; 13+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2024-03-08 17:47 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110323
--- Comment #9 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Oh and I meant to say it's the DECL_DECLARED_INLINE_P check that makes the
difference.
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug c++/110323] [11/12/13/14 Regression] Code for explicit instantiation of template method of template class not generated
2023-06-20 15:57 [Bug c++/110323] New: Code for explicit instantiation of template method of template class stlim0727 at gmail dot com
` (9 preceding siblings ...)
2024-03-08 17:47 ` mpolacek at gcc dot gnu.org
@ 2024-03-21 17:54 ` cvs-commit at gcc dot gnu.org
2024-03-21 17:55 ` [Bug c++/110323] [11/12/13 " mpolacek at gcc dot gnu.org
11 siblings, 0 replies; 13+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-03-21 17:54 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110323
--- Comment #10 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:
https://gcc.gnu.org/g:081f8937cb82da311c224da04b0c6cbd57a8fb5d
commit r14-9596-g081f8937cb82da311c224da04b0c6cbd57a8fb5d
Author: Marek Polacek <polacek@redhat.com>
Date: Thu Mar 7 20:41:23 2024 -0500
c++: explicit inst of template method not generated [PR110323]
Consider
constexpr int VAL = 1;
struct foo {
template <int B>
void bar(typename std::conditional<B==VAL, int, float>::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 <int N>
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) <case VAR_DECL>: 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.
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug c++/110323] [11/12/13 Regression] Code for explicit instantiation of template method of template class not generated
2023-06-20 15:57 [Bug c++/110323] New: Code for explicit instantiation of template method of template class stlim0727 at gmail dot com
` (10 preceding siblings ...)
2024-03-21 17:54 ` cvs-commit at gcc dot gnu.org
@ 2024-03-21 17:55 ` mpolacek at gcc dot gnu.org
11 siblings, 0 replies; 13+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2024-03-21 17:55 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110323
Marek Polacek <mpolacek at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Summary|[11/12/13/14 Regression] |[11/12/13 Regression] Code
|Code for explicit |for explicit instantiation
|instantiation of template |of template method of
|method of template class |template class not
|not generated |generated
--- Comment #11 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Fixed on trunk so far.
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2024-03-21 17:55 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-20 15:57 [Bug c++/110323] New: Code for explicit instantiation of template method of template class stlim0727 at gmail dot com
2023-06-20 16:12 ` [Bug c++/110323] [11/12/13/14 Regression] Code for explicit instantiation of template method of template class not generated pinskia at gcc dot gnu.org
2024-03-05 20:47 ` mpolacek at gcc dot gnu.org
2024-03-05 20:49 ` mpolacek at gcc dot gnu.org
2024-03-05 21:39 ` mpolacek at gcc dot gnu.org
2024-03-05 22:56 ` mpolacek at gcc dot gnu.org
2024-03-06 14:53 ` mpolacek at gcc dot gnu.org
2024-03-07 15:23 ` mpolacek at gcc dot gnu.org
2024-03-08 17:25 ` ppalka at gcc dot gnu.org
2024-03-08 17:43 ` mpolacek at gcc dot gnu.org
2024-03-08 17:47 ` mpolacek at gcc dot gnu.org
2024-03-21 17:54 ` cvs-commit at gcc dot gnu.org
2024-03-21 17:55 ` [Bug c++/110323] [11/12/13 " mpolacek at gcc dot gnu.org
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).