From 042640abbf66fd58e091ce5818f0fc72ddd8dd65 Mon Sep 17 00:00:00 2001 From: "chao.an" Date: Thu, 21 Apr 2022 22:48:02 +0800 Subject: [PATCH] arch/arm: add support for GCC LTO 1. Enable GCC link-time optimizer 2. Enable use of a linker plugin during link-time optimization Signed-off-by: chao.an --- arch/arm/src/Makefile | 11 +++++++++-- arch/arm/src/arm/Toolchain.defs | 28 +++++++++++++++++++++------- arch/arm/src/armv6-m/Toolchain.defs | 19 ++++++++++++++++--- arch/arm/src/armv7-a/Toolchain.defs | 28 +++++++++++++++++++++------- arch/arm/src/armv7-m/Toolchain.defs | 19 ++++++++++++++++--- arch/arm/src/armv7-r/Toolchain.defs | 28 +++++++++++++++++++++------- arch/arm/src/armv8-m/Toolchain.defs | 13 +++++++++++++ 7 files changed, 117 insertions(+), 29 deletions(-) diff --git a/arch/arm/src/Makefile b/arch/arm/src/Makefile index 61c70659a4..5b159d388d 100644 --- a/arch/arm/src/Makefile +++ b/arch/arm/src/Makefile @@ -89,8 +89,15 @@ LDFLAGS += $(addprefix -T,$(call CONVERT_PATH,$(ARCHSCRIPT))) $(EXTRALINKCMDS) # Override in Make.defs if linker is not 'ld' -LDSTARTGROUP ?= --start-group -LDENDGROUP ?= --end-group +ifeq ($(LD),$(CC)) + LDSTARTGROUP ?= -Wl,--start-group + LDENDGROUP ?= -Wl,--end-group + LDFLAGS := $(addprefix -Xlinker ,$(LDFLAGS)) + LDFLAGS += $(CFLAGS) +else + LDSTARTGROUP ?= --start-group + LDENDGROUP ?= --end-group +endif BOARDMAKE = $(if $(wildcard board$(DELIM)Makefile),y,) diff --git a/arch/arm/src/arm/Toolchain.defs b/arch/arm/src/arm/Toolchain.defs index 2c851b924f..b9d3025b5f 100644 --- a/arch/arm/src/arm/Toolchain.defs +++ b/arch/arm/src/arm/Toolchain.defs @@ -118,15 +118,29 @@ endif # Default toolchain -CC = $(CROSSDEV)gcc -CXX = $(CROSSDEV)g++ -CPP = $(CROSSDEV)gcc -E -P -x c -LD = $(CROSSDEV)ld -STRIP = $(CROSSDEV)strip --strip-unneeded -AR = $(CROSSDEV)ar rcs -NM = $(CROSSDEV)nm +CC = $(CROSSDEV)gcc +CXX = $(CROSSDEV)g++ +CPP = $(CROSSDEV)gcc -E -P -x c +STRIP = $(CROSSDEV)strip --strip-unneeded OBJCOPY = $(CROSSDEV)objcopy OBJDUMP = $(CROSSDEV)objdump +LD = $(CROSSDEV)ld +AR = $(CROSSDEV)ar rcs +NM = $(CROSSDEV)nm + +# Link Time Optimization + +ifeq ($(CONFIG_LTO_FULL),y) + MAXOPTIMIZATION += -flto + ifeq ($(CONFIG_ARM_TOOLCHAIN),GNU_EABI) + LD := $(CROSSDEV)gcc + AR := $(CROSSDEV)gcc-ar rcs + NM := $(CROSSDEV)gcc-nm + MAXOPTIMIZATION += -fuse-linker-plugin + MAXOPTIMIZATION += -fno-builtin + MAXOPTIMIZATION += -nodefaultlibs + endif +endif # Add the builtin library diff --git a/arch/arm/src/armv6-m/Toolchain.defs b/arch/arm/src/armv6-m/Toolchain.defs index 4b8d2c7cab..63ef66ca76 100644 --- a/arch/arm/src/armv6-m/Toolchain.defs +++ b/arch/arm/src/armv6-m/Toolchain.defs @@ -86,6 +86,9 @@ ifeq ($(CONFIG_LTO_THIN),y) MAXOPTIMIZATION += -flto=thin else ifeq ($(CONFIG_LTO_FULL),y) MAXOPTIMIZATION += -flto + ifeq ($(CONFIG_ARMV6M_TOOLCHAIN),GNU_EABI) + MAXOPTIMIZATION += -fuse-linker-plugin + endif endif # NuttX buildroot under Linux or Cygwin @@ -129,12 +132,22 @@ else CC = $(CROSSDEV)gcc CXX = $(CROSSDEV)g++ CPP = $(CROSSDEV)gcc -E -P -x c - LD = $(CROSSDEV)ld STRIP = $(CROSSDEV)strip --strip-unneeded - AR = $(CROSSDEV)ar rcs - NM = $(CROSSDEV)nm OBJCOPY = $(CROSSDEV)objcopy OBJDUMP = $(CROSSDEV)objdump + LD = $(CROSSDEV)ld + AR = $(CROSSDEV)ar rcs + NM = $(CROSSDEV)nm + + ifeq ($(CONFIG_LTO_FULL),y) + ifeq ($(CONFIG_ARMV6M_TOOLCHAIN),GNU_EABI) + LD := $(CROSSDEV)gcc + AR := $(CROSSDEV)gcc-ar rcs + NM := $(CROSSDEV)gcc-nm + MAXOPTIMIZATION += -fno-builtin + MAXOPTIMIZATION += -nodefaultlibs + endif + endif endif # Architecture flags diff --git a/arch/arm/src/armv7-a/Toolchain.defs b/arch/arm/src/armv7-a/Toolchain.defs index 454d71e1a2..b50ca4d873 100644 --- a/arch/arm/src/armv7-a/Toolchain.defs +++ b/arch/arm/src/armv7-a/Toolchain.defs @@ -165,15 +165,29 @@ endif # Default toolchain -CC = $(CROSSDEV)gcc -CXX = $(CROSSDEV)g++ -CPP = $(CROSSDEV)gcc -E -P -x c -LD = $(CROSSDEV)ld -STRIP = $(CROSSDEV)strip --strip-unneeded -AR = $(CROSSDEV)ar rcs -NM = $(CROSSDEV)nm +CC = $(CROSSDEV)gcc +CXX = $(CROSSDEV)g++ +CPP = $(CROSSDEV)gcc -E -P -x c +STRIP = $(CROSSDEV)strip --strip-unneeded OBJCOPY = $(CROSSDEV)objcopy OBJDUMP = $(CROSSDEV)objdump +LD = $(CROSSDEV)ld +AR = $(CROSSDEV)ar rcs +NM = $(CROSSDEV)nm + +# Link Time Optimization + +ifeq ($(CONFIG_LTO_FULL),y) + MAXOPTIMIZATION += -flto + ifeq ($(CONFIG_ARMV7A_TOOLCHAIN),GNU_EABI) + LD := $(CROSSDEV)gcc + AR := $(CROSSDEV)gcc-ar rcs + NM := $(CROSSDEV)gcc-nm + MAXOPTIMIZATION += -fuse-linker-plugin + MAXOPTIMIZATION += -fno-builtin + MAXOPTIMIZATION += -nodefaultlibs + endif +endif # Add the builtin library diff --git a/arch/arm/src/armv7-m/Toolchain.defs b/arch/arm/src/armv7-m/Toolchain.defs index 396238e8bc..84c264f505 100644 --- a/arch/arm/src/armv7-m/Toolchain.defs +++ b/arch/arm/src/armv7-m/Toolchain.defs @@ -136,6 +136,9 @@ ifeq ($(CONFIG_LTO_THIN),y) MAXOPTIMIZATION += -flto=thin else ifeq ($(CONFIG_LTO_FULL),y) MAXOPTIMIZATION += -flto + ifeq ($(CONFIG_ARMV7M_TOOLCHAIN),GNU_EABI) + MAXOPTIMIZATION += -fuse-linker-plugin + endif endif # NuttX buildroot under Linux or Cygwin @@ -188,12 +191,22 @@ else CC = $(CROSSDEV)gcc CXX = $(CROSSDEV)g++ CPP = $(CROSSDEV)gcc -E -P -x c - LD = $(CROSSDEV)ld STRIP = $(CROSSDEV)strip --strip-unneeded - AR = $(CROSSDEV)ar rcs - NM = $(CROSSDEV)nm OBJCOPY = $(CROSSDEV)objcopy OBJDUMP = $(CROSSDEV)objdump + LD = $(CROSSDEV)ld + AR = $(CROSSDEV)ar rcs + NM = $(CROSSDEV)nm + + ifeq ($(CONFIG_LTO_FULL),y) + ifeq ($(CONFIG_ARMV7M_TOOLCHAIN),GNU_EABI) + LD := $(CROSSDEV)gcc + AR := $(CROSSDEV)gcc-ar rcs + NM := $(CROSSDEV)gcc-nm + MAXOPTIMIZATION += -fno-builtin + MAXOPTIMIZATION += -nodefaultlibs + endif + endif endif # Architecture flags diff --git a/arch/arm/src/armv7-r/Toolchain.defs b/arch/arm/src/armv7-r/Toolchain.defs index 0a3c5038f7..c012e2e06f 100644 --- a/arch/arm/src/armv7-r/Toolchain.defs +++ b/arch/arm/src/armv7-r/Toolchain.defs @@ -141,15 +141,29 @@ endif # Default toolchain -CC = $(CROSSDEV)gcc -CXX = $(CROSSDEV)g++ -CPP = $(CROSSDEV)gcc -E -P -x c -LD = $(CROSSDEV)ld -STRIP = $(CROSSDEV)strip --strip-unneeded -AR = $(CROSSDEV)ar rcs -NM = $(CROSSDEV)nm +CC = $(CROSSDEV)gcc +CXX = $(CROSSDEV)g++ +CPP = $(CROSSDEV)gcc -E -P -x c +STRIP = $(CROSSDEV)strip --strip-unneeded OBJCOPY = $(CROSSDEV)objcopy OBJDUMP = $(CROSSDEV)objdump +LD = $(CROSSDEV)ld +AR = $(CROSSDEV)ar rcs +NM = $(CROSSDEV)nm + +# Link Time Optimization + +ifeq ($(CONFIG_LTO_FULL),y) + MAXOPTIMIZATION += -flto + ifeq ($(CONFIG_ARMV7R_TOOLCHAIN),GNU_EABI) + LD := $(CROSSDEV)gcc + AR := $(CROSSDEV)gcc-ar rcs + NM := $(CROSSDEV)gcc-nm + MAXOPTIMIZATION += -fuse-linker-plugin + MAXOPTIMIZATION += -fno-builtin + MAXOPTIMIZATION += -nodefaultlibs + endif +endif # Add the builtin library diff --git a/arch/arm/src/armv8-m/Toolchain.defs b/arch/arm/src/armv8-m/Toolchain.defs index 414358b033..43f707fcbe 100644 --- a/arch/arm/src/armv8-m/Toolchain.defs +++ b/arch/arm/src/armv8-m/Toolchain.defs @@ -145,6 +145,9 @@ ifeq ($(CONFIG_LTO_THIN),y) MAXOPTIMIZATION += -flto=thin else ifeq ($(CONFIG_LTO_FULL),y) MAXOPTIMIZATION += -flto + ifeq ($(CONFIG_ARMV8M_TOOLCHAIN),GNU_EABI) + MAXOPTIMIZATION += -fuse-linker-plugin + endif endif # NuttX buildroot under Linux or Cygwin @@ -203,6 +206,16 @@ else NM = $(CROSSDEV)nm OBJCOPY = $(CROSSDEV)objcopy OBJDUMP = $(CROSSDEV)objdump + + ifeq ($(CONFIG_LTO_FULL),y) + ifeq ($(CONFIG_ARMV8M_TOOLCHAIN),GNU_EABI) + LD := $(CROSSDEV)gcc + AR := $(CROSSDEV)gcc-ar rcs + NM := $(CROSSDEV)gcc-nm + MAXOPTIMIZATION += -fno-builtin + MAXOPTIMIZATION += -nodefaultlibs + endif + endif endif # Architecture flags