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

Equal store eliminator. #12272

Merged
merged 4 commits into from
Jan 5, 2022
Merged

Equal store eliminator. #12272

merged 4 commits into from
Jan 5, 2022

Conversation

chriseth
Copy link
Contributor

This removes subsequent stores to the same location with the same value, even if reads happen in between.

@chriseth
Copy link
Contributor Author

Partly solves #12211

@chriseth chriseth force-pushed the equalStoreEliminator branch 2 times, most recently from cc466dc to a528ffa Compare November 11, 2021 17:23
@@ -38,12 +38,12 @@ contract c {
// compileViaYul: also
// ----
// set(uint256): 7 -> true
// gas irOptimized: 110011
// gas irOptimized: 110119
Copy link
Member

Choose a reason for hiding this comment

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

Seems to be working 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🤷

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The assembly is actually shorter. My guess is that the removed mstore instructions makes it impossible for the opcode-based optimizer to remove the multiple keccak calls...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

--- /tmp/ir_old.yul	2022-01-03 15:46:30.242337855 +0100
+++ /tmp/ir_opt.yul	2022-01-03 15:41:35.197221133 +0100
@@ -79,34 +79,26 @@
                 mstore(_1, var_k)
                 mstore(0x20, _1)
                 sstore(keccak256(_1, 0x40), 0x01)
-                mstore(_1, var_k)
-                mstore(0x20, _1)
                 sstore(add(keccak256(_1, 0x40), 0x01), 0x03)
-                mstore(_1, var_k)
-                mstore(0x20, _1)
                 sstore(add(keccak256(_1, 0x40), 2), 0x04)
-                mstore(_1, var_k)
-                mstore(0x20, _1)
                 sstore(add(keccak256(_1, 0x40), 0x03), 2)
                 var := 0x01
             }
             function fun_copy(var_from, var_to) -> var_
             {
-                let _1 := 0x00
-                mstore(_1, var_from)
-                mstore(0x20, _1)
-                let dataSlot := keccak256(_1, 0x40)
-                mstore(_1, var_to)
-                mstore(0x20, _1)
-                let dataSlot_1 := keccak256(_1, 0x40)
+                mstore(0x00, var_from)
+                mstore(0x20, 0x00)
+                let dataSlot := keccak256(0x00, 0x40)
+                mstore(0x00, var_to)
+                let dataSlot_1 := keccak256(0x00, 0x40)
                 if iszero(eq(dataSlot_1, dataSlot))
                 {
                     sstore(dataSlot_1, sload(dataSlot))
                     let memberSlot := add(dataSlot_1, 1)
-                    let _2 := add(dataSlot, 1)
-                    if iszero(eq(memberSlot, _2))
+                    let _1 := add(dataSlot, 1)
+                    if iszero(eq(memberSlot, _1))
                     {
-                        sstore(memberSlot, sload(_2))
+                        sstore(memberSlot, sload(_1))
                         sstore(add(dataSlot_1, 2), sload(add(dataSlot, 2)))
                     }
                     sstore(add(dataSlot_1, 3), sload(add(dataSlot, 3)))
@@ -119,18 +111,12 @@
                 mstore(_1, var_k)
                 mstore(0x20, _1)
                 var_a := sload(keccak256(_1, 0x40))
-                mstore(_1, var_k)
-                mstore(0x20, _1)
                 var_x := sload(add(keccak256(_1, 0x40), 1))
-                mstore(_1, var_k)
-                mstore(0x20, _1)
                 var_y := sload(add(keccak256(_1, 0x40), 2))
-                mstore(_1, var_k)
-                mstore(0x20, _1)
                 var_c := sload(add(keccak256(_1, 0x40), 3))
             }
         }
-        data ".metadata" hex"a36469706673582212201575720a9612338487e4ea44beef0f99559f8ddb767d3b3a8f88b30e8203f9ee6c6578706572696d656e74616cf564736f6c634300080c0041"
+        data ".metadata" hex"a36469706673582212201300a8828ade0dfe77d7dbd65f00b07037165a285fa1ba1a0c986a59b74ec9b66c6578706572696d656e74616cf564736f6c634300080c0041"
     }
 }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The main change is that zero constants are now inlined, so I would say it's not an issue.

Copy link
Member

Choose a reason for hiding this comment

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

Should perhaps implement evaluating keccak for multi words (at least 2 words).

using namespace solidity::evmasm;
using namespace solidity::yul;

