Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is LLVM Comdat?

What does comdat in LLVM represents? You can find the source here:Comdata

An example from source level program representation (c++) would be very much helpful.

If you need more info, please feel free to ask. I find it in many places in llvm code base, but I can't able to figure it what exactly it is, and it's uses

Thanks for your help!

like image 878
PreeJackie Avatar asked Apr 19 '19 20:04

PreeJackie


1 Answers

I'm also learning about Comdat and see the below explanation from this blog.

A Comdat section is a section in the object file, in which objects are placed, which can be duplicated in other object files. Each object has information for the linker, indicating what it must do when duplicates are detected. The options can be: Any — do anything, ExactMatch — duplicates must completely match, otherwise an error occurs, Largest — take the object with the largest value, NoDublicates — there should not be a duplicate, SameSize — duplicates must have the same size, otherwise an error occurs.

In LLVM, Comdat data is represented by an enumeration:

enum SelectionKind {
    Any,          ///< The linker may choose any COMDAT.
    ExactMatch,   ///< The data referenced by the COMDAT must be the same.
    Largest,      ///< The linker will choose the largest COMDAT.
    NoDuplicates, ///< No other Module may specify this COMDAT.
    SameSize,     ///< The data referenced by the COMDAT must be the same size.
  };

and the class Comdat actually represents a pair (Name, SelectionKind). (In fact, everything is more complicated.) All variables that for some reason cannot be deleted are placed in a set of NotDiscardableComdats. With functions and global aliases, we do the same — something that can not be deleted is placed in NotDiscardableComdats. Then, separate optimization functions for global constructors, global functions, global variables, global aliases, and global destructors are called. Optimizations continue in the loop until no optimization is performed. At each iteration of the loop, the set of NotDiscardableComdats is set to zero.

like image 172
Trung Ta Avatar answered Sep 17 '22 15:09

Trung Ta