64 changes: 64 additions & 0 deletions llvm/test/CodeGen/BPF/preserve-static-offset/store-chain.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
; RUN: opt -O2 -mtriple=bpf-pc-linux -S -o - %s | FileCheck %s
;
; Check that bpf-preserve-static-offset folds chain of GEP instructions.
;
; Source:
; #define __ctx __attribute__((preserve_static_offset))
;
; struct bar {
; int aa;
; int bb;
; };
;
; struct foo {
; int a;
; struct bar b;
; } __ctx;
;
; void buz(struct foo *p) {
; p->b.bb = 42;
; }
;
; Compilation flag:
; clang -cc1 -O2 -triple bpf -S -emit-llvm -disable-llvm-passes -o - \
; | opt -passes=function(sroa) -S -o -

%struct.foo = type { i32, %struct.bar }
%struct.bar = type { i32, i32 }

; Function Attrs: nounwind
define dso_local void @buz(ptr noundef %p) #0 {
entry:
%0 = call ptr @llvm.preserve.static.offset(ptr %p)
%b = getelementptr inbounds %struct.foo, ptr %0, i32 0, i32 1
%bb = getelementptr inbounds %struct.bar, ptr %b, i32 0, i32 1
store i32 42, ptr %bb, align 4, !tbaa !2
ret void
}

; CHECK: define dso_local void @buz(ptr nocapture noundef writeonly %[[p:.*]])
; CHECK: tail call void (i32, ptr, i1, i8, i8, i8, i1, ...)
; CHECK-SAME: @llvm.bpf.getelementptr.and.store.i32
; CHECK-SAME: (i32 42,
; CHECK-SAME: ptr writeonly elementtype(%struct.foo) %[[p]],
; CHECK-SAME: i1 false, i8 0, i8 1, i8 2, i1 true, i32 immarg 0, i32 immarg 1, i32 immarg 1)
; CHECK-SAME: #[[v2:.*]], !tbaa
; CHECK: attributes #[[v2]] = { memory(argmem: write) }

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare ptr @llvm.preserve.static.offset(ptr readnone) #1

attributes #0 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{!"clang"}
!2 = !{!3, !4, i64 8}
!3 = !{!"foo", !4, i64 0, !7, i64 4}
!4 = !{!"int", !5, i64 0}
!5 = !{!"omnipotent char", !6, i64 0}
!6 = !{!"Simple C/C++ TBAA"}
!7 = !{!"bar", !4, i64 0, !4, i64 4}
136 changes: 136 additions & 0 deletions llvm/test/CodeGen/BPF/preserve-static-offset/store-pai.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
; RUN: opt -passes=bpf-preserve-static-offset -mtriple=bpf-pc-linux -S -o - %s | FileCheck %s
;
; Source:
; #define __ctx __attribute__((preserve_static_offset))
; #define __pai __attribute__((preserve_access_index))
;
; struct foo {
; char a[10];
; } __pai;
;
; struct bar {
; int a;
; int b;
; } __pai;
;
; struct buz {
; int _1;
; int _2;
; int _3;
; union {
; struct foo a;
; struct bar b[7];
; };
; } __pai __ctx;
;
; void quux(struct buz *p) {
; p->b[5].b = 42;
; }
;
; Compilation flag:
; clang -cc1 -O2 -triple bpf -S -emit-llvm -disable-llvm-passes \
; -debug-info-kind=limited -o - \
; | opt -passes=function(sroa) -S -o -

%struct.buz = type { i32, i32, i32, %union.anon }
%union.anon = type { [7 x %struct.bar] }
%struct.bar = type { i32, i32 }

