This is an archive of the discontinued LLVM Phabricator instance.

[flang] Use PointerAssociateLowerBounds when there is lower bounds
ClosedPublic

Authored by clementval on Feb 6 2023, 5:44 AM.

Details

Summary

The current code was not taking provided lower bounds when the pointer
is polymorphic and was just calling PointerAssociate. This patch
updates the behavior and use PointerAssociateLowerBounds with the provided
lower bounds.

Diff Detail

Event Timeline

clementval created this revision.Feb 6 2023, 5:44 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptFeb 6 2023, 5:44 AM
clementval requested review of this revision.Feb 6 2023, 5:44 AM

I think PointerAssociateLowerBounds must be called instead here. At first, I though, why not using computing ubounds even if not needed and using PointerAssociateRemapping to re-use createBoundArray like you did, but pointer association with bounds remapping is special because it requires the target to be contiguous (except maybe in the first dimension), which the normal pointer association, with potential new lower bounds, does not. The runtime relies on this contiguity to compute the new strides for the pointer in PointerAssociateRemapping. So `PointerAssociateRemapping cannot be used here.
I expect the following would give bad results:

module test
type t
 integer :: i
end type

contains
subroutine assoc(p, z)
 class(t), pointer :: p(:, :)
 class(t), target :: z(:, :)
 p(2:, 2:) => z
end subroutine
end module

 use test
 type(t) :: z(10, 10)
 class(t), pointer :: p(:, :)
 do j = 0, 9
  do i = 1,10
   z(i, j+1)%i = i + j*10
  end do
 end do
 call assoc(p, z(1:10:5, 1:10:5))
 print *, p(2:,2:)%i
end

This should print "1 6 51 56". The last two numbers will probably something else with this patch.

I think PointerAssociateLowerBounds must be called instead here. At first, I though, why not using computing ubounds even if not needed and using PointerAssociateRemapping to re-use createBoundArray like you did, but pointer association with bounds remapping is special because it requires the target to be contiguous (except maybe in the first dimension), which the normal pointer association, with potential new lower bounds, does not. The runtime relies on this contiguity to compute the new strides for the pointer in PointerAssociateRemapping. So `PointerAssociateRemapping cannot be used here.
I expect the following would give bad results:

module test
type t
 integer :: i
end type

contains
subroutine assoc(p, z)
 class(t), pointer :: p(:, :)
 class(t), target :: z(:, :)
 p(2:, 2:) => z
end subroutine
end module

 use test
 type(t) :: z(10, 10)
 class(t), pointer :: p(:, :)
 do j = 0, 9
  do i = 1,10
   z(i, j+1)%i = i + j*10
  end do
 end do
 call assoc(p, z(1:10:5, 1:10:5))
 print *, p(2:,2:)%i
end

This should print "1 6 51 56". The last two numbers will probably something else with this patch.

Somehow I didn't find PointerAssociateLowerBounds when I looked into the runtime :-). The test seems to pass with this but I'll update the patch to make use of the correct runtime function.

clementval updated this revision to Diff 495124.Feb 6 2023, 7:19 AM

Switch to use PointerAssociateLowerBounds

clementval retitled this revision from [flang] Use PointerAssociateRemapping when there is lower bounds to [flang] Use PointerAssociateLowerBounds when there is lower bounds.Feb 6 2023, 7:20 AM
clementval edited the summary of this revision. (Show Details)
This revision is now accepted and ready to land.Feb 6 2023, 7:47 AM
clementval updated this revision to Diff 495150.Feb 6 2023, 8:35 AM

clang-format