Skip to content

Commit 7f1adba

Browse files
committed
[flang] Fix LBOUND rewrite on descriptor components
GetLowerBoundHelper rewrite in https://reviews.llvm.org/D121488 was incorrect with POINTER/ALLOCATABLE components. The rewrite created a descriptor inquiry to the component symbol only instead of the whole named entity. The base information was lost, and not retrievable. LBOUND(a(10)%p) became LBOUND(p). Fix this regression, and also update DescriptorInquiry unparsing to carry the kind information. DescriptorInquiries are KIND 8 expressions, while LBOUND/SIZE/RANK, %LEN are default kind expressions. This caused `print *,lbound(x,kind=8)` to unparse as `print*,lbound(x)` which is not semantically the same (this unparsing issue was not an issue for lowering, but I noticed it while writing my regression test). Differential Revision: https://reviews.llvm.org/D122406
1 parent 3929f91 commit 7f1adba

File tree

5 files changed

+30
-16
lines changed

5 files changed

+30
-16
lines changed

flang/lib/Evaluate/formatting.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -732,20 +732,23 @@ llvm::raw_ostream &DescriptorInquiry::AsFortran(llvm::raw_ostream &o) const {
732732
o << "%STRIDE(";
733733
break;
734734
case Field::Rank:
735-
o << "rank(";
735+
o << "int(rank(";
736736
break;
737737
case Field::Len:
738+
o << "int(";
738739
break;
739740
}
740741
base_.AsFortran(o);
741742
if (field_ == Field::Len) {
742-
return o << "%len";
743+
o << "%len";
744+
} else if (field_ == Field::Rank) {
745+
o << ")";
743746
} else {
744-
if (field_ != Field::Rank && dimension_ >= 0) {
747+
if (dimension_ >= 0) {
745748
o << ",dim=" << (dimension_ + 1);
746749
}
747-
return o << ')';
748750
}
751+
return o << ",kind=" << DescriptorInquiry::Result::kind << ")";
749752
}
750753

751754
llvm::raw_ostream &Assignment::AsFortran(llvm::raw_ostream &o) const {

flang/lib/Evaluate/shape.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ class GetLowerBoundHelper
244244
return Result{1};
245245
}
246246

