From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by sourceware.org (Postfix) with ESMTPS id E36C3386F022 for ; Tue, 12 Jan 2021 14:05:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org E36C3386F022 Received: by mail-wr1-x42b.google.com with SMTP id t16so2641516wra.3 for ; Tue, 12 Jan 2021 06:05:42 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=z0OoK8VW5OdtJw+lFWtSrjvg6v2l9D0+W8F8xS2qtSo=; b=r8snyrI0NR4bbTUzMX1QcUbrWu3bFyM3eWSc56xbX8hd7c4B5OkdOlHdSx0EnqG6hv OcyokyFePsunDHRE/+Mr4DfBStiICi/pg5RvjaJTVPeCr/umU6URL60I2eH1jE5ZxvoG GmI5+jyOqHahQoEVarciZoMCkfxf5iEPb1I8gb5e6FBTpXaKgf1I8cxq6andxgDm81e9 fQHd2KQ4dVnVHc4xcBK1FnAUVkabF02eqrJM/k1JBxt5aav+ur1wAobXCTtZAfj0SCle HXULn5EU9GlBQhAcram1blE7gkDV/IGxScy/KnSAzRUPejT6LkhNOugAPCkZYsndpdfF X70A== X-Gm-Message-State: AOAM5330IOn1xcfPBaISGrvtIcoDOIh4F7s+BBjTbfT2O9Z4+6+koxFj TeKnWEPNzxwfYo/JVP1OTkuxjLCO8IMFDvPiQzE= X-Google-Smtp-Source: ABdhPJz4mnqgty5F67wxIgW0WCToAwHk8RddC62LPgtVKVkrLD0pplfGhipwBpYQbCmVZEetQ/88N5iy3zEfR5QavL8= X-Received: by 2002:adf:a319:: with SMTP id c25mr4626035wrb.262.1610460342087; Tue, 12 Jan 2021 06:05:42 -0800 (PST) MIME-Version: 1.0 References: <8735z6i8fn.fsf@oldenburg2.str.redhat.com> In-Reply-To: <8735z6i8fn.fsf@oldenburg2.str.redhat.com> From: =?UTF-8?B?4piCSm9zaCBDaGlhICjorJ3ku7vkuK0p?= Date: Tue, 12 Jan 2021 22:05:31 +0800 Message-ID: Subject: Re: Failure to optimize? To: Florian Weimer Cc: =?UTF-8?B?4piCSm9zaCBDaGlhICjorJ3ku7vkuK0pIHZpYSBHY2MtaGVscA==?= X-Spam-Status: No, score=-0.5 required=5.0 tests=BAYES_00, BODY_8BITS, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, HTML_MESSAGE, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: gcc-help@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-help mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 12 Jan 2021 14:05:44 -0000 I didn't mention it earlier, but I'd like to clarify that this is for "g++ -O3 -std=3Dc++17" on GCC 10.2. If I use "static thread_local int cursor =3D 0;" then cursor is thread-loca= l and such a race should be impossible, but the generated code for bar1() still has a branch. The branch persists also if I do this instead for bar1(): char const* bar1() { char const* result =3D foo(); if (result) cursor +=3D 1; else cursor +=3D 0; return result; } bar1(): sub rsp, 8 call foo() test rax, rax je .L1 add DWORD PTR fs:cursor@tpoff, 1 .L1: add rsp, 8 ret On Tue, Jan 12, 2021 at 9:36 PM Florian Weimer wrote: > * =E2=98=82Josh Chia (=E8=AC=9D=E4=BB=BB=E4=B8=AD) via Gcc-help: > > > I have a code snippet that I'm wondering why GCC didn't optimize the wa= y > I > > think it should: > > https://godbolt.org/z/1qKvax > > > > bar2() is a variant of bar1() that has been manually tweaked to avoid > > branches. I haven't done any benchmarks but, I would expect the > branchless > > bar2() to perform better than bar1() but GCC does not automatically > > optimize bar1() to be like bar2(); the generated code for bar1() and > bar2() > > are different and the generated code for bar1() contains a branch. > > The optimization is probably valid for C99, but not for C11, where the > memory model prevents the compiler from introducing spurious writes: > Another thread may modify the variable concurrently, and if this happens > only if foo returns NULL, the original bar1 function does not contain a > data race, but the branchless version would. > > Thanks, > Florian > -- > Red Hat GmbH, https://de.redhat.com/ , Registered seat: Grasbrunn, > Commercial register: Amtsgericht Muenchen, HRB 153243, > Managing Directors: Charles Cachera, Brian Klemm, Laurie Krebs, Michael > O'Neill > >