diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/libjulia/Inliner.cpp | 199 | ||||
-rw-r--r-- | test/libjulia/Rematerialiser.cpp | 179 | ||||
-rw-r--r-- | test/libjulia/Simplifier.cpp | 130 | ||||
-rw-r--r-- | test/libjulia/UnusedPruner.cpp | 129 |
4 files changed, 637 insertions, 0 deletions
diff --git a/test/libjulia/Inliner.cpp b/test/libjulia/Inliner.cpp new file mode 100644 index 00000000..88b51f28 --- /dev/null +++ b/test/libjulia/Inliner.cpp @@ -0,0 +1,199 @@ +/* + 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 <http://www.gnu.org/licenses/>. +*/ +/** + * @date 2017 + * Unit tests for the iulia function inliner. + */ + +#include <test/libjulia/Common.h> + +#include <libjulia/optimiser/ExpressionInliner.h> +#include <libjulia/optimiser/InlinableExpressionFunctionFinder.h> + +#include <libsolidity/inlineasm/AsmPrinter.h> + +#include <boost/test/unit_test.hpp> + +#include <boost/range/adaptors.hpp> +#include <boost/algorithm/string/join.hpp> + +using namespace std; +using namespace dev; +using namespace dev::julia; +using namespace dev::julia::test; +using namespace dev::solidity; + +namespace +{ +string inlinableFunctions(string const& _source) +{ + auto ast = disambiguate(_source); + + InlinableExpressionFunctionFinder funFinder; + funFinder(ast); + + return boost::algorithm::join( + funFinder.inlinableFunctions() | boost::adaptors::map_keys, + "," + ); +} + +string inlineFunctions(string const& _source, bool _julia = true) +{ + auto ast = disambiguate(_source, _julia); + ExpressionInliner(ast).run(); + return assembly::AsmPrinter(_julia)(ast); +} +} + +BOOST_AUTO_TEST_SUITE(IuliaInlinableFunctionFilter) + +BOOST_AUTO_TEST_CASE(smoke_test) +{ + BOOST_CHECK_EQUAL(inlinableFunctions("{ }"), ""); +} + +BOOST_AUTO_TEST_CASE(simple) +{ + BOOST_CHECK_EQUAL(inlinableFunctions("{ function f() -> x:u256 { x := 2:u256 } }"), "f"); + BOOST_CHECK_EQUAL(inlinableFunctions("{" + "function g(a:u256) -> b:u256 { b := a }" + "function f() -> x:u256 { x := g(2:u256) }" + "}"), "f,g"); +} + +BOOST_AUTO_TEST_CASE(simple_inside_structures) +{ + BOOST_CHECK_EQUAL(inlinableFunctions("{" + "switch 2:u256 " + "case 2:u256 {" + "function g(a:u256) -> b:u256 { b := a }" + "function f() -> x:u256 { x := g(2:u256) }" + "}" + "}"), "f,g"); + BOOST_CHECK_EQUAL(inlinableFunctions("{" + "for {" + "function g(a:u256) -> b:u256 { b := a }" + "} 1:u256 {" + "function f() -> x:u256 { x := g(2:u256) }" + "}" + "{" + "function h() -> y:u256 { y := 2:u256 }" + "}" + "}"), "f,g,h"); +} + +BOOST_AUTO_TEST_CASE(negative) +{ + BOOST_CHECK_EQUAL(inlinableFunctions("{ function f() -> x:u256 { } }"), ""); + BOOST_CHECK_EQUAL(inlinableFunctions("{ function f() -> x:u256 { x := 2:u256 {} } }"), ""); + BOOST_CHECK_EQUAL(inlinableFunctions("{ function f() -> x:u256 { x := f() } }"), ""); + BOOST_CHECK_EQUAL(inlinableFunctions("{ function f() -> x:u256 { x := x } }"), ""); + BOOST_CHECK_EQUAL(inlinableFunctions("{ function f() -> x:u256, y:u256 { x := 2:u256 } }"), ""); +} + + +BOOST_AUTO_TEST_SUITE_END() + +BOOST_AUTO_TEST_SUITE(IuliaFunctionInliner) + +BOOST_AUTO_TEST_CASE(simple) +{ + BOOST_CHECK_EQUAL( + inlineFunctions("{ function f() -> x:u256 { x := 2:u256 } let y:u256 := f() }"), + format("{ function f() -> x:u256 { x := 2:u256 } let y:u256 := 2:u256 }") + ); +} + +BOOST_AUTO_TEST_CASE(with_args) +{ + BOOST_CHECK_EQUAL( + inlineFunctions("{ function f(a:u256) -> x:u256 { x := a } let y:u256 := f(7:u256) }"), + format("{ function f(a:u256) -> x:u256 { x := a } let y:u256 := 7:u256 }") + ); +} + +BOOST_AUTO_TEST_CASE(no_inline_with_mload) +{ + // Does not inline because mload could be moved out of sequence + BOOST_CHECK_EQUAL( + inlineFunctions("{ function f(a) -> x { x := a } let y := f(mload(2)) }", false), + format("{ function f(a) -> x { x := a } let y := f(mload(2)) }", false) + ); +} + +BOOST_AUTO_TEST_CASE(no_move_with_side_effects) +{ + // The calls to g and h cannot be moved because g and h are not movable. Therefore, the call + // to f is not inlined. + BOOST_CHECK_EQUAL( + inlineFunctions("{" + "function f(a, b) -> x { x := add(b, a) }" + "function g() -> y { y := mload(0) mstore(0, 4) }" + "function h() -> z { mstore(0, 4) z := mload(0) }" + "let r := f(g(), h())" + "}", false), + format("{" + "function f(a, b) -> x { x := add(b, a) }" + "function g() -> y { y := mload(0) mstore(0, 4) }" + "function h() -> z { mstore(0, 4) z := mload(0) }" + "let r := f(g(), h())" + "}", false) + ); +} + +BOOST_AUTO_TEST_CASE(complex_with_evm) +{ + BOOST_CHECK_EQUAL( + inlineFunctions("{ function f(a) -> x { x := add(a, a) } let y := f(calldatasize()) }", false), + format("{ function f(a) -> x { x := add(a, a) } let y := add(calldatasize(), calldatasize()) }", false) + ); +} + +BOOST_AUTO_TEST_CASE(double_calls) +{ + BOOST_CHECK_EQUAL( + inlineFunctions("{" + "function f(a) -> x { x := add(a, a) }" + "function g(b, c) -> y { y := mul(mload(c), f(b)) }" + "let y := g(calldatasize(), 7)" + "}", false), + format("{" + "function f(a) -> x { x := add(a, a) }" + "function g(b, c) -> y { y := mul(mload(c), add(b, b)) }" + "let y_1 := mul(mload(7), add(calldatasize(), calldatasize()))" + "}", false) + ); +} + +BOOST_AUTO_TEST_CASE(double_recursive_calls) +{ + BOOST_CHECK_EQUAL( + inlineFunctions("{" + "function f(a, r) -> x { x := g(a, g(r, r)) }" + "function g(b, s) -> y { y := f(b, f(s, s)) }" + "let y := g(calldatasize(), 7)" + "}", false), + format("{" + "function f(a, r) -> x { x := g(a, f(r, f(r, r))) }" + "function g(b, s) -> y { y := f(b, g(s, f(s, f(s, s))))}" + "let y_1 := f(calldatasize(), g(7, f(7, f(7, 7))))" + "}", false) + ); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/test/libjulia/Rematerialiser.cpp b/test/libjulia/Rematerialiser.cpp new file mode 100644 index 00000000..8f928f8e --- /dev/null +++ b/test/libjulia/Rematerialiser.cpp @@ -0,0 +1,179 @@ +/* + 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 <http://www.gnu.org/licenses/>. +*/ +/** + * @date 2017 + * Unit tests for the rematerialiser optimizer stage. + */ + +#include <test/libjulia/Common.h> + +#include <libjulia/optimiser/Rematerialiser.h> + +#include <libsolidity/inlineasm/AsmPrinter.h> + +#include <boost/test/unit_test.hpp> + +#include <boost/range/adaptors.hpp> +#include <boost/algorithm/string/join.hpp> + +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(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( + "{ 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(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( + "{ 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_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. + 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_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() diff --git a/test/libjulia/Simplifier.cpp b/test/libjulia/Simplifier.cpp new file mode 100644 index 00000000..b48d45c8 --- /dev/null +++ b/test/libjulia/Simplifier.cpp @@ -0,0 +1,130 @@ +/* + 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 <http://www.gnu.org/licenses/>. +*/ +/** + * @date 2017 + * Unit tests for the expression simplifier optimizer stage. + */ + +#include <test/libjulia/Common.h> + +#include <libjulia/optimiser/ExpressionSimplifier.h> + +#include <libsolidity/inlineasm/AsmPrinter.h> + +#include <boost/test/unit_test.hpp> + +#include <boost/range/adaptors.hpp> +#include <boost/algorithm/string/join.hpp> + +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 = *(parse(_original, false).first);\ + (ExpressionSimplifier{})(b);\ + string result = p(b);\ + BOOST_CHECK_EQUAL(result, format(_expectation, false));\ +}\ +while(false) + +BOOST_AUTO_TEST_SUITE(IuliaSimplifier) + +BOOST_AUTO_TEST_CASE(smoke_test) +{ + CHECK("{ }", "{ }"); +} + +BOOST_AUTO_TEST_CASE(constants) +{ + CHECK( + "{ let a := add(1, mul(3, 4)) }", + "{ let a := 13 }" + ); +} + +BOOST_AUTO_TEST_CASE(invariant) +{ + CHECK( + "{ let a := mload(sub(7, 7)) let b := sub(a, 0) }", + "{ let a := mload(0) let b := a }" + ); +} + +BOOST_AUTO_TEST_CASE(reversed) +{ + CHECK( + "{ let a := add(0, mload(0)) }", + "{ let a := mload(0) }" + ); +} + +BOOST_AUTO_TEST_CASE(constant_propagation) +{ + CHECK( + "{ let a := add(7, sub(mload(0), 7)) }", + "{ let a := mload(0) }" + ); +} + +BOOST_AUTO_TEST_CASE(identity_rules_simple) +{ + CHECK( + "{ let a := mload(0) let b := sub(a, a) }", + "{ let a := mload(0) let b := 0 }" + ); +} + +BOOST_AUTO_TEST_CASE(identity_rules_complex) +{ + CHECK( + "{ let a := sub(calldataload(0), calldataload(0)) }", + "{ let a := 0 }" + ); +} + +BOOST_AUTO_TEST_CASE(identity_rules_negative) +{ + CHECK( + "{ let a := sub(calldataload(1), calldataload(0)) }", + "{ let a := sub(calldataload(1), calldataload(0)) }" + ); +} + +BOOST_AUTO_TEST_CASE(including_function_calls) +{ + CHECK( + "{ function f() -> a {} let b := add(7, sub(f(), 7)) }", + "{ function f() -> a {} let b := f() }" + ); +} + +BOOST_AUTO_TEST_CASE(inside_for) +{ + CHECK( + "{ for { let a := 10 } iszero(eq(a, 0)) { a := add(a, 1) } {} }", + "{ for { let a := 10 } iszero(iszero(a)) { a := add(a, 1) } {} }" + ); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/test/libjulia/UnusedPruner.cpp b/test/libjulia/UnusedPruner.cpp new file mode 100644 index 00000000..b86a54b3 --- /dev/null +++ b/test/libjulia/UnusedPruner.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 <http://www.gnu.org/licenses/>. +*/ +/** + * @date 2017 + * Unit tests for the pruning of unused variables and functions. + */ + +#include <test/libjulia/Common.h> + +#include <libjulia/optimiser/UnusedPruner.h> + +#include <libsolidity/inlineasm/AsmPrinter.h> + +#include <boost/test/unit_test.hpp> + +#include <boost/range/adaptors.hpp> +#include <boost/algorithm/string/join.hpp> + +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);\ + UnusedPruner::runUntilStabilised(b);\ + string result = p(b);\ + BOOST_CHECK_EQUAL(result, format(_expectation, false));\ +}\ +while(false) + +BOOST_AUTO_TEST_SUITE(IuliaUnusedPruner) + +BOOST_AUTO_TEST_CASE(smoke_test) +{ + CHECK("{ }", "{ }"); +} + +BOOST_AUTO_TEST_CASE(trivial) +{ + CHECK( + "{ let a := 1 let b := 1 mstore(0, 1) }", + "{ mstore(0, 1) }" + ); +} + +BOOST_AUTO_TEST_CASE(multi_declarations) +{ + CHECK( + "{ let x, y }", + "{ }" + ); +} + +BOOST_AUTO_TEST_CASE(multi_assignments) +{ + CHECK( + "{ let x, y x := 1 y := 2 }", + "{ let x, y x := 1 y := 2 }" + ); +} + +BOOST_AUTO_TEST_CASE(multi_partial_assignments) +{ + CHECK( + "{ let x, y x := 1 }", + "{ let x, y x := 1 }" + ); +} + +BOOST_AUTO_TEST_CASE(functions) +{ + CHECK( + "{ function f() { let a := 1 } function g() { f() } }", + "{ }" + ); +} + +BOOST_AUTO_TEST_CASE(intermediate_assignment) +{ + CHECK( + "{ let a := 1 a := 4 let b := 1 }", + "{ let a := 1 a := 4 }" + ); +} + +BOOST_AUTO_TEST_CASE(intermediate_multi_assignment){ + CHECK( + "{ let a, b function f() -> x { } a := f() b := 1 }", + "{ let a, b function f() -> x { } a := f() b := 1 }" + ); +} + +BOOST_AUTO_TEST_CASE(multi_declare) +{ + CHECK( + "{ function f() -> x, y { } let a, b := f() }", + "{ function f() -> x, y { } let a, b := f() }" + ); +} + +BOOST_AUTO_TEST_CASE(multi_assign) +{ + CHECK( + "{ let a let b function f() -> x, y { } a, b := f() }", + "{ let a let b function f() -> x, y { } a, b := f() }" + ); +} + +BOOST_AUTO_TEST_SUITE_END() |