1// RUN: %clang_pgogen -O2 -mllvm -pgo-function-entry-coverage -o %t %s
2// RUN: %run %t %t.profraw 1 1
3// RUN: llvm-profdata show --all-functions --counts %t.profraw | FileCheck %s
4
5// FIXME: llvm-profdata exits with "Malformed instrumentation profile data"
6// XFAIL: target={{.*windows.*}}
7
8#include "profile_test.h"
9#include <stdint.h>
10#include <stdio.h>
11#include <stdlib.h>
12
13int __llvm_profile_runtime = 0;
14uint64_t __llvm_profile_get_size_for_buffer(void);
15int __llvm_profile_write_buffer(char *);
16void __llvm_profile_reset_counters(void);
17int __llvm_profile_merge_from_buffer(const char *, uint64_t);
18
19__attribute__((noinline)) int dumpBuffer(const char *FileN, const char *Buffer,
20 uint64_t Size) {
21 FILE *File = fopen(filename: FileN, modes: "w");
22 if (!File)
23 return 1;
24 if (fwrite(ptr: Buffer, size: 1, n: Size, s: File) != Size)
25 return 1;
26 return fclose(stream: File);
27}
28
29int g = 0;
30__attribute__((noinline)) void foo(char c) {
31 if (c == '1')
32 g++;
33 else
34 g--;
35}
36
37/* This function is not profiled */
38__attribute__((noinline)) void bar(int M) { g += M; }
39
40int main(int argc, const char *argv[]) {
41 int i;
42 if (argc < 4)
43 return 1;
44
45 const uint64_t MaxSize = 10000;
46 static ALIGNED(sizeof(uint64_t)) char Buffer[MaxSize];
47
48 uint64_t Size = __llvm_profile_get_size_for_buffer();
49 if (Size > MaxSize)
50 return 1;
51
52 /* Start profiling. */
53 __llvm_profile_reset_counters();
54 foo(c: argv[2][0]);
55 /* End profiling by freezing counters. */
56 if (__llvm_profile_write_buffer(Buffer))
57 return 1;
58
59 /* Its profile will be discarded. */
60 for (i = 0; i < 10; i++)
61 bar(M: 1);
62
63 /* Start profiling again and merge in previously
64 saved counters in buffer. */
65 __llvm_profile_reset_counters();
66 __llvm_profile_merge_from_buffer(Buffer, Size);
67 foo(c: argv[3][0]);
68 /* End profiling */
69 if (__llvm_profile_write_buffer(Buffer))
70 return 1;
71
72 /* Its profile will be discarded. */
73 bar(M: 2);
74
75 /* Now it is time to dump the profile to file. */
76 return dumpBuffer(FileN: argv[1], Buffer, Size);
77}
78
79// CHECK-LABEL: dumpBuffer:
80// CHECK: Counters: 1
81// CHECK-NEXT: Block counts: [0]
82
83// CHECK-LABEL: foo:
84// CHECK: Counters: 1
85// CHECK-NEXT: Block counts: [1]
86
87// CHECK-LABEL: bar:
88// CHECK: Counters: 1
89// CHECK-NEXT: Block counts: [0]
90
91// CHECK-LABEL: main:
92// CHECK: Counters: 1
93// CHECK-NEXT: Block counts: [0]
94

source code of compiler-rt/test/profile/instrprof-merge-entry-cover.c