; Function Attrs: nounwind
define dso_local void @quux(ptr noundef %p) #0 !dbg !31 {
entry:
call void @llvm.dbg.value(metadata ptr %p, metadata !36, metadata !DIExpression()), !dbg !37
%0 = call ptr @llvm.preserve.static.offset(ptr %p), !dbg !38
%1 = call ptr @llvm.preserve.struct.access.index.p0.p0(ptr elementtype(%struct.buz) %0, i32 3, i32 3), !dbg !38, !llvm.preserve.access.index !4
%2 = call ptr @llvm.preserve.union.access.index.p0.p0(ptr %1, i32 1), !dbg !38, !llvm.preserve.access.index !3
%3 = call ptr @llvm.preserve.array.access.index.p0.p0(ptr elementtype([7 x %struct.bar]) %2, i32 1, i32 5), !dbg !39, !llvm.preserve.access.index !21
%4 = call ptr @llvm.preserve.struct.access.index.p0.p0(ptr elementtype(%struct.bar) %3, i32 1, i32 1), !dbg !40, !llvm.preserve.access.index !22
store i32 42, ptr %4, align 4, !dbg !41, !tbaa !42
ret void, !dbg !45
}

; CHECK: define dso_local void @quux(ptr noundef %[[p:.*]]) {{.*}} {
; CHECK-NEXT: entry:
; CHECK-NEXT: call void @llvm.dbg.value
; CHECK-NEXT: call void (i32, ptr, i1, i8, i8, i8, i1, ...)
; CHECK-SAME: @llvm.bpf.getelementptr.and.store.i32
; CHECK-SAME: (i32 42,
; CHECK-SAME: ptr writeonly elementtype(i8) %[[p]],
; CHECK-SAME: i1 false, i8 0, i8 1, i8 2, i1 true, i64 immarg 56)
; CHECK-SAME: #[[v5:.*]], !tbaa
; CHECK-NEXT: ret void, !dbg
; CHECK-NEXT: }
; CHECK: attributes #[[v5]] = { memory(argmem: write) }

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare void @llvm.dbg.declare(metadata, metadata, metadata) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare ptr @llvm.preserve.static.offset(ptr readnone) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(none)
declare ptr @llvm.preserve.struct.access.index.p0.p0(ptr, i32 immarg, i32 immarg) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(none)
declare ptr @llvm.preserve.union.access.index.p0.p0(ptr, i32 immarg) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(none)
declare ptr @llvm.preserve.array.access.index.p0.p0(ptr, i32 immarg, i32 immarg) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare void @llvm.dbg.value(metadata, metadata, metadata) #1

attributes #0 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { nocallback nofree nosync nounwind willreturn memory(none) }

!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!28, !29}
!llvm.ident = !{!30}

