1// RUN: %clang_profgen -DCHECK_SYMBOLS -O3 -o %t.symbols %s
2// RUN: llvm-nm %t.symbols | FileCheck %s --check-prefix=CHECK-SYMBOLS
3// RUN: %clang_profgen -O3 -o %t %s
4// RUN: %run %t %t.profraw
5// RUN: llvm-profdata merge -o %t.profdata %t.profraw
6// RUN: %clang_profuse=%t.profdata -o - -S -emit-llvm %s | FileCheck %s
7
8// This usage of llvm-nm assumes executables have symbol tables. They do not in
9// an MSVC environment, so we can't make this test portable.
10// UNSUPPORTED: target={{.*msvc.*}}
11
12// The MinGW CRT init files do reference malloc etc, so this test fails.
13// UNSUPPORTED: target={{.*windows-gnu.*}}
14
15#include <stdint.h>
16#include <stdlib.h>
17
18#ifndef CHECK_SYMBOLS
19#include <stdio.h>
20#endif
21
22int __llvm_profile_runtime = 0;
23uint64_t __llvm_profile_get_size_for_buffer(void);
24int __llvm_profile_write_buffer(char *);
25int __llvm_profile_merge_from_buffer(const char *, uint64_t Size);
26
27int write_buffer(uint64_t, const char *);
28int main(int argc, const char *argv[]) {
29 // CHECK-LABEL: define {{.*}} @main(
30 // CHECK: br i1 %{{.*}}, label %{{.*}}, label %{{.*}}, !prof ![[PD1:[0-9]+]]
31 if (argc < 2)
32 return 1;
33
34 const uint64_t MaxSize = 10000;
35 static char Buffer[MaxSize];
36
37 uint64_t Size = __llvm_profile_get_size_for_buffer();
38 if (Size > MaxSize)
39 return 1;
40 int Write = __llvm_profile_write_buffer(Buffer);
41 if (Write)
42 return Write;
43
44#ifdef CHECK_SYMBOLS
45 // Don't write it out. Since we're checking the symbols, we don't have libc
46 // available.
47 // Call merge function to make sure it does not bring in libc deps:
48 __llvm_profile_merge_from_buffer(Buffer, Size);
49 return 0;
50#else
51 // Actually write it out so we can FileCheck the output.
52 FILE *File = fopen(filename: argv[1], modes: "w");
53 if (!File)
54 return 1;
55 if (fwrite(ptr: Buffer, size: 1, n: Size, s: File) != Size)
56 return 1;
57 return fclose(stream: File);
58#endif
59}
60// CHECK: ![[PD1]] = !{!"branch_weights", i32 1, i32 2}
61
62// CHECK-SYMBOLS-NOT: {{ }}___cxx_global_var_init
63// CHECK-SYMBOLS-NOT: {{ }}___llvm_profile_register_write_file_atexit
64// CHECK-SYMBOLS-NOT: {{ }}___llvm_profile_set_filename
65// CHECK-SYMBOLS-NOT: {{ }}___llvm_profile_write_file
66// CHECK-SYMBOLS-NOT: {{ }}_fdopen
67// CHECK-SYMBOLS-NOT: {{ }}_fopen
68// CHECK-SYMBOLS-NOT: {{ }}_fwrite
69// CHECK-SYMBOLS-NOT: {{ }}_getenv
70// CHECK-SYMBOLS-NOT: {{ }}getenv
71// CHECK-SYMBOLS-NOT: {{ }}_malloc
72// CHECK-SYMBOLS-NOT: {{ }}malloc
73// CHECK-SYMBOLS-NOT: {{ }}_calloc
74// CHECK-SYMBOLS-NOT: {{ }}calloc
75// CHECK-SYMBOLS-NOT: {{ }}_free
76// CHECK-SYMBOLS-NOT: {{ }}free
77// CHECK-SYMBOLS-NOT: {{ }}_open
78// CHECK-SYMBOLS-NOT: {{ }}_getpagesize
79

source code of compiler-rt/test/profile/instrprof-without-libc.c