Received: by 2002:a05:6358:bb9e:b0:b9:5105:a5b4 with SMTP id df30csp5311393rwb; Wed, 7 Sep 2022 00:16:03 -0700 (PDT) X-Google-Smtp-Source: AA6agR5enWI4ngjXIAxSZZvY+2/v7zNFodhexBQwgnC9e3NWPeF3k/7mqooCwOHkfBYvnnQLWYHU X-Received: by 2002:a63:a43:0:b0:430:9ec5:4775 with SMTP id z3-20020a630a43000000b004309ec54775mr2281688pgk.572.1662534963499; Wed, 07 Sep 2022 00:16:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662534963; cv=none; d=google.com; s=arc-20160816; b=aMqRd/eUbJxe15zqQIfD43Rgf7cA1AFDLxpdcgkjwEIdsBoyIoPtpsDCWob8hryDq0 XvXo7IioOslrZJOCH5tD9nLEAys+sXXhNSN54k4MEca1iFde46bcsQaOV0C2vJzYTuma 2tH5TmVIbPYtfc3WKsROsnxUC/UUG6CrkqpNRR6pbKGpAfHbyDLTtMAu1PepgIC65Z1c CynW8SlzXapqvVcTcm8iGGY/JhBhmHQ0GWOnCoUFFeSnC5HjlAWIhZEWWW78C9x98xI9 dRWFIfOT1KvWBy47a0MNFeJC1PD1mPiI37BIq0U40AwOaC9ZGGfB45bBlmSZqEtjwyEo X5eg== 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=lB1tmo02fRBU5l99ERq/B48ipxrlpTBOmsY3GpHVDq0=; b=DQDsBYy7zhUsOVz3t9FUD6WsTLbdbMhcPZf/TIeN58epHjTQy0kDBA4fQ3e0KlzvQI IaMEiNAgBppLXDrTZ8ydu6tx6xBBIUFbDRnSQQ2uCXHWEKwVN66I01W5/XRhVOFdNKoW RVpQrkkWW3gy4se/wBdaCY6myiYG08a7yg6BsYVdUs4aPEnnvtmCWeXJOar2xoumff9d IOzWyQUEQrZEguDqW4GeSEaMme15mhsY6LmjsBkFyM6wcDHlnG67Om6Lox3XjUGxd8jh i7Y49MCgaoBaTVWpDm9hQ6hMjvyIY+PQLSrdKJzoUP9YwkyXZeYktpkQdaFWHQJg39GZ Bt1w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="jpxnKj/t"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bf12-20020a656d0c000000b0042c7b6728a5si3430602pgb.812.2022.09.07.00.15.51; Wed, 07 Sep 2022 00:16:03 -0700 (PDT) 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=@google.com header.s=20210112 header.b="jpxnKj/t"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229837AbiIGHE7 (ORCPT + 99 others); Wed, 7 Sep 2022 03:04:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229661AbiIGHE5 (ORCPT ); Wed, 7 Sep 2022 03:04:57 -0400 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A8A7B4B0E8 for ; Wed, 7 Sep 2022 00:04:55 -0700 (PDT) Received: by mail-wr1-x434.google.com with SMTP id bj14so5576924wrb.12 for ; Wed, 07 Sep 2022 00:04:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date; bh=lB1tmo02fRBU5l99ERq/B48ipxrlpTBOmsY3GpHVDq0=; b=jpxnKj/tEOqK+taEe1NmXQq96RAJosDbqCL3MYiTan0yY9wRDEYP/c9UDDpwK3wqFe 3Dk94gHbTndr8sH9LH1wRMAE7OvmiI8Kv4KKf7Qh7xWteJv72aYDkReV0FfVX2hDLIHt X8eIaBcG3xS0FPpWa3Q2u5awuNyRmr5qBo9yQShYvkUf/i+AYrOYrviOTZODAwAHr0+Q 65uYP7r9+32R+wrFTso8y4jpgB319KqVUnekM3YvXqv27A6BuhNU2Od/lhv9TldDXBN3 +5dlWoKjGF5CG7uoX+qiTaYcWIpaOiPskwkGvfNHu04UZ8wuB+hAxskBGugwsUPofBtJ zmfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=lB1tmo02fRBU5l99ERq/B48ipxrlpTBOmsY3GpHVDq0=; b=MYHM+IVKKl1EO/ODwPnlxKKE5hO3hQW+boqrJd8dY3CFwyklITyTnbtEsoCtQSTWJ+ MwpCtbOW5Q69P44mHpfisCNuKBFftkNJRcNBBMZ1Pj4CDwkQ03DCebzdIZ/PjrgFjq0s D+hMMuHOCHuuWc5anKtXyxsAobh+WlEzqP0iKobkcZI5pb9bJfDVBPrGIelO95tSrlKt ephBznZds8EIRILCfUGI8dUufple4ajTMj5JP6wqSiK+QyBvPjlQ6X0wi++Y2X72Tygo FMnOS/Rrt6gJGMM1c0A9U/CkEtSnW+Wnt2frB5c3iUFaZYa0CG8ZRPP17uMDarfhJKfz WZOA== X-Gm-Message-State: ACgBeo1SLE/cfxVlSGKAhqA7HQMZqmtAqr4W5uvuN2KJZGws1cgvl1cI 26wFww6VKBg8fEPZbaC2KvNp48aUOyfqxdjMVS9FIA== X-Received: by 2002:a5d:47ac:0:b0:226:df12:412f with SMTP id 12-20020a5d47ac000000b00226df12412fmr1129390wrb.675.1662534294077; Wed, 07 Sep 2022 00:04:54 -0700 (PDT) MIME-Version: 1.0 References: <20220511160319.1045812-1-mailhol.vincent@wanadoo.fr> <20220905003732.752-1-mailhol.vincent@wanadoo.fr> In-Reply-To: From: Nick Desaulniers Date: Wed, 7 Sep 2022 00:04:42 -0700 Message-ID: Subject: Re: [PATCH v7 0/2] x86/asm/bitops: optimize ff{s,z} functions for constant expressions To: Vincent Mailhol Cc: Borislav Petkov , Thomas Gleixner , Ingo Molnar , x86@kernel.org, Peter Zijlstra , Dave Hansen , "H . Peter Anvin" , Nathan Chancellor , Tom Rix , linux-kernel@vger.kernel.org, llvm@lists.linux.dev, David Howells , Jan Beulich , Christophe Jaillet , Joe Perches , Josh Poimboeuf , Yury Norov Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham 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 Tue, Sep 6, 2022 at 11:26 AM Nick Desaulniers wrote: > > On Sun, Sep 4, 2022 at 5:38 PM Vincent Mailhol > wrote: > > > > The compilers provide some builtin expression equivalent to the ffs(), > > __ffs() and ffz() functions of the kernel. The kernel uses optimized > > assembly which produces better code than the builtin > > functions. However, such assembly code can not be folded when used > > with constant expressions. > > Another tact which may help additional sources other than just the > Linux kernel; it seems that compilers should be able to fold this. > > Vincent, if you're interested in making such an optimization in LLVM, > we'd welcome the contribution, and I'd be happy to show you where to > make such changes within LLVM; please let me know off thread. Oh right, it already does. https://github.com/llvm/llvm-project/blob/ea953b9d9a65c202985a79f1f95da115829baef6/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp#L2635 I see what's happening. Constant propagation sinks constants into a specialized version of ffs when there's only 1 callsite in a given translation unit (or multiple call sites with the same constant). Then dead argument elimination removes the argument, so libcall optimization thinks this isn't the ffs(int) you're looking for, and skips it. Nice. https://github.com/llvm/llvm-project/issues/57599 I guess ffs() is usually forward declared in strings.h, so we don't have such a static inline definition available to constant prop/specialize in normal C code. -- Thanks, ~Nick Desaulniers