Skip to content

Commit a212332

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 02418fd commit a212332

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
@@ -722,7 +722,7 @@ profile-pgo-apply-stamp: profile-pgo-analyze-stamp
722722
# passes.
723723

724724
# List of binaries that BOLT runs on.
725-
BOLT_BINARIES = $(BUILDPYTHON)
725+
BOLT_BINARIES = @BOLT_BINARIES@
726726

727727
BOLT_INSTRUMENT_FLAGS ?= @BOLT_INSTRUMENT_FLAGS@
728728
BOLT_APPLY_FLAGS ?= @BOLT_APPLY_FLAGS@
@@ -734,8 +734,6 @@ clean-bolt:
734734
find . -name '*.bolt_inst' -exec rm -f {} ';'
735735
# The data files they produce.
736736
find . -name '*.fdata' -exec rm -f {} ';'
737-
# Copied of binaries before BOLT application.
738-
find . -name '*.prebolt' -exec rm -f {} ';'
739737

740738
# BOLTs dependencies are a bit wonky.
741739
#
@@ -753,28 +751,36 @@ profile-bolt-prebuild-stamp: @MAKE_BOLT_NATIVE_DEPENDENCY@
753751

754752
profile-bolt-instrument-stamp: profile-bolt-prebuild-stamp
755753
for bin in $(BOLT_BINARIES); do \
756-
if [ -e "$${bin}.prebolt" ]; then \
757-
echo "Restoring pre-BOLT binary $${bin}.prebolt"; \
754+
prebolt="$${bin}.prebolt"; \
755+
if [ -e "$${prebolt}" ]; then \
756+
echo "Restoring pre-BOLT binary $${prebolt}"; \
758757
mv "$${bin}.prebolt" "$${bin}"; \
759-
fi \
758+
fi; \
759+
cp "$${bin}" "$${prebolt}"; \
760760
done
761761
# Ensure prior BOLT state is purged.
762762
$(MAKE) clean-bolt
763-
@LLVM_BOLT@ ./$(BUILDPYTHON) -instrument -instrumentation-file-append-pid -instrumentation-file=$(abspath $(BUILDPYTHON).bolt) -o $(BUILDPYTHON).bolt_inst $(BOLT_INSTRUMENT_FLAGS)
763+
for bin in $(BOLT_BINARIES); do \
764+
@LLVM_BOLT@ $${bin} -instrument -instrumentation-file-append-pid -instrumentation-file=$(abspath $${bin}.bolt) -o $${bin}.bolt_inst $(BOLT_INSTRUMENT_FLAGS); \
765+
mv "$${bin}.bolt_inst" "$${bin}"; \
766+
done
764767
touch $@
765768

766769
profile-bolt-run-stamp: profile-bolt-instrument-stamp
767-
$(RUNSHARED) ./$(BUILDPYTHON).bolt_inst $(PROFILE_TASK) || true
770+
$(RUNSHARED) ./$(BUILDPYTHON) $(PROFILE_TASK) || true
768771
touch $@
769772

770773
profile-bolt-analyze-stamp: profile-bolt-run-stamp
771-
@MERGE_FDATA@ $(BUILDPYTHON).*.fdata > $(BUILDPYTHON).fdata
774+
for bin in $(BOLT_BINARIES); do \
775+
@MERGE_FDATA@ $${bin}.*.fdata > $${bin}.fdata; \
776+
done
772777
touch $@
773778

774779
profile-bolt-apply-stamp: profile-bolt-analyze-stamp
775-
@LLVM_BOLT@ ./$(BUILDPYTHON) -o $(BUILDPYTHON).bolt -data=$(BUILDPYTHON).fdata $(BOLT_APPLY_FLAGS)
776-
mv $(BUILDPYTHON) $(BUILDPYTHON).prebolt
777-
mv $(BUILDPYTHON).bolt $(BUILDPYTHON)
780+
for bin in $(BOLT_BINARIES); do \
781+
@LLVM_BOLT@ "$${bin}.prebolt" -o "$${bin}.bolt" -data="$${bin}.fdata" $(BOLT_APPLY_FLAGS); \
782+
mv "$${bin}.bolt" "$${bin}"; \
783+
done
778784
touch $@
779785

780786
# 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
@@ -2083,6 +2083,23 @@ if test "$Py_BOLT" = 'true' ; then
20832083
fi
20842084
fi
20852085

2086+
# Enable BOLT optimizations of libpython. Optional for now due to known
2087+
# crashes on LLVM 15. Seems to be fixed in LLVM 16.
2088+
AC_SUBST(BOLT_BINARIES)
2089+
BOLT_BINARIES='$(BUILDPYTHON)'
2090+
2091+
AC_MSG_CHECKING(for --with-bolt-libpython)
2092+
AC_ARG_WITH(bolt_libpython,
2093+
AS_HELP_STRING([--with-bolt-libpython], [enable BOLT optimization of libpython (WARNING: known to crash BOLT)]),
2094+
[with_bolt_libpython="yes"],
2095+
[with_bolt_libpython="no"])
2096+
AC_MSG_RESULT($with_bolt_libpython)
2097+
2098+
if test "${enable_shared}" = "yes" -a "${with_bolt_libpython}" = "yes"
2099+
then
2100+
BOLT_BINARIES="${BOLT_BINARIES} \$(INSTSONAME)"
2101+
fi
2102+
20862103
AC_ARG_VAR(BOLT_INSTRUMENT_FLAGS, Arguments to llvm-bolt when instrumenting binaries)
20872104
AC_MSG_CHECKING(BOLT_INSTRUMENT_FLAGS)
20882105
if test -z "${BOLT_INSTRUMENT_FLAGS}"

0 commit comments

Comments
 (0)