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

feat[venom]: add codesize optimization pass #4333

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

charles-cooper
Copy link
Member

@charles-cooper charles-cooper commented Oct 27, 2024

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

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

@charles-cooper charles-cooper marked this pull request as ready for review October 27, 2024 14:38
Comment on lines +12 to +15
i = 0
while i < len(bb.instructions):
inst = bb.instructions[i]
i += 1
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
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):
Copy link
Collaborator

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
Copy link
Collaborator

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?

Comment on lines +41 to +46
inst.opcode = "shl"
# sanity check
assert (val >> ix) << ix == val, val
assert (val >> ix) & 1 == 1

inst.operands = [IRLiteral(val >> ix), IRLiteral(ix)]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
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):
Copy link
Collaborator

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?

Copy link
Collaborator

@harkal harkal left a 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.

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