Skip to content

Commit

Permalink
refactor[venom]: refactor venom operand classes (#3915)
Browse files Browse the repository at this point in the history
the original `IRValue`, `IRVariable`, `IRLabel` and `IROperand`
hierarchy got convoluted after several refactors. this commit refactors
the hierarchy so that the relationship between the classes is clearer.
  • Loading branch information
harkal authored Apr 6, 2024
1 parent 7485cea commit 9d0d147
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 46 deletions.
63 changes: 19 additions & 44 deletions vyper/venom/basicblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ def __repr__(self) -> str:

class IROperand:
"""
IROperand represents an operand in IR. An operand is anything that can
be an argument to an IRInstruction
IROperand represents an IR operand. An operand is anything that can be
operated by instructions. It can be a literal, a variable, or a label.
"""

value: Any
Expand All @@ -102,18 +102,19 @@ class IROperand:
def name(self) -> str:
return self.value

def __hash__(self) -> int:
return hash(self.value)

class IRValue(IROperand):
"""
IRValue represents a value in IR. A value is anything that can be
operated by non-control flow instructions. That is, IRValues can be
IRVariables or IRLiterals.
"""
def __eq__(self, other) -> bool:
if not isinstance(other, type(self)):
return False
return self.value == other.value

pass
def __repr__(self) -> str:
return str(self.value)


class IRLiteral(IRValue):
class IRLiteral(IROperand):
"""
IRLiteral represents a literal in IR
"""
Expand All @@ -124,25 +125,13 @@ def __init__(self, value: int) -> None:
assert isinstance(value, int), "value must be an int"
self.value = value

def __hash__(self) -> int:
return self.value.__hash__()

def __eq__(self, other) -> bool:
if not isinstance(other, type(self)):
return False
return self.value == other.value

def __repr__(self) -> str:
return str(self.value)


class IRVariable(IRValue):
class IRVariable(IROperand):
"""
IRVariable represents a variable in IR. A variable is a string that starts with a %.
"""

value: str
offset: int = 0

def __init__(self, value: str, version: Optional[str | int] = None) -> None:
assert isinstance(value, str)
Expand All @@ -153,7 +142,6 @@ def __init__(self, value: str, version: Optional[str | int] = None) -> None:
if value[0] != "%":
value = f"%{value}"
self.value = value
self.offset = 0

@property
def name(self) -> str:
Expand All @@ -165,17 +153,6 @@ def version(self) -> int:
return 0
return int(self.value.split(":")[1])

def __hash__(self) -> int:
return self.value.__hash__()

def __eq__(self, other) -> bool:
if not isinstance(other, type(self)):
return False
return self.value == other.value

def __repr__(self) -> str:
return self.value


class IRLabel(IROperand):
"""
Expand All @@ -192,16 +169,14 @@ def __init__(self, value: str, is_symbol: bool = False) -> None:
self.value = value
self.is_symbol = is_symbol

def __hash__(self) -> int:
return hash(self.value)
def __eq__(self, other):
# no need for is_symbol to participate in equality
return super().__eq__(other)

def __eq__(self, other) -> bool:
if not isinstance(other, type(self)):
return False
return self.value == other.value

def __repr__(self) -> str:
return self.value
def __hash__(self):
# __hash__ is required when __eq__ is overridden --
# https://docs.python.org/3/reference/datamodel.html#object.__hash__
return super().__hash__()


class IRInstruction:
Expand Down
4 changes: 2 additions & 2 deletions vyper/venom/passes/make_ssa.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def _rename_vars(self, basic_block: IRBasicBlock):

# Pre-action
for inst in basic_block.instructions:
new_ops = []
new_ops: list[IROperand] = []
if inst.opcode != "phi":
for op in inst.operands:
if not isinstance(op, IRVariable):
Expand Down Expand Up @@ -143,7 +143,7 @@ def _remove_degenerate_phis(self, entry: IRBasicBlock):
if inst.opcode != "phi":
continue

new_ops = []
new_ops: list[IROperand] = []
for label, op in inst.phi_operands:
if op == inst.output:
continue
Expand Down

0 comments on commit 9d0d147

Please sign in to comment.