From d400c44007b6579f2da61bb57104c47f32fbdf9e Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 14 Dec 2017 17:38:32 +0100 Subject: [PATCH 01/10] Description of variable eliminator. --- libjulia/optimiser/README.md | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/libjulia/optimiser/README.md b/libjulia/optimiser/README.md index 771cb7071..24ee429c1 100644 --- a/libjulia/optimiser/README.md +++ b/libjulia/optimiser/README.md @@ -54,8 +54,27 @@ As an example, neither ``mload`` nor ``mstore`` would be allowed. ## Full Function Inliner -## Variable Eliminator +## Rematerialisation + +The rematerialisation stage tries to replace variable references by the expression that +was last assigned to the variable. This is of course only beneficial if this expression +is comparatively cheap to evaluate. Furthermore, it is only semantically equivalent if +the value of the expression did not change between the point of assignment and the +point of use. The main benefit of this stage is that it can save stack slots if it +leads to a variable being eliminated completely (see below), but it can also +save a DUP opcode on the EVM if the expression is very cheap. + +The algorithm only allows movable expressions (see above for a definition) in this case. +Expressions that contain other variables are also disallowed if one of those variables +have been assigned to in the meantime. This is also not applied to variables where +assignment and use span across loops and conditionals. + +## Unused Definition Pruner + +If a variable or function is not referenced, it is removed from the code. +If there are two assignments to a variable where the first one is a movable expression +and the variable is not used between the two assignments (and the second is not inside +a loop or conditional, the first one is not inside), the first assignment is removed. -## Unused Declaration Pruner ## Function Unifier From 016fb18ef83987c95ea3655afb1a8fd5ff6e3950 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 15 Dec 2017 11:55:18 +0100 Subject: [PATCH 02/10] Rematerialisation. --- libjulia/optimiser/ASTWalker.cpp | 2 +- libjulia/optimiser/NameCollector.cpp | 6 ++ libjulia/optimiser/NameCollector.h | 14 +++ libjulia/optimiser/Rematerialiser.cpp | 143 ++++++++++++++++++++++++++ libjulia/optimiser/Rematerialiser.h | 64 ++++++++++++ 5 files changed, 228 insertions(+), 1 deletion(-) create mode 100644 libjulia/optimiser/Rematerialiser.cpp create mode 100644 libjulia/optimiser/Rematerialiser.h diff --git a/libjulia/optimiser/ASTWalker.cpp b/libjulia/optimiser/ASTWalker.cpp index 6386b29dc..034449849 100644 --- a/libjulia/optimiser/ASTWalker.cpp +++ b/libjulia/optimiser/ASTWalker.cpp @@ -86,8 +86,8 @@ void ASTWalker::operator()(ForLoop const& _for) { (*this)(_for.pre); visit(*_for.condition); - (*this)(_for.post); (*this)(_for.body); + (*this)(_for.post); } void ASTWalker::operator()(Block const& _block) diff --git a/libjulia/optimiser/NameCollector.cpp b/libjulia/optimiser/NameCollector.cpp index f94104b75..510ee2897 100644 --- a/libjulia/optimiser/NameCollector.cpp +++ b/libjulia/optimiser/NameCollector.cpp @@ -67,3 +67,9 @@ map ReferencesCounter::countReferences(Expression const& _expres counter.visit(_expression); return counter.references(); } + +void Assignments::operator()(Assignment const& _assignment) +{ + for (auto const& var: _assignment.variableNames) + m_names.insert(var.name); +} diff --git a/libjulia/optimiser/NameCollector.h b/libjulia/optimiser/NameCollector.h index 7fe386f72..2d4a1d4bf 100644 --- a/libjulia/optimiser/NameCollector.h +++ b/libjulia/optimiser/NameCollector.h @@ -66,5 +66,19 @@ private: std::map m_references; }; +/** + * Specific AST walker that finds all variables that are assigned to. + */ +class Assignments: public ASTWalker +{ +public: + using ASTWalker::operator (); + virtual void operator()(Assignment const& _assignment) override; + + std::set const& names() const { return m_names; } +private: + std::set m_names; +}; + } } diff --git a/libjulia/optimiser/Rematerialiser.cpp b/libjulia/optimiser/Rematerialiser.cpp new file mode 100644 index 000000000..ca5f94b06 --- /dev/null +++ b/libjulia/optimiser/Rematerialiser.cpp @@ -0,0 +1,143 @@ +/*( + This file is part of solidity. + + solidity is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + solidity is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with solidity. If not, see . +*/ +/** + * Optimisation stage that replaces variables by their most recently assigned expressions. + */ + +#include + +#include +#include + +#include + +#include + +#include + +using namespace std; +using namespace dev; +using namespace dev::julia; + +void Rematerialiser::operator()(Assignment& _assignment) +{ + set names; + for (auto const& var: _assignment.variableNames) + names.insert(var.name); + handleAssignment(names, _assignment.value.get()); +} + +void Rematerialiser::operator()(VariableDeclaration& _varDecl) +{ + set names; + for (auto const& var: _varDecl.variables) + names.insert(var.name); + handleAssignment(names, _varDecl.value.get()); +} + +void Rematerialiser::operator()(If& _if) +{ + ASTModifier::operator()(_if); + + Assignments ass; + ass(_if.body); + handleAssignment(ass.names(), nullptr); +} + +void Rematerialiser::operator()(Switch& _switch) +{ + boost::apply_visitor(*this, *_switch.expression); + set assignedVariables; + for (auto& _case: _switch.cases) + { + (*this)(_case.body); + Assignments ass; + ass(_case.body); + assignedVariables += ass.names(); + // This is a little too destructive, we could retain the old replacements. + handleAssignment(ass.names(), nullptr); + } + handleAssignment(assignedVariables, nullptr); +} + +void Rematerialiser::operator()(ForLoop& _for) +{ + (*this)(_for.pre); + + Assignments ass; + ass(_for.body); + ass(_for.post); + handleAssignment(ass.names(), nullptr); + + visit(*_for.condition); + (*this)(_for.body); + (*this)(_for.post); + + handleAssignment(ass.names(), nullptr); +} + +void Rematerialiser::handleAssignment(set const& _variables, Expression* _value) +{ + MovableChecker movableChecker; + if (_value) + { + visit(*_value); + movableChecker.visit(*_value); + } + if (_variables.size() == 1) + { + string const& name = *_variables.begin(); + if (movableChecker.movable() && _value) + // TODO Plus heuristic about size of value + // TODO If _value is null, we could use zero. + m_substitutions[name] = _value; + else + m_substitutions.erase(name); + } + // Disallow substitutions that use a variable that will be reassigned by this assignment. + for (auto const& name: _variables) + for (auto const& ref: m_referencedBy[name]) + m_substitutions.erase(ref); + // Update the fact which variables are referenced by the newly assigned variables + for (auto const& name: _variables) + { + for (auto const& ref: m_references[name]) + m_referencedBy[ref].erase(name); + m_references[name].clear(); + } + auto const& referencedVariables = movableChecker.referencedVariables(); + for (auto const& name: _variables) + { + m_references[name] = referencedVariables; + for (auto const& ref: referencedVariables) + m_referencedBy[ref].insert(name); + } +} + +void Rematerialiser::visit(Expression& _e) +{ + if (_e.type() == typeid(Identifier)) + { + Identifier& identifier = boost::get(_e); + if (m_substitutions.count(identifier.name)) + { + string name = identifier.name; + _e = (ASTCopier{}).translate(*m_substitutions.at(name)); + } + } + ASTModifier::visit(_e); +} diff --git a/libjulia/optimiser/Rematerialiser.h b/libjulia/optimiser/Rematerialiser.h new file mode 100644 index 000000000..f2a3102ff --- /dev/null +++ b/libjulia/optimiser/Rematerialiser.h @@ -0,0 +1,64 @@ +/* + This file is part of solidity. + + solidity is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + solidity is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with solidity. If not, see . +*/ +/** + * Optimisation stage that replaces variables by their most recently assigned expressions. + */ + +#pragma once + +#include + +#include +#include +#include + +namespace dev +{ +namespace julia +{ + +/** + * Optimisation stage that replaces variables by their most recently assigned expressions. + * + * Prerequisite: Disambiguator + */ +class Rematerialiser: public ASTModifier +{ +public: + using ASTModifier::operator(); + virtual void operator()(Assignment& _assignment) override; + virtual void operator()(VariableDeclaration& _varDecl) override; + virtual void operator()(If& _if) override; + virtual void operator()(Switch& _switch) override; + virtual void operator()(ForLoop&) override; + +protected: + virtual void visit(Expression& _e) override; + +private: + void handleAssignment(std::set const& _names, Expression* _value); + + /// Substitutions to be performed, if possible. + std::map m_substitutions; + /// m_references[a].contains(b) <=> the current expression assigned to a references b + std::map> m_references; + /// m_referencedBy[b].contains(a) <=> the current expression assigned to a references b + std::map> m_referencedBy; +}; + +} +} From 30e6f69bfd1a6fd907b96060901b0962b1e9cf18 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 15 Dec 2017 13:03:47 +0100 Subject: [PATCH 03/10] Tests for rematerialiser. --- test/libjulia/Rematerialiser.cpp | 129 +++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 test/libjulia/Rematerialiser.cpp diff --git a/test/libjulia/Rematerialiser.cpp b/test/libjulia/Rematerialiser.cpp new file mode 100644 index 000000000..ae254ad93 --- /dev/null +++ b/test/libjulia/Rematerialiser.cpp @@ -0,0 +1,129 @@ +/* + This file is part of solidity. + + solidity is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + solidity is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with solidity. If not, see . +*/ +/** + * @date 2017 + * Unit tests for the rematerialiser optimizer stage. + */ + +#include + +#include + +#include + +#include + +#include +#include + +using namespace std; +using namespace dev; +using namespace dev::julia; +using namespace dev::julia::test; +using namespace dev::solidity; + + +#define CHECK(_original, _expectation)\ +do\ +{\ + assembly::AsmPrinter p;\ + Block b = disambiguate(_original, false);\ + (Rematerialiser{})(b);\ + string result = p(b);\ + BOOST_CHECK_EQUAL(result, format(_expectation, false));\ +}\ +while(false) + +BOOST_AUTO_TEST_SUITE(IuliaRematerialiser) + +BOOST_AUTO_TEST_CASE(smoke_test) +{ + CHECK("{ }", "{ }"); +} + +BOOST_AUTO_TEST_CASE(trivial) +{ + CHECK( + "{ let a := 1 let b := a mstore(0, b) }", + "{ let a := 1 let b := 1 mstore(0, 1) }" + ); +} + +BOOST_AUTO_TEST_CASE(expression) +{ + CHECK( + "{ let a := add(mul(calldatasize(), 2), number()) let b := add(a, a) }", + "{ let a := add(mul(calldatasize(), 2), number()) let b := add(" + "add(mul(calldatasize(), 2), number())," + "add(mul(calldatasize(), 2), number())" + ") }" + ); +} + +BOOST_AUTO_TEST_CASE(non_movable_instr) +{ + CHECK( + "{ let a := 1 let b := mload(a) let c := a mstore(add(a, b), c) }", + "{ let a := 1 let b := mload(1) let c := 1 mstore(add(1, b), 1) }" + ); +} + +BOOST_AUTO_TEST_CASE(non_movable_fun) +{ + CHECK( + "{ function f(x) -> y {} let a := 1 let b := f(a) let c := a mstore(add(a, b), c) }", + "{ function f(x) -> y {} let a := 1 let b := f(1) let c := 1 mstore(add(1, b), 1) }" + ); +} + +BOOST_AUTO_TEST_CASE(branches_if) +{ + CHECK( + "{ let a := 1 let b := 2 if b { pop(b) b := a } let c := b }", + "{ let a := 1 let b := 2 if 2 { pop(2) b := 1 } let c := b }" + ); +} + +BOOST_AUTO_TEST_CASE(branches_switch) +{ + CHECK( + "{ let a := 1 let b := 2 switch number() case 1 { b := a } default { let x := a let y := b b := a } pop(add(a, b)) }", + "{ let a := 1 let b := 2 switch number() case 1 { b := 1 } default { let x := 1 let y := b b := 1 } pop(add(1, b)) }" + ); +} + +BOOST_AUTO_TEST_CASE(branches_for) +{ + CHECK( + "{ let a := 1 for { pop(a) } a { pop(a) } { pop(a) } }", + "{ let a := 1 for { pop(1) } 1 { pop(1) } { pop(1) } }" + ); + CHECK( + "{ let a := 1 for { pop(a) } a { pop(a) } { a := 7 let c := a } let x := a }", + "{ let a := 1 for { pop(1) } a { pop(7) } { a := 7 let c := 7 } let x := a }" + ); +} + +BOOST_AUTO_TEST_CASE(reassignment) +{ + CHECK( + "{ let a := 1 pop(a) if a { a := 2 } let b := mload(a) pop(b) }", + "{ let a := 1 pop(1) if 1 { a := 2 } let b := mload(a) pop(b) }" + ); +} + +BOOST_AUTO_TEST_SUITE_END() From 22c4d282aae4279b37bd3d031b3baade2a3e508b Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 12 Jan 2018 16:14:59 +0100 Subject: [PATCH 04/10] Only substitute if all referenced variables are in scope. --- libjulia/optimiser/Rematerialiser.cpp | 52 +++++++++++++++++++++++++-- libjulia/optimiser/Rematerialiser.h | 9 +++++ test/libjulia/Rematerialiser.cpp | 9 +++++ 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/libjulia/optimiser/Rematerialiser.cpp b/libjulia/optimiser/Rematerialiser.cpp index ca5f94b06..09a9bb904 100644 --- a/libjulia/optimiser/Rematerialiser.cpp +++ b/libjulia/optimiser/Rematerialiser.cpp @@ -29,6 +29,8 @@ #include +#include + using namespace std; using namespace dev; using namespace dev::julia; @@ -46,6 +48,7 @@ void Rematerialiser::operator()(VariableDeclaration& _varDecl) set names; for (auto const& var: _varDecl.variables) names.insert(var.name); + m_variableScopes.back().first += names; handleAssignment(names, _varDecl.value.get()); } @@ -74,9 +77,23 @@ void Rematerialiser::operator()(Switch& _switch) handleAssignment(assignedVariables, nullptr); } +void Rematerialiser::operator()(FunctionDefinition& _fun) +{ + m_variableScopes.push_back(make_pair(set(), true)); + for (auto const& parameter: _fun.parameters) + m_variableScopes.back().first.insert(parameter.name); + for (auto const& var: _fun.returnVariables) + m_variableScopes.back().first.insert(var.name); + ASTModifier::operator()(_fun); + m_variableScopes.pop_back(); +} + void Rematerialiser::operator()(ForLoop& _for) { - (*this)(_for.pre); + // Special scope handling of the pre block. + m_variableScopes.push_back(make_pair(set(), false)); + for (auto& statement: _for.pre.statements) + visit(statement); Assignments ass; ass(_for.body); @@ -88,6 +105,17 @@ void Rematerialiser::operator()(ForLoop& _for) (*this)(_for.post); handleAssignment(ass.names(), nullptr); + + m_variableScopes.pop_back(); +} + +void Rematerialiser::operator()(Block& _block) +{ + size_t numScopes = m_variableScopes.size(); + m_variableScopes.push_back(make_pair(set(), false)); + ASTModifier::operator()(_block); + m_variableScopes.pop_back(); + solAssert(numScopes == m_variableScopes.size(), ""); } void Rematerialiser::handleAssignment(set const& _variables, Expression* _value) @@ -128,6 +156,18 @@ void Rematerialiser::handleAssignment(set const& _variables, Expression* } } +bool Rematerialiser::inScope(string const& _variableName) const +{ + for (auto const& scope: m_variableScopes | boost::adaptors::reversed) + { + if (scope.first.count(_variableName)) + return true; + if (scope.second) + return false; + } + return false; +} + void Rematerialiser::visit(Expression& _e) { if (_e.type() == typeid(Identifier)) @@ -136,7 +176,15 @@ void Rematerialiser::visit(Expression& _e) if (m_substitutions.count(identifier.name)) { string name = identifier.name; - _e = (ASTCopier{}).translate(*m_substitutions.at(name)); + bool doSubstitute = true; + for (auto const& ref: m_references[name]) + if (!inScope(ref)) + { + doSubstitute = false; + break; + } + if (doSubstitute) + _e = (ASTCopier{}).translate(*m_substitutions.at(name)); } } ASTModifier::visit(_e); diff --git a/libjulia/optimiser/Rematerialiser.h b/libjulia/optimiser/Rematerialiser.h index f2a3102ff..1accc3f66 100644 --- a/libjulia/optimiser/Rematerialiser.h +++ b/libjulia/optimiser/Rematerialiser.h @@ -44,20 +44,29 @@ public: virtual void operator()(VariableDeclaration& _varDecl) override; virtual void operator()(If& _if) override; virtual void operator()(Switch& _switch) override; + virtual void operator()(FunctionDefinition&) override; virtual void operator()(ForLoop&) override; + virtual void operator()(Block& _block) override; protected: + using ASTModifier::visit; virtual void visit(Expression& _e) override; private: void handleAssignment(std::set const& _names, Expression* _value); + /// Returns true iff the variable is in scope. + bool inScope(std::string const& _variableName) const; + /// Substitutions to be performed, if possible. std::map m_substitutions; /// m_references[a].contains(b) <=> the current expression assigned to a references b std::map> m_references; /// m_referencedBy[b].contains(a) <=> the current expression assigned to a references b std::map> m_referencedBy; + /// List of scopes, where each scope is a set of variables and a bool that tells + /// whether it is a function body (true) or not. + std::vector, bool>> m_variableScopes; }; } diff --git a/test/libjulia/Rematerialiser.cpp b/test/libjulia/Rematerialiser.cpp index ae254ad93..020f0020e 100644 --- a/test/libjulia/Rematerialiser.cpp +++ b/test/libjulia/Rematerialiser.cpp @@ -126,4 +126,13 @@ BOOST_AUTO_TEST_CASE(reassignment) ); } +BOOST_AUTO_TEST_CASE(do_not_move_out_of_scope) +{ + // Cannot replace by `let b := x` by `let b := a` since a is out of scope. + CHECK( + "{ let x { let a := sload(0) x := a } let b := x }", + "{ let x { let a := sload(0) x := a } let b := x }" + ); +} + BOOST_AUTO_TEST_SUITE_END() From e100af592b7f78166ba867bf4f9b151d3adece08 Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 1 Feb 2018 16:55:59 +0100 Subject: [PATCH 05/10] Fix for rematerializer. --- libjulia/optimiser/Rematerialiser.cpp | 4 ++++ test/libjulia/Rematerialiser.cpp | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/libjulia/optimiser/Rematerialiser.cpp b/libjulia/optimiser/Rematerialiser.cpp index 09a9bb904..bf7d7d16f 100644 --- a/libjulia/optimiser/Rematerialiser.cpp +++ b/libjulia/optimiser/Rematerialiser.cpp @@ -136,6 +136,10 @@ void Rematerialiser::handleAssignment(set const& _variables, Expression* else m_substitutions.erase(name); } + else + for (auto const& name: _variables) + m_substitutions.erase(name); + // Disallow substitutions that use a variable that will be reassigned by this assignment. for (auto const& name: _variables) for (auto const& ref: m_referencedBy[name]) diff --git a/test/libjulia/Rematerialiser.cpp b/test/libjulia/Rematerialiser.cpp index 020f0020e..d6cc0dae7 100644 --- a/test/libjulia/Rematerialiser.cpp +++ b/test/libjulia/Rematerialiser.cpp @@ -118,6 +118,18 @@ BOOST_AUTO_TEST_CASE(branches_for) ); } +BOOST_AUTO_TEST_CASE(branches_for_declared_in_init) +{ + CHECK( + "{ let b := 0 for { let a := 1 pop(a) } a { pop(a) } { b := 1 pop(a) } }", + "{ let b := 0 for { let a := 1 pop(1) } 1 { pop(1) } { b := 1 pop(1) } }" + ); + CHECK( + "{ let b := 0 for { let a := 1 pop(a) } lt(a, 0) { pop(a) a := add(a, 3) } { b := 1 pop(a) } }", + "{ let b := 0 for { let a := 1 pop(1) } lt(a, 0) { pop(a) a := add(a, 3) } { b := 1 pop(a) } }" + ); +} + BOOST_AUTO_TEST_CASE(reassignment) { CHECK( From 2b6a7665ee79e1397f72cca8fb21e44e29045844 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 2 Feb 2018 15:23:44 +0100 Subject: [PATCH 06/10] Refactor data flow analysis out of remat. --- libjulia/optimiser/DataFlowAnalyzer.cpp | 195 ++++++++++++++++++++++++ libjulia/optimiser/DataFlowAnalyzer.h | 78 ++++++++++ libjulia/optimiser/Rematerialiser.cpp | 159 ++----------------- libjulia/optimiser/Rematerialiser.h | 29 +--- 4 files changed, 284 insertions(+), 177 deletions(-) create mode 100644 libjulia/optimiser/DataFlowAnalyzer.cpp create mode 100644 libjulia/optimiser/DataFlowAnalyzer.h diff --git a/libjulia/optimiser/DataFlowAnalyzer.cpp b/libjulia/optimiser/DataFlowAnalyzer.cpp new file mode 100644 index 000000000..389f87153 --- /dev/null +++ b/libjulia/optimiser/DataFlowAnalyzer.cpp @@ -0,0 +1,195 @@ +/*( + This file is part of solidity. + + solidity is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + solidity is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with solidity. If not, see . +*/ +/** + * Base class to perform data flaw analysis during AST walks. + * Tracks assignments and is used as base class for both Rematerialiser and + * Common Subexpression Eliminator. + */ + +#include + +#include + +#include + +#include + +#include + +#include + +using namespace std; +using namespace dev; +using namespace dev::julia; + +void DataFlowAnalyzer::operator()(Assignment& _assignment) +{ + set names; + for (auto const& var: _assignment.variableNames) + names.insert(var.name); + solAssert(_assignment.value, ""); + visit(*_assignment.value); + handleAssignment(names, _assignment.value.get()); +} + +void DataFlowAnalyzer::operator()(VariableDeclaration& _varDecl) +{ + set names; + for (auto const& var: _varDecl.variables) + names.insert(var.name); + m_variableScopes.back().first += names; + if (_varDecl.value) + visit(*_varDecl.value); + handleAssignment(names, _varDecl.value.get()); +} + +void DataFlowAnalyzer::operator()(If& _if) +{ + ASTModifier::operator()(_if); + + Assignments assignments; + assignments(_if.body); + clearValues(assignments.names()); +} + +void DataFlowAnalyzer::operator()(Switch& _switch) +{ + visit(*_switch.expression); + set assignedVariables; + for (auto& _case: _switch.cases) + { + (*this)(_case.body); + Assignments assignments; + assignments(_case.body); + assignedVariables += assignments.names(); + // This is a little too destructive, we could retain the old values. + clearValues(assignments.names()); + } + clearValues(assignedVariables); +} + +void DataFlowAnalyzer::operator()(FunctionDefinition& _fun) +{ + m_variableScopes.push_back(make_pair(set(), true)); + for (auto const& parameter: _fun.parameters) + m_variableScopes.back().first.insert(parameter.name); + for (auto const& var: _fun.returnVariables) + m_variableScopes.back().first.insert(var.name); + ASTModifier::operator()(_fun); + m_variableScopes.pop_back(); +} + +void DataFlowAnalyzer::operator()(ForLoop& _for) +{ + // Special scope handling of the pre block. + m_variableScopes.push_back(make_pair(set(), false)); + for (auto& statement: _for.pre.statements) + visit(statement); + + Assignments assignments; + assignments(_for.body); + assignments(_for.post); + clearValues(assignments.names()); + + visit(*_for.condition); + (*this)(_for.body); + (*this)(_for.post); + + clearValues(assignments.names()); + + m_variableScopes.pop_back(); +} + +void DataFlowAnalyzer::operator()(Block& _block) +{ + size_t numScopes = m_variableScopes.size(); + m_variableScopes.push_back(make_pair(set(), false)); + ASTModifier::operator()(_block); + m_variableScopes.pop_back(); + solAssert(numScopes == m_variableScopes.size(), ""); +} + +void DataFlowAnalyzer::handleAssignment(set const& _variables, Expression* _value) +{ + clearValues(_variables); + + MovableChecker movableChecker; + if (_value) + movableChecker.visit(*_value); + if (_variables.size() == 1) + { + string const& name = *_variables.begin(); + // Expression has to be movable and cannot contain a reference + // to the variable that will be assigned to. + // TODO: Add a test for that + if (_value && movableChecker.movable() && !movableChecker.referencedVariables().count(name)) + // TODO If _value is null, we could use zero. + m_value[name] = _value; + } + + auto const& referencedVariables = movableChecker.referencedVariables(); + for (auto const& name: _variables) + { + m_references[name] = referencedVariables; + for (auto const& ref: referencedVariables) + m_referencedBy[ref].insert(name); + } +} + +void DataFlowAnalyzer::clearValues(set const& _variables) +{ + // All variables that reference variables to be cleared also have to be + // cleared, but not recursively, since only the value of the original + // variables changes. Example: + // let a := 1 + // let b := a + // let c := b + // let a := 2 + // add(b, c) + // In the last line, we can replace c by b, but not b by a. + // + // This cannot be easily tested since the substitutions will be done + // one by one on the fly, and the last line will just be add(1, 1) + + set variables = _variables; + // Clear variables that reference variables to be cleared. + for (auto const& name: variables) + for (auto const& ref: m_referencedBy[name]) + variables.insert(ref); + + // Clear the value and update the reference relation. + for (auto const& name: variables) + m_value.erase(name); + for (auto const& name: variables) + { + for (auto const& ref: m_references[name]) + m_referencedBy[ref].erase(name); + m_references[name].clear(); + } +} + +bool DataFlowAnalyzer::inScope(string const& _variableName) const +{ + for (auto const& scope: m_variableScopes | boost::adaptors::reversed) + { + if (scope.first.count(_variableName)) + return true; + if (scope.second) + return false; + } + return false; +} diff --git a/libjulia/optimiser/DataFlowAnalyzer.h b/libjulia/optimiser/DataFlowAnalyzer.h new file mode 100644 index 000000000..16d6e72b0 --- /dev/null +++ b/libjulia/optimiser/DataFlowAnalyzer.h @@ -0,0 +1,78 @@ +/* + This file is part of solidity. + + solidity is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + solidity is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with solidity. If not, see . +*/ +/** + * Base class to perform data flow analysis during AST walks. + * Tracks assignments and is used as base class for both Rematerialiser and + * Common Subexpression Eliminator. + */ + +#pragma once + +#include + +#include +#include +#include + +namespace dev +{ +namespace julia +{ + +/** + * Base class to perform data flow analysis during AST walks. + * Tracks assignments and is used as base class for both Rematerialiser and + * Common Subexpression Eliminator. + * + * Prerequisite: Disambiguator + */ +class DataFlowAnalyzer: public ASTModifier +{ +public: + using ASTModifier::operator(); + virtual void operator()(Assignment& _assignment) override; + virtual void operator()(VariableDeclaration& _varDecl) override; + virtual void operator()(If& _if) override; + virtual void operator()(Switch& _switch) override; + virtual void operator()(FunctionDefinition&) override; + virtual void operator()(ForLoop&) override; + virtual void operator()(Block& _block) override; + +protected: + /// Registers the assignment. + void handleAssignment(std::set const& _names, Expression* _value); + + /// Clears information about the valuse assigned to the given variables, + /// for example at points where control flow is merged. + void clearValues(std::set const& _names); + + /// Returns true iff the variable is in scope. + bool inScope(std::string const& _variableName) const; + + /// Current values of variables, always movable. + std::map m_value; + /// m_references[a].contains(b) <=> the current expression assigned to a references b + std::map> m_references; + /// m_referencedBy[b].contains(a) <=> the current expression assigned to a references b + std::map> m_referencedBy; + /// List of scopes, where each scope is a set of variables and a bool that tells + /// whether it is a function body (true) or not. + std::vector, bool>> m_variableScopes; +}; + +} +} diff --git a/libjulia/optimiser/Rematerialiser.cpp b/libjulia/optimiser/Rematerialiser.cpp index bf7d7d16f..eaa75e339 100644 --- a/libjulia/optimiser/Rematerialiser.cpp +++ b/libjulia/optimiser/Rematerialiser.cpp @@ -20,176 +20,35 @@ #include +#include #include -#include #include -#include - -#include - -#include - using namespace std; using namespace dev; using namespace dev::julia; -void Rematerialiser::operator()(Assignment& _assignment) -{ - set names; - for (auto const& var: _assignment.variableNames) - names.insert(var.name); - handleAssignment(names, _assignment.value.get()); -} - -void Rematerialiser::operator()(VariableDeclaration& _varDecl) -{ - set names; - for (auto const& var: _varDecl.variables) - names.insert(var.name); - m_variableScopes.back().first += names; - handleAssignment(names, _varDecl.value.get()); -} - -void Rematerialiser::operator()(If& _if) -{ - ASTModifier::operator()(_if); - - Assignments ass; - ass(_if.body); - handleAssignment(ass.names(), nullptr); -} - -void Rematerialiser::operator()(Switch& _switch) -{ - boost::apply_visitor(*this, *_switch.expression); - set assignedVariables; - for (auto& _case: _switch.cases) - { - (*this)(_case.body); - Assignments ass; - ass(_case.body); - assignedVariables += ass.names(); - // This is a little too destructive, we could retain the old replacements. - handleAssignment(ass.names(), nullptr); - } - handleAssignment(assignedVariables, nullptr); -} - -void Rematerialiser::operator()(FunctionDefinition& _fun) -{ - m_variableScopes.push_back(make_pair(set(), true)); - for (auto const& parameter: _fun.parameters) - m_variableScopes.back().first.insert(parameter.name); - for (auto const& var: _fun.returnVariables) - m_variableScopes.back().first.insert(var.name); - ASTModifier::operator()(_fun); - m_variableScopes.pop_back(); -} - -void Rematerialiser::operator()(ForLoop& _for) -{ - // Special scope handling of the pre block. - m_variableScopes.push_back(make_pair(set(), false)); - for (auto& statement: _for.pre.statements) - visit(statement); - - Assignments ass; - ass(_for.body); - ass(_for.post); - handleAssignment(ass.names(), nullptr); - - visit(*_for.condition); - (*this)(_for.body); - (*this)(_for.post); - - handleAssignment(ass.names(), nullptr); - - m_variableScopes.pop_back(); -} - -void Rematerialiser::operator()(Block& _block) -{ - size_t numScopes = m_variableScopes.size(); - m_variableScopes.push_back(make_pair(set(), false)); - ASTModifier::operator()(_block); - m_variableScopes.pop_back(); - solAssert(numScopes == m_variableScopes.size(), ""); -} - -void Rematerialiser::handleAssignment(set const& _variables, Expression* _value) -{ - MovableChecker movableChecker; - if (_value) - { - visit(*_value); - movableChecker.visit(*_value); - } - if (_variables.size() == 1) - { - string const& name = *_variables.begin(); - if (movableChecker.movable() && _value) - // TODO Plus heuristic about size of value - // TODO If _value is null, we could use zero. - m_substitutions[name] = _value; - else - m_substitutions.erase(name); - } - else - for (auto const& name: _variables) - m_substitutions.erase(name); - - // Disallow substitutions that use a variable that will be reassigned by this assignment. - for (auto const& name: _variables) - for (auto const& ref: m_referencedBy[name]) - m_substitutions.erase(ref); - // Update the fact which variables are referenced by the newly assigned variables - for (auto const& name: _variables) - { - for (auto const& ref: m_references[name]) - m_referencedBy[ref].erase(name); - m_references[name].clear(); - } - auto const& referencedVariables = movableChecker.referencedVariables(); - for (auto const& name: _variables) - { - m_references[name] = referencedVariables; - for (auto const& ref: referencedVariables) - m_referencedBy[ref].insert(name); - } -} - -bool Rematerialiser::inScope(string const& _variableName) const -{ - for (auto const& scope: m_variableScopes | boost::adaptors::reversed) - { - if (scope.first.count(_variableName)) - return true; - if (scope.second) - return false; - } - return false; -} - void Rematerialiser::visit(Expression& _e) { if (_e.type() == typeid(Identifier)) { Identifier& identifier = boost::get(_e); - if (m_substitutions.count(identifier.name)) + if (m_value.count(identifier.name)) { string name = identifier.name; - bool doSubstitute = true; + bool expressionValid = true; for (auto const& ref: m_references[name]) if (!inScope(ref)) { - doSubstitute = false; + expressionValid = false; break; } - if (doSubstitute) - _e = (ASTCopier{}).translate(*m_substitutions.at(name)); + solAssert(m_value.at(name), ""); + auto const& value = *m_value.at(name); + if (expressionValid && CodeSize::codeSize(value) <= 7) + _e = (ASTCopier{}).translate(value); } } - ASTModifier::visit(_e); + DataFlowAnalyzer::visit(_e); } diff --git a/libjulia/optimiser/Rematerialiser.h b/libjulia/optimiser/Rematerialiser.h index 1accc3f66..60dbfada5 100644 --- a/libjulia/optimiser/Rematerialiser.h +++ b/libjulia/optimiser/Rematerialiser.h @@ -20,7 +20,7 @@ #pragma once -#include +#include #include #include @@ -36,37 +36,12 @@ namespace julia * * Prerequisite: Disambiguator */ -class Rematerialiser: public ASTModifier +class Rematerialiser: public DataFlowAnalyzer { -public: - using ASTModifier::operator(); - virtual void operator()(Assignment& _assignment) override; - virtual void operator()(VariableDeclaration& _varDecl) override; - virtual void operator()(If& _if) override; - virtual void operator()(Switch& _switch) override; - virtual void operator()(FunctionDefinition&) override; - virtual void operator()(ForLoop&) override; - virtual void operator()(Block& _block) override; - protected: using ASTModifier::visit; virtual void visit(Expression& _e) override; -private: - void handleAssignment(std::set const& _names, Expression* _value); - - /// Returns true iff the variable is in scope. - bool inScope(std::string const& _variableName) const; - - /// Substitutions to be performed, if possible. - std::map m_substitutions; - /// m_references[a].contains(b) <=> the current expression assigned to a references b - std::map> m_references; - /// m_referencedBy[b].contains(a) <=> the current expression assigned to a references b - std::map> m_referencedBy; - /// List of scopes, where each scope is a set of variables and a bool that tells - /// whether it is a function body (true) or not. - std::vector, bool>> m_variableScopes; }; } From 773be40c19a2c3b72a5cf743e86e150533e0f25f Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 2 Feb 2018 15:24:38 +0100 Subject: [PATCH 07/10] Reassign test for remat. --- test/libjulia/Rematerialiser.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/libjulia/Rematerialiser.cpp b/test/libjulia/Rematerialiser.cpp index d6cc0dae7..fce287a58 100644 --- a/test/libjulia/Rematerialiser.cpp +++ b/test/libjulia/Rematerialiser.cpp @@ -74,6 +74,14 @@ BOOST_AUTO_TEST_CASE(expression) ); } +BOOST_AUTO_TEST_CASE(reassign) +{ + CHECK( + "{ let a := extcodesize(0) let b := a let c := b a := 2 let d := add(b, c) pop(a) pop(b) pop(c) pop(d) }", + "{ let a := extcodesize(0) let b := a let c := a a := 2 let d := add(b, c) pop(2) pop(b) pop(c) pop(add(b, c)) }" + ); +} + BOOST_AUTO_TEST_CASE(non_movable_instr) { CHECK( From 88a5d152d028d597fd8cd90bd99ee5b7f1388191 Mon Sep 17 00:00:00 2001 From: chriseth Date: Mon, 5 Feb 2018 18:02:32 +0100 Subject: [PATCH 08/10] Introduce struct for scopes. --- libjulia/optimiser/DataFlowAnalyzer.cpp | 16 ++++++++-------- libjulia/optimiser/DataFlowAnalyzer.h | 12 +++++++++--- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/libjulia/optimiser/DataFlowAnalyzer.cpp b/libjulia/optimiser/DataFlowAnalyzer.cpp index 389f87153..9ee3215d7 100644 --- a/libjulia/optimiser/DataFlowAnalyzer.cpp +++ b/libjulia/optimiser/DataFlowAnalyzer.cpp @@ -51,7 +51,7 @@ void DataFlowAnalyzer::operator()(VariableDeclaration& _varDecl) set names; for (auto const& var: _varDecl.variables) names.insert(var.name); - m_variableScopes.back().first += names; + m_variableScopes.back().variables += names; if (_varDecl.value) visit(*_varDecl.value); handleAssignment(names, _varDecl.value.get()); @@ -84,11 +84,11 @@ void DataFlowAnalyzer::operator()(Switch& _switch) void DataFlowAnalyzer::operator()(FunctionDefinition& _fun) { - m_variableScopes.push_back(make_pair(set(), true)); + m_variableScopes.emplace_back(true); for (auto const& parameter: _fun.parameters) - m_variableScopes.back().first.insert(parameter.name); + m_variableScopes.back().variables.insert(parameter.name); for (auto const& var: _fun.returnVariables) - m_variableScopes.back().first.insert(var.name); + m_variableScopes.back().variables.insert(var.name); ASTModifier::operator()(_fun); m_variableScopes.pop_back(); } @@ -96,7 +96,7 @@ void DataFlowAnalyzer::operator()(FunctionDefinition& _fun) void DataFlowAnalyzer::operator()(ForLoop& _for) { // Special scope handling of the pre block. - m_variableScopes.push_back(make_pair(set(), false)); + m_variableScopes.emplace_back(false); for (auto& statement: _for.pre.statements) visit(statement); @@ -117,7 +117,7 @@ void DataFlowAnalyzer::operator()(ForLoop& _for) void DataFlowAnalyzer::operator()(Block& _block) { size_t numScopes = m_variableScopes.size(); - m_variableScopes.push_back(make_pair(set(), false)); + m_variableScopes.emplace_back(false); ASTModifier::operator()(_block); m_variableScopes.pop_back(); solAssert(numScopes == m_variableScopes.size(), ""); @@ -186,9 +186,9 @@ bool DataFlowAnalyzer::inScope(string const& _variableName) const { for (auto const& scope: m_variableScopes | boost::adaptors::reversed) { - if (scope.first.count(_variableName)) + if (scope.variables.count(_variableName)) return true; - if (scope.second) + if (scope.isFunction) return false; } return false; diff --git a/libjulia/optimiser/DataFlowAnalyzer.h b/libjulia/optimiser/DataFlowAnalyzer.h index 16d6e72b0..4cb3d4cd0 100644 --- a/libjulia/optimiser/DataFlowAnalyzer.h +++ b/libjulia/optimiser/DataFlowAnalyzer.h @@ -69,9 +69,15 @@ protected: std::map> m_references; /// m_referencedBy[b].contains(a) <=> the current expression assigned to a references b std::map> m_referencedBy; - /// List of scopes, where each scope is a set of variables and a bool that tells - /// whether it is a function body (true) or not. - std::vector, bool>> m_variableScopes; + + struct Scope + { + explicit Scope(bool _isFunction): isFunction(_isFunction) {} + std::set variables; + bool isFunction; + }; + /// List of scopes. + std::vector m_variableScopes; }; } From 669b63ca5fd612cabc28ddb9fb8c58ab60b2c75a Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 6 Feb 2018 12:38:14 +0100 Subject: [PATCH 09/10] Test not to remat if the expression is too large. --- test/libjulia/Rematerialiser.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/libjulia/Rematerialiser.cpp b/test/libjulia/Rematerialiser.cpp index fce287a58..5bd692362 100644 --- a/test/libjulia/Rematerialiser.cpp +++ b/test/libjulia/Rematerialiser.cpp @@ -155,4 +155,16 @@ BOOST_AUTO_TEST_CASE(do_not_move_out_of_scope) ); } +BOOST_AUTO_TEST_CASE(do_not_remat_large_amounts_of_code) +{ + CHECK( + "{ let x := add(mul(calldataload(2), calldataload(4)), mul(2, calldatasize())) let b := x }", + "{ let x := add(mul(calldataload(2), calldataload(4)), mul(2, calldatasize())) let b := x }" + ); + CHECK( + "{ let x := add(mul(calldataload(2), calldataload(4)), calldatasize()) let b := x }", + "{ let x := add(mul(calldataload(2), calldataload(4)), calldatasize()) let b := add(mul(calldataload(2), calldataload(4)), calldatasize()) }" + ); +} + BOOST_AUTO_TEST_SUITE_END() From c0abddc9dcbf1f0437ac04119a0c8c238fad44c8 Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 6 Feb 2018 12:58:51 +0100 Subject: [PATCH 10/10] Test for self-referring assignment. --- libjulia/optimiser/DataFlowAnalyzer.cpp | 2 -- test/libjulia/Rematerialiser.cpp | 9 +++++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/libjulia/optimiser/DataFlowAnalyzer.cpp b/libjulia/optimiser/DataFlowAnalyzer.cpp index 9ee3215d7..566533930 100644 --- a/libjulia/optimiser/DataFlowAnalyzer.cpp +++ b/libjulia/optimiser/DataFlowAnalyzer.cpp @@ -135,9 +135,7 @@ void DataFlowAnalyzer::handleAssignment(set const& _variables, Expressio string const& name = *_variables.begin(); // Expression has to be movable and cannot contain a reference // to the variable that will be assigned to. - // TODO: Add a test for that if (_value && movableChecker.movable() && !movableChecker.referencedVariables().count(name)) - // TODO If _value is null, we could use zero. m_value[name] = _value; } diff --git a/test/libjulia/Rematerialiser.cpp b/test/libjulia/Rematerialiser.cpp index 5bd692362..8f928f8ef 100644 --- a/test/libjulia/Rematerialiser.cpp +++ b/test/libjulia/Rematerialiser.cpp @@ -146,6 +146,15 @@ BOOST_AUTO_TEST_CASE(reassignment) ); } +BOOST_AUTO_TEST_CASE(update_assignment_remat) +{ + // We cannot substitute `a` in `let b := a` + CHECK( + "{ let a := extcodesize(0) a := mul(a, 2) let b := a }", + "{ let a := extcodesize(0) a := mul(a, 2) let b := a }" + ); +} + BOOST_AUTO_TEST_CASE(do_not_move_out_of_scope) { // Cannot replace by `let b := x` by `let b := a` since a is out of scope.