xref: /llvm-project/llvm/test/CodeGen/BPF/preserve-static-offset/load-non-const.ll (revision 030b8cb1561db4161b108b59044717d89026cf70)
1; RUN: opt -passes=bpf-preserve-static-offset -mtriple=bpf-pc-linux -S -o - %s 2>&1 | FileCheck %s
2;
3; If load offset is not a constant bpf-preserve-static-offset should report a
4; warning and remove preserve.static.offset call.
5;
6; Source:
7;    #define __ctx __attribute__((preserve_static_offset))
8;
9;    struct foo {
10;      int a[7];
11;    } __ctx;
12;
13;    extern void consume(int);
14;
15;    void bar(struct foo *p, unsigned long i) {
16;      consume(p->a[i]);
17;    }
18;
19; Compilation flag:
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 -
23
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
27
28%struct.foo = type { [7 x i32] }
29
30; Function Attrs: nounwind
31define dso_local void @bar(ptr noundef %p, i64 noundef %i) #0 !dbg !5 {
32entry:
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
38  ret void, !dbg !15
39}
40
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
46
47declare void @consume(i32 noundef) #1
48
49; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
50declare ptr @llvm.preserve.static.offset(ptr readnone) #2
51
52attributes #0 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
53attributes #1 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
54attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
55
56!llvm.dbg.cu = !{!0}
57!llvm.module.flags = !{!2, !3}
58!llvm.ident = !{!4}
59
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}
64!4 = !{!"clang"}
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)
67!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)
76