221 lines · plain
1; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/gcc-simple.afdo -S | FileCheck %s2;3; Original code:4;5; #include <stdlib.h>6;7; long long int foo(long i) {8; if (rand() < 500) return 2; else if (rand() > 5000) return 10; else return 90;9; }10;11; int main() {12; long long int sum = 0;13; for (int k = 0; k < 3000; k++)14; for (int i = 0; i < 200000; i++) sum += foo(i);15; return sum > 0 ? 0 : 1;16; }17;18; This test was compiled down to bytecode at -O0 to avoid inlining foo() into19; main(). The profile was generated using a GCC-generated binary (also compiled20; at -O0). The conversion from the Linux Perf profile to the GCC autofdo21; profile used the converter at https://github.com/google/autofdo22;23; $ gcc -g -O0 gcc-simple.cc -o gcc-simple24; $ perf record -b ./gcc-simple25; $ create_gcov --binary=gcc-simple --gcov=gcc-simple.afdo26 27define i64 @_Z3fool(i64 %i) #0 !dbg !4 {28; CHECK: !prof ![[EC1:[0-9]+]]29entry:30 %retval = alloca i64, align 831 %i.addr = alloca i64, align 832 store i64 %i, ptr %i.addr, align 833 call void @llvm.dbg.declare(metadata ptr %i.addr, metadata !16, metadata !17), !dbg !1834 %call = call i32 @rand(), !dbg !1935; CHECK: !prof ![[PROF1:[0-9]+]]36 %cmp = icmp slt i32 %call, 500, !dbg !2137 br i1 %cmp, label %if.then, label %if.else, !dbg !2238; CHECK: !prof ![[PROF2:[0-9]+]]39 40if.then: ; preds = %entry41 store i64 2, ptr %retval, align 8, !dbg !2342 br label %return, !dbg !2343 44if.else: ; preds = %entry45 %call1 = call i32 @rand(), !dbg !2546; CHECK: !prof ![[PROF3:[0-9]+]]47 %cmp2 = icmp sgt i32 %call1, 5000, !dbg !2848 br i1 %cmp2, label %if.then.3, label %if.else.4, !dbg !2949; CHECK: !prof ![[PROF4:[0-9]+]]50 51if.then.3: ; preds = %if.else52 store i64 10, ptr %retval, align 8, !dbg !3053 br label %return, !dbg !3054 55if.else.4: ; preds = %if.else56 store i64 90, ptr %retval, align 8, !dbg !3257 br label %return, !dbg !3258 59return: ; preds = %if.else.4, %if.then.3, %if.then60 %0 = load i64, ptr %retval, align 8, !dbg !3461 ret i64 %0, !dbg !3462}63 64; Function Attrs: nounwind readnone65declare void @llvm.dbg.declare(metadata, metadata, metadata)66 67; Function Attrs: nounwind68declare i32 @rand()69 70; Function Attrs: nounwind uwtable71define i32 @main() #0 !dbg !9 {72; CHECK: !prof ![[EC2:[0-9]+]]73entry:74 %retval = alloca i32, align 475 %sum = alloca i64, align 876 %k = alloca i32, align 477 %i = alloca i32, align 478 store i32 0, ptr %retval, align 479 call void @llvm.dbg.declare(metadata ptr %sum, metadata !35, metadata !17), !dbg !3680 store i64 0, ptr %sum, align 8, !dbg !3681 call void @llvm.dbg.declare(metadata ptr %k, metadata !37, metadata !17), !dbg !3982 store i32 0, ptr %k, align 4, !dbg !3983 br label %for.cond, !dbg !4084 85for.cond: ; preds = %for.inc.4, %entry86 %0 = load i32, ptr %k, align 4, !dbg !4187 %cmp = icmp slt i32 %0, 3000, !dbg !4588 br i1 %cmp, label %for.body, label %for.end.6, !dbg !4689; CHECK: !prof ![[PROF6:[0-9]+]]90 91for.body: ; preds = %for.cond92 call void @llvm.dbg.declare(metadata ptr %i, metadata !47, metadata !17), !dbg !4993 store i32 0, ptr %i, align 4, !dbg !4994 br label %for.cond.1, !dbg !5095 96for.cond.1: ; preds = %for.inc, %for.body97 %1 = load i32, ptr %i, align 4, !dbg !5198 %cmp2 = icmp slt i32 %1, 200000, !dbg !5599 br i1 %cmp2, label %for.body.3, label %for.end, !dbg !56100; CHECK: !prof ![[PROF7:[0-9]+]]101 102for.body.3: ; preds = %for.cond.1103 %2 = load i32, ptr %i, align 4, !dbg !57104 %conv = sext i32 %2 to i64, !dbg !57105 %call = call i64 @_Z3fool(i64 %conv), !dbg !59106; CHECK: !prof ![[PROF8:[0-9]+]]107 %3 = load i64, ptr %sum, align 8, !dbg !60108 %add = add nsw i64 %3, %call, !dbg !60109 store i64 %add, ptr %sum, align 8, !dbg !60110 br label %for.inc, !dbg !61111 112for.inc: ; preds = %for.body.3113 %4 = load i32, ptr %i, align 4, !dbg !62114 %inc = add nsw i32 %4, 1, !dbg !62115 store i32 %inc, ptr %i, align 4, !dbg !62116 br label %for.cond.1, !dbg !64117 118for.end: ; preds = %for.cond.1119 br label %for.inc.4, !dbg !65120 121for.inc.4: ; preds = %for.end122 %5 = load i32, ptr %k, align 4, !dbg !67123 %inc5 = add nsw i32 %5, 1, !dbg !67124 store i32 %inc5, ptr %k, align 4, !dbg !67125 br label %for.cond, !dbg !68126 127for.end.6: ; preds = %for.cond128 %6 = load i64, ptr %sum, align 8, !dbg !69129 %cmp7 = icmp sgt i64 %6, 0, !dbg !70130 %cond = select i1 %cmp7, i32 0, i32 1, !dbg !69131 ret i32 %cond, !dbg !71132}133 134; CHECK: ![[EC1]] = !{!"function_entry_count", i64 24109}135; CHECK: ![[PROF1]] = !{!"branch_weights", i32 24109}136; CHECK: ![[PROF2]] = !{!"branch_weights", i32 30125, i32 30125}137; CHECK: ![[PROF3]] = !{!"branch_weights", i32 30124}138; CHECK: ![[PROF4]] = !{!"branch_weights", i32 30125, i32 29580}139; CHECK: ![[EC2]] = !{!"function_entry_count", i64 1}140; CHECK: ![[PROF6]] = !{!"branch_weights", i32 1, i32 2}141; CHECK: ![[PROF7]] = !{!"branch_weights", i32 18943, i32 1}142; CHECK: ![[PROF8]] = !{!"branch_weights", i32 18942}143 144attributes #0 = { nounwind uwtable "use-sample-profile" }145 146!llvm.dbg.cu = !{!0}147!llvm.module.flags = !{!13, !14}148!llvm.ident = !{!15}149 150!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 3.8.0 (trunk 247554) (llvm/trunk 247557)", isOptimized: false, runtimeVersion: 0, emissionKind: NoDebug, enums: !2)151!1 = !DIFile(filename: "discriminator.cc", directory: "/usr/local/google/home/dnovillo/llvm/test/autofdo")152!2 = !{}153!4 = distinct !DISubprogram(name: "foo", linkageName: "_Z3fool", scope: !1, file: !1, line: 3, type: !5, isLocal: false, isDefinition: true, scopeLine: 3, flags: DIFlagPrototyped, isOptimized: false, unit: !0, retainedNodes: !2)154!5 = !DISubroutineType(types: !6)155!6 = !{!7, !8}156!7 = !DIBasicType(name: "long long int", size: 64, align: 64, encoding: DW_ATE_signed)157!8 = !DIBasicType(name: "long int", size: 64, align: 64, encoding: DW_ATE_signed)158!9 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 7, type: !10, isLocal: false, isDefinition: true, scopeLine: 7, flags: DIFlagPrototyped, isOptimized: false, unit: !0, retainedNodes: !2)159!10 = !DISubroutineType(types: !11)160!11 = !{!12}161!12 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed)162!13 = !{i32 2, !"Dwarf Version", i32 4}163!14 = !{i32 2, !"Debug Info Version", i32 3}164!15 = !{!"clang version 3.8.0 (trunk 247554) (llvm/trunk 247557)"}165!16 = !DILocalVariable(name: "i", arg: 1, scope: !4, file: !1, line: 3, type: !8)166!17 = !DIExpression()167!18 = !DILocation(line: 3, column: 24, scope: !4)168!19 = !DILocation(line: 4, column: 7, scope: !20)169!20 = distinct !DILexicalBlock(scope: !4, file: !1, line: 4, column: 7)170!21 = !DILocation(line: 4, column: 14, scope: !20)171!22 = !DILocation(line: 4, column: 7, scope: !4)172!23 = !DILocation(line: 4, column: 21, scope: !24)173!24 = !DILexicalBlockFile(scope: !20, file: !1, discriminator: 1)174!25 = !DILocation(line: 4, column: 40, scope: !26)175!26 = !DILexicalBlockFile(scope: !27, file: !1, discriminator: 2)176!27 = distinct !DILexicalBlock(scope: !20, file: !1, line: 4, column: 40)177!28 = !DILocation(line: 4, column: 47, scope: !27)178!29 = !DILocation(line: 4, column: 40, scope: !20)179!30 = !DILocation(line: 4, column: 55, scope: !31)180!31 = !DILexicalBlockFile(scope: !27, file: !1, discriminator: 3)181!32 = !DILocation(line: 4, column: 71, scope: !33)182!33 = !DILexicalBlockFile(scope: !27, file: !1, discriminator: 4)183!34 = !DILocation(line: 5, column: 1, scope: !4)184!35 = !DILocalVariable(name: "sum", scope: !9, file: !1, line: 8, type: !7)185!36 = !DILocation(line: 8, column: 17, scope: !9)186!37 = !DILocalVariable(name: "k", scope: !38, file: !1, line: 9, type: !12)187!38 = distinct !DILexicalBlock(scope: !9, file: !1, line: 9, column: 3)188!39 = !DILocation(line: 9, column: 12, scope: !38)189!40 = !DILocation(line: 9, column: 8, scope: !38)190!41 = !DILocation(line: 9, column: 19, scope: !42)191!42 = !DILexicalBlockFile(scope: !43, file: !1, discriminator: 2)192!43 = !DILexicalBlockFile(scope: !44, file: !1, discriminator: 1)193!44 = distinct !DILexicalBlock(scope: !38, file: !1, line: 9, column: 3)194!45 = !DILocation(line: 9, column: 21, scope: !44)195!46 = !DILocation(line: 9, column: 3, scope: !38)196!47 = !DILocalVariable(name: "i", scope: !48, file: !1, line: 10, type: !12)197!48 = distinct !DILexicalBlock(scope: !44, file: !1, line: 10, column: 5)198!49 = !DILocation(line: 10, column: 14, scope: !48)199!50 = !DILocation(line: 10, column: 10, scope: !48)200!51 = !DILocation(line: 10, column: 21, scope: !52)201!52 = !DILexicalBlockFile(scope: !53, file: !1, discriminator: 5)202!53 = !DILexicalBlockFile(scope: !54, file: !1, discriminator: 1)203!54 = distinct !DILexicalBlock(scope: !48, file: !1, line: 10, column: 5)204!55 = !DILocation(line: 10, column: 23, scope: !54)205!56 = !DILocation(line: 10, column: 5, scope: !48)206!57 = !DILocation(line: 10, column: 49, scope: !58)207!58 = !DILexicalBlockFile(scope: !54, file: !1, discriminator: 2)208!59 = !DILocation(line: 10, column: 45, scope: !54)209!60 = !DILocation(line: 10, column: 42, scope: !54)210!61 = !DILocation(line: 10, column: 38, scope: !54)211!62 = !DILocation(line: 10, column: 34, scope: !63)212!63 = !DILexicalBlockFile(scope: !54, file: !1, discriminator: 4)213!64 = !DILocation(line: 10, column: 5, scope: !54)214!65 = !DILocation(line: 10, column: 50, scope: !66)215!66 = !DILexicalBlockFile(scope: !48, file: !1, discriminator: 3)216!67 = !DILocation(line: 9, column: 30, scope: !44)217!68 = !DILocation(line: 9, column: 3, scope: !44)218!69 = !DILocation(line: 11, column: 10, scope: !9)219!70 = !DILocation(line: 11, column: 14, scope: !9)220!71 = !DILocation(line: 11, column: 3, scope: !9)221