[flang-commits] [flang] [flang] AliasAnalysis: Fix pointer component logic (PR #94242)

Joel E. Denny via flang-commits flang-commits at lists.llvm.org
Tue Jun 18 09:50:32 PDT 2024


================
@@ -159,21 +193,56 @@ AliasResult AliasAnalysis::alias(Value lhs, Value rhs) {
     src2->attributes.set(Attribute::Target);
   }
 
-  // Dummy TARGET/POINTER argument may alias with a global TARGET/POINTER.
+  // Two TARGET/POINTERs may alias.
   if (src1->isTargetOrPointer() && src2->isTargetOrPointer() &&
       src1->isData() == src2->isData()) {
     LLVM_DEBUG(llvm::dbgs() << "  aliasing because of target or pointer\n");
     return AliasResult::MayAlias;
   }
 
-  // Box for POINTER component inside an object of a derived type
-  // may alias box of a POINTER object, as well as boxes for POINTER
-  // components inside two objects of derived types may alias.
-  if ((src1->isRecordWithPointerComponent() && src2->isTargetOrPointer()) ||
-      (src2->isRecordWithPointerComponent() && src1->isTargetOrPointer()) ||
-      (src1->isRecordWithPointerComponent() &&
-       src2->isRecordWithPointerComponent())) {
-    LLVM_DEBUG(llvm::dbgs() << "  aliasing because of pointer components\n");
+  // A pointer dummy arg (but not a pointer component of a dummy arg) may alias
+  // a pointer component and thus the associated composite.  That composite
+  // might be a global or another dummy arg.  This is an example of the global
+  // composite case:
+  //
+  // module m
+  //   type t
+  //      real, pointer :: p
+  //   end type
+  //   type(t) :: a
+  //   type(t) :: b
+  // contains
+  //   subroutine test(p)
+  //     real, pointer :: p
+  //     p = 42
+  //     a = b
+  //     print *, p
+  //   end subroutine
+  // end module
+  // program
+  //   use m
+  //   real, target :: x1 = 1
+  //   real, target :: x2 = 2
+  //   a%p => x1
+  //   b%p => x2
+  //   call test(a%p)
+  // end
+  //
+  // The dummy argument p is an alias for a%p, even for the purposes of pointer
+  // association during the assignment a = b.  Thus, the program should print 2.
+  if ((isRecordWithPointerComponent(val1->getType()) &&
+       src1->kind != SourceKind::Allocate &&
+       src2->kind == SourceKind::Argument &&
+       src2->attributes.test(Attribute::Pointer) && !src2->isData() &&
+       !isRecordWithPointerComponent(src2->valueType)) ||
----------------
jdenny-ornl wrote:

Thanks for reviewing.  

The `if` preceding this one uses `isTargetOrPointer` and covers aliasing among addresses *in* pointers (what they point towards).  The `if` here has the `!src2->isData()` condition and so examines only the address *of* the pointer.

Specifically, the `if` here checks for aliasing with the address of a pointer that is a dummy argument.  The condition `!isRecordWithPointerComponent(src2->valueType)` prevents it from mistaking a pointer component of a dummy argument as a pointer dummy argument.  For example, [the test alias-analysis-9.fir has a check](https://github.com/jdenny-ornl/llvm-project/blob/1e5fdd49f4c8558f5181b3bdb2d38002e0b34630/flang/test/Analysis/AliasAnalysis/alias-analysis-9.fir#L31-L34) whose result would change without that condition:

```
// module m
// type t
//  type(t), pointer :: next
//  integer :: i
// end type
// contains
// subroutine foo(x, y)
//   type(t) :: x, y
//   integer :: i1, i2
//   i1 = x%next%i
//   x = y
//   i2 = x%next%i
// end subroutine
// end module

// CHECK-DAG: y#0 <-> xnext1#0: NoAlias
```

`y#0` is the address of `y`.  `xnext1#0` is the address of `x%next`. Without the above condition, AliasAnalysis would treat `xnext1#0` as the address of a pointer dummy argument, and the result above would become MayAlias.

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


More information about the flang-commits mailing list