Skip to content

[KeyInstr] Add bitcode support #144102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions llvm/lib/Bitcode/Reader/BitcodeReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5082,7 +5082,9 @@ Error BitcodeReader::parseFunctionBody(Function *F) {

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

MDNode *Scope = nullptr, *IA = nullptr;
if (ScopeID) {
Expand All @@ -5097,8 +5099,9 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
if (!IA)
return error("Invalid record");
}

LastLoc = DILocation::get(Scope->getContext(), Line, Col, Scope, IA,
isImplicitCode);
isImplicitCode, AtomGroup, AtomRank);
I->setDebugLoc(LastLoc);
I = nullptr;
continue;
Expand Down
20 changes: 14 additions & 6 deletions llvm/lib/Bitcode/Reader/MetadataLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1416,18 +1416,21 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
break;
}
case bitc::METADATA_LOCATION: {
if (Record.size() != 5 && Record.size() != 6)
// 5: inlinedAt, 6: isImplicit, 8: Key Instructions fields.
if (Record.size() != 5 && Record.size() != 6 && Record.size() != 8)
return error("Invalid record");

IsDistinct = Record[0];
unsigned Line = Record[1];
unsigned Column = Record[2];
Metadata *Scope = getMD(Record[3]);
Metadata *InlinedAt = getMDOrNull(Record[4]);
bool ImplicitCode = Record.size() == 6 && Record[5];
bool ImplicitCode = Record.size() >= 6 && Record[5];
uint64_t AtomGroup = Record.size() == 8 ? Record[6] : 0;
uint8_t AtomRank = Record.size() == 8 ? Record[7] : 0;
MetadataList.assignValue(
GET_OR_DISTINCT(DILocation, (Context, Line, Column, Scope, InlinedAt,
ImplicitCode)),
ImplicitCode, AtomGroup, AtomRank)),
NextMetadataNo);
NextMetadataNo++;
break;
Expand Down Expand Up @@ -1857,7 +1860,7 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
break;
}
case bitc::METADATA_SUBPROGRAM: {
if (Record.size() < 18 || Record.size() > 21)
if (Record.size() < 18 || Record.size() > 22)
return error("Invalid record");

bool HasSPFlags = Record[0] & 4;
Expand Down Expand Up @@ -1909,6 +1912,9 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
bool HasTargetFuncName = false;
unsigned OffsetA = 0;
unsigned OffsetB = 0;
// Key instructions won't be enabled in old-format bitcode, so only
// check it if HasSPFlags is true.
bool UsesKeyInstructions = false;
if (!HasSPFlags) {
OffsetA = 2;
OffsetB = 2;
Expand All @@ -1921,7 +1927,9 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
} else {
HasAnnotations = Record.size() >= 19;
HasTargetFuncName = Record.size() >= 20;
UsesKeyInstructions = Record.size() >= 21 ? Record[20] : 0;
}

Metadata *CUorFn = getMDOrNull(Record[12 + OffsetB]);
DISubprogram *SP = GET_OR_DISTINCT(
DISubprogram,
Expand All @@ -1947,8 +1955,8 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
HasAnnotations ? getMDOrNull(Record[18 + OffsetB])
: nullptr, // annotations
HasTargetFuncName ? getMDString(Record[19 + OffsetB])
: nullptr // targetFuncName
));
: nullptr, // targetFuncName
UsesKeyInstructions));
MetadataList.assignValue(SP, NextMetadataNo);
NextMetadataNo++;

Expand Down
20 changes: 13 additions & 7 deletions llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1799,12 +1799,14 @@ unsigned ModuleBitcodeWriter::createDILocationAbbrev() {
// location (it's never more expensive than building an array size 1).
auto Abbv = std::make_shared<BitCodeAbbrev>();
Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_LOCATION));
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isDistinct
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // line
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // column
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // scope
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // inlinedAt
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImplicitCode
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // atomGroup
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // atomRank
return Stream.EmitAbbrev(std::move(Abbv));
}

Expand All @@ -1820,7 +1822,8 @@ void ModuleBitcodeWriter::writeDILocation(const DILocation *N,
Record.push_back(VE.getMetadataID(N->getScope()));
Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt()));
Record.push_back(N->isImplicitCode());

