Skip to content

Commit b421c14

Browse files
committed
[KeyInstr] Add bitcode support
1 parent ebd7f75 commit b421c14

File tree

5 files changed

+147
-15
lines changed

5 files changed

+147
-15
lines changed

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5082,7 +5082,9 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
50825082

50835083
unsigned Line = Record[0], Col = Record[1];
50845084
unsigned ScopeID = Record[2], IAID = Record[3];
5085-
bool isImplicitCode = Record.size() == 5 && Record[4];
5085+
bool isImplicitCode = Record.size() >= 5 && Record[4];
5086+
uint64_t AtomGroup = Record.size() == 7 ? Record[5] : 0;
5087+
uint8_t AtomRank = Record.size() == 7 ? Record[6] : 0;
50865088

50875089
MDNode *Scope = nullptr, *IA = nullptr;
50885090
if (ScopeID) {
@@ -5097,8 +5099,9 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
50975099
if (!IA)
50985100
return error("Invalid record");
50995101
}
5102+
51005103
LastLoc = DILocation::get(Scope->getContext(), Line, Col, Scope, IA,
5101-
isImplicitCode);
5104+
isImplicitCode, AtomGroup, AtomRank);
51025105
I->setDebugLoc(LastLoc);
51035106
I = nullptr;
51045107
continue;

llvm/lib/Bitcode/Reader/MetadataLoader.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,18 +1416,21 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
14161416
break;
14171417
}
14181418
case bitc::METADATA_LOCATION: {
1419-
if (Record.size() != 5 && Record.size() != 6)
1419+
// 5: inlinedAt, 6: isImplicit, 8: Key Instructions fields.
1420+
if (Record.size() != 5 && Record.size() != 6 && Record.size() != 8)
14201421
return error("Invalid record");
14211422

14221423
IsDistinct = Record[0];
14231424
unsigned Line = Record[1];
14241425
unsigned Column = Record[2];
14251426
Metadata *Scope = getMD(Record[3]);
14261427
Metadata *InlinedAt = getMDOrNull(Record[4]);
1427-
bool ImplicitCode = Record.size() == 6 && Record[5];
1428+
bool ImplicitCode = Record.size() >= 6 && Record[5];
1429+
uint64_t AtomGroup = Record.size() == 8 ? Record[6] : 0;
1430+
uint8_t AtomRank = Record.size() == 8 ? Record[7] : 0;
14281431
MetadataList.assignValue(
14291432
GET_OR_DISTINCT(DILocation, (Context, Line, Column, Scope, InlinedAt,
1430-
ImplicitCode)),
1433+
ImplicitCode, AtomGroup, AtomRank)),
14311434
NextMetadataNo);
14321435
NextMetadataNo++;
14331436
break;
@@ -1857,7 +1860,7 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
18571860
break;
18581861
}
18591862
case bitc::METADATA_SUBPROGRAM: {
1860-
if (Record.size() < 18 || Record.size() > 21)
1863+
if (Record.size() < 18 || Record.size() > 22)
18611864
return error("Invalid record");
18621865

18631866
bool HasSPFlags = Record[0] & 4;
@@ -1909,6 +1912,9 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
19091912
bool HasTargetFuncName = false;
19101913
unsigned OffsetA = 0;
19111914
unsigned OffsetB = 0;
1915+
// Key instructions won't be enabled in old-format bitcode, so only
1916+
// check it if HasSPFlags is true.
1917+
bool UsesKeyInstructions = false;
19121918
if (!HasSPFlags) {
19131919
OffsetA = 2;
19141920
OffsetB = 2;
@@ -1921,7 +1927,9 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
19211927
} else {
19221928
HasAnnotations = Record.size() >= 19;
19231929
HasTargetFuncName = Record.size() >= 20;
1930+
UsesKeyInstructions = Record.size() >= 21 ? Record[20] : 0;
19241931
}
1932+
19251933
Metadata *CUorFn = getMDOrNull(Record[12 + OffsetB]);
19261934
DISubprogram *SP = GET_OR_DISTINCT(
19271935
DISubprogram,
@@ -1947,8 +1955,8 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
19471955
HasAnnotations ? getMDOrNull(Record[18 + OffsetB])
19481956
: nullptr, // annotations
19491957
HasTargetFuncName ? getMDString(Record[19 + OffsetB])
1950-
: nullptr // targetFuncName
1951-
));
1958+
: nullptr, // targetFuncName
1959+
UsesKeyInstructions));
19521960
MetadataList.assignValue(SP, NextMetadataNo);
19531961
NextMetadataNo++;
19541962

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,12 +1799,14 @@ unsigned ModuleBitcodeWriter::createDILocationAbbrev() {
17991799
// location (it's never more expensive than building an array size 1).
18001800
auto Abbv = std::make_shared<BitCodeAbbrev>();
18011801
Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_LOCATION));
1802-
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1803-
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1804-
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1805-
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1806-
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1807-
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1802+
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isDistinct
1803+
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // line
1804+
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // column
1805+
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // scope
1806+
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // inlinedAt
1807+
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImplicitCode
1808+
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // atomGroup
1809+
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // atomRank
18081810
return Stream.EmitAbbrev(std::move(Abbv));
18091811
}
18101812

