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>
This commit is contained in:
Arthur Heymans 2024-08-21 10:12:16 +02:00 committed by Felix Held
commit b56f407614
2 changed files with 22 additions and 3 deletions

View file

@ -373,7 +373,7 @@ endef
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 @@ CFLAGS_common += -Wno-array-compare
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 @@ endif # CONFIG_CBFS_VERIFICATION
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)

View file

@ -116,6 +116,13 @@ config CCACHE
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