[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
This commit is contained in:
Yonghong Song 2019-12-06 23:00:41 -08:00
parent d714aa0dfd
commit 5ea611daf9
3 changed files with 137 additions and 5 deletions

View File

@ -1092,7 +1092,7 @@ void BTFDebug::processGlobals(bool ProcessingMapDef) {
// Only support the following globals:
// . static variables
// . non-static global variables
// . non-static weak or non-weak global variables
// Essentially means:
// . .bcc/.data/.rodata DataSec entities only contain static data
// . Other DataSec entities contain static or initialized global data.
@ -1101,12 +1101,13 @@ void BTFDebug::processGlobals(bool ProcessingMapDef) {
// corresponding ELF section flags.
auto Linkage = Global.getLinkage();
if (Linkage != GlobalValue::InternalLinkage &&
Linkage != GlobalValue::ExternalLinkage)
Linkage != GlobalValue::ExternalLinkage &&
Linkage != GlobalValue::WeakAnyLinkage)
continue;
uint32_t GVarInfo = Linkage == GlobalValue::ExternalLinkage
? BTF::VAR_GLOBAL_ALLOCATED
: BTF::VAR_STATIC;
uint32_t GVarInfo = Linkage == GlobalValue::InternalLinkage
? BTF::VAR_STATIC
: BTF::VAR_GLOBAL_ALLOCATED;
auto VarEntry =
std::make_unique<BTFKindVar>(Global.getName(), GVTypeId, GVarInfo);
uint32_t VarId = addType(std::move(VarEntry));

View File

@ -0,0 +1,66 @@
; RUN: llc -march=bpfel -filetype=asm -o - %s | FileCheck -check-prefixes=CHECK %s
; RUN: llc -march=bpfeb -filetype=asm -o - %s | FileCheck -check-prefixes=CHECK %s
;
; Source code:
; char g __attribute__((weak)) = 2;
; int test() {
; return g;
; }
; Compilation flag:
; clang -target bpf -O2 -g -S -emit-llvm test.c
@g = weak dso_local local_unnamed_addr global i8 2, align 1, !dbg !0
; Function Attrs: norecurse nounwind readonly
define dso_local i32 @test() local_unnamed_addr #0 !dbg !11 {
entry:
%0 = load i8, i8* @g, align 1, !dbg !15, !tbaa !16
%conv = sext i8 %0 to i32, !dbg !15
ret i32 %conv, !dbg !19
}
; CHECK: .long 55 # BTF_KIND_INT(id = 4)
; CHECK-NEXT: .long 16777216 # 0x1000000
; CHECK-NEXT: .long 1
; CHECK-NEXT: .long 16777224 # 0x1000008
; CHECK-NEXT: .long 60 # BTF_KIND_VAR(id = 5)
; CHECK-NEXT: .long 234881024 # 0xe000000
; CHECK-NEXT: .long 4
; CHECK-NEXT: .long 1
; CHECK-NEXT: .long 62 # BTF_KIND_DATASEC(id = 6)
; CHECK-NEXT: .long 251658241 # 0xf000001
; CHECK-NEXT: .long 0
; CHECK-NEXT: .long 5
; CHECK-NEXT: .long g
; CHECK-NEXT: .long 1
; CHECK: .ascii "char" # string offset=55
; CHECK: .byte 103 # string offset=60
; CHECK: .ascii ".data" # string offset=62
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" }
!llvm.dbg.cu = !{!2}
!llvm.module.flags = !{!7, !8, !9}
!llvm.ident = !{!10}
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
!1 = distinct !DIGlobalVariable(name: "g", scope: !2, file: !3, line: 1, type: !6, isLocal: false, isDefinition: true)
!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)
!3 = !DIFile(filename: "test.c", directory: "/tmp/home/yhs/work/tests/global")
!4 = !{}
!5 = !{!0}
!6 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
!7 = !{i32 7, !"Dwarf Version", i32 4}
!8 = !{i32 2, !"Debug Info Version", i32 3}
!9 = !{i32 1, !"wchar_size", i32 4}
!10 = !{!"clang version 10.0.0 (https://github.com/llvm/llvm-project.git edf6717d8d30034da932b95350898e03c90a5082)"}
!11 = distinct !DISubprogram(name: "test", scope: !3, file: !3, line: 2, type: !12, scopeLine: 2, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !4)
!12 = !DISubroutineType(types: !13)
!13 = !{!14}
!14 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!15 = !DILocation(line: 3, column: 10, scope: !11)
!16 = !{!17, !17, i64 0}
!17 = !{!"omnipotent char", !18, i64 0}
!18 = !{!"Simple C/C++ TBAA"}
!19 = !DILocation(line: 3, column: 3, scope: !11)

