xref: /llvm-project/llvm/test/CodeGen/BPF/CORE/anon-union-localvar-attr.ll (revision 6e6c1efe04d45b717091e06eec94f0eef64839b1)
1; RUN: opt -O2 -mtriple=bpf-pc-linux %s | llvm-dis > %t1
2; RUN: llc -o - %t1 | FileCheck %s
3;
4; Source:
5;   typedef union {
6;       struct {
7;               void	*kernel;
8;               void	*user;
9;       };
10;       unsigned is_kernel : 1;
11;   } __attribute__((preserve_access_index)) sockptr_t;
12;   void *foo(void);
13;   int test() {
14;     sockptr_t *arg = foo();
15;     return __builtin_preserve_field_info(arg->is_kernel, 1);
16;   }
17; Compilation flag:
18;   clang -target bpf -O2 -S -emit-llvm -g -Xclang -disable-llvm-passes test.c
19
20%union.sockptr_t = type { %struct.anon }
21%struct.anon = type { ptr, ptr }
22
23; Function Attrs: nounwind
24define dso_local i32 @test() #0 !dbg !7 {
25entry:
26  %arg = alloca ptr, align 8
27  call void @llvm.lifetime.start.p0(i64 8, ptr %arg) #6, !dbg !25
28  call void @llvm.dbg.declare(metadata ptr %arg, metadata !12, metadata !DIExpression()), !dbg !26
29  %call = call ptr @foo(), !dbg !27
30  store ptr %call, ptr %arg, align 8, !dbg !26, !tbaa !28
31  %0 = load ptr, ptr %arg, align 8, !dbg !32, !tbaa !28
32  %1 = call ptr @llvm.preserve.struct.access.index.p0.p0(ptr elementtype(%union.sockptr_t) %0, i32 0, i32 1), !dbg !33, !llvm.preserve.access.index !15
33  %2 = call i32 @llvm.bpf.preserve.field.info.p0(ptr %1, i64 1), !dbg !34
34  call void @llvm.lifetime.end.p0(i64 8, ptr %arg) #6, !dbg !35
35  ret i32 %2, !dbg !36
36}
37
38; CHECK:             .long   56                              # BTF_KIND_TYPEDEF(id = 7)
39; CHECK-NEXT:        .long   134217728                       # 0x8000000
40; CHECK-NEXT:        .long   8
41; CHECK-NEXT:        .long   0                               # BTF_KIND_UNION(id = 8)
42
43; CHECK:             .ascii  ".text"                         # string offset=10
44; CHECK:             .ascii  "sockptr_t"                     # string offset=56
45; CHECK:             .ascii  "0:1"                           # string offset=101
46
47; CHECK:             .long   16                              # FieldReloc
48; CHECK-NEXT:        .long   10                              # Field reloc section string offset=10
49; CHECK-NEXT:        .long   1
50; CHECK-NEXT:        .long   .Ltmp[[#]]
51; CHECK-NEXT:        .long   7
52; CHECK-NEXT:        .long   101
53; CHECK-NEXT:        .long   1
54
55; Function Attrs: argmemonly nocallback nofree nosync nounwind willreturn
56declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1
57
58; Function Attrs: nocallback nofree nosync nounwind readnone speculatable willreturn
59declare void @llvm.dbg.declare(metadata, metadata, metadata) #2
60
61declare !dbg !37 dso_local ptr @foo() #3
62
63; Function Attrs: nocallback nofree nosync nounwind readnone willreturn
64declare ptr @llvm.preserve.struct.access.index.p0.p0(ptr, i32 immarg, i32 immarg) #4
65
66; Function Attrs: nounwind readnone
67declare i32 @llvm.bpf.preserve.field.info.p0(ptr, i64 immarg) #5
68
69; Function Attrs: argmemonly nocallback nofree nosync nounwind willreturn
70declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1
71
72attributes #0 = { nounwind "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
73attributes #1 = { argmemonly nocallback nofree nosync nounwind willreturn }
74attributes #2 = { nocallback nofree nosync nounwind readnone speculatable willreturn }
75attributes #3 = { "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
76attributes #4 = { nocallback nofree nosync nounwind readnone willreturn }
77attributes #5 = { nounwind readnone }
78attributes #6 = { nounwind }
79
80!llvm.dbg.cu = !{!0}
81!llvm.module.flags = !{!2, !3, !4, !5}
82!llvm.ident = !{!6}
83
84!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 15.0.0 (https://github.com/llvm/llvm-project.git 8c7d5118961e7ffc0304126ec2122d21e2eb1f79)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
85!1 = !DIFile(filename: "test.c", directory: "/tmp/home/yhs/work/test/anon", checksumkind: CSK_MD5, checksum: "03904da5c5e53b14bb1effb6d9d5e025")
86!2 = !{i32 7, !"Dwarf Version", i32 5}
87!3 = !{i32 2, !"Debug Info Version", i32 3}
88!4 = !{i32 1, !"wchar_size", i32 4}
89!5 = !{i32 7, !"frame-pointer", i32 2}
90!6 = !{!"clang version 15.0.0 (https://github.com/llvm/llvm-project.git 8c7d5118961e7ffc0304126ec2122d21e2eb1f79)"}
91!7 = distinct !DISubprogram(name: "test", scope: !1, file: !1, line: 9, type: !8, scopeLine: 9, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !11)
92!8 = !DISubroutineType(types: !9)
93!9 = !{!10}
94!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
95!11 = !{!12}
96!12 = !DILocalVariable(name: "arg", scope: !7, file: !1, line: 10, type: !13)
97!13 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !14, size: 64)
98!14 = !DIDerivedType(tag: DW_TAG_typedef, name: "sockptr_t", file: !1, line: 7, baseType: !15)
99!15 = distinct !DICompositeType(tag: DW_TAG_union_type, file: !1, line: 1, size: 128, elements: !16)
100!16 = !{!17, !23}
101!17 = !DIDerivedType(tag: DW_TAG_member, scope: !15, file: !1, line: 2, baseType: !18, size: 128)
102!18 = distinct !DICompositeType(tag: DW_TAG_structure_type, scope: !15, file: !1, line: 2, size: 128, elements: !19)
103!19 = !{!20, !22}
104!20 = !DIDerivedType(tag: DW_TAG_member, name: "kernel", scope: !18, file: !1, line: 3, baseType: !21, size: 64)
105!21 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64)
106!22 = !DIDerivedType(tag: DW_TAG_member, name: "user", scope: !18, file: !1, line: 4, baseType: !21, size: 64, offset: 64)
107!23 = !DIDerivedType(tag: DW_TAG_member, name: "is_kernel", scope: !15, file: !1, line: 6, baseType: !24, size: 1, flags: DIFlagBitField, extraData: i64 0)
108!24 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned)
109!25 = !DILocation(line: 10, column: 3, scope: !7)
110!26 = !DILocation(line: 10, column: 14, scope: !7)
111!27 = !DILocation(line: 10, column: 20, scope: !7)
112!28 = !{!29, !29, i64 0}
113!29 = !{!"any pointer", !30, i64 0}
114!30 = !{!"omnipotent char", !31, i64 0}
115!31 = !{!"Simple C/C++ TBAA"}
116!32 = !DILocation(line: 11, column: 40, scope: !7)
117!33 = !DILocation(line: 11, column: 45, scope: !7)
118!34 = !DILocation(line: 11, column: 10, scope: !7)
119!35 = !DILocation(line: 12, column: 1, scope: !7)
120!36 = !DILocation(line: 11, column: 3, scope: !7)
121!37 = !DISubprogram(name: "foo", scope: !1, file: !1, line: 8, type: !38, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized, retainedNodes: !40)
122!38 = !DISubroutineType(types: !39)
123!39 = !{!21}
124!40 = !{}
125