Skip to content

[flang][debug] Avoid redundant debug data generation for derived types. #124473

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 1 commit into from
Jan 27, 2025
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
30 changes: 27 additions & 3 deletions flang/lib/Optimizer/Transforms/DebugTypeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ DebugTypeGenerator::DebugTypeGenerator(mlir::ModuleOp m,
mlir::SymbolTable *symbolTable_,
const mlir::DataLayout &dl)
: module(m), symbolTable(symbolTable_), dataLayout{&dl},
kindMapping(getKindMapping(m)), llvmTypeConverter(m, false, false, dl) {
kindMapping(getKindMapping(m)), llvmTypeConverter(m, false, false, dl),
derivedTypeDepth(0) {
LLVM_DEBUG(llvm::dbgs() << "DITypeAttr generator\n");

mlir::MLIRContext *context = module.getContext();
Expand Down Expand Up @@ -407,7 +408,10 @@ mlir::LLVM::DITypeAttr DebugTypeGenerator::convertRecordType(
/*baseType=*/nullptr, mlir::LLVM::DIFlags::Zero, offset * 8,
/*alignInBits=*/0, elements, /*dataLocation=*/nullptr, /*rank=*/nullptr,
/*allocated=*/nullptr, /*associated=*/nullptr);
if (canCacheThisType) {

// derivedTypeDepth == 1 means that it is a top level type which is safe to
// cache.
if (canCacheThisType || derivedTypeDepth == 1) {
typeCache[Ty] = finalAttr;
} else {
auto iter = typeCache.find(Ty);
Expand Down Expand Up @@ -663,7 +667,27 @@ DebugTypeGenerator::convertType(mlir::Type Ty, mlir::LLVM::DIFileAttr fileAttr,
return convertCharacterType(charTy, fileAttr, scope, declOp,
/*hasDescriptor=*/false);
} else if (auto recTy = mlir::dyn_cast_if_present<fir::RecordType>(Ty)) {
return convertRecordType(recTy, fileAttr, scope, declOp);
// For nested derived types like shown below, the call sequence of the
// convertRecordType will look something like as follows:
// convertRecordType (t1)
// convertRecordType (t2)
// convertRecordType (t3)
// We need to recognize when we are processing the top level type like t1
// to make caching decision. The variable `derivedTypeDepth` is used for
// this purpose and maintains the current depth of derived type processing.
// type t1
// type(t2), pointer :: p1
// end type
// type t2
// type(t3), pointer :: p2
// end type
// type t2
// integer a
// end type
derivedTypeDepth++;
auto result = convertRecordType(recTy, fileAttr, scope, declOp);
derivedTypeDepth--;
return result;
} else if (auto tupleTy = mlir::dyn_cast_if_present<mlir::TupleType>(Ty)) {
return convertTupleType(tupleTy, fileAttr, scope, declOp);
} else if (auto refTy = mlir::dyn_cast_if_present<fir::ReferenceType>(Ty)) {
Expand Down
1 change: 1 addition & 0 deletions flang/lib/Optimizer/Transforms/DebugTypeGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class DebugTypeGenerator {
std::uint64_t lenOffset;
std::uint64_t rankOffset;
std::uint64_t rankSize;
int32_t derivedTypeDepth;
llvm::DenseMap<mlir::Type, mlir::LLVM::DITypeAttr> typeCache;
};

Expand Down
8 changes: 6 additions & 2 deletions flang/test/Integration/debug-cyclic-derived-type-3.f90
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
! RUN: %flang_fc1 -emit-llvm -debug-info-kind=standalone %s -o -
! RUN: %flang_fc1 -emit-llvm -debug-info-kind=standalone %s -o - | FileCheck %s

! mainly test that this program does not cause an assertion failure
! testcase for issue 122024
Expand All @@ -17,7 +17,7 @@ module m1

program test
use m1
type(t1),pointer :: foo
type(t1),pointer :: foo, foo2
allocate(foo)
allocate(foo%x1)
allocate(foo%x1%x2)
Expand All @@ -30,3 +30,7 @@ subroutine sub1(bar)
use m1
type(t2) :: bar
end subroutine

! Test that file compiles ok and there is only one DICompositeType for "t1".
!CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "t1"{{.*}})
!CHECK-NOT: !DICompositeType(tag: DW_TAG_structure_type, name: "t1"{{.*}})
Loading