-
Notifications
You must be signed in to change notification settings - Fork 200
Add optimization for replacing identities of binary operators. #817
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -66,27 +66,37 @@ | |
| ast.Num(n=-1)], | ||
| keywords=[])), | ||
|
|
||
| # X * X => X ** 2 | ||
| (ast.BinOp(left=Placeholder(0), op=ast.Mult(), right=Placeholder(0)), | ||
| lambda: ast.BinOp(left=Placeholder(0), op=ast.Pow(), right=ast.Num(n=2))), | ||
|
|
||
| # a + "..." + b => "...".join((a, b)) | ||
| (ast.BinOp(left=ast.BinOp(left=Placeholder(0), | ||
| op=ast.Add(), | ||
| right=ast.Str(Placeholder(1))), | ||
| op=ast.Add(), | ||
| right=Placeholder(2)), | ||
| lambda: ast.Call(func=ast.Attribute( | ||
| ast.Attribute( | ||
| ast.Name('__builtin__', ast.Load(), None), | ||
| 'str', | ||
| ast.Load()), | ||
| 'join', ast.Load()), | ||
| args=[ast.Str(Placeholder(1)), | ||
| ast.Tuple([Placeholder(0), Placeholder(2)], ast.Load())], | ||
| keywords=[])), | ||
| ] | ||
|
|
||
| # Dictionary with ast operator name as keys for each a list of tuple of | ||
| # (left, right, replacement) is defined. | ||
| # replacement have to be a lambda function to have a new ast to replace when | ||
| # replacement is inserted in main ast | ||
| know_pattern_BinOp = { | ||
| ast.Mult.__name__ : [ | ||
| (Placeholder(0), ast.Num(1), lambda: Placeholder(0)), # X * 1 => X | ||
| (ast.Num(1), Placeholder(0), lambda: Placeholder(0)), # 1 * X => X | ||
| (Placeholder(0), Placeholder(0), lambda: ast.BinOp(left=Placeholder(0), op=ast.Pow(), right=ast.Num(n=2))), # X * X => X ** 2 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This case is already handled by |
||
| ], | ||
| ast.Add.__name__ : [ | ||
| (Placeholder(0), ast.Num(0), lambda: Placeholder(0)), # X + 0 => X | ||
| (ast.Num(0), Placeholder(0), lambda: Placeholder(0)), # 0 + X => X | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same copy issue as above, when one multiplies by one. |
||
| ( # a + "..." + b => "...".join((a, b)) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one looks correct :-) |
||
| ast.BinOp(left=Placeholder(0), op=ast.Add(), right=ast.Str(Placeholder(1))), | ||
| Placeholder(2), | ||
| lambda: ast.Call(func=ast.Attribute( | ||
| ast.Attribute(ast.Name('__builtin__', ast.Load(), None),'str',ast.Load()),'join', ast.Load()), | ||
| args=[ast.Str(Placeholder(1)),ast.Tuple([Placeholder(0), Placeholder(2)], ast.Load())], | ||
| keywords=[]) | ||
| ), | ||
|
|
||
| ], | ||
| ast.Sub.__name__ : [ | ||
| (Placeholder(0), ast.Num(0), lambda: Placeholder(0)), # X - 0 => X | ||
| (ast.Num(0), Placeholder(0), lambda: ast.UnaryOp(op=ast.USub(), operand=Placeholder(0))), # 0 - X => -X | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same copy issue here. |
||
| ], | ||
| } | ||
|
|
||
|
|
||
| class PlaceholderReplace(Transformation): | ||
|
|
||
|
|
@@ -125,3 +135,21 @@ def visit(self, node): | |
| node = PlaceholderReplace(check.placeholders).visit(replace()) | ||
| self.update = True | ||
| return super(PatternTransform, self).visit(node) | ||
|
|
||
| def visit_BinOp(self, node): | ||
| """ | ||
| Special method for BinOp. | ||
| Try to replace if node match the given pattern. | ||
| """ | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do you need that specialization?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought would be good for speeding-up the optimization. |
||
| self.generic_visit(node) | ||
| op_name = node.op.__class__.__name__ | ||
| if op_name in know_pattern_BinOp: | ||
| for left, right, replace in know_pattern_BinOp[op_name]: | ||
| check_left = Check(node.left, dict()) | ||
| if check_left.visit(left): | ||
| check_right = Check(node.right, check_left.placeholders) | ||
| if check_right.visit(right): | ||
| node = PlaceholderReplace(check_right.placeholders).visit(replace()) | ||
| self.update = True | ||
| break | ||
| return node | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, I relaized that even this simple transformation is not always correct:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Arg, damn python.. I need to find a way to make it safe...
For my test case I need somehow the replacements