Skip to content
This repository has been archived by the owner on Jan 1, 2023. It is now read-only.

Commit

Permalink
[DebugInfoMetadata] Added DIFlags interface in DIBasicType.
Browse files Browse the repository at this point in the history
Flags in DIBasicType will be used to pass attributes used in
DW_TAG_base_type, such as DW_AT_endianity.

Patch by Chirag Patel!

Differential Revision: https://reviews.llvm.org/D49610

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@339714 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
adrian-prantl committed Aug 14, 2018
1 parent a4dbf62 commit 3224b6c
Show file tree
Hide file tree
Showing 16 changed files with 94 additions and 44 deletions.
13 changes: 12 additions & 1 deletion include/llvm/BinaryFormat/Dwarf.def
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
defined HANDLE_DW_LNCT || defined HANDLE_DW_MACRO || \
defined HANDLE_DW_RLE || defined HANDLE_DW_CFA || \
defined HANDLE_DW_APPLE_PROPERTY || defined HANDLE_DW_UT || \
defined HANDLE_DWARF_SECTION || defined HANDLE_DW_IDX)
defined HANDLE_DWARF_SECTION || defined HANDLE_DW_IDX || \
defined HANDLE_DW_END)
#error "Missing macro definition of HANDLE_DW*"
#endif

Expand Down Expand Up @@ -100,6 +101,10 @@
#define HANDLE_DW_IDX(ID, NAME)
#endif

#ifndef HANDLE_DW_END
#define HANDLE_DW_END(ID, NAME)
#endif

HANDLE_DW_TAG(0x0000, null, 2, DWARF)
HANDLE_DW_TAG(0x0001, array_type, 2, DWARF)
HANDLE_DW_TAG(0x0002, class_type, 2, DWARF)
Expand Down Expand Up @@ -690,6 +695,11 @@ HANDLE_DW_ATE(0x10, UTF, 4, DWARF)
HANDLE_DW_ATE(0x11, UCS, 5, DWARF)
HANDLE_DW_ATE(0x12, ASCII, 5, DWARF)

// DWARF attribute endianity
HANDLE_DW_END(0x00, default)
HANDLE_DW_END(0x01, big)
HANDLE_DW_END(0x02, little)

