Repository navigation
Fix incorrectly-preserved flags in simplifier - #6847
Merged
Merged
Conversation
This flag is about the operation to be performed, which is changing.
Flags that describe the resulting value can always be kept. There are
likely other flags that should be unset here, but this is a start.
The following are a few examples of cases where the new/modified
operation could overflow even if the original cannot. The first one is
implemented in simplifier. The second will probably not happen, since
the compiler prefers isub, but it is simple and hopefully illustrative.
imul (cannotOverflow) ineg
x ==> imul
iconst -2 x
iconst 2
Here, there is (in general) a possibility that x could be 2**30. In that
case, multiplying by -2 gives -2**31 (i.e. MIN) without overflow.
However, if we multiply instead by 2, the true product is 2**31, which
is not representable, so the product overflows and produces MIN,
Furthermore, if the result of imul is MIN, then ineg overflows too.
isub (cannotOverflow) iadd
x ==> x
iconst MIN iconst MIN
For isub not to overflow, we require only that x be negative. The iadd
will have the same result, but because x is negative, it will always
overflow.
...whenever the constant value is updated, including at construction. Also set highWordZero correctly for lconst nodes at the same time. Most importantly, whenever a constant node is mutated in-place, this clears any of the aforementioned flags that no longer apply to the new value. I thought that such mutations were considered to be incorrect, since they change the result of the node. Either way, at least one pass (simplifier) modifies constant nodes with refcount 1. Since there are likely many places where it does this, and since improper preservation of flags is more or less the only harm it can do, this commit helps such passes by freshly determining the values of some flags that may have become stale. Passes that may remember some fact about a constant node based on its identity (pointer/global index), e.g. by memoizing the results of a function, must not update constant nodes in this way if doing so could invalidate such a remembered fact. Or at the very least they must be able to forget or correct the fact, though attempting to do so is error prone.
These are useful more broadly and should be more widely available. Additionally: - Add static assertions to check that the argument types are signed integral types (as they were already required to be). - Define a StickyOverflowFlag type and corresponding overloads for determining whether there is any overflow in a series of operations.
In particular, for a constant c > 0, preserve: - zero/sign flags and cannotOverflow when normalizing iadd to isub, i.e. where x+c becomes x-(-c); - cannotOverflow when rewriting an integer addition (-x)+y to y-x; - cannotOverflow when normalizing isub to iadd, i.e. where x-c becomes x+(-c); - cannotOverflow when rewriting a negation -(x-y) to y-x; - cannotOverflow (if possible) when coalescing right-hand-side constants for consecutive iadd/isub operations; and - cannotOverflow (if possible) when extracting ineg from imul, i.e. where x*(-c) becomes -(x*c); In all but the first case, the zero/sign flags are already preserved. Additionally, in the last case, i.e. when extracting ineg from imul, the resulting ineg and the new imul are either both zero or both nonzero. If ineg can't overflow, then neither can the new imul, and any pair of corresponding nonzero results from imul and ineg have opposite signs.
The new multiplication does not have the same result as the original addition or subtraction did.
jdmpapin
requested review from
0xdaryl,
Leonardo2718,
mstoodle and
vijaysun-omr
as code owners
December 13, 2022 16:53
vijaysun-omr
approved these changes
Jan 27, 2023
vijaysun-omr
approved these changes
Jan 27, 2023
Contributor
|
I have reviewed only a couple of commits so far, and will continue as time permits over the next (working) day or two. In the meantime I will launch some more tests. |
Contributor
|
Jenkins build all |
vijaysun-omr
approved these changes
Jan 31, 2023
vijaysun-omr
approved these changes
Jan 31, 2023
vijaysun-omr
approved these changes
Jan 31, 2023
Contributor
|
Completed my review and the main change seems to be the extraction of the routines to do the sub/add with overflow checking and their use in simplifier (checked the different cases where the overflow flag was being set and they look correct to me). Tests also seem to have passed. Merging. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TR::Node::recreate()unset the cannotOverflow flagX==0,X!=0,X>=0,X<=0flags correctly for constant nodesVPConstraint::{add,sub}WithOverflow()freestanding functionsiadd/isubasimulwhen distributing in simplifierPlease see the individual commit messages.