[mypyc] Split BinaryIntOp and introduce ComparisonOp, implements is/is not op#9313
Merged
TH3CHARLie merged 5 commits intopython:masterfrom Aug 18, 2020
TH3CHARLie:comparison-op-refactor
Merged
[mypyc] Split BinaryIntOp and introduce ComparisonOp, implements is/is not op#9313TH3CHARLie merged 5 commits intopython:masterfrom TH3CHARLie:comparison-op-refactor
TH3CHARLie merged 5 commits intopython:masterfrom
TH3CHARLie:comparison-op-refactor
Conversation
9 tasks
Collaborator
|
There are merge conflicts. Also, I can't see the definition of |
JukkaL
reviewed
Aug 18, 2020
Collaborator
JukkaL
left a comment
There was a problem hiding this comment.
Left a few nits about a comment.
Collaborator
Author
|
@JukkaL Looks like the github review fail to represent to diff clearly, so here's the definition of two classes from 2764aa0: class ComparisonOp(RegisterOp):
"""Comparison ops
The result type will always be boolean.
Support comparison between integer types and pointer types
"""
error_kind = ERR_NEVER
# S for signed and U for unsigned
EQ = 100 # type: Final
NEQ = 101 # type: Final
SLT = 102 # type: Final
SGT = 103 # type: Final
SLE = 104 # type: Final
SGE = 105 # type: Final
ULT = 106 # type: Final
UGT = 107 # type: Final
ULE = 108 # type: Final
UGE = 109 # type: Final
op_str = {
EQ: '==',
NEQ: '!=',
SLT: '<',
SGT: '>',
SLE: '<=',
SGE: '>=',
ULT: '<',
UGT: '>',
ULE: '<=',
UGE: '>=',
} # type: Final
def __init__(self, lhs: Value, rhs: Value, op: int, line: int = -1) -> None:
super().__init__(line)
self.type = bool_rprimitive
self.lhs = lhs
self.rhs = rhs
self.op = op
def sources(self) -> List[Value]:
return [self.lhs, self.rhs]
def to_str(self, env: Environment) -> str:
if self.op in (self.SLT, self.SGT, self.SLE, self.SGE):
sign_format = " :: signed"
elif self.op in (self.ULT, self.UGT, self.ULE, self.UGE):
sign_format = " :: unsigned"
else:
sign_format = ""
return env.format('%r = %r %s %r%s', self, self.lhs,
self.op_str[self.op], self.rhs, sign_format)
def accept(self, visitor: 'OpVisitor[T]') -> T:
return visitor.visit_comparison_op(self)class BinaryIntOp(RegisterOp):
"""Binary arithmetic and bitwise operations on integer types
These ops are low-level and will be eventually generated to simple x op y form.
The left and right values should be of low-level integer types that support those ops
"""
error_kind = ERR_NEVER
# arithmetic
ADD = 0 # type: Final
SUB = 1 # type: Final
MUL = 2 # type: Final
DIV = 3 # type: Final
MOD = 4 # type: Final
# bitwise
AND = 200 # type: Final
OR = 201 # type: Final
XOR = 202 # type: Final
LEFT_SHIFT = 203 # type: Final
RIGHT_SHIFT = 204 # type: Final
op_str = {
ADD: '+',
SUB: '-',
MUL: '*',
DIV: '/',
MOD: '%',
AND: '&',
OR: '|',
XOR: '^',
LEFT_SHIFT: '<<',
RIGHT_SHIFT: '>>',
} # type: Final
def __init__(self, type: RType, lhs: Value, rhs: Value, op: int, line: int = -1) -> None:
super().__init__(line)
self.type = type
self.lhs = lhs
self.rhs = rhs
self.op = op
def sources(self) -> List[Value]:
return [self.lhs, self.rhs]
def to_str(self, env: Environment) -> str:
return env.format('%r = %r %s %r', self, self.lhs,
self.op_str[self.op], self.rhs)
def accept(self, visitor: 'OpVisitor[T]') -> T:
return visitor.visit_binary_int_op(self) |
JukkaL
approved these changes
Aug 18, 2020
Collaborator
JukkaL
left a comment
There was a problem hiding this comment.
Looks good! The IR is now more consistent, as more low-level Python operations such as 'is' are represented using low-level IR ops. Feel free to merge after you've addressed my comment and the build is green.
| """cpy_r_r0 = PyDict_Contains(cpy_r_d, cpy_r_o);""") | ||
|
|
||
| def test_binary_int_op(self) -> None: | ||
| self.assert_emit(BinaryIntOp(short_int_rprimitive, self.s1, self.s2, BinaryIntOp.ADD, 1), |
Collaborator
There was a problem hiding this comment.
Thanks for adding more test coverage!
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.
BinaryIntOpused to represent arithmetic, bitwise and comparison operations on integer operations. However, this design prevents us to compare pointer types and all comparison operations should be of boolean return type while manually specifying this inBinaryIntOpis both verbose and error-prone.This PR splits
BinaryIntOpand moves the comparison functionalities toComparsionOp.Based on the new op, this PR also implements
isandis notop.