247-
Result operator()(const Symbol &symbol0) const {
247+
Result GetLowerBound(const Symbol &symbol0, NamedEntity &&base) const {
248248
const Symbol &symbol{symbol0.GetUltimate()};
249249
if (const auto *details{
250250
symbol.detailsIf<semantics::ObjectEntityDetails>()}) {
@@ -301,7 +301,7 @@ class GetLowerBoundHelper
301301
}
302302
}
303303
if (IsDescriptor(symbol)) {
304-
return ExtentExpr{DescriptorInquiry{NamedEntity{symbol0},
304+
return ExtentExpr{DescriptorInquiry{std::move(base),
305305
DescriptorInquiry::Field::LowerBound, dimension_}};
306306
}
307307
}
@@ -310,7 +310,7 @@ class GetLowerBoundHelper
310310
if (assoc->rank()) { // SELECT RANK case
311311
const Symbol &resolved{ResolveAssociations(symbol)};
312312
if (IsDescriptor(resolved) && dimension_ < *assoc->rank()) {
313-
return ExtentExpr{DescriptorInquiry{NamedEntity{symbol0},
313+
return ExtentExpr{DescriptorInquiry{std::move(base),
314314
DescriptorInquiry::Field::LowerBound, dimension_}};
315315
}
316316
} else {
@@ -324,9 +324,14 @@ class GetLowerBoundHelper
324324
}
325325
}
326326

327+
Result operator()(const Symbol &symbol0) const {
328+
return GetLowerBound(symbol0, NamedEntity{symbol0});
329+
}
330+
327331
Result operator()(const Component &component) const {
328332
if (component.base().Rank() == 0) {
329-
return (*this)(component.GetLastSymbol());
333+
return GetLowerBound(
334+
component.GetLastSymbol(), NamedEntity{common::Clone(component)});
330335
}
331336
return Result{1};
332337
}

flang/test/Evaluate/rewrite01.f90

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function returns_array_3()
2626
subroutine ubound_test(x, n, m)
2727
integer :: x(n, m)
2828
integer :: y(0:n, 0:m) ! UBOUND could be 0 if n or m are < 0
29-
!CHECK: PRINT *, [INTEGER(4)::int(size(x,dim=1),kind=4),int(size(x,dim=2),kind=4)]
29+
!CHECK: PRINT *, [INTEGER(4)::int(size(x,dim=1,kind=8),kind=4),int(size(x,dim=2,kind=8),kind=4)]
3030
print *, ubound(x)
3131
!CHECK: PRINT *, ubound(returns_array(n,m))
3232
print *, ubound(returns_array(n, m))
@@ -44,7 +44,7 @@ subroutine ubound_test(x, n, m)
4444

4545
subroutine size_test(x, n, m)
4646
integer :: x(n, m)
47-
!CHECK: PRINT *, int(size(x,dim=1)*size(x,dim=2),kind=4)
47+
!CHECK: PRINT *, int(size(x,dim=1,kind=8)*size(x,dim=2,kind=8),kind=4)
4848
print *, size(x)
4949
!CHECK: PRINT *, size(returns_array(n,m))
5050
print *, size(returns_array(n, m))
@@ -58,7 +58,7 @@ subroutine size_test(x, n, m)
5858

5959
subroutine shape_test(x, n, m)
6060
integer :: x(n, m)
61-
!CHECK: PRINT *, [INTEGER(4)::int(size(x,dim=1),kind=4),int(size(x,dim=2),kind=4)]
61+
!CHECK: PRINT *, [INTEGER(4)::int(size(x,dim=1,kind=8),kind=4),int(size(x,dim=2,kind=8),kind=4)]
6262
print *, shape(x)
6363
!CHECK: PRINT *, shape(returns_array(n,m))
6464
print *, shape(returns_array(n, m))
@@ -71,6 +71,10 @@ subroutine shape_test(x, n, m)
7171
subroutine lbound_test(x, n, m)
7272
integer :: x(n, m)
7373
integer :: y(0:n, 0:m) ! LBOUND could be 1 if n or m are < 0
74+
type t
75+
real, pointer :: p(:, :)
76+
end type
77+
type(t) :: a(10)
7478
!CHECK: PRINT *, [INTEGER(4)::1_4,1_4]
7579
print *, lbound(x)
7680
!CHECK: PRINT *, [INTEGER(4)::1_4,1_4]
@@ -85,6 +89,8 @@ subroutine lbound_test(x, n, m)
8589
print *, lbound(y)
8690
!CHECK: PRINT *, lbound(y,1_4)
8791
print *, lbound(y, 1)
92+
!CHECK: PRINT *, lbound(a(1_8)%p,dim=1,kind=8)
93+
print *, lbound(a(1)%p, 1, kind=8)
8894
end subroutine
8995

9096
!CHECK: len_test
@@ -98,8 +104,8 @@ subroutine len_test(a,b, c, d, e, n, m)
98104
integer, intent(in) :: n, m
99105
character(n), intent(in) :: e
100106

101-
!CHECK: PRINT *, int(a%len,kind=4)
102-
print *, len(a)
107+
!CHECK: PRINT *, int(a%len,kind=8)
108+
print *, len(a, kind=8)
103109
!CHECK: PRINT *, 5_4
104110
print *, len(a(1:5))
105111
!CHECK: PRINT *, len(b(a))

flang/test/Semantics/modfile30.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ function f2(x)
1919
!contains
2020
! function f1(x) result(y)
2121
! integer(4)::x(:)
22-
! integer(4)::y(1_8:size(x,dim=1))
22+
! integer(4)::y(1_8:size(x,dim=1,kind=8))
2323
! end
2424
! function f2(x)
2525
! integer(4)::x(:)
26-
! integer(4)::f2(1_8:size(x,dim=1))
26+
! integer(4)::f2(1_8:size(x,dim=1,kind=8))
2727
! end
2828
!end
2929

flang/test/Semantics/modfile33.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ subroutine s1(n, x, y, z, a, b)
572572
! real(4) :: x
573573
! real(4) :: y(1_8:4_8, 1_8:n)
574574
! real(4) :: z(1_8:2_8, 1_8:2_8, 1_8:2_8)
575-
! real(4) :: a(1_8:int(int(4_8*size(y,dim=2),kind=4),kind=8))
575+
! real(4) :: a(1_8:int(int(4_8*size(y,dim=2,kind=8),kind=4),kind=8))
576576
! real(4) :: b(1_8:add(y, z))
577577
! end
578578
!end

0 commit comments

Comments
 (0)