Skip to content

[flang][debug] Support assumed size arrays. #96316

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

Merged
merged 3 commits into from
Jul 10, 2024
Merged
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
39 changes: 23 additions & 16 deletions flang/lib/Optimizer/Transforms/DebugTypeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,28 +155,35 @@ mlir::LLVM::DITypeAttr DebugTypeGenerator::convertSequenceType(
fir::SequenceType seqTy, mlir::LLVM::DIFileAttr fileAttr,
mlir::LLVM::DIScopeAttr scope, mlir::Location loc) {
mlir::MLIRContext *context = module.getContext();
// FIXME: Only fixed sizes arrays handled at the moment.
if (seqTy.hasDynamicExtents())
return genPlaceholderType(context);

llvm::SmallVector<mlir::LLVM::DINodeAttr> elements;
mlir::LLVM::DITypeAttr elemTy =
convertType(seqTy.getEleTy(), fileAttr, scope, loc);

for (fir::SequenceType::Extent dim : seqTy.getShape()) {
auto intTy = mlir::IntegerType::get(context, 64);
// FIXME: Only supporting lower bound of 1 at the moment. The
// 'SequenceType' has information about the shape but not the shift. In
// cases where the conversion originated during the processing of
// 'DeclareOp', it may be possible to pass on this information. But the
// type conversion should ideally be based on what information present in
// the type class so that it works from everywhere (e.g. when it is part
// of a module or a derived type.)
auto countAttr = mlir::IntegerAttr::get(intTy, llvm::APInt(64, dim));
auto lowerAttr = mlir::IntegerAttr::get(intTy, llvm::APInt(64, 1));
auto subrangeTy = mlir::LLVM::DISubrangeAttr::get(
context, countAttr, lowerAttr, nullptr, nullptr);
elements.push_back(subrangeTy);
if (dim == seqTy.getUnknownExtent()) {
// FIXME: This path is taken for assumed size arrays but also for arrays
// with non constant extent. For the latter case, the DISubrangeAttr
// should point to a variable which will have the extent at runtime.
auto subrangeTy = mlir::LLVM::DISubrangeAttr::get(
context, /*count=*/nullptr, /*lowerBound=*/nullptr,
/*upperBound*/ nullptr, /*stride*/ nullptr);
elements.push_back(subrangeTy);
} else {
auto intTy = mlir::IntegerType::get(context, 64);
// FIXME: Only supporting lower bound of 1 at the moment. The
// 'SequenceType' has information about the shape but not the shift. In
// cases where the conversion originated during the processing of
// 'DeclareOp', it may be possible to pass on this information. But the
// type conversion should ideally be based on what information present in
// the type class so that it works from everywhere (e.g. when it is part
// of a module or a derived type.)
auto countAttr = mlir::IntegerAttr::get(intTy, llvm::APInt(64, dim));
auto lowerAttr = mlir::IntegerAttr::get(intTy, llvm::APInt(64, 1));
auto subrangeTy = mlir::LLVM::DISubrangeAttr::get(
context, countAttr, lowerAttr, nullptr, nullptr);
elements.push_back(subrangeTy);
}
}
// Apart from arrays, the `DICompositeTypeAttr` is used for other things like
// structure types. Many of its fields which are not applicable to arrays
Expand Down
22 changes: 22 additions & 0 deletions flang/test/Integration/debug-assumed-size-array-2.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
! RUN: %flang_fc1 -emit-llvm -debug-info-kind=standalone %s -o - | FileCheck %s

XFAIL: *

! Test that debug info for arrays with non constant extent is different from
! assumed size arrays.

module helper
implicit none
contains
subroutine fn (a1, n)
integer n
integer a1(5, n)
print *, a1(1,1)
end subroutine fn
end module helper

! CHECK-DAG: ![[TY1:[0-9]+]] = !DICompositeType(tag: DW_TAG_array_type{{.*}}elements: ![[ELEMS1:[0-9]+]]{{.*}})
! CHECK-DAG: ![[ELEMS1]] = !{![[ELM1:[0-9]+]], ![[ELM2:[0-9]+]]}
! CHECK-DAG: ![[ELM1]] = !DISubrange(count: 5, lowerBound: 1)
! CHECK-DAG: ![[ELM2]] = !DISubrange(count: [[VAR:[0-9]+]], lowerBound: 1)
! CHECK-DAG: ![[VAR]] = !DILocalVariable
20 changes: 20 additions & 0 deletions flang/test/Integration/debug-assumed-size-array.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
! RUN: %flang_fc1 -emit-llvm -debug-info-kind=standalone %s -o - | FileCheck %s

module helper
implicit none
contains
subroutine fn (a1, a2)
integer a1(5, *), a2(*)
print *, a1(1,1)
print *, a2(2)
end subroutine fn
end module helper

! CHECK-DAG: ![[TY1:[0-9]+]] = !DICompositeType(tag: DW_TAG_array_type{{.*}}elements: ![[ELEMS1:[0-9]+]]{{.*}})
! CHECK-DAG: ![[ELEMS1]] = !{![[ELM1:[0-9]+]], ![[EMPTY:[0-9]+]]}
! CHECK-DAG: ![[ELM1]] = !DISubrange(count: 5, lowerBound: 1)
! CHECK-DAG: ![[EMPTY]] = !DISubrange()
! CHECK-DAG: ![[TY2:[0-9]+]] = !DICompositeType(tag: DW_TAG_array_type{{.*}}elements: ![[ELEMS2:[0-9]+]]{{.*}})
! CHECK-DAG: ![[ELEMS2]] = !{![[EMPTY:[0-9]+]]}
! CHECK-DAG: !DILocalVariable(name: "a1"{{.*}}type: ![[TY1:[0-9]+]])
! CHECK-DAG: !DILocalVariable(name: "a2"{{.*}}type: ![[TY2:[0-9]+]])
21 changes: 21 additions & 0 deletions flang/test/Transforms/debug-assumed-size-array.fir
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: fir-opt --add-debug-info --mlir-print-debuginfo %s | FileCheck %s

module attributes {dlti.dl_spec = #dlti.dl_spec<>} {
func.func @_QMhelperPfn(%arg0: !fir.ref<!fir.array<5x?xi32>> {fir.bindc_name = "a1"}, %arg1: !fir.ref<!fir.array<?xi32>> {fir.bindc_name = "a2"}, %arg2: !fir.ref<!fir.array<2x?xi32>> {fir.bindc_name = "a3"}) {
%c5 = arith.constant 5 : index
%c1 = arith.constant 1 : index
%c-1 = arith.constant -1 : index
%0 = fir.undefined !fir.dscope
%1 = fircg.ext_declare %arg0(%c5, %c-1) dummy_scope %0 {uniq_name = "_QMhelperFfnEa1"} : (!fir.ref<!fir.array<5x?xi32>>, index, index, !fir.dscope) -> !fir.ref<!fir.array<5x?xi32>> loc(#loc1)
%2 = fircg.ext_declare %arg1(%c-1) dummy_scope %0 {uniq_name = "_QMhelperFfnEa2"} : (!fir.ref<!fir.array<?xi32>>, index, !fir.dscope) -> !fir.ref<!fir.array<?xi32>> loc(#loc2)
return
} loc(#loc3)
}
#loc3 = loc("test.f90":1:1)
#loc1 = loc("test.f90":3:1)
#loc2 = loc("test.f90":4:1)

// CHECK-DAG: #[[TY1:.*]] = #llvm.di_composite_type<tag = DW_TAG_array_type{{.*}}elements = #llvm.di_subrange<count = 5 : i64, lowerBound = 1 : i64>, #llvm.di_subrange<>>
// CHECK-DAG: #[[TY2:.*]] = #llvm.di_composite_type<tag = DW_TAG_array_type{{.*}}elements = #llvm.di_subrange<>>
// CHECK-DAG: #llvm.di_local_variable<{{.*}}name = "a1"{{.*}}type = #[[TY1]]>
// CHECK-DAG: #llvm.di_local_variable<{{.*}}name = "a2"{{.*}}type = #[[TY2]]>
Loading