From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.3 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 37FF1C43461 for ; Wed, 12 May 2021 08:15:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E1DC2613E9 for ; Wed, 12 May 2021 08:15:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230176AbhELIQu (ORCPT ); Wed, 12 May 2021 04:16:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230157AbhELIQt (ORCPT ); Wed, 12 May 2021 04:16:49 -0400 Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [IPv6:2a00:1450:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F23A6C06174A for ; Wed, 12 May 2021 01:15:40 -0700 (PDT) Received: by mail-ej1-x636.google.com with SMTP id k10so2452581ejj.8 for ; Wed, 12 May 2021 01:15:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=ofYbiesQgwQaEexlewrml7cRxApQkWB2Nx460iDcnyo=; b=cHIOWKyzF0AfFHiktnENLignESiqbB75M/2S8bNYGwb4UmCWst1kzfVRtHWBUILKXs c7eSZ9hehTk8NLHKdrT9XYNXDarjEYNKlTulZQDt9ENuebCRNglJRbSanTXy8bx1NlVm YW7PRTEc6U7mjbRYuKiT9exW6nbvZazhgiCk4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=ofYbiesQgwQaEexlewrml7cRxApQkWB2Nx460iDcnyo=; b=YvDMyZ+6n+y+/WiSy5LUIFto2DNhwEFi2Mq3oDGDp643/H+RubwY+bWrv3RFwoKsRY s0UhlGC9cBAzbubFnP7l8z05g/2hpfmh0E1okeir5Z9RRZeA7vulUfxIy9ZnYxXuD5Oe 7VgOojz0rHOpigvl7EiziQ3Xu88VANGZooiYEXC0aNrZ7YiYGQGP1Fso70Yt6aHZONX9 +5E+usKD9g3FTvCuLVv60oYJ1TKCee95L66plg+gH6w/v/hmCqpK0YtMqtkE21+Rr23P L4fa9L595TApKYSUpxXUxqHABrwC3GRzk/tGcM3lJ4U/2EIBMFX9zNoAI/km2mQulyun z8GQ== X-Gm-Message-State: AOAM532BIjKZE/z8PrgZN64Ly9hiSEVdhh8b/hQmbF55IWqXugocrxtf 63AcGn6+IcJzSaz4ZP3A0V2x/A== X-Google-Smtp-Source: ABdhPJzRky2jZTiXS/jGvK3y0xATQ4vtskLl8aSJ36xGqYFwRVDIZS1+rKVRogxxKQkuc5o4q2jaog== X-Received: by 2002:a17:906:f42:: with SMTP id h2mr37200326ejj.317.1620807339603; Wed, 12 May 2021 01:15:39 -0700 (PDT) Received: from [192.168.1.149] ([80.208.71.248]) by smtp.gmail.com with ESMTPSA id um28sm13885567ejb.63.2021.05.12.01.15.37 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 12 May 2021 01:15:39 -0700 (PDT) Subject: Re: [PATCH 11/12] tools: sync lib/find_bit implementation To: Arnd Bergmann , Rikard Falkeborn Cc: Tetsuo Handa , Andy Shevchenko , Yury Norov , Linux Kernel Mailing List , Andrew Morton , linux-m68k , Linux-Arch , Linux-SH , Alexey Klimov , David Sterba , Dennis Zhou , Geert Uytterhoeven , Jianpeng Ma , Joe Perches , John Paul Adrian Glaubitz , Josh Poimboeuf , Rich Felker , Stefano Brivio , Wei Yang , Wolfram Sang , Yoshinori Sato References: <20210401003153.97325-1-yury.norov@gmail.com> <20210401003153.97325-12-yury.norov@gmail.com> <1ac7bbc2-45d9-26ed-0b33-bf382b8d858b@I-love.SAKURA.ne.jp> <151de51e-9302-1f59-407a-e0d68bbaf11c@i-love.sakura.ne.jp> From: Rasmus Villemoes Message-ID: <030ae370-967c-22d4-56f8-cb0435be7540@rasmusvillemoes.dk> Date: Wed, 12 May 2021 10:15:37 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-sh@vger.kernel.org On 12/05/2021 09.48, Arnd Bergmann wrote: > On Tue, May 11, 2021 at 10:39 PM Rikard Falkeborn > wrote: >> On Tue, May 11, 2021 at 08:53:53PM +0900, Tetsuo Handa wrote: > >>> #define GENMASK_INPUT_CHECK(h, l) \ >>> (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \ >>> __builtin_constant_p((l) > (h)), (l) > (h), 0))) >>> >>> __GENMASK() does not need "h" and "l" being a constant. >>> >>> Yes, small_const_nbits(size) in find_next_bit() can guarantee that "size" is a >>> constant and hence "h" argument in GENMASK_INPUT_CHECK() call is also a constant. >>> But nothing can guarantee that "offset" is a constant, and hence nothing can >>> guarantee that "l" argument in GENMASK_INPUT_CHECK() call is also a constant. >>> >>> Then, how can (l) > (h) in __builtin_constant_p((l) > (h)) be evaluated at build time >>> if either l or h (i.e. "offset" and "size - 1" in find_next_bit()) lacks a guarantee of >>> being a constant? >>> >> >> So the idea is that if (l > h) is constant, __builtin_constant_p should >> evaluate that, and if it is not it should use zero instead as input to >> __builtin_chose_expr(). This works with non-const inputs in many other >> places in the kernel, but apparently in this case with a certain >> compiler, it doesn't so I guess we need to work around it. > > I have a vague memory that __builtin_constant_p() inside of > __builtin_choose_expr() > always evaluates to false because of the order in which the compiler processes > those: If constant-folding only happens after __builtin_choose_expr(), then > __builtin_constant_p() has to be false. It's more complicated than that. __builtin_constant_p on something which is a bona-fide Integer Constant Expression (ICE) gets folded early to a 1. And then it turns out that such a __builtin_constant_p() that folds early to a 1 can be "stronger" than a literal 1, in the sense that when used as the controlling expression of a ?: with nonsense in the false branch, the former is OK but the latter fails: https://lore.kernel.org/lkml/c68a0f46-346c-70a0-a9b8-31747888f05f@rasmusvillemoes.dk/ Now what happens when the argument to __builtin_constant_p is not an ICE is a lot more complicated. The argument _may_ be so obviously non-constant that it can be folded early to a 0, hence still be suitable as first argument to __b_c_e. But it is also possible that the compiler leaves it unevaluated, in the "hope" that a later optimization stage could prove the argument constant. And that's the case where __b_c_e will then break, because that can't be left unevaluated for very long - the very _type_ of the result depends on which branch is chosen. tl;dr: there's no "order in which the compiler processes those", __b_c_p can get evaluated (folded) early, before __b_c_e inspects it, or be left for later stages. Rasmus