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

refactor[venom]: optimize lattice evaluation #4368

Merged
Merged
33 changes: 20 additions & 13 deletions vyper/venom/passes/sccp/sccp.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,28 +228,35 @@ def _eval(self, inst) -> LatticeItem:
changed.
"""
opcode = inst.opcode

ret = None
ops = []
for op in inst.operands:
if isinstance(op, IRVariable):
ops.append(self.lattice[op])
elif isinstance(op, IRLabel):
return LatticeEnum.BOTTOM
else:
ops.append(op)
# Evaluate the operand according to the lattice
match op:
case IRLabel():
return LatticeEnum.BOTTOM
case IRVariable():
eval_result = self.lattice[op]
case _:
eval_result = op

# If any operand is BOTTOM, the whole operation is BOTTOM
# and we can stop the evaluation early
if eval_result is LatticeEnum.BOTTOM:
ret = LatticeEnum.BOTTOM
break

ret = None
if LatticeEnum.BOTTOM in ops:
ret = LatticeEnum.BOTTOM
else:
ops.append(eval_result)

# If we haven't found BOTTOM yet, evaluate the operation
if ret is None:
if opcode in ARITHMETIC_OPS:
harkal marked this conversation as resolved.
Show resolved Hide resolved
fn = ARITHMETIC_OPS[opcode]
ret = IRLiteral(fn(ops)) # type: ignore
elif len(ops) > 0:
ret = ops[0] # type: ignore
else:
raise CompilerPanic("Bad constant evaluation")

# Update the lattice if the value changed
old_val = self.lattice.get(inst.output, LatticeEnum.TOP)
if old_val != ret:
self.lattice[inst.output] = ret # type: ignore
Expand Down
Loading