public inbox for
 help / color / mirror / Atom feed
From: Aldy Hernandez <>
To: Richard Biener <>,
	Jakub Jelinek <>
Cc: Joseph Myers <>,
	GCC patches <>,
	Andrew MacLeod <>
Subject: Re: [PATCH] [range-ops] Implement sqrt.
Date: Fri, 18 Nov 2022 11:37:42 +0100	[thread overview]
Message-ID: <> (raw)
In-Reply-To: <>

On 11/18/22 09:39, Richard Biener wrote:
> On Thu, Nov 17, 2022 at 8:38 PM Jakub Jelinek via Gcc-patches
> <> wrote:
>> On Thu, Nov 17, 2022 at 06:59:45PM +0000, Joseph Myers wrote:
>>> On Thu, 17 Nov 2022, Aldy Hernandez via Gcc-patches wrote:
>>>> So... is the optimization wrong?  Are we not allowed to substitute
>>>> that NAN if we know it's gonna happen?  Should we also allow F F F F F
>>>> in the test?  Or something else?
>>> This seems like the usual ambiguity about what transformations
>>> -ftrapping-math (on by default) is meant to prevent.
>>> Generally it's understood to prevent transformations that add *or remove*
>>> exceptions, so folding a case that raises "invalid" to a NaN (with
>>> "invalid" no longer raised) is invalid with -ftrapping-math.  But that
>>> doesn't tend to be applied if the operation raising the exceptions has a
>>> result that is otherwise unused - in such a case the operation may still
>>> be removed completely (the exception isn't properly treated as a side
>>> effect to avoid dead code elimination; cf. Marc Glisse's -ffenv-access
>>> patches from August 2020).  And it may often also not be applied to
>>> "inexact".
>> The problem is that the above model I'm afraid is largely incompatible with
>> the optimizations ranger provides.
>> A strict model where no operations that could raise exceptions are discarded
>> is easy, we let frange optimize as much as it wants and just tell DCE not to
>> eliminate operations that can raise exceptions.
>> But in the model where some exceptions can be discarded if results are unused
>> but not others where they are used, there is no way to distinguish between
>> the result of the operation really isn't needed and ranger figured out a
>> result (or usable range of something) and therefore the result of the
>> operation isn't needed.
>> Making frange more limited with -ftrapping-math, making it punt for
>> operations that could raise an exception would be quite drastic
>> pessimization.  Perhaps for -ftrapping-math we could say no frange value is
>> singleton and so at least for most of operations we actually wouldn't
>> optimize out the whole computation when we know the result?  Still, we could
>> also just have
>> r = long_computation (x, y, z);
>> if (r > 42.0)
>> and if frange figures out that r must be [256.0, 1024.0] and never NAN, we'd
>> still happily optimize away the comparison.
> Yes, I don't think singling out the singleton case will help.

There is also simplify_using_ranges::fold_cond() which is used by VRP 
and DOM to fold conditionals.  So twiddling frange::singleton_p will 
have no effect here since FP conditionals results are integers (f > 3.0 
is true or false).

And now that we're on this subject...

We are very careful in frange (range-op-floats.o) to avoid returning 
true/false in relational which may have a NAN.  This keeps us from 
folding conditionals that may result in a trapping NAN.

For example, if we see [if (x_5 unord_lt 10.0)...] and we know x_5 is 
[-INF, -8.0] +-NAN, this conditional is always true, but we return 
VARYING to avoid folding a NAN producing conditional.  I wonder whether 
we're being too conservative?

An alternative woudld be:
	z_8 = x_5 unord_lt 10.0
	goto true_side;

But if DCE is going to clean that up anyhow without regards to 
exceptions, then maybe we can fold these conditionals altogether?  If 
not in this release, then in the next one.

ISTM that range-ops should always tell the truth of what it knows, 
instead of being conservative wrt exceptions.  It should be up to the 
clients (VRP or simplify_using_ranges::fold_cond) to use the information 

> Practically strictly
> preserving IEEE exceptions is only important for a very small audience, and
> for that even INEXACT will matter (but we still have -ftrapping-math
> by default).
> For that audience likely all constant / range propagation is futile and thus the
> easiest thing might be to simply cut that off completely?
> I'd say what ranger does is reasonable with -ftrapping-math given the current
> practice of handling this option.  There's no point in trying to preserve the
> (by accident) "better" handling without ranger.  Instead as Joseph says somebody
> would need to sit down, split -ftrapping-math, adjust the default and thorougly
> document things (also with -fnon-call-exceptions which magically makes
> IEEE flag raising operations possibly throw exceptions).  As there's currently
> no code motion barriers for FP code with respect to exception flag inspection
> any dead code we preserve is likely going to be unhelpful.
> So for now simply amend the documentation as to what -ftrapping-math
> currently means with respect to range/constant propagation?

So something like "Even in the presence of -ftrapping-math, VRP may fold 
operations that may cause exceptions  For example, an addition that is 
guaranteed to produce a NAN, may be replaced with a NAN, thus eliding 
the addition.  This may cause any exception that may have been generated 
by the addition to not appear in the final program."



  reply	other threads:[~2022-11-18 10:37 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-13 20:05 Aldy Hernandez
2022-11-13 20:39 ` Jakub Jelinek
2022-11-14  7:45   ` Aldy Hernandez
2022-11-14 14:30     ` Jeff Law
2022-11-14 14:35       ` Jakub Jelinek
2022-11-14 14:48         ` Jeff Law
2022-11-14 15:01         ` Aldy Hernandez
2022-11-14 21:55   ` Joseph Myers
2022-11-16 20:32     ` Jakub Jelinek
2022-11-17 16:40       ` Aldy Hernandez
2022-11-17 16:48         ` Aldy Hernandez
2022-11-17 17:42           ` Aldy Hernandez
2022-11-17 18:59         ` Joseph Myers
2022-11-17 19:37           ` Jakub Jelinek
2022-11-17 20:43             ` Joseph Myers
2022-11-18  8:39             ` Richard Biener
2022-11-18 10:37               ` Aldy Hernandez [this message]
2022-11-18 10:44                 ` Jakub Jelinek
2022-11-18 11:20                   ` Aldy Hernandez
2022-11-18 11:57                     ` Aldy Hernandez
2022-11-18 12:14                   ` Richard Biener

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:

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

  git send-email \ \ \ \ \ \ \ \

* 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).