Skip to content
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

[flang] Reset lbounds for allocatable function results. #65286

Merged
merged 1 commit into from
Sep 5, 2023

Conversation

vzakhari
Copy link
Contributor

@vzakhari vzakhari commented Sep 5, 2023

With HLFIR the lbounds for the ALLOCATABLE result are taken from the mutable box created for the result, so the non-default lbounds might be propagated further causing incorrect result, e.g.:

program p
  real, allocatable :: p5(:)
  allocate(p5, source=real_init())
  print *, lbound(p5, 1) ! must print 1, but prints 7
contains
  function real_init()
    real, allocatable :: real_init(:)
    allocate(real_init(7:8))
  end function real_init
end program p

With FIR lowering the box passed for source has explicit lower bound 1 at the call site, but the runtime box initialized by real_init call still has lower bound 7. I am not sure if the runtime box initialized b real_init will ever be accessed in a debugger via Fortran variable names, but I think that having the right runtime bounds that can be accessible via examining registers/stack might be good in general. So I decided to update the runtime bounds at the point of return.

This change "fixes" the test above for HLFIR.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D156187

With HLFIR the lbounds for the ALLOCATABLE result are taken
from the mutable box created for the result, so the non-default
lbounds might be propagated further causing incorrect result, e.g.:
```
program p
  real, allocatable :: p5(:)
  allocate(p5, source=real_init())
  print *, lbound(p5, 1) ! must print 1, but prints 7
contains
  function real_init()
    real, allocatable :: real_init(:)
    allocate(real_init(7:8))
  end function real_init
end program p
```

With FIR lowering the box passed for `source` has explicit lower
bound 1 at the call site, but the runtime box initialized by
`real_init` call still has lower bound 7. I am not sure if
the runtime box initialized b `real_init` will ever be accessed
in a debugger via Fortran variable names, but I think that
having the right runtime bounds that can be accessible via
examining registers/stack might be good in general. So I decided
to update the runtime bounds at the point of return.

This change "fixes" the test above for HLFIR.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D156187
@vzakhari vzakhari requested a review from a team as a code owner September 5, 2023 00:18
@DavidTruby
Copy link
Member

Is there a reason to re-open this review on github? I think existing reviews can just go through phab and github is just for new reviews.

@vzakhari
Copy link
Contributor Author

vzakhari commented Sep 5, 2023

Is there a reason to re-open this review on github? I think existing reviews can just go through phab and github is just for new reviews.

I just want to try the new workflow today, and I do not have any new patches yet.

@DavidTruby
Copy link
Member

Ok no worries I just wasn't sure if you were actually looking for reviews since it's already accepted :)

Copy link
Contributor

@jeanPerier jeanPerier left a comment

Choose a reason for hiding this comment

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

LGTM

@vzakhari vzakhari merged commit de8939f into llvm:main Sep 5, 2023
1 check passed
avillega pushed a commit to avillega/llvm-project that referenced this pull request Sep 11, 2023
With HLFIR the lbounds for the ALLOCATABLE result are taken from the
mutable box created for the result, so the non-default lbounds might be
propagated further causing incorrect result, e.g.:
```
program p
  real, allocatable :: p5(:)
  allocate(p5, source=real_init())
  print *, lbound(p5, 1) ! must print 1, but prints 7
contains
  function real_init()
    real, allocatable :: real_init(:)
    allocate(real_init(7:8))
  end function real_init
end program p
```

With FIR lowering the box passed for `source` has explicit lower bound 1
at the call site, but the runtime box initialized by `real_init` call
still has lower bound 7. I am not sure if the runtime box initialized by
`real_init` will ever be accessed in a debugger via Fortran variable
names, but I think that having the right runtime bounds that can be
accessible via examining registers/stack might be good in general. So I
decided to update the runtime bounds at the point of return.

This change fixes the test above for HLFIR.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D156187
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants