-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Pattern PUSH0 SHR appears in optimized code #15024
Comments
@ekpyron This is what I mentioned in our chat, it should be reproducible from this |
note: there could be considerations that this optimization could make an execution diff if the stack had no items on it prior to the |
This seems to be an issue only in the legacy pipeline. With IR pipelineIRsolc test.sol --debug-info none --optimize --optimize-runs 200000 --ir | grep shr shr(224, value)
shr(0, value) Optimized IRsolc test.sol --debug-info none --optimize --optimize-runs 200000 --ir-optimized | grep shr if eq(0x5ec01e4d, shr(224, calldataload(0))) Assemblysolc test.sol --debug-info none --optimize --optimize-runs 200000 --asm --via-ir | grep shr --context=1
legacyAssemblysolc test.sol --debug-info none --optimize --optimize-runs 200000 --asm | grep shr --context=1
|
Legacy and via-IR share the same set of rules here, though... I'd have guessed that we generate a |
I looked into this and found out that the problem also happens with evm versions prior to solc test.sol --evm-version paris --debug-info none --optimize --optimize-runs 200000 --asm | grep shr --context=1 also generates the pattern:
I then investigated the Common Subexpression Eliminator (CSE) and found out that the optimizer can successfully identify and replace the pattern BOOST_AUTO_TEST_CASE(cse_replace_shr_push_value0_any)
{
AssemblyItems input {
AssemblyItem(20),
Instruction::KECCAK256,
AssemblyItem(0),
Instruction::SHR,
Instruction::JUMP
};
AssemblyItems output {
AssemblyItem(20),
Instruction::KECCAK256,
Instruction::JUMP
};
checkCSE(input, output);
checkFullCSE(input, output);
}
BOOST_AUTO_TEST_CASE(cse_replace_shr_push0_instruction_any)
{
AssemblyItems input {
AssemblyItem(20),
Instruction::KECCAK256,
Instruction::PUSH0,
Instruction::SHR,
Instruction::JUMP
};
AssemblyItems output {
AssemblyItem(20),
Instruction::KECCAK256,
Instruction::PUSH0,
Instruction::SHR,
Instruction::JUMP
};
checkCSE(input, output);
checkFullCSE(input, output);
} The problem occurs in the specific example provided in this issue, because when the CSE optimizes the block (between two control flow breaking instructions) in which the original asm items (size 24)
optimized asm items (size 24)
|
Description
Optimizer does not optimize out
PUSH0 SHR
instructions ( x >> 0 is always x)Environment
solc/solc0.8.23 -o build/examples/push0shr.sol/ --overwrite --ast-compact-json --asm --bin-runtime --bin --optimize --optimize-runs 200000 --abi --combined-json=srcmap-runtime,generated-sources-runtime examples/push0shr.so
Steps to Reproduce
compile the following example with the above command:
The pattern appears to be generated by the source location s:l:f = (207, 97, 0)
The text was updated successfully, but these errors were encountered: