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"
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
18 cat > ${OUTDIR}/memprof.cc
<< EOF
34 char *recurse(unsigned n) {
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];
55 // a and c have short lifetimes
58 // b, d, e, and f have long lifetimes and will be detected as cold by default.
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);
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
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
104 cat > ${OUTDIR}/memprof_missing_leaf.cc
<< EOF
107 #define USE_MUSTTAIL 0
110 // clang::musttail requires that the argument signature matches that of the caller.
111 void *bar(std::size_t s) {
113 [[clang::musttail]] return ::operator new (s);
120 char *a = (char *)bar(1);
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
138 int main(int argc, char **argv) {
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