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

wip - reorder commutative ops #3952

Closed
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions vyper/venom/ir_node_to_venom.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ def emit_body_blocks():

return _global_symbols.get(ir.value) or symbols.get(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}")
Expand Down
8 changes: 8 additions & 0 deletions vyper/venom/passes/dft.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
function: IRFunction
Expand Down Expand Up @@ -40,6 +42,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}"
Expand Down
Loading