!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, retainedTypes: !2, splitDebugInlining: false, nameTableKind: None)
!1 = !DIFile(filename: "some-file.c", directory: "/some/dir/")
!2 = !{!3, !21}
!3 = distinct !DICompositeType(tag: DW_TAG_union_type, scope: !4, file: !1, line: 17, size: 448, elements: !11)
!4 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "buz", file: !1, line: 13, size: 544, elements: !5)
!5 = !{!6, !8, !9, !10}
!6 = !DIDerivedType(tag: DW_TAG_member, name: "_1", scope: !4, file: !1, line: 14, baseType: !7, size: 32)
!7 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!8 = !DIDerivedType(tag: DW_TAG_member, name: "_2", scope: !4, file: !1, line: 15, baseType: !7, size: 32, offset: 32)
!9 = !DIDerivedType(tag: DW_TAG_member, name: "_3", scope: !4, file: !1, line: 16, baseType: !7, size: 32, offset: 64)
!10 = !DIDerivedType(tag: DW_TAG_member, scope: !4, file: !1, line: 17, baseType: !3, size: 448, offset: 96)
!11 = !{!12, !20}
!12 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !3, file: !1, line: 18, baseType: !13, size: 80)
!13 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "foo", file: !1, line: 4, size: 80, elements: !14)
!14 = !{!15}
!15 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !13, file: !1, line: 5, baseType: !16, size: 80)
!16 = !DICompositeType(tag: DW_TAG_array_type, baseType: !17, size: 80, elements: !18)
!17 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
!18 = !{!19}
!19 = !DISubrange(count: 10)
!20 = !DIDerivedType(tag: DW_TAG_member, name: "b", scope: !3, file: !1, line: 19, baseType: !21, size: 448)
!21 = !DICompositeType(tag: DW_TAG_array_type, baseType: !22, size: 448, elements: !26)
!22 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "bar", file: !1, line: 8, size: 64, elements: !23)
!23 = !{!24, !25}
!24 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !22, file: !1, line: 9, baseType: !7, size: 32)
!25 = !DIDerivedType(tag: DW_TAG_member, name: "b", scope: !22, file: !1, line: 10, baseType: !7, size: 32, offset: 32)
!26 = !{!27}
!27 = !DISubrange(count: 7)
!28 = !{i32 2, !"Debug Info Version", i32 3}
!29 = !{i32 1, !"wchar_size", i32 4}
!30 = !{!"clang"}
!31 = distinct !DISubprogram(name: "quux", scope: !1, file: !1, line: 23, type: !32, scopeLine: 23, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !35)
!32 = !DISubroutineType(types: !33)
!33 = !{null, !34}
!34 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !4, size: 64)
!35 = !{!36}
!36 = !DILocalVariable(name: "p", arg: 1, scope: !31, file: !1, line: 23, type: !34)
!37 = !DILocation(line: 0, scope: !31)
!38 = !DILocation(line: 24, column: 6, scope: !31)
!39 = !DILocation(line: 24, column: 3, scope: !31)
!40 = !DILocation(line: 24, column: 11, scope: !31)
!41 = !DILocation(line: 24, column: 13, scope: !31)
!42 = !{!43, !43, i64 0}
!43 = !{!"omnipotent char", !44, i64 0}
!44 = !{!"Simple C/C++ TBAA"}
!45 = !DILocation(line: 25, column: 1, scope: !31)
60 changes: 60 additions & 0 deletions llvm/test/CodeGen/BPF/preserve-static-offset/store-simple.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
; RUN: opt -O2 -mtriple=bpf-pc-linux -S -o - %s | FileCheck %s
;
; Check handling of a simple store instruction by bpf-preserve-static-offset.
; Verify:
; - presence of gep.and.store intrinsic call
; - correct attributes for intrinsic call
; - presence of tbaa annotations
;
; Source:
; #define __ctx __attribute__((preserve_static_offset))
;
; struct foo {
; int _;
; int a;
; } __ctx;
;
; void bar(struct foo *p) {
; p->a = 7;
; }
;
; Compilation flag:
; clang -cc1 -O2 -triple bpf -S -emit-llvm -disable-llvm-passes -o - \
; | opt -passes=function(sroa) -S -o -

%struct.foo = type { i32, i32 }

; Function Attrs: nounwind
define dso_local void @bar(ptr noundef %p) #0 {
entry:
%0 = call ptr @llvm.preserve.static.offset(ptr %p)
%a = getelementptr inbounds %struct.foo, ptr %0, i32 0, i32 1
store i32 7, ptr %a, align 4, !tbaa !2
ret void
}

; CHECK: define dso_local void @bar(ptr nocapture noundef writeonly %[[p:.*]])
; CHECK: tail call void (i32, ptr, i1, i8, i8, i8, i1, ...)
; CHECK-SAME: @llvm.bpf.getelementptr.and.store.i32
; CHECK-SAME: (i32 7,
; CHECK-SAME: ptr writeonly elementtype(%struct.foo) %[[p]],
; CHECK-SAME: i1 false, i8 0, i8 1, i8 2, i1 true, i32 immarg 0, i32 immarg 1)
; CHECK-SAME: #[[v2:.*]], !tbaa
; CHECK: attributes #[[v2]] = { memory(argmem: write) }

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare ptr @llvm.preserve.static.offset(ptr readnone) #1

attributes #0 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{!"clang"}
!2 = !{!3, !4, i64 4}
!3 = !{!"foo", !4, i64 0, !4, i64 4}
!4 = !{!"int", !5, i64 0}
!5 = !{!"omnipotent char", !6, i64 0}
!6 = !{!"Simple C/C++ TBAA"}
62 changes: 62 additions & 0 deletions llvm/test/CodeGen/BPF/preserve-static-offset/store-undo-align.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
; RUN: opt --bpf-check-and-opt-ir -mtriple=bpf-pc-linux -S -o - %s | FileCheck %s
;
; Check that getelementptr.and.store unroll restores alignment spec.
;
; Source:
; #define __ctx __attribute__((preserve_static_offset))
;
; typedef int aligned_int __attribute__((aligned(128)));
;
; struct foo {
; int _;
; aligned_int a;
; } __ctx;
;
; void bar(struct foo *p) {
; p->a = 42;
; }
;
; Compilation flag:
; clang -cc1 -O2 -triple bpf -S -emit-llvm -disable-llvm-passes -o - \
; | opt -passes=sroa,bpf-preserve-static-offset -S -o -