void EqualStoreEliminator::run(OptimiserStepContext& _context, Block& _ast)
Copy link
Contributor

Choose a reason for hiding this comment

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

As far as I can tell we only access the const member dialect for context. Should the parameter be a const ref maybe?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a standard interface, but I can check.

// isSimpleStore only returns something if the arguments are identifiers.
if (ExpressionStatement const* expression = get_if<ExpressionStatement>(&_statement))
{
if (auto vars = isSimpleStore(StoreLoadLocation::Storage, *expression))
Copy link
Contributor

Choose a reason for hiding this comment

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

Not related to the PR changes, but the name isSimpleStore is very confusing given that it returns two variables. Without reading the docs to that function I'd have no idea what's going on here...

Copy link
Contributor

Choose a reason for hiding this comment

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

Something like extractStoreArguments might be more understandable?

Copy link
Member

Choose a reason for hiding this comment

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

I think that removing auto might help.

Too bad that a destructuring assignment like auto [location, value] = won't work here due to the optional.

using ASTModifier::visit;
void visit(Statement& _statement) override;

void tryResolve(
Copy link
Contributor

Choose a reason for hiding this comment

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

Are the functions tryResolve and tryEvaluateKecak implemented and called anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I think that was a copy-past-leftover.

Copy link
Member

@cameel cameel left a comment

Choose a reason for hiding this comment

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

Documentation for the new step is missing.

Also maybe a few more tests beyond the one from the issue might be useful. We can see the effect in full suite tests but I think this also needs some dedicated ones.

libyul/optimiser/EqualStoreEliminator.h Show resolved Hide resolved
Comment on lines +65 to +59
if (*currentValue == vars->second)
m_pendingRemovals.insert(&_statement);
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this condition a bit too simple?

The docstring says that this is best used when there are no literal arguments. So let's take this code:

    mstore(0, 0)
    mstore(0, 0)

ExpressionSplitter seems to remove literal arguments and it turns the code to this:

    let _1 := 0
    let _2 := 0
    mstore(_2, _1)
    let _3 := 0
    let _4 := 0
    mstore(_4, _3)

but it this would defeat this optimization.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you also need to call the CSE to make this work properly, but I'm actually currently wondering whether we check somewhere that the value is an ssa variable with a movable value, this should be another condition.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah no, it's fine - we only replace variables by variables and we clear everything whenever there was an assignment to the variable.

Copy link
Member

Choose a reason for hiding this comment

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

Can you mention it in the docstring? A comment in this function would not hurt either. This kind of assumption is not easy to notice other than from the fact that the code looks way too simple for that it does so there must be some "catch" somewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What exactly do you want me to mention? These are all properties of the base class.

Comment on lines 240 to 244
{"equalStoreEliminator", [&]() {
disambiguate();
EqualStoreEliminator::run(*m_context, *m_ast);
}},
Copy link
Member

Choose a reason for hiding this comment

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

ForLoopInitRewriter is listed in the docstring as a dependency too.

* Optimisation stage that removes mstore and sstore operations if they store the same
* value that is already known to be in that slot.
*
* Works best if the code is in SSA form - without literal arguments.
Copy link
Member

Choose a reason for hiding this comment

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

I'd make it more explicit. "Works best" does not really tell me what I can expect if my code does not satisfy this.

Suggested change
* Works best if the code is in SSA form - without literal arguments.
* Does not optimize parts of code with variables not in SSA form or with calls with literal arguments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But that is exactly what is written there, isn't it? We use this wording in all the other steps as well.

Copy link
Member

@hrkrshnn hrkrshnn left a comment

Choose a reason for hiding this comment

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

Looks good. I'll commit a couple of tests that I wrote locally while testing this.

Edit: also updated the failing chromosome test.

@@ -23,6 +23,7 @@

#include <libyul/optimiser/Semantics.h>
#include <libyul/optimiser/OptimiserStep.h>
#include <libyul/optimiser/OptimizerUtilities.h>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
#include <libyul/optimiser/OptimizerUtilities.h>

Copy link
Member

Choose a reason for hiding this comment

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

Actually, will update this myself.

@hrkrshnn hrkrshnn force-pushed the equalStoreEliminator branch 4 times, most recently from 3c64328 to 8a4821e Compare January 3, 2022 12:03
@@ -178,7 +178,7 @@ contract DepositContract is IDepositContract, ERC165 {
// compileViaYul: also
// ----
// constructor()
// gas irOptimized: 1558001
// gas irOptimized: 1557137
Copy link
Member

Choose a reason for hiding this comment

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

diff -u /tmp/develop.yul /tmp/eq.yul
--- /tmp/develop.yul	2022-01-03 17:32:01.880093589 +0530
+++ /tmp/eq.yul	2022-01-03 17:31:28.243155455 +0530
@@ -546,7 +546,6 @@
                 calldatacopy(pos, value0, value1)
                 let _1 := add(pos, value1)
                 mstore(_1, 0)
-                mstore(_1, 0)
                 end := add(_1, 16)
             }
             function calldata_array_index_range_access_bytes_calldata_3157(offset, length) -> offsetOut, lengthOut
@@ -572,8 +571,6 @@
                 let _1 := add(pos, value1)
                 mstore(_1, /** @src 0:6663:6664  "0" */ 0x00)
                 /// @src 0:4559:9399  "contract DepositContract is IDepositContract, ERC165 {..."
-                mstore(_1, /** @src 0:6663:6664  "0" */ 0x00)
-                /// @src 0:4559:9399  "contract DepositContract is IDepositContract, ERC165 {..."
                 end := add(_1, 32)
             }
             function abi_encode_packed_bytes32_bytes_calldata(pos, value0, value1, value2) -> end
@@ -937,7 +934,7 @@
                 mstore8(memory_array_index_access_bytes_3175(memPtr), /** @src 0:9377:9390  "bytesValue[0]" */ byte(/** @src -1:-1:-1 */ 0, /** @src 0:9377:9390  "bytesValue[0]" */ _1))
             }
         }
