Remove fuzzer mode from yul optimizer unit test framework.

This commit is contained in:
Bhargava Shastry 2021-01-06 13:04:42 +01:00
parent 3e7659596e
commit 02b28b42e8
37 changed files with 163 additions and 198 deletions

View File

@ -74,11 +74,9 @@ TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _line
soltestAssert(m_dialect, "Dialect not set.");
m_object->analysisInfo = m_analysisInfo;
YulOptimizerTester tester(m_object, *m_dialect, m_optimizerStep, false);
YulOptimizerTestCommon tester(m_object, *m_dialect, m_optimizerStep);
bool success = tester.runStep();
if (!success)
if (!tester.runStep())
{
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Invalid optimizer step: " << m_optimizerStep << endl;
return TestResult::FatalError;

View File

@ -74,11 +74,10 @@ using namespace solidity::yul::test;
using namespace solidity::frontend;
using namespace std;
YulOptimizerTester::YulOptimizerTester(
YulOptimizerTestCommon::YulOptimizerTestCommon(
shared_ptr<Object> _obj,
Dialect const& _dialect,
string const& _optimizerStep,
bool _fuzzerMode
string const& _optimizerStep
)
{
m_object = _obj;
@ -86,10 +85,9 @@ YulOptimizerTester::YulOptimizerTester(
m_analysisInfo = m_object->analysisInfo;
m_dialect = &_dialect;
m_optimizerStep = _optimizerStep;
m_fuzzerMode = _fuzzerMode;
}
bool YulOptimizerTester::runStep()
bool YulOptimizerTestCommon::runStep()
{
yulAssert(m_dialect, "Dialect not set.");
@ -120,8 +118,7 @@ bool YulOptimizerTester::runStep()
else if (m_optimizerStep == "varNameCleaner")
{
disambiguate();
if (m_fuzzerMode)
FunctionHoister::run(*m_context, *m_ast);
FunctionHoister::run(*m_context, *m_ast);
FunctionGrouper::run(*m_context, *m_ast);
VarNameCleaner::run(*m_context, *m_ast);
}
@ -138,11 +135,8 @@ bool YulOptimizerTester::runStep()
else if (m_optimizerStep == "commonSubexpressionEliminator")
{
disambiguate();
if (m_fuzzerMode)
{
ForLoopInitRewriter::run(*m_context, *m_ast);
FunctionHoister::run(*m_context, *m_ast);
}
ForLoopInitRewriter::run(*m_context, *m_ast);
FunctionHoister::run(*m_context, *m_ast);
CommonSubexpressionEliminator::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "conditionalUnsimplifier")
@ -202,21 +196,15 @@ bool YulOptimizerTester::runStep()
else if (m_optimizerStep == "rematerialiser")
{
disambiguate();
if (m_fuzzerMode)
{
ForLoopInitRewriter::run(*m_context, *m_ast);
FunctionHoister::run(*m_context, *m_ast);
}
ForLoopInitRewriter::run(*m_context, *m_ast);
FunctionHoister::run(*m_context, *m_ast);
Rematerialiser::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "expressionSimplifier")
{
disambiguate();
if (m_fuzzerMode)
{
ForLoopInitRewriter::run(*m_context, *m_ast);
FunctionHoister::run(*m_context, *m_ast);
}
ForLoopInitRewriter::run(*m_context, *m_ast);
FunctionHoister::run(*m_context, *m_ast);
ExpressionSplitter::run(*m_context, *m_ast);
CommonSubexpressionEliminator::run(*m_context, *m_ast);
ExpressionSimplifier::run(*m_context, *m_ast);
@ -231,8 +219,7 @@ bool YulOptimizerTester::runStep()
disambiguate();
ExpressionSplitter::run(*m_context, *m_ast);
ForLoopInitRewriter::run(*m_context, *m_ast);
if (m_fuzzerMode)
FunctionHoister::run(*m_context, *m_ast);
FunctionHoister::run(*m_context, *m_ast);
CommonSubexpressionEliminator::run(*m_context, *m_ast);
ExpressionSimplifier::run(*m_context, *m_ast);
UnusedPruner::run(*m_context, *m_ast);
@ -244,8 +231,7 @@ bool YulOptimizerTester::runStep()
else if (m_optimizerStep == "unusedFunctionParameterPruner")
{
disambiguate();
if (m_fuzzerMode)
ForLoopInitRewriter::run(*m_context, *m_ast);
ForLoopInitRewriter::run(*m_context, *m_ast);
FunctionHoister::run(*m_context, *m_object->code);
LiteralRematerialiser::run(*m_context, *m_object->code);
UnusedFunctionParameterPruner::run(*m_context, *m_object->code);
@ -282,8 +268,7 @@ bool YulOptimizerTester::runStep()
else if (m_optimizerStep == "ssaPlusCleanup")
{
disambiguate();
if (m_fuzzerMode)
ForLoopInitRewriter::run(*m_context, *m_ast);
ForLoopInitRewriter::run(*m_context, *m_ast);
SSATransform::run(*m_context, *m_ast);
RedundantAssignEliminator::run(*m_context, *m_ast);
}
@ -291,8 +276,7 @@ bool YulOptimizerTester::runStep()
{
disambiguate();
ForLoopInitRewriter::run(*m_context, *m_ast);
if (m_fuzzerMode)
FunctionHoister::run(*m_context, *m_ast);
FunctionHoister::run(*m_context, *m_ast);
ExpressionSplitter::run(*m_context, *m_ast);
CommonSubexpressionEliminator::run(*m_context, *m_ast);
ExpressionSimplifier::run(*m_context, *m_ast);
@ -307,23 +291,20 @@ bool YulOptimizerTester::runStep()
{
disambiguate();
ForLoopInitRewriter::run(*m_context, *m_ast);
if (m_fuzzerMode)
FunctionHoister::run(*m_context, *m_ast);
FunctionHoister::run(*m_context, *m_ast);
LoopInvariantCodeMotion::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "controlFlowSimplifier")
{
disambiguate();
if (m_fuzzerMode)
ForLoopInitRewriter::run(*m_context, *m_ast);
ForLoopInitRewriter::run(*m_context, *m_ast);
ControlFlowSimplifier::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "structuralSimplifier")
{
disambiguate();
ForLoopInitRewriter::run(*m_context, *m_ast);
if (m_fuzzerMode)
FunctionHoister::run(*m_context, *m_ast);
FunctionHoister::run(*m_context, *m_ast);
LiteralRematerialiser::run(*m_context, *m_ast);
StructuralSimplifier::run(*m_context, *m_ast);
}
@ -335,11 +316,8 @@ bool YulOptimizerTester::runStep()
else if (m_optimizerStep == "equivalentFunctionCombiner")
{
disambiguate();
if (m_fuzzerMode)
{
ForLoopInitRewriter::run(*m_context, *m_ast);
FunctionHoister::run(*m_context, *m_ast);
}
ForLoopInitRewriter::run(*m_context, *m_ast);
FunctionHoister::run(*m_context, *m_ast);
EquivalentFunctionCombiner::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "ssaReverser")
@ -350,26 +328,21 @@ bool YulOptimizerTester::runStep()
else if (m_optimizerStep == "ssaAndBack")
{
disambiguate();
if (m_fuzzerMode)
ForLoopInitRewriter::run(*m_context, *m_ast);
ForLoopInitRewriter::run(*m_context, *m_ast);
// apply SSA
SSATransform::run(*m_context, *m_ast);
RedundantAssignEliminator::run(*m_context, *m_ast);
// reverse SSA
SSAReverser::run(*m_context, *m_ast);
if (m_fuzzerMode)
FunctionHoister::run(*m_context, *m_ast);
FunctionHoister::run(*m_context, *m_ast);
CommonSubexpressionEliminator::run(*m_context, *m_ast);
UnusedPruner::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "stackCompressor")
{
disambiguate();
if (m_fuzzerMode)
{
ForLoopInitRewriter::run(*m_context, *m_ast);
FunctionHoister::run(*m_context, *m_ast);
}
ForLoopInitRewriter::run(*m_context, *m_ast);
FunctionHoister::run(*m_context, *m_ast);
FunctionGrouper::run(*m_context, *m_ast);
size_t maxIterations = 16;
StackCompressor::run(*m_dialect, *m_object, true, maxIterations);
@ -438,23 +411,19 @@ bool YulOptimizerTester::runStep()
return true;
}
shared_ptr<Block> YulOptimizerTester::run()
shared_ptr<Block> YulOptimizerTestCommon::run()
{
bool success = runStep();
if (!success)
return nullptr;
else
return m_ast;
return runStep() ? m_ast : nullptr;
}
void YulOptimizerTester::disambiguate()
void YulOptimizerTestCommon::disambiguate()
{
*m_object->code = std::get<Block>(Disambiguator(*m_dialect, *m_analysisInfo)(*m_object->code));
m_analysisInfo.reset();
updateContext();
}
void YulOptimizerTester::updateContext()
void YulOptimizerTestCommon::updateContext()
{
m_nameDispenser = make_unique<NameDispenser>(*m_dialect, *m_object->code, m_reservedIdentifiers);
m_context = make_unique<OptimiserStepContext>(OptimiserStepContext{

View File

@ -35,14 +35,13 @@ namespace solidity::yul
namespace solidity::yul::test
{
class YulOptimizerTester
class YulOptimizerTestCommon
{
public:
explicit YulOptimizerTester(
explicit YulOptimizerTestCommon(
std::shared_ptr<Object> _obj,
Dialect const& _dialect,
std::string const& _optimizerStep,
bool _fuzzerMode
std::string const& _optimizerStep
);
/// Runs chosen optimiser step returning pointer
/// to yul AST Block post optimisation.
@ -64,7 +63,6 @@ private:
std::shared_ptr<Object> m_object;
std::shared_ptr<Block> m_ast;
std::shared_ptr<AsmAnalysisInfo> m_analysisInfo;
bool m_fuzzerMode;
};
}

View File

@ -27,6 +27,15 @@
// step: commonSubexpressionEliminator
//
// {
// let _6 := 0x20
// let _7 := allocate(_6)
// pop(_7)
// let _8 := 0x40
// let x := allocate(_8)
// let _9 := 2
// let _10 := 3
// let _11 := array_index_access(x, _10)
// mstore(_11, _9)
// function allocate(size) -> p
// {
// let _1 := 0x40
@ -41,13 +50,4 @@
// let _5 := mul(index, _4)
// p_1 := add(array, _5)
// }
// let _6 := 0x20
// let _7 := allocate(_6)
// pop(_7)
// let _8 := 0x40
// let x := allocate(_8)
// let _9 := 2
// let _10 := 3
// let _11 := array_index_access(x, _10)
// mstore(_11, _9)
// }

View File

@ -11,6 +11,11 @@
// step: commonSubexpressionEliminator
//
// {
// let i := mload(3)
// let a := double(i)
// let b := a
// let c := double_with_se(i)
// let d := double_with_se(i)
// function double(x) -> y
// { y := add(x, x) }
// function double_with_se(x_1) -> y_2
@ -18,9 +23,4 @@
// y_2 := add(x_1, x_1)
// mstore(40, 4)
// }
// let i := mload(3)
// let a := double(i)
// let b := a
// let c := double_with_se(i)
// let d := double_with_se(i)
// }

View File

@ -13,12 +13,12 @@
// step: commonSubexpressionEliminator
//
// {
// let a
// let b := a
// sstore(a, a)
// function f() -> x
// {
// let y := x
// mstore(x, 7)
// }
// let a
// let b := a
// sstore(a, a)
// }

View File

@ -8,8 +8,8 @@
// step: expressionSimplifier
//
// {
// function f() -> x, z
// { }
// let c, d := f()
// sstore(add(add(d, c), 7), 20)
// function f() -> x, z
// { }
// }

View File

@ -7,7 +7,7 @@
// step: expressionSimplifier
//
// {
// sstore(0, f())
// function f() -> a
// { }
// sstore(0, f())
// }

View File

@ -7,9 +7,9 @@
// step: expressionSimplifier
//
// {
// function f(a) -> b
// { }
// let _2 := f(1)
// let _3 := 0
// sstore(_3, sub(f(_3), _2))
// function f(a) -> b
// { }
// }

View File

@ -8,9 +8,9 @@
// step: expressionSimplifier
//
// {
// sstore(0, sub(f1(), f2()))
// function f1() -> a
// { }
// function f2() -> b
// { }
// sstore(0, sub(f1(), f2()))
// }

View File

@ -8,7 +8,7 @@
// step: expressionSimplifier
//
// {
// sstore(sub(f(), f()), 8)
// function f() -> a
// { mstore(a, 1) }
// sstore(sub(f(), f()), 8)
// }

View File

@ -8,8 +8,8 @@
// step: expressionSimplifier
//
// {
// function f() -> x
// { mstore(x, 1337) }
// pop(f())
// mstore(0, 0)
// function f() -> x
// { mstore(x, 1337) }
// }

View File

@ -10,7 +10,7 @@
// step: expressionSimplifier
//
// {
// let t, v := f()
// function f() -> c, d
// { sstore(d, 7) }
// let t, v := f()
// }

View File

@ -7,7 +7,7 @@
// step: fullSimplify
//
// {
// mstore(f(), 0)
// function f() -> a
// { }
// mstore(f(), 0)
// }

View File

@ -6,9 +6,9 @@
// step: fullSimplify
//
// {
// function f(a) -> b
// { }
// let _2 := f(1)
// let _3 := 0
// mstore(_3, sub(f(_3), _2))
// function f(a) -> b
// { }
// }

View File

@ -8,9 +8,9 @@
// step: fullSimplify
//
// {
// mstore(0, sub(f1(), f2()))
// function f1() -> a
// { }
// function f2() -> b
// { }
// mstore(0, sub(f1(), f2()))
// }

View File

@ -8,7 +8,7 @@
// step: fullSimplify
//
// {
// mstore(0, sub(f(), f()))
// function f() -> a
// { mstore(1, 2) }
// mstore(0, sub(f(), f()))
// }

View File

@ -10,20 +10,20 @@
// step: loadResolver
//
// {
// let _1 := 1337
// let _2 := 0
// mstore(_2, _1)
// foo(42)
// sstore(_2, mload(_2))
// function foo(x)
// {
// for { }
// x
// {
// let _1 := 0
// x := mload(_1)
// mstore(_1, _1)
// let _7 := 0
// x := mload(_7)
// mstore(_7, _7)
// }
// { }
// }
// let _4 := 1337
// let _5 := 0
// mstore(_5, _4)
// foo(42)
// sstore(_5, mload(_5))
// }

View File

@ -15,6 +15,11 @@
// step: loadResolver
//
// {
// let _1 := 1337
// let _2 := 0
// mstore(_2, _1)
// funcWithLoop(42)
// sstore(_2, mload(_2))
// function userNot(x) -> y
// { y := iszero(x) }
// function funcWithLoop(x_1)
@ -22,14 +27,9 @@
// for { }
// userNot(x_1)
// {
// let _1 := 0
// mstore(_1, _1)
// let _7 := 0
// mstore(_7, _7)
// }
// { }
// }
// let _3 := 1337
// let _4 := 0
// mstore(_4, _3)
// funcWithLoop(42)
// sstore(_4, mload(_4))
// }

View File

@ -15,18 +15,18 @@
// step: loadResolver
//
// {
// let _1 := 1337
// let _2 := 0
// mstore(_2, _1)
// funcWithLoop(42)
// sstore(_2, mload(_2))
// function userNot(x) -> y
// { y := iszero(x) }
// function funcWithLoop(x_1)
// {
// let _1 := 0
// mstore(_1, _1)
// let _7 := 0
// mstore(_7, _7)
// for { } userNot(x_1) { }
// { }
// }
// let _3 := 1337
// let _4 := 0
// mstore(_4, _3)
// funcWithLoop(42)
// sstore(_4, mload(_4))
// }

View File

@ -12,17 +12,17 @@
// step: loadResolver
//
// {
// let _1 := 9
// let _2 := 2
// mstore(_2, _1)
// reads()
// let _4 := _1
// let _5 := 0
// sstore(_5, _4)
// stores()
// sstore(_5, mload(_2))
// function stores()
// { mstore(0, 1) }
// function reads()
// { sstore(9, mload(7)) }
// let _6 := 9
// let _7 := 2
// mstore(_7, _6)
// reads()
// let _9 := _6
// let _10 := 0
// sstore(_10, _9)
// stores()
// sstore(_10, mload(_7))
// }

View File

@ -15,10 +15,6 @@
// step: loopInvariantCodeMotion
//
// {
// function g() -> x
// { x := create(100, 0, 32) }
// function f() -> x_1
// { x_1 := mload(0) }
// let b := 1
// let a := 1
// let z := f()
@ -27,4 +23,8 @@
// let q := sload(5)
// let r := g()
// }
// function g() -> x
// { x := create(100, 0, 32) }
// function f() -> x_1
// { x_1 := mload(0) }
// }

View File

@ -12,8 +12,6 @@
// step: loopInvariantCodeMotion
//
// {
// function g() -> x
// { x := add(sload(mload(x)), 1) }
// let b := 1
// let a := 1
// let t := mload(g())
@ -21,4 +19,6 @@
// let q := g()
// for { } iszero(eq(a, 10)) { a := add(a, 1) }
// { }
// function g() -> x
// { x := add(sload(mload(x)), 1) }
// }

View File

@ -12,14 +12,14 @@
// step: loopInvariantCodeMotion
//
// {
// function f() -> x
// { x := mload(g()) }
// function g() -> x_1
// { x_1 := add(sload(x_1), 1) }
// let b := 1
// let a := 1
// let t := extcodesize(f())
// let q := g()
// for { } iszero(eq(a, 10)) { a := add(a, 1) }
// { }
// function f() -> x
// { x := mload(g()) }
// function g() -> x_1
// { x_1 := add(sload(x_1), 1) }
// }

View File

@ -12,14 +12,14 @@
// step: loopInvariantCodeMotion
//
// {
// function f() -> x
// { x := g() }
// function g() -> x_1
// { x_1 := add(x_1, 1) }
// let b := 1
// let a := 1
// let t := sload(f())
// let q := g()
// for { } iszero(eq(a, 10)) { a := add(a, 1) }
// { }
// function f() -> x
// { x := g() }
// function g() -> x_1
// { x_1 := add(x_1, 1) }
// }

View File

@ -14,10 +14,6 @@
//
// {
// let a := 1
// function f() -> x
// { invalid() }
// function g() -> y
// { return(0, 0) }
// let i := 1
// for { } iszero(eq(i, 10)) { a := add(i, 1) }
// {
@ -26,4 +22,8 @@
// let d := g()
// let e := sload(g())
// }
// function f() -> x
// { invalid() }
// function g() -> y
// { return(0, 0) }
// }

View File

@ -12,13 +12,6 @@
// step: loopInvariantCodeMotion
//
// {
// function f() -> x
// { x := g() }
// function g() -> x_1
// {
// for { } 1 { }
// { }
// }
// let b := 1
// let a := 1
// for { } iszero(eq(a, 10)) { a := add(a, 1) }
@ -26,4 +19,11 @@
// let t := f()
// let q := g()
// }
// function f() -> x
// { x := g() }
// function g() -> x_1
// {
// for { } 1 { }
// { }
// }
// }

View File

@ -12,13 +12,6 @@
// step: loopInvariantCodeMotion
//
// {
// function f() -> x
// { x := g() }
// function g() -> x_1
// {
// for { } 1 { }
// { }
// }
// let b := 1
// let a := 1
// for { } iszero(eq(a, 10)) { a := add(a, 1) }
@ -26,4 +19,11 @@
// let t := mload(f())
// let q := g()
// }
// function f() -> x
// { x := g() }
// function g() -> x_1
// {
// for { } 1 { }
// { }
// }
// }

View File

@ -12,10 +12,6 @@
// step: loopInvariantCodeMotion
//
// {
// function f() -> x
// { x := g() }
// function g() -> x_1
// { x_1 := g() }
// let b := 1
// let a := 1
// for { } iszero(eq(a, 10)) { a := add(a, 1) }
@ -23,4 +19,8 @@
// let t := f()
// let q := g()
// }
// function f() -> x
// { x := g() }
// function g() -> x_1
// { x_1 := g() }
// }

View File

@ -15,13 +15,6 @@
// step: loopInvariantCodeMotion
//
// {
// function f() -> x
// { x := g() }
// function g() -> x_1
// {
// x_1 := add(x_1, 1)
// sstore(0x00, 0x00)
// }
// let b := 1
// let a := 1
// for { } iszero(eq(a, 10)) { a := add(a, 1) }
@ -29,4 +22,11 @@
// let t := extcodesize(f())
// let q := sload(g())
// }
// function f() -> x
// { x := g() }
// function g() -> x_1
// {
// x_1 := add(x_1, 1)
// sstore(0x00, 0x00)
// }
// }

View File

@ -12,13 +12,6 @@
// step: loopInvariantCodeMotion
//
// {
// function f() -> x
// { x := g() }
// function g() -> x_1
// {
// for { } 1 { }
// { }
// }
// let b := 1
// let a := 1
// for { } iszero(eq(a, 10)) { a := add(a, 1) }
@ -26,4 +19,11 @@
// let t := extcodesize(f())
// let q := g()
// }
// function f() -> x
// { x := g() }
// function g() -> x_1
// {
// for { } 1 { }
// { }
// }
// }

View File

@ -12,10 +12,6 @@
// step: loopInvariantCodeMotion
//
// {
// function f() -> x
// { x := g() }
// function g() -> x_1
// { x_1 := g() }
// let b := 1
// let a := 1
// for { } iszero(eq(a, 10)) { a := add(a, 1) }
@ -23,4 +19,8 @@
// let t := extcodesize(f())
// let q := sload(g())
// }
// function f() -> x
// { x := g() }
// function g() -> x_1
// { x_1 := g() }
// }

View File

@ -14,6 +14,10 @@
// step: loopInvariantCodeMotion
//
// {
// let b := 1
// let a := 1
// for { } iszero(eq(a, 10)) { a := add(a, 1) }
// { let q := sload(g()) }
// function f() -> x
// { x := g() }
// function g() -> x_1
@ -21,8 +25,4 @@
// x_1 := add(x_1, 1)
// sstore(0x00, 0x00)
// }
// let b := 1
// let a := 1
// for { } iszero(eq(a, 10)) { a := add(a, 1) }
// { let q := sload(g()) }
// }

View File

@ -11,6 +11,10 @@
// step: loopInvariantCodeMotion
//
// {
// let b := 1
// let a := 1
// for { } iszero(eq(a, 10)) { a := add(a, 1) }
// { let t := sload(f()) }
// function f() -> x
// { x := g() }
// function g() -> x_1
@ -18,8 +22,4 @@
// for { } 1 { }
// { }
// }
// let b := 1
// let a := 1
// for { } iszero(eq(a, 10)) { a := add(a, 1) }
// { let t := sload(f()) }
// }

View File

@ -11,11 +11,11 @@
// step: loopInvariantCodeMotion
//
// {
// function g() -> x
// { x := add(mload(x), 1) }
// let b := 1
// let a := 1
// let q := keccak256(g(), 32)
// for { } iszero(eq(a, 10)) { a := add(a, 1) }
// { sstore(0, a) }
// function g() -> x
// { x := add(mload(x), 1) }
// }

View File

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

View File

@ -24,6 +24,8 @@
// step: ssaAndBack
//
// {
// let a, b := abi_decode_t_bytes_calldata_ptr(mload(0), mload(1))
// mstore(a, b)
// function abi_decode_t_bytes_calldata_ptr(offset_12, end_13) -> arrayPos_14, length_15
// {
// if iszero(slt(add(offset_12, 0x1f), end_13))
@ -38,6 +40,4 @@
// arrayPos_14 := add(offset_12, 0x20)
// if gt(add(add(offset_12, length_15), 0x20), end_13) { revert(0, 0) }
// }
// let a, b := abi_decode_t_bytes_calldata_ptr(mload(0), mload(1))
// mstore(a, b)
// }