Felix Held has submitted this change. ( https://review.coreboot.org/c/coreboot/+/84003?usp=email )
Change subject: Add initial experimental LTO support ......................................................................
Add initial experimental LTO support
This will not succeed in compiling on all target and compiler combinations but at least gets the ball rolling. The change is not invasive.
Some notes: - GCC has issues with LTO on ARM - Clang uses LLD automatically on some arch - Clang with LTO fails on x86 as it forwards the linking to GCC for some reason - SMM building succeeds but the binary is empty
Change-Id: Ieb9204777fd349542744a8946e2207731c37969c Signed-off-by: Arthur Heymans arthur@aheymans.xyz Reviewed-on: https://review.coreboot.org/c/coreboot/+/84003 Tested-by: build bot (Jenkins) no-reply@coreboot.org Reviewed-by: Lean Sheng Tan sheng.tan@9elements.com Reviewed-by: Nico Huber nico.h@gmx.de --- M Makefile.mk M src/Kconfig 2 files changed, 22 insertions(+), 3 deletions(-)
Approvals: build bot (Jenkins): Verified Nico Huber: Looks good to me, approved Lean Sheng Tan: Looks good to me, approved
diff --git a/Makefile.mk b/Makefile.mk index 9efef49..e7a9ab1 100644 --- a/Makefile.mk +++ b/Makefile.mk @@ -373,7 +373,7 @@ cbfs-files-processor-struct= \ $(eval $(2): $(1) $(obj)/build.h $(obj)/fmap_config.h $(KCONFIG_AUTOHEADER); \ printf " CC+STRIP $(1)\n"; \ - $(CC_ramstage) -MMD $(CPPFLAGS_ramstage) $(CFLAGS_ramstage) --param asan-globals=0 $$(ramstage-c-ccopts) -include $(KCONFIG_AUTOHEADER) -MT $(2) -o $(2).tmp -c $(1) && \ + $(CC_ramstage) -MMD $(CPPFLAGS_ramstage) $(CFLAGS_ramstage) -fno-lto --param asan-globals=0 $$(ramstage-c-ccopts) -include $(KCONFIG_AUTOHEADER) -MT $(2) -o $(2).tmp -c $(1) && \ $(OBJCOPY_ramstage) -O binary --only-section='.*data*' --only-section='.*bss*' \ --set-section-flags .*bss*=alloc,contents,load $(2).tmp $(2); \ rm -f $(2).tmp) \ @@ -547,6 +547,14 @@ endif endif
+ifeq ($(CONFIG_LTO),y) +CFLAGS_common += -flto +# Clang can not deal with GCC lto objects +ifeq ($(CONFIG_COMPILER_GCC),y) +ADAFLAGS_common += -flto +endif +endif + ADAFLAGS_common += -gnatp ADAFLAGS_common += -Wuninitialized ADAFLAGS_common += -Wall @@ -1316,11 +1324,15 @@
define link_stage # $1 stage name - +ifeq ($(CONFIG_LTO),y) +$$(objcbfs)/$(1).debug: $$$$($(1)-libs) $$$$($(1)-objs) + @printf " LINK $$(subst $$(obj)/,,$$(@))\n" + $$(CC_$(1)) $$(CPPFLAGS_$(1)) $$(CFLAGS_$(1)) $$(LDFLAGS_$(1):%=-Wl,%) -o $$@ -L$$(obj) $$(COMPILER_RT_FLAGS_$(1):%=-Wl,%) -Wl,--whole-archive -Wl,--start-group $$(filter-out %.ld,$$($(1)-objs)) $$($(1)-libs) -Wl,--no-whole-archive $$(COMPILER_RT_$(1)) -Wl,--end-group -T $(call src-to-obj,$(1),$(CONFIG_MEMLAYOUT_LD_FILE)) +else $$(objcbfs)/$(1).debug: $$$$($(1)-libs) $$$$($(1)-objs) @printf " LINK $$(subst $$(obj)/,,$$(@))\n" $$(LD_$(1)) $$(LDFLAGS_$(1)) -o $$@ -L$$(obj) $$(COMPILER_RT_FLAGS_$(1)) --whole-archive --start-group $$(filter-out %.ld,$$($(1)-objs)) $$($(1)-libs) --no-whole-archive $$(COMPILER_RT_$(1)) --end-group -T $(call src-to-obj,$(1),$(CONFIG_MEMLAYOUT_LD_FILE)) - +endif endef
ifeq ($(CONFIG_SEPARATE_ROMSTAGE),y) diff --git a/src/Kconfig b/src/Kconfig index b6dc67f..55fad95 100644 --- a/src/Kconfig +++ b/src/Kconfig @@ -116,6 +116,13 @@
For details see https://ccache.samba.org.
+config LTO + bool "Use link time optimization (LTO) (experimental)" + default n + help + Compile with link time optimization. This can often decrease the + final binary size, but may increase compilation time. + config IWYU bool "Test platform with include-what-you-use" help