-        data ".metadata" hex"a364697066735822122006c8a10f3f04d8c69b52af5dbc3a82224acdfaa0224b0f3ed0cf38c99a4f0c7e6c6578706572696d656e74616cf564736f6c637823302e382e31322d63692e323032312e362e32332b636f6d6d69742e36383439373734620062"
+        data ".metadata" hex"a3646970667358221220a5a5d6e1a1822c1fcf45d9e6ac103252bac21d4af4c04b1a690185ad3147faf46c6578706572696d656e74616cf564736f6c63782b302e382e31322d646576656c6f702e323032322e312e332b636f6d6d69742e33633634333238612e6d6f64006a"
     }
 }
 

Diff finished.  Mon Jan  3 17:32:12 2022

Here's the diff!

Copy link
Member

@hrkrshnn hrkrshnn left a comment

Choose a reason for hiding this comment

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

Going to fix it myself.

Comment on lines 33 to 34
#include <libevmasm/GasMeter.h>
#include <libsolutil/Keccak256.h>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
#include <libevmasm/GasMeter.h>
#include <libsolutil/Keccak256.h>

#include <libevmasm/GasMeter.h>
#include <libsolutil/Keccak256.h>

#include <limits>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
#include <limits>

#include <libyul/optimiser/CallGraphGenerator.h>
#include <libyul/optimiser/OptimizerUtilities.h>
#include <libyul/optimiser/Semantics.h>
#include <libyul/optimiser/CallGraphGenerator.h>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
#include <libyul/optimiser/CallGraphGenerator.h>

#include <libyul/optimiser/OptimizerUtilities.h>
#include <libyul/optimiser/Semantics.h>
#include <libyul/optimiser/CallGraphGenerator.h>
#include <libyul/SideEffects.h>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
#include <libyul/SideEffects.h>

@hrkrshnn hrkrshnn force-pushed the equalStoreEliminator branch from a88709c to 40d2719 Compare January 4, 2022 05:41
hrkrshnn
hrkrshnn previously approved these changes Jan 4, 2022
Copy link
Member

@hrkrshnn hrkrshnn left a comment

Choose a reason for hiding this comment

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

LGTM! Added a couple more tests and removed some more unused headers in the last commit. Feel free to squash the commits.

Would be nice to get a yes from @bshastry before merging, with regard to changes in fuzzer.

@hrkrshnn hrkrshnn force-pushed the equalStoreEliminator branch from a60bef5 to 85d1a57 Compare January 4, 2022 07:57
Copy link
Contributor

@bshastry bshastry left a comment

Choose a reason for hiding this comment

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

LGTM wrt fuzzer once #12276 is merged

@chriseth chriseth merged commit c16867c into develop Jan 5, 2022
@chriseth chriseth deleted the equalStoreEliminator branch January 5, 2022 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants