Skip to content

Commit 0432221

Browse files
authored
[flang][debug] Support allocatables. (#95557)
This PR adds debug support for allocatable. The allocatable arrays use the existing functionality to read the array information from descriptor. The allocatable for the scalar shows up as pointer to the scalar. While testing this, I notices that values of allocated and associated flags were swapped. This is also fixed in this PR. Here is how the debugging of the allocatable looks like with this patch in place. integer, allocatable :: ar1(:, :) real, allocatable :: sc allocate(sc) allocate(ar1(3, 4)) (gdb) ptype ar1 type = integer, allocatable (3,4) (gdb) p ar1 $1 = ((5, 6, 7) (9, 10, 11) (13, 14, 15) (17, 18, 19)) (gdb) p sc $2 = (PTR TO -> ( real )) 0x205300 (gdb) p *sc $3 = 3.1400001
1 parent 457e895 commit 0432221

File tree

4 files changed

+87
-2
lines changed

4 files changed

+87
-2
lines changed

flang/lib/Optimizer/Transforms/DebugTypeGenerator.cpp

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ DebugTypeGenerator::DebugTypeGenerator(mlir::ModuleOp m)
6060
// The debug information requires the offset of certain fields in the
6161
// descriptors like lower_bound and extent for each dimension.
6262
mlir::Type llvmDimsType = getDescFieldTypeModel<kDimsPosInBox>()(context);
63+
mlir::Type llvmPtrType = getDescFieldTypeModel<kAddrPosInBox>()(context);
6364
dimsOffset = getComponentOffset<kDimsPosInBox>(*dl, context, llvmDimsType);
6465
dimsSize = dl->getTypeSize(llvmDimsType);
66+
ptrSize = dl->getTypeSize(llvmPtrType);
6567
}
6668

