[llvm] 6e6c1ef - [BPF] Handle anon record for CO-RE relocations
Yonghong Song via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 13 15:16:53 PDT 2022
Author: Yonghong Song
Date: 2022-07-13T15:16:16-07:00
New Revision: 6e6c1efe04d45b717091e06eec94f0eef64839b1
URL: https://github.com/llvm/llvm-project/commit/6e6c1efe04d45b717091e06eec94f0eef64839b1
DIFF: https://github.com/llvm/llvm-project/commit/6e6c1efe04d45b717091e06eec94f0eef64839b1.diff
LOG: [BPF] Handle anon record for CO-RE relocations
When doing experiment in kernel, for kernel data structure sockptr_t
in CO-RE operation, I hit an assertion error. The sockptr_t definition
and usage look like below:
#pragma clang attribute push (__attribute__((preserve_access_index)), apply_to = record)
typedef struct {
union {
void *kernel;
void *user;
};
unsigned is_kernel : 1;
} sockptr_t;
#pragma clang attribute pop
int test(sockptr_t *arg) {
return arg->is_kernel;
}
The assertion error looks like
clang: ../lib/Target/BPF/BPFAbstractMemberAccess.cpp:878: llvm::Value*
{anonymous}::BPFAbstractMemberAccess::computeBaseAndAccessKey(llvm::CallInst*,
{anonymous}::BPFAbstractMemberAccess::CallInfo&, std::__cxx11::string&,
llvm::MDNode*&): Assertion `TypeName.size()' failed.
In this particular, the clang frontend attach the debuginfo metadata associated
with anon structure with the preserve_access_info IR intrinsic. But the first
debuginfo type has to be a named type so libbpf can have a sound start to
do CO-RE relocation.
Besides the above approach using pragma to push attribute, the below typedef/struct
definition can have preserve_access_index directly applying to the anon struct.
typedef struct {
union {
void *kernel;
void *user;
};
unsigned is_kernel : 1;
} __attribute__((preserve_access_index) sockptr_t;
This patch fixed the issue by preprocessing function argument/return types
and local variable types used by other CO-RE intrinsics. For any
typedef struct/union { ... } typedef_name
an association of <anon struct/union, typedef> is recorded to replace
the IR intrinsic metadata 'anon struct/union' to 'typedef'.
It is possible that two different 'typedef' types may have identical
anon struct/union type. For such a case, the association will be
<anon struct/union, nullptr> to indicate the invalid case.
Differential Revision: https://reviews.llvm.org/D129621
Added:
llvm/test/CodeGen/BPF/CORE/anon-struct-argument-pragma.ll
llvm/test/CodeGen/BPF/CORE/anon-union-localvar-attr.ll
llvm/test/CodeGen/BPF/CORE/anon-union-localvar-pragma.ll
Modified:
llvm/lib/Target/BPF/BPFAbstractMemberAccess.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Target/BPF/BPFAbstractMemberAccess.cpp b/llvm/lib/Target/BPF/BPFAbstractMemberAccess.cpp
index 349cdd92ae625..9aad9375d913a 100644
--- a/llvm/lib/Target/BPF/BPFAbstractMemberAccess.cpp
+++ b/llvm/lib/Target/BPF/BPFAbstractMemberAccess.cpp
@@ -149,6 +149,13 @@ class BPFAbstractMemberAccess final {
// The base call is not an input of any other preserve_*
// intrinsics.
std::map<CallInst *, CallInfo> BaseAICalls;
+ // A map to hold <AnonRecord, TypeDef> relationships
+ std::map<DICompositeType *, DIDerivedType *> AnonRecords;
+
+ void CheckAnonRecordType(DIDerivedType *ParentTy, DIType *Ty);
+ void CheckCompositeType(DIDerivedType *ParentTy, DICompositeType *CTy);
+ void CheckDerivedType(DIDerivedType *ParentTy, DIDerivedType *DTy);
+ void ResetMetadata(struct CallInfo &CInfo);
bool doTransformation(Function &F);
@@ -221,10 +228,80 @@ bool BPFAbstractMemberAccess::run(Function &F) {
if (M->debug_compile_units().empty())
return false;
+ // For each argument/return/local_variable type, trace the type
+ // pattern like '[derived_type]* [composite_type]' to check
+ // and remember (anon record -> typedef) relations where the
+ // anon record is defined as
+ // typedef [const/volatile/restrict]* [anon record]
+ DISubprogram *SP = F.getSubprogram();
+ if (SP && SP->isDefinition()) {
+ for (DIType *Ty: SP->getType()->getTypeArray())
+ CheckAnonRecordType(nullptr, Ty);
+ for (const DINode *DN : SP->getRetainedNodes()) {
+ if (const auto *DV = dyn_cast<DILocalVariable>(DN))
+ CheckAnonRecordType(nullptr, DV->getType());
+ }
+ }
+
DL = &M->getDataLayout();
return doTransformation(F);
}
+void BPFAbstractMemberAccess::ResetMetadata(struct CallInfo &CInfo) {
+ if (auto Ty = dyn_cast<DICompositeType>(CInfo.Metadata)) {
+ if (AnonRecords.find(Ty) != AnonRecords.end()) {
+ if (AnonRecords[Ty] != nullptr)
+ CInfo.Metadata = AnonRecords[Ty];
+ }
+ }
+}
+
+void BPFAbstractMemberAccess::CheckCompositeType(DIDerivedType *ParentTy,
+ DICompositeType *CTy) {
+ if (!CTy->getName().empty() || !ParentTy ||
+ ParentTy->getTag() != dwarf::DW_TAG_typedef)
+ return;
+
+ if (AnonRecords.find(CTy) == AnonRecords.end()) {
+ AnonRecords[CTy] = ParentTy;
+ return;
+ }
+
+ // Two or more typedef's may point to the same anon record.
+ // If this is the case, set the typedef DIType to be nullptr
+ // to indicate the duplication case.
+ DIDerivedType *CurrTy = AnonRecords[CTy];
+ if (CurrTy == ParentTy)
+ return;
+ AnonRecords[CTy] = nullptr;
+}
+
+void BPFAbstractMemberAccess::CheckDerivedType(DIDerivedType *ParentTy,
+ DIDerivedType *DTy) {
+ DIType *BaseType = DTy->getBaseType();
+ if (!BaseType)
+ return;
+
+ unsigned Tag = DTy->getTag();
+ if (Tag == dwarf::DW_TAG_pointer_type)
+ CheckAnonRecordType(nullptr, BaseType);
+ else if (Tag == dwarf::DW_TAG_typedef)
+ CheckAnonRecordType(DTy, BaseType);
+ else
+ CheckAnonRecordType(ParentTy, BaseType);
+}
+
+void BPFAbstractMemberAccess::CheckAnonRecordType(DIDerivedType *ParentTy,
+ DIType *Ty) {
+ if (!Ty)
+ return;
+
+ if (auto *CTy = dyn_cast<DICompositeType>(Ty))
+ return CheckCompositeType(ParentTy, CTy);
+ else if (auto *DTy = dyn_cast<DIDerivedType>(Ty))
+ return CheckDerivedType(ParentTy, DTy);
+}
+
static bool SkipDIDerivedTag(unsigned Tag, bool skipTypedef) {
if (Tag != dwarf::DW_TAG_typedef && Tag != dwarf::DW_TAG_const_type &&
Tag != dwarf::DW_TAG_volatile_type &&
@@ -298,6 +375,7 @@ bool BPFAbstractMemberAccess::IsPreserveDIAccessIndexCall(const CallInst *Call,
CInfo.Metadata = Call->getMetadata(LLVMContext::MD_preserve_access_index);
if (!CInfo.Metadata)
report_fatal_error("Missing metadata for llvm.preserve.union.access.index intrinsic");
+ ResetMetadata(CInfo);
CInfo.AccessIndex = getConstant(Call->getArgOperand(1));
CInfo.Base = Call->getArgOperand(0);
return true;
@@ -307,6 +385,7 @@ bool BPFAbstractMemberAccess::IsPreserveDIAccessIndexCall(const CallInst *Call,
CInfo.Metadata = Call->getMetadata(LLVMContext::MD_preserve_access_index);
if (!CInfo.Metadata)
report_fatal_error("Missing metadata for llvm.preserve.struct.access.index intrinsic");
+ ResetMetadata(CInfo);
CInfo.AccessIndex = getConstant(Call->getArgOperand(2));
CInfo.Base = Call->getArgOperand(0);
CInfo.RecordAlignment = DL->getABITypeAlign(getBaseElementType(Call));
diff --git a/llvm/test/CodeGen/BPF/CORE/anon-struct-argument-pragma.ll b/llvm/test/CodeGen/BPF/CORE/anon-struct-argument-pragma.ll
new file mode 100644
index 0000000000000..46507e3338211
--- /dev/null
+++ b/llvm/test/CodeGen/BPF/CORE/anon-struct-argument-pragma.ll
@@ -0,0 +1,100 @@
+; RUN: opt -O2 -mtriple=bpf-pc-linux %s | llvm-dis > %t1
+; RUN: llc -o - %t1 | FileCheck %s
+;
+; Source:
+; #pragma clang attribute push (__attribute__((preserve_access_index)), apply_to = record)
+; typedef struct {
+; union {
+; void *kernel;
+; void *user;
+; };
+; unsigned is_kernel : 1;
+; } sockptr_t;
+; #pragma clang attribute pop
+; int test(sockptr_t *arg) {
+; return arg->is_kernel;
+; }
+; Compilation flag:
+; clang -target bpf -O2 -S -emit-llvm -g -Xclang -disable-llvm-passes test.c
+
+%struct.sockptr_t = type { %union.anon, i8 }
+%union.anon = type { ptr }
+
+; Function Attrs: nounwind
+define dso_local i32 @test(ptr noundef %arg) #0 !dbg !7 {
+entry:
+ %arg.addr = alloca ptr, align 8
+ store ptr %arg, ptr %arg.addr, align 8, !tbaa !25
+ call void @llvm.dbg.declare(metadata ptr %arg.addr, metadata !24, metadata !DIExpression()), !dbg !29
+ %0 = load ptr, ptr %arg.addr, align 8, !dbg !30, !tbaa !25
+ %1 = call ptr @llvm.preserve.struct.access.index.p0.p0(ptr elementtype(%struct.sockptr_t) %0, i32 1, i32 1), !dbg !31, !llvm.preserve.access.index !13
+ %bf.load = load i8, ptr %1, align 8, !dbg !31
+ %bf.clear = and i8 %bf.load, 1, !dbg !31
+ %bf.cast = zext i8 %bf.clear to i32, !dbg !31
+ ret i32 %bf.cast, !dbg !32
+}
+
+; CHECK: .long 1 # BTF_KIND_TYPEDEF(id = 2)
+; CHECK-NEXT: .long 134217728 # 0x8000000
+; CHECK-NEXT: .long 3
+; CHECK-NEXT: .long 0 # BTF_KIND_STRUCT(id = 3)
+
+; CHECK: .ascii "sockptr_t" # string offset=1
+; CHECK: .ascii ".text" # string offset=59
+; CHECK: .ascii "0:1" # string offset=65
+
+; CHECK: .long 16 # FieldReloc
+; CHECK-NEXT: .long 59 # Field reloc section string offset=59
+; CHECK-NEXT: .long 1
+; CHECK-NEXT: .long .Ltmp[[#]]
+; CHECK-NEXT: .long 2
+; CHECK-NEXT: .long 65
+; CHECK-NEXT: .long 0
+
+; Function Attrs: nocallback nofree nosync nounwind readnone speculatable willreturn
+declare void @llvm.dbg.declare(metadata, metadata, metadata) #1
+
+; Function Attrs: nocallback nofree nosync nounwind readnone willreturn
+declare ptr @llvm.preserve.struct.access.index.p0.p0(ptr, i32 immarg, i32 immarg) #2
+
+attributes #0 = { nounwind "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+attributes #1 = { nocallback nofree nosync nounwind readnone speculatable willreturn }
+attributes #2 = { nocallback nofree nosync nounwind readnone willreturn }
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!2, !3, !4, !5}
+!llvm.ident = !{!6}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 15.0.0 (https://github.com/llvm/llvm-project.git d81a8759c969344c1e96992aab30f5b5a9d5ffd3)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
+!1 = !DIFile(filename: "test.c", directory: "/tmp/home/yhs/work/test/anon", checksumkind: CSK_MD5, checksum: "7ba33bf2146cc86b1c8396f6d3eace81")
+!2 = !{i32 7, !"Dwarf Version", i32 5}
+!3 = !{i32 2, !"Debug Info Version", i32 3}
+!4 = !{i32 1, !"wchar_size", i32 4}
+!5 = !{i32 7, !"frame-pointer", i32 2}
+!6 = !{!"clang version 15.0.0 (https://github.com/llvm/llvm-project.git d81a8759c969344c1e96992aab30f5b5a9d5ffd3)"}
+!7 = distinct !DISubprogram(name: "test", scope: !1, file: !1, line: 10, type: !8, scopeLine: 10, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !23)
+!8 = !DISubroutineType(types: !9)
+!9 = !{!10, !11}
+!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!11 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !12, size: 64)
+!12 = !DIDerivedType(tag: DW_TAG_typedef, name: "sockptr_t", file: !1, line: 8, baseType: !13)
+!13 = distinct !DICompositeType(tag: DW_TAG_structure_type, file: !1, line: 2, size: 128, elements: !14)
+!14 = !{!15, !21}
+!15 = !DIDerivedType(tag: DW_TAG_member, scope: !13, file: !1, line: 3, baseType: !16, size: 64)
+!16 = distinct !DICompositeType(tag: DW_TAG_union_type, scope: !13, file: !1, line: 3, size: 64, elements: !17)
+!17 = !{!18, !20}
+!18 = !DIDerivedType(tag: DW_TAG_member, name: "kernel", scope: !16, file: !1, line: 4, baseType: !19, size: 64)
+!19 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64)
+!20 = !DIDerivedType(tag: DW_TAG_member, name: "user", scope: !16, file: !1, line: 5, baseType: !19, size: 64)
+!21 = !DIDerivedType(tag: DW_TAG_member, name: "is_kernel", scope: !13, file: !1, line: 7, baseType: !22, size: 1, offset: 64, flags: DIFlagBitField, extraData: i64 64)
+!22 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned)
+!23 = !{!24}
+!24 = !DILocalVariable(name: "arg", arg: 1, scope: !7, file: !1, line: 10, type: !11)
+!25 = !{!26, !26, i64 0}
+!26 = !{!"any pointer", !27, i64 0}
+!27 = !{!"omnipotent char", !28, i64 0}
+!28 = !{!"Simple C/C++ TBAA"}
+!29 = !DILocation(line: 10, column: 21, scope: !7)
+!30 = !DILocation(line: 11, column: 10, scope: !7)
+!31 = !DILocation(line: 11, column: 15, scope: !7)
+!32 = !DILocation(line: 11, column: 3, scope: !7)
diff --git a/llvm/test/CodeGen/BPF/CORE/anon-union-localvar-attr.ll b/llvm/test/CodeGen/BPF/CORE/anon-union-localvar-attr.ll
new file mode 100644
index 0000000000000..dd8c86897275e
--- /dev/null
+++ b/llvm/test/CodeGen/BPF/CORE/anon-union-localvar-attr.ll
@@ -0,0 +1,124 @@
+; RUN: opt -O2 -mtriple=bpf-pc-linux %s | llvm-dis > %t1
+; RUN: llc -o - %t1 | FileCheck %s
+;
+; Source:
+; typedef union {
+; struct {
+; void *kernel;
+; void *user;
+; };
+; unsigned is_kernel : 1;
+; } __attribute__((preserve_access_index)) sockptr_t;
+; void *foo(void);
+; int test() {
+; sockptr_t *arg = foo();
+; return __builtin_preserve_field_info(arg->is_kernel, 1);
+; }
+; Compilation flag:
+; clang -target bpf -O2 -S -emit-llvm -g -Xclang -disable-llvm-passes test.c
+
+%union.sockptr_t = type { %struct.anon }
+%struct.anon = type { ptr, ptr }
+
+; Function Attrs: nounwind
+define dso_local i32 @test() #0 !dbg !7 {
+entry:
+ %arg = alloca ptr, align 8
+ call void @llvm.lifetime.start.p0(i64 8, ptr %arg) #6, !dbg !25
+ call void @llvm.dbg.declare(metadata ptr %arg, metadata !12, metadata !DIExpression()), !dbg !26
+ %call = call ptr @foo(), !dbg !27
+ store ptr %call, ptr %arg, align 8, !dbg !26, !tbaa !28
+ %0 = load ptr, ptr %arg, align 8, !dbg !32, !tbaa !28
+ %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
+ %2 = call i32 @llvm.bpf.preserve.field.info.p0(ptr %1, i64 1), !dbg !34
+ call void @llvm.lifetime.end.p0(i64 8, ptr %arg) #6, !dbg !35
+ ret i32 %2, !dbg !36
+}
+
+; CHECK: .long 56 # BTF_KIND_TYPEDEF(id = 7)
+; CHECK-NEXT: .long 134217728 # 0x8000000
+; CHECK-NEXT: .long 8
+; CHECK-NEXT: .long 0 # BTF_KIND_UNION(id = 8)
+
+; CHECK: .ascii ".text" # string offset=10
+; CHECK: .ascii "sockptr_t" # string offset=56
+; CHECK: .ascii "0:1" # string offset=101
+
+; CHECK: .long 16 # FieldReloc
+; CHECK-NEXT: .long 10 # Field reloc section string offset=10
+; CHECK-NEXT: .long 1
+; CHECK-NEXT: .long .Ltmp[[#]]
+; CHECK-NEXT: .long 7
+; CHECK-NEXT: .long 101
+; CHECK-NEXT: .long 1
+
+; Function Attrs: argmemonly nocallback nofree nosync nounwind willreturn
+declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1
+
+; Function Attrs: nocallback nofree nosync nounwind readnone speculatable willreturn
+declare void @llvm.dbg.declare(metadata, metadata, metadata) #2
+
+declare !dbg !37 dso_local ptr @foo() #3
+
+; Function Attrs: nocallback nofree nosync nounwind readnone willreturn
+declare ptr @llvm.preserve.struct.access.index.p0.p0(ptr, i32 immarg, i32 immarg) #4
+
+; Function Attrs: nounwind readnone
+declare i32 @llvm.bpf.preserve.field.info.p0(ptr, i64 immarg) #5
+
+; Function Attrs: argmemonly nocallback nofree nosync nounwind willreturn
+declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1
+
+attributes #0 = { nounwind "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+attributes #1 = { argmemonly nocallback nofree nosync nounwind willreturn }
+attributes #2 = { nocallback nofree nosync nounwind readnone speculatable willreturn }
+attributes #3 = { "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+attributes #4 = { nocallback nofree nosync nounwind readnone willreturn }
+attributes #5 = { nounwind readnone }
+attributes #6 = { nounwind }
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!2, !3, !4, !5}
+!llvm.ident = !{!6}
+
+!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)
+!1 = !DIFile(filename: "test.c", directory: "/tmp/home/yhs/work/test/anon", checksumkind: CSK_MD5, checksum: "03904da5c5e53b14bb1effb6d9d5e025")
+!2 = !{i32 7, !"Dwarf Version", i32 5}
+!3 = !{i32 2, !"Debug Info Version", i32 3}
+!4 = !{i32 1, !"wchar_size", i32 4}
+!5 = !{i32 7, !"frame-pointer", i32 2}
+!6 = !{!"clang version 15.0.0 (https://github.com/llvm/llvm-project.git 8c7d5118961e7ffc0304126ec2122d21e2eb1f79)"}
+!7 = distinct !DISubprogram(name: "test", scope: !1, file: !1, line: 9, type: !8, scopeLine: 9, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !11)
+!8 = !DISubroutineType(types: !9)
+!9 = !{!10}
+!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!11 = !{!12}
+!12 = !DILocalVariable(name: "arg", scope: !7, file: !1, line: 10, type: !13)
+!13 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !14, size: 64)
+!14 = !DIDerivedType(tag: DW_TAG_typedef, name: "sockptr_t", file: !1, line: 7, baseType: !15)
+!15 = distinct !DICompositeType(tag: DW_TAG_union_type, file: !1, line: 1, size: 128, elements: !16)
+!16 = !{!17, !23}
+!17 = !DIDerivedType(tag: DW_TAG_member, scope: !15, file: !1, line: 2, baseType: !18, size: 128)
+!18 = distinct !DICompositeType(tag: DW_TAG_structure_type, scope: !15, file: !1, line: 2, size: 128, elements: !19)
+!19 = !{!20, !22}
+!20 = !DIDerivedType(tag: DW_TAG_member, name: "kernel", scope: !18, file: !1, line: 3, baseType: !21, size: 64)
+!21 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64)
+!22 = !DIDerivedType(tag: DW_TAG_member, name: "user", scope: !18, file: !1, line: 4, baseType: !21, size: 64, offset: 64)
+!23 = !DIDerivedType(tag: DW_TAG_member, name: "is_kernel", scope: !15, file: !1, line: 6, baseType: !24, size: 1, flags: DIFlagBitField, extraData: i64 0)
+!24 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned)
+!25 = !DILocation(line: 10, column: 3, scope: !7)
+!26 = !DILocation(line: 10, column: 14, scope: !7)
+!27 = !DILocation(line: 10, column: 20, scope: !7)
+!28 = !{!29, !29, i64 0}
+!29 = !{!"any pointer", !30, i64 0}
+!30 = !{!"omnipotent char", !31, i64 0}
+!31 = !{!"Simple C/C++ TBAA"}
+!32 = !DILocation(line: 11, column: 40, scope: !7)
+!33 = !DILocation(line: 11, column: 45, scope: !7)
+!34 = !DILocation(line: 11, column: 10, scope: !7)
+!35 = !DILocation(line: 12, column: 1, scope: !7)
+!36 = !DILocation(line: 11, column: 3, scope: !7)
+!37 = !DISubprogram(name: "foo", scope: !1, file: !1, line: 8, type: !38, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized, retainedNodes: !40)
+!38 = !DISubroutineType(types: !39)
+!39 = !{!21}
+!40 = !{}
diff --git a/llvm/test/CodeGen/BPF/CORE/anon-union-localvar-pragma.ll b/llvm/test/CodeGen/BPF/CORE/anon-union-localvar-pragma.ll
new file mode 100644
index 0000000000000..d2fee10d27710
--- /dev/null
+++ b/llvm/test/CodeGen/BPF/CORE/anon-union-localvar-pragma.ll
@@ -0,0 +1,127 @@
+; RUN: opt -O2 -mtriple=bpf-pc-linux %s | llvm-dis > %t1
+; RUN: llc -o - %t1 | FileCheck %s
+;
+; Source:
+; #pragma clang attribute push (__attribute__((preserve_access_index)), apply_to = record)
+; typedef union {
+; struct {
+; void *kernel;
+; void *user;
+; };
+; unsigned is_kernel : 1;
+; } sockptr_t;
+; #pragma clang attribute pop
+; void *foo(void);
+; int test() {
+; sockptr_t *arg = foo();
+; return __builtin_preserve_field_info(arg->is_kernel, 1);
+; }
+; Compilation flag:
+; clang -target bpf -O2 -S -emit-llvm -g -Xclang -disable-llvm-passes test.c
+
+%union.sockptr_t = type { %struct.anon }
+%struct.anon = type { ptr, ptr }
+
+; Function Attrs: nounwind
+define dso_local i32 @test() #0 !dbg !7 {
+entry:
+ %arg = alloca ptr, align 8
+ call void @llvm.lifetime.start.p0(i64 8, ptr %arg) #6, !dbg !25
+ call void @llvm.dbg.declare(metadata ptr %arg, metadata !12, metadata !DIExpression()), !dbg !26
+ %call = call ptr @foo(), !dbg !27
+ store ptr %call, ptr %arg, align 8, !dbg !26, !tbaa !28
+ %0 = load ptr, ptr %arg, align 8, !dbg !32, !tbaa !28
+ %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
+ %2 = call i32 @llvm.bpf.preserve.field.info.p0(ptr %1, i64 1), !dbg !34
+ call void @llvm.lifetime.end.p0(i64 8, ptr %arg) #6, !dbg !35
+ ret i32 %2, !dbg !36
+}
+
+; CHECK: .long 56 # BTF_KIND_TYPEDEF(id = 7)
+; CHECK-NEXT: .long 134217728 # 0x8000000
+; CHECK-NEXT: .long 8
+; CHECK-NEXT: .long 0 # BTF_KIND_UNION(id = 8)
+
+; CHECK: .ascii ".text" # string offset=10
+; CHECK: .ascii "sockptr_t" # string offset=56
+; CHECK: .ascii "0:1" # string offset=101
+
+
+; CHECK: .long 16 # FieldReloc
+; CHECK-NEXT: .long 10 # Field reloc section string offset=10
+; CHECK-NEXT: .long 1
+; CHECK-NEXT: .long .Ltmp[[#]]
+; CHECK-NEXT: .long 7
+; CHECK-NEXT: .long 101
+; CHECK-NEXT: .long 1
+
+; Function Attrs: argmemonly nocallback nofree nosync nounwind willreturn
+declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1
+
+; Function Attrs: nocallback nofree nosync nounwind readnone speculatable willreturn
+declare void @llvm.dbg.declare(metadata, metadata, metadata) #2
+
+declare !dbg !37 dso_local ptr @foo() #3
+
+; Function Attrs: nocallback nofree nosync nounwind readnone willreturn
+declare ptr @llvm.preserve.struct.access.index.p0.p0(ptr, i32 immarg, i32 immarg) #4
+
+; Function Attrs: nounwind readnone
+declare i32 @llvm.bpf.preserve.field.info.p0(ptr, i64 immarg) #5
+
+; Function Attrs: argmemonly nocallback nofree nosync nounwind willreturn
+declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1
+
+attributes #0 = { nounwind "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+attributes #1 = { argmemonly nocallback nofree nosync nounwind willreturn }
+attributes #2 = { nocallback nofree nosync nounwind readnone speculatable willreturn }
+attributes #3 = { "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+attributes #4 = { nocallback nofree nosync nounwind readnone willreturn }
+attributes #5 = { nounwind readnone }
+attributes #6 = { nounwind }
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!2, !3, !4, !5}
+!llvm.ident = !{!6}
+
+!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)
+!1 = !DIFile(filename: "test.c", directory: "/tmp/home/yhs/work/test/anon", checksumkind: CSK_MD5, checksum: "2c5f698241a8b5ddf345a5743dfca258")
+!2 = !{i32 7, !"Dwarf Version", i32 5}
+!3 = !{i32 2, !"Debug Info Version", i32 3}
+!4 = !{i32 1, !"wchar_size", i32 4}
+!5 = !{i32 7, !"frame-pointer", i32 2}
+!6 = !{!"clang version 15.0.0 (https://github.com/llvm/llvm-project.git 8c7d5118961e7ffc0304126ec2122d21e2eb1f79)"}
+!7 = distinct !DISubprogram(name: "test", scope: !1, file: !1, line: 11, type: !8, scopeLine: 11, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !11)
+!8 = !DISubroutineType(types: !9)
+!9 = !{!10}
+!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!11 = !{!12}
+!12 = !DILocalVariable(name: "arg", scope: !7, file: !1, line: 12, type: !13)
+!13 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !14, size: 64)
+!14 = !DIDerivedType(tag: DW_TAG_typedef, name: "sockptr_t", file: !1, line: 8, baseType: !15)
+!15 = distinct !DICompositeType(tag: DW_TAG_union_type, file: !1, line: 2, size: 128, elements: !16)
+!16 = !{!17, !23}
+!17 = !DIDerivedType(tag: DW_TAG_member, scope: !15, file: !1, line: 3, baseType: !18, size: 128)
+!18 = distinct !DICompositeType(tag: DW_TAG_structure_type, scope: !15, file: !1, line: 3, size: 128, elements: !19)
+!19 = !{!20, !22}
+!20 = !DIDerivedType(tag: DW_TAG_member, name: "kernel", scope: !18, file: !1, line: 4, baseType: !21, size: 64)
+!21 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64)
+!22 = !DIDerivedType(tag: DW_TAG_member, name: "user", scope: !18, file: !1, line: 5, baseType: !21, size: 64, offset: 64)
+!23 = !DIDerivedType(tag: DW_TAG_member, name: "is_kernel", scope: !15, file: !1, line: 7, baseType: !24, size: 1, flags: DIFlagBitField, extraData: i64 0)
+!24 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned)
+!25 = !DILocation(line: 12, column: 3, scope: !7)
+!26 = !DILocation(line: 12, column: 14, scope: !7)
+!27 = !DILocation(line: 12, column: 20, scope: !7)
+!28 = !{!29, !29, i64 0}
+!29 = !{!"any pointer", !30, i64 0}
+!30 = !{!"omnipotent char", !31, i64 0}
+!31 = !{!"Simple C/C++ TBAA"}
+!32 = !DILocation(line: 13, column: 40, scope: !7)
+!33 = !DILocation(line: 13, column: 45, scope: !7)
+!34 = !DILocation(line: 13, column: 10, scope: !7)
+!35 = !DILocation(line: 14, column: 1, scope: !7)
+!36 = !DILocation(line: 13, column: 3, scope: !7)
+!37 = !DISubprogram(name: "foo", scope: !1, file: !1, line: 10, type: !38, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized, retainedNodes: !40)
+!38 = !DISubroutineType(types: !39)
+!39 = !{!21}
+!40 = !{}
More information about the llvm-commits
mailing list