Skip to content

[flang] Retrieve shape from selector when generating assoc sym type #137117

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions flang/lib/Lower/ConvertType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,23 @@ struct TypeBuilderImpl {
bool isPolymorphic = (Fortran::semantics::IsPolymorphic(symbol) ||
Fortran::semantics::IsUnlimitedPolymorphic(symbol)) &&
!Fortran::semantics::IsAssumedType(symbol);
if (const auto *assocDetails =
Copy link
Member Author

Choose a reason for hiding this comment

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

Not very familiar with this part of the codebase, so hopefully this is a sane fix.

Copy link
Contributor

Choose a reason for hiding this comment

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

Did you look into why ultimate.IsObjectArray is returning false for that case?
That seems a bit odd to me.

Copy link
Member Author

@ergawy ergawy Apr 30, 2025

Choose a reason for hiding this comment

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

Thanks for the review and sorry for the late response, was busy doing other stuff.

Did you look into why ultimate.IsObjectArray is returning false for that case?

This is because it calls Symbol::GetShape and Symbol::GetShape returns the shape only in the case of ObjectEntityDetails. For AssocEntityDetails (and other variants), it returns null. See https://github.com/llvm/llvm-project/blob/main/flang/lib/Semantics/symbol.cpp#L401.

Do you think we should extend GetShape to work with AssocEntityDetails as well instead of what I am doing here?

Copy link
Contributor

@jeanPerier jeanPerier May 19, 2025

Choose a reason for hiding this comment

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

Sorry for the very late reply, I missed the mails, do not hesitate to call me back for review when I am late to reply. Thanks for the reply, I can understand why Symbol::GetShape returns nothing given there is no textual shape-spec in the source for selectors.

However, evaluate::GetShape that is called here in lowering is supposed to work directly with AssocEntityDetails (I see some handling for it here).

So to avoid duplicating code, maybe the best is just to simplify the code to do:

auto shapeExpr= Fortran::evaluate::GetShape(....);
if (shapeExpr && !shapeExpr->empty())
  // Statically ranked array.
  fir::SequenceType::Shape shape;
  translateShape(shape, std::move(*shapeExpr));
  ty = ...
} else if (!shapeExpr ) {
  // Assumed-rank.
  ty = fir::SequenceType::get([shape](fir::SequenceType::Shape{}, ty);
}

ultimate.detailsIf<Fortran::semantics::AssocEntityDetails>()) {
const auto &selector = assocDetails->expr();

if (selector && selector->Rank() > 0) {
auto shapeExpr = Fortran::evaluate::GetShape(
converter.getFoldingContext(), selector);

fir::SequenceType::Shape shape;
// If there is no shapExpr, this is an assumed-rank, and the empty shape
// will build the desired fir.array<*:T> type.
if (shapeExpr)
translateShape(shape, std::move(*shapeExpr));
ty = fir::SequenceType::get(shape, ty);
}
}

if (ultimate.IsObjectArray()) {
auto shapeExpr =
Fortran::evaluate::GetShape(converter.getFoldingContext(), ultimate);
Expand Down
22 changes: 22 additions & 0 deletions flang/test/Lower/do_concurrent_local_assoc_entity.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
! RUN: %flang_fc1 -emit-hlfir -o - %s | FileCheck %s

subroutine local_assoc
implicit none
integer i
real, dimension(2:11) :: aa

associate(a => aa(4:))
do concurrent (i = 4:11) local(a)
a(i) = 0
end do
end associate
end subroutine local_assoc

! CHECK: %[[C8:.*]] = arith.constant 8 : index

! CHECK: fir.do_loop {{.*}} unordered {
! CHECK: %[[LOCAL_ALLOC:.*]] = fir.alloca !fir.array<8xf32> {bindc_name = "a", pinned, uniq_name = "{{.*}}local_assocEa"}
! CHECK: %[[LOCAL_SHAPE:.*]] = fir.shape %[[C8]] :
! CHECK: %[[LOCAL_DECL:.*]]:2 = hlfir.declare %[[LOCAL_ALLOC]](%[[LOCAL_SHAPE]])
! CHECK: hlfir.designate %[[LOCAL_DECL]]#0 (%{{.*}})
! CHECK: }