Received: by 10.192.165.156 with SMTP id m28csp432556imm; Thu, 19 Apr 2018 01:21:25 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+pG4xSZ+rEvLjG03NTRAaX2cbm6xjbjTxVYIXn9E5TOXRJtJgL/XCKxpUoztfzJja30IaT X-Received: by 10.98.89.89 with SMTP id n86mr4979340pfb.158.1524126085683; Thu, 19 Apr 2018 01:21:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524126085; cv=none; d=google.com; s=arc-20160816; b=XL6VnxIGa4xh6zJZ8PN64WkrXGDm5/Qfowb3QXLL+5eM0mslTwNWYO8Fxh//cveOP2 tyc6kOF2yGRXMQCxPpcDJinhCvGJYfs6GOBNIomq7cgUzzPTeYYyjFNiZSQXDX6kZhbl 8etzjhHli0rRFB7rS2Sf4U7uTqlgDe75Pwv8686JaPT5Jge/mkhYuY03vWroS7pICHrn fSP25HZMLE3fTADSksDoP3YZfadN+NFnNL6uelAC/7PutCBCUHQdIc4ueB22PVUFjJJe 20Iv+zaymirIQ46clHNcfPpi+qgpBZplbSkJ4po+O3fCjt/i0kDq9tH7YSzrcKajZE+V mhFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature :arc-authentication-results; bh=z0qq6PE1ABuJocyXKatYub7Uqjg/GXRK4z4250nQK3U=; b=ZIw8DwGdqkKzFw+sqaJvE1D8/Yp0ty2NP9dN53vi57y9UB9HlsdgccYgTBU6OXfB+F 1L8VUjvIEHrHE/udbaQwCBtRUp5WS/SfDMlHMwJHF4ubmvg8h7CkFnNqtP/KYN45OvrS pbPXEvinBiUQgYNp5mlobW8XH3hTvLTQFvOoMPuS6wciIG/f/c7MqNrM2nAw8rKSFogK 1LT1Idy4/VC/ooEBq7iIIUlVopjJTvfp41W7Ow96q/ilgxwdHX8ifLiAR2HImzPUWL42 rLeTij52YurmJFHR9+IMCpJ8NrgLYT/P2y0TMCV/w6bzBrzwVQsYSrBG2yzsx9whMLsq EOnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=fJihQUXZ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o6si2687175pgs.51.2018.04.19.01.21.10; Thu, 19 Apr 2018 01:21:25 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=fJihQUXZ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752911AbeDSIUC (ORCPT + 99 others); Thu, 19 Apr 2018 04:20:02 -0400 Received: from mail-wr0-f182.google.com ([209.85.128.182]:34090 "EHLO mail-wr0-f182.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751182AbeDSIT6 (ORCPT ); Thu, 19 Apr 2018 04:19:58 -0400 Received: by mail-wr0-f182.google.com with SMTP id d19-v6so11555455wre.1 for ; Thu, 19 Apr 2018 01:19:57 -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=z0qq6PE1ABuJocyXKatYub7Uqjg/GXRK4z4250nQK3U=; b=fJihQUXZ9ama58fgT90eBeGSCgHbR3FKti309eMqQ3DDw34fqxWpqq8OlwLrOGcv8e XLbn5OMjw2mq0W1Mkm5eTI0p1SAmjIxjlvYaolD9vVTjOgHeQCJvuOTPTMpVSyUuvC9p mop4gurQFYwrbiZdTDwkXS1Qaza1ORJKx9OKQ= 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=z0qq6PE1ABuJocyXKatYub7Uqjg/GXRK4z4250nQK3U=; b=BMxA/1Hjvlm2XqyTd/B9jKk6DOc1Y/17KgNjqSbetKP3H8ykTQ+N3gcg5guyPyiejj F7YQBp0+mPtRln/6lgvbBScNyugwmc6oNZ0ak3iDJYSkP71cuWcGiE89GjzguvaI3foP 1ZoUZg6RYLWhQyL1ODbG05niw06UJtlA+AffvI/Bv/7VDuCCMPzg2r98CTixLtkR+HiB 2aH7IeXmiyG39jRbm5k5mVnM6IDMTKGUw0QOYYyFSns9a9U9I5x08AfNvi1wmYVOn+0m IXw1vYx9SyxAnsMkWDC1EBKSNSrFz+jMOpjfoPTm8HBXoGfxj3SVumD37MczDpGWKST3 ta3A== X-Gm-Message-State: ALQs6tA4aapSAb89Ji8JBnX1v/4/3i9GwiwCy0nCzzwj3abwKDNPwNcI 5APgi0v0GFPCQaVHLmA+aDIK8g== X-Received: by 10.80.143.196 with SMTP id y62mr7333731edy.275.1524125996542; Thu, 19 Apr 2018 01:19:56 -0700 (PDT) Received: from [172.26.252.144] ([193.47.71.171]) by smtp.gmail.com with ESMTPSA id i15sm2247237edb.56.2018.04.19.01.19.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Apr 2018 01:19:55 -0700 (PDT) Subject: Re: [PATCH v3 1/6] ilog2: create truly constant version for sparse To: Linus Torvalds , Martin Wilck Cc: "Martin K. Petersen" , Linux SCSI List , Linux Kernel Mailing List , Hannes Reinecke , James Bottomley , Xose Vazquez Perez , Bart Van Assche , Ard Biesheuvel , Thomas Gleixner , Jonathan Corbet , Randy Dunlap References: <20180417233511.6573-1-mwilck@suse.com> <20180417233511.6573-2-mwilck@suse.com> <1524039174.3028.24.camel@suse.com> From: Rasmus Villemoes Message-ID: Date: Thu, 19 Apr 2018 10:19:54 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2018-04-18 23:21, Linus Torvalds wrote: > On Wed, Apr 18, 2018 at 1:12 AM, Martin Wilck wrote: >> >> No, it doesn't (gcc 7.3.0). -> https://paste.opensuse.org/27471594 >> It doesn't even warn on an expression like this: >> >> #define SIZE (1<<10) >> static int foo[ilog2(SIZE)]; > > Ok, I think this is the "random gcc versions act differently" issue. > > Sometimes __builtin_constant_p() to a ternary operation acts as a > constant expression, and sometimes it doesn't. So __builtin_constant_p on an actual ICE always fold immediately to 1. Looking at the gcc history, that seems to have been there since at least 2000, but likely forever. And when it's the controlling expression of a ternary, it's apparently a stronger 1 than a literal 1: int foo(int); #define SIZE (1<<10) #define half(x) (__builtin_constant_p(x) ? (x)>>1 : foo(x)) int bla[half(SIZE)]; int bla2[1 ? 123 : foo(3)+2]; on godbolt.org, gcc 4.1 and gcc4.4 are perfectly happy with this, but newer gccs complain error: variably modified 'bla2' at file scope. Argh. > I suspect using the __is_constexpr() trick would make it rather more > technically correct, but would actually generate much worse code. > > Because it would make us do that dynamic "__ilog2_uXX()" function call > even when you have a compile-time constant value, if it wasn't > actually a constant expression (ie a constant argument passed to an > inline function, for example). It's a bit ugly, but I suppose one could keep a __builtin_constant_p() check in the not-ICE-branch, so there's really three cases (ICE, constant due to various optimizations, really not known at compile time). But don't we use gcc's intrinsics for fls when available, so that gcc should be able to know the semantics of __builtin_fls(some-constant) and hence evaluate that itself at compile time? Somewhat unrelated, we should probably move the __is_constexpr helper from kernel.h to some more basic compiler header, to avoid cyclic header dependencies. Rasmus