1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: "Jan Alexander Steffens (heftig)" <jan.steffens@gmail.com>
3 Date: Tue, 17 Nov 2020 22:45:47 +0100
4 Subject: [PATCH] Fixes for LTO+PGO support
6 Cherry-picked from Firefox Nightly.
8 build/moz.configure/lto-pgo.configure | 7 +++++--
9 config/makefiles/rust.mk | 21 +++++++++++++++++----
10 2 files changed, 22 insertions(+), 6 deletions(-)
12 diff --git a/build/moz.configure/lto-pgo.configure b/build/moz.configure/lto-pgo.configure
13 index 366c6691f7d1..e5342a037ee9 100644
14 --- a/build/moz.configure/lto-pgo.configure
15 +++ b/build/moz.configure/lto-pgo.configure
16 @@ -229,7 +229,10 @@ def lto(value, c_compiler, ld64_known_good, target, instrumented_build):
19 num_cores = multiprocessing.cpu_count()
20 - cflags.append("-flto")
21 + if len(value) and value[0].lower() == 'full':
22 + cflags.append("-flto")
24 + cflags.append("-flto=thin")
25 cflags.append("-flifetime-dse=1")
27 ldflags.append("-flto=%s" % num_cores)
28 @@ -258,6 +261,6 @@ set_config('MOZ_LTO', lto.enabled)
29 set_define('MOZ_LTO', lto.enabled)
30 set_config('MOZ_LTO_CFLAGS', lto.cflags)
31 set_config('MOZ_LTO_LDFLAGS', lto.ldflags)
32 -set_config('MOZ_LTO_RUST', lto.rust_lto)
33 +set_config('MOZ_LTO_RUST_CROSS', lto.rust_lto)
34 add_old_configure_assignment('MOZ_LTO_CFLAGS', lto.cflags)
35 add_old_configure_assignment('MOZ_LTO_LDFLAGS', lto.ldflags)
36 diff --git a/config/makefiles/rust.mk b/config/makefiles/rust.mk
37 index f4475054f897..f19dfbc72fad 100644
38 --- a/config/makefiles/rust.mk
39 +++ b/config/makefiles/rust.mk
40 @@ -59,17 +59,19 @@ cargo_rustc_flags = $(CARGO_RUSTCFLAGS)
41 ifndef DEVELOPER_OPTIONS
43 # Enable link-time optimization for release builds, but not when linking
45 +# gkrust_gtest. And not when doing cross-language LTO.
46 +ifndef MOZ_LTO_RUST_CROSS
47 ifeq (,$(findstring gkrust_gtest,$(RUST_LIBRARY_FILE)))
48 cargo_rustc_flags += -Clto
50 # Versions of rust >= 1.45 need -Cembed-bitcode=yes for all crates when
52 ifeq (,$(filter 1.38.% 1.39.% 1.40.% 1.41.% 1.42.% 1.43.% 1.44.%,$(RUSTC_VERSION)))
53 RUSTFLAGS += -Cembed-bitcode=yes
59 ifdef CARGO_INCREMENTAL
60 export CARGO_INCREMENTAL
61 @@ -195,10 +197,19 @@ target_rust_ltoable := force-cargo-library-build
62 target_rust_nonltoable := force-cargo-test-run force-cargo-library-check $(foreach b,build check,force-cargo-program-$(b))
65 -rust_pgo_flags := $(if $(MOZ_PROFILE_GENERATE),-C profile-generate=$(topobjdir)) $(if $(MOZ_PROFILE_USE),-C profile-use=$(PGO_PROFILE_PATH))
66 +ifdef MOZ_PROFILE_GENERATE
67 +rust_pgo_flags := -C profile-generate=$(topobjdir)
68 +# The C compiler may be passed extra llvm flags for PGO that we also want to pass to rust as well.
69 +# In PROFILE_GEN_CFLAGS, they look like "-mllvm foo", and we want "-C llvm-args=foo", so first turn
70 +# "-mllvm foo" into "-mllvm:foo" so that it becomes a unique argument, that we can then filter for,
71 +# excluding other flags, and then turn into the right string.
72 +rust_pgo_flags += $(patsubst -mllvm:%,-C llvm-args=%,$(filter -mllvm:%,$(subst -mllvm ,-mllvm:,$(PROFILE_GEN_CFLAGS))))
73 +else # MOZ_PROFILE_USE
74 +rust_pgo_flags := -C profile-use=$(PGO_PROFILE_PATH)
78 -$(target_rust_ltoable): RUSTFLAGS:=$(rustflags_override) $(rustflags_sancov) $(RUSTFLAGS) $(if $(MOZ_LTO_RUST),-Clinker-plugin-lto) $(rust_pgo_flags)
79 +$(target_rust_ltoable): RUSTFLAGS:=$(rustflags_override) $(rustflags_sancov) $(RUSTFLAGS) $(if $(MOZ_LTO_RUST_CROSS),-Clinker-plugin-lto) $(rust_pgo_flags)
80 $(target_rust_nonltoable): RUSTFLAGS:=$(rustflags_override) $(rustflags_sancov) $(RUSTFLAGS)
82 TARGET_RECIPES := $(target_rust_ltoable) $(target_rust_nonltoable)
83 @@ -312,17 +323,19 @@ $(RUST_LIBRARY_FILE): force-cargo-library-build
84 # When we are building in --enable-release mode; we add an additional check to confirm
85 # that we are not importing any networking-related functions in rust code. This reduces
86 # the chance of proxy bypasses originating from rust code.
87 -# The check only works when rust code is built with -Clto.
88 +# The check only works when rust code is built with -Clto but without MOZ_LTO_RUST_CROSS.
89 # Sanitizers and sancov also fail because compiler-rt hooks network functions.
90 ifndef MOZ_PROFILE_GENERATE
91 ifeq ($(OS_ARCH), Linux)
92 ifeq (,$(rustflags_sancov)$(MOZ_ASAN)$(MOZ_TSAN)$(MOZ_UBSAN))
93 +ifndef MOZ_LTO_RUST_CROSS
94 ifneq (,$(filter -Clto,$(cargo_rustc_flags)))
95 $(call py_action,check_binary,--target --networking $@)
102 force-cargo-library-check:
103 $(call CARGO_CHECK) --lib $(cargo_target_flag) $(rust_features_flag)