Skip to content

Commit 528c063

Browse files
committed
Address review comments.
1 parent e399ae1 commit 528c063

File tree

8 files changed

+172
-87
lines changed

8 files changed

+172
-87
lines changed

llvm/docs/CallGraphSection.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ Each record in the `.callgraph` section has the following binary layout:
1313
| Function Entry PC | `uintptr_t` | 32/64 | The address of the function's entry point. |
1414
| 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. |
1515
| 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-
| Direct Callees Array | `uintptr_t[]` | Variable | An array of unique direct callee entry point addresses. |
16+
| Direct Callees Array | `uintptr_t[]` | Variable | An array of unique direct callee entry point addresses. This field is only present if there is at least one direct callee. |
1717
| 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. |
18-
| Indirect Target Type IDs Array | `uint64_t[]` | Variable | An array of unique indirect call target type IDs. |
18+
| Indirect Target Type IDs Array | `uint64_t[]` | Variable | An array of unique indirect call target type IDs. This field is only present if there is at least one indirect target type ID. |

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,13 +1696,19 @@ void AsmPrinter::emitCallGraphSection(const MachineFunction &MF,
16961696
const auto &DirectCallees = FuncCGInfo.DirectCallees;
16971697
const auto &IndirectCalleeTypeIDs = FuncCGInfo.IndirectCalleeTypeIDs;
16981698

1699+
enum CallGraphFlags : uint8_t {
1700+
IsIndirectTargetFlag = 1u << 0,
1701+
HasDirectCalleesFlag = 1u << 1,
1702+
HasIndirectCalleesFlag = 1u << 2,
1703+
};
1704+
16991705
uint8_t Flags = 0;
17001706
if (IsIndirectTarget)
1701-
Flags |= 1u << 0; // Set the first LSB bit to 1.
1707+
Flags |= IsIndirectTargetFlag;
17021708
if (DirectCallees.size() > 0)
1703-
Flags |= 1u << 1; // Set the second LSB bit to 1.
1709+
Flags |= HasDirectCalleesFlag;
17041710
if (IndirectCalleeTypeIDs.size() > 0)
1705-
Flags |= 1u << 2; // Set the third LSB bit to 1.
1711+
Flags |= HasIndirectCalleesFlag;
17061712

17071713
// Emit function's call graph information.
17081714
// 1) CallGraphSectionFormatVersion
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
;; Test if temporary labels are generated for each indirect callsite.
2+
;; Test if the .callgraph section contains the MD5 hash of callees' type (type id)
3+
;; is correctly paired with its corresponding temporary label generated for indirect
4+
;; call sites annotated with !callee_type metadata.
5+
;; Test if the .callgraph section contains unique direct callees.
6+
7+
; RUN: llc -mtriple=arm-unknown-linux --call-graph-section -o - < %s | FileCheck %s
8+
9+
declare !type !0 void @direct_foo()
10+
declare !type !1 i32 @direct_bar(i8)
11+
declare !type !2 ptr @direct_baz(ptr)
12+
13+
; CHECK: ball:
14+
; CHECK-NEXT: [[LABEL_FUNC:\.Lfunc_begin[0-9]+]]:
15+
define ptr @ball() {
16+
entry:
17+
call void @direct_foo()
18+
%fp_foo_val = load ptr, ptr null, align 8
19+
call void (...) %fp_foo_val(), !callee_type !0
20+
call void @direct_foo()
21+
%fp_bar_val = load ptr, ptr null, align 8
22+
%call_fp_bar = call i32 %fp_bar_val(i8 0), !callee_type !2
23+
%call_fp_bar_direct = call i32 @direct_bar(i8 1)
24+
%fp_baz_val = load ptr, ptr null, align 8
25+
%call_fp_baz = call ptr %fp_baz_val(ptr null), !callee_type !4
26+
call void @direct_foo()
27+
%call_fp_baz_direct = call ptr @direct_baz(ptr null)
28+
call void @direct_foo()
29+
ret ptr %call_fp_baz
30+
}
31+
32+
!0 = !{!1}
33+
!1 = !{i64 0, !"_ZTSFvE.generalized"}
34+
!2 = !{!3}
35+
!3 = !{i64 0, !"_ZTSFicE.generalized"}
36+
!4 = !{!5}
37+
!5 = !{i64 0, !"_ZTSFPvS_E.generalized"}
38+
39+
; CHECK: .section .callgraph,"o",%progbits,.text
40+
;; Version
41+
; CHECK-NEXT: .byte 0
42+
;; Flags
43+
; CHECK-NEXT: .byte 7
44+
;; Function Entry PC
45+
; CHECK-NEXT: .long [[LABEL_FUNC]]
46+
;; Function type ID -- set to 0 as no type metadata attached to function.
47+
; CHECK-NEXT: .long 0
48+
; CHECK-NEXT: .long 0
49+
;; Number of unique direct callees.
50+
; CHECK-NEXT: .byte 3
51+
;; Direct callees.
52+
; CHECK-NEXT: .long direct_foo
53+
; CHECK-NEXT: .long direct_bar
54+
; CHECK-NEXT: .long direct_baz
55+
;; Number of unique indirect target type IDs.
56+
; CHECK-NEXT: .byte 3
57+
;; Indirect type IDs.
58+
; CHECK-NEXT: .long 838288420
59+
; CHECK-NEXT: .long 1053552373
60+
; CHECK-NEXT: .long 1505527380
61+
; CHECK-NEXT: .long 814631809
62+
; CHECK-NEXT: .long 342417018
63+
; CHECK-NEXT: .long 2013108216
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
;; Tests that we store the type identifiers in .callgraph section of the object file for tailcalls.
2+
3+
; RUN: llc -mtriple=arm-unknown-linux --call-graph-section -filetype=obj -o - < %s | \
4+
; RUN: llvm-readelf -x .callgraph - | FileCheck %s
5+
6+
define i32 @check_tailcall(ptr %func, i8 %x) !type !0 {
7+
entry:
8+
%call = tail call i32 %func(i8 signext %x), !callee_type !1
9+
ret i32 %call
10+
}
11+
12+
define i32 @main(i32 %argc) !type !3 {
13+
entry:
14+
%andop = and i32 %argc, 1
15+
%cmp = icmp eq i32 %andop, 0
16+
%foo.bar = select i1 %cmp, ptr @foo, ptr @bar
17+
%call.i = tail call i32 %foo.bar(i8 signext 97), !callee_type !1
18+
ret i32 %call.i
19+
}
20+
21+
declare !type !2 i32 @foo(i8 signext)
22+
23+
declare !type !2 i32 @bar(i8 signext)
24+
25+
!0 = !{i64 0, !"_ZTSFiPvcE.generalized"}
26+
!1 = !{!2}
27+
!2 = !{i64 0, !"_ZTSFicE.generalized"}
28+
!3 = !{i64 0, !"_ZTSFiiE.generalized"}
29+
30+
; CHECK: Hex dump of section '.callgraph':
31+
; CHECK-NEXT: 0x00000000 00050000 00008e19 0b7f3326 e3000154
32+
; CHECK-NEXT: 0x00000010 86bc5981 4b8e3000 05100000 00a150b8
33+
;; Verify that the type id 0x308e4b8159bc8654 is in section.
34+
; CHECK-NEXT: 0x00000020 3e0cfe3c b2015486 bc59814b 8e30
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
;; Tests that we store the type identifiers in .callgraph section of the object file.
2+
3+
; RUN: llc -mtriple=arm-unknown-linux --call-graph-section -filetype=obj -o - < %s | \
4+
; RUN: llvm-readelf -x .callgraph - | FileCheck %s
5+
6+
declare !type !0 void @foo()
7+
8+
declare !type !1 i32 @bar(i8)
9+
10+
declare !type !2 ptr @baz(ptr)
11+
12+
define void @main() {
13+
entry:
14+
%fp_foo_val = load ptr, ptr null, align 8
15+
call void (...) %fp_foo_val(), !callee_type !1
16+
%fp_bar_val = load ptr, ptr null, align 8
17+
%call_fp_bar = call i32 %fp_bar_val(i8 0), !callee_type !3
18+
%fp_baz_val = load ptr, ptr null, align 8
19+
%call_fp_baz = call ptr %fp_baz_val(ptr null), !callee_type !4
20+
ret void
21+
}
22+
23+
;; Check that the numeric type id (md5 hash) for the below type ids are emitted
24+
;; to the callgraph section.
25+
!0 = !{i64 0, !"_ZTSFvE.generalized"}
26+
!1 = !{!0}
27+
!2 = !{i64 0, !"_ZTSFicE.generalized"}
28+
!3 = !{!2}
29+
!4 = !{!5}
30+
!5 = !{i64 0, !"_ZTSFPvS_E.generalized"}
31+
32+
;; Make sure following type IDs are in call graph section
33+
;; 0x5eecb3e2444f731f, 0x814b8e305486bc59, 0xf897fd777ade6814
34+
; CHECK: Hex dump of section '.callgraph':
35+
; CHECK-NEXT: 0x00000000 00050000 00000000 00000000 00000324
36+
; CHECK-NEXT: 0x00000010 44f731f5 eecb3e54 86bc5981 4b8e307a
37+
; CHECK-NEXT: 0x00000020 de6814f8 97fd77

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

Lines changed: 16 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,14 @@
44
;; call sites annotated with !callee_type metadata.
55
;; Test if the .callgraph section contains unique direct callees.
66

7-
; REQUIRES: x86-registered-target
8-
; REQUIRES: arm-registered-target
9-
10-
; RUN: llc -mtriple=x86_64-unknown-linux --call-graph-section -o - < %s | FileCheck --check-prefix=X64 %s
11-
; RUN: llc -mtriple=arm-unknown-linux --call-graph-section -o - < %s | FileCheck --check-prefix=ARM32 %s
7+
; RUN: llc -mtriple=x86_64-unknown-linux --call-graph-section -o - < %s | FileCheck %s
128

139
declare !type !0 void @direct_foo()
1410
declare !type !1 i32 @direct_bar(i8)
1511
declare !type !2 ptr @direct_baz(ptr)
1612

17-
; X64: ball:
18-
; X64-NEXT: [[LABEL_FUNC:\.Lfunc_begin[0-9]+]]:
19-
; ARM32: ball:
20-
; ARM32-NEXT: [[LABEL_FUNC:\.Lfunc_begin[0-9]+]]:
13+
; CHECK: ball:
14+
; CHECK-NEXT: [[LABEL_FUNC:\.Lfunc_begin[0-9]+]]:
2115
define ptr @ball() {
2216
entry:
2317
call void @direct_foo()
@@ -42,50 +36,24 @@ entry:
4236
!4 = !{!5}
4337
!5 = !{i64 0, !"_ZTSFPvS_E.generalized"}
4438

45-
; X64: .section .callgraph,"o",@progbits,.text
46-
;; Version
47-
; X64-NEXT: .byte 0
48-
;; Flags
49-
; X64-NEXT: .byte 7
50-
;; Function Entry PC
51-
; X64-NEXT: .quad [[LABEL_FUNC]]
52-
;; Function type ID -- set to 0 as no type metadata attached to function.
53-
; X64-NEXT: .quad 0
54-
;; Number of unique direct callees.
55-
; X64-NEXT: .byte 3
56-
;; Direct callees.
57-
; X64-NEXT: .quad direct_foo
58-
; X64-NEXT: .quad direct_bar
59-
; X64-NEXT: .quad direct_baz
60-
;; Number of unique indirect target type IDs.
61-
; X64-NEXT: .byte 3
62-
;; Indirect type IDs.
63-
; X64-NEXT: .quad 4524972987496481828
64-
; X64-NEXT: .quad 3498816979441845844
65-
; X64-NEXT: .quad 8646233951371320954
66-
67-
; ARM32: .section .callgraph,"o",%progbits,.text
39+
; CHECK: .section .callgraph,"o",@progbits,.text
6840
;; Version
69-
; ARM32-NEXT: .byte 0
41+
; CHECK-NEXT: .byte 0
7042
;; Flags
71-
; ARM32-NEXT: .byte 7
43+
; CHECK-NEXT: .byte 7
7244
;; Function Entry PC
73-
; ARM32-NEXT: .long [[LABEL_FUNC]]
45+
; CHECK-NEXT: .quad [[LABEL_FUNC]]
7446
;; Function type ID -- set to 0 as no type metadata attached to function.
75-
; ARM32-NEXT: .long 0
76-
; ARM32-NEXT: .long 0
47+
; CHECK-NEXT: .quad 0
7748
;; Number of unique direct callees.
78-
; ARM32-NEXT: .byte 3
49+
; CHECK-NEXT: .byte 3
7950
;; Direct callees.
80-
; ARM32-NEXT: .long direct_foo
81-
; ARM32-NEXT: .long direct_bar
82-
; ARM32-NEXT: .long direct_baz
51+
; CHECK-NEXT: .quad direct_foo
52+
; CHECK-NEXT: .quad direct_bar
53+
; CHECK-NEXT: .quad direct_baz
8354
;; Number of unique indirect target type IDs.
84-
; ARM32-NEXT: .byte 3
55+
; CHECK-NEXT: .byte 3
8556
;; Indirect type IDs.
86-
; ARM32-NEXT: .long 838288420
87-
; ARM32-NEXT: .long 1053552373
88-
; ARM32-NEXT: .long 1505527380
89-
; ARM32-NEXT: .long 814631809
90-
; ARM32-NEXT: .long 342417018
91-
; ARM32-NEXT: .long 2013108216
57+
; CHECK-NEXT: .quad 4524972987496481828
58+
; CHECK-NEXT: .quad 3498816979441845844
59+
; CHECK-NEXT: .quad 8646233951371320954

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

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
;; Tests that we store the type identifiers in .callgraph section of the object file for tailcalls.
22

3-
; REQUIRES: x86-registered-target
4-
; REQUIRES: arm-registered-target
5-
63
; RUN: llc -mtriple=x86_64-unknown-linux --call-graph-section -filetype=obj -o - < %s | \
7-
; RUN: llvm-readelf -x .callgraph - | FileCheck --check-prefix=X64 %s
8-
; RUN: llc -mtriple=arm-unknown-linux --call-graph-section -filetype=obj -o - < %s | \
9-
; RUN: llvm-readelf -x .callgraph - | FileCheck --check-prefix=ARM32 %s
4+
; RUN: llvm-readelf -x .callgraph - | FileCheck %s
105

116
define i32 @check_tailcall(ptr %func, i8 %x) !type !0 {
127
entry:
@@ -32,15 +27,9 @@ declare !type !2 i32 @bar(i8 signext)
3227
!2 = !{i64 0, !"_ZTSFicE.generalized"}
3328
!3 = !{i64 0, !"_ZTSFiiE.generalized"}
3429

35-
; X64: Hex dump of section '.callgraph':
36-
; X64-NEXT: 0x00000000 00050000 00000000 00008e19 0b7f3326
37-
; X64-NEXT: 0x00000010 e3000154 86bc5981 4b8e3000 05000000
38-
;; Verify that the type id 0x308e4b8159bc8654 is in section.
39-
; X64-NEXT: 0x00000020 00000000 00a150b8 3e0cfe3c b2015486
40-
; X64-NEXT: 0x00000030 bc59814b 8e30
41-
42-
; ARM32: Hex dump of section '.callgraph':
43-
; ARM32-NEXT: 0x00000000 00050000 00008e19 0b7f3326 e3000154
44-
; ARM32-NEXT: 0x00000010 86bc5981 4b8e3000 05100000 00a150b8
30+
; CHECK: Hex dump of section '.callgraph':
31+
; CHECK-NEXT: 0x00000000 00050000 00000000 00008e19 0b7f3326
32+
; CHECK-NEXT: 0x00000010 e3000154 86bc5981 4b8e3000 05000000
4533
;; Verify that the type id 0x308e4b8159bc8654 is in section.
46-
; ARM32-NEXT: 0x00000020 3e0cfe3c b2015486 bc59814b 8e30
34+
; CHECK-NEXT: 0x00000020 00000000 00a150b8 3e0cfe3c b2015486
35+
; CHECK-NEXT: 0x00000030 bc59814b 8e30

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

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
;; Tests that we store the type identifiers in .callgraph section of the object file.
22

3-
; REQUIRES: x86-registered-target
4-
; REQUIRES: arm-registered-target
5-
63
; RUN: llc -mtriple=x86_64-unknown-linux --call-graph-section -filetype=obj -o - < %s | \
7-
; RUN: llvm-readelf -x .callgraph - | FileCheck --check-prefix=X64 %s
8-
; RUN: llc -mtriple=arm-unknown-linux --call-graph-section -filetype=obj -o - < %s | \
9-
; RUN: llvm-readelf -x .callgraph - | FileCheck --check-prefix=ARM32 %s
4+
; RUN: llvm-readelf -x .callgraph - | FileCheck %s
105

116
declare !type !0 void @foo()
127

@@ -36,14 +31,7 @@ entry:
3631

3732
;; Make sure following type IDs are in call graph section
3833
;; 0x5eecb3e2444f731f, 0x814b8e305486bc59, 0xf897fd777ade6814
39-
; X64: Hex dump of section '.callgraph':
40-
; X64-NEXT: 0x00000000 00050000 00000000 00000000 00000000
41-
; X64-NEXT: 0x00000010 00000324 44f731f5 eecb3e54 86bc5981
42-
; X64-NEXT: 0x00000020 4b8e307a de6814f8 97fd77
43-
44-
;; Make sure following type IDs are in call graph section
45-
;; 0x5eecb3e2444f731f, 0x814b8e305486bc59, 0xf897fd777ade6814
46-
; ARM32: Hex dump of section '.callgraph':
47-
; ARM32-NEXT: 0x00000000 00050000 00000000 00000000 00000324
48-
; ARM32-NEXT: 0x00000010 44f731f5 eecb3e54 86bc5981 4b8e307a
49-
; ARM32-NEXT: 0x00000020 de6814f8 97fd77
34+
; CHECK: Hex dump of section '.callgraph':
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)