View File

@ -0,0 +1,65 @@
; RUN: llc -march=bpfel -filetype=asm -o - %s | FileCheck -check-prefixes=CHECK %s
; RUN: llc -march=bpfeb -filetype=asm -o - %s | FileCheck -check-prefixes=CHECK %s
;
; Source code:
; char g __attribute__((weak));
; int test() {
; return g;
; }
; Compilation flag:
; clang -target bpf -O2 -g -S -emit-llvm test.c
@g = weak dso_local local_unnamed_addr global i8 0, align 1, !dbg !0
; Function Attrs: norecurse nounwind readonly
define dso_local i32 @test() local_unnamed_addr #0 !dbg !11 {
entry:
%0 = load i8, i8* @g, align 1, !dbg !15, !tbaa !16
%conv = sext i8 %0 to i32, !dbg !15
ret i32 %conv, !dbg !19
}
; CHECK: .long 55 # BTF_KIND_INT(id = 4)
; CHECK-NEXT: .long 16777216 # 0x1000000
; CHECK-NEXT: .long 1
; CHECK-NEXT: .long 16777224 # 0x1000008
; CHECK-NEXT: .long 60 # BTF_KIND_VAR(id = 5)
; CHECK-NEXT: .long 234881024 # 0xe000000
; CHECK-NEXT: .long 4
; CHECK-NEXT: .long 1
; CHECK-NEXT: .long 62 # BTF_KIND_DATASEC(id = 6)
; CHECK-NEXT: .long 251658241 # 0xf000001
; CHECK-NEXT: .long 0
; CHECK-NEXT: .long 5
; CHECK-NEXT: .long g
; CHECK-NEXT: .long 1
; CHECK: .ascii "char" # string offset=55
; CHECK: .byte 103 # string offset=60
; CHECK: .ascii ".bss" # string offset=62
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" }
!llvm.dbg.cu = !{!2}
!llvm.module.flags = !{!7, !8, !9}
!llvm.ident = !{!10}
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
!1 = distinct !DIGlobalVariable(name: "g", scope: !2, file: !3, line: 1, type: !6, isLocal: false, isDefinition: true)
!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)
!3 = !DIFile(filename: "test.c", directory: "/tmp/home/yhs/work/tests/global")
!4 = !{}
!5 = !{!0}
!6 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
!7 = !{i32 7, !"Dwarf Version", i32 4}
!8 = !{i32 2, !"Debug Info Version", i32 3}
!9 = !{i32 1, !"wchar_size", i32 4}
!10 = !{!"clang version 10.0.0 (https://github.com/llvm/llvm-project.git edf6717d8d30034da932b95350898e03c90a5082)"}
!11 = distinct !DISubprogram(name: "test", scope: !3, file: !3, line: 2, type: !12, scopeLine: 2, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !4)
!12 = !DISubroutineType(types: !13)
!13 = !{!14}
!14 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!15 = !DILocation(line: 3, column: 10, scope: !11)
!16 = !{!17, !17, i64 0}
!17 = !{!"omnipotent char", !18, i64 0}
!18 = !{!"Simple C/C++ TBAA"}
!19 = !DILocation(line: 3, column: 3, scope: !11)