Skip to content

Commit 632cbbe

Browse files
committed
Make counts ULEB128. Omit count fields using Flags if they are zero.
1 parent 90265cc commit 632cbbe

File tree

5 files changed

+35
-25
lines changed

5 files changed

+35
-25
lines changed

llvm/docs/CallGraphSection.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ Each record in the `.callgraph` section has the following binary layout:
99
| Field | Type | Size (bits) | Description |
1010
| -------------------------------------- | ------------- | ----------- | ------------------------------------------------------------------------------------------------------- |
1111
| Format Version | `uint8_t` | 8 | The version of the record format. The current version is 0. |
12-
| Flags | `uint8_t` | 8 | Bit 0 is set if the function is a valid indirect call target. Other bits are reserved. |
12+
| Flags | `uint8_t` | 8 | A bitfield where: Bit 0 is set if the function is a potential indirect call target; Bit 1 is set if there are direct callees; Bit 2 is set if there are indirect callees. The remaining 5 bits are reserved. |
1313
| Function Entry PC | `uintptr_t` | 32/64 | The address of the function's entry point. |
14-
| Function Type ID | `uint64_t` | 64 | The type ID of the function. This field is non-zero if the function is an indirect call target and its type is known. |
15-
| Number of Unique Direct Callees | `uint32_t` | 32 | The number of unique direct call destinations from this function. |
16-
| Number of Unique Indirect Target Type IDs| `uint32_t` | 32 | The number of unique indirect call target type IDs within the function. |
14+
| Function Type ID | `uint64_t` | 64 | The type ID of the function. This field is non-zero if the function is a potential indirect call target and its type is known. |
15+
| Number of Unique Direct Callees | `ULEB128` | Variable | The number of unique direct call destinations from this function. This field is only present if there is at least one direct callee. |
16+
| Number of Unique Indirect Target Type IDs| `ULEB128` | Variable | The number of unique indirect call target type IDs. This field is only present if there is at least one indirect target type ID. |
1717
| Direct Callees Array | `uintptr_t[]` | Variable | An array of unique direct callee entry point addresses. |
1818
| Indirect Target Type IDs Array | `uint64_t[]` | Variable | An array of unique indirect call target type IDs. |

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,19 +1695,30 @@ void AsmPrinter::emitCallGraphSection(const MachineFunction &MF,
16951695
/*IgnoreAssumeLikeCalls=*/true,
16961696
/*IgnoreLLVMUsed=*/false);
16971697

1698+
const auto &DirectCallees = FuncCGInfo.DirectCallees;
1699+
const auto &IndirectCalleeTypeIDs = FuncCGInfo.IndirectCalleeTypeIDs;
1700+
16981701
uint8_t Flags = 0;
16991702
if (IsIndirectTarget)
1700-
Flags |= 1 << 0; // Set the LSB bit to 1.
1703+
Flags |= 1u << 0; // Set the first LSB bit to 1.
1704+
if (DirectCallees.size() > 0)
1705+
Flags |= 1u << 1; // Set the second LSB bit to 1.
1706+
if (IndirectCalleeTypeIDs.size() > 0)
1707+
Flags |= 1u << 2; // Set the third LSB bit to 1.
17011708

