Skip to content

Commit d519d88

Browse files
committed
[llvm][DebugInfo] Support for versioned DISourceLanguageName
1 parent a824276 commit d519d88

File tree

6 files changed

+56
-18
lines changed

6 files changed

+56
-18
lines changed

llvm/include/llvm/IR/DebugInfoMetadata.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ class DISourceLanguageName {
8888

8989
/// Returns a versioned or unversioned language name.
9090
uint16_t getName() const { return Name; }
91+
uint32_t getVersion() const {
92+
assert(hasVersionedName() && "Tried getting version on unversioned name");
93+
return *Version;
94+
}
9195

9296
// Transitional API for cases where we do not yet support
9397
// versioned source language names. Use \ref getName instead.

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5837,8 +5837,9 @@ bool LLParser::parseDICompileUnit(MDNode *&Result, bool IsDistinct) {
58375837
return tokError("missing 'distinct', required for !DICompileUnit");
58385838

58395839
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5840-
REQUIRED(language, DwarfLangField, ); \
58415840
REQUIRED(file, MDField, (/* AllowNull */ false)); \
5841+
OPTIONAL(language, DwarfLangField, ); \
5842+
OPTIONAL(sourceLanguageName, MDUnsignedField, ); \
58425843
OPTIONAL(producer, MDStringField, ); \
58435844
OPTIONAL(isOptimized, MDBoolField, ); \
58445845
OPTIONAL(flags, MDStringField, ); \
@@ -5860,12 +5861,19 @@ bool LLParser::parseDICompileUnit(MDNode *&Result, bool IsDistinct) {
58605861
PARSE_MD_FIELDS();
58615862
#undef VISIT_MD_FIELDS
58625863

5864+
if (!language.Seen && !sourceLanguageName.Seen)
5865+
return tokError("missing one of 'language' or 'sourceLanguageName', "
5866+
"required for !DICompileUnit");
5867+
58635868
Result = DICompileUnit::getDistinct(
5864-
Context, DISourceLanguageName(language.Val), file.Val, producer.Val,
5865-
isOptimized.Val, flags.Val, runtimeVersion.Val, splitDebugFilename.Val,
5866-
emissionKind.Val, enums.Val, retainedTypes.Val, globals.Val, imports.Val,
5867-
macros.Val, dwoId.Val, splitDebugInlining.Val, debugInfoForProfiling.Val,
5868-
nameTableKind.Val, rangesBaseAddress.Val, sysroot.Val, sdk.Val);
5869+
Context,
5870+
language.Seen ? DISourceLanguageName(language.Val)
5871+
: DISourceLanguageName(sourceLanguageName.Val, 0),
5872+
file.Val, producer.Val, isOptimized.Val, flags.Val, runtimeVersion.Val,
5873+
splitDebugFilename.Val, emissionKind.Val, enums.Val, retainedTypes.Val,
5874+
globals.Val, imports.Val, macros.Val, dwoId.Val, splitDebugInlining.Val,
5875+
debugInfoForProfiling.Val, nameTableKind.Val, rangesBaseAddress.Val,
5876+
sysroot.Val, sdk.Val);
58695877
return false;
58705878
}
58715879

llvm/lib/Bitcode/Reader/MetadataLoader.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,12 +1867,18 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
18671867
// distinct. It's always distinct.
18681868
IsDistinct = true;
18691869

1870+
const auto LangVersionMask = (uint64_t(1) << 63);
1871+
const bool HasVersionedLanguage = Record[1] & LangVersionMask;
1872+
18701873
auto *CU = DICompileUnit::getDistinct(
1871-
Context, DISourceLanguageName(Record[1]), getMDOrNull(Record[2]),
1872-
getMDString(Record[3]), Record[4], getMDString(Record[5]), Record[6],
1873-
getMDString(Record[7]), Record[8], getMDOrNull(Record[9]),
1874-
getMDOrNull(Record[10]), getMDOrNull(Record[12]),
1875-
getMDOrNull(Record[13]),
1874+
Context,
1875+
HasVersionedLanguage
1876+
? DISourceLanguageName(Record[1] & ~LangVersionMask, 0)
1877+
: DISourceLanguageName(Record[1]),
1878+
getMDOrNull(Record[2]), getMDString(Record[3]), Record[4],
1879+
getMDString(Record[5]), Record[6], getMDString(Record[7]), Record[8],
1880+
getMDOrNull(Record[9]), getMDOrNull(Record[10]),
1881+
getMDOrNull(Record[12]), getMDOrNull(Record[13]),
18761882
Record.size() <= 15 ? nullptr : getMDOrNull(Record[15]),
18771883
Record.size() <= 14 ? 0 : Record[14],
18781884
Record.size() <= 16 ? true : Record[16],

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2106,7 +2106,13 @@ void ModuleBitcodeWriter::writeDICompileUnit(const DICompileUnit *N,
21062106
assert(N->isDistinct() && "Expected distinct compile units");
21072107
Record.push_back(/* IsDistinct */ true);
21082108

2109-
Record.push_back(N->getSourceLanguage().getUnversionedName());
2109+
auto Lang = N->getSourceLanguage();
2110+
Record.push_back(Lang.getName());
2111+
// Set bit so the MetadataLoader can distniguish between versioned and
2112+
// unversioned names.
2113+
if (Lang.hasVersionedName())
2114+
Record.back() ^= (uint64_t(1) << 63);
2115+
21102116
Record.push_back(VE.getMetadataOrNullID(N->getFile()));
21112117
Record.push_back(VE.getMetadataOrNullID(N->getRawProducer()));
21122118
Record.push_back(N->isOptimized());

llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "DwarfExpression.h"
1717
#include "llvm/ADT/APFloat.h"
1818
#include "llvm/ADT/APInt.h"
19+
#include "llvm/BinaryFormat/Dwarf.h"
1920
#include "llvm/CodeGen/TargetRegisterInfo.h"
2021
#include "llvm/IR/Constants.h"
2122
#include "llvm/IR/DataLayout.h"
@@ -705,8 +706,15 @@ void DwarfUnit::addType(DIE &Entity, const DIType *Ty,
705706
}
706707

707708
llvm::dwarf::SourceLanguage DwarfUnit::getSourceLanguage() const {
708-
return static_cast<llvm::dwarf::SourceLanguage>(
709-
getLanguage().getUnversionedName());
709+
const auto &Lang = getLanguage();
710+
711+
if (!Lang.hasVersionedName())
712+
return static_cast<llvm::dwarf::SourceLanguage>(Lang.getName());
713+
714+
return llvm::dwarf::toDW_LANG(
715+
static_cast<llvm::dwarf::SourceLanguageName>(Lang.getName()),
716+
Lang.getVersion())
717+
.value_or(llvm::dwarf::DW_LANG_hi_user);
710718
}
711719

712720
std::string DwarfUnit::getParentContextString(const DIScope *Context) const {

llvm/lib/IR/AsmWriter.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2370,10 +2370,16 @@ static void writeDICompileUnit(raw_ostream &Out, const DICompileUnit *N,
23702370
Out << "!DICompileUnit(";
23712371
MDFieldPrinter Printer(Out, WriterCtx);
23722372

2373-
Printer.printDwarfEnum("language",
2374-
N->getSourceLanguage().getUnversionedName(),
2375-
dwarf::LanguageString,
2376-
/* ShouldSkipZero */ false);
2373+
auto Lang = N->getSourceLanguage();
2374+
if (Lang.hasVersionedName())
2375+
Printer.printDwarfEnum(
2376+
"sourceLanguageName",
2377+
static_cast<llvm::dwarf::SourceLanguageName>(Lang.getName()),
2378+
dwarf::LanguageDescription,
2379+
/* ShouldSkipZero */ false);
2380+
else
2381+
Printer.printDwarfEnum("language", Lang.getName(), dwarf::LanguageString,
2382+
/* ShouldSkipZero */ false);
23772383

23782384
Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false);
23792385
Printer.printString("producer", N->getProducer());

0 commit comments

Comments
 (0)