Skip to content

Commit ebd0b1f

Browse files
authored
Merge pull request #11747 from swiftlang/bitint-debug-info-to-21.x
🍒 [lldb][TypeSystem] Better support for _BitInt types This patch ensures we make use of the `DW_AT_bit_size` on `DW_TAG_base_type`s and adjusts `TypeSystemClang` to recognize `_BitInt`. For DWARF from older versions of Clang that didn't emit a `DW_AT_bit_size`, we would create `_BitInt`s using the byte-size. Not sure we can do much better than that. But the situation beforehand wasn't much better. Before: ``` (lldb) v (char) a = '\x01' (unsigned char) b = '\x01' (long) c = 2 (unsigned long) d = 2 ``` After: ``` (lldb) v (_BitInt(2)) a = 1 (unsigned _BitInt(2)) b = 1 (_BitInt(52)) c = 2 (unsigned _BitInt(52)) d = 2 ``` (cherry picked from commit deb54ba)
2 parents 077bc64 + 2823e28 commit ebd0b1f

32 files changed

+620
-109
lines changed

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,14 +1146,16 @@ llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
11461146
}
11471147

11481148
llvm::DIType *CGDebugInfo::CreateType(const BitIntType *Ty) {
1149-
1150-
StringRef Name = Ty->isUnsigned() ? "unsigned _BitInt" : "_BitInt";
1149+
SmallString<32> Name;
1150+
llvm::raw_svector_ostream OS(Name);
1151+
OS << (Ty->isUnsigned() ? "unsigned _BitInt(" : "_BitInt(")
1152+
<< Ty->getNumBits() << ")";
11511153
llvm::dwarf::TypeKind Encoding = Ty->isUnsigned()
11521154
? llvm::dwarf::DW_ATE_unsigned
11531155
: llvm::dwarf::DW_ATE_signed;
1154-
11551156
return DBuilder.createBasicType(Name, CGM.getContext().getTypeSize(Ty),
1156-
Encoding);
1157+
Encoding, llvm::DINode::FlagZero, 0,
1158+
Ty->getNumBits());
11571159
}
11581160

