Handle divisor bigger than dividend in can_div_away_from_zero_p #176
No reviewers
Labels
No labels
Compat/Breaking
Frontend/ada
Frontend/c
Frontend/c++
Frontend/rust
General/forge
Kind/Bug
Kind/Documentation
Kind/Enhancement
Kind/Feature
Kind/Security
Kind/Testing
Library/libgcc
Library/libstdc++
Midend/gimple
Midend/rtl
Midend/tree
Priority
Critical
Priority
High
Priority
Low
Priority
Medium
Reviewed
Confirmed
Reviewed
Duplicate
Reviewed
Invalid
Reviewed
Won't Fix
Status
Abandoned
Status
Blocked
Status
Need More Info
Target/aarch64
Target/arm
Target/i386
bug
duplicate
enhancement
help wanted
invalid
question
wontfix
No milestone
No assignees
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gcc/gcc!176
Loading…
Reference in a new issue
No description provided.
Delete branch "chris.bazley/gcc:pr125767"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
PR middle-end/125767
When the magnitude of the divisor is known to be bigger
than that of the dividend, the output quotient should be 1
when divisor and dividend have the same sign or -1
when they have opposite signs.
Previously, can_div_away_from_zero_p was overreliant on
can_div_trunc_p: if can_div_trunc_p returned a failure
indication then can_div_away_from_zero_p did likewise.
This matters because can_div_away_from_zero_p is commonly
used to answer questions such as "How many registers does
this value occupy?" If the register has a scalable vector
type, then the answer should be 1 if the number of bits
occupied by the value is known to be not greater than
the minimum number of bits in the vector type (as well as
if the number of bits occupied by the value is known to be
smaller). Previously, can_div_away_from_zero_p had to be
used with care to avoid wrongly concluding that a value of
type V16QI would not fit in a register of type VNx16QI.
That could cause selection of inefficient instruction
sequences or even an ICE in cases where V8QI in VNx16QI
behaved as expected.
Because polynomial division truncated toward zero can
fail to produce a constant quotient in cases where
polynomial division rounded away from zero can produce a
constant quotient, it is not sufficient for the
implementation of can_div_away_from_zero_p to simply adjust
the quotient produced by can_div_trunc_p.
can_div_trunc_p requires |b * Q| <= |a| whereas
can_div_away_from_zero_p requires |b * Q| >= |a|. The
latter can be proven in cases where the former cannot.
For example, when a = 16 + 0i and b = 16 + 16i, |b * Q|
cannot be smaller than |a| unless Q = 0, which would
violate a common requirement that |a - b * Q| < |b|
(because |a| >= 16 and |b| >= 16). In contrast, |b * Q|
is bigger than |a| if Q > 1 or i > 0. Crucially, it's
impossible to know the value of i at compile time, so
can_div_trunc_p must conservatively return false.
Another way of looking at it is:
Q = (16 + 0i) / (16 + 16i)
= 16 / (16 + 16i)
= 1 / (1 + i)
which is not "some constant Q" that these functions must
find in order to return true; however, we can be sure that
whatever the unknowable value of 1 / (1 + i) is, it lies
within the range 0 < Q <= 1. We cannot know whether that
answer should be truncated to zero, but we can know that
it should be rounded to one.
gcc/ChangeLog:
gcc/testsuite/ChangeLog:
CC: rdsandiford@googlemail.com, tamar.christina@arm.com, rguenther@suse.de
Hi @chris.bazley. This bot helps send your PR as a patch series to the mailing list.
Next steps
CC: Reviewer1 <revi.ewer@example.com>, Reviewer2 <an.other@example.com>./preview./submit.Changes since vXsection, and comment with/submitagain.See Submitting Patches and UsingForge.
Consider joining the gcc and gcc-patches mailing lists..
For real time communication, check the gcc irc channels.
Or join
#overseerson Libera Chat, particularly if this automation is not working (stay online to get replies, IRC does not save messages if people are not online).de5cdb31d1c692b66804/submit
Version 1 of this pull request has been stored. It includes the following commits:
c692b66804Sent patch series version 1 containing 1 patches to gcc-patches mailing list gcc-patches@gcc.gnu.org and cc'd rdsandiford@googlemail.com, tamar.christina@arm.com.
Cover letter
How did you test this? When I tried bootstrapping with it locally, I got:
The suggestion below includes a snippet that uses
POLY_INT_TYPEto avoid that.@ -2423,0 +2450,4 @@(1) r = 7 + -9 = -2 OR r = -7 + 9 = 2(2) |7| <= |-9| OR |-7| <= |9|(3) |-2| < |-9| OR |2| < |9| */if ((known_lt (a, 0) && known_le (-a, b))Negating a negative value could lead to signed overflow at the extremes. Also, the
known_*conditions aren't trivial, especially for wide-int-based types, so it would be better not to repeat them if possible.How about replacing these two blocks with:
I have a feeling there's a more elegant way to write it, but…
@rsandifo wrote in gcc/gcc-TEST#176 (comment):
Hi Richard, thanks for your review.
I was aware of this issue when I wrote the first version of this patch, and initially followed the same practice as you (only negate positive numbers). e.g., this code was part of my initial version:
I gave up on that approach because it did not work for unsigned coefficients. e.g.
I considered using type traits to check the signedness of Ca and Cb, but then I started to think that my concern about overflow was outweighed by the complexity of avoiding it (and I wasn't sure that the overloaded - operator had the danger I was concerned about anyway).
How have you solved that issue in your version?
In my code,
(known_lt (a, 0) && known_le (-a, b)impliesIn your proposed replacement, the equivalent check is
which implies
I think it's equivalent, but it needs one more comparison. The other difference is that it does not negate a negative number.
@rsandifo wrote in gcc/gcc-TEST#176/files (comment):
In my code,
(known_gt (a, 0) && known_lt (b, 0) && known_le (a, -b))implies
In your proposed replacement, the equivalent check is
which implies
So I think they are equivalent too.
@chris.bazley wrote in gcc/gcc-TEST#176 (comment):
Would you prefer !std::is_unsigned::value or known_lt (-a, a) for this purpose? I assume that the former is cheaper.
@chris.bazley wrote in gcc/gcc-TEST#176 (comment):
Scratch that, I think wi::neg() is the way.
Implementing support for mixed-signedness coefficients is much harder than I first realised. Even an expression such as known_le (b, a) can give the wrong result if the type of b is signed but the type of a is unsigned, or vice versa.
I am wondering why the fundamental comparison operations aren't defined like this:
template<unsigned int N, typename Ca, typename Cb>
inline typename if_nonpoly<Cb, bool>::type
maybe_lt (const poly_int<N, Ca> &a, const Cb &b)
{
typedef POLY_CAST (Ca, Cb) NCa;
typedef POLY_CAST (Cb, Ca) NCb;
if (N >= 2)
for (unsigned int i = 1; i < N; i++)
if (NCa (a.coeffs[i]) < NCa (0))
return true;
return NCa (a.coeffs[0]) < NCb (b);
}
I now have an upgrade for maybe_lt and friends that I am happy with, but there is still the issue that can_div_away_from_zero_p can fall back to using can_div_trunc_p, which does not handle mixed signed/unsigned types.
@chris.bazley wrote in gcc/gcc-TEST#176 (comment):
Please see gcc/gcc-TEST#179 and gcc/gcc-TEST#180
I don't have more time to spend on this so I have simply copied and pasted your code. Here are the scenarios in which I expect it to return the wrong result:
However, I cannot actually see any examples of such inputs in GCC, so maybe it does not matter.
@ -2161,6 +2161,14 @@ test_can_div_away_from_zero_p ()ph::make (4, 5, 6),&const_quot));ASSERT_EQ (const_quot, C (0));ASSERT_TRUE (can_div_away_from_zero_p (ph::make (16, 0, 0),It would be good to test the third coefficient for something other than 0, at least in some of the tests.
The way the tests work is that the function is instantiated for N==1, N==2, and N==3. Testing something in the third coefficient would leave the N==2 case unaffected, i.e. it would still be
[16, 0] / [16, 16].b1e0a9ca39b7fdbdfbcdb7fdbdfbcd159653ff18@ -2423,0 +2440,4 @@}if (known_gt (b, POLY_INT_TYPE (Cb) (0))){wi::overflow_type ovf;I'm not sure you'll like this solution, so I'll also push an alternative.
Version 2 of this pull request has been stored. It includes the following commits:
159653ff18159653ff18d17fe346dfd17fe346df5c94e3b7bb5c94e3b7bbc353477a4ac353477a4afb031559f8/submit
Version 3 of this pull request has been stored. It includes the following commits:
fb031559f8Pull Request versions:
c6133f911ec692b66804c6133f911e159653ff18c6133f911efb031559f8In order to compare , clone this repository and run
Sent patch series version 3 containing 1 patches to gcc-patches mailing list gcc-patches@gcc.gnu.org and cc'd rdsandiford@googlemail.com, tamar.christina@arm.com, rguenther@suse.de.
Cover letter
Pushed to upstream/master as
87c2f1ce7cPull request closed