public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Marc Glisse <marc.glisse@inria.fr>
To: Richard Biener <richard.guenther@gmail.com>
Cc: "gcc-patches@gcc.gnu.org" <gcc-patches@gcc.gnu.org>,
	    Jeff Law <law@redhat.com>,
	Feng Xue OS <fxue@os.amperecomputing.com>
Subject: Re: [PATCH] Remove empty loop with assumed finiteness (PR tree-optimization/89713)
Date: Mon, 20 May 2019 13:04:00 -0000	[thread overview]
Message-ID: <alpine.DEB.2.21.1905201452240.28567@grove.saclay.inria.fr> (raw)
In-Reply-To: <CAFiYyc3KPF-R-mNSFPt9_0WXTCR-ZoqkjTTq04==SDDu9WF_xw@mail.gmail.com>

On Mon, 20 May 2019, Richard Biener wrote:

> On Sat, May 18, 2019 at 4:00 PM Marc Glisse <marc.glisse@inria.fr> wrote:
>>
>> (@Feng Xue, it is better to include testcases in your patches)
>>
>>>> I'm not a big fan of this patch.  I'd rather be looking at either
>>>> improving our analysis
>>
>> Better analysis cannot hurt.
>>
>>>> or adding attributes to the loops to help the analysis bits prove
>>>> termination.
>>
>> There may not be a loop in the source code, it may be a recursive function
>> call that gcc turned into a loop. Plus, I know that it applies to all
>> loops in my program.
>>
>> We could have a function to compute the length of a linked list:
>> struct A { A*p; };
>> unsigned l(A*a){return a?l(a->p)+1:0;}
>>
>> and because of other optimizations, it turns out that we do not actually
>> use this length
>> void g(A*a){l(a);}
>>
>> wouldn't it be nice if gcc could remove all that useless code, instead of
>> keeping a useless loop on the off chance that it might be infinite?
>>
>>> And we had sth similar in the past and ended up removing it. -funsafe-loop-optimizations IIRC.
>>
>> IIUC that was slightly different: "This option tells the loop optimizer to
>> assume that loop indices do not overflow, and that loops with nontrivial
>> exit condition are not infinite."
>>
>> The assumption on indices looks unsafe indeed if it applied to unsigned
>> indices in non-empty loops.
>
> The question is of couse what a "nontrivial exit condition" is.  Indeed
> the general handling of unsigned wrapping was what made the option
> useless in practice.
>
> But we thoroughly need to specify "nontrivial exit condition", if going
> as far as non-constant exit condition, that is, only do {} while (1) is required
> to be detected as infinite then this breaks down to unsigned wrapping IVs
> not be infinite.  Otherwise it requires the compiler to be able to correctly
> analyze all unsigned IVs which I know we do not at the moment (SCEV
> has limits).

We also want to handle pointer-chasing loops (lists, trees), not 
specifically unsigned IV.

> So - any suggestion as to how define "nontrivial exit condition"?
>
>> But the C++ standard went to the trouble of banning infinite loops without
>> side effects specifically to enable DCE on this type of code... Actually,
>> an infinite loop with a trivial exit condition looks like a great
>> opportunity to use __builtin_unreachable() to me ;-) (I have been wanting
>> a -fmain-does-return -fno-funny-business for years, since I looked at
>> replacing some malloc with stack allocations, but that's all out of scope
>> for this patch)
>>
>> Why exactly are we trying so hard to preserve no-side-effect, infinite
>> loops? What are they good for? Note that reading an atomic or volatile
>> variable counts as a side effect for this purpose. It looks like some kind
>> of busy waiting, but without checking a flag, so it can only be stopped by
>> some external action (say a signal), so if the OS has any notion of sleep
>> for a thread, blocking would be better. Or maybe it is running through a
>> circular list, ensuring that it stays in RAM? Anyway it seems specific
>> enough that that strange code should be the one needing an annotation.
>
> I guess we preserve them because we have to?
>
> I suppose we could add a flag that allows us to elide
> loops with no side-effect and non-constant exit condition
> (so only preserve do{}while (1))?