%struct.foo = type { i32, [124 x i8], i32, [124 x i8] }

; Function Attrs: nounwind
define dso_local void @bar(ptr noundef %p) #0 {
entry:
call void (i32, ptr, i1, i8, i8, i8, i1, ...)
@llvm.bpf.getelementptr.and.store.i32
(i32 42,
ptr writeonly elementtype(%struct.foo) %p,
i1 false, i8 0, i8 1, i8 7, i1 true, i32 immarg 0, i32 immarg 2)
#3, !tbaa !2
ret void
}

; CHECK: define dso_local void @bar(ptr noundef %[[p:.*]])
; CHECK: %[[v2:.*]] = getelementptr inbounds %struct.foo, ptr %[[p]], i32 0, i32 2
; CHECK: store i32 42, ptr %[[v2]], align 128
; CHECK: ret void

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare ptr @llvm.preserve.static.offset(ptr readnone) #1

; Function Attrs: nocallback nofree nounwind willreturn
declare void @llvm.bpf.getelementptr.and.store.i32(i32, ptr nocapture, i1 immarg, i8 immarg, i8 immarg, i8 immarg, i1 immarg, ...) #2

attributes #0 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { nocallback nofree nounwind willreturn }
attributes #3 = { memory(argmem: write) }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{!"clang"}
!2 = !{!3, !4, i64 128}
!3 = !{!"foo", !4, i64 0, !4, i64 128}
!4 = !{!"int", !5, i64 0}
!5 = !{!"omnipotent char", !6, i64 0}
!6 = !{!"Simple C/C++ TBAA"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
; RUN: opt --bpf-check-and-opt-ir -mtriple=bpf-pc-linux -S -o - %s | FileCheck %s
;
; Check that getelementptr.and.load unroll can skip 'inbounds' flag.
;
; Source:
; #define __ctx __attribute__((preserve_static_offset))
;
; struct bar {
; int aa;
; int bb;
; };
;
; struct foo {
; int a;
; struct bar b;
; } __ctx;
;
; void buz(struct foo *p) {
; p->b.bb = 42;
; }
;
; Compilation flag:
; clang -cc1 -O2 -triple bpf -S -emit-llvm -disable-llvm-passes -o - \
; | opt -passes=sroa,bpf-preserve-static-offset -S -o -

%struct.foo = type { i32, %struct.bar }
%struct.bar = type { i32, i32 }

; Function Attrs: nounwind
define dso_local void @buz(ptr noundef %p) #0 {
entry:
call void (i32, ptr, i1, i8, i8, i8, i1, ...)
@llvm.bpf.getelementptr.and.store.i32
(i32 42,
ptr writeonly elementtype(%struct.foo) %p,
i1 false, i8 0, i8 1, i8 2, i1 false, i32 immarg 0, i32 immarg 1, i32 immarg 1)
#3, !tbaa !2
ret void
}

; CHECK: define dso_local void @buz(ptr noundef %[[p:.*]])
; CHECK: %[[v2:.*]] = getelementptr %struct.foo, ptr %[[p]], i32 0, i32 1, i32 1
; CHECK: store i32 42, ptr %[[v2]], align 4
; CHECK: ret void

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare ptr @llvm.preserve.static.offset(ptr readnone) #1

; Function Attrs: nocallback nofree nounwind willreturn
declare void @llvm.bpf.getelementptr.and.store.i32(i32, ptr nocapture, i1 immarg, i8 immarg, i8 immarg, i8 immarg, i1 immarg, ...) #2

