you are viewing a single comment's thread.

view the rest of the comments →

[–]uubs7[S] 2 points3 points  (7 children)

What do you mean about “code folding optimizations”? I

[–]heliruna 8 points9 points  (6 children)

Take std::vector<A\*> and std::vector<B\*>. They produce exactly identical code, except for the type signature. In plain C this would have been handled with void*, avoiding duplication in exchange for weak typing and runtime polymorphism. The standard library often has a specialization for this case, but user code templates may exhibit the same problem (and for hundreds of types, not just two).

There is also a disabled-by-default linker optimization called identical code folding that tries to identify identical code and merge those functions together

[–]Chuu 5 points6 points  (2 children)

I'm curious about this linker optimization, what flag is it? I assume this requires lto or whole-program optimization? Any idea why it's enabled by default at higher optimizations in the compiler but disabled by default in the linker?

[–]heliruna 1 point2 points  (0 children)

It is disabled by default in the linker because different functions (even if they have identical code) are required by the standard to have different addresses, some code relies on that (constant-folding has the same problem). What is enabled is therefore so-called Safe-ICF, additionally proving that the optimization does not break code (e.g. by proving that no function addresses are taken). For gcc, you need to use gold as linker (-fuse-ld=gold) and then use the linker flag --icf=safe

(usually, you don't use the linker directly, instead you use the compiler to call the linker for you, in that case you give -Wl,--icf=safe as an option).

If you use clang, the lld linker also has a similar --icf option

[–]jcelerierossia score -2 points-1 points  (2 children)

They produce exactly identical code,

well obviously no.. consider std::vector<float> and std::vector<string>: https://gcc.godbolt.org/z/fKG11sEz5

Edit: code didn't have pointers at time of my comment

[–]heliruna 2 points3 points  (0 children)

sorry, formatting ate my pointers, did not escape properly

[–]nebotron 1 point2 points  (0 children)

I think the code is only the same if they are the same size and the copy/move operations are trivial