Skip to content

Commit 7dcff8f

Browse files
committed
build-all: Add support for PGO builds
Allow both doing one single stage at a time, or driving the full build with three stages all in one command.
1 parent d3c7a1b commit 7dcff8f

File tree

1 file changed

+106
-4
lines changed

1 file changed

+106
-4
lines changed

build-all.sh

Lines changed: 106 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,55 @@ while [ $# -gt 0 ]; do
7676
--llvm-only)
7777
LLVM_ONLY=1
7878
;;
79+
--stage1)
80+
STAGE1=1
81+
LLVM_ARGS="$LLVM_ARGS --disable-lldb --disable-clang-tools-extra"
82+
NO_LLDB=1
83+
;;
84+
--profile|--profile=*)
85+
case "$1" in
86+
--profile=*)
87+
INSTRUMENTATION="=${1#*=}"
88+
;;
89+
esac
90+
PROFILE=1
91+
LLVM_ARGS="$LLVM_ARGS --disable-lldb --disable-clang-tools-extra --with-clang --disable-dylib --instrumented$INSTRUMENTATION"
92+
NO_LLDB=1
93+
LLVM_ONLY=1
94+
;;
95+
--pgo|--pgo=*)
96+
PGO=1
97+
LLVM_ARGS="$LLVM_ARGS --with-clang $1"
98+
;;
99+
--full-pgo|--full-pgo=*)
100+
case "$1" in
101+
--full-pgo=*)
102+
INSTRUMENTATION="=${1#*=}"
103+
;;
104+
esac
105+
PGO=1
106+
FULL_PGO=1
107+
;;
79108
*)
80109
if [ -n "$PREFIX" ]; then
81-
echo Unrecognized parameter $1
82-
exit 1
110+
if [ -n "$PREFIX_PGO" ]; then
111+
echo Unrecognized parameter $1
112+
exit 1
113+
fi
114+
PREFIX_PGO="$1"
115+
else
116+
PREFIX="$1"
83117
fi
84-
PREFIX="$1"
85118
;;
86119
esac
87120
shift
88121
done
89122
if [ -z "$PREFIX" ]; then
90-
echo "$0 [--host-clang[=clang]] [--enable-asserts] [--disable-dylib] [--with-clang] [--thinlto] [--full-llvm] [--disable-lldb] [--disable-lldb-mi] [--disable-clang-tools-extra] [--host=triple] [--with-default-win32-winnt=0x601] [--with-default-msvcrt=ucrt] [--enable-cfguard|--disable-cfguard] [--no-runtimes] [--llvm-only] [--no-tools] [--wipe-runtimes] [--clean-runtimes] dest"
123+
echo "$0 [--host-clang[=clang]] [--enable-asserts] [--disable-dylib] [--with-clang] [--thinlto] [--full-llvm] [--disable-lldb] [--disable-lldb-mi] [--disable-clang-tools-extra] [--host=triple] [--with-default-win32-winnt=0x601] [--with-default-msvcrt=ucrt] [--enable-cfguard|--disable-cfguard] [--no-runtimes] [--llvm-only] [--no-tools] [--wipe-runtimes] [--clean-runtimes] [--stage1] [--profile[=type]] [--pgo[=profile]] [--full-pgo[=type]] dest [pgo-dest]"
124+
exit 1
125+
fi
126+
if [ -n "$PREFIX_PGO" ] && [ -z "$PGO" ] && [ -z "$FULL_PGO" ]; then
127+
echo Unrecognized parameter $1
91128
exit 1
92129
fi
93130

@@ -102,15 +139,80 @@ if [ -n "${HOST_CLANG}" ] && [ "${CFGUARD_ARGS}" = "--enable-cfguard" ]; then
102139
"${HOST_CLANG}" -c -x c -o - - -Werror -mguard=cf </dev/null >/dev/null 2>/dev/null || CFGUARD_ARGS="--disable-cfguard"
103140
fi
104141

