Do RVO and copy elision only work within one compilation unit or not?

1.1k Views Asked by At

Do they work across different object files? Do they work across different DLLs?

I know this depends on the compiler. I'm curious if there are any compilers and optimization settings that will make this work.

1

There are 1 best solutions below

6
On BEST ANSWER

Normally, yes, but in principle, using Link-Time-Optimization (-flto for GCC/Clang compilers and linkers) or Link-Time-Code-Generation (/LTCG and /GL for MSVC's compiler and linker), the compiler and linker can leverage their shared knowledge and perhaps inline code and elide copies. GCC's manual states:

[...] this causes all the interprocedural analyses and optimizations in GCC to work across the two files as if they were a single one. This means, for example, that the inliner is able to inline functions in bar.o into functions in foo.o and vice-versa.

Note this will not work with DLLs, because a shared library's code is fixed and already fully compiled.

RVO only needs information about the function itself (as it constructs the function's return value in-place instead of copying/moving on return. This will likely work without the aboce options.