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