Skip to content

Commit 88c90ca

Browse files
authored
Fix various issues with PGO+LTO makefile (#55581)
This fixes various issues with the PGO+LTO makefile - `USECCACHE` doesn't work throwing an error at https://github.com/JuliaLang/julia/blob/eb5587dac02d1f6edf486a71b95149139cc5d9f7/Make.inc#L734 This is because setting `CC` and `CCX` by passing them as arguments to `make` prevents `Make.inc` from prepending these variables with `ccache` as `Make.inc` doesn't use override. To workaround this I instead set `USECLANG` and add the toolchain to the `PATH`. - To deal with similar issues for the other make flags, I pass them as environment variables which can be edited in `Make.inc`. - I add a way to build in one go by creating the `all` target, now you can just run `make` and a PGO+LTO build that profiles Julia's build will be generated. - I workaround `PROFRAW_FILES` not being reevaluated after `stage1` builds, this caused the generation of `PROFILE_FILE` to run an outdated command if `stage1` was built and affected the profraw files. This is important when building in one go. - I add a way to run rules like `binary-dist` which are not defined in this makefile with the correct toolchain which for example prevents `make binary-dist` from unnecessarily rebuilding `sys.ji`. - Include `-Wl,--undefined-version` till #54533 gets fixed. These changes need to be copied to the PGO+LTO+BOLT makefile and some to the BOLT makefile in a later pr. --------- Co-authored-by: Zentrik <Zentrik@users.noreply.github.com>
1 parent 68feddc commit 88c90ca

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

contrib/pgo-lto/Makefile

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ STAGE0_TOOLS:=$(STAGE0_BUILD)/usr/tools/
88

99
PROFILE_DIR:=$(CURDIR)/profiles
1010
PROFILE_FILE:=$(PROFILE_DIR)/merged.prof
11-
PROFRAW_FILES:=$(wildcard $(PROFILE_DIR)/*.profraw)
1211
JULIA_ROOT:=$(CURDIR)/../..
1312

1413
LLVM_CXXFILT:=$(STAGE0_TOOLS)llvm-cxxfilt
@@ -26,15 +25,16 @@ AFTER_STAGE1_MESSAGE:='You can now optionally collect more profiling data for us
2625
Note that running extensive scripts may result in counter overflows, which can be detected by running $\
2726
`make top`. Afterwards run `make stage2`.'
2827

29-
TOOLCHAIN_FLAGS = $\
30-
"CC=$(STAGE0_TOOLS)clang" $\
31-
"CXX=$(STAGE0_TOOLS)clang++" $\
32-
"LD=$(STAGE0_TOOLS)ld.lld" $\
33-
"AR=$(STAGE0_TOOLS)llvm-ar" $\
34-
"RANLIB=$(STAGE0_TOOLS)llvm-ranlib" $\
35-
"CFLAGS+=$(PGO_CFLAGS)" $\
36-
"CXXFLAGS+=$(PGO_CXXFLAGS)" $\
37-
"LDFLAGS+=$(PGO_LDFLAGS)"
28+
STAGE1_FLAGS:=LDFLAGS="-fuse-ld=lld -flto=thin -Wl,--undefined-version -fprofile-generate=$(PROFILE_DIR)" $\
29+
CFLAGS="-fprofile-generate=$(PROFILE_DIR) -Xclang -mllvm -Xclang -vp-counters-per-site=$(COUNTERS_PER_SITE)" $\
30+
CXXFLAGS="-fprofile-generate=$(PROFILE_DIR) -Xclang -mllvm -Xclang -vp-counters-per-site=$(COUNTERS_PER_SITE)"
31+
STAGE2_FLAGS:=LDFLAGS="-fuse-ld=lld -flto=thin -Wl,--undefined-version -fprofile-use=$(PROFILE_FILE) -Wl,--icf=safe" $\
32+
CFLAGS="-fprofile-use=$(PROFILE_FILE)" $\
33+
CXXFLAGS="-fprofile-use=$(PROFILE_FILE)"
34+
35+
COMMON_FLAGS:=USECLANG=1 USE_BINARYBUILDER_LLVM=0
36+
37+
all: stage2 # Default target as first in file
3838

3939
$(STAGE0_BUILD) $(STAGE1_BUILD) $(STAGE2_BUILD):
4040
$(MAKE) -C $(JULIA_ROOT) O=$@ configure
@@ -48,26 +48,20 @@ stage0: | $(STAGE0_BUILD)
4848
touch $@
4949

5050
$(STAGE1_BUILD): stage0
51-
stage1: PGO_CFLAGS:=-fprofile-generate=$(PROFILE_DIR) -Xclang -mllvm -Xclang -vp-counters-per-site=$(COUNTERS_PER_SITE)
52-
stage1: PGO_CXXFLAGS:=-fprofile-generate=$(PROFILE_DIR) -Xclang -mllvm -Xclang -vp-counters-per-site=$(COUNTERS_PER_SITE)
53-
stage1: PGO_LDFLAGS:=-fuse-ld=lld -flto=thin -fprofile-generate=$(PROFILE_DIR)
54-
stage1: export USE_BINARYBUILDER_LLVM=0
5551
stage1: | $(STAGE1_BUILD)
56-
$(MAKE) -C $(STAGE1_BUILD) $(TOOLCHAIN_FLAGS) && touch $@
52+
@echo "--- Build Julia Stage 1 - with instrumentation"
53+
PATH=$(STAGE0_TOOLS):$$PATH $(STAGE1_FLAGS) $(MAKE) -C $(STAGE1_BUILD) $(COMMON_FLAGS) && touch $@
5754
@echo $(AFTER_STAGE1_MESSAGE)
5855

59-
stage2: PGO_CFLAGS:=-fprofile-use=$(PROFILE_FILE)
60-
stage2: PGO_CXXFLAGS:=-fprofile-use=$(PROFILE_FILE)
61-
stage2: PGO_LDFLAGS:=-fuse-ld=lld -flto=thin -fprofile-use=$(PROFILE_FILE) -Wl,--icf=safe
62-
stage2: export USE_BINARYBUILDER_LLVM=0
6356
stage2: $(PROFILE_FILE) | $(STAGE2_BUILD)
64-
$(MAKE) -C $(STAGE2_BUILD) $(TOOLCHAIN_FLAGS) && touch $@
57+
@echo "--- Build Julia Stage 2 - PGO + LTO optimised"
58+
PATH=$(STAGE0_TOOLS):$$PATH $(STAGE2_FLAGS) $(MAKE) -C $(STAGE2_BUILD) $(COMMON_FLAGS) && touch $@
6559

66-
install: stage2
67-
$(MAKE) -C $(STAGE2_BUILD) USE_BINARYBUILDER_LLVM=0 install
60+
.DEFAULT: stage2
61+
PATH=$(STAGE0_TOOLS):$$PATH $(STAGE2_FLAGS) $(MAKE) -C $(STAGE2_BUILD) $(COMMON_FLAGS) $@
6862

69-
$(PROFILE_FILE): stage1 $(PROFRAW_FILES)
70-
$(LLVM_PROFDATA) merge -output=$@ $(PROFRAW_FILES)
63+
$(PROFILE_FILE): stage1 $(wildcard $(PROFILE_DIR)/*.profraw)
64+
$(LLVM_PROFDATA) merge -output=$@ $(PROFILE_DIR)/*.profraw
7165

7266
# show top 50 functions
7367
top: $(PROFILE_FILE)

0 commit comments

Comments
 (0)