Skip to content

Commit

Permalink
lto: Add Link Time Optimization support for GCC
Browse files Browse the repository at this point in the history
This requires a modern version of GCC and various other patches in order
to work. LTO results in a smaller kernel binary with better performance.

Based off of work from Andi Kleen <[email protected]>.

Signed-off-by: Sultan Alsawaf <[email protected]>
  • Loading branch information
kerneltoast authored and Arjun-Ingole committed Jan 26, 2022
1 parent 8f9ac3a commit cc03e71
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
20 changes: 18 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,8 @@ LD = $(CROSS_COMPILE)ld
CC = $(CROSS_COMPILE)gcc
LDGOLD = $(CROSS_COMPILE)ld.gold
CPP = $(CC) -E
AR = $(CROSS_COMPILE)ar
NM = $(CROSS_COMPILE)nm
AR ?= $(CROSS_COMPILE)ar
NM ?= $(CROSS_COMPILE)nm
STRIP = $(CROSS_COMPILE)strip
OBJCOPY = $(CROSS_COMPILE)objcopy
OBJDUMP = $(CROSS_COMPILE)objdump
Expand Down Expand Up @@ -684,6 +684,22 @@ LLVM_NM := llvm-nm
export LLVM_AR LLVM_NM
endif

ifdef CONFIG_LTO_GCC
LTO_CFLAGS := -flto -flto=jobserver -fno-fat-lto-objects \
-fuse-linker-plugin -fwhole-program
KBUILD_CFLAGS += $(LTO_CFLAGS)
LTO_LDFLAGS := $(LTO_CFLAGS) -Wno-lto-type-mismatch -Wno-psabi \
-Wno-stringop-overflow -flinker-output=nolto-rel
LDFINAL := $(CONFIG_SHELL) $(srctree)/scripts/gcc-ld $(LTO_LDFLAGS)
AR := $(CROSS_COMPILE)gcc-ar
NM := $(CROSS_COMPILE)gcc-nm
DISABLE_LTO := -fno-lto
export DISABLE_LTO LDFINAL
else
LDFINAL := $(LD)
export LDFINAL
endif

# The arch Makefile can set ARCH_{CPP,A,C}FLAGS to override the default
# values of the respective KBUILD_* variables
ARCH_CPPFLAGS :=
Expand Down
3 changes: 3 additions & 0 deletions init/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,9 @@ config CC_OPTIMIZE_FOR_SIZE

endchoice

config LTO_GCC
bool "Enable Link Time Optimization for GCC"

config SYSCTL
bool

Expand Down
2 changes: 1 addition & 1 deletion scripts/Makefile.lib
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ $(obj)/%: $(src)/%_shipped
# ---------------------------------------------------------------------------

quiet_cmd_ld = LD $@
cmd_ld = $(LD) $(LDFLAGS) $(ldflags-y) $(LDFLAGS_$(@F)) \
cmd_ld = $(LDFINAL) $(LDFLAGS) $(ldflags-y) $(LDFLAGS_$(@F)) \
$(filter-out FORCE,$^) -o $@

# Objcopy
Expand Down
4 changes: 2 additions & 2 deletions scripts/link-vmlinux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ modpost_link()
info LD vmlinux.o
fi

${LD} ${LDFLAGS} -r -o ${1} $(lto_lds) ${objects}
${LDFINAL} ${LDFLAGS} -r -o ${1} $(lto_lds) ${objects}
}

# If CONFIG_LTO_CLANG is selected, we postpone running recordmcount until
Expand All @@ -150,7 +150,7 @@ vmlinux_link()
local objects

if [ "${SRCARCH}" != "um" ]; then
local ld=${LD}
local ld=${LDFINAL}
local ldflags="${LDFLAGS} ${LDFLAGS_vmlinux}"

if [ -n "${LDFINAL_vmlinux}" ]; then
Expand Down

0 comments on commit cc03e71

Please sign in to comment.