Merge pull request #6963 from ethereum/runOptimizerOnAllDialects

[Yul] Run optimizer on all dialects.
This commit is contained in:
chriseth 2019-06-19 10:51:24 +02:00 committed by GitHub
commit 53f26d9719
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 29 additions and 20 deletions

View File

@ -422,9 +422,10 @@ void CompilerContext::appendInlineAssembly(
if (_optimiserSettings.runYulOptimiser && _localVariables.empty())
{
bool const isCreation = m_runtimeContext != nullptr;
yul::GasMeter meter(dialect, isCreation, _optimiserSettings.expectedExecutionsPerDeployment);
yul::OptimiserSuite::run(
dialect,
yul::GasMeter(dialect, isCreation, _optimiserSettings.expectedExecutionsPerDeployment),
&meter,
*parserResult,
analysisInfo,
_optimiserSettings.optimizeStackAllocation,

View File

@ -94,8 +94,6 @@ void AssemblyStack::optimize()
if (!m_optimiserSettings.runYulOptimiser)
return;
if (m_language != Language::StrictAssembly)
solUnimplemented("Optimizer for both loose assembly and Yul is not yet implemented");
solAssert(m_analysisSuccessful, "Analysis was not successful.");
m_analysisSuccessful = false;
@ -147,17 +145,18 @@ void AssemblyStack::optimize(Object& _object, bool _isCreation)
for (auto& subNode: _object.subObjects)
if (auto subObject = dynamic_cast<Object*>(subNode.get()))
optimize(*subObject, false);
if (EVMDialect const* dialect = dynamic_cast<EVMDialect const*>(&languageToDialect(m_language, m_evmVersion)))
{
GasMeter meter(*dialect, _isCreation, m_optimiserSettings.expectedExecutionsPerDeployment);
OptimiserSuite::run(
*dialect,
meter,
*_object.code,
*_object.analysisInfo,
m_optimiserSettings.optimizeStackAllocation
);
}
Dialect const& dialect = languageToDialect(m_language, m_evmVersion);
unique_ptr<GasMeter> meter;
if (EVMDialect const* evmDialect = dynamic_cast<EVMDialect const*>(&dialect))
meter = make_unique<GasMeter>(*evmDialect, _isCreation, m_optimiserSettings.expectedExecutionsPerDeployment);
OptimiserSuite::run(
dialect,
meter.get(),
*_object.code,
*_object.analysisInfo,
m_optimiserSettings.optimizeStackAllocation
);
}
MachineAssemblyObject AssemblyStack::assemble(Machine _machine) const

View File

@ -25,6 +25,7 @@
#include <boost/noncopyable.hpp>
#include <vector>
#include <set>
namespace yul
{
@ -69,6 +70,8 @@ struct Dialect: boost::noncopyable
virtual BuiltinFunction const* discardFunction() const { return nullptr; }
virtual BuiltinFunction const* equalityFunction() const { return nullptr; }
virtual std::set<YulString> fixedFunctionNames() const { return {}; }
Dialect(AsmFlavour _flavour): flavour(_flavour) {}
virtual ~Dialect() = default;

View File

@ -115,9 +115,9 @@ wasm::Expression EWasmCodeTransform::operator()(Label const&)
return {};
}
wasm::Expression EWasmCodeTransform::operator()(FunctionalInstruction const&)
wasm::Expression EWasmCodeTransform::operator()(FunctionalInstruction const& _f)
{
yulAssert(false, "");
yulAssert(false, "EVM instruction in ewasm code: " + eth::instructionInfo(_f.instruction).name);
return {};
}

View File

@ -48,6 +48,8 @@ struct WasmDialect: public Dialect
BuiltinFunction const* discardFunction() const override { return builtin("drop"_yulstring); }
BuiltinFunction const* equalityFunction() const override { return builtin("i64.eq"_yulstring); }
std::set<YulString> fixedFunctionNames() const override { return {"main"_yulstring}; }
static WasmDialect const& instance();
private:

View File

@ -60,7 +60,7 @@ using namespace yul;
void OptimiserSuite::run(
Dialect const& _dialect,
GasMeter const& _meter,
GasMeter const* _meter,
Block& _ast,
AsmAnalysisInfo const& _analysisInfo,
bool _optimizeStackAllocation,
@ -68,6 +68,7 @@ void OptimiserSuite::run(
)
{
set<YulString> reservedIdentifiers = _externallyUsedIdentifiers;
reservedIdentifiers += _dialect.fixedFunctionNames();
Block ast = boost::get<Block>(Disambiguator(_dialect, _analysisInfo, reservedIdentifiers)(_ast));
@ -210,7 +211,10 @@ void OptimiserSuite::run(
FunctionGrouper{}(ast);
if (EVMDialect const* dialect = dynamic_cast<EVMDialect const*>(&_dialect))
ConstantOptimiser{*dialect, _meter}(ast);
{
yulAssert(_meter, "");
ConstantOptimiser{*dialect, *_meter}(ast);
}
VarNameCleaner{ast, _dialect, reservedIdentifiers}(ast);
yul::AsmAnalyzer::analyzeStrictAssertCorrect(_dialect, ast);

View File

@ -41,7 +41,7 @@ class OptimiserSuite
public:
static void run(
Dialect const& _dialect,
GasMeter const& _meter,
GasMeter const* _meter,
Block& _ast,
AsmAnalysisInfo const& _analysisInfo,
bool _optimizeStackAllocation,

View File

@ -292,7 +292,7 @@ TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _line
else if (m_optimizerStep == "fullSuite")
{
GasMeter meter(dynamic_cast<EVMDialect const&>(*m_dialect), false, 200);
OptimiserSuite::run(*m_dialect, meter, *m_ast, *m_analysisInfo, true);
OptimiserSuite::run(*m_dialect, &meter, *m_ast, *m_analysisInfo, true);
}
else
{