[llvm] [MergeFunctions] Combine instruction metadata instead of comparing it (PR #225921)

Mian Miftah via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 25 06:09:11 PDT 2026


================
@@ -1094,11 +1157,41 @@ void MergeFunctions::mergeInstrAnnotations(Function *Dst, Function *Src) {
   // equivalent functions need not store their basic blocks in the same order.
   ReversePostOrderTraversal<Function *> DstRPOT(Dst);
   ReversePostOrderTraversal<Function *> SrcRPOT(Src);
+
+  // A noalias scope declaration sets how long its scope lasts, for example one
+  // loop iteration, and FunctionComparator does not compare the declared
+  // scopes. If they differ, the same !alias.scope or !noalias can mean
+  // different things in the two functions, so drop them.
+  bool SameScopeDecls = true;
+  for (auto [DstBB, SrcBB] : llvm::zip_equal(DstRPOT, SrcRPOT))
+    for (auto [DstI, SrcI] : llvm::zip_equal(*DstBB, *SrcBB))
+      if (auto *DstDecl = dyn_cast<NoAliasScopeDeclInst>(&DstI))
+        if (DstDecl->getScopeList() !=
+            cast<NoAliasScopeDeclInst>(SrcI).getScopeList())
+          SameScopeDecls = false;
+
+  // combineMetadataForCSE() drops these kinds, even if they are the same on
+  // both instructions. Losing them would make the merged function worse than
+  // both originals: memcpy would lose its TBAA, atomics would be expanded to
+  // compare-and-swap loops, and inline assembly diagnostics would lose their
+  // source location.
+  LLVMContext &Ctx = Dst->getContext();
+  const unsigned KeepIfSame[] = {
+      LLVMContext::MD_tbaa_struct, LLVMContext::MD_atomic_ignore_denormal_mode,
----------------
mmiftahx wrote:

Makes sense. SimplifyCFG already has the same problem with `!dereferenceable` on `inttoptr`, since `combineMetadataForCSE()` keeps it when it's the same on both. 

I'll put up a fix for the skipping in SimplifyCFG first, and then do the `combineMetadataForCSE()` change without the `!nofreeobj` special case.

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


More information about the llvm-commits mailing list