Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compare Expressions: Fix the conditions for coalescing a node with its parent #943

Merged
merged 7 commits into from
Nov 19, 2020

Conversation

mgrang
Copy link

@mgrang mgrang commented Nov 17, 2020

We can coalesce a BinaryNode if any one of the following conditions are
true:

  1. The parent has the same operator as the current node OR
  2. The current node has just one child (for example, as a result of
    constant folding) and the parent and current operators are commutative and
    associative.

Fixes issue #932

We had erroneously added the condition that to coalesce nodes the binary
operators of the parent and child node must be equal. This fails for the case
where we first constant-fold nodes having a differen operator than the parent
and then need to coalesce the constant folded node into the parent.

Fixes issue #932
@mgrang mgrang requested review from dtarditi, kkjeer and sulekhark and removed request for dtarditi November 17, 2020 00:08
@mgrang
Copy link
Author

mgrang commented Nov 17, 2020

The comment here needs to be updated to be in sync with the fix.

Done.

We can coalesce a BinaryNode if any one of the following conditions are
true:
1. The parent has the same operator as the current node OR
2. The current node has just one child (for example, as a result of
constant folding) and the parent and current operators are commutative and
associative.
@mgrang mgrang changed the title Compare Expressions: Remove erroneous condition on coalescing nodes Compare Expressions: Fix the conditions for coalescing a node with its parent Nov 17, 2020
@mgrang
Copy link
Author

mgrang commented Nov 18, 2020

Some notes from the PR review discussion:

  1. Maybe we should remove the operator as part of constant folding. For example, given nodes +,1,2,3 we should not replace them with +,6 but 6. This means we need to remove the operator as part of constant folding itself. We also need to handle the case where an operator has constant and non-constant nodes. For example, given +,a,2,3 we should not get rid of the operator in this case.
    Note: Constant folding may be more broadly useful in bounds checking. So it's a good idea to make constant folding self-sufficient.

  2. The tree is not really a "binary" tree but an n-ary tree. So calling the operator nodes as BinaryNode sounds confusing. Maybe we should rename BinaryNode to InnerNode or OperatorNode. This is fixed in Compare Expressions: Rename BinaryNode to OperatorNode [NFC] #946

// 1. The parent has the same operator as the current node OR
// 2. The current node has just one child (for example, as a result of
// constant folding) and the parent and current operators are commutative and
// associative.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thoughts are as follows:
a) Should the condition 1 above not be "parent has the same operator as the current node, and the operator is commutative and associative" ?
b) If we implement constant folding to be complete in itself, then the current node can never have just one child because of constant folding. It is possible that the current node will have just one child if the operator at the current node is a unary operator.

Copy link
Author

@mgrang mgrang Nov 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thoughts are as follows:
a) Should the condition 1 above not be "parent has the same operator as the current node, and the operator is commutative and associative" ?
Could you elaborate why this should not be the case? We should not try to coalesce different operators and coalescing only makes sense if the operator is commutative and associative because after coalescing we also need to sort the nodes.
---> I meant the same thing. It seems like my question was confusing. The main point of my question is that the comment on lines 39 thro' 42 in the file PreorderAST.cpp is a bit unclear about what you just explained in the sentence above.

b) If we implement constant folding to be complete in itself, then the current node can never have just one child because of constant folding. It is possible that the current node will have just one child if the operator at the current node is a unary operator.
We have now implemented constant folding to be complete in itself. As part of constant folding we now invoke this method to coalesce the node. So we need this condition here. Otherwise we would end up duplicating logic to coalesce nodes.
---> Sounds good.

Copy link
Contributor

@sulekhark sulekhark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix assert in bounds widening "BinaryNode operator must equal parent operator"
3 participants