Skip to content

Commit dcbf4bf

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 c92110f commit dcbf4bf

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
@@ -693,7 +693,7 @@ profile-pgo-apply-stamp: profile-pgo-analyze-stamp
693693
# passes.
694694

695695
# List of binaries that BOLT runs on.
696-
BOLT_BINARIES = $(BUILDPYTHON)
696+
BOLT_BINARIES = @BOLT_BINARIES@
697697

698698
BOLT_INSTRUMENT_FLAGS ?= @BOLT_INSTRUMENT_FLAGS@
699699
BOLT_APPLY_FLAGS ?= @BOLT_APPLY_FLAGS@
@@ -705,8 +705,6 @@ clean-bolt:
705705
find . -name '*.bolt_inst' -exec rm -f {} ';'
706706
# The data files they produce.
707707
find . -name '*.fdata' -exec rm -f {} ';'
708-
# Copied of binaries before BOLT application.
709-
find . -name '*.prebolt' -exec rm -f {} ';'
710708

711709
# BOLTs dependencies are a bit wonky.
712710
#
@@ -724,28 +722,36 @@ profile-bolt-prebuild-stamp: @MAKE_BOLT_NATIVE_DEPENDENCY@
724722

725723
profile-bolt-instrument-stamp: profile-bolt-prebuild-stamp
726724
for bin in $(BOLT_BINARIES); do \
727-
if [ -e "$${bin}.prebolt" ]; then \
728-
echo "Restoring pre-BOLT binary $${bin}.prebolt"; \
725+
prebolt="$${bin}.prebolt"; \
726+
if [ -e "$${prebolt}" ]; then \
727+
echo "Restoring pre-BOLT binary $${prebolt}"; \
729728
mv "$${bin}.prebolt" "$${bin}"; \
730-
fi \
729+
fi; \
730+
cp "$${bin}" "$${prebolt}"; \
731731
done
732732
# Ensure prior BOLT state is purged.
733733
$(MAKE) clean-bolt
734-
@LLVM_BOLT@ ./$(BUILDPYTHON) -instrument -instrumentation-file-append-pid -instrumentation-file=$(abspath $(BUILDPYTHON).bolt) -o $(BUILDPYTHON).bolt_inst $(BOLT_INSTRUMENT_FLAGS)
734+
for bin in $(BOLT_BINARIES); do \
735+
@LLVM_BOLT@ $${bin} -instrument -instrumentation-file-append-pid -instrumentation-file=$(abspath $${bin}.bolt) -o $${bin}.bolt_inst $(BOLT_INSTRUMENT_FLAGS); \
736+
mv "$${bin}.bolt_inst" "$${bin}"; \
737+
done
735738
touch $@
736739

737740
profile-bolt-run-stamp: profile-bolt-instrument-stamp
738-
$(RUNSHARED) ./$(BUILDPYTHON).bolt_inst $(PROFILE_TASK) || true
741+
$(RUNSHARED) ./$(BUILDPYTHON) $(PROFILE_TASK) || true
739742
touch $@
740743

741744
profile-bolt-analyze-stamp: profile-bolt-run-stamp
742-
@MERGE_FDATA@ $(BUILDPYTHON).*.fdata > $(BUILDPYTHON).fdata
745+
for bin in $(BOLT_BINARIES); do \
746+
@MERGE_FDATA@ $${bin}.*.fdata > $${bin}.fdata; \
747+
done
743748
touch $@
744749

745750
profile-bolt-apply-stamp: profile-bolt-analyze-stamp
746-
@LLVM_BOLT@ ./$(BUILDPYTHON) -o $(BUILDPYTHON).bolt -data=$(BUILDPYTHON).fdata $(BOLT_APPLY_FLAGS)
747-
mv $(BUILDPYTHON) $(BUILDPYTHON).prebolt
748-
mv $(BUILDPYTHON).bolt $(BUILDPYTHON)
751+
for bin in $(BOLT_BINARIES); do \
752+
@LLVM_BOLT@ "$${bin}.prebolt" -o "$${bin}.bolt" -data="$${bin}.fdata" $(BOLT_APPLY_FLAGS); \
753+
mv "$${bin}.bolt" "$${bin}"; \
754+
done
749755
touch $@
750756

751757
# 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)