From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-4.3 required=3.0 tests=AWL,BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, NICE_REPLY_A,RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 8DE631F66E for ; Sat, 22 Aug 2020 10:31:01 +0000 (UTC) Received: from localhost ([::1]:39078 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1k9Qnc-0008Vw-6f for normalperson@yhbt.net; Sat, 22 Aug 2020 06:31:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49830) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k9QnY-0008Uo-7k for bug-gnulib@gnu.org; Sat, 22 Aug 2020 06:30:56 -0400 Received: from mo4-p00-ob.smtp.rzone.de ([85.215.255.21]:20136) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k9QnV-0004NS-JR for bug-gnulib@gnu.org; Sat, 22 Aug 2020 06:30:55 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1598092251; s=strato-dkim-0002; d=clisp.org; h=References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: X-RZG-CLASS-ID:X-RZG-AUTH:From:Subject:Sender; bh=czlurjBb8+CefXErnklukAYtg4l8m/xi36SVwlfbw/g=; b=EgPbGdqSg8Dti255S21IwKGACuc5pAVygnnRTG5FSnOAVPlVPg5R5jxFsRBSkEgIPh Zu7OOfkisApXYtnf5jiz/ITUq9ETfrvnf6HQB8yPjGscjNH2C0U5Gq228QBZ2YKDUnlB sGasSEHGIw5FtWTJjrD/MT0yeVaVsayNMMCsNsBFaKaHbl3ZfA5Es4gyJ83wOuRZtfpY s4ng38VofYZ7beRQFRW8e2FD0PJ8hLBPxNNkznbQ7VBjRZppgtMppgcUCbSAjDRBBbTj FjCL/MIlfwbtUzBHe0zbpJ5llwp9pN3urjoA6LpxaFiJjfBnQwwAcPF6cc1QKrmsk7sj hXww== X-RZG-AUTH: ":Ln4Re0+Ic/6oZXR1YgKryK8brlshOcZlIWs+iCP5vnk6shH+AHjwLuWOHqfyyPs=" X-RZG-CLASS-ID: mo00 Received: from bruno.haible.de by smtp.strato.de (RZmta 46.10.7 DYNA|AUTH) with ESMTPSA id z05f0fw7MAUnDNa (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (curve X9_62_prime256v1 with 256 ECDH bits, eq. 3072 bits RSA)) (Client did not present a certificate); Sat, 22 Aug 2020 12:30:49 +0200 (CEST) From: Bruno Haible To: Paul Eggert Subject: Re: Clang __built_assume Date: Sat, 22 Aug 2020 12:30:48 +0200 Message-ID: <4290937.bpigslQC6F@omega> User-Agent: KMail/5.1.3 (Linux/4.4.0-186-generic; KDE/5.18.0; x86_64; ; ) In-Reply-To: References: <5e607e6f-ee3a-41bc-67eb-cdc0571d00d0@cs.ucla.edu> <2538007.71trb2julv@omega> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="UTF-8" Received-SPF: none client-ip=85.215.255.21; envelope-from=bruno@clisp.org; helo=mo4-p00-ob.smtp.rzone.de X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/22 06:30:51 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_PASS=-0.001, SPF_NONE=0.001, URIBL_BLOCKED=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: bug-gnulib@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Gnulib discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: bug-gnulib@gnu.org Errors-To: bug-gnulib-bounces+normalperson=yhbt.net@gnu.org Sender: "bug-gnulib" Paul Eggert wrote: > On 8/17/20 4:37 PM, Bruno Haible wrote: > >> + Avoid Clang=E2=80=99s __builtin_assume, as clang 9.0.1 -Wassume can > >> + generate a bogus diagnostic "the argument to '__builtin_assume' has > >> + side effects that will be discarded" even when the argument has no > >> + side effects. */ > > Do you have a test case, that we could check on clang 10 and on future > > clang versions? >=20 > Here's a short test that elicits the bogus warning for me now. >=20 > static int f (int x) { return x; } > int main (void) { __builtin_assume (f (1)); return 0; } Thanks. I confirm it's still the same with clang 10. But a small modification of the test case produces no warning: static __attribute__ ((__const__)) int f (int x) { return x; } int main (void) { __builtin_assume (f (1)); return 0; } I find it quite natural that * If you want to tell the compiler that it can make assumptions about a function call, the compiler can evaluate the function call at compile-time. If you don't want this, write static int f (int x) { return x; } int main (void) { int r =3D f (1); __builtin_assume (r); return 0; } * You need to mark those functions that the compiler may evaluate at compile-time. * There is a diagnostic if the compiler can't take benefit of the __builtin_assume invocation, although you intended it to have some. So, I don't think the warning is bogus. Back to the verify module. The warning tells us to move the side effect outside of __builtin_assume. If I do this, I get an assume() macro that * produces no warning, * in clang versions < 9, has the desired optimization effect, whereas the current definition doesn't. Test case: =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D #if 1 /* Current definition */ # define assume(R) ((R) ? (void) 0 : __builtin_unreachable ()) #else /* Proposed definition */ # define assume(R) \ ((void) ({ __typeof__ (R) _gl_verify_temp =3D (R); \ __builtin_assume (_gl_verify_temp); })) #endif static int f (int x) { return x; } int main (void) { assume (f (1)); return 0; } int g (int x) { assume (x >=3D 4); return (x > 1 ? x + 3 : 2 * x + 10); } =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D With clang 8 and the current definition: g: # @g leal 3(%rdi), %ecx cmpl $1, %edi leal 10(%rdi,%rdi), %eax cmovgl %ecx, %eax retq With clang 8 and the proposed definition: g: # @g leal 3(%rdi), %eax retq Here's a proposed patch. 2020-08-22 Bruno Haible verify: Do use __built_assume on clang. * lib/verify.h (assume): Use clang=E2=80=99s __builtin_assume, with a temp= orary variable in a statement expression. diff --git a/lib/verify.h b/lib/verify.h index d485a02..0f3c6f9 100644 =2D-- a/lib/verify.h +++ b/lib/verify.h @@ -246,6 +246,13 @@ template =20 /* @assert.h omit start@ */ =20 +#if defined __has_builtin +/* = */ +# define _GL_HAS_BUILTIN_ASSUME __has_builtin (__builtin_assume) +#else +# define _GL_HAS_BUILTIN_ASSUME 0 +#endif + #if 3 < __GNUC__ + (3 < __GNUC_MINOR__ + (4 <=3D __GNUC_PATCHLEVEL__)) # define _GL_HAS_BUILTIN_TRAP 1 #elif defined __has_builtin @@ -305,14 +312,16 @@ template =20 Although assuming R can help a compiler generate better code or diagnostics, performance can suffer if R uses hard-to-optimize =2D features such as function calls not inlined by the compiler. =2D =2D Avoid Clang=E2=80=99s __builtin_assume, as clang 9.0.1 -Wassume can =2D generate a bogus diagnostic "the argument to '__builtin_assume' has =2D side effects that will be discarded" even when the argument has no =2D side effects. */ =2D =2D#if _GL_HAS_BUILTIN_UNREACHABLE + features such as function calls not inlined by the compiler. */ + +#if _GL_HAS_BUILTIN_ASSUME +/* Use a temporary variable, to avoid a clang warning + "the argument to '__builtin_assume' has side effects that will be disca= rded" + if R contains invocations of functions not marked as 'const'. */ +# define assume(R) \ + ((void) ({ __typeof__ (R) _gl_verify_temp =3D (R); \ + __builtin_assume (_gl_verify_temp); })) +#elif _GL_HAS_BUILTIN_UNREACHABLE # define assume(R) ((R) ? (void) 0 : __builtin_unreachable ()) #elif 1200 <=3D _MSC_VER # define assume(R) __assume (R)