Skip to content

Commit 4f3f09e

Browse files
authored
[flang][debug] Add stride information for assumed shape array. (#106703)
Without this information, debugger could present wrong values for arrays in certain cases as shown in issue #105646. Fixes #105646.
1 parent 29c076b commit 4f3f09e

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

flang/lib/Optimizer/Transforms/DebugTypeGenerator.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ mlir::LLVM::DITypeAttr DebugTypeGenerator::convertBoxedSequenceType(
116116
unsigned offset = dimsOffset;
117117
const unsigned indexSize = dimsSize / 3;
118118
for ([[maybe_unused]] auto _ : seqTy.getShape()) {
119-
// For each dimension, find the offset of count and lower bound in the
120-
// descriptor and generate the dwarf expression to extract it.
119+
// For each dimension, find the offset of count, lower bound and stride in
120+
// the descriptor and generate the dwarf expression to extract it.
121121
// FIXME: If `indexSize` happens to be bigger than address size on the
122122
// system then we may have to change 'DW_OP_deref' here.
123123
addOp(llvm::dwarf::DW_OP_push_object_address, {});
@@ -139,10 +139,18 @@ mlir::LLVM::DITypeAttr DebugTypeGenerator::convertBoxedSequenceType(
139139
mlir::LLVM::DIExpressionAttr::get(context, ops);
140140
ops.clear();
141141

142+
addOp(llvm::dwarf::DW_OP_push_object_address, {});
143+
addOp(llvm::dwarf::DW_OP_plus_uconst,
144+
{offset + (indexSize * kDimStridePos)});
145+
addOp(llvm::dwarf::DW_OP_deref, {});
146+
// stride[i] = *(base_addr + offset + (indexSize * kDimStridePos))
147+
mlir::LLVM::DIExpressionAttr strideAttr =
148+
mlir::LLVM::DIExpressionAttr::get(context, ops);
149+
ops.clear();
150+
142151
offset += dimsSize;
143152
mlir::LLVM::DISubrangeAttr subrangeTy = mlir::LLVM::DISubrangeAttr::get(
144-
context, countAttr, lowerAttr, /*upperBound=*/nullptr,
145-
/*stride=*/nullptr);
153+
context, countAttr, lowerAttr, /*upperBound=*/nullptr, strideAttr);
146154
elements.push_back(subrangeTy);
147155
}
148156
return mlir::LLVM::DICompositeTypeAttr::get(

flang/test/Integration/debug-assumed-shape-array.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ end subroutine ff
88

99
! CHECK-DAG: !DICompositeType(tag: DW_TAG_array_type{{.*}}elements: ![[ELEMS:[0-9]+]], dataLocation: !DIExpression(DW_OP_push_object_address, DW_OP_deref))
1010
! CHECK-DAG: ![[ELEMS]] = !{![[ELEM1:[0-9]+]], ![[ELEM2:[0-9]+]]}
11-
! CHECK-DAG: ![[ELEM1]] = !DISubrange(count: !DIExpression(DW_OP_push_object_address, DW_OP_plus_uconst, 32, DW_OP_deref), lowerBound: !DIExpression(DW_OP_push_object_address, DW_OP_plus_uconst, 24, DW_OP_deref))
12-
! CHECK-DAG: ![[ELEM2]] = !DISubrange(count: !DIExpression(DW_OP_push_object_address, DW_OP_plus_uconst, 56, DW_OP_deref), lowerBound: !DIExpression(DW_OP_push_object_address, DW_OP_plus_uconst, 48, DW_OP_deref))
11+
! CHECK-DAG: ![[ELEM1]] = !DISubrange(count: !DIExpression(DW_OP_push_object_address, DW_OP_plus_uconst, 32, DW_OP_deref), lowerBound: !DIExpression(DW_OP_push_object_address, DW_OP_plus_uconst, 24, DW_OP_deref), stride: !DIExpression(DW_OP_push_object_address, DW_OP_plus_uconst, 40, DW_OP_deref))
12+
! CHECK-DAG: ![[ELEM2]] = !DISubrange(count: !DIExpression(DW_OP_push_object_address, DW_OP_plus_uconst, 56, DW_OP_deref), lowerBound: !DIExpression(DW_OP_push_object_address, DW_OP_plus_uconst, 48, DW_OP_deref), stride: !DIExpression(DW_OP_push_object_address, DW_OP_plus_uconst, 64, DW_OP_deref))
1313

flang/test/Transforms/debug-assumed-shape-array.fir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ module attributes {dlti.dl_spec = #dlti.dl_spec<#dlti.dl_entry<i64, dense<64> :
1111
#loc2 = loc("test1.f90":3:16)
1212

1313
// CHECK: #llvm.di_composite_type<tag = DW_TAG_array_type
14-
// CHECK-SAME: elements = #llvm.di_subrange<count = #llvm.di_expression<[DW_OP_push_object_address, DW_OP_plus_uconst(32), DW_OP_deref]>, lowerBound = #llvm.di_expression<[DW_OP_push_object_address, DW_OP_plus_uconst(24), DW_OP_deref]>>,
15-
// CHECK-SAME: #llvm.di_subrange<count = #llvm.di_expression<[DW_OP_push_object_address, DW_OP_plus_uconst(56), DW_OP_deref]>, lowerBound = #llvm.di_expression<[DW_OP_push_object_address, DW_OP_plus_uconst(48), DW_OP_deref]>>
14+
// CHECK-SAME: elements = #llvm.di_subrange<count = #llvm.di_expression<[DW_OP_push_object_address, DW_OP_plus_uconst(32), DW_OP_deref]>, lowerBound = #llvm.di_expression<[DW_OP_push_object_address, DW_OP_plus_uconst(24), DW_OP_deref]>, stride = #llvm.di_expression<[DW_OP_push_object_address, DW_OP_plus_uconst(40), DW_OP_deref]>>,
15+
// CHECK-SAME: #llvm.di_subrange<count = #llvm.di_expression<[DW_OP_push_object_address, DW_OP_plus_uconst(56), DW_OP_deref]>, lowerBound = #llvm.di_expression<[DW_OP_push_object_address, DW_OP_plus_uconst(48), DW_OP_deref]>, stride = #llvm.di_expression<[DW_OP_push_object_address, DW_OP_plus_uconst(64), DW_OP_deref]>>
1616
// CHECK-SAME: dataLocation = <[DW_OP_push_object_address, DW_OP_deref]>>

0 commit comments

Comments
 (0)