attributes #0 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { nocallback nofree nounwind willreturn }
attributes #3 = { memory(argmem: write) }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{!"clang"}
!2 = !{!3, !4, i64 8}
!3 = !{!"foo", !4, i64 0, !7, i64 4}
!4 = !{!"int", !5, i64 0}
!5 = !{!"omnipotent char", !6, i64 0}
!6 = !{!"Simple C/C++ TBAA"}
!7 = !{!"bar", !4, i64 0, !4, i64 4}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
; RUN: opt --bpf-check-and-opt-ir -mtriple=bpf-pc-linux -S -o - %s | FileCheck %s
;
; Check unroll of getelementptr.and.store when direct memory offset is
; used instead of field indexes.
;
; Source:
; #define __ctx __attribute__((preserve_static_offset))
;
; struct foo {
; char aa;
; char bb;
; };
;
; struct bar {
; char a;
; struct foo b;
; } __ctx;
;
; void buz(struct bar *p) {
; ((struct foo *)(((char*)&p->b) + 1))->bb = 42;
; }
;
; Compilation flag:
; clang -cc1 -O2 -triple bpf -S -emit-llvm -disable-llvm-passes -o - \
; | opt -passes=sroa,bpf-preserve-static-offset -S -o -

; Function Attrs: nounwind
define dso_local void @buz(ptr noundef %p) #0 {
entry:
call void (i8, ptr, i1, i8, i8, i8, i1, ...)
@llvm.bpf.getelementptr.and.store.i8
(i8 42,
ptr writeonly elementtype(i8) %p,
i1 false, i8 0, i8 1, i8 0, i1 true, i64 immarg 3)
#3, !tbaa !2
ret void
}

; CHECK: define dso_local void @buz(ptr noundef %[[p:.*]])
; CHECK: %[[v2:.*]] = getelementptr inbounds i8, ptr %[[p]], i64 3
; CHECK: store i8 42, ptr %[[v2]], align 1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare ptr @llvm.preserve.static.offset(ptr readnone) #1

; Function Attrs: nocallback nofree nounwind willreturn
declare void @llvm.bpf.getelementptr.and.store.i8(i8, ptr nocapture, i1 immarg, i8 immarg, i8 immarg, i8 immarg, i1 immarg, ...) #2

attributes #0 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { nocallback nofree nounwind willreturn }
attributes #3 = { memory(argmem: write) }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{!"clang"}
!2 = !{!3, !4, i64 1}
!3 = !{!"foo", !4, i64 0, !4, i64 1}
!4 = !{!"omnipotent char", !5, i64 0}
!5 = !{!"Simple C/C++ TBAA"}
68 changes: 68 additions & 0 deletions llvm/test/CodeGen/BPF/preserve-static-offset/store-undo-chain.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
; RUN: opt --bpf-check-and-opt-ir -mtriple=bpf-pc-linux -S -o - %s | FileCheck %s
;
; Check unroll of getelementptr.and.store when several field indexes
; are specified in a chain.
;
; Source:
; #define __ctx __attribute__((preserve_static_offset))
;
; struct bar {
; int aa;
; int bb;
; };
;
; struct foo {
; int a;
; struct bar b;
; } __ctx;
;
; void buz(struct foo *p) {
; p->b.bb = 42;
; }
;
; Compilation flag:
; clang -cc1 -O2 -triple bpf -S -emit-llvm -disable-llvm-passes -o - \
; | opt -passes=sroa,bpf-preserve-static-offset -S -o -

%struct.foo = type { i32, %struct.bar }
%struct.bar = type { i32, i32 }

; Function Attrs: nounwind
define dso_local void @buz(ptr noundef %p) #0 {
entry:
call void (i32, ptr, i1, i8, i8, i8, i1, ...)
@llvm.bpf.getelementptr.and.store.i32
(i32 42,
ptr writeonly elementtype(%struct.foo) %p,
i1 false, i8 0, i8 1, i8 2, i1 true, i32 immarg 0, i32 immarg 1, i32 immarg 1)
#3, !tbaa !2
ret void
}

; CHECK: define dso_local void @buz(ptr noundef %[[p:.*]])
; CHECK: %[[v2:.*]] = getelementptr inbounds %struct.foo, ptr %[[p]], i32 0, i32 1, i32 1
; CHECK: store i32 42, ptr %[[v2]], align 4
; CHECK: ret void

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare ptr @llvm.preserve.static.offset(ptr readnone) #1