17021709
// Emit function's call graph information.
17031710
// 1) CallGraphSectionFormatVersion
1704-
// 2) Flags - Bit 0 is set to 1 if the function is a valid indirect target.
1705-
// Other bits are reserved for future use.
1711+
// 2) Flags
1712+
// a. LSB bit 0 is set to 1 if the function is a potential indirect
1713+
// target.
1714+
// b. LSB bit 1 is set to 1 if there are direct callees.
1715+
// c. LSB bit 2 is set to 1 if there are indirect callees.
1716+
// d. Rest of the 5 bits in Flags are reserved for any future use.
17061717
// 3) Function entry PC.
17071718
// 4) FunctionTypeID if the function is indirect target and its type id is
17081719
// known, otherwise it is set to 0.
1709-
// 5) Number of unique direct callees.
1710-
// 6) Number of unique indirect target type IDs.
1720+
// 5) Number of unique direct callees, if at least one exists.
1721+
// 6) Number of unique indirect target type IDs, if at least one exists.
17111722
// 7) For each unique direct callee, the callee's PC.
17121723
// 8) Each unique indirect target type id.
17131724
OutStreamer->emitInt8(CallGraphSectionFormatVersion::V_0);
@@ -1718,10 +1729,11 @@ void AsmPrinter::emitCallGraphSection(const MachineFunction &MF,
17181729
OutStreamer->emitInt64(TypeId->getZExtValue());
17191730
else
17201731
OutStreamer->emitInt64(0);
1721-
const auto &DirectCallees = FuncCGInfo.DirectCallees;
1722-
const auto &IndirectCalleeTypeIDs = FuncCGInfo.IndirectCalleeTypeIDs;
1723-
OutStreamer->emitInt32(DirectCallees.size());
1724-
OutStreamer->emitInt32(IndirectCalleeTypeIDs.size());
1732+
1733+
if (DirectCallees.size() > 0)
1734+
OutStreamer->emitULEB128IntValue(DirectCallees.size());
1735+
if (IndirectCalleeTypeIDs.size() > 0)
1736+
OutStreamer->emitULEB128IntValue(IndirectCalleeTypeIDs.size());
17251737
for (const auto &CalleeSymbol : DirectCallees)
17261738
OutStreamer->emitSymbolValue(CalleeSymbol, TM.getProgramPointerSize());
17271739
FuncCGInfo.DirectCallees.clear();

llvm/test/CodeGen/X86/call-graph-section-assembly.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ entry:
4040
;; Version
4141
; CHECK-NEXT: .byte 0
4242
;; Flags
43-
; CHECK-NEXT: .byte 1
43+
; CHECK-NEXT: .byte 7
4444
;; Function Entry PC
4545
; CHECK-NEXT: .quad [[LABEL_FUNC]]
4646
;; Function type ID -- set to 0 as no type metadata attached to function.
4747
; CHECK-NEXT: .quad 0
4848
;; Number of unique direct callees.
49-
; CHECK-NEXT: .long 3
49+
; CHECK-NEXT: .byte 3
5050
;; Number of unique indirect target type IDs.
51-
; CHECK-NEXT: .long 3
51+
; CHECK-NEXT: .byte 3
5252
;; Direct callees.
5353
; CHECK-NEXT: .quad direct_foo
5454
; CHECK-NEXT: .quad direct_bar

llvm/test/CodeGen/X86/call-graph-section-tailcall.ll

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ declare !type !2 i32 @bar(i8 signext)
2828
!3 = !{i64 0, !"_ZTSFiiE.generalized"}
2929

3030
; CHECK: Hex dump of section '.callgraph':
31-
; CHECK-NEXT: 0x00000000 00010000 00000000 00008e19 0b7f3326
32-
; CHECK-NEXT: 0x00000010 e3000000 00000100 00005486 bc59814b
33-
; CHECK-NEXT: 0x00000020 8e300001 00000000 00000000 a150b83e
31+
; CHECK-NEXT: 0x00000000 00050000 00000000 00008e19 0b7f3326
32+
; CHECK-NEXT: 0x00000010 e3000154 86bc5981 4b8e3000 05000000
3433
;; Verify that the type id 0x308e4b8159bc8654 is in section.
35-
; CHECK-NEXT: 0x00000030 0cfe3cb2 00000000 01000000 5486bc59
36-
; CHECK-NEXT: 0x00000040 814b8e30
34+
; CHECK-NEXT: 0x00000020 00000000 00a150b8 3e0cfe3c b2015486
35+
; CHECK-NEXT: 0x00000030 bc59814b 8e30

llvm/test/CodeGen/X86/call-graph-section.ll

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ entry:
3232
;; Make sure following type IDs are in call graph section
3333
;; 0x5eecb3e2444f731f, 0x814b8e305486bc59, 0xf897fd777ade6814
3434
; CHECK: Hex dump of section '.callgraph':
35-
; CHECK-NEXT: 0x00000000 00010000 00000000 00000000 00000000
36-
; CHECK-NEXT: 0x00000010 00000000 00000300 00002444 f731f5ee
37-
; CHECK-NEXT: 0x00000020 cb3e5486 bc59814b 8e307ade 6814f897
38-
; CHECK-NEXT: 0x00000030 fd77
35+
; CHECK-NEXT: 0x00000000 00050000 00000000 00000000 00000000
36+
; CHECK-NEXT: 0x00000010 00000324 44f731f5 eecb3e54 86bc5981
37+
; CHECK-NEXT: 0x00000020 4b8e307a de6814f8 97fd77

0 commit comments

Comments
 (0)