Skip to content

Commit b632a18

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 0deaac8 commit b632a18

File tree

3 files changed

+62
-12
lines changed

3 files changed

+62
-12
lines changed

Makefile.pre.in

+18-12
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ profile-pgo-apply-stamp: profile-pgo-analyze-stamp
720720
# passes.
721721

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

725725
BOLT_INSTRUMENT_FLAGS ?= @BOLT_INSTRUMENT_FLAGS@
726726
BOLT_APPLY_FLAGS ?= @BOLT_APPLY_FLAGS@
@@ -732,8 +732,6 @@ clean-bolt:
732732
find . -name '*.bolt_inst' -exec rm -f {} ';'
733733
# The data files they produce.
734734
find . -name '*.fdata' -exec rm -f {} ';'
735-
# Copied of binaries before BOLT application.
736-
find . -name '*.prebolt' -exec rm -f {} ';'
737735

738736
# BOLTs dependencies are a bit wonky.
739737
#
@@ -751,28 +749,36 @@ profile-bolt-prebuild-stamp: @MAKE_BOLT_NATIVE_DEPENDENCY@
751749

752750
profile-bolt-instrument-stamp: profile-bolt-prebuild-stamp
753751
for bin in $(BOLT_BINARIES); do \
754-
if [ -e "$${bin}.prebolt" ]; then \
755-
echo "Restoring pre-BOLT binary $${bin}.prebolt"; \
752+
prebolt="$${bin}.prebolt"; \
753+
if [ -e "$${prebolt}" ]; then \
754+
echo "Restoring pre-BOLT binary $${prebolt}"; \
756755
mv "$${bin}.prebolt" "$${bin}"; \
757-
fi \
756+
fi; \
757+
cp "$${bin}" "$${prebolt}"; \
758758
done
759759
# Ensure prior BOLT state is purged.
760760
$(MAKE) clean-bolt
761-
@LLVM_BOLT@ ./$(BUILDPYTHON) -instrument -instrumentation-file-append-pid -instrumentation-file=$(abspath $(BUILDPYTHON).bolt) -o $(BUILDPYTHON).bolt_inst $(BOLT_INSTRUMENT_FLAGS)
761+
for bin in $(BOLT_BINARIES); do \
762+
@LLVM_BOLT@ $${bin} -instrument -instrumentation-file-append-pid -instrumentation-file=$(abspath $${bin}.bolt) -o $${bin}.bolt_inst $(BOLT_INSTRUMENT_FLAGS); \
763+
mv "$${bin}.bolt_inst" "$${bin}"; \
764+
done
762765
touch $@
763766

764767
profile-bolt-run-stamp: profile-bolt-instrument-stamp
765-
$(RUNSHARED) ./$(BUILDPYTHON).bolt_inst $(PROFILE_TASK) || true
768+
$(RUNSHARED) ./$(BUILDPYTHON) $(PROFILE_TASK) || true
766769
touch $@
767770

768771
profile-bolt-analyze-stamp: profile-bolt-run-stamp
769-
@MERGE_FDATA@ $(BUILDPYTHON).*.fdata > $(BUILDPYTHON).fdata
772+
for bin in $(BOLT_BINARIES); do \
773+
@MERGE_FDATA@ $${bin}.*.fdata > $${bin}.fdata; \
774+
done
770775
touch $@
771776

772777
profile-bolt-apply-stamp: profile-bolt-analyze-stamp
773-
@LLVM_BOLT@ ./$(BUILDPYTHON) -o $(BUILDPYTHON).bolt -data=$(BUILDPYTHON).fdata $(BOLT_APPLY_FLAGS)
774-
mv $(BUILDPYTHON) $(BUILDPYTHON).prebolt
775-
mv $(BUILDPYTHON).bolt $(BUILDPYTHON)
778+
for bin in $(BOLT_BINARIES); do \
779+
@LLVM_BOLT@ "$${bin}.prebolt" -o "$${bin}.bolt" -data="$${bin}.fdata" $(BOLT_APPLY_FLAGS); \
780+
mv "$${bin}.bolt" "$${bin}"; \
781+
done
776782
touch $@
777783

778784
# End of profile-based optimization rules.

configure

+27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

+17
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)