Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp298376rwb; Wed, 7 Dec 2022 18:21:25 -0800 (PST) X-Google-Smtp-Source: AA0mqf6nyU96RC/jtna0UurNr4v+QkPKwfhREuABOGItOMQMPs78vMqql/RYBpmI+AUEWcE29Pyt X-Received: by 2002:a05:6402:5308:b0:460:19c3:2992 with SMTP id eo8-20020a056402530800b0046019c32992mr9334361edb.1.1670466084943; Wed, 07 Dec 2022 18:21:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670466084; cv=none; d=google.com; s=arc-20160816; b=tffyKzAaglIYkJv5DcRHm4FgRumQG3Bqe0jgJDe0/+6UkcI7761p0G05soh1In+rUh CA4RyfV6LEI2zBEg6yujq6hVTtfhlkALLbbKgRDFifG+n/FiQFXRa/4CD5TrlOpGSMiG 38556SJepZoyh+jYfKYYZoqzNIb9chIQXZFHfeDXuMD/tt7t5R/RLgKKo5sFMfdUUOIt UmK8x9y9eeR7R6MaoxHbHRzNjcOQc6pg7qpqzkxwAauswCjrEuztY0O7Id3sMvPy3X5Q DFxsEUeeY3dBAUw5lsFMFy8ik2j/wADQU8SXRrc3pHcgTCyIv0+Ex/RINHnzbXJRDkpZ 9Ppw== 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:dkim-filter; bh=k3I8on4gq36IvbTZWeLET12jxubwa9OOg1ZKsIOl2ss=; b=Ydg4kwK9RZtkHf2VmpW+jqu7SsOU+Epmg5fAQrReWBzy/daMQqa7qUox5lFLDvKTVp P5FV5ANYwaKxQqY5C9d06gWO815WG0EhMST08j7oA09L+PvnBL58dFmMroFVslCHpiib YCDs9rdgI/ZiolV18wWB2uwvRIvCFf0Ytd4pmhqUZuz194h4+dwB+QxTihhhbobEDWJa oKxsl6q47qJfLatcsejv4AshDGeyss0EuaA9FDftIlqi/oqNtBLNQpVTv/TJFAAHZMep YJOQUmT44VDZkWK/GJ8ZivDnS+Hi7YpDMGUCRveruMPRBRfZEGHVpSammfPdA4vMfG6b HdtA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nifty.com header.s=dec2015msa header.b=mtOo8T0c; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id js9-20020a17090797c900b007413f3cdd18si20678294ejc.238.2022.12.07.18.20.45; Wed, 07 Dec 2022 18:21:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@nifty.com header.s=dec2015msa header.b=mtOo8T0c; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229650AbiLHBno (ORCPT + 75 others); Wed, 7 Dec 2022 20:43:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229782AbiLHBnl (ORCPT ); Wed, 7 Dec 2022 20:43:41 -0500 Received: from conssluserg-01.nifty.com (conssluserg-01.nifty.com [210.131.2.80]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F42134B756; Wed, 7 Dec 2022 17:43:39 -0800 (PST) Received: from mail-ot1-f44.google.com (mail-ot1-f44.google.com [209.85.210.44]) (authenticated) by conssluserg-01.nifty.com with ESMTP id 2B81hOiI017429; Thu, 8 Dec 2022 10:43:25 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conssluserg-01.nifty.com 2B81hOiI017429 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1670463805; bh=k3I8on4gq36IvbTZWeLET12jxubwa9OOg1ZKsIOl2ss=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=mtOo8T0cq1jDd6kIRWnpp0UdEGEL2YahHo5hb57KLXkHKlw8MIhG/J1CiPCd6uPFd KKpT1X4bdoAnRLZXhc9q8WChe3TyVckcHUCuiN/uu+KE4MPcRKvWhMrzjkaYjf+6s8 IWkC8+NobGSN3dDvY4HESmxF7kgvUZJRCtoKi0bzL35t6jeGAn3CTMqAV14BCNeiFX JNGvjTlqlXbxWxIGWZS7Hh9kGRDaKqmhczCH/0m0lPLHW/ugIrYX0aQV/k6EkxJtwA FI03uKGiOTh7uoplKlf7YGo8xaMqTiCt06gFAxnztMsh5U5dF7v3xWrHV2JPTW6jvw ol6jaKsi8Yu7w== X-Nifty-SrcIP: [209.85.210.44] Received: by mail-ot1-f44.google.com with SMTP id v19-20020a9d5a13000000b0066e82a3872dso81239oth.5; Wed, 07 Dec 2022 17:43:25 -0800 (PST) X-Gm-Message-State: ANoB5pknK4HFf5TwNXRqg/xKfhE6CqrxDe/UP4sy5xMaomiCi2AMcq5z K+AsOjz1EAPD0I/ICMjB7ZThqZHV2aCM2fj2MZw= X-Received: by 2002:a9d:282:0:b0:66c:794e:f8c6 with SMTP id 2-20020a9d0282000000b0066c794ef8c6mr47413478otl.343.1670463804040; Wed, 07 Dec 2022 17:43:24 -0800 (PST) MIME-Version: 1.0 References: <20221123151828.509565-1-masahiroy@kernel.org> In-Reply-To: <20221123151828.509565-1-masahiroy@kernel.org> From: Masahiro Yamada Date: Thu, 8 Dec 2022 10:42:47 +0900 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v2 1/5] kbuild: add test-{le,ge,lt,gt} macros To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Albert Ou , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Ingo Molnar , Nathan Chancellor , Nick Desaulniers , Nicolas Schier , Palmer Dabbelt , Paul Walmsley , Thomas Gleixner , Tom Rix , linux-riscv@lists.infradead.org, llvm@lists.linux.dev, x86@kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_SOFTFAIL autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Nov 24, 2022 at 12:19 AM Masahiro Yamada wrote: > > Because GNU Make is only able to handle strings, it is very hard to > perform arighmetic in Makefiles. I terribly missed that GNU Make 4.4 introduced the $(intcmp ...) function. https://www.gnu.org/software/make/manual/make.html#Conditional-Functions I am thinking if we can do better by using it. > > When we compare two integers, we invokes shell. One example is in the > top Makefile: > > ifeq ($(shell test $(CONFIG_LLD_VERSION) -lt 130000; echo $$?),0) > > This is more expensive than using built-in functions since it forks a > process. > > If we know the two have the same number of digits, we can do better. > > This commit adds four macros, test-le, test-ge, test-lt, test-gt. > > $(call test-lt, A, B) is evaluated to 'y' if A is less than B, or > empty otherwise. This will replace $(call shell test A -lt B). > > Again, the limitation is that A and B must have the same number of > digits because these macros are based on $(sort ) function. > > $(call test-lt, 1, 9) --> y (Works!) > $(call test-lt, 10, 9) --> y (Not work...) > > To make the latter work, you need to add '0' prefix to align the number > of digits: > > $(call test-lt, 10, 09) --> empty (Works!) > > Actually, we can live with this limitation in many places. As for the > example above, we know $(CONFIG_LLD_VERSION) is 6-digits because the > minimal supported version of LLVM is 11.0.0. > > So, the shell invocation can be replaced with more efficient code: > > ifeq ($(call test-lt, $(CONFIG_LLD_VERSION), 130000),y) > > Of course, this assumption will break when LLVM 100 is released, but it > will be far in the future. > > Signed-off-by: Masahiro Yamada > --- > > (no changes since v1) > > Makefile | 2 +- > arch/riscv/Makefile | 2 +- > arch/x86/Makefile | 2 +- > scripts/Kbuild.include | 10 ++++++++++ > 4 files changed, 13 insertions(+), 3 deletions(-) > > diff --git a/Makefile b/Makefile > index 6f846b1f2618..eb80332f7b51 100644 > --- a/Makefile > +++ b/Makefile > @@ -986,7 +986,7 @@ KBUILD_LDFLAGS += -mllvm -import-instr-limit=5 > # Check for frame size exceeding threshold during prolog/epilog insertion > # when using lld < 13.0.0. > ifneq ($(CONFIG_FRAME_WARN),0) > -ifeq ($(shell test $(CONFIG_LLD_VERSION) -lt 130000; echo $$?),0) > +ifeq ($(call test-lt, $(CONFIG_LLD_VERSION), 130000),y) > KBUILD_LDFLAGS += -plugin-opt=-warn-stack-size=$(CONFIG_FRAME_WARN) > endif > endif > diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile > index 0d13b597cb55..faf2c2177094 100644 > --- a/arch/riscv/Makefile > +++ b/arch/riscv/Makefile > @@ -37,7 +37,7 @@ else > endif > > ifeq ($(CONFIG_LD_IS_LLD),y) > -ifeq ($(shell test $(CONFIG_LLD_VERSION) -lt 150000; echo $$?),0) > +ifeq ($(call test-lt, $(CONFIG_LLD_VERSION), 150000),y) > KBUILD_CFLAGS += -mno-relax > KBUILD_AFLAGS += -mno-relax > ifndef CONFIG_AS_IS_LLVM > diff --git a/arch/x86/Makefile b/arch/x86/Makefile > index 415a5d138de4..e72c7a49cd59 100644 > --- a/arch/x86/Makefile > +++ b/arch/x86/Makefile > @@ -211,7 +211,7 @@ endif > KBUILD_LDFLAGS += -m elf_$(UTS_MACHINE) > > ifdef CONFIG_LTO_CLANG > -ifeq ($(shell test $(CONFIG_LLD_VERSION) -lt 130000; echo $$?),0) > +ifeq ($(call test-lt, $(CONFIG_LLD_VERSION), 130000),y) > KBUILD_LDFLAGS += -plugin-opt=-stack-alignment=$(if $(CONFIG_X86_32),4,8) > endif > endif > diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include > index cbe28744637b..9996f34327cb 100644 > --- a/scripts/Kbuild.include > +++ b/scripts/Kbuild.include > @@ -11,6 +11,16 @@ space := $(empty) $(empty) > space_escape := _-_SPACE_-_ > pound := \# > > +### > +# Comparison macros. > +# Usage: $(call test-le, A, B) > +# works like shell's "test A -le B", but A and B must have the same number of > +# digits since it is just ASCII sort. > +test-le = $(if $(filter $1, $(firstword $(sort $1 $2))),y) > +test-ge = $(call test-le, $2, $1) > +test-lt = $(if $(filter-out $2, $(firstword $(sort $1 $2))),y) > +test-gt = $(call test-lt, $2, $1) > + > ### > # Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o > dot-target = $(dir $@).$(notdir $@) > -- > 2.34.1 > -- Best Regards Masahiro Yamada