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