Skip to content

Conversation

@Jezurko
Copy link

@Jezurko Jezurko commented Oct 31, 2025

The general idea is that the COMDAT group conflicts have to be resolved before the module is being linked (because COMDAT groups are used/thrown away as a whole), for that I added a hook to the linker that allows "presummary" of a module (moduleOpSummary()). After that, the COMDAT conflict resolution is fairly simple, after I finally understood how it's supposed to be done (resources bellow).
The "tricky" part about it is that it has to modify the already summarized operations.
The whole process is basically like this:

  • when a module is being processed, the COMDAT resolution is computed
  • for dropped COMDAT groups from previous modules the relevant operations are dropped by converting to declarations
  • if a symbol is a member of a COMDAT, the function isLinkNeeded for LLVM Dialect linking uses the COMDAT resolution to see if it was selected by a COMDAT. If yes, it says the symbol has to be linked. Note: if a COMDAT is replacing another COMDAT, the symbols form the replaced COMDAT are dropped to declarations, so the conflict resolution is usually simple. The declarations are stripped of COMDAT and do not participate in COMDAT resolution anymore (this is necessary to keep the IR valid; llvm-link does the same). The exception are NoDeduplicate COMDATs which should be handled in the conflict resolution.

The missing stuff:

  • replace aliases for dropped globals/funcs with declarations (see llvm-link for reference)
  • import more comdat-related tests from llvm-link
  • check that nodeduplicate comdat users conflicts are properly resolved in all cases (in getConflictResolution() ; see llvm-link for reference)

The main references in llvm-link are in the file llvm/lib/Linker/LinkModules.cpp:

  • getComdatResult() (and the functions used by it)
  • run() (precomputing the COMDAT resolution and handling dropping of the operations)
  • linkIfNeeded() (for nodeduplicate comdat conflict handling)

Some resources I found useful for understanding COMDAT and linking of COMDAT (althoug they are a bit sparse… Unfortunately reading the code of llvm-link was a must):
https://github.com/incc-project/inccblog/blob/main/linking/comdat.md
https://maskray.me/blog/2021-07-25-comdat-and-section-group
https://habr.com/en/articles/458442/ (not that helpful, but might help draw the whole picture)

@Jezurko Jezurko self-assigned this Oct 31, 2025
@Jezurko Jezurko force-pushed the rk/comdat branch 2 times, most recently from 575bfbc to 3f6a88d Compare November 7, 2025 13:53
@Jezurko Jezurko force-pushed the rk/comdat branch 16 times, most recently from 11fbdbb to 8b7f763 Compare November 14, 2025 15:47
…ummary

This allows the linker interface to pre-compute some information that might be necessary for the linking.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants