Skip to content

Commit cf1cdde

Browse files
authored
[llvm][DebugInfo] Add 'sourceLanguageVersion' field support to DICompileUnit (#162632)
Depends on: * #162445 In preparation to emit DWARFv6's `DW_AT_language_version`.
1 parent a1b0db8 commit cf1cdde

File tree

8 files changed

+83
-6
lines changed

8 files changed

+83
-6
lines changed

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5876,6 +5876,7 @@ bool LLParser::parseDICompileUnit(MDNode *&Result, bool IsDistinct) {
58765876
REQUIRED(file, MDField, (/* AllowNull */ false)); \
58775877
OPTIONAL(language, DwarfLangField, ); \
58785878
OPTIONAL(sourceLanguageName, DwarfSourceLangNameField, ); \
5879+
OPTIONAL(sourceLanguageVersion, MDUnsignedField, (0, UINT32_MAX)); \
58795880
OPTIONAL(producer, MDStringField, ); \
58805881
OPTIONAL(isOptimized, MDBoolField, ); \
58815882
OPTIONAL(flags, MDStringField, ); \
@@ -5905,10 +5906,15 @@ bool LLParser::parseDICompileUnit(MDNode *&Result, bool IsDistinct) {
59055906
return error(Loc, "can only specify one of 'language' and "
59065907
"'sourceLanguageName' on !DICompileUnit");
59075908

5909+
if (sourceLanguageVersion.Seen && !sourceLanguageName.Seen)
5910+
return error(Loc, "'sourceLanguageVersion' requires an associated "
5911+
"'sourceLanguageName' on !DICompileUnit");
5912+
59085913
Result = DICompileUnit::getDistinct(
59095914
Context,
59105915
language.Seen ? DISourceLanguageName(language.Val)
5911-
: DISourceLanguageName(sourceLanguageName.Val, 0),
5916+
: DISourceLanguageName(sourceLanguageName.Val,
5917+
sourceLanguageVersion.Val),
59125918
file.Val, producer.Val, isOptimized.Val, flags.Val, runtimeVersion.Val,
59135919
splitDebugFilename.Val, emissionKind.Val, enums.Val, retainedTypes.Val,
59145920
globals.Val, imports.Val, macros.Val, dwoId.Val, splitDebugInlining.Val,

llvm/lib/Bitcode/Reader/MetadataLoader.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,7 +1860,7 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
18601860
break;
18611861
}
18621862
case bitc::METADATA_COMPILE_UNIT: {
1863-
if (Record.size() < 14 || Record.size() > 22)
1863+
if (Record.size() < 14 || Record.size() > 23)
18641864
return error("Invalid record");
18651865

18661866
// Ignore Record[0], which indicates whether this compile unit is
@@ -1869,11 +1869,13 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
18691869

18701870
const auto LangVersionMask = (uint64_t(1) << 63);
18711871
const bool HasVersionedLanguage = Record[1] & LangVersionMask;
1872+
const uint32_t LanguageVersion = Record.size() > 22 ? Record[22] : 0;
18721873

18731874
auto *CU = DICompileUnit::getDistinct(
18741875
Context,
18751876
HasVersionedLanguage
1876-
? DISourceLanguageName(Record[1] & ~LangVersionMask, 0)
1877+
? DISourceLanguageName(Record[1] & ~LangVersionMask,
1878+
LanguageVersion)
18771879
: DISourceLanguageName(Record[1]),
18781880
getMDOrNull(Record[2]), getMDString(Record[3]), Record[4],
18791881
getMDString(Record[5]), Record[6], getMDString(Record[7]), Record[8],

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2142,6 +2142,7 @@ void ModuleBitcodeWriter::writeDICompileUnit(const DICompileUnit *N,
21422142
Record.push_back(N->getRangesBaseAddress());
21432143
Record.push_back(VE.getMetadataOrNullID(N->getRawSysRoot()));
21442144
Record.push_back(VE.getMetadataOrNullID(N->getRawSDK()));
2145+
Record.push_back(Lang.hasVersionedName() ? Lang.getVersion() : 0);
21452146

21462147
Stream.EmitRecord(bitc::METADATA_COMPILE_UNIT, Record, Abbrev);
21472148
Record.clear();

llvm/lib/IR/AsmWriter.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2374,16 +2374,21 @@ static void writeDICompileUnit(raw_ostream &Out, const DICompileUnit *N,
23742374
Out << "!DICompileUnit(";
23752375
MDFieldPrinter Printer(Out, WriterCtx);
23762376

2377-
auto Lang = N->getSourceLanguage();
2378-
if (Lang.hasVersionedName())
2377+
DISourceLanguageName Lang = N->getSourceLanguage();
2378+
2379+
if (Lang.hasVersionedName()) {
23792380
Printer.printDwarfEnum(
23802381
"sourceLanguageName",
23812382
static_cast<llvm::dwarf::SourceLanguageName>(Lang.getName()),
23822383
dwarf::SourceLanguageNameString,
23832384
/* ShouldSkipZero */ false);
2384-
else
2385+
2386+
Printer.printInt("sourceLanguageVersion", Lang.getVersion(),
2387+
/*ShouldSkipZero=*/true);
2388+
} else {
23852389
Printer.printDwarfEnum("language", Lang.getName(), dwarf::LanguageString,
23862390
/* ShouldSkipZero */ false);
2391+
}
23872392

23882393
Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false);
23892394
Printer.printString("producer", N->getProducer());
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
; RUN: split-file %s %t
2+
; RUN: not llvm-as < %t/dw_lang_with_version.ll -disable-output 2>&1 | FileCheck %s --check-prefix=WRONG-ATTR
3+
; RUN: not llvm-as < %t/overflow.ll -disable-output 2>&1 | FileCheck %s --check-prefix=OVERFLOW
4+
; RUN: not llvm-as < %t/version_without_name.ll -disable-output 2>&1 | FileCheck %s --check-prefix=NO-NAME
5+
; RUN: not llvm-as < %t/negative.ll -disable-output 2>&1 | FileCheck %s --check-prefix=NEGATIVE
6+
7+
; WRONG-ATTR: error: 'sourceLanguageVersion' requires an associated 'sourceLanguageName' on !DICompileUnit
8+
; OVERFLOW: error: value for 'sourceLanguageVersion' too large, limit is 4294967295
9+
; NEGATIVE: error: expected unsigned integer
10+
; NO-NAME: error: missing one of 'language' or 'sourceLanguageName', required for !DICompileUnit
11+
12+
;--- dw_lang_with_version.ll
13+
!0 = distinct !DICompileUnit(language: DW_LANG_C, sourceLanguageVersion: 1,
14+
file: !DIFile(filename: "", directory: ""))
15+
16+
;--- overflow.ll
17+
!0 = distinct !DICompileUnit(sourceLanguageName: DW_LNAME_C, sourceLanguageVersion: 4294967298)
18+
19+
;--- negative.ll
20+
!0 = distinct !DICompileUnit(sourceLanguageName: DW_LNAME_C, sourceLanguageVersion: -1,
21+
file: !DIFile(filename: "", directory: ""))
22+
23+
;--- version_without_name.ll
24+
!0 = distinct !DICompileUnit(sourceLanguageVersion: 1,
25+
file: !DIFile(filename: "", directory: ""))
1.72 KB
Binary file not shown.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; RUN: llvm-as < %s | llvm-dis | llvm-as | llvm-dis | FileCheck %s --implicit-check-not "sourceLanguageVersion: 0"
2+
3+
; CHECK: sourceLanguageVersion: 120
4+
5+
source_filename = "cu.cpp"
6+
target triple = "arm64-apple-macosx"
7+
8+
!llvm.dbg.cu = !{!0, !5}
9+
!llvm.module.flags = !{!3, !4}
10+
11+
!0 = distinct !DICompileUnit(sourceLanguageName: DW_LNAME_ObjC_plus_plus, sourceLanguageVersion: 120, file: !1, producer: "handwritten", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, globals: !2, splitDebugInlining: false, nameTableKind: Apple, sysroot: "/")
12+
!1 = !DIFile(filename: "cu.cpp", directory: "/tmp")
13+
!2 = !{}
14+
!3 = !{i32 7, !"Dwarf Version", i32 5}
15+
!4 = !{i32 2, !"Debug Info Version", i32 3}
16+
!5 = distinct !DICompileUnit(sourceLanguageName: DW_LNAME_ObjC_plus_plus, sourceLanguageVersion: 0, file: !6, producer: "handwritten", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, globals: !2, splitDebugInlining: false, nameTableKind: Apple, sysroot: "/")
17+
!6 = !DIFile(filename: "cu2.cpp", directory: "/tmp")
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
; Test loading metadata which was not aware of versioned language names.
2+
;
3+
; RUN: llvm-dis -o - %p/Inputs/compile-unit-no-versioned-language.bc \
4+
; RUN: | FileCheck %s --implicit-check-not "sourceLanguageName" --implicit-check-not "sourceLanguageVersion"
5+
6+
; Input bitcode file was compiled from following source on
7+
; LLVM commit `fc22b58c25963ece6b041cadbdc931c2338955e4`:
8+
;
9+
; source_filename = "cu.cpp"
10+
; target triple = "arm64-apple-macosx"
11+
;
12+
; !llvm.dbg.cu = !{!0}
13+
; !llvm.module.flags = !{!3, !4}
14+
;
15+
; !0 = distinct !DICompileUnit(language: DW_LANG_ObjC, file: !1, producer: "handwritten", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, globals: !2, splitDebugInlining: false, nameTableKind: Apple, sysroot: "/")
16+
; !1 = !DIFile(filename: "cu.cpp", directory: "/tmp")
17+
; !2 = !{}
18+
; !3 = !{i32 7, !"Dwarf Version", i32 5}
19+
; !4 = !{i32 2, !"Debug Info Version", i32 3}
20+
21+
; CHECK: distinct !DICompileUnit(language: DW_LANG_ObjC,

0 commit comments

Comments
 (0)