Move rematerialiser tests.

This commit is contained in:
chriseth 2018-10-11 17:59:45 +02:00
parent 49958e4720
commit 6dfd0adc0b
19 changed files with 292 additions and 179 deletions

View File

@ -1,179 +0,0 @@
/*
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(YulRematerialiser)
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()

View File

@ -30,6 +30,7 @@
#include <libjulia/optimiser/ExpressionInliner.h>
#include <libjulia/optimiser/FullInliner.h>
#include <libjulia/optimiser/MainFunction.h>
#include <libjulia/optimiser/Rematerialiser.h>
#include <libsolidity/parsing/Scanner.h>
#include <libsolidity/inlineasm/AsmPrinter.h>
@ -128,6 +129,11 @@ bool YulOptimizerTest::run(ostream& _stream, string const& _linePrefix, bool con
(FunctionGrouper{})(*m_ast);
(MainFunction{})(*m_ast);
}
else if (m_optimizerStep == "rematerialiser")
{
disambiguate();
(Rematerialiser{})(*m_ast);
}
else
{
FormattedScope(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Invalid optimizer step: " << m_optimizerStep << endl;

View File

@ -0,0 +1,21 @@
{
let a := 1
for { pop(a) } a { pop(a) } {
pop(a)
}
}
// ----
// rematerialiser
// {
// let a := 1
// for {
// pop(1)
// }
// 1
// {
// pop(1)
// }
// {
// pop(1)
// }
// }

View File

@ -0,0 +1,25 @@
{
let a := 1
for { pop(a) } a { pop(a) } {
a := 7
let c := a
}
let x := a
}
// ----
// rematerialiser
// {
// let a := 1
// for {
// pop(1)
// }
// a
// {
// pop(7)
// }
// {
// a := 7
// let c := 7
// }
// let x := a
// }

View File

@ -0,0 +1,23 @@
{
let b := 0
for { let a := 1 pop(a) } a { pop(a) } {
b := 1 pop(a)
}
}
// ----
// rematerialiser
// {
// let b := 0
// for {
// let a := 1
// pop(1)
// }
// 1
// {
// pop(1)
// }
// {
// b := 1
// pop(1)
// }
// }

View File

@ -0,0 +1,24 @@
{
let b := 0
for { let a := 1 pop(a) } lt(a, 0) { pop(a) a := add(a, 3) } {
b := 1 pop(a)
}
}
// ----
// rematerialiser
// {
// let b := 0
// for {
// let a := 1
// pop(1)
// }
// lt(a, 0)
// {
// pop(a)
// a := add(a, 3)
// }
// {
// b := 1
// pop(a)
// }
// }

View File

@ -0,0 +1,18 @@
{
let a := 1
let b := 2
if b { pop(b) b := a }
let c := b
}
// ----
// rematerialiser
// {
// let a := 1
// let b := 2
// if 2
// {
// pop(2)
// b := 1
// }
// let c := b
// }

View File

@ -0,0 +1,24 @@
{
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))
}
// ----
// rematerialiser
// {
// 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))
// }

View File

@ -0,0 +1,19 @@
// Cannot replace `let b := x` by `let b := a` since a is out of scope.
{
let x
{
let a := sload(0)
x := a
}
let b := x
}
// ----
// rematerialiser
// {
// let x
// {
// let a := sload(0)
// x := a
// }
// let b := x
// }

View File

@ -0,0 +1,10 @@
{
let x := add(mul(calldataload(2), calldataload(4)), mul(2, calldatasize()))
let b := x
}
// ----
// rematerialiser
// {
// let x := add(mul(calldataload(2), calldataload(4)), mul(2, calldatasize()))
// let b := x
// }

View File

@ -0,0 +1,10 @@
{
let x := add(mul(calldataload(2), calldataload(4)), calldatasize())
let b := x
}
// ----
// rematerialiser
// {
// let x := add(mul(calldataload(2), calldataload(4)), calldatasize())
// let b := add(mul(calldataload(2), calldataload(4)), calldatasize())
// }

View File

@ -0,0 +1,10 @@
{
let a := add(mul(calldatasize(), 2), number())
let b := add(a, a)
}
// ----
// rematerialiser
// {
// let a := add(mul(calldatasize(), 2), number())
// let b := add(add(mul(calldatasize(), 2), number()), add(mul(calldatasize(), 2), number()))
// }

View File

@ -0,0 +1,18 @@
{
function f(x) -> y {}
let a := 1
let b := f(a)
let c := a
mstore(add(a, b), c)
}
// ----
// rematerialiser
// {
// function f(x) -> y
// {
// }
// let a := 1
// let b := f(1)
// let c := 1
// mstore(add(1, b), 1)
// }

View File

@ -0,0 +1,14 @@
{
let a := 1
let b := mload(a)
let c := a
mstore(add(a, b), c)
}
// ----
// rematerialiser
// {
// let a := 1
// let b := mload(1)
// let c := 1
// mstore(add(1, b), 1)
// }

View File

@ -0,0 +1,21 @@
{
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)
}
// ----
// rematerialiser
// {
// 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))
// }

View File

@ -0,0 +1,19 @@
{
let a := 1
pop(a)
if a { a := 2 }
let b := mload(a)
pop(b)
}
// ----
// rematerialiser
// {
// let a := 1
// pop(1)
// if 1
// {
// a := 2
// }
// let b := mload(a)
// pop(b)
// }

View File

@ -0,0 +1,5 @@
{}
// ----
// rematerialiser
// {
// }

View File

@ -0,0 +1,12 @@
{
let a := 1
let b := a
mstore(0, b)
}
// ----
// rematerialiser
// {
// let a := 1
// let b := 1
// mstore(0, 1)
// }

View File

@ -0,0 +1,13 @@
// We cannot substitute `a` in `let b := a`
{
let a := extcodesize(0)
a := mul(a, 2)
let b := a
}
// ----
// rematerialiser
// {
// let a := extcodesize(0)
// a := mul(a, 2)
// let b := a
// }