[Yul Optimizer] Extend LoadResolver to assume zeroed storage during creation code. (And zeroed memory at transaction start.)
- Dominant language
- C++
- Stars
- 25.7k
- Forks
- 6.2k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 21
Description
Consider the following contract:
```
contract C {
bytes x = "0123";
}
```
The creation code of ``C`` has to initialize the state variable ``x``. However, in order to do so, it first *reads* the length field from storage, resulting in ``--ir-optimized --optimize`` of above contract to produce:
```
object "C_4" {
code {
{
let _1 := memoryguard(0x80)
mstore(64, _1)
if callvalue() { revert(0, 0) }
let _2 := 0x00
let _3 := sload( _2) // We can assume ``sload(_2)`` to return 0!
let length := _2
... rest of creation code....
}
object "C_4_deployed" {
code {
{
revert(0, 0)
}
}
}
}
```
Since at creation time, all storage slots are zero, we can resolve ``sload(_2)`` in this snippet to plain ``0``.
This optimization should be done in the ``LoadResolver`` optimizer step, by enriching it with the knowledge that all storage is zero at creation time. To that end, ``LoadResolver`` will need to determine whether it is run on creation code - this can for example be done by checking if it is run on a Yul object with a name that is not suffixed with ``_deployed``.
Similarly, in a separate step, we can extend the ``LoadResolver`` to also assume that all memory is zero at the begin of each transaction. Note that this should be done separately, so there's two sub-tasks here:
- [ ] Amend the optimizer to assume zero-valued storage at the beginning of creation code.
- [ ] Amend the optimizer to assume zero-valued memory at the beginning of each transaction.
Contributor guide
Research direction
Start by locating the Yul optimizer's LoadResolver step and the logic that identifies creation-code objects versus deployed objects. Verify the existing optimizer tests for storage loads, then cover the creation-code case so zero-valued storage is assumed; treat zero-valued memory at transaction start as a separate follow-up, as requested.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, solidity
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100