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: msvc
11
12#include <stdint.h>
13#include <stdlib.h>
14
15#ifndef CHECK_SYMBOLS
16#include <stdio.h>
17#endif
18
19int __llvm_profile_runtime = 0;
20uint64_t __llvm_profile_get_size_for_buffer(void);
21int __llvm_profile_write_buffer(char *);
22void __llvm_profile_merge_from_buffer(const char *, uint64_t Size);
23
24int write_buffer(uint64_t, const char *);
25int main(int argc, const char *argv[]) {
26 // CHECK-LABEL: define {{.*}} @main(
27 // CHECK: br i1 %{{.*}}, label %{{.*}}, label %{{.*}}, !prof ![[PD1:[0-9]+]]
28 if (argc < 2)
29 return 1;
30
31 const uint64_t MaxSize = 10000;
32 static char Buffer[MaxSize];
33
34 uint64_t Size = __llvm_profile_get_size_for_buffer();
35 if (Size > MaxSize)
36 return 1;
37 int Write = __llvm_profile_write_buffer(Buffer);
38 if (Write)
39 return Write;
40
41#ifdef CHECK_SYMBOLS
42 // Don't write it out. Since we're checking the symbols, we don't have libc
43 // available.
44 // Call merge function to make sure it does not bring in libc deps:
45 __llvm_profile_merge_from_buffer(Buffer, Size);
46 return 0;
47#else
48 // Actually write it out so we can FileCheck the output.
49 FILE *File = fopen(argv[1], "w");
50 if (!File)
51 return 1;
52 if (fwrite(Buffer, 1, Size, File) != Size)
53 return 1;
54 return fclose(File);
55#endif
56}
57// CHECK: ![[PD1]] = !{!"branch_weights", i32 1, i32 2}
58
59// CHECK-SYMBOLS-NOT: {{ }}___cxx_global_var_init
60// CHECK-SYMBOLS-NOT: {{ }}___llvm_profile_register_write_file_atexit
61// CHECK-SYMBOLS-NOT: {{ }}___llvm_profile_set_filename
62// CHECK-SYMBOLS-NOT: {{ }}___llvm_profile_write_file
63// CHECK-SYMBOLS-NOT: {{ }}_fdopen
64// CHECK-SYMBOLS-NOT: {{ }}_fopen
65// CHECK-SYMBOLS-NOT: {{ }}_fwrite
66// CHECK-SYMBOLS-NOT: {{ }}_getenv
67// CHECK-SYMBOLS-NOT: {{ }}getenv
68// CHECK-SYMBOLS-NOT: {{ }}_malloc
69// CHECK-SYMBOLS-NOT: {{ }}malloc
70// CHECK-SYMBOLS-NOT: {{ }}_calloc
71// CHECK-SYMBOLS-NOT: {{ }}calloc
72// CHECK-SYMBOLS-NOT: {{ }}_free
73// CHECK-SYMBOLS-NOT: {{ }}free
74// CHECK-SYMBOLS-NOT: {{ }}_open
75// CHECK-SYMBOLS-NOT: {{ }}_getpagesize
76