Skip to content

Commit

Permalink
Merge branch 'master' into refactor/load-vyz
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-cooper authored Nov 20, 2024
2 parents ff7682d + 0c23b21 commit 0ffcbac
Showing 1 changed file with 25 additions and 23 deletions.
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

0 comments on commit 0ffcbac

Please sign in to comment.