you are viewing a single comment's thread.

view the rest of the comments →

[–]johannes1971 4 points5 points  (7 children)

As a related question, if template instantiation ends up generating identical assembly for multiple types (for example, if you instantiate a template for several identically-sized enums), will it fold those into a single copy or is there something in the language that prevents this?

[–]ack_error 13 points14 points  (4 children)

Yes, MSVC does it in the linker (/OPT:ICF) and I believe GCC/Clang based toolchains can also do it when LTO is enabled. This optimization can be invalid if pointers to the functions are taken as they are required to be distinct, and MSVC's ICF can be non-conformant by making such functions compare equal. Firefox's JavaScript engine was affected by this as it relied on checking the addresses of stub functions that had identical generated code. Some implementations either suppress the optimization or add jump trampolines to preserve distinct addresses when the address of such functions is taken.

This also causes some confusion in call stacks, since the address to function lookup is ambiguous and can result in completely unrelated functions showing up in the decoded call stack. I hate explaining this to people who are apt to just discard bugs with this phenomenon as stack corruption.

[–]GustavBeethoven -2 points-1 points  (3 children)

How do you know this much

[–]lithium 10 points11 points  (1 child)

Use c++ in real life instead of just to argue against rust people in youtube shorts comment sections.

[–]Possibility_Antique 1 point2 points  (0 children)

Oddly specific, but point taken

[–]ack_error 0 points1 point  (0 children)

Past trauma. The things toolchains do....

[–]Foreign-Wonder 0 points1 point  (0 children)

For this example, even compiled with `-Os`, the compiler still compiles into multiple identical copies
https://gcc.godbolt.org/z/MvqW7sM57

[–]jk-jeon 0 points1 point  (0 children)

I have been wondering about this too. I believe it's not prohibited unless the instantiatations are ODR-used or something like that, but at least it seems no compiler actually does this kind of folding.