From patchwork Tue Mar 16 12:25:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marek_Beh=C3=BAn?= X-Patchwork-Id: 1453886 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=nic.cz header.i=@nic.cz header.a=rsa-sha256 header.s=default header.b=TXBrnOo+; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4F0CMn07zSz9sRR for ; Tue, 16 Mar 2021 23:31:48 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 8AF058290A; Tue, 16 Mar 2021 13:28:11 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=nic.cz Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; secure) header.d=nic.cz header.i=@nic.cz header.b="TXBrnOo+"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 02D8582652; Tue, 16 Mar 2021 13:27:13 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail.nic.cz (mail.nic.cz [217.31.204.67]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 1357E82699 for ; Tue, 16 Mar 2021 13:26:49 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=nic.cz Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=marek.behun@nic.cz Received: from dellmb.labs.office.nic.cz (unknown [IPv6:2001:1488:fffe:6:cac7:3539:7f1f:463]) by mail.nic.cz (Postfix) with ESMTPSA id A3C00140B41; Tue, 16 Mar 2021 13:26:48 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=nic.cz; s=default; t=1615897608; bh=gNMB0zbnYs/A7BUjpbVJ/DqDeZVrwEPfr3V8TqGy2Wk=; h=From:To:Date; b=TXBrnOo+0zAx2+iiTB1cdgw46Sh4+tn8jGIMAr++QoEhn/bfhhXlXiq/lhhNSpabt 30iNzLFsndIrXTNrAJ04m5XdbXQ6nXas3k1vzOW96cQKO7VozwWoOwnZjVmLCh/dRE 155z5R3AUkVJ0C0xdaGGDhmViVhdW/8rS5YnvW+c= From: =?utf-8?q?Marek_Beh=C3=BAn?= To: u-boot@lists.denx.de, Simon Glass , Heiko Schocher , Patrick Delaunay , Patrice CHOTARD , Miquel Raynal , Tom Rini , =?utf-8?q?Pali_Roh=C3=A1r?= , Stefan Roese , Heinrich Schuchardt , Alexander Graf , Marek Vasut , Neil Armstrong , Ryder Lee , Adam Ford , Bin Meng , Harald Seiler , Tim Harvey Cc: =?utf-8?q?Marek_Beh=C3=BAn?= Subject: [PATCH u-boot v3 17/39] build: support building with Link Time Optimizations Date: Tue, 16 Mar 2021 13:25:47 +0100 Message-Id: <20210316122609.6523-18-marek.behun@nic.cz> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210316122609.6523-1-marek.behun@nic.cz> References: <20210316122609.6523-1-marek.behun@nic.cz> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.4 at phobos.denx.de X-Virus-Status: Clean Add plumbing for building U-Boot with Link Time Optimizations. When building with LTO, $(PLATFORM_LIBS) has to be in --whole-archive / --no-whole-archive group, otherwise some functions declared in assembly may not be resolved and linking may fail. Signed-off-by: Marek BehĂșn --- Kbuild | 2 ++ Kconfig | 24 ++++++++++++++++++++++++ Makefile | 37 +++++++++++++++++++++++++++++++++++++ scripts/Makefile.lib | 3 +++ scripts/Makefile.spl | 17 +++++++++++++++++ 5 files changed, 83 insertions(+) diff --git a/Kbuild b/Kbuild index 1eac091594..bf52e54051 100644 --- a/Kbuild +++ b/Kbuild @@ -10,6 +10,8 @@ generic-offsets-file := include/generated/generic-asm-offsets.h always := $(generic-offsets-file) targets := lib/asm-offsets.s +CFLAGS_REMOVE_asm-offsets.o := $(LTO_CFLAGS) + $(obj)/$(generic-offsets-file): $(obj)/lib/asm-offsets.s FORCE $(call filechk,offsets,__GENERIC_ASM_OFFSETS_H__) diff --git a/Kconfig b/Kconfig index 86f0a39bb0..f8c1a77bed 100644 --- a/Kconfig +++ b/Kconfig @@ -85,6 +85,30 @@ config SPL_OPTIMIZE_INLINING do what it thinks is best, which is desirable in some cases for size reasons. +config ARCH_SUPPORTS_LTO + bool + +config LTO + bool "Enable Link Time Optimizations" + depends on ARCH_SUPPORTS_LTO + default n + help + This option enables Link Time Optimization (LTO), a mechanism which + allows the compiler to optimize between different compilation units. + + This can optimize away dead code paths, resulting in smaller binary + size (if CC_OPTIMIZE_FOR_SIZE is enabled). + + This option is not available for every architecture and may + introduce bugs. + + Currently, when compiling with GCC, due to a weird bug regarding + jobserver, the final linking will not respect make's --jobs argument. + Instead all available processors will be used (as reported by the + nproc command). + + If unsure, say n. + config TPL_OPTIMIZE_INLINING bool "Allow compiler to uninline functions marked 'inline' in TPL" depends on TPL diff --git a/Makefile b/Makefile index c9b5fac7a9..952c165d8f 100644 --- a/Makefile +++ b/Makefile @@ -677,6 +677,28 @@ else KBUILD_CFLAGS += -O2 endif +LTO_CFLAGS := +LTO_FINAL_LDFLAGS := +export LTO_CFLAGS LTO_FINAL_LDFLAGS +ifdef CONFIG_LTO + ifeq ($(cc-name),clang) + LTO_CFLAGS += -flto + LTO_FINAL_LDFLAGS += -flto + else + NPROC := $(shell nproc 2>/dev/null || echo 1) + LTO_CFLAGS += -flto=$(NPROC) + LTO_FINAL_LDFLAGS += -fuse-linker-plugin -flto=$(NPROC) + + # use plugin aware tools + AR = $(CROSS_COMPILE)gcc-ar + NM = $(CROSS_COMPILE)gcc-nm + endif + + CFLAGS_NON_EFI += $(LTO_CFLAGS) + + KBUILD_CFLAGS += $(LTO_CFLAGS) +endif + KBUILD_CFLAGS += $(call cc-option,-fno-stack-protector) KBUILD_CFLAGS += $(call cc-option,-fno-delete-null-pointer-checks) @@ -1746,6 +1768,20 @@ ARCH_POSTLINK := $(wildcard $(srctree)/arch/$(ARCH)/Makefile.postlink) # Rule to link u-boot # May be overridden by arch/$(ARCH)/config.mk +ifdef CONFIG_LTO +quiet_cmd_u-boot__ ?= LTO $@ + cmd_u-boot__ ?= \ + $(CC) -nostdlib -nostartfiles \ + $(LTO_FINAL_LDFLAGS) $(c_flags) \ + $(KBUILD_LDFLAGS:%=-Wl,%) $(LDFLAGS_u-boot:%=-Wl,%) -o $@ \ + -T u-boot.lds $(u-boot-init) \ + -Wl,--whole-archive \ + $(u-boot-main) \ + $(PLATFORM_LIBS) \ + -Wl,--no-whole-archive \ + -Wl,-Map,u-boot.map; \ + $(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true) +else quiet_cmd_u-boot__ ?= LD $@ cmd_u-boot__ ?= $(LD) $(KBUILD_LDFLAGS) $(LDFLAGS_u-boot) -o $@ \ -T u-boot.lds $(u-boot-init) \ @@ -1754,6 +1790,7 @@ quiet_cmd_u-boot__ ?= LD $@ --no-whole-archive \ $(PLATFORM_LIBS) -Map u-boot.map; \ $(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true) +endif quiet_cmd_smap = GEN common/system_map.o cmd_smap = \ diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib index 78543c6dd1..78bbebe7e9 100644 --- a/scripts/Makefile.lib +++ b/scripts/Makefile.lib @@ -419,6 +419,9 @@ $(obj)/%_efi.so: $(obj)/%.o $(obj)/efi_crt0.o $(obj)/efi_reloc.o $(obj)/efi_free targets += $(obj)/efi_crt0.o $(obj)/efi_reloc.o $(obj)/efi_freestanding.o +CFLAGS_REMOVE_efi_reloc.o := $(LTO_CFLAGS) +CFLAGS_REMOVE_efi_freestanding.o := $(LTO_CFLAGS) + # ACPI # --------------------------------------------------------------------------- # diff --git a/scripts/Makefile.spl b/scripts/Makefile.spl index 44ed6d6826..9c9d59635d 100644 --- a/scripts/Makefile.spl +++ b/scripts/Makefile.spl @@ -426,6 +426,22 @@ $(obj)/$(SPL_BIN).sym: $(obj)/$(SPL_BIN) FORCE # Rule to link u-boot-spl # May be overridden by arch/$(ARCH)/config.mk +ifdef CONFIG_LTO +quiet_cmd_u-boot-spl ?= LTO $@ + cmd_u-boot-spl ?= \ + ( \ + cd $(obj) && \ + $(CC) -nostdlib -nostartfiles $(LTO_FINAL_LDFLAGS) $(c_flags) \ + $(KBUILD_LDFLAGS:%=-Wl,%) $(LDFLAGS_$(@F):%=-Wl,%) \ + $(patsubst $(obj)/%,%,$(u-boot-spl-init)) \ + -Wl,--whole-archive \ + $(patsubst $(obj)/%,%,$(u-boot-spl-main)) \ + $(patsubst $(obj)/%,%,$(u-boot-spl-platdata)) \ + $(PLATFORM_LIBS) \ + -Wl,--no-whole-archive \ + -Wl,-Map,$(SPL_BIN).map -o $(SPL_BIN) \ + ) +else quiet_cmd_u-boot-spl ?= LD $@ cmd_u-boot-spl ?= \ ( \ @@ -438,6 +454,7 @@ quiet_cmd_u-boot-spl ?= LD $@ --no-whole-archive \ $(PLATFORM_LIBS) -Map $(SPL_BIN).map -o $(SPL_BIN) \ ) +endif $(obj)/$(SPL_BIN): $(u-boot-spl-platdata) $(u-boot-spl-init) \ $(u-boot-spl-main) $(obj)/u-boot-spl.lds FORCE