Skip to content

[flang][debug] Handle allocatable strings. #95906

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 2 commits into from
Jun 24, 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
42 changes: 34 additions & 8 deletions flang/lib/Optimizer/Transforms/DebugTypeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ DebugTypeGenerator::DebugTypeGenerator(mlir::ModuleOp m)
// descriptors like lower_bound and extent for each dimension.
mlir::Type llvmDimsType = getDescFieldTypeModel<kDimsPosInBox>()(context);
mlir::Type llvmPtrType = getDescFieldTypeModel<kAddrPosInBox>()(context);
mlir::Type llvmLenType = getDescFieldTypeModel<kElemLenPosInBox>()(context);
dimsOffset = getComponentOffset<kDimsPosInBox>(*dl, context, llvmDimsType);
dimsSize = dl->getTypeSize(llvmDimsType);
ptrSize = dl->getTypeSize(llvmPtrType);
lenOffset = getComponentOffset<kElemLenPosInBox>(*dl, context, llvmLenType);
}

static mlir::LLVM::DITypeAttr genBasicType(mlir::MLIRContext *context,
Expand Down Expand Up @@ -192,10 +194,8 @@ mlir::LLVM::DITypeAttr DebugTypeGenerator::convertSequenceType(

mlir::LLVM::DITypeAttr DebugTypeGenerator::convertCharacterType(
fir::CharacterType charTy, mlir::LLVM::DIFileAttr fileAttr,
mlir::LLVM::DIScopeAttr scope, mlir::Location loc) {
mlir::LLVM::DIScopeAttr scope, mlir::Location loc, bool hasDescriptor) {
mlir::MLIRContext *context = module.getContext();
if (!charTy.hasConstantLen())
return genPlaceholderType(context);

// DWARF 5 says the following about the character encoding in 5.1.1.2.
// "DW_ATE_ASCII and DW_ATE_UCS specify encodings for the Fortran 2003
Expand All @@ -205,16 +205,38 @@ mlir::LLVM::DITypeAttr DebugTypeGenerator::convertCharacterType(
if (charTy.getFKind() != 1)
encoding = llvm::dwarf::DW_ATE_UCS;

uint64_t sizeInBits = 0;
mlir::LLVM::DIExpressionAttr lenExpr = nullptr;
mlir::LLVM::DIExpressionAttr locExpr = nullptr;

if (hasDescriptor) {
llvm::SmallVector<mlir::LLVM::DIExpressionElemAttr> ops;
auto addOp = [&](unsigned opc, llvm::ArrayRef<uint64_t> vals) {
ops.push_back(mlir::LLVM::DIExpressionElemAttr::get(context, opc, vals));
};
addOp(llvm::dwarf::DW_OP_push_object_address, {});
addOp(llvm::dwarf::DW_OP_plus_uconst, {lenOffset});
lenExpr = mlir::LLVM::DIExpressionAttr::get(context, ops);
ops.clear();

addOp(llvm::dwarf::DW_OP_push_object_address, {});
addOp(llvm::dwarf::DW_OP_deref, {});
locExpr = mlir::LLVM::DIExpressionAttr::get(context, ops);
} else if (charTy.hasConstantLen()) {
sizeInBits =
charTy.getLen() * kindMapping.getCharacterBitsize(charTy.getFKind());
} else {
return genPlaceholderType(context);
}

// FIXME: Currently the DIStringType in llvm does not have the option to set
// type of the underlying character. This restricts out ability to represent
// string with non-default characters. Please see issue #95440 for more
// details.
return mlir::LLVM::DIStringTypeAttr::get(
context, llvm::dwarf::DW_TAG_string_type,
mlir::StringAttr::get(context, ""),
charTy.getLen() * kindMapping.getCharacterBitsize(charTy.getFKind()),
/*alignInBits=*/0, /*stringLength=*/nullptr,
/*stringLengthExp=*/nullptr, /*stringLocationExp=*/nullptr, encoding);
mlir::StringAttr::get(context, ""), sizeInBits, /*alignInBits=*/0,
/*stringLength=*/nullptr, lenExpr, locExpr, encoding);
}

mlir::LLVM::DITypeAttr DebugTypeGenerator::convertPointerLikeType(
Expand All @@ -229,6 +251,9 @@ mlir::LLVM::DITypeAttr DebugTypeGenerator::convertPointerLikeType(
if (auto seqTy = mlir::dyn_cast_or_null<fir::SequenceType>(elTy))
return convertBoxedSequenceType(seqTy, fileAttr, scope, loc, genAllocated,
genAssociated);
if (auto charTy = mlir::dyn_cast_or_null<fir::CharacterType>(elTy))
return convertCharacterType(charTy, fileAttr, scope, loc,
/*hasDescriptor=*/true);

mlir::LLVM::DITypeAttr elTyAttr = convertType(elTy, fileAttr, scope, loc);

Expand Down Expand Up @@ -274,7 +299,8 @@ DebugTypeGenerator::convertType(mlir::Type Ty, mlir::LLVM::DIFileAttr fileAttr,
} else if (auto seqTy = mlir::dyn_cast_or_null<fir::SequenceType>(Ty)) {
return convertSequenceType(seqTy, fileAttr, scope, loc);
} else if (auto charTy = mlir::dyn_cast_or_null<fir::CharacterType>(Ty)) {
return convertCharacterType(charTy, fileAttr, scope, loc);
return convertCharacterType(charTy, fileAttr, scope, loc,
/*hasDescriptor=*/false);
} else if (auto boxTy = mlir::dyn_cast_or_null<fir::BoxType>(Ty)) {
auto elTy = boxTy.getElementType();
if (auto seqTy = mlir::dyn_cast_or_null<fir::SequenceType>(elTy))
Expand Down
4 changes: 3 additions & 1 deletion flang/lib/Optimizer/Transforms/DebugTypeGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class DebugTypeGenerator {
mlir::LLVM::DITypeAttr convertCharacterType(fir::CharacterType charTy,
mlir::LLVM::DIFileAttr fileAttr,
mlir::LLVM::DIScopeAttr scope,
mlir::Location loc);
mlir::Location loc,
bool hasDescriptor);

mlir::LLVM::DITypeAttr
convertPointerLikeType(mlir::Type elTy, mlir::LLVM::DIFileAttr fileAttr,
Expand All @@ -60,6 +61,7 @@ class DebugTypeGenerator {
std::uint64_t dimsSize;
std::uint64_t dimsOffset;
std::uint64_t ptrSize;
std::uint64_t lenOffset;
};

} // namespace fir
Expand Down
6 changes: 5 additions & 1 deletion flang/test/Integration/debug-char-type-1.f90
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module helper
character(len=40) :: str
character(len=:), allocatable :: str2
end module helper

program test
Expand All @@ -11,11 +12,14 @@ program test
first = '3.14 = π'
second = 'Fortran'
str = 'Hello World!'
str2 = 'A quick brown fox jumps over a lazy dog'
end program test

! CHECK-DAG: !DIGlobalVariable(name: "str"{{.*}}type: ![[TY40:[0-9]+]]{{.*}})
! CHECK-DAG: ![[TY40]] = !DIStringType(size: 320, encoding: DW_ATE_ASCII)
! CHECK-DAG: !DIGlobalVariable(name: "str2"{{.*}}type: ![[TY:[0-9]+]]{{.*}})
! CHECK-DAG: ![[TY]] = !DIStringType(stringLengthExpression: !DIExpression(DW_OP_push_object_address, DW_OP_plus_uconst, 8), stringLocationExpression: !DIExpression(DW_OP_push_object_address, DW_OP_deref), encoding: DW_ATE_ASCII)
! CHECK-DAG: !DILocalVariable(name: "first"{{.*}}type: ![[TY8:[0-9]+]])
! CHECK-DAG: ![[TY8]] = !DIStringType(size: 256, encoding: DW_ATE_UCS)
! CHECK-DAG: !DILocalVariable(name: "second"{{.*}}type: ![[TY10:[0-9]+]])
! CHECK-DAG: ![[TY10]] = !DIStringType(size: 80, encoding: DW_ATE_ASCII)
! CHECK-DAG: ![[TY10]] = !DIStringType(size: 80, encoding: DW_ATE_ASCII)
9 changes: 8 additions & 1 deletion flang/test/Transforms/debug-char-type-1.fir
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@ module attributes {dlti.dl_spec = #dlti.dl_spec<>} {
%0 = fir.zero_bits !fir.char<4,20>
fir.has_value %0 : !fir.char<4,20>
} loc(#loc1)
fir.global @_QMhelperEstr3 : !fir.box<!fir.heap<!fir.char<1,?>>> {
%c0 = arith.constant 0 : index
%0 = fir.zero_bits !fir.heap<!fir.char<1,?>>
%1 = fir.embox %0 typeparams %c0 : (!fir.heap<!fir.char<1,?>>, index) -> !fir.box<!fir.heap<!fir.char<1,?>>>
fir.has_value %1 : !fir.box<!fir.heap<!fir.char<1,?>>>
} loc(#loc1)
}
#loc1 = loc("string.f90":1:1)

// CHECK-DAG: #[[TY1:.*]] = #llvm.di_string_type<tag = DW_TAG_string_type, name = "", sizeInBits = 320, encoding = DW_ATE_ASCII>
// CHECK-DAG: #llvm.di_global_variable<{{.*}}name = "str1"{{.*}}type = #[[TY1]]{{.*}}>
// CHECK-DAG: #[[TY2:.*]] = #llvm.di_string_type<tag = DW_TAG_string_type, name = "", sizeInBits = 640, encoding = DW_ATE_UCS>
// CHECK-DAG: #llvm.di_global_variable<{{.*}}name = "str2"{{.*}}type = #[[TY2]]{{.*}}>

// CHECK-DAG: #[[TY3:.*]] = #llvm.di_string_type<tag = DW_TAG_string_type{{.*}}stringLengthExp = <[DW_OP_push_object_address, DW_OP_plus_uconst(8)]>, stringLocationExp = <[DW_OP_push_object_address, DW_OP_deref]>, encoding = DW_ATE_ASCII>
// CHECK-DAG: #llvm.di_global_variable<{{.*}}name = "str3"{{.*}}type = #[[TY3]]{{.*}}>
Loading