// DWARF virtuality codes.
HANDLE_DW_VIRTUALITY(0x00, none)
HANDLE_DW_VIRTUALITY(0x01, virtual)
Expand Down Expand Up @@ -909,3 +919,4 @@ HANDLE_DW_IDX(0x05, type_hash)
#undef HANDLE_DW_UT
#undef HANDLE_DWARF_SECTION
#undef HANDLE_DW_IDX
#undef HANDLE_DW_END
5 changes: 2 additions & 3 deletions include/llvm/BinaryFormat/Dwarf.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,8 @@ enum DecimalSignEncoding {

enum EndianityEncoding {
// Endianity attribute values
DW_END_default = 0x00,
DW_END_big = 0x01,
DW_END_little = 0x02,
#define HANDLE_DW_END(ID, NAME) DW_END_##NAME = ID,
#include "llvm/BinaryFormat/Dwarf.def"
DW_END_lo_user = 0x40,
DW_END_hi_user = 0xff
};
Expand Down
6 changes: 4 additions & 2 deletions include/llvm/IR/DIBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,11 @@ namespace llvm {
/// type.
/// \param Name Type name.
/// \param SizeInBits Size of the type.
/// \param Encoding DWARF encoding code, e.g. dwarf::DW_ATE_float.
/// \param Encoding DWARF encoding code, e.g., dwarf::DW_ATE_float.
/// \param Flags Optional DWARF attributes, e.g., DW_AT_endianity.
DIBasicType *createBasicType(StringRef Name, uint64_t SizeInBits,
unsigned Encoding);
unsigned Encoding,
DINode::DIFlags Flags = DINode::FlagZero);

/// Create debugging information entry for a qualified
/// type, e.g. 'const int'.
Expand Down
4 changes: 3 additions & 1 deletion include/llvm/IR/DebugInfoFlags.def
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ HANDLE_DI_FLAG((1 << 23), TypePassByReference)
HANDLE_DI_FLAG((1 << 24), FixedEnum)
HANDLE_DI_FLAG((1 << 25), Thunk)
HANDLE_DI_FLAG((1 << 26), Trivial)
HANDLE_DI_FLAG((1 << 27), BigEndian)
HANDLE_DI_FLAG((1 << 28), LittleEndian)

// To avoid needing a dedicated value for IndirectVirtualBase, we use
// the bitwise or of Virtual and FwdDecl, which does not otherwise
Expand All @@ -57,7 +59,7 @@ HANDLE_DI_FLAG((1 << 2) | (1 << 5), IndirectVirtualBase)
#ifdef DI_FLAG_LARGEST_NEEDED
// intended to be used with ADT/BitmaskEnum.h
// NOTE: always must be equal to largest flag, check this when adding new flag
HANDLE_DI_FLAG((1 << 26), Largest)
HANDLE_DI_FLAG((1 << 28), Largest)
#undef DI_FLAG_LARGEST_NEEDED
#endif

Expand Down
27 changes: 16 additions & 11 deletions include/llvm/IR/DebugInfoMetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,8 @@ class DIType : public DIScope {
bool isTypePassByReference() const {
return getFlags() & FlagTypePassByReference;
}
bool isBigEndian() const { return getFlags() & FlagBigEndian; }
bool isLittleEndian() const { return getFlags() & FlagLittleEndian; }

static bool classof(const Metadata *MD) {
switch (MD->getMetadataID()) {
Expand All @@ -739,40 +741,43 @@ class DIBasicType : public DIType {

DIBasicType(LLVMContext &C, StorageType Storage, unsigned Tag,
uint64_t SizeInBits, uint32_t AlignInBits, unsigned Encoding,
ArrayRef<Metadata *> Ops)
DIFlags Flags, ArrayRef<Metadata *> Ops)
: DIType(C, DIBasicTypeKind, Storage, Tag, 0, SizeInBits, AlignInBits, 0,
FlagZero, Ops),
Flags, Ops),
Encoding(Encoding) {}
~DIBasicType() = default;

static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
StringRef Name, uint64_t SizeInBits,
uint32_t AlignInBits, unsigned Encoding,
StorageType Storage, bool ShouldCreate = true) {
DIFlags Flags, StorageType Storage,
bool ShouldCreate = true) {
return getImpl(Context, Tag, getCanonicalMDString(Context, Name),
SizeInBits, AlignInBits, Encoding, Storage, ShouldCreate);
SizeInBits, AlignInBits, Encoding, Flags, Storage,
ShouldCreate);
}
static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
MDString *Name, uint64_t SizeInBits,
uint32_t AlignInBits, unsigned Encoding,
StorageType Storage, bool ShouldCreate = true);
DIFlags Flags, StorageType Storage,
bool ShouldCreate = true);

TempDIBasicType cloneImpl() const {
return getTemporary(getContext(), getTag(), getName(), getSizeInBits(),
getAlignInBits(), getEncoding());
getAlignInBits(), getEncoding(), getFlags());
}

public:
DEFINE_MDNODE_GET(DIBasicType, (unsigned Tag, StringRef Name),
(Tag, Name, 0, 0, 0))
(Tag, Name, 0, 0, 0, FlagZero))
DEFINE_MDNODE_GET(DIBasicType,
(unsigned Tag, StringRef Name, uint64_t SizeInBits,
uint32_t AlignInBits, unsigned Encoding),
(Tag, Name, SizeInBits, AlignInBits, Encoding))
uint32_t AlignInBits, unsigned Encoding, DIFlags Flags),
(Tag, Name, SizeInBits, AlignInBits, Encoding, Flags))
DEFINE_MDNODE_GET(DIBasicType,
(unsigned Tag, MDString *Name, uint64_t SizeInBits,
uint32_t AlignInBits, unsigned Encoding),
(Tag, Name, SizeInBits, AlignInBits, Encoding))
uint32_t AlignInBits, unsigned Encoding, DIFlags Flags),
(Tag, Name, SizeInBits, AlignInBits, Encoding, Flags))

TempDIBasicType clone() const { return cloneImpl(); }

