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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 25 additions & 23 deletions vyper/venom/passes/sccp/sccp.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,35 +227,37 @@ def _eval(self, inst) -> LatticeItem:
instruction to the SSA work list if the knowledge about the variable
changed.
"""
opcode = inst.opcode

ops = []
def finalize(ret):
# 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
self._add_ssa_work_items(inst)
return ret

opcode = inst.opcode
ops: list[IROperand] = []
for op in inst.operands:
if isinstance(op, IRVariable):
ops.append(self.lattice[op])
elif isinstance(op, IRLabel):
return LatticeEnum.BOTTOM
# Evaluate the operand according to the lattice
if isinstance(op, IRLabel):
return finalize(LatticeEnum.BOTTOM)
elif isinstance(op, IRVariable):
eval_result = self.lattice[op]
else:
ops.append(op)
eval_result = op

ret = None
if LatticeEnum.BOTTOM in ops:
ret = LatticeEnum.BOTTOM
else:
if opcode in ARITHMETIC_OPS:
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")
# If any operand is BOTTOM, the whole operation is BOTTOM
# and we can stop the evaluation early
if eval_result is LatticeEnum.BOTTOM:
return finalize(LatticeEnum.BOTTOM)

old_val = self.lattice.get(inst.output, LatticeEnum.TOP)
if old_val != ret:
self.lattice[inst.output] = ret # type: ignore
self._add_ssa_work_items(inst)
assert isinstance(eval_result, IROperand)
ops.append(eval_result)

return ret # type: ignore
# If we haven't found BOTTOM yet, evaluate the operation
fn = ARITHMETIC_OPS[opcode]
return finalize(IRLiteral(fn(ops)))

def _add_ssa_work_items(self, inst: IRInstruction):
for target_inst in self.dfg.get_uses(inst.output): # type: ignore
Expand Down
Loading