Skip to content
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

llvm: emit metadata for exported global variables #15349

Merged
merged 2 commits into from
Apr 20, 2023

Conversation

tw4452852
Copy link
Contributor

One use case is to genearte BTF information from global variable's metadata.

One use case is to genearte BTF information from global variable's metadata.

Signed-off-by: Tw <weii.tan>
@squeek502
Copy link
Collaborator

squeek502 commented Apr 18, 2023

By chance could this affect/fix #15095?

@tw4452852
Copy link
Contributor Author

@squeek502 I think so. Because this patch adds the missing connection between variable declearation and its debug information.

@tw4452852 tw4452852 closed this Apr 19, 2023
@tw4452852 tw4452852 reopened this Apr 19, 2023
@tw4452852
Copy link
Contributor Author

tw4452852 commented Apr 19, 2023

Close by accident, reopen.

@tw4452852 tw4452852 changed the title llvm: emit metadata for global variable llvm: emit metadata for exported global variables Apr 20, 2023
@Vexu
Copy link
Member

Vexu commented Apr 20, 2023

Doesn't seem to do anything for #15095

@Vexu Vexu enabled auto-merge (squash) April 20, 2023 09:44
@Vexu Vexu merged commit 31b6d14 into ziglang:master Apr 20, 2023
10 checks passed
@andrewrk
Copy link
Member

Can you please show the difference in LLVM IR of this change as well as some clang generated IR for comparison?

@tw4452852
Copy link
Contributor Author

tw4452852 commented Apr 21, 2023

@andrewrk Sure, here it is:

~/t/test/src> cat foo.zig 
export var bar: u32 = 0;

Before:

~/t/test/src> zig build-obj -target bpfel-freestanding -O ReleaseSafe foo.zig   -fno-emit-bin -femit-llvm-ir=- 
LLVM Emit Object... ; ModuleID = 'foo'
source_filename = "foo"
target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
target triple = "bpfel-unknown-unknown-eabi"

@bar = dso_local local_unnamed_addr global i32 0, align 4

!llvm.module.flags = !{!0, !1}
!llvm.dbg.cu = !{!2}

