[flang-commits] [flang] [flang] AliasAnalysis: Fix pointer component logic (PR #94242)
Joel E. Denny via flang-commits
flang-commits at lists.llvm.org
Wed Jun 19 12:32:26 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:
I find it confusing that the `target` attribute on `c` is relevant in your example given that there are no pointers or other targets with the type of `c`.
Here is Fortran 2023 sec. 15.5.2.14, 1 (3) (b):
> 1 While an entity is associated with a dummy argument, the following restrictions hold.
>
> (3) Action that affects the value of the entity or any subobject of it shall be taken only through the dummy argument unless
>
> (b) the dummy argument is a scalar, assumed-shape, or assumed-rank object, and has the TARGET attribute but not the INTENT (IN) or CONTIGUOUS attributes, and the actual argument is a target other than a coindexed object or an array section with a vector subscript,
`c` is the dummy argument. `a` is the actual argument. But the above requires that "the actual argument is a target", which 3.141 defines as:
> entity that is pointer associated with a pointer (3.104), entity on the right-hand-side of a pointer assignment statement, or entity with the TARGET attribute
I don't see how `a` is a target in your example, so I don't see how your example is valid Fortran. I think we can fix your example by adding the `target` attribute to `a`. Do you agree?
It's a surprise to me to that the `target` attribute can be used to permit aliasing with other targets via dummy arguments. I thought the sole purpose of the `target` attribute was to enable an entity to be the target of a pointer.
I want to make sure we're on the same page so far before I start working on fixing this case.
https://github.com/llvm/llvm-project/pull/94242
More information about the flang-commits
mailing list