Expand Down
8 changes: 5 additions & 3 deletions lib/AsmParser/LLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4281,19 +4281,21 @@ bool LLParser::ParseDIEnumerator(MDNode *&Result, bool IsDistinct) {
}

/// ParseDIBasicType:
/// ::= !DIBasicType(tag: DW_TAG_base_type, name: "int", size: 32, align: 32)
/// ::= !DIBasicType(tag: DW_TAG_base_type, name: "int", size: 32, align: 32,
/// encoding: DW_ATE_encoding, flags: 0)
bool LLParser::ParseDIBasicType(MDNode *&Result, bool IsDistinct) {
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_base_type)); \
OPTIONAL(name, MDStringField, ); \
OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX)); \
OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
OPTIONAL(encoding, DwarfAttEncodingField, );
OPTIONAL(encoding, DwarfAttEncodingField, ); \
OPTIONAL(flags, DIFlagField, );
PARSE_MD_FIELDS();
#undef VISIT_MD_FIELDS

Result = GET_OR_DISTINCT(DIBasicType, (Context, tag.Val, name.Val, size.Val,
align.Val, encoding.Val));
align.Val, encoding.Val, flags.Val));
return false;
}

Expand Down
7 changes: 5 additions & 2 deletions lib/Bitcode/Reader/MetadataLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1211,14 +1211,17 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
break;
}
case bitc::METADATA_BASIC_TYPE: {
if (Record.size() != 6)
if (Record.size() < 6 || Record.size() > 7)
return error("Invalid record");

IsDistinct = Record[0];
DINode::DIFlags Flags = (Record.size() > 6) ?
static_cast<DINode::DIFlags>(Record[6]) : DINode::FlagZero;

MetadataList.assignValue(
GET_OR_DISTINCT(DIBasicType,
(Context, Record[1], getMDString(Record[2]), Record[3],
Record[4], Record[5])),
Record[4], Record[5], Flags)),
NextMetadataNo);
NextMetadataNo++;
break;
Expand Down
1 change: 1 addition & 0 deletions lib/Bitcode/Writer/BitcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,7 @@ void ModuleBitcodeWriter::writeDIBasicType(const DIBasicType *N,
Record.push_back(N->getSizeInBits());
Record.push_back(N->getAlignInBits());
Record.push_back(N->getEncoding());
Record.push_back(N->getFlags());

Stream.EmitRecord(bitc::METADATA_BASIC_TYPE, Record, Abbrev);
Record.clear();
Expand Down
5 changes: 5 additions & 0 deletions lib/CodeGen/AsmPrinter/DwarfUnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,11 @@ void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIBasicType *BTy) {

uint64_t Size = BTy->getSizeInBits() >> 3;
addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);

if (BTy->isBigEndian())
addUInt(Buffer, dwarf::DW_AT_endianity, None, dwarf::DW_END_big);
else if (BTy->isLittleEndian())
addUInt(Buffer, dwarf::DW_AT_endianity, None, dwarf::DW_END_little);
}

void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy) {
Expand Down
1 change: 1 addition & 0 deletions lib/IR/AsmWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1787,6 +1787,7 @@ static void writeDIBasicType(raw_ostream &Out, const DIBasicType *N,
Printer.printInt("align", N->getAlignInBits());
Printer.printDwarfEnum("encoding", N->getEncoding(),
dwarf::AttributeEncodingString);
Printer.printDIFlags("flags", N->getFlags());
Out << ")";
}

Expand Down
5 changes: 3 additions & 2 deletions lib/IR/DIBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,11 @@ DIBasicType *DIBuilder::createNullPtrType() {
}

DIBasicType *DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
unsigned Encoding) {
unsigned Encoding,
DINode::DIFlags Flags) {
assert(!Name.empty() && "Unable to create type without name");
return DIBasicType::get(VMContext, dwarf::DW_TAG_base_type, Name, SizeInBits,
0, Encoding);
0, Encoding, Flags);
}

