[Mlir-commits] [mlir] [mlir][analysis] Add interprocedural analysis to LocalAliasAnalysis (PR #177994)

lonely eagle llvmlistbot at llvm.org
Mon Jan 26 08:58:06 PST 2026


https://github.com/linuxlonelyeagle created https://github.com/llvm/llvm-project/pull/177994

None

>From 0fe31bd3c3aac3d30457e93643c16679fc6b9f67 Mon Sep 17 00:00:00 2001
From: linuxlonelyeagle <2020382038 at qq.com>
Date: Mon, 26 Jan 2026 16:57:01 +0000
Subject: [PATCH] add interprocedural analysis to LocalAliasAnalysis.

---
 .../AliasAnalysis/LocalAliasAnalysis.cpp      | 38 +++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/mlir/lib/Analysis/AliasAnalysis/LocalAliasAnalysis.cpp b/mlir/lib/Analysis/AliasAnalysis/LocalAliasAnalysis.cpp
index 5a4679ef31422..acec7ed144929 100644
--- a/mlir/lib/Analysis/AliasAnalysis/LocalAliasAnalysis.cpp
+++ b/mlir/lib/Analysis/AliasAnalysis/LocalAliasAnalysis.cpp
@@ -339,6 +339,44 @@ AliasResult LocalAliasAnalysis::aliasImpl(Value lhs, Value rhs) {
     return AliasResult::MustAlias;
   }
 
+  // If both the lhs and rhs are function arguments, we perform interprocedural
+  // analysis.
+  if (isa<BlockArgument>(lhs) && isa<BlockArgument>(rhs) &&
+      lhs.getParentRegion() && lhs.getParentRegion() == rhs.getParentRegion() &&
+      isa<FunctionOpInterface>(lhs.getParentRegion()->getParentOp())) {
+    BlockArgument lhsArg = dyn_cast<BlockArgument>(lhs);
+    BlockArgument rhsArg = dyn_cast<BlockArgument>(rhs);
+    FunctionOpInterface function =
+        cast<FunctionOpInterface>(lhs.getParentRegion()->getParentOp());
+    // There are unknown function calls, so we cannot perform the analysis, we
+    // assume the result is MayAlias.
+    if (!function.isPrivate())
+      return AliasResult::MayAlias;
+
+    size_t mustAlias = 0, mayAlias = 0, noAlias = 0;
+    if (std::optional<SymbolTable::UseRange> uses =
+            function.getSymbolUses(function->getParentOfType<ModuleOp>())) {
+      for (auto use : *uses) {
+        Operation *user = use.getUser();
+        AliasResult userResult =
+            aliasImpl(user->getOperand(lhsArg.getArgNumber()),
+                      user->getOperand(rhsArg.getArgNumber()));
+        if (userResult.isNo())
+          ++noAlias;
+        else if (userResult.isMay())
+          ++mayAlias;
+        else if (userResult.isMust())
+          ++mustAlias;
+      }
+    }
+    if (mayAlias || (noAlias && mustAlias))
+      return AliasResult::MayAlias;
+    else if (noAlias)
+      return AliasResult::NoAlias;
+    else if (mustAlias)
+      return AliasResult::MustAlias;
+  }
+
   Operation *lhsAllocScope = nullptr, *rhsAllocScope = nullptr;
   std::optional<MemoryEffects::EffectInstance> lhsAlloc, rhsAlloc;
 



More information about the Mlir-commits mailing list