; Function Attrs: nocallback nofree nounwind willreturn
declare void @llvm.bpf.getelementptr.and.store.i32(i32, ptr nocapture, i1 immarg, i8 immarg, i8 immarg, i8 immarg, i1 immarg, ...) #2

attributes #0 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { nocallback nofree nounwind willreturn }
attributes #3 = { memory(argmem: write) }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{!"clang"}
!2 = !{!3, !4, i64 8}
!3 = !{!"foo", !4, i64 0, !7, i64 4}
!4 = !{!"int", !5, i64 0}
!5 = !{!"omnipotent char", !6, i64 0}
!6 = !{!"Simple C/C++ TBAA"}
!7 = !{!"bar", !4, i64 0, !4, i64 4}
61 changes: 61 additions & 0 deletions llvm/test/CodeGen/BPF/preserve-static-offset/store-undo-simple.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
; RUN: opt --bpf-check-and-opt-ir -mtriple=bpf-pc-linux -S -o - %s | FileCheck %s
;
; Check unroll of getelementptr.and.store.
;
; Source:
; #define __ctx __attribute__((preserve_static_offset))
;
; struct foo {
; int a;
; int b;
; } __ctx;
;
; extern void consume(int);
;
; void bar(struct foo *p){
; p->b = 42;
; }
;
; Compilation flag:
; clang -cc1 -O2 -triple bpf -S -emit-llvm -disable-llvm-passes -o - \
; | opt -passes=sroa,bpf-preserve-static-offset -S -o -

%struct.foo = type { i32, i32 }

; Function Attrs: nounwind
define dso_local void @bar(ptr noundef %p) #0 {
entry:
call void (i32, ptr, i1, i8, i8, i8, i1, ...)
@llvm.bpf.getelementptr.and.store.i32
(i32 42,
ptr writeonly elementtype(%struct.foo) %p,
i1 false, i8 0, i8 1, i8 2, i1 true, i32 immarg 0, i32 immarg 1)
#3, !tbaa !2
ret void
}

; CHECK: define dso_local void @bar(ptr noundef %[[p:.*]])
; CHECK: %[[v2:.*]] = getelementptr inbounds %struct.foo, ptr %[[p]], i32 0, i32 1
; CHECK: store i32 42, ptr %[[v2]], align 4

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare ptr @llvm.preserve.static.offset(ptr readnone) #1

; Function Attrs: nocallback nofree nounwind willreturn
declare void @llvm.bpf.getelementptr.and.store.i32(i32, ptr nocapture, i1 immarg, i8 immarg, i8 immarg, i8 immarg, i1 immarg, ...) #2

attributes #0 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { nocallback nofree nounwind willreturn }
attributes #3 = { memory(argmem: write) }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{!"clang"}
!2 = !{!3, !4, i64 4}
!3 = !{!"foo", !4, i64 0, !4, i64 4}
!4 = !{!"int", !5, i64 0}
!5 = !{!"omnipotent char", !6, i64 0}
!6 = !{!"Simple C/C++ TBAA"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
; RUN: opt --bpf-check-and-opt-ir -mtriple=bpf-pc-linux -S -o - %s | FileCheck %s
;
; Check that unroll of getelementptr.and.store restores volatile.
;
; Source:
; #define __ctx __attribute__((preserve_static_offset))
;
; struct foo {
; int a;
; volatile int b;
; } __ctx;
;
; extern void consume(int);
;
; void bar(struct foo *p){
; p->b = 42;
; }
;
; Compilation flag:
; clang -cc1 -O2 -triple bpf -S -emit-llvm -disable-llvm-passes -o - \
; | opt -passes=sroa,bpf-preserve-static-offset -S -o -

%struct.foo = type { i32, i32 }

; Function Attrs: nounwind
define dso_local void @bar(ptr noundef %p) #0 {
entry:
call void (i32, ptr, i1, i8, i8, i8, i1, ...)
@llvm.bpf.getelementptr.and.store.i32
(i32 42,
ptr elementtype(%struct.foo) %p,
i1 true, i8 0, i8 1, i8 2, i1 true, i32 immarg 0, i32 immarg 1),
!tbaa !2
ret void
}