!0 = !{i32 2, !"Debug Info Version", i32 3}
!1 = !{i32 2, !"Dwarf Version", i32 4}
!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "zig 0.11.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !26)
!3 = !DIFile(filename: "foo", directory: "/home/tw/t/test/src")
!4 = !{!5, !20}
!5 = !DICompositeType(tag: DW_TAG_enumeration_type, name: "builtin.CompilerBackend", scope: !6, file: !6, line: 1545, baseType: !7, size: 64, align: 64, elements: !8)
!6 = !DIFile(filename: "builtin.zig", directory: "/home/tw/code/zig/stage3/lib/zig/std")
!7 = !DIBasicType(name: "u64", size: 64, encoding: DW_ATE_unsigned)
!8 = !{!9, !10, !11, !12, !13, !14, !15, !16, !17, !18, !19}
!9 = !DIEnumerator(name: "other", value: 0, isUnsigned: true)
!10 = !DIEnumerator(name: "stage1", value: 1, isUnsigned: true)
!11 = !DIEnumerator(name: "stage2_llvm", value: 2, isUnsigned: true)
!12 = !DIEnumerator(name: "stage2_c", value: 3, isUnsigned: true)
!13 = !DIEnumerator(name: "stage2_wasm", value: 4, isUnsigned: true)
!14 = !DIEnumerator(name: "stage2_arm", value: 5, isUnsigned: true)
!15 = !DIEnumerator(name: "stage2_x86_64", value: 6, isUnsigned: true)
!16 = !DIEnumerator(name: "stage2_aarch64", value: 7, isUnsigned: true)
!17 = !DIEnumerator(name: "stage2_x86", value: 8, isUnsigned: true)
!18 = !DIEnumerator(name: "stage2_riscv64", value: 9, isUnsigned: true)
!19 = !DIEnumerator(name: "stage2_sparc64", value: 10, isUnsigned: true)
!20 = !DICompositeType(tag: DW_TAG_enumeration_type, name: "builtin.OutputMode", scope: !6, file: !6, line: 642, baseType: !21, size: 8, align: 8, elements: !22)
!21 = !DIBasicType(name: "u2", size: 8, encoding: DW_ATE_unsigned)
!22 = !{!23, !24, !25}
!23 = !DIEnumerator(name: "Exe", value: 0, isUnsigned: true)
!24 = !DIEnumerator(name: "Lib", value: 1, isUnsigned: true)
!25 = !DIEnumerator(name: "Obj", value: 2, isUnsigned: true)
!26 = !{!27, !30, !34, !36}
!27 = !DIGlobalVariableExpression(var: !28, expr: !DIExpression())
!28 = distinct !DIGlobalVariable(name: "zig_backend", linkageName: "builtin.zig_backend", scope: !29, file: !29, line: 6, type: !5, isLocal: true, isDefinition: true)
!29 = !DIFile(filename: "builtin.zig", directory: "/home/tw/t/test/zig-cache/o/c1221be412c7bc2df05b39d0beeb4049")
!30 = !DIGlobalVariableExpression(var: !31, expr: !DIExpression())
!31 = distinct !DIGlobalVariable(name: "simplified_logic", linkageName: "start.simplified_logic", scope: !32, file: !32, line: 20, type: !33, isLocal: true, isDefinition: true)
!32 = !DIFile(filename: "start.zig", directory: "/home/tw/code/zig/stage3/lib/zig/std")
!33 = !DIBasicType(name: "bool", size: 8, encoding: DW_ATE_boolean)
!34 = !DIGlobalVariableExpression(var: !35, expr: !DIExpression())
!35 = distinct !DIGlobalVariable(name: "output_mode", linkageName: "builtin.output_mode", scope: !29, file: !29, line: 8, type: !20, isLocal: true, isDefinition: true)
!36 = !DIGlobalVariableExpression(var: !37, expr: !DIExpression())
!37 = distinct !DIGlobalVariable(name: "bar", linkageName: "bar", scope: !38, file: !38, line: 1, type: !39, isLocal: false, isDefinition: true)
!38 = !DIFile(filename: "foo.zig", directory: "/home/tw/t/test/src")
!39 = !DIBasicType(name: "u32", size: 32, encoding: DW_ATE_unsigned)

After:

~/t/test/src> zig build-obj -target bpfel-freestanding -O ReleaseSafe foo.zig   -fno-emit-bin -femit-llvm-ir=- 
LLVM Emit Object... ; ModuleID = 'foo'
source_filename = "foo"
target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
target triple = "bpfel-unknown-unknown-eabi"

@bar = dso_local local_unnamed_addr global i32 0, align 4, !dbg !0

!llvm.module.flags = !{!4, !5}
!llvm.dbg.cu = !{!6}

