Skip to content

Commit 5ea611d

Browse files
committed
[BPF] Support weak global variables for BTF
Generate types for global variables with "weak" attribute. Keep allocation scope the same for both weak and non-weak globals as ELF symbol table can determine whether a global symbol is weak or not. Differential Revision: https://reviews.llvm.org/D71162
1 parent d714aa0 commit 5ea611d

File tree

3 files changed

+137
-5
lines changed

3 files changed

+137
-5
lines changed

llvm/lib/Target/BPF/BTFDebug.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ void BTFDebug::processGlobals(bool ProcessingMapDef) {
10921092

10931093
// Only support the following globals:
10941094
// . static variables
1095-
// . non-static global variables
1095+
// . non-static weak or non-weak global variables
10961096
// Essentially means:
10971097
// . .bcc/.data/.rodata DataSec entities only contain static data
10981098
// . Other DataSec entities contain static or initialized global data.
@@ -1101,12 +1101,13 @@ void BTFDebug::processGlobals(bool ProcessingMapDef) {
11011101
// corresponding ELF section flags.
11021102
auto Linkage = Global.getLinkage();
11031103
if (Linkage != GlobalValue::InternalLinkage &&
1104-
Linkage != GlobalValue::ExternalLinkage)
1104+
Linkage != GlobalValue::ExternalLinkage &&
1105+
Linkage != GlobalValue::WeakAnyLinkage)
11051106
continue;
11061107

1107-
uint32_t GVarInfo = Linkage == GlobalValue::ExternalLinkage
1108-
? BTF::VAR_GLOBAL_ALLOCATED
1109-
: BTF::VAR_STATIC;
1108+
uint32_t GVarInfo = Linkage == GlobalValue::InternalLinkage
1109+
? BTF::VAR_STATIC
1110+
: BTF::VAR_GLOBAL_ALLOCATED;
11101111
auto VarEntry =
11111112
std::make_unique<BTFKindVar>(Global.getName(), GVTypeId, GVarInfo);
11121113
uint32_t VarId = addType(std::move(VarEntry));
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
; RUN: llc -march=bpfel -filetype=asm -o - %s | FileCheck -check-prefixes=CHECK %s
2+
; RUN: llc -march=bpfeb -filetype=asm -o - %s | FileCheck -check-prefixes=CHECK %s
3+
;
4+
; Source code:
5+
; char g __attribute__((weak)) = 2;
6+
; int test() {
7+
; return g;
8+
; }
9+
; Compilation flag:
10+
; clang -target bpf -O2 -g -S -emit-llvm test.c
11+
12+
@g = weak dso_local local_unnamed_addr global i8 2, align 1, !dbg !0
13+
; Function Attrs: norecurse nounwind readonly
14+
define dso_local i32 @test() local_unnamed_addr #0 !dbg !11 {
15+
entry:
16+
%0 = load i8, i8* @g, align 1, !dbg !15, !tbaa !16
17+
%conv = sext i8 %0 to i32, !dbg !15
18+
ret i32 %conv, !dbg !19
19+
}
20+
21+
; CHECK: .long 55 # BTF_KIND_INT(id = 4)
22+
; CHECK-NEXT: .long 16777216 # 0x1000000
23+
; CHECK-NEXT: .long 1
24+
; CHECK-NEXT: .long 16777224 # 0x1000008
25+
; CHECK-NEXT: .long 60 # BTF_KIND_VAR(id = 5)
26+
; CHECK-NEXT: .long 234881024 # 0xe000000
27+
; CHECK-NEXT: .long 4
28+
; CHECK-NEXT: .long 1
29+
; CHECK-NEXT: .long 62 # BTF_KIND_DATASEC(id = 6)
30+
; CHECK-NEXT: .long 251658241 # 0xf000001
31+
; CHECK-NEXT: .long 0
32+
; CHECK-NEXT: .long 5
33+
; CHECK-NEXT: .long g
34+
; CHECK-NEXT: .long 1
35+
36+
; CHECK: .ascii "char" # string offset=55
37+
; CHECK: .byte 103 # string offset=60
38+
; CHECK: .ascii ".data" # string offset=62
39+
40+
41+
attributes #0 = { norecurse nounwind readonly "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
42+
43+
!llvm.dbg.cu = !{!2}
44+
!llvm.module.flags = !{!7, !8, !9}
45+
!llvm.ident = !{!10}
46+
47+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
48+
!1 = distinct !DIGlobalVariable(name: "g", scope: !2, file: !3, line: 1, type: !6, isLocal: false, isDefinition: true)
49+
!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 10.0.0 (https://github.com/llvm/llvm-project.git edf6717d8d30034da932b95350898e03c90a5082)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5, nameTableKind: None)
50+
!3 = !DIFile(filename: "test.c", directory: "/tmp/home/yhs/work/tests/global")
51+
!4 = !{}
52+
!5 = !{!0}
53+
!6 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
54+
!7 = !{i32 7, !"Dwarf Version", i32 4}
55+
!8 = !{i32 2, !"Debug Info Version", i32 3}
56+
!9 = !{i32 1, !"wchar_size", i32 4}
57+
!10 = !{!"clang version 10.0.0 (https://github.com/llvm/llvm-project.git edf6717d8d30034da932b95350898e03c90a5082)"}
58+
!11 = distinct !DISubprogram(name: "test", scope: !3, file: !3, line: 2, type: !12, scopeLine: 2, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !4)
59+
!12 = !DISubroutineType(types: !13)
60+
!13 = !{!14}
61+
!14 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
62+
!15 = !DILocation(line: 3, column: 10, scope: !11)
63+
!16 = !{!17, !17, i64 0}
64+
!17 = !{!"omnipotent char", !18, i64 0}
65+
!18 = !{!"Simple C/C++ TBAA"}
66+
!19 = !DILocation(line: 3, column: 3, scope: !11)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
; RUN: llc -march=bpfel -filetype=asm -o - %s | FileCheck -check-prefixes=CHECK %s
2+
; RUN: llc -march=bpfeb -filetype=asm -o - %s | FileCheck -check-prefixes=CHECK %s
3+
;
4+
; Source code:
5+
; char g __attribute__((weak));
6+
; int test() {
7+
; return g;
8+
; }
9+
; Compilation flag:
10+
; clang -target bpf -O2 -g -S -emit-llvm test.c
11+
12+
@g = weak dso_local local_unnamed_addr global i8 0, align 1, !dbg !0
13+
; Function Attrs: norecurse nounwind readonly
14+
define dso_local i32 @test() local_unnamed_addr #0 !dbg !11 {
15+
entry:
16+
%0 = load i8, i8* @g, align 1, !dbg !15, !tbaa !16
17+
%conv = sext i8 %0 to i32, !dbg !15
18+
ret i32 %conv, !dbg !19
19+
}
20+
21+
; CHECK: .long 55 # BTF_KIND_INT(id = 4)
22+
; CHECK-NEXT: .long 16777216 # 0x1000000
23+
; CHECK-NEXT: .long 1
24+
; CHECK-NEXT: .long 16777224 # 0x1000008
25+
; CHECK-NEXT: .long 60 # BTF_KIND_VAR(id = 5)
26+
; CHECK-NEXT: .long 234881024 # 0xe000000
27+
; CHECK-NEXT: .long 4
28+
; CHECK-NEXT: .long 1
29+
; CHECK-NEXT: .long 62 # BTF_KIND_DATASEC(id = 6)
30+
; CHECK-NEXT: .long 251658241 # 0xf000001
31+
; CHECK-NEXT: .long 0
32+
; CHECK-NEXT: .long 5
33+
; CHECK-NEXT: .long g
34+
; CHECK-NEXT: .long 1
35+
36+
; CHECK: .ascii "char" # string offset=55
37+
; CHECK: .byte 103 # string offset=60
38+
; CHECK: .ascii ".bss" # string offset=62
39+
40+
attributes #0 = { norecurse nounwind readonly "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
41+
42+
!llvm.dbg.cu = !{!2}
43+
!llvm.module.flags = !{!7, !8, !9}
44+
!llvm.ident = !{!10}
45+
46+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
47+
!1 = distinct !DIGlobalVariable(name: "g", scope: !2, file: !3, line: 1, type: !6, isLocal: false, isDefinition: true)
48+
!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 10.0.0 (https://github.com/llvm/llvm-project.git edf6717d8d30034da932b95350898e03c90a5082)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5, nameTableKind: None)
49+
!3 = !DIFile(filename: "test.c", directory: "/tmp/home/yhs/work/tests/global")
50+
!4 = !{}
51+
!5 = !{!0}
52+
!6 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
53+
!7 = !{i32 7, !"Dwarf Version", i32 4}
54+
!8 = !{i32 2, !"Debug Info Version", i32 3}
55+
!9 = !{i32 1, !"wchar_size", i32 4}
56+
!10 = !{!"clang version 10.0.0 (https://github.com/llvm/llvm-project.git edf6717d8d30034da932b95350898e03c90a5082)"}
57+
!11 = distinct !DISubprogram(name: "test", scope: !3, file: !3, line: 2, type: !12, scopeLine: 2, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !4)
58+
!12 = !DISubroutineType(types: !13)
59+
!13 = !{!14}
60+
!14 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
61+
!15 = !DILocation(line: 3, column: 10, scope: !11)
62+
!16 = !{!17, !17, i64 0}
63+
!17 = !{!"omnipotent char", !18, i64 0}
64+
!18 = !{!"Simple C/C++ TBAA"}
65+
!19 = !DILocation(line: 3, column: 3, scope: !11)

0 commit comments

Comments
 (0)