; CHECK: define dso_local void @bar(ptr noundef %[[p:.*]])
; CHECK: entry:
; CHECK: %[[v2:.*]] = getelementptr inbounds %struct.foo, ptr %[[p]], i32 0, i32 1
; CHECK: store volatile i32 42, ptr %[[v2]], align 4

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare ptr @llvm.preserve.static.offset(ptr readnone) #1

; Function Attrs: nocallback nofree nounwind willreturn
declare void @llvm.bpf.getelementptr.and.store.i32(i32, ptr nocapture, i1 immarg, i8 immarg, i8 immarg, i8 immarg, i1 immarg, ...) #2

attributes #0 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { nocallback nofree nounwind willreturn }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{!"clang"}
!2 = !{!3, !4, i64 4}
!3 = !{!"foo", !4, i64 0, !4, i64 4}
!4 = !{!"int", !5, i64 0}
!5 = !{!"omnipotent char", !6, i64 0}
!6 = !{!"Simple C/C++ TBAA"}
104 changes: 104 additions & 0 deletions llvm/test/CodeGen/BPF/preserve-static-offset/store-unroll-inline.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
; RUN: opt -O2 -mtriple=bpf-pc-linux -S -o - %s | FileCheck %s
;
; Check position of bpf-preserve-static-offset pass in the pipeline:
; - preserve.static.offset call is preserved if address is passed as
; a parameter to an inline-able function;
; - second bpf-preserve-static-offset pass (after inlining) should introduce
; getelementptr.and.store call using the preserved marker after loops
; unrolling;
; - memory(argmem: readwrite) and tbaa attributes should allow
; removing one getelementptr.and.store call.
;
; Source:
; #define __ctx __attribute__((preserve_static_offset))
;
; struct foo {
; int a;
; int b[4];
; } __ctx;
;
; static inline void bar(int * restrict p, unsigned long i) {
; p[0] = i;
; }
;
; void quux(struct foo *p){
; unsigned long i = 0;
; #pragma clang loop unroll(full)
; while (i < 2) {
; bar(p->b, i);
; ++i;
; }
; }
;
; Compilation flag:
; clang -cc1 -O2 -triple bpf -S -emit-llvm -disable-llvm-passes -o - \
; | opt -passes=function(sroa) -S -o -

%struct.foo = type { i32, [4 x i32] }

; Function Attrs: nounwind
define dso_local void @quux(ptr noundef %p) #0 {
entry:
br label %while.cond

while.cond: ; preds = %while.body, %entry
%i.0 = phi i64 [ 0, %entry ], [ %inc, %while.body ]
%cmp = icmp ult i64 %i.0, 2
br i1 %cmp, label %while.body, label %while.end

while.body: ; preds = %while.cond
%0 = call ptr @llvm.preserve.static.offset(ptr %p)
%b = getelementptr inbounds %struct.foo, ptr %0, i32 0, i32 1
%arraydecay = getelementptr inbounds [4 x i32], ptr %b, i64 0, i64 0
call void @bar(ptr noundef %arraydecay, i64 noundef %i.0)
%inc = add i64 %i.0, 1
br label %while.cond, !llvm.loop !2

while.end: ; preds = %while.cond
ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: inlinehint nounwind
define internal void @bar(ptr noalias noundef %p, i64 noundef %i) #2 {
entry:
%conv = trunc i64 %i to i32
%arrayidx = getelementptr inbounds i32, ptr %p, i64 0
store i32 %conv, ptr %arrayidx, align 4, !tbaa !5
ret void
}

; CHECK: define dso_local void @quux(ptr nocapture noundef writeonly %[[p:.*]])
; CHECK-NEXT: entry:
; CHECK-NEXT: tail call void (i32, ptr, i1, i8, i8, i8, i1, ...)
; CHECK-SAME: @llvm.bpf.getelementptr.and.store.i32
; CHECK-SAME: (i32 1,
; CHECK-SAME: ptr writeonly elementtype(%struct.foo) %[[p]],
; CHECK-SAME: i1 false, i8 0, i8 1, i8 2, i1 true, i64 immarg 0, i32 immarg 1)
; CHECK-NEXT: ret void

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare ptr @llvm.preserve.static.offset(ptr readnone) #3

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

