<div dir="ltr"><div dir="ltr">On Wed, May 26, 2021 at 12:54 AM Fāng-ruì Sòng via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">I have studied the GC state of Windows (IR/frontend) PGO a bit.<br>
Below is my understanding. Hope folks can clarify it I make a mistake:)<br>
<br>
For PGO, we need cnts/data/vals/names sections. cnts/data are main<br>
metadata sections. vals is for value profiling.<br>
For Windows, the cnts section is named .lprfc$M and the data section<br>
is named .lprfd$M.<br>
The garbage collection story is unfortunate.<br>
<br>
If an IMAGE_COMDAT_SELECT_ASSOCIATIVE section defines an external<br>
symbol, MSVC link.exe may report a spurious duplicate symbol error<br>
(error LNK2005), even if the associative section would be discarded<br>
after handling the leader symbol.<br>
lld-link doesn't have this limitation.<br>
However, a portable implementation needs to work around MSVC link.exe.<br></blockquote><div><br></div><div>Yes, that's pretty much how I remember this working.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
For a COMDAT .lprfd$M, its symbol must be external (linkonce_odr),<br>
otherwise references to a non-prevailing symbol would cause an error.<br>
Due to the limitation, .lprfd$M has to reside in its own COMDAT, no<br>
sharing with .lprfc$M.<br></blockquote><div><br></div><div>Yes. I'm sure the instrumented code references the counters, so the counters must be external. However, if the __profd_ symbol is not referenced directly from anywhere, perhaps we could make it internal and have it join the __profc_* comdat to reverse the edge. If there are explicit __profd_ references from code, this won't work.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Different COMDAT groups mean that the liveness of one .lprfc$M does<br>
not make its associative .lprfd$M live.<br>
Since a .lprfd$M may be unreferenced, we have to conservatively assume<br>
all COMDAT .lprfd$M live.<br>
Since .lprfc$M input sections parallel .lprfd$M input sections, we<br>
have to conservatively assume all COMDAT .lprfc$M live.<br>
For an external symbol, we use a /INCLUDE: directive in .drectve to<br>
mark it as a GC root.<br>
As a result, .drectve may have many /INCLUDE: directives, just to work<br>
around the link.exe limitation.<br>
IIRC Chrome folks have found that the /INCLUDE: directives can cause<br>
huge memory usage.<br></blockquote><div><br></div><div>Yes, I have wanted to implement an LLD-specific extension for this feature. The general problem is that symbol names are long, and we should avoid repeating them in the object file if at all possible. I think llvm.used and __attribute__((used)) should be compatible with link.exe, but it would be reasonable to make PGO an LLD-only feature to get these object file size savings. Specifically, we can copy the design of .llvm_addrsig to mark symbols as GC roots.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Note: for ELF we can use R_*_NONE to establish an artificial<br>
dependency edge between two sections.<br>
I don't think PE-COFF provides a similar feature.<br></blockquote><div><br></div><div>Yep, not so far as I am aware.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
So does link.exe accept feature requests? The external symbol<br>
limitation in an IMAGE_COMDAT_SELECT_ASSOCIATIVE section looks quite<br>
unfortunate...<br></blockquote><div><br></div><div>I have a few points of contact at Microsoft, but there isn't a good general way to submit these kinds of low-level ABI feature requests.</div><div><br></div><div>The way the feature is designed, it only really works well when the associative data is static, unreferenced, and discovered only by section concatenation and iteration. This supports very many use cases: debug info, unwind info, dynamic initializers, asan global metadata, control flow guard metadata, others. In the PGO use case, I think we would be violating a hidden invariant that all the associative data should be static and unreferenced.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
It seems that /OPT:REF has another inefficiency - it cannot discard<br>
non-COMDAT sections.<br>
IIUC Fuchsia folks found that GC on non-COMDAT functions can save a<br>
lot of space (40%? of PGO metadata sections). It is unfortunate that<br>
link.exe doesn't discard non-COMDAT sections...<br></blockquote><div><br></div><div>This is not a major issue in practice because with -ffunction-sections / -fdata-sections, everything is in a comdat section, they just all use IMAGE_COMDAT_SELECT_NODUPLICATES.</div><div><br></div><div>I guess I really only have one idea here: find a way to avoid __profd_* references, and make __profc_ the comdat leader.</div><div><br></div><div>----</div><div><br></div><div>Separately, I have noticed that PGO greatly inhibits GC for other reasons. See the example from my last patch: <a href="https://reviews.llvm.org/D102818">https://reviews.llvm.org/D102818</a></div><div>For most linkonce_odr functions (so basically everything), the __profd_ global references the function, and then __profd_ is added to llvm.used. This prevents GC from working on any linkonce_odr function, which is bad for code size, so I removed the feature from most coverage builds.</div></div></div>