Skip to content

feat[venom]: improvements for iszero handling and memmerge#4469

Merged
charles-cooper merged 24 commits intovyperlang:masterfrom
HodanPlodky:feat/venom/algebraicopt-and-memmerge-update
Apr 7, 2025
Merged

feat[venom]: improvements for iszero handling and memmerge#4469
charles-cooper merged 24 commits intovyperlang:masterfrom
HodanPlodky:feat/venom/algebraicopt-and-memmerge-update

Conversation

@HodanPlodky
Copy link
Copy Markdown
Collaborator

@HodanPlodky HodanPlodky commented Feb 6, 2025

What I did

How I did it

How to verify it

Commit message

Updates for `AlgebraicOptimizationPass` and `BranchOptimizationPass`
for handling interaction between conditionals and `iszero` instruction
better. Update for `MemMergePass` to handle `dload`/`mstore` pairs
merging into `dloadbytes` instruction even when the src and dst
pointers are not literal.

Description for the changelog

Cute Animal Picture

Put a link to a cute animal picture inside the parenthesis-->

@codecov
Copy link
Copy Markdown

codecov bot commented Feb 6, 2025

Codecov Report

Attention: Patch coverage is 92.30769% with 4 lines in your changes missing coverage. Please review.

Project coverage is 92.56%. Comparing base (0e737fc) to head (fb49f19).
Report is 89 commits behind head on master.

Files with missing lines Patch % Lines
vyper/venom/passes/algebraic_optimization.py 86.66% 1 Missing and 1 partial ⚠️
vyper/venom/passes/memmerging.py 90.47% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4469      +/-   ##
==========================================
- Coverage   92.57%   92.56%   -0.01%     
==========================================
  Files         123      123              
  Lines       17456    17494      +38     
  Branches     2946     2957      +11     
==========================================
+ Hits        16160    16194      +34     
- Misses        894      896       +2     
- Partials      402      404       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

self._handle_bb_memzero(bb)
self._handle_bb(bb, "calldataload", "calldatacopy", allow_dst_overlaps_src=True)
self._handle_bb(bb, "dload", "dloadbytes", allow_dst_overlaps_src=True)
self._handle_bb(bb, "dload", "codecopy", allow_dst_overlaps_src=True)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems sus

prev_inst = self.dfg.get_producing_instruction(cond)
if cost_a >= cost_b and prev_inst.opcode == "iszero":
if cost_a >= cost_b and prev_inst.opcode == "eq":
prev_inst.opcode = "xor"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is only valid if prev_inst has no other uses

@HodanPlodky HodanPlodky marked this pull request as ready for review February 21, 2025 11:37

# for these instruction exist optimization that
# could benefit from iszero
def iszero_can_help(inst: IRInstruction) -> bool:
Copy link
Copy Markdown
Member

@charles-cooper charles-cooper Mar 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: rename to prefer_iszero (consistent with naming in algebraic optimization pass)

def prefer_iszero(inst: IRInstruction) -> bool:
if inst.opcode == "eq":
return True
if inst.opcode in ["gt", "lt"]:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also sgt, slt

Suggested change
if inst.opcode in ["gt", "lt"]:
if inst.opcode in COMPARATOR_INSTRUCTIONS:

Comment on lines +40 to +44
elif cost_a >= cost_b and prefer_iszero(prev_inst):
new_cond = fn.get_next_variable()
inst = IRInstruction("iszero", [term_inst.operands[0]], output=new_cond)
bb.insert_instruction(inst, index=-1)
term_inst.operands = [new_cond, term_inst.operands[2], term_inst.operands[1]]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the code for this branch is duplicated with the next branch, so we can just fix the condition.

Suggested change
elif cost_a >= cost_b and prefer_iszero(prev_inst):
new_cond = fn.get_next_variable()
inst = IRInstruction("iszero", [term_inst.operands[0]], output=new_cond)
bb.insert_instruction(inst, index=-1)
term_inst.operands = [new_cond, term_inst.operands[2], term_inst.operands[1]]
elif cost_a > cost_b or (cost_a >= cost_b and prefer_iszero(prev_inst)):
new_cond = fn.get_next_variable()
inst = IRInstruction("iszero", [term_inst.operands[0]], output=new_cond)
bb.insert_instruction(inst, index=-1)
term_inst.operands = [new_cond, term_inst.operands[2], term_inst.operands[1]]

@charles-cooper charles-cooper changed the title feat[venom]: updates for algebraic opts and memmerge feat[venom]: improvements for iszero handling and memmerge Apr 7, 2025
@charles-cooper charles-cooper enabled auto-merge (squash) April 7, 2025 18:22
@charles-cooper charles-cooper merged commit 4463878 into vyperlang:master Apr 7, 2025
159 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants