History

kbuild: rust: add AutoFDO support

This patch enables AutoFDO build support for Rust code within the Linux kernel. This allows Rust code to be profiled and optimized based on the profile. The RUSTFLAGS variable was suffixed with *_AUTOFDO_CLANG to match the naming of the config option, which is called CONFIG_AUTOFDO_CLANG. This implementation has been verified in Android, first by inspecting the object files and confirming that they look correct. After that, it was verified as below: 1. Running the binderAddInts benchmark [1] with Rust Binder built as rust_binder.ko module, using a Pixel 9 Pro. 2. Collecting a profile on a Pixel 10 Pro XL using the app-launch benchmark, which starts different apps many times, on a device with Rust Binder as a built-in kernel module. (C Binder was not present on the device.) 3. Using the collected profile, run the binderAddInts benchmark again with Rust Binder built both as a rust_binder.ko module, and as a built-in kernel module. 4. In both cases, Rust Binder without AutoFDO was approximately 13% slower than the AutoFDO optimized version. Built-in vs .ko did not make a measurable performance difference. All of the above was verified in conjunction with my helpers inlining series [2], which confirmed that this worked correctly for helpers too once [3] was fixed in the helpers inlining series.

Alice Ryhl committed 7 months ago

Commit a960c2c1 parent fc1ce3a2 files · +8 −1Browse files

Unified · Split

2 files
  1. scripts/Makefile.autofdo +5 −1
  2. scripts/Makefile.lib +3 −0
scripts/Makefile.autofdo+5−1
@@ -3,14 +3,18 @@ · 2 unmodified lines
3# Enable available and selected Clang AutoFDO features.3# Enable available and selected Clang AutoFDO features.
4 4
5CFLAGS_AUTOFDO_CLANG := -fdebug-info-for-profiling -mllvm -enable-fs-discriminator=true -mllvm -improved-fs-discriminator=true5CFLAGS_AUTOFDO_CLANG := -fdebug-info-for-profiling -mllvm -enable-fs-discriminator=true -mllvm -improved-fs-discriminator=true
6RUSTFLAGS_AUTOFDO_CLANG := -Zdebug-info-for-profiling -Cllvm-args=-enable-fs-discriminator=true -Cllvm-args=-improved-fs-discriminator=true
6 7
7ifndef CONFIG_DEBUG_INFO8ifndef CONFIG_DEBUG_INFO
8 CFLAGS_AUTOFDO_CLANG += -gmlt9 CFLAGS_AUTOFDO_CLANG += -gmlt
10 RUSTFLAGS_AUTOFDO_CLANG += -Cdebuginfo=line-tables-only
9endif11endif
10 12
11ifdef CLANG_AUTOFDO_PROFILE13ifdef CLANG_AUTOFDO_PROFILE
12 CFLAGS_AUTOFDO_CLANG += -fprofile-sample-use=$(CLANG_AUTOFDO_PROFILE) -ffunction-sections14 CFLAGS_AUTOFDO_CLANG += -fprofile-sample-use=$(CLANG_AUTOFDO_PROFILE) -ffunction-sections
13 CFLAGS_AUTOFDO_CLANG += -fsplit-machine-functions15 CFLAGS_AUTOFDO_CLANG += -fsplit-machine-functions
16 RUSTFLAGS_AUTOFDO_CLANG += -Zprofile-sample-use=$(CLANG_AUTOFDO_PROFILE) -Zfunction-sections=y
17 RUSTFLAGS_AUTOFDO_CLANG += -Cllvm-args=-split-machine-functions
14endif18endif
15 19
16ifdef CONFIG_LTO_CLANG_THIN20ifdef CONFIG_LTO_CLANG_THIN
@@ -21,4 +25,4 @@ ifdef CONFIG_LTO_CLANG_THIN · 4 unmodified lines
21 KBUILD_LDFLAGS += -plugin-opt=-split-machine-functions25 KBUILD_LDFLAGS += -plugin-opt=-split-machine-functions
22endif26endif
23 27
24export CFLAGS_AUTOFDO_CLANG28export CFLAGS_AUTOFDO_CLANG RUSTFLAGS_AUTOFDO_CLANG
scripts/Makefile.lib+3−0
@@ -123,6 +123,9 @@ ifeq ($(CONFIG_AUTOFDO_CLANG),y) · 122 unmodified lines
123_c_flags += $(if $(patsubst n%,, \123_c_flags += $(if $(patsubst n%,, \
124 $(AUTOFDO_PROFILE_$(target-stem).o)$(AUTOFDO_PROFILE)$(is-kernel-object)), \124 $(AUTOFDO_PROFILE_$(target-stem).o)$(AUTOFDO_PROFILE)$(is-kernel-object)), \
125 $(CFLAGS_AUTOFDO_CLANG))125 $(CFLAGS_AUTOFDO_CLANG))
126_rust_flags += $(if $(patsubst n%,, \
127 $(AUTOFDO_PROFILE_$(target-stem).o)$(AUTOFDO_PROFILE)$(is-kernel-object)), \
128 $(RUSTFLAGS_AUTOFDO_CLANG))
126endif129endif
127 130
128#131#