Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp1393228pxb; Fri, 20 Nov 2020 08:25:33 -0800 (PST) X-Google-Smtp-Source: ABdhPJwj6Z4y8bdUm7cElL8qE9R014Hhs1qhGtA4MVpNPUCp5zUKlRu0SUadp6EY1xTRzB1wFpdF X-Received: by 2002:a05:6402:2363:: with SMTP id a3mr10822071eda.388.1605889533590; Fri, 20 Nov 2020 08:25:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605889533; cv=none; d=google.com; s=arc-20160816; b=dz4PAvemlQyAHA3Yv4ZoIvbiWWclFUYfeb8r+u2gxrRkePXadVHOp18uefM7gSxRSz 1L3wR1xkSU6WQ04/yIp59VaTbAhHPLFvhjEHEV47tpnyyvUnodCx+IfQQIjdF/ZtTJgr 1r544kpD1Bkf1kJ+Pul2yyBerr5iADFExDvjIEMCvwdPxFulfL/QrPupiXcQsC2228mg XvR6fUilOjgLillJjAANuveqp8T4wOKaIQR2fy+L4tmpak//CgjpXGoQyhArGvK1MVLL T6LNA7cqlRVsWxxDMTVAGuszp3OO+2c3m9pNe1BEE3u0AL7tcPaGF2wOLo+RX/hkcDPc 1q9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=vOo6J2qOaNEsmcSt6ibqCSmyULbkJ5ut9vT5313WIA4=; b=XADLCXKOfaIUGp+vJfTCet3QY9hZGUgDAzPI4Kf10UKcMAL6xNT8+pssBt0WlE6zSt 22npYTuVyiDU+tThbsoMGWcuVzP+TkfhHOyfk17AvJH+L045IBxOHc3jQ7olY5chPg34 q3VnyH3Gc0XeKex5wbmwE9F1JqOiaqZJFDT4fZULo5sA+x8t9WknWIHeYvWg4AprB58G f7ABehED2chpnMb6tJIpQxVGYX5BwpWwmPDt2jkZyb9CdHznlbCg3ZNW0D7lbfZnfhoi N1XYpNmh4Z5YyUO+L5Ky4EIrJ0otNdHGx+ta6ByrCP2f0udDPryjqJU+0iRwS251tA85 IHXA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=BnDD+UEZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a13si2160762edk.386.2020.11.20.08.25.09; Fri, 20 Nov 2020 08:25:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=BnDD+UEZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730041AbgKTQXc (ORCPT + 99 others); Fri, 20 Nov 2020 11:23:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49934 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730017AbgKTQXY (ORCPT ); Fri, 20 Nov 2020 11:23:24 -0500 Received: from mail-ua1-x944.google.com (mail-ua1-x944.google.com [IPv6:2607:f8b0:4864:20::944]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8907CC061A47 for ; Fri, 20 Nov 2020 08:23:24 -0800 (PST) Received: by mail-ua1-x944.google.com with SMTP id r23so3306506uak.0 for ; Fri, 20 Nov 2020 08:23:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=vOo6J2qOaNEsmcSt6ibqCSmyULbkJ5ut9vT5313WIA4=; b=BnDD+UEZp9fE0ahf/zN/kLbG7kjRVUr1faC9KKf98v7DPL1XE0vyh9dxjpCHrS3lNW kKvDMNikzoKSTXwSpuCq2nMSCpe/evgWjYPck5JKJ8+CthvbR0ZNc9v0IupNF+zBlKnE uWG4Vnm7aF8vloRUfGNs1t64C7wzHzCYxkl+1FW8NQnQrHmCwCwd5XrUMJbaJCsjLXkf PDvNljmVdmQyxI1ZTz4Ld366+uXkkI5lh48Nb6TVvQ1syIEF+qmtpYm348qLXzUWJTvt eZGAJVW1RbhW1zWWoyWjl4wJmt5Yp9gZSkDByUKeL3sJfgBeoD9b6u8myXwoWwoCSaXl zRGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=vOo6J2qOaNEsmcSt6ibqCSmyULbkJ5ut9vT5313WIA4=; b=GU49RH6f4pN/tQV27STMCsq50xjROwFaURXk2haDYFacWzkczQffS6F4RriyoCCDR5 F/XHNa1SRPbwQA6X2OMWwiG4v5YFTzSgpo0sqMvf6LvCOgH2ZfreWiPuEYupBu3wH2eA yKkARgoaPyylr+6d6kOaAG3DZkEJ+Z4oE4zCYTGYHfRWqhkR6y9PgSDre4iYxbxtM5v6 yL0MUfuGSh1XN/dSnA8xxxDalQD2G6aOkRO4Gv+/JxIhBOA0Y2glouwhFBHZqqsRmii6 U8K3wGyyCT8ISuqnq+QSgcoeFWfxd93s2mi+ek+MN2CwWXq1OGyCt47cE8tFeR/eFrF4 3u1A== X-Gm-Message-State: AOAM5332oh0T2pYqH94rpeBtwyYdoCl5MJcy5CXugvAYsz7LWDj6ieoF 8XG4KtabeM0bsqfI4/Md0D48lBpBTxnjH8p4X6U+aA== X-Received: by 2002:ab0:36db:: with SMTP id v27mr12115443uau.66.1605889402882; Fri, 20 Nov 2020 08:23:22 -0800 (PST) MIME-Version: 1.0 References: <20201118220731.925424-1-samitolvanen@google.com> <20201118220731.925424-3-samitolvanen@google.com> In-Reply-To: From: Sami Tolvanen Date: Fri, 20 Nov 2020 08:23:11 -0800 Message-ID: Subject: Re: [PATCH v7 02/17] kbuild: add support for Clang LTO To: Nick Desaulniers Cc: Masahiro Yamada , Steven Rostedt , Will Deacon , Josh Poimboeuf , Peter Zijlstra , Greg Kroah-Hartman , "Paul E. McKenney" , Kees Cook , clang-built-linux , Kernel Hardening , linux-arch , Linux ARM , Linux Kbuild mailing list , LKML , linux-pci@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Nov 18, 2020 at 3:49 PM Nick Desaulniers wrote: > > On Wed, Nov 18, 2020 at 2:07 PM Sami Tolvanen wrote: > > > > This change adds build system support for Clang's Link Time > > Optimization (LTO). With -flto, instead of ELF object files, Clang > > produces LLVM bitcode, which is compiled into native code at link > > time, allowing the final binary to be optimized globally. For more > > details, see: > > > > https://llvm.org/docs/LinkTimeOptimization.html > > > > The Kconfig option CONFIG_LTO_CLANG is implemented as a choice, > > which defaults to LTO being disabled. To use LTO, the architecture > > must select ARCH_SUPPORTS_LTO_CLANG and support: > > > > - compiling with Clang, > > - compiling inline assembly with Clang's integrated assembler, > > - and linking with LLD. > > > > While using full LTO results in the best runtime performance, the > > compilation is not scalable in time or memory. CONFIG_THINLTO > > enables ThinLTO, which allows parallel optimization and faster > > incremental builds. ThinLTO is used by default if the architecture > > also selects ARCH_SUPPORTS_THINLTO: > > > > https://clang.llvm.org/docs/ThinLTO.html > > > > To enable LTO, LLVM tools must be used to handle bitcode files. The > > easiest way is to pass the LLVM=1 option to make: > > > > $ make LLVM=1 defconfig > > $ scripts/config -e LTO_CLANG > > $ make LLVM=1 > > > > Alternatively, at least the following LLVM tools must be used: > > > > CC=clang LD=ld.lld AR=llvm-ar NM=llvm-nm > > > > To prepare for LTO support with other compilers, common parts are > > gated behind the CONFIG_LTO option, and LTO can be disabled for > > specific files by filtering out CC_FLAGS_LTO. > > > > Signed-off-by: Sami Tolvanen > > Reviewed-by: Kees Cook > > --- > > Makefile | 19 +++++++- > > arch/Kconfig | 75 +++++++++++++++++++++++++++++++ > > include/asm-generic/vmlinux.lds.h | 11 +++-- > > scripts/Makefile.build | 9 +++- > > scripts/Makefile.modfinal | 9 +++- > > scripts/Makefile.modpost | 21 ++++++++- > > scripts/link-vmlinux.sh | 32 +++++++++---- > > 7 files changed, 158 insertions(+), 18 deletions(-) > > > > diff --git a/Makefile b/Makefile > > index 8c8feb4245a6..240560e88d69 100644 > > --- a/Makefile > > +++ b/Makefile > > @@ -893,6 +893,21 @@ KBUILD_CFLAGS += $(CC_FLAGS_SCS) > > export CC_FLAGS_SCS > > endif > > > > +ifdef CONFIG_LTO_CLANG > > +ifdef CONFIG_THINLTO > > +CC_FLAGS_LTO += -flto=thin -fsplit-lto-unit > > +KBUILD_LDFLAGS += --thinlto-cache-dir=$(extmod-prefix).thinlto-cache > > +else > > +CC_FLAGS_LTO += -flto > > +endif > > +CC_FLAGS_LTO += -fvisibility=default > > +endif > > + > > +ifdef CONFIG_LTO > > +KBUILD_CFLAGS += $(CC_FLAGS_LTO) > > +export CC_FLAGS_LTO > > +endif > > + > > ifdef CONFIG_DEBUG_FORCE_FUNCTION_ALIGN_32B > > KBUILD_CFLAGS += -falign-functions=32 > > endif > > @@ -1473,7 +1488,7 @@ MRPROPER_FILES += include/config include/generated \ > > *.spec > > > > # Directories & files removed with 'make distclean' > > -DISTCLEAN_FILES += tags TAGS cscope* GPATH GTAGS GRTAGS GSYMS > > +DISTCLEAN_FILES += tags TAGS cscope* GPATH GTAGS GRTAGS GSYMS .thinlto-cache > > > > # clean - Delete most, but leave enough to build external modules > > # > > @@ -1719,7 +1734,7 @@ PHONY += compile_commands.json > > > > clean-dirs := $(KBUILD_EXTMOD) > > clean: rm-files := $(KBUILD_EXTMOD)/Module.symvers $(KBUILD_EXTMOD)/modules.nsdeps \ > > - $(KBUILD_EXTMOD)/compile_commands.json > > + $(KBUILD_EXTMOD)/compile_commands.json $(KBUILD_EXTMOD)/.thinlto-cache > > > > PHONY += help > > help: > > diff --git a/arch/Kconfig b/arch/Kconfig > > index 56b6ccc0e32d..a41fcb3ca7c6 100644 > > --- a/arch/Kconfig > > +++ b/arch/Kconfig > > @@ -598,6 +598,81 @@ config SHADOW_CALL_STACK > > reading and writing arbitrary memory may be able to locate them > > and hijack control flow by modifying the stacks. > > > > +config LTO > > + bool > > + > > +config ARCH_SUPPORTS_LTO_CLANG > > + bool > > + help > > + An architecture should select this option if it supports: > > + - compiling with Clang, > > + - compiling inline assembly with Clang's integrated assembler, > > + - and linking with LLD. > > + > > +config ARCH_SUPPORTS_THINLTO > > + bool > > + help > > + An architecture should select this option if it supports Clang's > > + ThinLTO. > > + > > +config THINLTO > > + bool "Clang ThinLTO" > > + depends on LTO_CLANG && ARCH_SUPPORTS_THINLTO > > + default y > > + help > > + This option enables Clang's ThinLTO, which allows for parallel > > + optimization and faster incremental compiles. More information > > + can be found from Clang's documentation: > > + > > + https://clang.llvm.org/docs/ThinLTO.html > > + > > + If you say N here, the compiler will use full LTO, which may > > + produce faster code, but building the kernel will be significantly > > + slower as the linker won't efficiently utilize multiple threads. > > + > > + If unsure, say Y. > > I think the order of these new configs makes it so that ThinLTO > appears above LTO in menuconfig; I don't like that, and wish it came > immediately after. Does `THINLTO` have to be defined _after_ the > choice for LTO_NONE/LTO_CLANG, perhaps? > > Secondly, I don't like how ThinLTO is a config and not a choice. If I > don't set ThinLTO, what am I getting? That's a rhetorical question; I > know its full LTO, and I guess the help text does talk about the > tradeoffs and what you would get. I guess what's curious to me is > "why does it display ThinLTO? Why not FullLTO?" I can't help but > wonder if a kconfig `choice` rather than a `config` would be better > here, that way it's more obvious the user is making a choice between > ThinLTO vs Full LTO, rather than the current patches which look like > "ThinkLTO on/off." Changing the ThinLTO config to a choice and moving it after the main LTO config sounds like a good idea to me. I'll see if I can change this in v8. Thanks! Sami