DIDerivedType *DIBuilder::createQualifiedType(unsigned Tag, DIType *FromTy) {
Expand Down
9 changes: 5 additions & 4 deletions lib/IR/DebugInfoMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,14 @@ DIEnumerator *DIEnumerator::getImpl(LLVMContext &Context, int64_t Value,
DIBasicType *DIBasicType::getImpl(LLVMContext &Context, unsigned Tag,
MDString *Name, uint64_t SizeInBits,
uint32_t AlignInBits, unsigned Encoding,
StorageType Storage, bool ShouldCreate) {
DIFlags Flags, StorageType Storage,
bool ShouldCreate) {
assert(isCanonical(Name) && "Expected canonical MDString");
DEFINE_GETIMPL_LOOKUP(DIBasicType,
(Tag, Name, SizeInBits, AlignInBits, Encoding));
(Tag, Name, SizeInBits, AlignInBits, Encoding, Flags));
Metadata *Ops[] = {nullptr, nullptr, Name};
DEFINE_GETIMPL_STORE(DIBasicType, (Tag, SizeInBits, AlignInBits, Encoding),
Ops);
DEFINE_GETIMPL_STORE(DIBasicType, (Tag, SizeInBits, AlignInBits, Encoding,
Flags), Ops);
}

Optional<DIBasicType::Signedness> DIBasicType::getSignedness() const {
Expand Down
10 changes: 6 additions & 4 deletions lib/IR/LLVMContextImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -376,20 +376,22 @@ template <> struct MDNodeKeyImpl<DIBasicType> {
uint64_t SizeInBits;
uint32_t AlignInBits;
unsigned Encoding;
unsigned Flags;

MDNodeKeyImpl(unsigned Tag, MDString *Name, uint64_t SizeInBits,
uint32_t AlignInBits, unsigned Encoding)
uint32_t AlignInBits, unsigned Encoding, unsigned Flags)
: Tag(Tag), Name(Name), SizeInBits(SizeInBits), AlignInBits(AlignInBits),
Encoding(Encoding) {}
Encoding(Encoding), Flags(Flags) {}
MDNodeKeyImpl(const DIBasicType *N)
: Tag(N->getTag()), Name(N->getRawName()), SizeInBits(N->getSizeInBits()),
AlignInBits(N->getAlignInBits()), Encoding(N->getEncoding()) {}
AlignInBits(N->getAlignInBits()), Encoding(N->getEncoding()), Flags(N->getFlags()) {}

bool isKeyOf(const DIBasicType *RHS) const {
return Tag == RHS->getTag() && Name == RHS->getRawName() &&
SizeInBits == RHS->getSizeInBits() &&
AlignInBits == RHS->getAlignInBits() &&
Encoding == RHS->getEncoding();
Encoding == RHS->getEncoding() &&
Flags == RHS->getFlags();
}

unsigned getHashValue() const {
Expand Down
2 changes: 2 additions & 0 deletions lib/IR/Verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,8 @@ void Verifier::visitDIBasicType(const DIBasicType &N) {
AssertDI(N.getTag() == dwarf::DW_TAG_base_type ||
N.getTag() == dwarf::DW_TAG_unspecified_type,
"invalid tag", &N);
AssertDI(!(N.isBigEndian() && N.isLittleEndian()) ,
"has conflicting flags", &N);
}

void Verifier::visitDIDerivedType(const DIDerivedType &N) {
Expand Down
9 changes: 7 additions & 2 deletions test/Assembler/debug-info.ll
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
; RUN: llvm-as < %s | llvm-dis | llvm-as | llvm-dis | FileCheck %s
; RUN: verify-uselistorder %s

; CHECK: !named = !{!0, !0, !1, !2, !3, !4, !5, !6, !7, !8, !8, !9, !10, !11, !12, !13, !14, !15, !16, !17, !18, !19, !20, !21, !22, !23, !24, !25, !26, !27, !27, !28, !29, !30, !31, !32, !33, !34, !35, !36, !37}
!named = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9, !10, !11, !12, !13, !14, !15, !16, !17, !18, !19, !20, !21, !22, !23, !24, !25, !26, !27, !28, !29, !30, !31, !32, !33, !34, !35, !36, !37, !38, !39, !40}
; CHECK: !named = !{!0, !0, !1, !2, !3, !4, !5, !6, !7, !8, !8, !9, !10, !11, !12, !13, !14, !15, !16, !17, !18, !19, !20, !21, !22, !23, !24, !25, !26, !27, !27, !28, !29, !30, !31, !32, !33, !34, !35, !36, !37, !38, !39}
!named = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9, !10, !11, !12, !13, !14, !15, !16, !17, !18, !19, !20, !21, !22, !23, !24, !25, !26, !27, !28, !29, !30, !31, !32, !33, !34, !35, !36, !37, !38, !39, !40, !41, !42}

; CHECK: !0 = !DISubrange(count: 3)
; CHECK-NEXT: !1 = !DISubrange(count: 3, lowerBound: 4)
Expand Down Expand Up @@ -94,3 +94,8 @@
; CHECK-NEXT: !37 = !DIFile(filename: "file", directory: "dir", checksumkind: CSK_MD5, checksum: "3a420e2646916a475e68de8d48f779f5", source: "int source() { }\0A")
!39 = !DIFile(filename: "file", directory: "dir", source: "int source() { }\0A")
!40 = !DIFile(filename: "file", directory: "dir", checksumkind: CSK_MD5, checksum: "3a420e2646916a475e68de8d48f779f5", source: "int source() { }\0A")

; CHECK-NEXT: !38 = !DIBasicType(name: "u64.be", size: 64, align: 1, encoding: DW_ATE_unsigned, flags: DIFlagBigEndian)
; CHECK-NEXT: !39 = !DIBasicType(name: "u64.le", size: 64, align: 1, encoding: DW_ATE_unsigned, flags: DIFlagLittleEndian)
!41 = !DIBasicType(name: "u64.be", size: 64, align: 1, encoding: DW_ATE_unsigned, flags: DIFlagBigEndian)
!42 = !DIBasicType(name: "u64.le", size: 64, align: 1, encoding: DW_ATE_unsigned, flags: DIFlagLittleEndian)
26 changes: 17 additions & 9 deletions unittests/IR/MetadataTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -997,34 +997,41 @@ typedef MetadataTest DIBasicTypeTest;

TEST_F(DIBasicTypeTest, get) {
auto *N =
DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7);
DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7,
DINode::FlagZero);
EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag());
EXPECT_EQ("special", N->getName());
EXPECT_EQ(33u, N->getSizeInBits());
EXPECT_EQ(26u, N->getAlignInBits());
EXPECT_EQ(7u, N->getEncoding());
EXPECT_EQ(0u, N->getLine());
EXPECT_EQ(DINode::FlagZero, N->getFlags());
EXPECT_EQ(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
26, 7));
26, 7, DINode::FlagZero));

EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
"special", 33, 26, 7));
"special", 33, 26, 7, DINode::FlagZero));
EXPECT_NE(N,
DIBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7));
DIBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7,
DINode::FlagZero));
EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32,
26, 7));
26, 7, DINode::FlagZero));
EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
25, 7));
25, 7, DINode::FlagZero));
EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
26, 6));
26, 6, DINode::FlagZero));
EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
26, 7, DINode::FlagBigEndian));
EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
26, 7, DINode::FlagLittleEndian));

TempDIBasicType Temp = N->clone();
EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
}

TEST_F(DIBasicTypeTest, getWithLargeValues) {
auto *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special",
UINT64_MAX, UINT32_MAX - 1, 7);
UINT64_MAX, UINT32_MAX - 1, 7, DINode::FlagZero);
EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
EXPECT_EQ(UINT32_MAX - 1, N->getAlignInBits());
}
Expand All @@ -1038,14 +1045,15 @@ TEST_F(DIBasicTypeTest, getUnspecified) {
EXPECT_EQ(0u, N->getAlignInBits());
EXPECT_EQ(0u, N->getEncoding());
EXPECT_EQ(0u, N->getLine());
EXPECT_EQ(DINode::FlagZero, N->getFlags());
}

typedef MetadataTest DITypeTest;

TEST_F(DITypeTest, clone) {
// Check that DIType has a specialized clone that returns TempDIType.
DIType *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "int", 32, 32,
dwarf::DW_ATE_signed);
dwarf::DW_ATE_signed, DINode::FlagZero);

TempDIType Temp = N->clone();
EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Expand Down

0 comments on commit 3224b6c

Please sign in to comment.