* [Bug c++/115670] auto return type does not change the linkage for anonymous structs
2024-06-26 20:07 [Bug c++/115670] New: missed optimization - anonymous structures federico at kircheis dot it
@ 2024-06-26 20:11 ` pinskia at gcc dot gnu.org
2024-06-26 21:08 ` [Bug c++/115670] auto return type does not change the linkage for local structs pinskia at gcc dot gnu.org
` (5 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-26 20:11 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115670
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Keywords| |missed-optimization
Ever confirmed|0 |1
Summary|missed optimization - dead |auto return type does not
|code elimination for |change the linkage for
|anonymous structures |anonymous structs
Status|UNCONFIRMED |NEW
Severity|normal |enhancement
Last reconfirmed| |2024-06-26
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed.
Doing:
```
decltype(a) bar(){
return a;
}
```
GCC is able to change the linkage of bar. it is just auto which is not handled.
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/115670] auto return type does not change the linkage for local structs
2024-06-26 20:07 [Bug c++/115670] New: missed optimization - anonymous structures federico at kircheis dot it
2024-06-26 20:11 ` [Bug c++/115670] auto return type does not change the linkage for anonymous structs pinskia at gcc dot gnu.org
@ 2024-06-26 21:08 ` pinskia at gcc dot gnu.org
2024-06-27 17:38 ` [Bug c++/115670] auto placeholder for return type should change the linkage of the function if the type becomes local linkage too federico at kircheis dot it
` (4 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-26 21:08 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115670
--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Simplified case too:
```
auto bar(){
struct {int i;} t;
t.i = 0;
return t;
}
```
This should be local linkage.
Another testcase:
```
namespace {
struct a {int t = 12;};
}
auto bar(){
struct a t;
return t;
}
```
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/115670] auto placeholder for return type should change the linkage of the function if the type becomes local linkage too
2024-06-26 20:07 [Bug c++/115670] New: missed optimization - anonymous structures federico at kircheis dot it
2024-06-26 20:11 ` [Bug c++/115670] auto return type does not change the linkage for anonymous structs pinskia at gcc dot gnu.org
2024-06-26 21:08 ` [Bug c++/115670] auto return type does not change the linkage for local structs pinskia at gcc dot gnu.org
@ 2024-06-27 17:38 ` federico at kircheis dot it
2024-06-27 17:42 ` federico at kircheis dot it
` (3 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: federico at kircheis dot it @ 2024-06-27 17:38 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115670
--- Comment #3 from Federico Kircheis <federico at kircheis dot it> ---
I've collected the example mentioned here and in my original report
https://godbolt.org/z/o4893zhPs
~~~~
struct {
int i = 42;
} const a;
auto foo0(){
return a;
}
int foo1(decltype(a)&){
return 1;
}
decltype(a) foo2(){
return a;
}
namespace {
struct s{};
s foo3(){
return {};
}
}
s foo4(){
return {};
}
auto foo5(){
struct {} t;
return t;
}
~~~~
Except for foo0 and foo5, `-Wunused-function` emits a diagnostic (should I make
a separate issue for the warning?)
> GCC is able to change the linkage of bar. it is just auto which is not handled.
As you can se in the emitted code, even if GCC changes the linkage (I'm sorry,
I did not verify that), it still generates the corresponding code.
I suppose that that code can be discarded later by the linker.
In the case of MSVC, as far as I've understood, as the function is never used,
no code is emitted (less work for the linker).
I guess that a more generic approach would be that all unused code (all code
that triggers a diagnostic with -Wunused-function could be avoided...
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/115670] auto placeholder for return type should change the linkage of the function if the type becomes local linkage too
2024-06-26 20:07 [Bug c++/115670] New: missed optimization - anonymous structures federico at kircheis dot it
` (2 preceding siblings ...)
2024-06-27 17:38 ` [Bug c++/115670] auto placeholder for return type should change the linkage of the function if the type becomes local linkage too federico at kircheis dot it
@ 2024-06-27 17:42 ` federico at kircheis dot it
2024-06-27 20:28 ` pinskia at gcc dot gnu.org
` (2 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: federico at kircheis dot it @ 2024-06-27 17:42 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115670
--- Comment #4 from Federico Kircheis <federico at kircheis dot it> ---
Sorry, I've posted the wrong link in the previous reply, this is the correct
one
https://godbolt.org/z/nhrM46ajs
Also
~~~~
struct s2{
s i; //s is in anonymous namespace
};
s2 foo6(){
return {};
}
~~~~
does not warn with `-Wunused-function`
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/115670] auto placeholder for return type should change the linkage of the function if the type becomes local linkage too
2024-06-26 20:07 [Bug c++/115670] New: missed optimization - anonymous structures federico at kircheis dot it
` (3 preceding siblings ...)
2024-06-27 17:42 ` federico at kircheis dot it
@ 2024-06-27 20:28 ` pinskia at gcc dot gnu.org
2024-06-27 20:35 ` pinskia at gcc dot gnu.org
2024-06-30 11:12 ` federico at kircheis dot it
6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-27 20:28 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115670
--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
So at -O1, foo0, foo5, foo6 are the only ones left.
at -O0, foo1, foo2, foo3, foo4 are also emitted but that is expected since GCC
emits static unused functions always at -O0.
So let's keep this for foo0/foo5. I will file one for foo6 in a second.
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/115670] auto placeholder for return type should change the linkage of the function if the type becomes local linkage too
2024-06-26 20:07 [Bug c++/115670] New: missed optimization - anonymous structures federico at kircheis dot it
` (4 preceding siblings ...)
2024-06-27 20:28 ` pinskia at gcc dot gnu.org
@ 2024-06-27 20:35 ` pinskia at gcc dot gnu.org
2024-06-30 11:12 ` federico at kircheis dot it
6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-27 20:35 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115670
--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #5)
> So at -O1, foo0, foo5, foo6 are the only ones left.
>
> at -O0, foo1, foo2, foo3, foo4 are also emitted but that is expected since
> GCC emits static unused functions always at -O0.
>
>
> So let's keep this for foo0/foo5. I will file one for foo6 in a second.
Note if the definition of the anonymous namespace is in a header, then GCC does
warn for s2 (not for foo6 though).
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/115670] auto placeholder for return type should change the linkage of the function if the type becomes local linkage too
2024-06-26 20:07 [Bug c++/115670] New: missed optimization - anonymous structures federico at kircheis dot it
` (5 preceding siblings ...)
2024-06-27 20:35 ` pinskia at gcc dot gnu.org
@ 2024-06-30 11:12 ` federico at kircheis dot it
6 siblings, 0 replies; 8+ messages in thread
From: federico at kircheis dot it @ 2024-06-30 11:12 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115670
--- Comment #7 from Federico Kircheis <federico at kircheis dot it> ---
I've made a similar report to clang
(https://github.com/llvm/llvm-project/issues/97162) and they found another
function that could be optimized out
~~~
auto foo7() { return 1; }
~~~
They made me also realize that optimizing foo6 might be more complicated than I
thought initially (keyword: function pointers)
^ permalink raw reply [flat|nested] 8+ messages in thread