Skip to content

[flang] Improve alias analysis to be precise for box and box.base_addr #80335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 5, 2024

Conversation

razvanlupusoru
Copy link
Contributor

After PR#68727 the source for both the fir.box_addr and a box became the same. Thus the detection that only one of the sources was direct and the special logic around it was being skipped. As a result, the test included would show a "MayAlias" result instead of a "NoAlias" result.

After PR#68727 the source for both the fir.box_addr and a box became the
same. Thus the detection that only one of the sources was direct
and the special logic around it was being skipped. As a result,
the test included would show a "MayAlias" result instead of a "NoAlias"
result.
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Feb 1, 2024
@llvmbot
Copy link
Member

llvmbot commented Feb 1, 2024

@llvm/pr-subscribers-flang-fir-hlfir

Author: Razvan Lupusoru (razvanlupusoru)

Changes

After PR#68727 the source for both the fir.box_addr and a box became the same. Thus the detection that only one of the sources was direct and the special logic around it was being skipped. As a result, the test included would show a "MayAlias" result instead of a "NoAlias" result.


Full diff: https://github.com/llvm/llvm-project/pull/80335.diff

2 Files Affected:

  • (modified) flang/lib/Optimizer/Analysis/AliasAnalysis.cpp (+1-1)
  • (added) flang/test/Analysis/AliasAnalysis/alias-analysis-8.fir (+40)
diff --git a/flang/lib/Optimizer/Analysis/AliasAnalysis.cpp b/flang/lib/Optimizer/Analysis/AliasAnalysis.cpp
index 90072eea323be..ea2064e32a44d 100644
--- a/flang/lib/Optimizer/Analysis/AliasAnalysis.cpp
+++ b/flang/lib/Optimizer/Analysis/AliasAnalysis.cpp
@@ -106,7 +106,7 @@ AliasResult AliasAnalysis::alias(Value lhs, Value rhs) {
   // Though nothing is known about them, they would only alias with targets or
   // pointers
   bool directSourceToNonTargetOrPointer = false;
-  if (lhsSrc.u != rhsSrc.u) {
+  if (lhsSrc.u != rhsSrc.u || lhsSrc.kind != rhsSrc.kind) {
     if ((lhsSrc.kind == SourceKind::Direct && !rhsSrc.isTargetOrPointer()) ||
         (rhsSrc.kind == SourceKind::Direct && !lhsSrc.isTargetOrPointer()))
       directSourceToNonTargetOrPointer = true;
diff --git a/flang/test/Analysis/AliasAnalysis/alias-analysis-8.fir b/flang/test/Analysis/AliasAnalysis/alias-analysis-8.fir
new file mode 100644
index 0000000000000..a04c9523f472a
--- /dev/null
+++ b/flang/test/Analysis/AliasAnalysis/alias-analysis-8.fir
@@ -0,0 +1,40 @@
+// RUN: fir-opt %s -pass-pipeline='builtin.module(func.func(test-fir-alias-analysis))' 2>&1 | FileCheck %s
+
+// Exercise that a box reference does not alias with the address loaded from the box
+// module mm
+//   real, allocatable :: arr(:)
+// contains
+// subroutine sub
+//   arr(1) = ubound(arr, 1)
+// end subroutine
+// end module
+
+// CHECK: box#0 <-> boxaddr#0: NoAlias
+
+fir.global @_QMmmEarr : !fir.box<!fir.heap<!fir.array<?xf32>>> {
+  %c0 = arith.constant 0 : index
+  %0 = fir.zero_bits !fir.heap<!fir.array<?xf32>>
+  %1 = fir.shape %c0 : (index) -> !fir.shape<1>
+  %2 = fir.embox %0(%1) : (!fir.heap<!fir.array<?xf32>>, !fir.shape<1>) -> !fir.box<!fir.heap<!fir.array<?xf32>>>
+  fir.has_value %2 : !fir.box<!fir.heap<!fir.array<?xf32>>>
+}
+func.func @_QMmmPsub() {
+  %c1 = arith.constant 1 : index
+  %c1_i64 = arith.constant 1 : i64
+  %c0 = arith.constant 0 : index
+  %0 = fir.address_of(@_QMmmEarr) : !fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>
+  %1 = fir.declare %0 {fortran_attrs = #fir.var_attrs<allocatable>, uniq_name = "_QMmmEarr", test.ptr = "box"} : (!fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>
+  %2 = fir.load %1 : !fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>
+  %3:3 = fir.box_dims %2, %c0 : (!fir.box<!fir.heap<!fir.array<?xf32>>>, index) -> (index, index, index)
+  %4 = fir.convert %3#1 : (index) -> i64
+  %5 = fir.convert %3#0 : (index) -> i64
+  %6 = arith.addi %4, %5 : i64
+  %7 = arith.subi %6, %c1_i64 : i64
+  %8 = fir.convert %7 : (i64) -> i32
+  %9 = fir.convert %8 : (i32) -> f32
+  %10 = fir.box_addr %2 {test.ptr = "boxaddr"} : (!fir.box<!fir.heap<!fir.array<?xf32>>>) -> !fir.heap<!fir.array<?xf32>>
+  %11 = fir.shape_shift %3#0, %3#1 : (index, index) -> !fir.shapeshift<1>
+  %12 = fir.array_coor %10(%11) %c1 : (!fir.heap<!fir.array<?xf32>>, !fir.shapeshift<1>, index) -> !fir.ref<f32>
+  fir.store %9 to %12 : !fir.ref<f32>
+  return
+}

Copy link
Contributor

@tblah tblah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense to me, but please wait for approval from at least one of the other reviewers.

Out of interest, are you able to share what you are using this for? I believe the existing alias analysis infrastructure should already be able to distinguish between box and non-box accesses.

@razvanlupusoru
Copy link
Contributor Author

razvanlupusoru commented Feb 2, 2024

Out of interest, are you able to share what you are using this for?

I was trying it out on some OpenACC. I am interested in creating an alias analysis framework (at MLIR level) for it as well.

I believe the existing alias analysis infrastructure should already be able to distinguish between box and non-box accesses.

Are you referring to the TBAA implementation? As far as I understood this was mostly for passing information to LLVM, not for alias analysis in MLIR passes.

@tblah
Copy link
Contributor

tblah commented Feb 2, 2024

Are you referring to the TBAA implementation? As far as I understood this was mostly for passing information to LLVM, not for alias analysis in MLIR passes.

Yes that was what I meant. Yes it is not for MLIR passes.

@razvanlupusoru razvanlupusoru merged commit 702664e into llvm:main Feb 5, 2024
agozillon pushed a commit to agozillon/llvm-project that referenced this pull request Feb 5, 2024
llvm#80335)

After PR#68727 the source for both the fir.box_addr and a box became the
same. Thus the detection that only one of the sources was direct and the
special logic around it was being skipped. As a result, the test
included would show a "MayAlias" result instead of a "NoAlias" result.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants