Follow up to d0858bffa11, add missing REQUIRES x86
[llvm-project.git] / llvm / test / Transforms / PGOProfile / Inputs / update_memprof_inputs.sh
blob74e7e36641b43f78a15785658fa5d901dde737a9
1 #!/bin/bash
3 if [ $# -lt 2 ]; then
4 echo "Path to clang and llvm-profdata required!"
5 echo "Usage: update_memprof_inputs.sh /path/to/updated/clang /path/to/updated/llvm-profdata"
6 exit 1
7 else
8 CLANG=$1
9 LLVMPROFDATA=$2
12 # Allows the script to be invoked from other directories.
13 OUTDIR=$(dirname $(realpath -s $0))
15 # Note that changes in the code below which affect relative line number
16 # offsets of calls from their parent function can affect callsite matching in
17 # the LLVM IR.
18 cat > ${OUTDIR}/memprof.cc << EOF
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 char *foo() {
23 return new char[10];
25 char *foo2() {
26 return foo();
28 char *bar() {
29 return foo2();
31 char *baz() {
32 return foo2();
34 char *recurse(unsigned n) {
35 if (!n)
36 return foo();
37 return recurse(n-1);
39 int main(int argc, char **argv) {
40 // Test allocations with different combinations of stack contexts and
41 // coldness (based on lifetime, since they are all accessed a single time
42 // per byte via the memset).
43 char *a = new char[10];
44 char *b = new char[10];
45 char *c = foo();
46 char *d = foo();
47 char *e = bar();
48 char *f = baz();
49 memset(a, 0, 10);
50 memset(b, 0, 10);
51 memset(c, 0, 10);
52 memset(d, 0, 10);
53 memset(e, 0, 10);
54 memset(f, 0, 10);
55 // a and c have short lifetimes
56 delete[] a;
57 delete[] c;
58 // b, d, e, and f have long lifetimes and will be detected as cold by default.
59 sleep(200);
60 delete[] b;
61 delete[] d;
62 delete[] e;
63 delete[] f;
65 // Loop ensures the two calls to recurse have stack contexts that only differ
66 // in one level of recursion. We should get two stack contexts reflecting the
67 // different levels of recursion and different allocation behavior (since the
68 // first has a very long lifetime and the second has a short lifetime).
69 for (unsigned i = 0; i < 2; i++) {
70 char *g = recurse(i + 3);
71 memset(g, 0, 10);
72 if (!i)
73 sleep(200);
74 delete[] g;
76 return 0;
78 EOF
80 COMMON_FLAGS="-fuse-ld=lld -Wl,--no-rosegment -gmlt -fdebug-info-for-profiling -mno-omit-leaf-frame-pointer -fno-omit-frame-pointer -fno-optimize-sibling-calls -m64 -Wl,-build-id -no-pie"
82 ${CLANG} ${COMMON_FLAGS} -fmemory-profile ${OUTDIR}/memprof.cc -o ${OUTDIR}/memprof.exe
83 env MEMPROF_OPTIONS=log_path=stdout ${OUTDIR}/memprof.exe > ${OUTDIR}/memprof.memprofraw
85 # Generate another profile without any column numbers.
86 ${CLANG} ${COMMON_FLAGS} -gno-column-info -fmemory-profile ${OUTDIR}/memprof.cc -o ${OUTDIR}/memprof.nocolinfo.exe
87 env MEMPROF_OPTIONS=log_path=stdout ${OUTDIR}/memprof.nocolinfo.exe > ${OUTDIR}/memprof.nocolinfo.memprofraw
89 ${CLANG} ${COMMON_FLAGS} -fprofile-generate=. \
90 ${OUTDIR}/memprof.cc -o ${OUTDIR}/pgo.exe
91 env LLVM_PROFILE_FILE=${OUTDIR}/memprof_pgo.profraw ${OUTDIR}/pgo.exe
92 ${LLVMPROFDATA} merge --text ${OUTDIR}/memprof_pgo.profraw -o ${OUTDIR}/memprof_pgo.proftext
94 rm ${OUTDIR}/memprof.cc
95 rm ${OUTDIR}/pgo.exe
96 rm ${OUTDIR}/memprof_pgo.profraw
98 # Use musttail to simulate a missing leaf debug frame in the profiled binary.
99 # Note we don't currently match onto explicit ::operator new calls, which is
100 # why the non-musttail case uses implicit new (which doesn't support musttail).
101 # Note that changes in the code below which affect relative line number
102 # offsets of calls from their parent function can affect callsite matching in
103 # the LLVM IR.
104 cat > ${OUTDIR}/memprof_missing_leaf.cc << EOF
105 #include <new>
106 #ifndef USE_MUSTTAIL
107 #define USE_MUSTTAIL 0
108 #endif
110 // clang::musttail requires that the argument signature matches that of the caller.
111 void *bar(std::size_t s) {
112 #if USE_MUSTTAIL
113 [[clang::musttail]] return ::operator new (s);
114 #else
115 return new char[s];
116 #endif
119 int main() {
120 char *a = (char *)bar(1);
121 delete a;
122 return 0;
126 ${CLANG} ${COMMON_FLAGS} -fmemory-profile -DUSE_MUSTTAIL=1 ${OUTDIR}/memprof_missing_leaf.cc -o ${OUTDIR}/memprof_missing_leaf.exe
127 env MEMPROF_OPTIONS=log_path=stdout ${OUTDIR}/memprof_missing_leaf.exe > ${OUTDIR}/memprof_missing_leaf.memprofraw
129 rm ${OUTDIR}/memprof_missing_leaf.cc
131 cat > ${OUTDIR}/memprof_internal_linkage.cc << EOF
132 #include <cstring>
133 #include <unistd.h>
134 static void foo() {
135 int *a = new int[5];
136 memset(a, 0, 5);
138 int main(int argc, char **argv) {
139 foo();
140 return 0;
144 ${CLANG} ${COMMON_FLAGS} -fmemory-profile -funique-internal-linkage-names ${OUTDIR}/memprof_internal_linkage.cc -o ${OUTDIR}/memprof_internal_linkage.exe
145 env MEMPROF_OPTIONS=log_path=stdout ${OUTDIR}/memprof_internal_linkage.exe > ${OUTDIR}/memprof_internal_linkage.memprofraw
147 rm ${OUTDIR}/memprof_internal_linkage.cc