142+
if [ -n "$FULL_PGO" ]; then
143+
if [ -z "$PREFIX_PGO" ]; then
144+
echo Must provide a second destination for a PGO build
145+
exit 1
146+
fi
147+
./build-all.sh "$PREFIX" --stage1 $LLVM_ARGS $MINGW_ARGS $CFGUARD_ARGS
148+
unset COMPILER_LAUNCHER
149+
./build-all.sh "$PREFIX" --profile$INSTRUMENTATION $LLVM_ARGS
150+
./build-all.sh "$PREFIX" "$PREFIX_PGO" --thinlto --pgo --llvm-only $LLVM_ARGS
151+
# If one already has a usable profile, one could also do the following
152+
# two steps only:
153+
# ./build-all.sh "$PREFIX" --stage1 --llvm-only
154+
# ./build-all.sh "$PREFIX" "$PREFIX_PGO" --pgo
155+
exit 0
156+
fi
157+
158+
if [ -n "$PROFILE" ]; then
159+
export PATH=$PREFIX/bin:$PATH
160+
STAGE1_PREFIX=$PREFIX
161+
PREFIX=/tmp/dummy-prefix
162+
elif [ -n "$PGO" ]; then
163+
if [ -z "$PREFIX_PGO" ]; then
164+
echo Must provide a second destination for a PGO build
165+
exit 1
166+
fi
167+
export PATH=$PREFIX/bin:$PATH
168+
STAGE1_PREFIX=$PREFIX
169+
PREFIX=$PREFIX_PGO
170+
171+
if [ -n "$LLVM_ONLY" ] && [ "$PREFIX" != "$STAGE1_PREFIX" ] ; then
172+
# Only rebuilding LLVM, not any runtimes. Copy the stage1 toolchain
173+
# and rebuild LLVM on top of it.
174+
rm -rf $PREFIX
175+
mkdir -p "$(dirname "$PREFIX")"
176+
cp -a "$STAGE1_PREFIX" "$PREFIX"
177+
# Remove the native Linux/macOS runtimes which aren't needed in
178+
# the final distribution.
179+
rm -rf "$PREFIX"/lib/clang/*/lib/darwin
180+
rm -rf "$PREFIX"/lib/clang/*/lib/linux
181+
fi
182+
fi
183+
184+
if [ "$(uname)" = "Darwin" ]; then
185+
if [ -n "$PROFILE" ] || [ -n "$PGO" ]; then
186+
# Using a custom Clang, which doesn't find the SDK automatically.
187+
# CMake sets this automatically, but if using the compiler directly,
188+
# this is needed. (If compilation uses "cc" or "gcc", it will miss
189+
# the stage1 llvm-mingw toolchain and use the system compiler anyway.)
190+
export SDKROOT=$(xcrun --show-sdk-path)
191+
fi
192+
fi
193+
105194
if [ -z "$NO_TOOLS" ]; then
106195
if [ -z "${HOST_CLANG}" ]; then
107196
./build-llvm.sh $PREFIX $LLVM_ARGS $HOST_ARGS
197+
if [ -n "$PROFILE" ]; then
198+
./pgo-training.sh llvm-project/llvm/build-instrumented $STAGE1_PREFIX
199+
exit 0
200+
fi
108201
if [ -z "$NO_LLDB" ] && [ -z "$NO_LLDB_MI" ]; then
109202
./build-lldb-mi.sh $PREFIX $HOST_ARGS
110203
fi
111204
if [ -z "$FULL_LLVM" ]; then
112205
./strip-llvm.sh $PREFIX $HOST_ARGS
113206
fi
207+
if [ -n "$STAGE1" ]; then
208+
if [ "$(uname)" = "Darwin" ]; then
209+
./build-llvm.sh $PREFIX --macos-native-tools
210+
fi
211+
# Build runtimes. On Linux, this is needed for profiling.
212+
# On macOS, it is also needed for OS availability helpers like
213+
# __isPlatformVersionAtLeast.
214+
./build-compiler-rt.sh --native $PREFIX
215+
fi
114216
fi
115217
if [ -n "$LLVM_ONLY" ]; then
116218
exit 0

0 commit comments

Comments
 (0)