@@ -1820,7 +1822,8 @@ void ModuleBitcodeWriter::writeDILocation(const DILocation *N,
18201822
Record.push_back(VE.getMetadataID(N->getScope()));
18211823
Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt()));
18221824
Record.push_back(N->isImplicitCode());
1823-
1825+
Record.push_back(N->getAtomGroup());
1826+
Record.push_back(N->getAtomRank());
18241827
Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev);
18251828
Record.clear();
18261829
}
@@ -2141,6 +2144,7 @@ void ModuleBitcodeWriter::writeDISubprogram(const DISubprogram *N,
21412144
Record.push_back(VE.getMetadataOrNullID(N->getThrownTypes().get()));
21422145
Record.push_back(VE.getMetadataOrNullID(N->getAnnotations().get()));
21432146
Record.push_back(VE.getMetadataOrNullID(N->getRawTargetFuncName()));
2147+
Record.push_back(N->getKeyInstructionsEnabled());
21442148

21452149
Stream.EmitRecord(bitc::METADATA_SUBPROGRAM, Record, Abbrev);
21462150
Record.clear();
@@ -3717,6 +3721,8 @@ void ModuleBitcodeWriter::writeFunction(
37173721
Vals.push_back(VE.getMetadataOrNullID(DL->getScope()));
37183722
Vals.push_back(VE.getMetadataOrNullID(DL->getInlinedAt()));
37193723
Vals.push_back(DL->isImplicitCode());
3724+
Vals.push_back(DL->getAtomGroup());
3725+
Vals.push_back(DL->getAtomRank());
37203726
Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
37213727
Vals.clear();
37223728
LastDL = DL;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
; RUN: split-file %s %t
2+
; RUN: llvm-as %t/key-instr-enabled.ll -o %t/key-instr-enabled.bc
3+
; RUN: llvm-as %t/key-instr-disabled.ll -o %t/key-instr-disabled.bc
4+
; RUN: llvm-link %t/key-instr-enabled.bc %t/key-instr-disabled.bc -o - | llvm-dis | FileCheck %s
5+
6+
;; Check the Key Instructions metadata is preserved correctly when linking a
7+
;; modules with Key Instructions enabled/disabled.
8+
9+
;; Key Instructions enabled.
10+
; CHECK: void @f() !dbg [[f:!.*]] {
11+
; CHECK-NEXT: entry:
12+
; CHECK-NEXT: ret void, !dbg [[enabled:!.*]]
13+
; CHECK-NEXT: }
14+
15+
;; Key Instructions disabled.
16+
; CHECK: void @g() !dbg [[g:!.*]] {
17+
; CHECK-NEXT: entry:
18+
; CHECK-NEXT: ret void, !dbg [[disabled:!.*]]
19+
; CHECK-NEXT: }
20+
21+
; CHECK: !llvm.dbg.cu = !{!0, !2}
22+
; CHECK-NEXT: !llvm.module.flags = !{!4}
23+
24+
; CHECK: [[file1:!.*]] = !DIFile(filename: "key-instr-enabled.cpp", directory: "/")
25+
; CHECK: [[file2:!.*]] = !DIFile(filename: "key-instr-disabled.cpp", directory: "/")
26+
; CHECK: [[f]] = distinct !DISubprogram(name: "f", scope: [[file1]]{{.*}}, keyInstructions: true)
27+
; CHECK: [[enabled]] = !DILocation(line: 1, column: 11, scope: [[f]], atomGroup: 1, atomRank: 1)
28+
; CHECK: [[g]] = distinct !DISubprogram(name: "g", scope: [[file2]]
29+
; CHECK-NOT: keyInstructions
30+
; CHECK-SAME: )
31+
; CHECK: [[disabled]] = !DILocation(line: 1, column: 11, scope: [[g]])
32+
33+
;--- key-instr-enabled.ll
34+
define dso_local void @f() !dbg !10 {
35+
entry:
36+
ret void, !dbg !13
37+
}
38+
39+
!llvm.dbg.cu = !{!0}
40+
!llvm.module.flags = !{!3}
41+
42+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 21.0.0git", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
43+
!1 = !DIFile(filename: "key-instr-enabled.cpp", directory: "/")
44+
!3 = !{i32 2, !"Debug Info Version", i32 3}
45+
!9 = !{!"clang version 21.0.0git"}
46+
!10 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !11, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, keyInstructions: true)
47+
!11 = !DISubroutineType(types: !12)
48+
!12 = !{null}
49+
!13 = !DILocation(line: 1, column: 11, scope: !10, atomGroup: 1, atomRank: 1)
50+
51+
;--- key-instr-disabled.ll
52+
define dso_local void @g() !dbg !10 {
53+
entry:
54+
ret void, !dbg !13
55+
}
56+
57+
!llvm.dbg.cu = !{!0}
58+
!llvm.module.flags = !{!3}
59+
60+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 21.0.0git", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
61+
!1 = !DIFile(filename: "key-instr-disabled.cpp", directory: "/")
62+
!3 = !{i32 2, !"Debug Info Version", i32 3}
63+
!9 = !{!"clang version 21.0.0git"}
64+
!10 = distinct !DISubprogram(name: "g", scope: !1, file: !1, line: 1, type: !11, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, keyInstructions: false)
65+
!11 = !DISubroutineType(types: !12)
66+
!12 = !{null}
67+
!13 = !DILocation(line: 1, column: 11, scope: !10)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt %s -o - -S | llvm-as - | llvm-dis - | FileCheck %s
3+
4+
; Key Instructions enabled.
5+
define dso_local void @f() !dbg !10 {
6+
; CHECK-LABEL: define dso_local void @f(
7+
; CHECK-SAME: ) !dbg [[DBG3:![0-9]+]] {
8+
; CHECK-NEXT: [[ENTRY:.*:]]
9+
; CHECK-NEXT: ret void, !dbg [[DBG6:![0-9]+]]
10+
;
11+
entry:
12+
ret void, !dbg !13
13+
}
14+
15+
; Key Instructions disabled.
16+
define dso_local void @g() !dbg !14 {
17+
; CHECK-LABEL: define dso_local void @g(
18+
; CHECK-SAME: ) !dbg [[DBG7:![0-9]+]] {
19+
; CHECK-NEXT: [[ENTRY:.*:]]
20+
; CHECK-NEXT: ret void, !dbg [[DBG8:![0-9]+]]
21+
;
22+
entry:
23+
ret void, !dbg !15
24+
}
25+
26+
!llvm.dbg.cu = !{!0}
27+
!llvm.module.flags = !{!3}
28+
29+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 21.0.0git", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
30+
!1 = !DIFile(filename: "key-instr.cpp", directory: "/")
31+
!3 = !{i32 2, !"Debug Info Version", i32 3}
32+
!9 = !{!"clang version 21.0.0git"}
33+
!10 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !11, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, keyInstructions: true)
34+
!11 = !DISubroutineType(types: !12)
35+
!12 = !{null}
36+
!13 = !DILocation(line: 1, scope: !10, atomGroup: 1, atomRank: 1)
37+
!14 = distinct !DISubprogram(name: "g", scope: !1, file: !1, line: 2, type: !11, scopeLine: 2, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, keyInstructions: false)
38+
!15 = !DILocation(line: 2, scope: !14)
39+
;.
40+
; CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: [[META1:![0-9]+]], producer: "{{.*}}clang version {{.*}}", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
41+
; CHECK: [[META1]] = !DIFile(filename: "{{.*}}key-instr.cpp", directory: {{.*}})
42+
; CHECK: [[DBG3]] = distinct !DISubprogram(name: "f", scope: [[META1]], file: [[META1]], line: 1, type: [[META4:![0-9]+]], scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: [[META0]], keyInstructions: true)
43+
; CHECK: [[META4]] = !DISubroutineType(types: [[META5:![0-9]+]])
44+
; CHECK: [[META5]] = !{null}
45+
; CHECK: [[DBG6]] = !DILocation(line: 1, scope: [[DBG3]], atomGroup: 1, atomRank: 1)
46+
; CHECK: [[DBG7]] = distinct !DISubprogram(name: "g", scope: [[META1]], file: [[META1]], line: 2, type: [[META4]], scopeLine: 2, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: [[META0]])
47+
; CHECK: [[DBG8]] = !DILocation(line: 2, scope: [[DBG7]])
48+
;.

0 commit comments

Comments
 (0)