attributes #0 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
attributes #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{!"clang"}
!2 = distinct !{!2, !3, !4}
!3 = !{!"llvm.loop.mustprogress"}
!4 = !{!"llvm.loop.unroll.full"}
!5 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
56 changes: 56 additions & 0 deletions llvm/test/CodeGen/BPF/preserve-static-offset/store-volatile.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
; RUN: opt -O2 -mtriple=bpf-pc-linux -S -o - %s | FileCheck %s
;
; Check handling of a volatile store instruction by bpf-preserve-static-offset.
;
; Source:
; #define __ctx __attribute__((preserve_static_offset))
;
; struct foo {
; int a;
; volatile int b;
; } __ctx;
;
; void bar(struct foo *p) {
; p->b = 42;
; }
;
; Compilation flag:
; clang -cc1 -O2 -triple bpf -S -emit-llvm -disable-llvm-passes -o - \
; | opt -passes=function(sroa) -S -o -

%struct.foo = type { i32, i32 }

; Function Attrs: nounwind
define dso_local void @bar(ptr noundef %p) #0 {
entry:
%0 = call ptr @llvm.preserve.static.offset(ptr %p)
%b = getelementptr inbounds %struct.foo, ptr %0, i32 0, i32 1
store volatile i32 42, ptr %b, align 4, !tbaa !2
ret void
}

; CHECK: define dso_local void @bar(ptr nocapture noundef %[[p:.*]])
; CHECK: tail call void (i32, ptr, i1, i8, i8, i8, i1, ...)
; CHECK-SAME: @llvm.bpf.getelementptr.and.store.i32
; CHECK-SAME: (i32 42,
; CHECK-SAME: ptr elementtype(%struct.foo) %[[p]],
; CHECK-SAME: i1 true, i8 0, i8 1, i8 2, i1 true, i32 immarg 0, i32 immarg 1),
; CHECK-NOT: #{{[0-9]+}}
; CHECK-SAME: !tbaa

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare ptr @llvm.preserve.static.offset(ptr readnone) #1

attributes #0 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{!"clang"}
!2 = !{!3, !4, i64 4}
!3 = !{!"foo", !4, i64 0, !4, i64 4}
!4 = !{!"int", !5, i64 0}
!5 = !{!"omnipotent char", !6, i64 0}
!6 = !{!"Simple C/C++ TBAA"}
51 changes: 51 additions & 0 deletions llvm/test/CodeGen/BPF/preserve-static-offset/store-zero.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
; RUN: opt -O2 -mtriple=bpf-pc-linux -S -o - %s | FileCheck %s
;
; Check that stores from zero offset are not modified by bpf-preserve-static-offset.
;
; Source:
; #define __ctx __attribute__((preserve_static_offset))
;
; struct foo {
; int a;
; } __ctx;
;
; void bar(struct foo *p) {
; p->a = 0;
; }
;
; Compilation flag:
; clang -cc1 -O2 -triple bpf -S -emit-llvm -disable-llvm-passes -o - \
; | opt -passes=function(sroa) -S -o -

%struct.foo = type { i32 }

; Function Attrs: nounwind
define dso_local void @bar(ptr noundef %p) #0 {
entry:
%0 = call ptr @llvm.preserve.static.offset(ptr %p)
%a = getelementptr inbounds %struct.foo, ptr %0, i32 0, i32 0
store i32 0, ptr %a, align 4, !tbaa !2
ret void
}

; CHECK: define dso_local void @bar(ptr nocapture noundef writeonly %[[p:.*]])
; CHECK-NEXT: entry:
; CHECK-NEXT: store i32 0, ptr %[[p]], align 4, !tbaa
; CHECK-NEXT: ret void

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare ptr @llvm.preserve.static.offset(ptr readnone) #1

attributes #0 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{!"clang"}
!2 = !{!3, !4, i64 0}
!3 = !{!"foo", !4, i64 0}
!4 = !{!"int", !5, i64 0}
!5 = !{!"omnipotent char", !6, i64 0}
!6 = !{!"Simple C/C++ TBAA"}