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

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 24 00:56:36 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,
----------------
nikic wrote:

Though I'm also thinking we should just make combineMetadataForCSE() retain metadata by default if it's exactly the same on both instructions. Maybe it's a lack of imagination, but I can't think of a case where it would not be valid to retain metadata if it's *exactly* the same on both instructions.

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


More information about the llvm-commits mailing list