@@ -562,6 +562,8 @@ is zero. The address space qualifier must precede any other attributes.
562
562
563
563
LLVM allows an explicit section to be specified for globals. If the
564
564
target supports it, it will emit globals to the section specified.
565
+ Additionally, the global can placed in a comdat if the target has the necessary
566
+ support.
565
567
566
568
By default, global initializers are optimized by assuming that global
567
569
variables defined within the module are not modified from their
@@ -627,8 +629,9 @@ an optional ``unnamed_addr`` attribute, a return type, an optional
627
629
:ref: `parameter attribute <paramattrs >` for the return type, a function
628
630
name, a (possibly empty) argument list (each with optional :ref: `parameter
629
631
attributes <paramattrs>`), optional :ref: `function attributes <fnattrs >`,
630
- an optional section, an optional alignment, an optional :ref: `garbage
631
- collector name <gc>`, an optional :ref: `prefix <prefixdata >`, an opening
632
+ an optional section, an optional alignment,
633
+ an optional :ref: `comdat <langref_comdats >`,
634
+ an optional :ref: `garbage collector name <gc >`, an optional :ref: `prefix <prefixdata >`, an opening
632
635
curly brace, a list of basic blocks, and a closing curly brace.
633
636
634
637
LLVM function declarations consist of the "``declare ``" keyword, an
@@ -658,6 +661,7 @@ predecessors, it also cannot have any :ref:`PHI nodes <i_phi>`.
658
661
659
662
LLVM allows an explicit section to be specified for functions. If the
660
663
target supports it, it will emit functions to the section specified.
664
+ Additionally, the function can placed in a COMDAT.
661
665
662
666
An explicit alignment may be specified for a function. If not present,
663
667
or if the alignment is set to zero, the alignment of the function is set
@@ -673,8 +677,8 @@ Syntax::
673
677
define [linkage] [visibility] [DLLStorageClass]
674
678
[cconv] [ret attrs]
675
679
<ResultType> @<FunctionName> ([argument list])
676
- [unnamed_addr] [fn Attrs] [section "name"] [align N ]
677
- [gc] [prefix Constant] { ... }
680
+ [unnamed_addr] [fn Attrs] [section "name"] [comdat $<ComdatName> ]
681
+ [align N] [ gc] [prefix Constant] { ... }
678
682
679
683
.. _langref_aliases :
680
684
@@ -716,6 +720,89 @@ some can only be checked when producing an object file:
716
720
* No global value in the expression can be a declaration, since that
717
721
would require a relocation, which is not possible.
718
722
723
+ .. _langref_comdats :
724
+
725
+ Comdats
726
+ -------
727
+
728
+ Comdat IR provides access to COFF and ELF object file COMDAT functionality.
729
+
730
+ Comdats have a name which represents the COMDAT key. All global objects which
731
+ specify this key will only end up in the final object file if the linker chooses
732
+ that key over some other key. Aliases are placed in the same COMDAT that their
733
+ aliasee computes to, if any.
734
+
735
+ Comdats have a selection kind to provide input on how the linker should
736
+ choose between keys in two different object files.
737
+
738
+ Syntax::
739
+
740
+ $<Name> = comdat SelectionKind
741
+
742
+ The selection kind must be one of the following:
743
+
744
+ ``any ``
745
+ The linker may choose any COMDAT key, the choice is arbitrary.
746
+ ``exactmatch ``
747
+ The linker may choose any COMDAT key but the sections must contain the
748
+ same data.
749
+ ``largest ``
750
+ The linker will choose the section containing the largest COMDAT key.
751
+ ``noduplicates ``
752
+ The linker requires that only section with this COMDAT key exist.
753
+ ``samesize ``
754
+ The linker may choose any COMDAT key but the sections must contain the
755
+ same amount of data.
756
+
757
+ Note that the Mach-O platform doesn't support COMDATs and ELF only supports
758
+ ``any `` as a selection kind.
759
+
760
+ Here is an example of a COMDAT group where a function will only be selected if
761
+ the COMDAT key's section is the largest:
762
+
763
+ .. code-block :: llvm
764
+
765
+ $foo = comdat largest
766
+ @foo = global i32 2, comdat $foo
767
+
768
+ define void @bar() comdat $foo {
769
+ ret void
770
+ }
771
+
772
+ In a COFF object file, this will create a COMDAT section with selection kind
773
+ ``IMAGE_COMDAT_SELECT_LARGEST `` containing the contents of the ``@foo `` symbol
774
+ and another COMDAT section with selection kind
775
+ ``IMAGE_COMDAT_SELECT_ASSOCIATIVE `` which is associated with the first COMDAT
776
+ section and contains the contents of the ``@baz `` symbol.
777
+
778
+ There are some restrictions on the properties of the global object.
779
+ It, or an alias to it, must have the same name as the COMDAT group when
780
+ targeting COFF.
781
+ The contents and size of this object may be used during link-time to determine
782
+ which COMDAT groups get selected depending on the selection kind.
783
+ Because the name of the object must match the name of the COMDAT group, the
784
+ linkage of the global object must not be local; local symbols can get renamed
785
+ if a collision occurs in the symbol table.
786
+
787
+ The combined use of COMDATS and section attributes may yield surprising results.
788
+ For example:
789
+
790
+ .. code-block :: llvm
791
+
792
+ $foo = comdat any
793
+ $bar = comdat any
794
+ @g1 = global i32 42, section "sec", comdat $foo
795
+ @g2 = global i32 42, section "sec", comdat $bar
796
+
797
+ From the object file perspective, this requires the creation of two sections
798
+ with the same name. This is necessary because both globals belong to different
799
+ COMDAT groups and COMDATs, at the object file level, are represented by
800
+ sections.
801
+
802
+ Note that certain IR constructs like global variables and functions may create
803
+ COMDATs in the object file in addition to any which are specified using COMDAT
804
+ IR. This arises, for example, when a global variable has linkonce_odr linkage.
805
+
719
806
.. _namedmetadatastructure :
720
807
721
808
Named Metadata
0 commit comments