[llvm] [MergeFunctions] keep the symbol of a function an alias points at on COFF (PR #222024)

Saleem Abdulrasool via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 16 13:47:12 PDT 2026


================
@@ -860,6 +859,52 @@ static bool canCreateAliasFor(Function *F) {
   return true;
 }
 
+static bool hasNonLocalAlias(const Function *F) {
+  for (const GlobalAlias &GA : F->getParent()->aliases())
+    if (!GA.hasLocalLinkage() && GA.getAliaseeObject() == F)
+      return true;
+  return false;
+}
+
+/// COFF names a local alias target's fallback symbol after unrelated contents
+/// of the object, so two objects defining the alias disagree (LNK1227).
+static bool needsStableSymbolForAlias(const Function *F) {
+  return F->getParent()->getTargetTriple().isOSBinFormatCOFF();
+}
+
+/// Prefix of the name tryConvertToMergedFunction gives a shared body.
+static constexpr StringRef MergedFunctionPrefix = "__llvm_mergefunc$";
+
+/// Names \p F after its contents and puts it in a COMDAT, so every object
+/// merging this body agrees on it.
+static bool tryConvertToMergedFunction(Function *F) {
+  // The ODR path aliases both halves of a merge to the same body.
+  if (F->getName().starts_with(MergedFunctionPrefix))
+    return true;
+
+  // An externally visible name is fixed; the ODR path passes a nameless body.
+  if (F->hasName() && !F->hasLocalLinkage())
+    return false;
+
+  Module *M = F->getParent();
+  std::string Name =
+      (MergedFunctionPrefix +
+       Twine::utohexstr(StructuralHash(*F, /*DetailedHash=*/true)))
+          .str();
+  // A uniquing suffix would be numbered per object, so it cannot be used.
+  if (M->getNamedValue(Name))
+    return false;
+
+  F->setName(Name);
+  F->setLinkage(GlobalValue::LinkOnceODRLinkage);
+  F->setDLLStorageClass(GlobalValue::DefaultStorageClass);
----------------
compnerd wrote:

Wait why is this unconditionally setting this to internal storage? I can have a LinkOnceODR which participates in dynamic linking.

https://github.com/llvm/llvm-project/pull/222024


More information about the llvm-commits mailing list