Skip to content

Commit 8622fd8

Browse files
committed
gh-xxxx: enable BOLT optimization of libpython
Before, we only supported running BOLT on the main `python` binary. If a shared library was in play, it wouldn't be optimized. That was leaving a ton of optimization opportunities on the floor. This commit adds support for running BOLT on libpython. Functionality is disabled by default because BOLT asserts on LLVM 15, which is the latest LLVM. I've built LLVM tip and it is able to process libpython just fine. So it is known to work.
1 parent 2eef208 commit 8622fd8

File tree

3 files changed

+62
-12
lines changed

3 files changed

+62
-12
lines changed

Makefile.pre.in

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ profile-pgo-apply-stamp: profile-pgo-analyze-stamp
696696
# passes.
697697

698698
# List of binaries that BOLT runs on.
699-
BOLT_BINARIES = $(BUILDPYTHON)
699+
BOLT_BINARIES = @BOLT_BINARIES@
700700

701701
BOLT_INSTRUMENT_FLAGS ?= @BOLT_INSTRUMENT_FLAGS@
702702
BOLT_APPLY_FLAGS ?= @BOLT_APPLY_FLAGS@
@@ -708,8 +708,6 @@ clean-bolt:
708708
find . -name '*.bolt_inst' -exec rm -f {} ';'
709709
# The data files they produce.
710710
find . -name '*.fdata' -exec rm -f {} ';'
711-
# Copied of binaries before BOLT application.
712-
find . -name '*.prebolt' -exec rm -f {} ';'
713711

714712
# BOLTs dependencies are a bit wonky.
715713
#
@@ -727,28 +725,36 @@ profile-bolt-prebuild-stamp: @MAKE_BOLT_NATIVE_DEPENDENCY@
727725

728726
profile-bolt-instrument-stamp: profile-bolt-prebuild-stamp
729727
for bin in $(BOLT_BINARIES); do \
730-
if [ -e "$${bin}.prebolt" ]; then \
731-
echo "Restoring pre-BOLT binary $${bin}.prebolt"; \
728+
prebolt="$${bin}.prebolt"; \
729+
if [ -e "$${prebolt}" ]; then \
730+
echo "Restoring pre-BOLT binary $${prebolt}"; \
732731
mv "$${bin}.prebolt" "$${bin}"; \
733-
fi \
732+
fi; \
733+
cp "$${bin}" "$${prebolt}"; \
734734
done
735735
# Ensure prior BOLT state is purged.
736736
$(MAKE) clean-bolt
737-
@LLVM_BOLT@ ./$(BUILDPYTHON) -instrument -instrumentation-file-append-pid -instrumentation-file=$(abspath $(BUILDPYTHON).bolt) -o $(BUILDPYTHON).bolt_inst $(BOLT_INSTRUMENT_FLAGS)
737+
for bin in $(BOLT_BINARIES); do \
738+
@LLVM_BOLT@ $${bin} -instrument -instrumentation-file-append-pid -instrumentation-file=$(abspath $${bin}.bolt) -o $${bin}.bolt_inst $(BOLT_INSTRUMENT_FLAGS); \
739+
mv "$${bin}.bolt_inst" "$${bin}"; \
740+
done
738741
touch $@
739742

740743
profile-bolt-run-stamp: profile-bolt-instrument-stamp
741-
$(RUNSHARED) ./$(BUILDPYTHON).bolt_inst $(PROFILE_TASK) || true
744+
$(RUNSHARED) ./$(BUILDPYTHON) $(PROFILE_TASK) || true
742745
touch $@
743746

744747
profile-bolt-analyze-stamp: profile-bolt-run-stamp
745-
@MERGE_FDATA@ $(BUILDPYTHON).*.fdata > $(BUILDPYTHON).fdata
748+
for bin in $(BOLT_BINARIES); do \
749+
@MERGE_FDATA@ $${bin}.*.fdata > $${bin}.fdata; \
750+
done
746751
touch $@
747752

748753
profile-bolt-apply-stamp: profile-bolt-analyze-stamp
749-
@LLVM_BOLT@ ./$(BUILDPYTHON) -o $(BUILDPYTHON).bolt -data=$(BUILDPYTHON).fdata $(BOLT_APPLY_FLAGS)
750-
mv $(BUILDPYTHON) $(BUILDPYTHON).prebolt
751-
mv $(BUILDPYTHON).bolt $(BUILDPYTHON)
754+
for bin in $(BOLT_BINARIES); do \
755+
@LLVM_BOLT@ "$${bin}.prebolt" -o "$${bin}.bolt" -data="$${bin}.fdata" $(BOLT_APPLY_FLAGS); \
756+
mv "$${bin}.bolt" "$${bin}"; \
757+
done
752758
touch $@
753759

754760
# End of profile-based optimization rules.

configure

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,6 +2066,23 @@ if test "$Py_BOLT" = 'true' ; then
20662066
fi
20672067
fi
20682068

2069+
# Enable BOLT optimizations of libpython. Optional for now due to known
2070+
# crashes on LLVM 15. Seems to be fixed in LLVM 16.
2071+
AC_SUBST(BOLT_BINARIES)
2072+
BOLT_BINARIES='$(BUILDPYTHON)'
2073+
2074+
AC_MSG_CHECKING(for --with-bolt-libpython)
2075+
AC_ARG_WITH(bolt_libpython,
2076+
AS_HELP_STRING([--with-bolt-libpython], [enable BOLT optimization of libpython (WARNING: known to crash BOLT)]),
2077+
[with_bolt_libpython="yes"],
2078+
[with_bolt_libpython="no"])
2079+
AC_MSG_RESULT($with_bolt_libpython)
2080+
2081+
if test "${enable_shared}" = "yes" -a "${with_bolt_libpython}" = "yes"
2082+
then
2083+
BOLT_BINARIES="${BOLT_BINARIES} \$(INSTSONAME)"
2084+
fi
2085+
20692086
AC_ARG_VAR(BOLT_INSTRUMENT_FLAGS, Arguments to llvm-bolt when instrumenting binaries)
20702087
AC_MSG_CHECKING(BOLT_INSTRUMENT_FLAGS)
20712088
if test -z "${BOLT_INSTRUMENT_FLAGS}"

0 commit comments

Comments
 (0)