11591161
llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {

clang/test/CodeGen/bit-int.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %clang_cc1 -x c++ %s -debug-info-kind=standalone -gno-column-info -emit-llvm -o - | FileCheck %s
2+
// RUN: %clang_cc1 -x c %s -debug-info-kind=standalone -gno-column-info -emit-llvm -o - | FileCheck %s
3+
4+
unsigned _BitInt(17) a;
5+
_BitInt(2) b;
6+
7+
// CHECK: !DIBasicType(name: "_BitInt(2)", size: 8, dataSize: 2, encoding: DW_ATE_signed)
8+
// CHECK: !DIBasicType(name: "unsigned _BitInt(17)", size: 32, dataSize: 17, encoding: DW_ATE_unsigned)

lldb/include/lldb/Symbol/CompilerType.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ class CompilerType {
433433
std::optional<uint64_t>
434434
GetByteStride(ExecutionContextScope *exe_scope) const;
435435

436-
lldb::Encoding GetEncoding(uint64_t &count) const;
436+
lldb::Encoding GetEncoding() const;
437437

438438
lldb::Format GetFormat() const;
439439

lldb/include/lldb/Symbol/Type.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ class Type : public std::enable_shared_from_this<Type>, public UserID {
507507

508508
lldb::Format GetFormat();
509509

510-
lldb::Encoding GetEncoding(uint64_t &count);
510+
lldb::Encoding GetEncoding();
511511

512512
SymbolContextScope *GetSymbolContextScope() { return m_context; }
513513
const SymbolContextScope *GetSymbolContextScope() const { return m_context; }

lldb/include/lldb/Symbol/TypeSystem.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,7 @@ class TypeSystem : public PluginInterface,
349349
GetByteStride(lldb::opaque_compiler_type_t type,
350350
ExecutionContextScope *exe_scope) = 0;
351351

352-
virtual lldb::Encoding GetEncoding(lldb::opaque_compiler_type_t type,
353-
uint64_t &count) = 0;
352+
virtual lldb::Encoding GetEncoding(lldb::opaque_compiler_type_t type) = 0;
354353

355354
virtual lldb::Format GetFormat(lldb::opaque_compiler_type_t type) = 0;
356355

lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,10 @@ ParsedDWARFTypeAttributes::ParsedDWARFTypeAttributes(const DWARFDIE &die) {
465465
byte_size = form_value.Unsigned();
466466
break;
467467

468+
case DW_AT_bit_size:
469+
data_bit_size = form_value.Unsigned();
470+
break;
471+
468472
case DW_AT_alignment:
469473
alignment = form_value.Unsigned();
470474
break;
@@ -839,13 +843,18 @@ DWARFASTParserClang::ParseTypeModifier(const SymbolContext &sc,
839843
// there...
840844
[[fallthrough]];
841845

842-
case DW_TAG_base_type:
846+
case DW_TAG_base_type: {
843847
resolve_state = Type::ResolveState::Full;
848+
// If a builtin type's size isn't a multiple of a byte, DWARF producers may
849+
// add a precise bit-size to the type. Use the most precise bit-size
850+
// possible.
851+
const uint64_t bit_size = attrs.data_bit_size
852+
? *attrs.data_bit_size
853+
: attrs.byte_size.value_or(0) * 8;
844854
clang_type = m_ast.GetBuiltinTypeForDWARFEncodingAndBitSize(
845-
attrs.name.GetStringRef(), attrs.encoding,
846-
attrs.byte_size.value_or(0) * 8);
855+
attrs.name.GetStringRef(), attrs.encoding, bit_size);
847856
break;
848-
857+
}
849858
case DW_TAG_pointer_type:
850859
encoding_data_type = Type::eEncodingIsPointerUID;
851860
break;

lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@ struct ParsedDWARFTypeAttributes {
592592
lldb_private::plugin::dwarf::DWARFFormValue type;
593593
lldb::LanguageType class_language = lldb::eLanguageTypeUnknown;
594594
std::optional<uint64_t> byte_size;
595+
std::optional<uint64_t> data_bit_size;
595596
std::optional<uint64_t> alignment;
596597
size_t calling_convention = llvm::dwarf::DW_CC_normal;
597598
uint32_t bit_stride = 0;

lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,8 @@ CompilerType TypeSystemClang::GetBuiltinTypeForDWARFEncodingAndBitSize(
987987

988988
case DW_ATE_signed:
989989
if (!type_name.empty()) {
990+
if (type_name.starts_with("_BitInt"))
991+
return GetType(ast.getBitIntType(/*Unsigned=*/false, bit_size));
990992
if (type_name == "wchar_t" &&
991993
QualTypeMatchesBitSize(bit_size, ast, ast.WCharTy) &&
992994
(getTargetInfo() &&
@@ -1043,6 +1045,8 @@ CompilerType TypeSystemClang::GetBuiltinTypeForDWARFEncodingAndBitSize(
10431045

10441046
case DW_ATE_unsigned:
10451047
if (!type_name.empty()) {
1048+
if (type_name.starts_with("unsigned _BitInt"))
1049+
return GetType(ast.getBitIntType(/*Unsigned=*/true, bit_size));
10461050
if (type_name == "wchar_t") {
10471051
if (QualTypeMatchesBitSize(bit_size, ast, ast.WCharTy)) {
10481052
if (!(getTargetInfo() &&
@@ -3925,6 +3929,13 @@ TypeSystemClang::GetTypeInfo(lldb::opaque_compiler_type_t type,
39253929
->getModifiedType()
39263930
.getAsOpaquePtr(),
39273931
pointee_or_element_clang_type);
3932+
case clang::Type::BitInt: {
3933+
uint32_t type_flags = eTypeIsScalar | eTypeIsInteger | eTypeHasValue;
3934+
if (qual_type->isSignedIntegerType())
3935+
type_flags |= eTypeIsSigned;
3936+
3937+
return type_flags;
3938+
}
39283939
case clang::Type::Builtin: {
39293940
const clang::BuiltinType *builtin_type =
39303941
llvm::cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
@@ -4955,12 +4966,10 @@ TypeSystemClang::GetTypeBitAlign(lldb::opaque_compiler_type_t type,
49554966
return {};
49564967
}
49574968

4958-
lldb::Encoding TypeSystemClang::GetEncoding(lldb::opaque_compiler_type_t type,
4959-
uint64_t &count) {
4969+
lldb::Encoding TypeSystemClang::GetEncoding(lldb::opaque_compiler_type_t type) {
49604970
if (!type)
49614971
return lldb::eEncodingInvalid;
49624972

4963-
count = 1;
49644973
clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
49654974

49664975
switch (qual_type->getTypeClass()) {
@@ -4996,7 +5005,6 @@ lldb::Encoding TypeSystemClang::GetEncoding(lldb::opaque_compiler_type_t type,
49965005
case clang::Type::DependentVector:
49975006
case clang::Type::ExtVector:
49985007
case clang::Type::Vector:
4999-
// TODO: Set this to more than one???
50005008
break;
50015009

50025010
case clang::Type::BitInt:
@@ -5196,11 +5204,10 @@ lldb::Encoding TypeSystemClang::GetEncoding(lldb::opaque_compiler_type_t type,
51965204
const clang::ComplexType *complex_type =
51975205
qual_type->getAsComplexIntegerType();
51985206
if (complex_type)
5199-
encoding = GetType(complex_type->getElementType()).GetEncoding(count);
5207+
encoding = GetType(complex_type->getElementType()).GetEncoding();
52005208
else
52015209
encoding = lldb::eEncodingSint;
52025210
}
5203-
count = 2;
52045211
return encoding;
52055212
}
52065213

@@ -5256,7 +5263,7 @@ lldb::Encoding TypeSystemClang::GetEncoding(lldb::opaque_compiler_type_t type,
52565263
case clang::Type::HLSLInlineSpirv:
52575264
break;
52585265
}
5259-
count = 0;
5266+
52605267
return lldb::eEncodingInvalid;
52615268
}
52625269

lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -907,8 +907,7 @@ class TypeSystemClang : public TypeSystem {
907907
return {};
908908
}
909909

910-
lldb::Encoding GetEncoding(lldb::opaque_compiler_type_t type,
911-
uint64_t &count) override;
910+
lldb::Encoding GetEncoding(lldb::opaque_compiler_type_t type) override;
912911

913912
lldb::Format GetFormat(lldb::opaque_compiler_type_t type) override;
914913

lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6775,11 +6775,9 @@ SwiftASTContext::GetTypeBitAlign(opaque_compiler_type_t type,
67756775
return {};
67766776
}
67776777

6778-
lldb::Encoding SwiftASTContext::GetEncoding(opaque_compiler_type_t type,
6779-
uint64_t &count) {
6778+
lldb::Encoding SwiftASTContext::GetEncoding(opaque_compiler_type_t type) {
67806779
VALID_OR_RETURN_CHECK_TYPE(type, lldb::eEncodingInvalid);
67816780

6782-
count = 1;
67836781
swift::CanType swift_can_type(GetCanonicalSwiftType(type));
67846782

67856783
const swift::TypeKind type_kind = swift_can_type->getKind();
@@ -6842,7 +6840,7 @@ lldb::Encoding SwiftASTContext::GetEncoding(opaque_compiler_type_t type,
68426840
case swift::TypeKind::UnownedStorage:
68436841
case swift::TypeKind::WeakStorage:
68446842
return ToCompilerType(swift_can_type->getReferenceStorageReferent())
6845-
.GetEncoding(count);
6843+
.GetEncoding();
68466844
break;
68476845

68486846
case swift::TypeKind::ExistentialMetatype:
@@ -6877,7 +6875,6 @@ lldb::Encoding SwiftASTContext::GetEncoding(opaque_compiler_type_t type,
68776875
assert(false && "Not a canonical type");
68786876
break;
68796877
}
6880-
count = 0;
68816878
return lldb::eEncodingInvalid;
68826879
}
68836880

0 commit comments

Comments
 (0)