!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
!1 = distinct !DIGlobalVariable(name: "bar", linkageName: "bar", scope: !2, file: !2, line: 1, type: !3, isLocal: false, isDefinition: true)
!2 = !DIFile(filename: "foo.zig", directory: "/home/tw/t/test/src")
!3 = !DIBasicType(name: "u32", size: 32, encoding: DW_ATE_unsigned)
!4 = !{i32 2, !"Debug Info Version", i32 3}
!5 = !{i32 2, !"Dwarf Version", i32 4}
!6 = distinct !DICompileUnit(language: DW_LANG_C99, file: !7, producer: "zig 0.11.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !8, globals: !30)
!7 = !DIFile(filename: "foo", directory: "/home/tw/t/test/src")
!8 = !{!9, !24}
!9 = !DICompositeType(tag: DW_TAG_enumeration_type, name: "builtin.CompilerBackend", scope: !10, file: !10, line: 1545, baseType: !11, size: 64, align: 64, elements: !12)
!10 = !DIFile(filename: "builtin.zig", directory: "/home/tw/code/zig/stage3/lib/zig/std")
!11 = !DIBasicType(name: "u64", size: 64, encoding: DW_ATE_unsigned)
!12 = !{!13, !14, !15, !16, !17, !18, !19, !20, !21, !22, !23}
!13 = !DIEnumerator(name: "other", value: 0, isUnsigned: true)
!14 = !DIEnumerator(name: "stage1", value: 1, isUnsigned: true)
!15 = !DIEnumerator(name: "stage2_llvm", value: 2, isUnsigned: true)
!16 = !DIEnumerator(name: "stage2_c", value: 3, isUnsigned: true)
!17 = !DIEnumerator(name: "stage2_wasm", value: 4, isUnsigned: true)
!18 = !DIEnumerator(name: "stage2_arm", value: 5, isUnsigned: true)
!19 = !DIEnumerator(name: "stage2_x86_64", value: 6, isUnsigned: true)
!20 = !DIEnumerator(name: "stage2_aarch64", value: 7, isUnsigned: true)
!21 = !DIEnumerator(name: "stage2_x86", value: 8, isUnsigned: true)
!22 = !DIEnumerator(name: "stage2_riscv64", value: 9, isUnsigned: true)
!23 = !DIEnumerator(name: "stage2_sparc64", value: 10, isUnsigned: true)
!24 = !DICompositeType(tag: DW_TAG_enumeration_type, name: "builtin.OutputMode", scope: !10, file: !10, line: 642, baseType: !25, size: 8, align: 8, elements: !26)
!25 = !DIBasicType(name: "u2", size: 8, encoding: DW_ATE_unsigned)
!26 = !{!27, !28, !29}
!27 = !DIEnumerator(name: "Exe", value: 0, isUnsigned: true)
!28 = !DIEnumerator(name: "Lib", value: 1, isUnsigned: true)
!29 = !DIEnumerator(name: "Obj", value: 2, isUnsigned: true)
!30 = !{!31, !34, !38, !0}
!31 = !DIGlobalVariableExpression(var: !32, expr: !DIExpression())
!32 = distinct !DIGlobalVariable(name: "zig_backend", linkageName: "builtin.zig_backend", scope: !33, file: !33, line: 6, type: !9, isLocal: true, isDefinition: true)
!33 = !DIFile(filename: "builtin.zig", directory: "/home/tw/t/test/zig-cache/o/95f34795906157beed77417e714d3875")
!34 = !DIGlobalVariableExpression(var: !35, expr: !DIExpression())
!35 = distinct !DIGlobalVariable(name: "simplified_logic", linkageName: "start.simplified_logic", scope: !36, file: !36, line: 20, type: !37, isLocal: true, isDefinition: true)
!36 = !DIFile(filename: "start.zig", directory: "/home/tw/code/zig/stage3/lib/zig/std")
!37 = !DIBasicType(name: "bool", size: 8, encoding: DW_ATE_boolean)
!38 = !DIGlobalVariableExpression(var: !39, expr: !DIExpression())
!39 = distinct !DIGlobalVariable(name: "output_mode", linkageName: "builtin.output_mode", scope: !33, file: !33, line: 8, type: !24, isLocal: true, isDefinition: true)

As you can see, the metadata for bar is appended after this change.

Clang:

~/t/test/src> cat foo.c
unsigned int bar = 0;
~/t/test/src> 
~/t/test/src> clang -g -O2 -target bpf -S foo.c -emit-llvm -o -
; ModuleID = 'foo.c'
source_filename = "foo.c"
target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
target triple = "bpf"

@bar = dso_local local_unnamed_addr global i32 0, align 4, !dbg !0

!llvm.dbg.cu = !{!2}
!llvm.module.flags = !{!7, !8, !9}
!llvm.ident = !{!10}

!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
!1 = distinct !DIGlobalVariable(name: "bar", scope: !2, file: !3, line: 1, type: !6, isLocal: false, isDefinition: true)
!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 12.0.1", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5, splitDebugInlining: false, nameTableKind: None)
!3 = !DIFile(filename: "foo.c", directory: "/home/tw/t/test/src")
!4 = !{}
!5 = !{!0}
!6 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned)
!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 12.0.1"}

@tw4452852 tw4452852 deleted the llvm_global_variable_metadata branch April 21, 2023 01:49
@andrewrk
Copy link
Member

andrewrk commented Apr 24, 2023

Thanks @tw4452852! Looks great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants