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

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 25 00:38:49 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:

I agree that there is an issue with !nofreeobj in that example, but it really doesn't have anything to do with combineMetadataForCSE().

This issue is specific to the instruction skipping support in SimplifyCFG, which ends up interchanging @free and !nofreeobj without dropping the metadata. The fact that it also happens to be CSEd with another instruction is secondary.

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


More information about the llvm-commits mailing list