Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1669870ybt; Thu, 25 Jun 2020 11:15:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzrOMCap93jQGmqh88lNPPTiqY/3ZUDPyxoS6IVSbgRaiq13UEiS4m4COt77pSqNLy+V6ba X-Received: by 2002:a17:906:7e04:: with SMTP id e4mr22044353ejr.502.1593108905668; Thu, 25 Jun 2020 11:15:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593108905; cv=none; d=google.com; s=arc-20160816; b=opADtLtyrp+nYtBDp/Nyseh6yl0469kbfQ6K3IDI6iWSWM2+Du/Yeyv8aa+llfGZ6W DyRzDIlwE6lN0B7+LPKylDNcXf2GqiBH4pck3W4nuAl7p7O3ZLohHU/alMSG6LLA0BXn aQqjbeYw0NBz/UrOgYOmVIxMTg2Isqa37p3PI6Et/DQKYdU4F2lxDiMbJ6Ba+vpASnSJ SJKp5D0SFsvyblDHjeS0dH/eleb08+WpyAgC4aNTRufTQd+GC3I7Cx3Ie/T4U/UQogCU gyS7cah+pefQuOFRtW4iyQUvsuTKOH/qCVZWCmrehLtzYY+8vg30E8dbCsmz0ykoFmns WGVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=tN3RRFkW8H40rA8SGu1JDOigZLSzT5qVO0+D16XtuLc=; b=RJlLcbPEa5NZXTZBRft5zJh5ixqhOmei5XllfqRSah4UeqSwghcqvrqj31O2oN9C1j f8uy6Bw8WbA0oi9CVGNfKfx5RZxl4KoMQzcnCetjF/z9KX7vxpCZ03iGcMH6G9XN+2bb /zm18P3Py3Sy9fvKFvJ6Hli1HbXO1QbWfvzs/15z48SKFIwg3rChjaFapR4H3YKJfwYI JhgyDxVWF2xOP2NzxHDjjT31iJzYtjW9zsVJL2xA1rqeoR9n5p6Sp9C0ViOeKUQgSbvI Mb1N5Sx2F2XkEzg6iqaEXZj9xWaLbJE3f4KJqNCjBUOCX71UWLZsiz7n7oWSULpcIqS7 dmfQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="FNy/7DFb"; 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 n9si2806497eja.529.2020.06.25.11.14.41; Thu, 25 Jun 2020 11:15:05 -0700 (PDT) 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="FNy/7DFb"; 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 S2406049AbgFYQNs (ORCPT + 99 others); Thu, 25 Jun 2020 12:13:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34936 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390703AbgFYQNr (ORCPT ); Thu, 25 Jun 2020 12:13:47 -0400 Received: from mail-pj1-x1043.google.com (mail-pj1-x1043.google.com [IPv6:2607:f8b0:4864:20::1043]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5B15BC08C5DD for ; Thu, 25 Jun 2020 09:13:47 -0700 (PDT) Received: by mail-pj1-x1043.google.com with SMTP id cv18so660039pjb.1 for ; Thu, 25 Jun 2020 09:13:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=tN3RRFkW8H40rA8SGu1JDOigZLSzT5qVO0+D16XtuLc=; b=FNy/7DFbxnBgI72+dWF1OpzoyRqVVhI3k7b313hoPAknwI2gtcpnrbI/QxduZaLChO XsS20NJXJUX+BgZZni3WHhBJhCOxGeey5qw4s+h96KkQc1DbqtdERktvlQbbWfMI5Qtj CMFRos0WWK71LdR3xO8mzueiOZZD/HrvnbZcL7TWPhuTj08pp29CzK2iwQtWYxS/KzPS DbZRhxXQXIM+BXTF2MYRYYBA9EL7Z4t+n9j3emOXcA8xRPQhjqftNQZaWno22ZXuxY9B OJCpp0YifwADaKWWU1uXVBh+ae7gxCNBEpNdOASG/NzMPjRUdcy8BrCxoR+02HHMW5zQ TEBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=tN3RRFkW8H40rA8SGu1JDOigZLSzT5qVO0+D16XtuLc=; b=EL+39h0v49lFxcAiNKWjv9NhmDJTPl7gvOqN75/c0cO2Zze7KPmmCK9jUAGOf/TCWf 0mGe3YpJe7YoaPFmfKk82nTMoSEpSCjU9HqBpLtuKD7d1x6vxrL9RuPuLlWDxKIlb85w ltiAXFEcO4Kf7vrMB4S2GlXZWyc2iNUbzw/2Cxom6PlxYTFyuZb61zEHeN1oL6WVHq55 i74IUM+/FpkMgAyWZYRywnraTFeBk3psgrzDp0AjurI55dyfxakrL6X6yYxhnY/DsmSO uHPNo3wGnPJDzZXXnjdzzIzTI2ysQx1mgdtQqhN1YmwhAv6QrH0xCTYThvq+GKhUeWx7 1ixw== X-Gm-Message-State: AOAM533k43LrOmX8pKap8MwbN8n+4QC5vR+YXfpZkdMRqEq2sKf+F1iJ R7l8NBs8R/V9T/76SlFkywgNJA== X-Received: by 2002:a17:90a:7c4e:: with SMTP id e14mr4310401pjl.52.1593101626297; Thu, 25 Jun 2020 09:13:46 -0700 (PDT) Received: from google.com ([2620:15c:201:2:ce90:ab18:83b0:619]) by smtp.gmail.com with ESMTPSA id v7sm22710119pfn.147.2020.06.25.09.13.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jun 2020 09:13:45 -0700 (PDT) Date: Thu, 25 Jun 2020 09:13:39 -0700 From: Sami Tolvanen To: Nathan Chancellor Cc: Masahiro Yamada , Will Deacon , Greg Kroah-Hartman , "Paul E. McKenney" , Kees Cook , Nick Desaulniers , clang-built-linux@googlegroups.com, kernel-hardening@lists.openwall.com, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, x86@kernel.org Subject: Re: [PATCH 02/22] kbuild: add support for Clang LTO Message-ID: <20200625161339.GA173089@google.com> References: <20200624203200.78870-1-samitolvanen@google.com> <20200624203200.78870-3-samitolvanen@google.com> <20200625022647.GB2871607@ubuntu-n2-xlarge-x86> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200625022647.GB2871607@ubuntu-n2-xlarge-x86> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jun 24, 2020 at 07:26:47PM -0700, Nathan Chancellor wrote: > Hi Sami, > > On Wed, Jun 24, 2020 at 01:31:40PM -0700, 'Sami Tolvanen' via Clang Built Linux 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. > > > > Note that support for DYNAMIC_FTRACE and MODVERSIONS are added in > > follow-up patches. > > > > Signed-off-by: Sami Tolvanen > > --- > > Makefile | 16 ++++++++ > > arch/Kconfig | 66 +++++++++++++++++++++++++++++++ > > include/asm-generic/vmlinux.lds.h | 11 ++++-- > > scripts/Makefile.build | 9 ++++- > > scripts/Makefile.modfinal | 9 ++++- > > scripts/Makefile.modpost | 24 ++++++++++- > > scripts/link-vmlinux.sh | 32 +++++++++++---- > > 7 files changed, 151 insertions(+), 16 deletions(-) > > > > diff --git a/Makefile b/Makefile > > index ac2c61c37a73..0c7fe6fb2143 100644 > > --- a/Makefile > > +++ b/Makefile > > @@ -886,6 +886,22 @@ KBUILD_CFLAGS += $(CC_FLAGS_SCS) > > export CC_FLAGS_SCS > > endif > > > > +ifdef CONFIG_LTO_CLANG > > +ifdef CONFIG_THINLTO > > +CC_FLAGS_LTO_CLANG := -flto=thin $(call cc-option, -fsplit-lto-unit) > > +KBUILD_LDFLAGS += --thinlto-cache-dir=.thinlto-cache > > +else > > +CC_FLAGS_LTO_CLANG := -flto > > +endif > > +CC_FLAGS_LTO_CLANG += -fvisibility=default > > +endif > > + > > +ifdef CONFIG_LTO > > +CC_FLAGS_LTO := $(CC_FLAGS_LTO_CLANG) > > +KBUILD_CFLAGS += $(CC_FLAGS_LTO) > > +export CC_FLAGS_LTO > > +endif > > + > > # arch Makefile may override CC so keep this after arch Makefile is included > > NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include) > > > > diff --git a/arch/Kconfig b/arch/Kconfig > > index 8cc35dc556c7..e00b122293f8 100644 > > --- a/arch/Kconfig > > +++ b/arch/Kconfig > > @@ -552,6 +552,72 @@ 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 > > + > > +choice > > + prompt "Link Time Optimization (LTO)" > > + default LTO_NONE > > + help > > + This option enables Link Time Optimization (LTO), which allows the > > + compiler to optimize binaries globally. > > + > > + If unsure, select LTO_NONE. > > + > > +config LTO_NONE > > + bool "None" > > + > > +config LTO_CLANG > > + bool "Clang's Link Time Optimization (EXPERIMENTAL)" > > + depends on CC_IS_CLANG && CLANG_VERSION >= 110000 && LD_IS_LLD > > I am curious, what is the reason for gating this at clang 11.0.0? > > Presumably this? https://github.com/ClangBuiltLinux/linux/issues/510 > > It might be nice to notate this so that we do not have to wonder :) Yes, that's the reason. I'll add a note about it. Thanks! Sami