1 ; RUN: opt -passes=bpf-preserve-static-offset -mtriple=bpf-pc-linux -S -o - %s 2>&1 | FileCheck %s
3 ; If load offset is not a constant bpf-preserve-static-offset should report a
4 ; warning and remove preserve.static.offset call.
7 ; #define __ctx __attribute__((preserve_static_offset))
13 ; extern void consume(int);
15 ; void bar(struct foo *p, unsigned long i) {
20 ; clang -cc1 -O2 -debug-info-kind=line-tables-only -triple bpf \
21 ; -S -emit-llvm -disable-llvm-passes -o - \
22 ; | opt -passes=function(sroa) -S -o -
24 ; CHECK: warning: some-file.c:10:11: in function bar void (ptr, i64):
25 ; CHECK-SAME: Non-constant offset in access to a field of a type marked with
26 ; CHECK-SAME: preserve_static_offset might be rejected by BPF verifier
28 %struct.foo = type { [7 x i32] }
30 ; Function Attrs: nounwind
31 define dso_local void @bar(ptr noundef %p, i64 noundef %i) #0 !dbg !5 {
33 %0 = call ptr @llvm.preserve.static.offset(ptr %p), !dbg !8
34 %a = getelementptr inbounds %struct.foo, ptr %0, i32 0, i32 0, !dbg !8
35 %arrayidx = getelementptr inbounds [7 x i32], ptr %a, i64 0, i64 %i, !dbg !9
36 %1 = load i32, ptr %arrayidx, align 4, !dbg !9, !tbaa !10
37 call void @consume(i32 noundef %1), !dbg !14
41 ; CHECK: define dso_local void @bar(ptr noundef %[[p:.*]], i64 noundef %[[i:.*]])
42 ; CHECK: %[[a:.*]] = getelementptr inbounds %struct.foo, ptr %[[p]], i32 0, i32 0, !dbg
43 ; CHECK-NEXT: %[[arrayidx:.*]] = getelementptr inbounds [7 x i32], ptr %[[a]], i64 0, i64 %[[i]], !dbg
44 ; CHECK-NEXT: %[[v5:.*]] = load i32, ptr %[[arrayidx]], align 4, !dbg {{.*}}, !tbaa
45 ; CHECK-NEXT: call void @consume(i32 noundef %[[v5]]), !dbg
47 declare void @consume(i32 noundef) #1
49 ; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
50 declare ptr @llvm.preserve.static.offset(ptr readnone) #2
52 attributes #0 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
53 attributes #1 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
54 attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
57 !llvm.module.flags = !{!2, !3}
60 !0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: LineTablesOnly, splitDebugInlining: false, nameTableKind: None)
61 !1 = !DIFile(filename: "some-file.c", directory: "/some/dir/")
62 !2 = !{i32 2, !"Debug Info Version", i32 3}
63 !3 = !{i32 1, !"wchar_size", i32 4}
65 !5 = distinct !DISubprogram(name: "bar", scope: !1, file: !1, line: 9, type: !6, scopeLine: 9, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
66 !6 = !DISubroutineType(types: !7)
68 !8 = !DILocation(line: 10, column: 14, scope: !5)
69 !9 = !DILocation(line: 10, column: 11, scope: !5)
70 !10 = !{!11, !11, i64 0}
71 !11 = !{!"int", !12, i64 0}
72 !12 = !{!"omnipotent char", !13, i64 0}
73 !13 = !{!"Simple C/C++ TBAA"}
74 !14 = !DILocation(line: 10, column: 3, scope: !5)
75 !15 = !DILocation(line: 11, column: 1, scope: !5)