[Mlir-commits] [mlir] [mlir][analysis] Add interprocedural analysis to LocalAliasAnalysis (PR #177994)
lonely eagle
llvmlistbot at llvm.org
Mon Jan 26 19:07:02 PST 2026
https://github.com/linuxlonelyeagle updated https://github.com/llvm/llvm-project/pull/177994
>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 1/2] 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;
>From a6ec1717a2e0b453598ae6bfd4f4d001d8f55335 Mon Sep 17 00:00:00 2001
From: linuxlonelyeagle <2020382038 at qq.com>
Date: Tue, 27 Jan 2026 03:06:46 +0000
Subject: [PATCH 2/2] add test.
---
mlir/test/Analysis/test-alias-analysis.mlir | 62 +++++++++++++++++++++
1 file changed, 62 insertions(+)
diff --git a/mlir/test/Analysis/test-alias-analysis.mlir b/mlir/test/Analysis/test-alias-analysis.mlir
index d71adee05c7a3..b0fd2da3b7e6d 100644
--- a/mlir/test/Analysis/test-alias-analysis.mlir
+++ b/mlir/test/Analysis/test-alias-analysis.mlir
@@ -1,4 +1,5 @@
// RUN: mlir-opt %s -pass-pipeline='builtin.module(func.func(test-alias-analysis))' -split-input-file -allow-unregistered-dialect 2>&1 | FileCheck %s
+// RUN: mlir-opt %s -pass-pipeline='builtin.module(test-alias-analysis)' -split-input-file -allow-unregistered-dialect 2>&1 | FileCheck %s -check-prefix=CHECK-INTERPROCEDURAL
// CHECK-LABEL: Testing : "simple"
// CHECK-DAG: func.region0#0 <-> func.region0#1: MayAlias
@@ -272,3 +273,64 @@ func.func @distinct_objects(%arg: memref<?xf32>, %arg1: memref<?xf32>) attribute
%0, %1 = memref.distinct_objects %arg, %arg1 {test.ptr = "distinct"} : memref<?xf32>, memref<?xf32>
return
}
+
+// -----
+
+// CHECK-INTERPROCEDURAL-LABEL: Testing : "m_may_alias"
+// CHECK-INTERPROCEDURAL-DAG: func_foo.region0#0 <-> func_foo.region0#1: MayAlias
+// CHECK-INTERPROCEDURAL-DAG: func_foo.region0#0 <-> alloc_1#0: MayAlias
+// CHECK-INTERPROCEDURAL-DAG: func_foo.region0#1 <-> alloc_1#0: MayAlias
+// CHECK-INTERPROCEDURAL-DAG: func_foo.region0#0 <-> alloc_2#0: MayAlias
+// CHECK-INTERPROCEDURAL-DAG: func_foo.region0#1 <-> alloc_2#0: MayAlias
+// CHECK-INTERPROCEDURAL-DAG: alloc_1#0 <-> alloc_2#0: NoAlias
+module @m_may_alias{
+ func.func private @foo(%arg: memref<8x64xf32>, %arg1:memref<8x64xf32>) attributes {test.ptr = "func_foo"} {
+ return
+ }
+
+ func.func @main(%arg: memref<2xf32>, %arg1: memref<2xf32>) {
+ %2 = memref.alloc() {test.ptr = "alloc_1"} : memref<8x64xf32>
+ %3 = memref.alloc() {test.ptr = "alloc_2"} : memref<8x64xf32>
+ func.call @foo(%3, %3) : (memref<8x64xf32>, memref<8x64xf32>) -> ()
+ func.call @foo(%2, %3) : (memref<8x64xf32>, memref<8x64xf32>) -> ()
+ return
+ }
+}
+
+// -----
+
+// CHECK-INTERPROCEDURAL-LABEL: Testing : "m_must_alias"
+// CHECK-INTERPROCEDURAL-DAG: func_foo.region0#0 <-> func_foo.region0#1: MustAlias
+
+module @m_must_alias{
+ func.func private @foo(%arg: memref<8x64xf32>, %arg1:memref<8x64xf32>) attributes {test.ptr = "func_foo"} {
+ return
+ }
+
+ func.func @main(%arg: memref<2xf32>, %arg1: memref<2xf32>) {
+ %2 = memref.alloc() {test.ptr = "alloc_1"} : memref<8x64xf32>
+ %3 = memref.alloc() {test.ptr = "alloc_2"} : memref<8x64xf32>
+ func.call @foo(%3, %3) : (memref<8x64xf32>, memref<8x64xf32>) -> ()
+ func.call @foo(%3, %3) : (memref<8x64xf32>, memref<8x64xf32>) -> ()
+ return
+ }
+}
+
+// -----
+
+// CHECK-INTERPROCEDURAL-LABEL: Testing : "m_no_alias"
+// CHECK-INTERPROCEDURAL-DAG: func_foo.region0#0 <-> func_foo.region0#1: NoAlias
+
+module @m_no_alias{
+ func.func private @foo(%arg: memref<8x64xf32>, %arg1:memref<8x64xf32>) attributes {test.ptr = "func_foo"} {
+ return
+ }
+
+ func.func @main(%arg: memref<2xf32>, %arg1: memref<2xf32>) {
+ %2 = memref.alloc() {test.ptr = "alloc_1"} : memref<8x64xf32>
+ %3 = memref.alloc() {test.ptr = "alloc_2"} : memref<8x64xf32>
+ func.call @foo(%2, %3) : (memref<8x64xf32>, memref<8x64xf32>) -> ()
+ func.call @foo(%2, %3) : (memref<8x64xf32>, memref<8x64xf32>) -> ()
+ return
+ }
+}
More information about the Mlir-commits
mailing list