Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp2703783iog; Mon, 20 Jun 2022 02:55:19 -0700 (PDT) X-Google-Smtp-Source: AGRyM1u6UQnSCzrOMceBuKlM3W4uVjGYIqzM2+XfvckYG718hic0pevFIMhC+sf0TURbI27woTQJ X-Received: by 2002:a17:906:9b86:b0:6f8:24e7:af7d with SMTP id dd6-20020a1709069b8600b006f824e7af7dmr20729366ejc.295.1655718919222; Mon, 20 Jun 2022 02:55:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655718919; cv=none; d=google.com; s=arc-20160816; b=Bvi/lxpGPY4PEJEDX2DNneDZMEsrOAdo73OppiIjgv0SHKFHy6ZECfGnCYhZn0CiyV rpvjwU69Mvahi1k433zuhDx0zYiBv2EN/UeC2x3/BdQUnOIw2fwk9wKBJQSmT6d0aOzX VK1AEOOKJ1iCz+4TuNRlL8Dz70csYX26Hhn/0DJMN1T1Lp02eyFVXiqqDJTxqjSlaycm 43oKb6yUojqr+C8LKMGIIjiZ9i7V7rBQFD/dsCc2VkuSgoZoDJoVy+t01rUILncGgr7E BdyxRVGyrFoUThKKoaYCuoIjWLLkFBQ8XHPpCd+1Qannh32NMqEexH8Xl+Pq507Bdg6G SNnw== 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=8DyE/KWQUZrty8rXx81JIsG8zxsuYBbuRVM0whdGKxU=; b=iGRC1GAQlbvKbXx7fc0/3/R8y58MjGXrnfxqSlKsnjUHA6gji6xIuuPXY6jyxkONhA Cke3Bs3lYjYKsyCeMQ+8jeZpkWjS6R10uVFP/dL+u39YkMMDR6Z7zQMIh40hOa/C06TO VhMqwHb58Yqpyd0kwKji8G3jHJCWDdkueSjS7exK6wwWcguBKRYspsUTwoaLkYvatsey zlnQZkAx7mzBvGpc2b/GmpIiG62jW0svKwwJ0EDNnq6wkXogBx6beBRjq6Uj3xd0w/AC JN04hDPWFVN0UPAd7e2DJyIEjqiw2O6Neke9l9uV13qoVQHMGje4V8ccoyi04Naod75O x/4Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=YMx5LG7W; 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 nc19-20020a1709071c1300b006ff1a60c3ebsi14402830ejc.821.2022.06.20.02.54.51; Mon, 20 Jun 2022 02:55:19 -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=YMx5LG7W; 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 S240304AbiFTJwS (ORCPT + 99 others); Mon, 20 Jun 2022 05:52:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54274 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240222AbiFTJwN (ORCPT ); Mon, 20 Jun 2022 05:52:13 -0400 Received: from mail-yb1-xb2f.google.com (mail-yb1-xb2f.google.com [IPv6:2607:f8b0:4864:20::b2f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 81E6813E19 for ; Mon, 20 Jun 2022 02:52:11 -0700 (PDT) Received: by mail-yb1-xb2f.google.com with SMTP id t1so18002046ybd.2 for ; Mon, 20 Jun 2022 02:52:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=8DyE/KWQUZrty8rXx81JIsG8zxsuYBbuRVM0whdGKxU=; b=YMx5LG7Wdq+ZbP+hmySua4p+q8ITcfNOgA4wNN233s19a/7cF/MJtq3fr/LfgGQy4p ogvSHl2Cf2DYFO76BS+OtDLYvlEEfUWUUgII7m+zE9Wag3P6PJmdocnBsDRN/vqEINWI ESFYkAqEprhYE9UBRvqjrDhG4Bu6CEiZVt9gmH+r/aYAAW3S3pWUCuTKX2VFxgqZG2fL 7fcp5qJdRksrTWb/CAUc1PuUQq8hnng4kyvcesKz7U2bEjUgU6ubETbvxyw0B2MBoO4B lteMT6G/lEy2Rzwd8AoL9zq5hwcwZzAfUZEWGcxA46w79qz+mEgvNVgv0mt59z/DFyWm ty1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=8DyE/KWQUZrty8rXx81JIsG8zxsuYBbuRVM0whdGKxU=; b=Jb/w4CNFDcObno+x3DEj01DLN8pgsd1nHNJMnGuN0pVWIiDZXt0qF6YAXbAFtGExtn Nrk3LKZJmTrhn8hEzxjWn2J6hghjUZ9RppRj9lCT6TGxOaIP9zEyMG0xu76Da/vz2zWp //AGPbjqgVVpjFC6C0RykhuSU64G+hVUCEoHLIg6EdING1We+Kj+OyGX1BHs9F4F0u0v a6YT0kocij0B+se+ikdJ4tn1CrFoUJpXFtEesLBiOhKZTCdSqY8fNgi5QhI/6IhF4XCj JxrJ0fWpacMnawIDMV61blNLAfqEmFGdwBq5YDNVXj/OJDYqmXzgk12pOYF5S5+HqsdT UAHA== X-Gm-Message-State: AJIora/lM6NCjFMefP8DddQmQDWh6ILR4NCWf54FltIyLR1XAloqQ4oW bSyi+fkz8rf1DNfUeNmU8O+Q4wWTjj+yhQoqgun3RA== X-Received: by 2002:a25:94a:0:b0:668:df94:fdf4 with SMTP id u10-20020a25094a000000b00668df94fdf4mr9911328ybm.425.1655718730567; Mon, 20 Jun 2022 02:52:10 -0700 (PDT) MIME-Version: 1.0 References: <20220617144031.2549432-1-alexandr.lobakin@intel.com> <20220617144031.2549432-7-alexandr.lobakin@intel.com> In-Reply-To: <20220617144031.2549432-7-alexandr.lobakin@intel.com> From: Marco Elver Date: Mon, 20 Jun 2022 11:51:34 +0200 Message-ID: Subject: Re: [PATCH v3 6/7] bitops: let optimize out non-atomic bitops on compile-time constants To: Alexander Lobakin Cc: Arnd Bergmann , Yury Norov , Andy Shevchenko , Mark Rutland , Matt Turner , Brian Cain , Geert Uytterhoeven , Yoshinori Sato , Rich Felker , "David S. Miller" , Kees Cook , "Peter Zijlstra (Intel)" , Borislav Petkov , Tony Luck , Maciej Fijalkowski , Jesse Brandeburg , Greg Kroah-Hartman , linux-alpha@vger.kernel.org, linux-hexagon@vger.kernel.org, linux-ia64@vger.kernel.org, linux-m68k@lists.linux-m68k.org, linux-sh@vger.kernel.org, sparclinux@vger.kernel.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org 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=unavailable 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 Fri, 17 Jun 2022 at 19:00, Alexander Lobakin wrote: > > Currently, many architecture-specific non-atomic bitop > implementations use inline asm or other hacks which are faster or > more robust when working with "real" variables (i.e. fields from > the structures etc.), but the compilers have no clue how to optimize > them out when called on compile-time constants. That said, the > following code: > > DECLARE_BITMAP(foo, BITS_PER_LONG) = { }; // -> unsigned long foo[1]; > unsigned long bar = BIT(BAR_BIT); > unsigned long baz = 0; > > __set_bit(FOO_BIT, foo); > baz |= BIT(BAZ_BIT); > > BUILD_BUG_ON(!__builtin_constant_p(test_bit(FOO_BIT, foo)); > BUILD_BUG_ON(!__builtin_constant_p(bar & BAR_BIT)); > BUILD_BUG_ON(!__builtin_constant_p(baz & BAZ_BIT)); > > triggers the first assertion on x86_64, which means that the > compiler is unable to evaluate it to a compile-time initializer > when the architecture-specific bitop is used even if it's obvious. > In order to let the compiler optimize out such cases, expand the > bitop() macro to use the "constant" C non-atomic bitop > implementations when all of the arguments passed are compile-time > constants, which means that the result will be a compile-time > constant as well, so that it produces more efficient and simple > code in 100% cases, comparing to the architecture-specific > counterparts. > > The savings are architecture, compiler and compiler flags dependent, > for example, on x86_64 -O2: > > GCC 12: add/remove: 78/29 grow/shrink: 332/525 up/down: 31325/-61560 (-30235) > LLVM 13: add/remove: 79/76 grow/shrink: 184/537 up/down: 55076/-141892 (-86816) > LLVM 14: add/remove: 10/3 grow/shrink: 93/138 up/down: 3705/-6992 (-3287) > > and ARM64 (courtesy of Mark): > > GCC 11: add/remove: 92/29 grow/shrink: 933/2766 up/down: 39340/-82580 (-43240) > LLVM 14: add/remove: 21/11 grow/shrink: 620/651 up/down: 12060/-15824 (-3764) > > Cc: Mark Rutland > Signed-off-by: Alexander Lobakin Reviewed-by: Marco Elver > --- > include/linux/bitops.h | 18 +++++++++++++++++- > 1 file changed, 17 insertions(+), 1 deletion(-) > > diff --git a/include/linux/bitops.h b/include/linux/bitops.h > index 3c3afbae1533..26a43360c4ae 100644 > --- a/include/linux/bitops.h > +++ b/include/linux/bitops.h > @@ -33,8 +33,24 @@ extern unsigned long __sw_hweight64(__u64 w); > > #include > > +/* > + * Many architecture-specific non-atomic bitops contain inline asm code and due > + * to that the compiler can't optimize them to compile-time expressions or > + * constants. In contrary, gen_*() helpers are defined in pure C and compilers > + * optimize them just well. > + * Therefore, to make `unsigned long foo = 0; __set_bit(BAR, &foo)` effectively > + * equal to `unsigned long foo = BIT(BAR)`, pick the generic C alternative when > + * the arguments can be resolved at compile time. That expression itself is a > + * constant and doesn't bring any functional changes to the rest of cases. > + * The casts to `uintptr_t` are needed to mitigate `-Waddress` warnings when > + * passing a bitmap from .bss or .data (-> `!!addr` is always true). > + */ > #define bitop(op, nr, addr) \ > - op(nr, addr) > + ((__builtin_constant_p(nr) && \ > + __builtin_constant_p((uintptr_t)(addr) != (uintptr_t)NULL) && \ > + (uintptr_t)(addr) != (uintptr_t)NULL && \ > + __builtin_constant_p(*(const unsigned long *)(addr))) ? \ > + const##op(nr, addr) : op(nr, addr)) > > #define __set_bit(nr, addr) bitop(___set_bit, nr, addr) > #define __clear_bit(nr, addr) bitop(___clear_bit, nr, addr) > -- > 2.36.1 >