6769
static mlir::LLVM::DITypeAttr genBasicType(mlir::MLIRContext *context,
@@ -104,8 +106,8 @@ mlir::LLVM::DITypeAttr DebugTypeGenerator::convertBoxedSequenceType(
104106
// allocated = associated = (*base_addr != 0)
105107
mlir::LLVM::DIExpressionAttr valid =
106108
mlir::LLVM::DIExpressionAttr::get(context, ops);
107-
mlir::LLVM::DIExpressionAttr associated = genAllocated ? valid : nullptr;
108-
mlir::LLVM::DIExpressionAttr allocated = genAssociated ? valid : nullptr;
109+
mlir::LLVM::DIExpressionAttr allocated = genAllocated ? valid : nullptr;
110+
mlir::LLVM::DIExpressionAttr associated = genAssociated ? valid : nullptr;
109111
ops.clear();
110112

111113
llvm::SmallVector<mlir::LLVM::DINodeAttr> elements;
@@ -217,6 +219,28 @@ mlir::LLVM::DITypeAttr DebugTypeGenerator::convertCharacterType(
217219
/*stringLengthExp=*/nullptr, /*stringLocationExp=*/nullptr, encoding);
218220
}
219221

222+
mlir::LLVM::DITypeAttr DebugTypeGenerator::convertPointerLikeType(
223+
mlir::Type elTy, mlir::LLVM::DIFileAttr fileAttr,
224+
mlir::LLVM::DIScopeAttr scope, mlir::Location loc, bool genAllocated,
225+
bool genAssociated) {
226+
mlir::MLIRContext *context = module.getContext();
227+
228+
// Arrays and character need different treatment because DWARF have special
229+
// constructs for them to get the location from the descriptor. Rest of
230+
// types are handled like pointer to underlying type.
231+
if (auto seqTy = mlir::dyn_cast_or_null<fir::SequenceType>(elTy))
232+
return convertBoxedSequenceType(seqTy, fileAttr, scope, loc, genAllocated,
233+
genAssociated);
234+
235+
mlir::LLVM::DITypeAttr elTyAttr = convertType(elTy, fileAttr, scope, loc);
236+
237+
return mlir::LLVM::DIDerivedTypeAttr::get(
238+
context, llvm::dwarf::DW_TAG_pointer_type,
239+
mlir::StringAttr::get(context, ""), elTyAttr, ptrSize,
240+
/*alignInBits=*/0, /*offset=*/0,
241+
/*optional<address space>=*/std::nullopt, /*extra data=*/nullptr);
242+
}
243+
220244
mlir::LLVM::DITypeAttr
221245
DebugTypeGenerator::convertType(mlir::Type Ty, mlir::LLVM::DIFileAttr fileAttr,
222246
mlir::LLVM::DIScopeAttr scope,
@@ -258,6 +282,10 @@ DebugTypeGenerator::convertType(mlir::Type Ty, mlir::LLVM::DIFileAttr fileAttr,
258282
if (auto seqTy = mlir::dyn_cast_or_null<fir::SequenceType>(elTy))
259283
return convertBoxedSequenceType(seqTy, fileAttr, scope, loc, false,
260284
false);
285+
if (auto heapTy = mlir::dyn_cast_or_null<fir::HeapType>(elTy))
286+
return convertPointerLikeType(heapTy.getElementType(), fileAttr, scope,
287+
loc, /*genAllocated=*/true,
288+
/*genAssociated=*/false);
261289
return genPlaceholderType(context);
262290
} else {
263291
// FIXME: These types are currently unhandled. We are generating a

flang/lib/Optimizer/Transforms/DebugTypeGenerator.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,17 @@ class DebugTypeGenerator {
4949
mlir::LLVM::DIFileAttr fileAttr,
5050
mlir::LLVM::DIScopeAttr scope,
5151
mlir::Location loc);
52+
53+
mlir::LLVM::DITypeAttr
54+
convertPointerLikeType(mlir::Type elTy, mlir::LLVM::DIFileAttr fileAttr,
55+
mlir::LLVM::DIScopeAttr scope, mlir::Location loc,
56+
bool genAllocated, bool genAssociated);
57+
5258
mlir::ModuleOp module;
5359
KindMapping kindMapping;
5460
std::uint64_t dimsSize;
5561
std::uint64_t dimsOffset;
62+
std::uint64_t ptrSize;
5663
};
5764

5865
} // namespace fir
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
! RUN: %flang_fc1 -emit-llvm -debug-info-kind=standalone %s -o - | FileCheck %s
2+
3+
subroutine ff(n, m)
4+
integer n, m, i, j
5+
integer, allocatable :: ar1(:, :)
6+
real, allocatable :: sc
7+
8+
allocate(ar1(n, m))
9+
allocate(sc)
10+
sc = 3.14
11+
12+
print *, sc
13+
print *, ar1
14+
end subroutine ff
15+
16+
17+
! CHECK-DAG: !DILocalVariable(name: "ar1"{{.*}}type: ![[TY1:[0-9]+]])
18+
! CHECK-DAG: ![[TY1]] = !DICompositeType(tag: DW_TAG_array_type{{.*}}elements: ![[ELEMS2:[0-9]+]]{{.*}}dataLocation{{.*}}allocated: !DIExpression(DW_OP_push_object_address, DW_OP_deref, DW_OP_lit0, DW_OP_ne))
19+
! CHECK-DAG: ![[ELEMS2]] = !{![[ELEM1:[0-9]+]], ![[ELEM2:[0-9]+]]}
20+
! CHECK-DAG: ![[ELEM1]] = !DISubrange
21+
! CHECK-DAG: ![[ELEM2]] = !DISubrange
22+
! CHECK-DAG: !DILocalVariable(name: "sc"{{.*}}type: ![[TY2:[0-9]+]])
23+
! CHECK-DAG: ![[TY2]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[TY3:[0-9]+]]{{.*}})
24+
! CHECK-DAG: ![[TY3]] = !DIBasicType(name: "real"{{.*}})
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: fir-opt --add-debug-info --mlir-print-debuginfo %s | FileCheck %s
2+
3+
4+
module attributes {dlti.dl_spec = #dlti.dl_spec<>} {
5+
func.func private @_QFPff() {
6+
%c1 = arith.constant 1 : index
7+
%c0 = arith.constant 0 : index
8+
%0 = fir.undefined !fir.dscope
9+
%1 = fir.alloca !fir.box<!fir.heap<!fir.array<?x?xi32>>> {bindc_name = "ar2", uniq_name = "_QFFffEar2"}
10+
%4 = fircg.ext_declare %1 {uniq_name = "_QFFffEar2"} : (!fir.ref<!fir.box<!fir.heap<!fir.array<?x?xi32>>>>) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?x?xi32>>>> loc(#loc1)
11+
%15 = fir.alloca !fir.box<!fir.heap<f32>> {bindc_name = "sc", uniq_name = "_QFFffEsc"}
12+
%18 = fircg.ext_declare %15 {uniq_name = "_QFFffEsc"} : (!fir.ref<!fir.box<!fir.heap<f32>>>) -> !fir.ref<!fir.box<!fir.heap<f32>>> loc(#loc2)
13+
return
14+
} loc(#loc3)
15+
}
16+
17+
#loc1 = loc("test.f90":3:3)
18+
#loc2 = loc("test.f90":4:3)
19+
#loc3 = loc("test.f90":1:3)
20+
21+
// CHECK-DAG: #[[TY1:.*]] = #llvm.di_basic_type<tag = DW_TAG_base_type, name = "real"{{.*}}>
22+
// CHECK-DAG: #[[TY2:.*]] = #llvm.di_composite_type<tag = DW_TAG_array_type{{.*}}#llvm.di_subrange{{.*}}#llvm.di_subrange{{.*}}allocated = <[DW_OP_push_object_address, DW_OP_deref, DW_OP_lit0, DW_OP_ne]>>
23+
// CHECK-DAG: #[[TY3:.*]] = #llvm.di_derived_type<tag = DW_TAG_pointer_type{{.*}}baseType = #[[TY1]]{{.*}}>
24+
25+
// CHECK-DAG: #llvm.di_local_variable<{{.*}}name = "ar2"{{.*}}type = #[[TY2]]>
26+
// CHECK-DAG: #llvm.di_local_variable<{{.*}}name = "sc"{{.*}}type = #[[TY3]]>

0 commit comments

Comments
 (0)