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

Adjust Yul optimizer to rematerialize zero literals. #14225

Closed
Tracked by #14073
NunoFilipeSantos opened this issue May 15, 2023 · 1 comment · Fixed by #14469
Closed
Tracked by #14073

Adjust Yul optimizer to rematerialize zero literals. #14225

NunoFilipeSantos opened this issue May 15, 2023 · 1 comment · Fixed by #14469
Assignees

Comments

@NunoFilipeSantos
Copy link
Contributor

No description provided.

@NunoFilipeSantos NunoFilipeSantos mentioned this issue May 15, 2023
12 tasks
@ekpyron ekpyron changed the title Adjust Yul optimizer to never CSE zeroes. (can be done in a later step after 0.8.20, if need be) Adjust Yul optimizer to never CSE zeroes. May 15, 2023
@ekpyron
Copy link
Member

ekpyron commented May 15, 2023

The high-level task here is:
We never want to produce bytecode that uses a DUP[N] opcode for dupping up a constant literal zero, since PUSH0 is cheaper than any DUP.

We already have the LiteralRematerializer in the optimizer (part of Rematerializer.cpp), which replaces all variables with their literal value and which occurs as T in the optimizer sequence.

However, after literal rematerialization we run the Common Subexpression Eliminator again, which will reverse this, i.e. which will replace multiple uses of a variable with the same constant value by multiple references to that constant.
The CSE already has a special case for not doing this for return variables; see the comment

				// Prevent using the default value of return variables
				// instead of literal zeros.

in CommonSubexpressionEliminator.cpp.

The easy fix for this issue is to generalize that logic to all occurrences of zero, in case the EVM version supports PUSH0 (which can be queried from the EVMVersion which should be accessible via the Yul dialect in the optimizer step).

We should then check if any other optimizer steps suffer due to this, i.e. if the data flow analyzer, etc., keep working as intended with this change. I'd expect everything will be fine, but if we see unexpected gas cost increases or the optimizer fails to simplify anything, we'll need to dig into that.

@ekpyron ekpyron moved this from To do to In Progress in Solidity Focus Board Jun 13, 2023
@NunoFilipeSantos NunoFilipeSantos moved this from In Progress to To do in Solidity Focus Board Jul 17, 2023
@nikola-matic nikola-matic changed the title Adjust Yul optimizer to never CSE zeroes. Adjust Yul optimizer to rematerialize zero literals. Aug 7, 2023
@github-project-automation github-project-automation bot moved this from To do to Done in Solidity Focus Board Aug 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
3 participants