Record.push_back(N->getAtomGroup());
Record.push_back(N->getAtomRank());
Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev);
Record.clear();
}
Expand Down Expand Up @@ -2141,6 +2144,7 @@ void ModuleBitcodeWriter::writeDISubprogram(const DISubprogram *N,
Record.push_back(VE.getMetadataOrNullID(N->getThrownTypes().get()));
Record.push_back(VE.getMetadataOrNullID(N->getAnnotations().get()));
Record.push_back(VE.getMetadataOrNullID(N->getRawTargetFuncName()));
Record.push_back(N->getKeyInstructionsEnabled());

Stream.EmitRecord(bitc::METADATA_SUBPROGRAM, Record, Abbrev);
Record.clear();
Expand Down Expand Up @@ -3717,6 +3721,8 @@ void ModuleBitcodeWriter::writeFunction(
Vals.push_back(VE.getMetadataOrNullID(DL->getScope()));
Vals.push_back(VE.getMetadataOrNullID(DL->getInlinedAt()));
Vals.push_back(DL->isImplicitCode());
Vals.push_back(DL->getAtomGroup());
Vals.push_back(DL->getAtomRank());
Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
Vals.clear();
LastDL = DL;
Expand Down
67 changes: 67 additions & 0 deletions llvm/test/DebugInfo/KeyInstructions/Generic/link-two-modes.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
; RUN: split-file %s %t
; RUN: llvm-as %t/key-instr-enabled.ll -o %t/key-instr-enabled.bc
; RUN: llvm-as %t/key-instr-disabled.ll -o %t/key-instr-disabled.bc
; RUN: llvm-link %t/key-instr-enabled.bc %t/key-instr-disabled.bc -o - | llvm-dis | FileCheck %s

;; Check the Key Instructions metadata is preserved correctly when linking a
;; modules with Key Instructions enabled/disabled.

;; Key Instructions enabled.
; CHECK: void @f() !dbg [[f:!.*]] {
; CHECK-NEXT: entry:
; CHECK-NEXT: ret void, !dbg [[enabled:!.*]]
; CHECK-NEXT: }

;; Key Instructions disabled.
; CHECK: void @g() !dbg [[g:!.*]] {
; CHECK-NEXT: entry:
; CHECK-NEXT: ret void, !dbg [[disabled:!.*]]
; CHECK-NEXT: }

; CHECK: !llvm.dbg.cu = !{!0, !2}
; CHECK-NEXT: !llvm.module.flags = !{!4}

; CHECK: [[file1:!.*]] = !DIFile(filename: "key-instr-enabled.cpp", directory: "/")
; CHECK: [[file2:!.*]] = !DIFile(filename: "key-instr-disabled.cpp", directory: "/")
; CHECK: [[f]] = distinct !DISubprogram(name: "f", scope: [[file1]]{{.*}}, keyInstructions: true)
; CHECK: [[enabled]] = !DILocation(line: 1, column: 11, scope: [[f]], atomGroup: 1, atomRank: 1)
; CHECK: [[g]] = distinct !DISubprogram(name: "g", scope: [[file2]]
; CHECK-NOT: keyInstructions
; CHECK-SAME: )
; CHECK: [[disabled]] = !DILocation(line: 1, column: 11, scope: [[g]])

;--- key-instr-enabled.ll
define dso_local void @f() !dbg !10 {
entry:
ret void, !dbg !13
}

!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!3}

!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)
!1 = !DIFile(filename: "key-instr-enabled.cpp", directory: "/")
!3 = !{i32 2, !"Debug Info Version", i32 3}
!9 = !{!"clang version 21.0.0git"}
!10 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !11, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, keyInstructions: true)
!11 = !DISubroutineType(types: !12)
!12 = !{null}
!13 = !DILocation(line: 1, column: 11, scope: !10, atomGroup: 1, atomRank: 1)

;--- key-instr-disabled.ll
define dso_local void @g() !dbg !10 {
entry:
ret void, !dbg !13
}

!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!3}

!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)
!1 = !DIFile(filename: "key-instr-disabled.cpp", directory: "/")
!3 = !{i32 2, !"Debug Info Version", i32 3}
!9 = !{!"clang version 21.0.0git"}
!10 = distinct !DISubprogram(name: "g", scope: !1, file: !1, line: 1, type: !11, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, keyInstructions: false)
!11 = !DISubroutineType(types: !12)
!12 = !{null}
!13 = !DILocation(line: 1, column: 11, scope: !10)
48 changes: 48 additions & 0 deletions llvm/test/DebugInfo/KeyInstructions/Generic/roundtrip.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt %s -o - -S | llvm-as - | llvm-dis - | FileCheck %s
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initial opt run un-necessary?


; Key Instructions enabled.
define dso_local void @f() !dbg !10 {
; CHECK-LABEL: define dso_local void @f(
; CHECK-SAME: ) !dbg [[DBG3:![0-9]+]] {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: ret void, !dbg [[DBG6:![0-9]+]]
;
entry:
ret void, !dbg !13
}

; Key Instructions disabled.
define dso_local void @g() !dbg !14 {
; CHECK-LABEL: define dso_local void @g(
; CHECK-SAME: ) !dbg [[DBG7:![0-9]+]] {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: ret void, !dbg [[DBG8:![0-9]+]]
;
entry:
ret void, !dbg !15
}

!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!3}

!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)
!1 = !DIFile(filename: "key-instr.cpp", directory: "/")
!3 = !{i32 2, !"Debug Info Version", i32 3}
!9 = !{!"clang version 21.0.0git"}
!10 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !11, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, keyInstructions: true)
!11 = !DISubroutineType(types: !12)
!12 = !{null}
!13 = !DILocation(line: 1, scope: !10, atomGroup: 1, atomRank: 1)
!14 = distinct !DISubprogram(name: "g", scope: !1, file: !1, line: 2, type: !11, scopeLine: 2, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, keyInstructions: false)
!15 = !DILocation(line: 2, scope: !14)
;.
; 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)
; CHECK: [[META1]] = !DIFile(filename: "{{.*}}key-instr.cpp", directory: {{.*}})
; 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)
; CHECK: [[META4]] = !DISubroutineType(types: [[META5:![0-9]+]])
; CHECK: [[META5]] = !{null}
; CHECK: [[DBG6]] = !DILocation(line: 1, scope: [[DBG3]], atomGroup: 1, atomRank: 1)
; CHECK: [[DBG7]] = distinct !DISubprogram(name: "g", scope: [[META1]], file: [[META1]], line: 2, type: [[META4]], scopeLine: 2, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: [[META0]])
; CHECK: [[DBG8]] = !DILocation(line: 2, scope: [[DBG7]])
;.
Loading