Mask generation optimization
- Dominant language
- C++
- Stars
- 25.7k
- Forks
- 6.2k
- Avg merge
- 2d 19h
- Merged PRs (30d)
- 29
Description
## Abstract
I have a large contract compiled with `--via-ir --optimize --optimize-runs 2`
Looking at the generated opcodes I see the following mask generation patterns that can be optimized:
I've implemented a change to the constant optimizer for this here: https://github.com/ethereum/solidity/compare/develop...moh-eulith:solidity:mask_generation
May I create a PR from the above?
1) Mask generation
15 instances of `PUSH1 0x1 PUSH1 0x1 PUSH1 0x[0-9A-F]* SHL SUB NOT`
(sub case of the next one)
-> `PUSH0 NOT PUSH1 $bits SHL` (saves 4 bytes)
247 instances of `PUSH1 0x1 PUSH1 0x1 PUSH1 0x[0-9A-F]* SHL SUB`, saves 3*247 bytes
`PUSH1 0x1 PUSH1 0x1 PUSH1 0x40 SHL SUB` : 8 bytes, 3 + 3 + 3 + 3 + 3 = 15 gas
-> `PUSH0 NOT PUSH1 0xC0 SHR`: 5 bytes, 2 + 3 + 3 + 3 = 11 gas
2) Shifted mask generation
53 instances of `PUSH1 0x1 PUSH1 0x[0-9A-F]* SHL PUSH1 0x1 PUSH1 0x[0-9A-F]* SHL SUB`: saves 3*53
`PUSH1 0x1 PUSH1 0x40 SHL PUSH1 0x1 PUSH1 0x80 SHL SUB`: 11 opcodes
-> `PUSH0 NOT PUSH1 0xC0 SHR PUSH1 0x40 SHL`: 8 opcodes
## Motivation
Reduce both binary size and gas.
## Specification
just an optimizer change, so no spec changes.
## Backwards Compatibility
just an optimizer change, so backwards compatible.
Contributor guide
Research direction
Start with the constant optimizer and compare its behavior with the referenced mask_generation branch. Verify that the listed mask and shifted-mask opcode patterns are replaced while preserving behavior, and check the stated byte-size and gas reductions in generated opcodes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, solidity
- Domain
- compilers, performance
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100