Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relation between MSVC Compiler & linker option for COMDAT folding

Tags:

This question has some answers on SO but mine is slightly different. Before marking as duplicate, please give it a shot.

MSVC has always provided the /Gy compiler option to enable identical functions to be folded into COMDAT sections. At the same time, the linker also provides the /OPT:ICF option. Is my understanding right that these two options must be used in conjunction? That is, while the former packages functions into COMDAT, the latter eliminates redundant COMDATs. Is that correct?

If yes, then either we use both or turn off both?

like image 976
ForeverLearning Avatar asked Nov 11 '16 19:11

ForeverLearning


People also ask

What compiler does MSVC use?

Microsoft C++ Compiler (MSVC) This is the default compiler for most Visual Studio C++ projects and is recommended if you are targeting Windows.

What is the difference between MinGW and MSVC?

Basically, MinGW is a port of Linux (GNU, actually) tools for Windows, which happen to include a port of GCC (the actual compiler). MSVC, on the other hand, is a Windows native tool chain and runtime for building C/C++ executables.

Is MSVC an ac compiler?

Microsoft C/C++ (MSVC) is a C and C++ compiler that, in its latest versions, conforms to some of the latest C language standards, including C11 and C17.

Where is MSVC compiler installed?

More precisely, the default path where you'll find the compiler is C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin .


1 Answers

Answer from someone who communicated with me off-line. Helped me understand these options a lot better.

===================================

That is essentially true. Suppose we talk just C, or C++ but with no member functions. Without /Gy, the compiler creates object files that are in some sense irreducible. If the linker wants just one function from the object, it gets them all. This is specially a consideration in programming for libraries, such that if you mean to be kind to the library's users, you should write your library as lots of small object files, typically one non-static function per object, so that the user of the library doesn't bloat from having to carry code that actually never executes.

With /Gy, the compiler creates object files that have COMDATs. Each function is in its own COMDAT, which is to some extent a mini-object. If the linker wants just one function from the object, it can pick out just that one. The linker's /OPT switch gives you some control over what the linker does with this selectivity - but without /Gy there's nothing to select.

Or very little. It's at least conceivable that the linker could, for instance, fold functions that are each the whole of the code in an object file and happen to have identical code. It's certainly conceivable that the linker could eliminate a whole object file that contains nothing that's referenced. After all, it does this with object files in libraries. The rule in practice, however, used to be that if you add a non-COMDAT object file to the linker's command line, then you're saying you want that in the binary even if unreferenced. The difference between what's conceivable and what's done is typically huge.

Best, then, to stick with the quick answer. The linker options benefit from being able to separate functions (and variables) from inside each object file, but the separation depends on the code and data to have been organised into COMDATs, which is the compiler's work.

===================================

like image 92
ForeverLearning Avatar answered Sep 26 '22 16:09

ForeverLearning