diff --git a/llvm/lib/IR/Metadata.cpp b/llvm/lib/IR/Metadata.cpp index 97976308fcd0..fc1f3e7a3cf1 100644 --- a/llvm/lib/IR/Metadata.cpp +++ b/llvm/lib/IR/Metadata.cpp @@ -753,6 +753,10 @@ GenericDwarfNode *GenericDwarfNode::getImpl(LLVMContext &Context, unsigned Tag, ArrayRef DwarfOps, StorageType Storage, bool ShouldCreate) { + // Canonicalize empty string to a nullptr. + if (Header && Header->getString().empty()) + Header = nullptr; + unsigned Hash = 0; if (Storage == Uniqued) { GenericDwarfNodeInfo::KeyTy Key(Tag, Header, DwarfOps); diff --git a/llvm/unittests/IR/MetadataTest.cpp b/llvm/unittests/IR/MetadataTest.cpp index f2140d621723..e2ed3d7946bb 100644 --- a/llvm/unittests/IR/MetadataTest.cpp +++ b/llvm/unittests/IR/MetadataTest.cpp @@ -596,6 +596,14 @@ TEST_F(GenericDwarfNodeTest, get) { EXPECT_EQ(N, GenericDwarfNode::get(Context, 15, Header, Ops1)); } +TEST_F(GenericDwarfNodeTest, getEmptyHeader) { + // Canonicalize !"" to null. + auto *Header = MDString::get(Context, ""); + EXPECT_NE(nullptr, Header); + auto *N = GenericDwarfNode::get(Context, 15, Header, None); + EXPECT_EQ(nullptr, N->getHeader()); +} + typedef MetadataTest MetadataAsValueTest; TEST_F(MetadataAsValueTest, MDNode) {