-
-
Notifications
You must be signed in to change notification settings - Fork 801
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
feat[venom]: add codesize optimization pass #4333
base: master
Are you sure you want to change the base?
feat[venom]: add codesize optimization pass #4333
Conversation
i = 0 | ||
while i < len(bb.instructions): | ||
inst = bb.instructions[i] | ||
i += 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i = 0 | |
while i < len(bb.instructions): | |
inst = bb.instructions[i] | |
i += 1 | |
for inst in bb.instructions: |
|
||
val = op.value | ||
|
||
if val == (2**256 - 1): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make use of constants that we have for numbers like these
|
||
# transform things like 0xffff...01 to (not 0xfe) | ||
binz = bin(val)[2:] | ||
if (ix := binz.find("0")) > 8: # `not` is 1 byte |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe, extract 8 and 24 to constants like BITS_FOR_SHL
and BITS_FOR_NOT
?
inst.opcode = "shl" | ||
# sanity check | ||
assert (val >> ix) << ix == val, val | ||
assert (val >> ix) & 1 == 1 | ||
|
||
inst.operands = [IRLiteral(val >> ix), IRLiteral(ix)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
inst.opcode = "shl" | |
# sanity check | |
assert (val >> ix) << ix == val, val | |
assert (val >> ix) & 1 == 1 | |
inst.operands = [IRLiteral(val >> ix), IRLiteral(ix)] | |
# sanity check | |
assert (val >> ix) << ix == val, val | |
assert (val >> ix) & 1 == 1 | |
inst.opcode = "shl" | |
inst.operands = [IRLiteral(val >> ix), IRLiteral(ix)] |
for bb in self.function.get_basic_blocks(): | ||
self._process_bb(bb) | ||
|
||
def _process_bb(self, bb): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For readability, it might be a good thing to add comments to "separate" the three rules the processing applies, or make them separate methods?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have made some comments in the code above. Additionally, we could add some tests for the optimization cases.
What I did
basic codesize optimization which strengthens large literals to evm computations which are shorter
How I did it
How to verify it
saves 450 bytes (2.5%) on https://gist.github.com/charles-cooper/eed70340aee2a47478ca0fc2ea6d5140
Commit message
Commit message for the final, squashed PR. (Optional, but reviewers will appreciate it! Please see our commit message style guide for what we would ideally like to see in a commit message.)
Description for the changelog
Cute Animal Picture