The C++ standard says that do{}while(1) is __builtin_unreachable(), we 
don't have to preserve it. There is no mention of anything like a 
"nontrivial exit condition". Other languages may have a different opinion 
though, so it would probably need a flag indeed... But I am curious what 
the point of such a loop is.

-- 
Marc Glisse

  parent reply	other threads:[~2019-05-20 13:04 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-17  4:17 Feng Xue OS
2019-05-17 16:47 ` Jeff Law
2019-05-17 18:50   ` Richard Biener
2019-05-18 14:00     ` Marc Glisse
2019-05-20  7:50       ` Richard Biener
2019-05-20  8:27         ` Feng Xue OS
2019-05-20  9:19           ` Richard Biener
2019-05-20  9:48             ` Feng Xue OS
2019-05-20 11:54               ` Richard Biener
2019-05-20 14:00                 ` Feng Xue OS
2019-05-20 14:04                   ` Richard Biener
2019-05-20 14:51                     ` Feng Xue OS
2019-05-21 10:12                       ` Richard Biener
2019-05-21 14:24                         ` Richard Biener
2019-05-22 13:44                           ` Michael Matz
2019-05-24 16:02                             ` [PATCH V3] " Feng Xue OS
2019-05-24  9:15                           ` [PATCH V2] " Feng Xue OS
2019-05-29 11:16                             ` Richard Biener
2019-06-04  6:49                               ` [PATCH V4] " Feng Xue OS
2019-06-04  8:24                                 ` Marc Glisse
2019-06-04 15:16                                   ` [PATCH V5] " Feng Xue OS
2019-06-04 15:24                                     ` [PATCH V6] " Feng Xue OS
2019-06-05 11:05                                       ` Richard Biener
2019-06-06 10:00                                         ` [PATCH V7] " Feng Xue OS
2019-06-11  2:40                                           ` [PATCH V8] " Feng Xue OS
2019-06-12  9:43                                             ` Richard Biener
2019-06-15 12:05                                               ` [committed][nvptx, libgomp] Update pr85381-{2,4}.c test-cases Tom de Vries
2019-05-20 13:04         ` Marc Glisse [this message]
2019-05-20 13:26           ` [PATCH] Remove empty loop with assumed finiteness (PR tree-optimization/89713) Richard Biener
2019-05-20 14:49             ` Michael Matz
2019-05-21  8:06               ` Marc Glisse
2020-04-01 13:36 ` [PATCH][RFC] c/94392 - only enable -ffinite-loops for C++ Richard Biener
2020-04-01 13:47   ` Jakub Jelinek
2020-04-01 13:52     ` Richard Biener
2020-04-01 15:56       ` Jan Hubicka
2020-04-01 16:59         ` Richard Biener
2020-04-01 19:15   ` Jason Merrill
2020-04-02  9:12     ` Richard Biener
2020-04-02  9:17       ` Jakub Jelinek
2020-04-02  9:41         ` Richard Biener
2020-04-03  8:29       ` Revert "[nvptx, libgomp] Update pr85381-{2, 4}.c test-cases" [PR89713, PR94392] (was: [PATCH][RFC] c/94392 - only enable -ffinite-loops for C++) Thomas Schwinge
2020-04-03  9:36         ` Revert "[nvptx, libgomp] Update pr85381-{2,4}.c " Richard Biener
2020-04-03 10:34           ` Jakub Jelinek
2020-10-30 14:09           ` Revert "[nvptx, libgomp] Update pr85381-{2, 4}.c " Thomas Schwinge
2020-10-30 14:16             ` Revert "[nvptx, libgomp] Update pr85381-{2,4}.c " Jakub Jelinek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.DEB.2.21.1905201452240.28567@grove.saclay.inria.fr \
    --to=marc.glisse@inria.fr \
    --cc=fxue@os.amperecomputing.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=law@redhat.com \
    --cc=richard.guenther@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).