feat[venom]: improvements for iszero handling and memmerge#4469
Merged
charles-cooper merged 24 commits intovyperlang:masterfrom Apr 7, 2025
Merged
Conversation
Codecov ReportAttention: Patch coverage is
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. 🚀 New features to boost your workflow:
|
vyper/venom/passes/memmerging.py
Outdated
| 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) |
| 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" |
Member
There was a problem hiding this comment.
this is only valid if prev_inst has no other uses
instruction that can benefit from inserting iszero
|
|
||
| # for these instruction exist optimization that | ||
| # could benefit from iszero | ||
| def iszero_can_help(inst: IRInstruction) -> bool: |
Member
There was a problem hiding this comment.
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"]: |
Member
There was a problem hiding this comment.
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]] |
Member
There was a problem hiding this comment.
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]] |
used iterator instead of list Co-authored-by: Charles Cooper <[email protected]>
comment update Co-authored-by: Charles Cooper <[email protected]>
charles-cooper
approved these changes
Apr 7, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What I did
How I did it
How to verify it
Commit message
Description for the changelog
Cute Animal Picture