Pass "optimize stack allocation" flag down to the stack compressor.

This commit is contained in:
chriseth 2019-03-13 17:44:16 +01:00
parent 556d11dae0
commit 38cbf8d230
11 changed files with 27 additions and 14 deletions

View File

@ -428,6 +428,7 @@ void CompilerContext::appendInlineAssembly(
yul::EVMDialect::strictAssemblyForEVM(m_evmVersion),
*parserResult,
analysisInfo,
_optimiserSettings.optimizeStackAllocation,
externallyUsedIdentifiers
);
analysisInfo = yul::AsmAnalysisInfo{};

View File

@ -135,7 +135,10 @@ void AssemblyStack::optimize(Object& _object)
for (auto& subNode: _object.subObjects)
if (auto subObject = dynamic_cast<Object*>(subNode.get()))
optimize(*subObject);
OptimiserSuite::run(languageToDialect(m_language, m_evmVersion), *_object.code, *_object.analysisInfo);
// TODO: Store this as setting - it should be the same as the flag passed to
// ::assemble(...)
bool optimizeStackAllocation = false;
OptimiserSuite::run(languageToDialect(m_language, m_evmVersion), *_object.code, *_object.analysisInfo, optimizeStackAllocation);
}
MachineAssemblyObject AssemblyStack::assemble(Machine _machine, bool _optimize) const

View File

@ -33,7 +33,11 @@ using namespace yul;
using namespace dev;
using namespace dev::solidity;
std::map<YulString, int> CompilabilityChecker::run(std::shared_ptr<Dialect> _dialect, Block const& _ast)
map<YulString, int> CompilabilityChecker::run(
shared_ptr<Dialect> _dialect,
Block const& _ast,
bool _optimizeStackAllocation
)
{
if (_dialect->flavour == AsmFlavour::Yul)
return {};
@ -43,12 +47,11 @@ std::map<YulString, int> CompilabilityChecker::run(std::shared_ptr<Dialect> _dia
solAssert(dynamic_cast<EVMDialect const*>(_dialect.get()), "");
shared_ptr<NoOutputEVMDialect> noOutputDialect = make_shared<NoOutputEVMDialect>(dynamic_pointer_cast<EVMDialect>(_dialect));
bool optimize = true;
yul::AsmAnalysisInfo analysisInfo =
yul::AsmAnalyzer::analyzeStrictAssertCorrect(noOutputDialect, _ast);
NoOutputAssembly assembly;
CodeTransform transform(assembly, analysisInfo, _ast, *noOutputDialect, optimize);
CodeTransform transform(assembly, analysisInfo, _ast, *noOutputDialect, _optimizeStackAllocation);
try
{
transform(_ast);

View File

@ -39,7 +39,11 @@ namespace yul
class CompilabilityChecker
{
public:
static std::map<YulString, int> run(std::shared_ptr<Dialect> _dialect, Block const& _ast);
static std::map<YulString, int> run(
std::shared_ptr<Dialect> _dialect,
Block const& _ast,
bool _optimizeStackAllocation
);
};
}

View File

@ -74,15 +74,15 @@ void eliminateVariables(shared_ptr<Dialect> const& _dialect, ASTNode& _node, siz
}
bool StackCompressor::run(shared_ptr<Dialect> const& _dialect, Block& _ast)
bool StackCompressor::run(shared_ptr<Dialect> const& _dialect, Block& _ast, bool _optimizeStackAllocation)
{
yulAssert(
_ast.statements.size() > 0 && _ast.statements.at(0).type() == typeid(Block),
"Need to run the function grouper before the stack compressor."
);
for (size_t iterations = 0; iterations < 4; iterations++)
for (size_t iterations = 0; iterations < 6; iterations++)
{
map<YulString, int> stackSurplus = CompilabilityChecker::run(_dialect, _ast);
map<YulString, int> stackSurplus = CompilabilityChecker::run(_dialect, _ast, _optimizeStackAllocation);
if (stackSurplus.empty())
return true;

View File

@ -41,7 +41,7 @@ class StackCompressor
public:
/// Try to remove local variables until the AST is compilable.
/// @returns true if it was successful.
static bool run(std::shared_ptr<Dialect> const& _dialect, Block& _ast);
static bool run(std::shared_ptr<Dialect> const& _dialect, Block& _ast, bool _optimizeStackAllocation);
};
}

View File

@ -59,6 +59,7 @@ void OptimiserSuite::run(
shared_ptr<Dialect> const& _dialect,
Block& _ast,
AsmAnalysisInfo const& _analysisInfo,
bool _optimizeStackAllocation,
set<YulString> const& _externallyUsedIdentifiers
)
{
@ -185,7 +186,7 @@ void OptimiserSuite::run(
UnusedPruner::runUntilStabilised(*_dialect, ast, reservedIdentifiers);
FunctionGrouper{}(ast);
StackCompressor::run(_dialect, ast);
solAssert(StackCompressor::run(_dialect, ast, _optimizeStackAllocation), "");
BlockFlattener{}(ast);
VarNameCleaner{ast, *_dialect, reservedIdentifiers}(ast);

View File

@ -42,6 +42,7 @@ public:
std::shared_ptr<Dialect> const& _dialect,
Block& _ast,
AsmAnalysisInfo const& _analysisInfo,
bool _optimizeStackAllocation,
std::set<YulString> const& _externallyUsedIdentifiers = {}
);
};

View File

@ -39,7 +39,7 @@ string check(string const& _input)
{
shared_ptr<Block> ast = yul::test::parse(_input, false).first;
BOOST_REQUIRE(ast);
map<YulString, int> functions = CompilabilityChecker::run(EVMDialect::strictAssemblyForEVM(dev::test::Options::get().evmVersion()), *ast);
map<YulString, int> functions = CompilabilityChecker::run(EVMDialect::strictAssemblyForEVM(dev::test::Options::get().evmVersion()), *ast, true);
string out;
for (auto const& function: functions)
out += function.first.str() + ": " + to_string(function.second) + " ";

View File

@ -247,11 +247,11 @@ bool YulOptimizerTest::run(ostream& _stream, string const& _linePrefix, bool con
{
disambiguate();
(FunctionGrouper{})(*m_ast);
StackCompressor::run(m_dialect, *m_ast);
StackCompressor::run(m_dialect, *m_ast, true);
(BlockFlattener{})(*m_ast);
}
else if (m_optimizerStep == "fullSuite")
OptimiserSuite::run(m_dialect, *m_ast, *m_analysisInfo);
OptimiserSuite::run(m_dialect, *m_ast, *m_analysisInfo, true);
else
{
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Invalid optimizer step: " << m_optimizerStep << endl;

View File

@ -198,7 +198,7 @@ public:
SSAReverser::run(*m_ast);
break;
case 'p':
StackCompressor::run(m_dialect, *m_ast);
StackCompressor::run(m_dialect, *m_ast, true);
break;
default:
cout << "Unknown option." << endl;