Re: [PATCH] Add support for floating point endpoints to frange.
On 8/29/2022 8:30 AM, Aldy Hernandez via Gcc-patches wrote: On Mon, Aug 29, 2022 at 4:27 PM Jakub Jelinek wrote: On Mon, Aug 29, 2022 at 04:20:16PM +0200, Aldy Hernandez wrote: Sure, I can add the HONOR_NANS, but can we even "see" a NAN in the IL for -ffinite-math-only? Sure, you can, e.g. __builtin_nan{,s}{,f,l} etc. would do it. It would be UB to use it at runtime in -ffinite-math-only code though. Another question is, when making a range VARYING, do you set the NAN property or not when !HONOR_NANS && MODE_HAS_NANS? A range of VARYING sets the NAN property to unknown (fp_prop::VARYING). If you prefer we can set the property to fp_prop::NO for !HONOR_NANS && MODE_HAS_NANS. If the format doesn't have NaNs or the user explicitly disables them, then the state should be NO, otherwise YES. Jeff
Re: [PATCH] Add support for floating point endpoints to frange.
On 8/29/2022 8:42 AM, Toon Moene wrote: On 8/29/22 16:36, Aldy Hernandez wrote: On Mon, Aug 29, 2022 at 4:30 PM Toon Moene wrote: On 8/29/22 16:15, Aldy Hernandez wrote: But even with -ffinite-math-only, is there any benefit to propagating a known NAN? For example: The original intent (in 2002) for the option -ffinite-math-only was for the optimizers to ignore all the various exceptions to common optimizations because they might not work correctly when presented with a NaN or an Inf. I do not know what the effect for floating point range information would be - offhand. But in the *spirit* of this option would be to ignore that the range [5.0, 5.0] would "also" contain NaN, for instance. Hmm, this is somewhat similar to what Jakub suggested. Perhaps we could categorically set !NAN for !HONOR_NANS at frange construction time? For reference: bool HONOR_NANS (machine_mode m) { return MODE_HAS_NANS (m) && !flag_finite_math_only; } Thanks. Aldy Yep, I think that would do it. Agreed. Jeff
Re: [PATCH] Add support for floating point endpoints to frange.
On 8/29/2022 8:15 AM, Aldy Hernandez via Gcc-patches wrote: On Mon, Aug 29, 2022 at 4:08 PM Toon Moene wrote: On 8/29/22 15:54, Jakub Jelinek via Gcc-patches wrote: On Mon, Aug 29, 2022 at 03:45:33PM +0200, Aldy Hernandez wrote: For convenience, singleton_p() returns false for a NAN. IMO, it makes the implementation cleaner, but I'm not wed to the idea if someone objects. If singleton_p() is used to decide whether one can just replace a variable with singleton range with a constant, then certainly. If MODE_HAS_SIGNED_ZEROS, zero has 2 representations (-0.0 and 0.0) and NaNs have lots of different representations (the sign bit is ignored except for stuff like copysign/signbit, there are qNaNs and sNaNs and except for the single case how Inf is represented, all other values of the mantissa mean different representations of NaN). So, unless we track which exact form of NaN can appear, NaN or any [x, x] range with NaN property set can't be a singleton. There could be programs that propagate something important in NaN mantissa and would be upset if frange kills that. Of course, one needs to take into account that when a FPU creates NaN, it will create the canonical qNaN. Jakub But the NaNs are irrelevant with -ffinite-math-only, as are the various Infs (I do not know offhand which MODE_ that is) ... But even with -ffinite-math-only, is there any benefit to propagating a known NAN? For example: In theory, yes, but I don't know if it's worth the headache in practice for NaNs, particularly given they can have many representations. Jeff
Re: [PATCH] Add support for floating point endpoints to frange.
On 8/29/2022 7:54 AM, Jakub Jelinek via Gcc-patches wrote: On Mon, Aug 29, 2022 at 03:45:33PM +0200, Aldy Hernandez wrote: For convenience, singleton_p() returns false for a NAN. IMO, it makes the implementation cleaner, but I'm not wed to the idea if someone objects. If singleton_p() is used to decide whether one can just replace a variable with singleton range with a constant, then certainly. If MODE_HAS_SIGNED_ZEROS, zero has 2 representations (-0.0 and 0.0) and NaNs have lots of different representations (the sign bit is ignored except for stuff like copysign/signbit, there are qNaNs and sNaNs and except for the single case how Inf is represented, all other values of the mantissa mean different representations of NaN). So, unless we track which exact form of NaN can appear, NaN or any [x, x] range with NaN property set can't be a singleton. There could be programs that propagate something important in NaN mantissa and would be upset if frange kills that. Of course, one needs to take into account that when a FPU creates NaN, it will create the canonical qNaN. I've always thought of singleton_p as having that purpose -- but in the integer world we never really had to think about multiple representations of the same value. So it's entirely possible we're using singleton_p for multiple purposes. Clearly if the representation has multiple representations for the same value, then we have to be more careful with propagation. So we may need to separate the concept of "this has a value we can propagate" from "this has a constant value, but the value may have multiple represenatations". I don't think it's worth trying to track the various NaN representations, but I do think it's worth tracking +-Inf and +-0.0. jeff
Re: [PATCH] Add support for floating point endpoints to frange.
On Mon, 29 Aug 2022, Jakub Jelinek via Gcc-patches wrote: > On Mon, Aug 29, 2022 at 03:45:33PM +0200, Aldy Hernandez wrote: > > For convenience, singleton_p() returns false for a NAN. IMO, it makes > > the implementation cleaner, but I'm not wed to the idea if someone > > objects. > > If singleton_p() is used to decide whether one can just replace a variable > with singleton range with a constant, then certainly. > If MODE_HAS_SIGNED_ZEROS, zero has 2 representations (-0.0 and 0.0) and Note also for decimal floating point that many real numbers - if they don't require the full precision of the type to represent the number - can be represented with different quantum exponents (so compare equal but can't be used to replace each other). -- Joseph S. Myers jos...@codesourcery.com
Re: [PATCH] Add support for floating point endpoints to frange.
OK, I'm good to go. As the patch was getting rather large, I have split it into two parts. The first is the core endpoints support to frange along with removal of the +-INF markers (since they are no longer needed). The second part is the FP relational operators. Splitting it up should help in reviewing/improving the code for readers. Also, it always helps regression hunting to have smaller pieces ;-). Retested on x86-64 Linux. Pushed. On Mon, Aug 29, 2022 at 4:42 PM Toon Moene wrote: > > On 8/29/22 16:36, Aldy Hernandez wrote: > > > On Mon, Aug 29, 2022 at 4:30 PM Toon Moene wrote: > >> > >> On 8/29/22 16:15, Aldy Hernandez wrote: > >> > >>> But even with -ffinite-math-only, is there any benefit to propagating > >>> a known NAN? For example: > >> > >> The original intent (in 2002) for the option -ffinite-math-only was for > >> the optimizers to ignore all the various exceptions to common > >> optimizations because they might not work correctly when presented with > >> a NaN or an Inf. > >> > >> I do not know what the effect for floating point range information would > >> be - offhand. > >> > >> But in the *spirit* of this option would be to ignore that the range > >> [5.0, 5.0] would "also" contain NaN, for instance. > > > > Hmm, this is somewhat similar to what Jakub suggested. Perhaps we > > could categorically set !NAN for !HONOR_NANS at frange construction > > time? > > > > For reference: > > bool > > HONOR_NANS (machine_mode m) > > { > >return MODE_HAS_NANS (m) && !flag_finite_math_only; > > } > > > > Thanks. > > Aldy > > > > Yep, I think that would do it. > > Thanks, > > -- > Toon Moene - e-mail: t...@moene.org - phone: +31 346 214290 > Saturnushof 14, 3738 XG Maartensdijk, The Netherlands > From 7ded1c025a44d9e3e141bd1c7694aef3931acb69 Mon Sep 17 00:00:00 2001 From: Aldy Hernandez Date: Tue, 30 Aug 2022 08:23:33 +0200 Subject: [PATCH] Add support for floating point endpoints to frange. The current implementation of frange is just a type with some bits to represent NAN and INF. We can do better and represent endpoints to ultimately solve longstanding PRs such as PR24021. This patch adds these endpoints. In follow-up patches I will add support for a bare bones PLUS_EXPR range-op-float entry to solve the PR. I have chosen to use REAL_VALUE_TYPEs for the endpoints, since that's what we use underneath the trees. This will be somewhat analogous to our eventual use of wide-ints in the irange. No sense going through added levels of indirection if we can avoid it. That, plus real.* already has a nice API for dealing with floats. With this patch, ranges will be closed float point intervals, which make the implementation simpler, since we don't have to keep track of open/closed intervals. This is conservative enough for use in the ranger world, as we'd rather err on the side of more elements in a range, than less. For example, even though we cannot precisely represent the open interval (3.0, 5.0) with this approach, it is perfectably reasonable to represent it as [3.0, 5.0] since the closed interval is a super set of the open one. In the VRP/ranger world, it is always better to err on the side of more information in a range, than not. After all, when we don't know anything about a range, we just use VARYING which is a fancy term for a range spanning the entire domain. Since REAL_VALUE_TYPEs have properly defined infinity and NAN semantics, all the math can be made to work: [-INF, 3.0] !NAN=> Numbers <= 3.0 (NAN cannot happen) [3.0, 3.0] => 3.0 or NAN. [3.0, +INF] => Numbers >= 3.0 (NAN is possible) [-INF, +INF]=> VARYING (NAN is possible) [-INF, +INF] !NAN => Entire domain. NAN cannot happen. Also, since REAL_VALUE_TYPEs can represent the minimum and maximum representable values of a TYPE_MODE, we can disambiguate between them and negative and positive infinity (see get_max_float in real.cc). This also makes the math all work. For example, suppose we know nothing about x and y (VARYING). On the TRUE side of x > y, we can deduce that: (a) x cannot be NAN (b) y cannot be NAN (c) y cannot be +INF. (c) means that we can drop the upper bound of "y" from +INF to the maximum representable value for its type. Having endpoints with different representation for infinity and the maximum representable values, means we can drop the +-INF properties we currently have in the frange. gcc/ChangeLog: * range-op-float.cc (frange_set_nan): New. (frange_drop_inf): New. (frange_drop_ninf): New. (foperator_equal::op1_range): Adjust for endpoints. (foperator_lt::op1_range): Same. (foperato
Re: [PATCH] Add support for floating point endpoints to frange.
On 8/29/22 16:36, Aldy Hernandez wrote: On Mon, Aug 29, 2022 at 4:30 PM Toon Moene wrote: On 8/29/22 16:15, Aldy Hernandez wrote: But even with -ffinite-math-only, is there any benefit to propagating a known NAN? For example: The original intent (in 2002) for the option -ffinite-math-only was for the optimizers to ignore all the various exceptions to common optimizations because they might not work correctly when presented with a NaN or an Inf. I do not know what the effect for floating point range information would be - offhand. But in the *spirit* of this option would be to ignore that the range [5.0, 5.0] would "also" contain NaN, for instance. Hmm, this is somewhat similar to what Jakub suggested. Perhaps we could categorically set !NAN for !HONOR_NANS at frange construction time? For reference: bool HONOR_NANS (machine_mode m) { return MODE_HAS_NANS (m) && !flag_finite_math_only; } Thanks. Aldy Yep, I think that would do it. Thanks, -- Toon Moene - e-mail: t...@moene.org - phone: +31 346 214290 Saturnushof 14, 3738 XG Maartensdijk, The Netherlands
Re: [PATCH] Add support for floating point endpoints to frange.
On Mon, Aug 29, 2022 at 4:30 PM Toon Moene wrote: > > On 8/29/22 16:15, Aldy Hernandez wrote: > > > But even with -ffinite-math-only, is there any benefit to propagating > > a known NAN? For example: > > The original intent (in 2002) for the option -ffinite-math-only was for > the optimizers to ignore all the various exceptions to common > optimizations because they might not work correctly when presented with > a NaN or an Inf. > > I do not know what the effect for floating point range information would > be - offhand. > > But in the *spirit* of this option would be to ignore that the range > [5.0, 5.0] would "also" contain NaN, for instance. Hmm, this is somewhat similar to what Jakub suggested. Perhaps we could categorically set !NAN for !HONOR_NANS at frange construction time? For reference: bool HONOR_NANS (machine_mode m) { return MODE_HAS_NANS (m) && !flag_finite_math_only; } Thanks. Aldy
Re: [PATCH] Add support for floating point endpoints to frange.
On Mon, Aug 29, 2022 at 4:27 PM Jakub Jelinek wrote: > > On Mon, Aug 29, 2022 at 04:20:16PM +0200, Aldy Hernandez wrote: > > Sure, I can add the HONOR_NANS, but can we even "see" a NAN in the IL > > for -ffinite-math-only? > > Sure, you can, e.g. __builtin_nan{,s}{,f,l} etc. would do it. > It would be UB to use it at runtime in -ffinite-math-only code though. > Another question is, when making a range VARYING, do you set the NAN > property or not when !HONOR_NANS && MODE_HAS_NANS? A range of VARYING sets the NAN property to unknown (fp_prop::VARYING). If you prefer we can set the property to fp_prop::NO for !HONOR_NANS && MODE_HAS_NANS. ?? Aldy
Re: [PATCH] Add support for floating point endpoints to frange.
On 8/29/22 16:15, Aldy Hernandez wrote: But even with -ffinite-math-only, is there any benefit to propagating a known NAN? For example: The original intent (in 2002) for the option -ffinite-math-only was for the optimizers to ignore all the various exceptions to common optimizations because they might not work correctly when presented with a NaN or an Inf. I do not know what the effect for floating point range information would be - offhand. But in the *spirit* of this option would be to ignore that the range [5.0, 5.0] would "also" contain NaN, for instance. Kind regards, -- Toon Moene - e-mail: t...@moene.org - phone: +31 346 214290 Saturnushof 14, 3738 XG Maartensdijk, The Netherlands
Re: [PATCH] Add support for floating point endpoints to frange.
On Mon, Aug 29, 2022 at 04:20:16PM +0200, Aldy Hernandez wrote: > Sure, I can add the HONOR_NANS, but can we even "see" a NAN in the IL > for -ffinite-math-only? Sure, you can, e.g. __builtin_nan{,s}{,f,l} etc. would do it. It would be UB to use it at runtime in -ffinite-math-only code though. Another question is, when making a range VARYING, do you set the NAN property or not when !HONOR_NANS && MODE_HAS_NANS? > I suppose it's cleaner with HONOR_NANS Jakub
Re: [PATCH] Add support for floating point endpoints to frange.
On Mon, Aug 29, 2022 at 4:17 PM Jakub Jelinek wrote: > > On Mon, Aug 29, 2022 at 04:08:58PM +0200, Aldy Hernandez wrote: > > On Mon, Aug 29, 2022 at 3:55 PM Jakub Jelinek wrote: > > > > > > On Mon, Aug 29, 2022 at 03:45:33PM +0200, Aldy Hernandez wrote: > > > > For convenience, singleton_p() returns false for a NAN. IMO, it makes > > > > the implementation cleaner, but I'm not wed to the idea if someone > > > > objects. > > > > > > If singleton_p() is used to decide whether one can just replace a variable > > > with singleton range with a constant, then certainly. > > > If MODE_HAS_SIGNED_ZEROS, zero has 2 representations (-0.0 and 0.0) and > > > NaNs have lots of different representations (the sign bit is ignored > > > except for stuff like copysign/signbit, there are qNaNs and sNaNs and > > > except for the single case how Inf is represented, all other values of the > > > mantissa mean different representations of NaN). So, unless we track > > > which > > > exact form of NaN can appear, NaN or any [x, x] range with NaN property > > > > Ok that was more or less what I was thinking. And no, we don't keep > > track of the type of NANs. > > > > How does this look? > > > > bool > > frange::singleton_p (tree *result) const > > { > > if (m_kind == VR_RANGE && real_identical (_min, _max)) > > { > > // If we're honoring signed zeros, fail because we don't know > > // which zero we have. This avoids propagating the wrong zero. > > if (HONOR_SIGNED_ZEROS (m_type) && zero_p ()) > > return false; > > > > // Return false for any singleton that may be a NAN. > > if (!get_nan ().no_p ()) > > return false; > > Perhaps if (HONOR_NANS (m_type) && !get_nan ().no_p ()) instead? > Or do you ensure the nan property is never set for -ffinite-math-only? See followup with Tom downthread. Sure, I can add the HONOR_NANS, but can we even "see" a NAN in the IL for -ffinite-math-only? I suppose it's cleaner with HONOR_NANS Aldy
Re: [PATCH] Add support for floating point endpoints to frange.
On Mon, Aug 29, 2022 at 04:08:58PM +0200, Aldy Hernandez wrote: > On Mon, Aug 29, 2022 at 3:55 PM Jakub Jelinek wrote: > > > > On Mon, Aug 29, 2022 at 03:45:33PM +0200, Aldy Hernandez wrote: > > > For convenience, singleton_p() returns false for a NAN. IMO, it makes > > > the implementation cleaner, but I'm not wed to the idea if someone > > > objects. > > > > If singleton_p() is used to decide whether one can just replace a variable > > with singleton range with a constant, then certainly. > > If MODE_HAS_SIGNED_ZEROS, zero has 2 representations (-0.0 and 0.0) and > > NaNs have lots of different representations (the sign bit is ignored > > except for stuff like copysign/signbit, there are qNaNs and sNaNs and > > except for the single case how Inf is represented, all other values of the > > mantissa mean different representations of NaN). So, unless we track which > > exact form of NaN can appear, NaN or any [x, x] range with NaN property > > Ok that was more or less what I was thinking. And no, we don't keep > track of the type of NANs. > > How does this look? > > bool > frange::singleton_p (tree *result) const > { > if (m_kind == VR_RANGE && real_identical (_min, _max)) > { > // If we're honoring signed zeros, fail because we don't know > // which zero we have. This avoids propagating the wrong zero. > if (HONOR_SIGNED_ZEROS (m_type) && zero_p ()) > return false; > > // Return false for any singleton that may be a NAN. > if (!get_nan ().no_p ()) > return false; Perhaps if (HONOR_NANS (m_type) && !get_nan ().no_p ()) instead? Or do you ensure the nan property is never set for -ffinite-math-only? > > if (result) > *result = build_real (m_type, m_min); > return true; > } > return false; > } Otherwise LGTM. Jakub
Re: [PATCH] Add support for floating point endpoints to frange.
On Mon, Aug 29, 2022 at 4:08 PM Toon Moene wrote: > > On 8/29/22 15:54, Jakub Jelinek via Gcc-patches wrote: > > > On Mon, Aug 29, 2022 at 03:45:33PM +0200, Aldy Hernandez wrote: > > >> For convenience, singleton_p() returns false for a NAN. IMO, it makes > >> the implementation cleaner, but I'm not wed to the idea if someone > >> objects. > > > > If singleton_p() is used to decide whether one can just replace a variable > > with singleton range with a constant, then certainly. > > If MODE_HAS_SIGNED_ZEROS, zero has 2 representations (-0.0 and 0.0) and > > NaNs have lots of different representations (the sign bit is ignored > > except for stuff like copysign/signbit, there are qNaNs and sNaNs and > > except for the single case how Inf is represented, all other values of the > > mantissa mean different representations of NaN). So, unless we track which > > exact form of NaN can appear, NaN or any [x, x] range with NaN property > > set can't be a singleton. There could be programs that propagate something > > important in NaN mantissa and would be upset if frange kills that. > > Of course, one needs to take into account that when a FPU creates NaN, it > > will create the canonical qNaN. > > > > Jakub > > > > But the NaNs are irrelevant with -ffinite-math-only, as are the various > Infs (I do not know offhand which MODE_ that is) ... But even with -ffinite-math-only, is there any benefit to propagating a known NAN? For example: if (__builtin_isnan (x)) { use(x); } or perhaps: if (x != x) { use(x); } Should we propagate NAN into the use of x? For that matter, AFAICT we don't even generate the unordered comparisons needed to distinguish a NAN for -ffinite-math-only. void foo(float f) { if (__builtin_isnan (f)) bark(); } $ cat a.c.*original ;; Function foo (null) ;; enabled by -tree-original { if (0) { bark (); } } Cheers. Aldy
Re: [PATCH] Add support for floating point endpoints to frange.
On Mon, Aug 29, 2022 at 3:55 PM Jakub Jelinek wrote: > > On Mon, Aug 29, 2022 at 03:45:33PM +0200, Aldy Hernandez wrote: > > For convenience, singleton_p() returns false for a NAN. IMO, it makes > > the implementation cleaner, but I'm not wed to the idea if someone > > objects. > > If singleton_p() is used to decide whether one can just replace a variable > with singleton range with a constant, then certainly. > If MODE_HAS_SIGNED_ZEROS, zero has 2 representations (-0.0 and 0.0) and > NaNs have lots of different representations (the sign bit is ignored > except for stuff like copysign/signbit, there are qNaNs and sNaNs and > except for the single case how Inf is represented, all other values of the > mantissa mean different representations of NaN). So, unless we track which > exact form of NaN can appear, NaN or any [x, x] range with NaN property Ok that was more or less what I was thinking. And no, we don't keep track of the type of NANs. How does this look? bool frange::singleton_p (tree *result) const { if (m_kind == VR_RANGE && real_identical (_min, _max)) { // If we're honoring signed zeros, fail because we don't know // which zero we have. This avoids propagating the wrong zero. if (HONOR_SIGNED_ZEROS (m_type) && zero_p ()) return false; // Return false for any singleton that may be a NAN. if (!get_nan ().no_p ()) return false; if (result) *result = build_real (m_type, m_min); return true; } return false; } Thanks. Aldy > set can't be a singleton. There could be programs that propagate something > important in NaN mantissa and would be upset if frange kills that. > Of course, one needs to take into account that when a FPU creates NaN, it > will create the canonical qNaN. > > Jakub >
Re: [PATCH] Add support for floating point endpoints to frange.
On 8/29/22 15:54, Jakub Jelinek via Gcc-patches wrote: On Mon, Aug 29, 2022 at 03:45:33PM +0200, Aldy Hernandez wrote: For convenience, singleton_p() returns false for a NAN. IMO, it makes the implementation cleaner, but I'm not wed to the idea if someone objects. If singleton_p() is used to decide whether one can just replace a variable with singleton range with a constant, then certainly. If MODE_HAS_SIGNED_ZEROS, zero has 2 representations (-0.0 and 0.0) and NaNs have lots of different representations (the sign bit is ignored except for stuff like copysign/signbit, there are qNaNs and sNaNs and except for the single case how Inf is represented, all other values of the mantissa mean different representations of NaN). So, unless we track which exact form of NaN can appear, NaN or any [x, x] range with NaN property set can't be a singleton. There could be programs that propagate something important in NaN mantissa and would be upset if frange kills that. Of course, one needs to take into account that when a FPU creates NaN, it will create the canonical qNaN. Jakub But the NaNs are irrelevant with -ffinite-math-only, as are the various Infs (I do not know offhand which MODE_ that is) ... Kind regards, -- Toon Moene - e-mail: t...@moene.org - phone: +31 346 214290 Saturnushof 14, 3738 XG Maartensdijk, The Netherlands
Re: [PATCH] Add support for floating point endpoints to frange.
On Mon, Aug 29, 2022 at 03:45:33PM +0200, Aldy Hernandez wrote: > For convenience, singleton_p() returns false for a NAN. IMO, it makes > the implementation cleaner, but I'm not wed to the idea if someone > objects. If singleton_p() is used to decide whether one can just replace a variable with singleton range with a constant, then certainly. If MODE_HAS_SIGNED_ZEROS, zero has 2 representations (-0.0 and 0.0) and NaNs have lots of different representations (the sign bit is ignored except for stuff like copysign/signbit, there are qNaNs and sNaNs and except for the single case how Inf is represented, all other values of the mantissa mean different representations of NaN). So, unless we track which exact form of NaN can appear, NaN or any [x, x] range with NaN property set can't be a singleton. There could be programs that propagate something important in NaN mantissa and would be upset if frange kills that. Of course, one needs to take into account that when a FPU creates NaN, it will create the canonical qNaN. Jakub
Re: [PATCH] Add support for floating point endpoints to frange.
Jakub, et al... here is the latest version of the frange endpoints patch addressing the signed zero problem (well treating +-0.0 ambiguously), as well as implementing all the relational operators. [Andrew M: I mostly copied our relop code from irange, while keeping track NANs, etc. It should be pleasantly familiar.] A few notes... We can now represent things like [5.0, 5.0], which is the singleton 5.0 *or* a NAN. OTOH, we could also have [5.0, 5.0] !NAN, which is 5.0 without the possibility of a NAN. This allows us to track appropriate ranges on both sides of an if, but keep track of which side (true side) we definitely know we won't have a NAN. As mentioned, frange::singleton_p([0,0]) returns false for any version of 0.0 (unless !HONOR_SIGNED_ZEROS). I'll work on zero tracking at a later time. The patch is getting pretty large as is. For convenience, singleton_p() returns false for a NAN. IMO, it makes the implementation cleaner, but I'm not wed to the idea if someone objects. This patch passes all tests for all languages, including go and ada, with the aforementioned exception of gcc.dg/tree-ssa/phi-opt-24.c which is getting confused because we have propagated (correctly) a 0.0 into the PHI. Hints? Takers? ;-). Aldy On Fri, Aug 26, 2022 at 9:44 PM Andrew Pinski wrote: > > On Fri, Aug 26, 2022 at 12:16 PM Aldy Hernandez wrote: > > > > On Fri, Aug 26, 2022 at 7:40 PM Andrew Pinski wrote: > > > > > > On Fri, Aug 26, 2022 at 8:55 AM Aldy Hernandez wrote: > > > > > > > > [pinskia: I'm CCing you as the author of the match.pd pattern.] > > > > > > > > So, as I wrap up the work here (latest patch attached), I see there's > > > > another phiopt regression (not spaceship related). I was hoping > > > > someone could either give me a hand, or offer some guidance. > > > > > > > > The failure is in gcc.dg/tree-ssa/phi-opt-24.c. > > > > > > > > We fail to transform the following into -A: > > > > > > > > /* { dg-options "-O2 -fno-signed-zeros -fdump-tree-phiopt" } */ > > > > > > > > float f0(float A) > > > > { > > > > // A == 0? A : -Asame as -A > > > > if (A == 0) return A; > > > > return -A; > > > > } > > > > > > > > This is because the abs/negative match.pd pattern here: > > > > > > > > /* abs/negative simplifications moved from > > > > fold_cond_expr_with_comparison, > > > >Need to handle (A - B) case as fold_cond_expr_with_comparison does. > > > >Need to handle UN* comparisons. > > > >... > > > >... > > > > > > > > Sees IL that has the 0.0 propagated. > > > > > > > > Instead of: > > > > > > > >[local count: 1073741824]: > > > > if (A_2(D) == 0.0) > > > > goto ; [34.00%] > > > > else > > > > goto ; [66.00%] > > > > > > > >[local count: 708669601]: > > > > _3 = -A_2(D); > > > > > > > >[local count: 1073741824]: > > > > # _1 = PHI > > > > > > > > It now sees: > > > > > > > >[local count: 1073741824]: > > > > # _1 = PHI <0.0(2), _3(3)> > > > > > > > > which it leaves untouched, causing the if conditional to survive. > > > > > > > > Is this something that can be done by improving the match.pd pattern, > > > > or should be done elsewhere? > > > > > > Oh the pattern which is supposed to catch this does: > > > (simplify > > >(cnd (cmp @0 zerop) integer_zerop (negate@1 @0)) > > > (if (!HONOR_SIGNED_ZEROS (type)) > > > @1)) > > > > On trunk without any patches, for the following snippet with -O2 > > -fno-signed-zeros -fdump-tree-phiopt-folding... > > > > float f0(float A) > > { > > // A == 0? A : -Asame as -A > > if (A == 0) return A; > > return -A; > > } > > > > ...the phiopt2 dump file has: > > > > Applying pattern match.pd:4805, gimple-match.cc:69291, which > > corresponds to the aforementioned pattern. So it looks like that was > > the pattern that was matching that isn't any more? > > > > Are you saying this pattern should only work with integers? > > I am saying the pattern which is right after the one that matches > (without your patch) currrently works for integer only. > You could change integer_zerop to zerop in that pattern but I am
Re: [PATCH] Add support for floating point endpoints to frange.
On Fri, Aug 26, 2022 at 12:16 PM Aldy Hernandez wrote: > > On Fri, Aug 26, 2022 at 7:40 PM Andrew Pinski wrote: > > > > On Fri, Aug 26, 2022 at 8:55 AM Aldy Hernandez wrote: > > > > > > [pinskia: I'm CCing you as the author of the match.pd pattern.] > > > > > > So, as I wrap up the work here (latest patch attached), I see there's > > > another phiopt regression (not spaceship related). I was hoping > > > someone could either give me a hand, or offer some guidance. > > > > > > The failure is in gcc.dg/tree-ssa/phi-opt-24.c. > > > > > > We fail to transform the following into -A: > > > > > > /* { dg-options "-O2 -fno-signed-zeros -fdump-tree-phiopt" } */ > > > > > > float f0(float A) > > > { > > > // A == 0? A : -Asame as -A > > > if (A == 0) return A; > > > return -A; > > > } > > > > > > This is because the abs/negative match.pd pattern here: > > > > > > /* abs/negative simplifications moved from fold_cond_expr_with_comparison, > > >Need to handle (A - B) case as fold_cond_expr_with_comparison does. > > >Need to handle UN* comparisons. > > >... > > >... > > > > > > Sees IL that has the 0.0 propagated. > > > > > > Instead of: > > > > > >[local count: 1073741824]: > > > if (A_2(D) == 0.0) > > > goto ; [34.00%] > > > else > > > goto ; [66.00%] > > > > > >[local count: 708669601]: > > > _3 = -A_2(D); > > > > > >[local count: 1073741824]: > > > # _1 = PHI > > > > > > It now sees: > > > > > >[local count: 1073741824]: > > > # _1 = PHI <0.0(2), _3(3)> > > > > > > which it leaves untouched, causing the if conditional to survive. > > > > > > Is this something that can be done by improving the match.pd pattern, > > > or should be done elsewhere? > > > > Oh the pattern which is supposed to catch this does: > > (simplify > >(cnd (cmp @0 zerop) integer_zerop (negate@1 @0)) > > (if (!HONOR_SIGNED_ZEROS (type)) > > @1)) > > On trunk without any patches, for the following snippet with -O2 > -fno-signed-zeros -fdump-tree-phiopt-folding... > > float f0(float A) > { > // A == 0? A : -Asame as -A > if (A == 0) return A; > return -A; > } > > ...the phiopt2 dump file has: > > Applying pattern match.pd:4805, gimple-match.cc:69291, which > corresponds to the aforementioned pattern. So it looks like that was > the pattern that was matching that isn't any more? > > Are you saying this pattern should only work with integers? I am saying the pattern which is right after the one that matches (without your patch) currrently works for integer only. You could change integer_zerop to zerop in that pattern but I am not 100% sure that is valid thing to do. Note there are a few other patterns in that for loop that does integer_zerop which might need to be zerop too. Thanks, Andrew Pinski > > Aldy >
Re: [PATCH] Add support for floating point endpoints to frange.
On Fri, Aug 26, 2022 at 7:40 PM Andrew Pinski wrote: > > On Fri, Aug 26, 2022 at 8:55 AM Aldy Hernandez wrote: > > > > [pinskia: I'm CCing you as the author of the match.pd pattern.] > > > > So, as I wrap up the work here (latest patch attached), I see there's > > another phiopt regression (not spaceship related). I was hoping > > someone could either give me a hand, or offer some guidance. > > > > The failure is in gcc.dg/tree-ssa/phi-opt-24.c. > > > > We fail to transform the following into -A: > > > > /* { dg-options "-O2 -fno-signed-zeros -fdump-tree-phiopt" } */ > > > > float f0(float A) > > { > > // A == 0? A : -Asame as -A > > if (A == 0) return A; > > return -A; > > } > > > > This is because the abs/negative match.pd pattern here: > > > > /* abs/negative simplifications moved from fold_cond_expr_with_comparison, > >Need to handle (A - B) case as fold_cond_expr_with_comparison does. > >Need to handle UN* comparisons. > >... > >... > > > > Sees IL that has the 0.0 propagated. > > > > Instead of: > > > >[local count: 1073741824]: > > if (A_2(D) == 0.0) > > goto ; [34.00%] > > else > > goto ; [66.00%] > > > >[local count: 708669601]: > > _3 = -A_2(D); > > > >[local count: 1073741824]: > > # _1 = PHI > > > > It now sees: > > > >[local count: 1073741824]: > > # _1 = PHI <0.0(2), _3(3)> > > > > which it leaves untouched, causing the if conditional to survive. > > > > Is this something that can be done by improving the match.pd pattern, > > or should be done elsewhere? > > Oh the pattern which is supposed to catch this does: > (simplify >(cnd (cmp @0 zerop) integer_zerop (negate@1 @0)) > (if (!HONOR_SIGNED_ZEROS (type)) > @1)) On trunk without any patches, for the following snippet with -O2 -fno-signed-zeros -fdump-tree-phiopt-folding... float f0(float A) { // A == 0? A : -Asame as -A if (A == 0) return A; return -A; } ...the phiopt2 dump file has: Applying pattern match.pd:4805, gimple-match.cc:69291, which corresponds to the aforementioned pattern. So it looks like that was the pattern that was matching that isn't any more? Are you saying this pattern should only work with integers? Aldy
Re: [PATCH] Add support for floating point endpoints to frange.
On Fri, Aug 26, 2022, 19:40 Andrew Pinski wrote: > On Fri, Aug 26, 2022 at 8:55 AM Aldy Hernandez wrote: > > > > [pinskia: I'm CCing you as the author of the match.pd pattern.] > > > > So, as I wrap up the work here (latest patch attached), I see there's > > another phiopt regression (not spaceship related). I was hoping > > someone could either give me a hand, or offer some guidance. > > > > The failure is in gcc.dg/tree-ssa/phi-opt-24.c. > > > > We fail to transform the following into -A: > > > > /* { dg-options "-O2 -fno-signed-zeros -fdump-tree-phiopt" } */ > > > > float f0(float A) > > { > > // A == 0? A : -Asame as -A > > if (A == 0) return A; > > return -A; > > } > > > > This is because the abs/negative match.pd pattern here: > > > > /* abs/negative simplifications moved from > fold_cond_expr_with_comparison, > >Need to handle (A - B) case as fold_cond_expr_with_comparison does. > >Need to handle UN* comparisons. > >... > >... > > > > Sees IL that has the 0.0 propagated. > > > > Instead of: > > > >[local count: 1073741824]: > > if (A_2(D) == 0.0) > > goto ; [34.00%] > > else > > goto ; [66.00%] > > > >[local count: 708669601]: > > _3 = -A_2(D); > > > >[local count: 1073741824]: > > # _1 = PHI > > > > It now sees: > > > >[local count: 1073741824]: > > # _1 = PHI <0.0(2), _3(3)> > > > > which it leaves untouched, causing the if conditional to survive. > > > > Is this something that can be done by improving the match.pd pattern, > > or should be done elsewhere? > > Oh the pattern which is supposed to catch this does: > (simplify >(cnd (cmp @0 zerop) integer_zerop (negate@1 @0)) > (if (!HONOR_SIGNED_ZEROS (type)) > @1)) > > Notice the integer_zerop here. > fold_cond_expr_with_comparison has integer_zerop there too. > I am not 100% sure you can replace A_2 with 0.0 where you are doing it > as mentioned in another thread. > Are you sure we can't make the replacement, cause the test runs with -fno-signed-zeros? Aldy > Thanks, > Andrew Pinski > > > > > > Thanks. > > Aldy > >
Re: [PATCH] Add support for floating point endpoints to frange.
On Fri, Aug 26, 2022 at 8:55 AM Aldy Hernandez wrote: > > [pinskia: I'm CCing you as the author of the match.pd pattern.] > > So, as I wrap up the work here (latest patch attached), I see there's > another phiopt regression (not spaceship related). I was hoping > someone could either give me a hand, or offer some guidance. > > The failure is in gcc.dg/tree-ssa/phi-opt-24.c. > > We fail to transform the following into -A: > > /* { dg-options "-O2 -fno-signed-zeros -fdump-tree-phiopt" } */ > > float f0(float A) > { > // A == 0? A : -Asame as -A > if (A == 0) return A; > return -A; > } > > This is because the abs/negative match.pd pattern here: > > /* abs/negative simplifications moved from fold_cond_expr_with_comparison, >Need to handle (A - B) case as fold_cond_expr_with_comparison does. >Need to handle UN* comparisons. >... >... > > Sees IL that has the 0.0 propagated. > > Instead of: > >[local count: 1073741824]: > if (A_2(D) == 0.0) > goto ; [34.00%] > else > goto ; [66.00%] > >[local count: 708669601]: > _3 = -A_2(D); > >[local count: 1073741824]: > # _1 = PHI > > It now sees: > >[local count: 1073741824]: > # _1 = PHI <0.0(2), _3(3)> > > which it leaves untouched, causing the if conditional to survive. > > Is this something that can be done by improving the match.pd pattern, > or should be done elsewhere? Oh the pattern which is supposed to catch this does: (simplify (cnd (cmp @0 zerop) integer_zerop (negate@1 @0)) (if (!HONOR_SIGNED_ZEROS (type)) @1)) Notice the integer_zerop here. fold_cond_expr_with_comparison has integer_zerop there too. I am not 100% sure you can replace A_2 with 0.0 where you are doing it as mentioned in another thread. Thanks, Andrew Pinski > > Thanks. > Aldy
Re: [PATCH] Add support for floating point endpoints to frange.
[pinskia: I'm CCing you as the author of the match.pd pattern.] So, as I wrap up the work here (latest patch attached), I see there's another phiopt regression (not spaceship related). I was hoping someone could either give me a hand, or offer some guidance. The failure is in gcc.dg/tree-ssa/phi-opt-24.c. We fail to transform the following into -A: /* { dg-options "-O2 -fno-signed-zeros -fdump-tree-phiopt" } */ float f0(float A) { // A == 0? A : -Asame as -A if (A == 0) return A; return -A; } This is because the abs/negative match.pd pattern here: /* abs/negative simplifications moved from fold_cond_expr_with_comparison, Need to handle (A - B) case as fold_cond_expr_with_comparison does. Need to handle UN* comparisons. ... ... Sees IL that has the 0.0 propagated. Instead of: [local count: 1073741824]: if (A_2(D) == 0.0) goto ; [34.00%] else goto ; [66.00%] [local count: 708669601]: _3 = -A_2(D); [local count: 1073741824]: # _1 = PHI It now sees: [local count: 1073741824]: # _1 = PHI <0.0(2), _3(3)> which it leaves untouched, causing the if conditional to survive. Is this something that can be done by improving the match.pd pattern, or should be done elsewhere? Thanks. Aldy From a1f64017defaf6d5841b54e8bc867d63738a9f5c Mon Sep 17 00:00:00 2001 From: Aldy Hernandez Date: Tue, 23 Aug 2022 13:36:33 +0200 Subject: [PATCH] Add support for floating point endpoints to frange. The current implementation of frange is just a type with some bits to represent NAN and INF. We can do better and represent endpoints to ultimately solve longstanding PRs such as PR24021. This patch adds these endpoints. In follow-up patches I will add support for relational operators using them, as well as a bare bones PLUS_EXPR range-op-float entry to solve the PR. I have chosen to use REAL_VALUE_TYPEs for the endpoints, since that's what we use underneath the trees. This will be somewhat analogous to our eventual use of wide-ints in the irange. No sense going through added levels of indirection if we can avoid it. That, plus real.* already has a nice API for dealing with floats. With this patch, ranges will be closed float point intervals, which make the implementation simpler, since we don't have to keep track of open/closed intervals. This is conservative enough for use in the ranger world, as we'd rather err on the side of more elements in a range, than less. For example, even though we cannot precisely represent the open interval (3.0, 5.0) with this approach, it is perfectably reasonable to represent it as [3.0, 5.0] since the closed interval is a super set of the open one. In the VRP/ranger world, it is always better to err on the side of more information in a range, than not. After all, when we don't know anything about a range, we just use VARYING which is a fancy term for a range spanning the entire domain. Since REAL_VALUE_TYPEs have properly defined infinity and NAN semantics, all the math can be made to work: [-INF, 3.0] !NAN => Numbers <= 3.0 (NAN cannot happen) [3.0, +INF] => Numbers >= 3.0 (NAN is possible) [-INF, +INF] => VARYING (NAN is possible) [-INF, +INF] !NAN => Entire domain. NAN cannot happen. Also, since REAL_VALUE_TYPEs can represent the minimum and maximum representable values of a TYPE_MODE, we can disambiguate between them and negative and positive infinity (see get_max_float in real.cc). This also makes the math all work. For example, suppose we know nothing about x and y (VARYING). On the TRUE side of x > y, we can deduce that: (a) x cannot be NAN (b) y cannot be NAN (c) y cannot be +INF. (c) means that we can drop the upper bound of "y" from +INF to the maximum representable value for its type. Having endpoints with different representation for infinity and the maximum representable values, means we can drop the +-INF properties we currently have in the frange. I will do this as a follow-up patch, as well as contributing more detailed relational operators. gcc/ChangeLog: * value-range-pretty-print.cc (vrange_printer::visit): Adapt for endpoints. * value-range.cc (frange::set): Same. (frange::normalize_kind): Same. (frange::operator=): Same. (frange::verify_range): Same. (frange::union_): Adapt for endpoints and special case NAN ranges. (frange::intersect): Same. (frange::operator==): Special case NAN ranges. (frange::contains_p): New. (frange::singleton_p): New. (frange_float): New. (frange_nan): New. (early_nan_resolve): New. (real_max_representable): New. (real_min_representable): New. (range_tests_floats): New. (range_tests): Call range_tests_floats. * value-range.h (frange::lower_bound): New. (frange::upper_bound): New. (vrp_val_min): Use real_inf with a sign argument. (frange::frange): New. (frange::set_varying): Adapt for endpoints. (frange::set_undefined): Adapt for endpoints. --- gcc/range-op-float.cc | 16 +-
[PATCH] Add support for floating point endpoints to frange.
The current implementation of frange is just a type with some bits to represent NAN and INF. We can do better and represent endpoints to ultimately solve longstanding PRs such as PR24021. This patch adds these endpoints. In follow-up patches I will add support for relational operators using them, as well as a bare bones PLUS_EXPR range-op-float entry to solve the PR. I have chosen to use REAL_VALUE_TYPEs for the endpoints, since that's what we use underneath the trees. This will be somewhat analogous to our eventual use of wide-ints in the irange. No sense going through added levels of indirection if we can avoid it. That, plus real.* already has a nice API for dealing with floats. With this patch, ranges will be closed float point intervals, which make the implementation simpler, since we don't have to keep track of open/closed intervals. This is conservative enough for use in the ranger world, as we'd rather err on the side of more elements in a range, than less. For example, even though we cannot precisely represent the open interval (3.0, 5.0) with this approach, it is perfectably reasonable to represent it as [3.0, 5.0] since the closed interval is a super set of the open one. In the VRP/ranger world, it is always better to err on the side of more information in a range, than not. After all, when we don't know anything about a range, we just use VARYING which is a fancy term for a range spanning the entire domain. Since REAL_VALUE_TYPEs have properly defined infinity and NAN semantics, all the math can be made to work: [-INF, 3.0] !NAN=> Numbers <= 3.0 (NAN cannot happen) [3.0, +INF] => Numbers >= 3.0 (NAN is possible) [-INF, +INF]=> VARYING (NAN is possible) [-INF, +INF] !NAN => Entire domain. NAN cannot happen. Also, since REAL_VALUE_TYPEs can represent the minimum and maximum representable values of a TYPE_MODE, we can disambiguate between them and negative and positive infinity (see get_max_float in real.cc). This also makes the math all work. For example, suppose we know nothing about x and y (VARYING). On the TRUE side of x > y, we can deduce that: (a) x cannot be NAN (b) y cannot be NAN (c) y cannot be +INF. (c) means that we can drop the upper bound of "y" from +INF to the maximum representable value for its type. Having endpoints with different representation for infinity and the maximum representable values, means we can drop the +-INF properties we currently have in the frange. I will do this as a follow-up patch, as well as contributing more detailed relational operators. I am still tweaking some regressed tests because of the usual premature threading and VRP in the presence of smarter ranges. I figured I'd post now to give others a chance to comment in the meantime. gcc/ChangeLog: * value-range-pretty-print.cc (vrange_printer::visit): Adapt for endpoints. * value-range.cc (frange::set): Same. (frange::normalize_kind): Same. (frange::union_): Same. (frange::intersect): Same. (frange::operator=): Same. (frange::verify_range): Same. (frange::contains_p): New. (frange::singleton_p): New. (frange_float): New. (real_max_representable): New. (real_min_representable): New. (range_tests_floats): New. (range_tests): Call range_tests_floats. * value-range.h (frange::lower_bound): New. (frange::upper_bound): New. (vrp_val_min): Use real_inf with a sign argument. (frange::frange): New. (frange::set_varying): Adapt for endpoints. (frange::set_undefined): Adapt for endpoints. --- gcc/value-range-pretty-print.cc | 12 +- gcc/value-range.cc | 259 ++-- gcc/value-range.h | 53 ++- 3 files changed, 312 insertions(+), 12 deletions(-) diff --git a/gcc/value-range-pretty-print.cc b/gcc/value-range-pretty-print.cc index cbf50d3d854..1a026c22f99 100644 --- a/gcc/value-range-pretty-print.cc +++ b/gcc/value-range-pretty-print.cc @@ -122,19 +122,29 @@ vrange_printer::print_irange_bitmasks (const irange ) const void vrange_printer::visit (const frange ) const { + tree type = r.type (); + pp_string (pp, "[frange] "); if (r.undefined_p ()) { pp_string (pp, "UNDEFINED"); return; } - dump_generic_node (pp, r.type (), 0, TDF_NONE, false); + dump_generic_node (pp, type, 0, TDF_NONE, false); pp_string (pp, " "); if (r.varying_p ()) { pp_string (pp, "VARYING"); return; } + pp_character (pp, '['); + dump_generic_node (pp, +build_real (type, r.lower_bound ()), 0, TDF_NONE, false); + pp_string (pp, ", "); + dump_generic_node (pp, +build_real (type, r.upper_bound ()), 0, TDF_NONE, false); + pp_string (pp, "] "); + print_frange_prop ("NAN", r.get_nan ()); print_frange_prop ("INF", r.get_inf ());