From 786ca4567b5bcff93d39620faf9a1ea947344253 Mon Sep 17 00:00:00 2001 From: Charles Cooper Date: Sun, 14 Apr 2024 17:27:32 -0400 Subject: [PATCH] wip - reorder commutative ops --- vyper/venom/ir_node_to_venom.py | 1 + vyper/venom/passes/dft.py | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/vyper/venom/ir_node_to_venom.py b/vyper/venom/ir_node_to_venom.py index f610e17f58..757f753a44 100644 --- a/vyper/venom/ir_node_to_venom.py +++ b/vyper/venom/ir_node_to_venom.py @@ -548,6 +548,7 @@ def emit_body_blocks(): elif isinstance(ir.value, str) and ir.value in symbols: return symbols[ir.value] elif ir.is_literal: + # return ctx.get_basic_block().append_instruction("store", IRLiteral(ir.value)) return IRLiteral(ir.value) else: raise Exception(f"Unknown IR node: {ir}") diff --git a/vyper/venom/passes/dft.py b/vyper/venom/passes/dft.py index 5d149cf003..87717e7260 100644 --- a/vyper/venom/passes/dft.py +++ b/vyper/venom/passes/dft.py @@ -4,6 +4,8 @@ from vyper.venom.function import IRFunction from vyper.venom.passes.base_pass import IRPass +COMMUTATIVE_OPS = frozenset(["mul", "add", "xor", "or", "and", "eq"]) + class DFTPass(IRPass): inst_order: dict[IRInstruction, int] @@ -37,6 +39,12 @@ def _process_instruction_r(self, bb: IRBasicBlock, inst: IRInstruction, offset: self.inst_order[inst] = 0 return + if inst.opcode in COMMUTATIVE_OPS and all(isinstance(t, IRVariable) for t in inst.operands): + liveness_order = list(inst.liveness) + # higher index in liveness_order means shorter time to live + ttl = lambda item: -liveness_order.index(item) # noqa: E731 + inst.operands.sort(key=ttl) + for op in inst.get_inputs(): target = self.dfg.get_producing_instruction(op) assert target is not None, f"no producing instruction for {op}"