Skip to content

Commit fcf9a90

Browse files
committed
[flang][debug] Support allocatables.
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 35f9e5f commit fcf9a90

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed

flang/lib/Optimizer/Transforms/DebugTypeGenerator.cpp

Lines changed: 29 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;
@@ -190,6 +192,28 @@ mlir::LLVM::DITypeAttr DebugTypeGenerator::convertSequenceType(
190192
/* associated */ nullptr);
191193
}
192194

195+
mlir::LLVM::DITypeAttr DebugTypeGenerator::convertPointerLikeType(
196+
mlir::Type elTy, mlir::LLVM::DIFileAttr fileAttr,
197+
mlir::LLVM::DIScopeAttr scope, mlir::Location loc, bool genAllocated,
198+
bool genAssociated) {
199+
mlir::MLIRContext *context = module.getContext();
200+
201+
// Arrays and character need different treatment because DWARF have special
202+
// constructs for them to get the location from the descriptor. Rest of
203+
// types are handled like pointer to underlying type.
204+
if (auto seqTy = mlir::dyn_cast_or_null<fir::SequenceType>(elTy))
205+
return convertBoxedSequenceType(seqTy, fileAttr, scope, loc, genAllocated,
206+
genAssociated);
207+
208+
mlir::LLVM::DITypeAttr elTyAttr = convertType(elTy, fileAttr, scope, loc);
209+
210+
return mlir::LLVM::DIDerivedTypeAttr::get(
211+
context, llvm::dwarf::DW_TAG_pointer_type,
212+
mlir::StringAttr::get(context, ""), elTyAttr, ptrSize,
213+
/*alignInBits*/ 0, /* offset */ 0,
214+
/* optional<address space> */ std::nullopt, /* extra data */ nullptr);
215+
}
216+
193217
mlir::LLVM::DITypeAttr
194218
DebugTypeGenerator::convertType(mlir::Type Ty, mlir::LLVM::DIFileAttr fileAttr,
195219
mlir::LLVM::DIScopeAttr scope,
@@ -229,6 +253,9 @@ DebugTypeGenerator::convertType(mlir::Type Ty, mlir::LLVM::DIFileAttr fileAttr,
229253
if (auto seqTy = mlir::dyn_cast_or_null<fir::SequenceType>(elTy))
230254
return convertBoxedSequenceType(seqTy, fileAttr, scope, loc, false,
231255
false);
256+
if (auto heapTy = mlir::dyn_cast_or_null<fir::HeapType>(elTy))
257+
return convertPointerLikeType(heapTy.getElementType(), fileAttr, scope,
258+
loc, true, false);
232259
return genPlaceholderType(context);
233260
} else {
234261
// 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
@@ -45,10 +45,17 @@ class DebugTypeGenerator {
4545
mlir::LLVM::DIFileAttr fileAttr,
4646
mlir::LLVM::DIScopeAttr scope, mlir::Location loc,
4747
bool genAllocated, bool genAssociated);
48+
49+
mlir::LLVM::DITypeAttr
50+
convertPointerLikeType(mlir::Type elTy, mlir::LLVM::DIFileAttr fileAttr,
51+
mlir::LLVM::DIScopeAttr scope, mlir::Location loc,
52+
bool genAllocated, bool genAssociated);
53+
4854
mlir::ModuleOp module;
4955
KindMapping kindMapping;
5056
std::uint64_t dimsSize;
5157
std::uint64_t dimsOffset;
58+
std::uint64_t ptrSize;
5259
};
5360

5461
} // 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)