Make Yul optimizer not fail for wasm.

This commit is contained in:
chriseth
2019-06-17 18:42:47 +02:00
parent 8260ae1397
commit 6cb6fe35ef
17 changed files with 321 additions and 210 deletions
+225
View File
@@ -0,0 +1,225 @@
/*
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/>.
*/
/**
* Optimisation stage that replaces constants by expressions that compute them.
*/
#include <libyul/backends/evm/ConstantOptimiser.h>
#include <libyul/optimiser/ASTCopier.h>
#include <libyul/backends/evm/EVMMetrics.h>
#include <libyul/AsmData.h>
#include <libyul/Utilities.h>
#include <libdevcore/CommonData.h>
using namespace std;
using namespace dev;
using namespace yul;
using Representation = ConstantOptimiser::Representation;
namespace
{
struct MiniEVMInterpreter: boost::static_visitor<u256>
{
explicit MiniEVMInterpreter(EVMDialect const& _dialect): m_dialect(_dialect) {}
u256 eval(Expression const& _expr)
{
return boost::apply_visitor(*this, _expr);
}
u256 eval(dev::eth::Instruction _instr, vector<Expression> const& _arguments)
{
vector<u256> args;
for (auto const& arg: _arguments)
args.emplace_back(eval(arg));
switch (_instr)
{
case eth::Instruction::ADD:
return args.at(0) + args.at(1);
case eth::Instruction::SUB:
return args.at(0) - args.at(1);
case eth::Instruction::MUL:
return args.at(0) * args.at(1);
case eth::Instruction::EXP:
return exp256(args.at(0), args.at(1));
case eth::Instruction::SHL:
return args.at(0) > 255 ? 0 : (args.at(1) << unsigned(args.at(0)));
case eth::Instruction::NOT:
return ~args.at(0);
default:
yulAssert(false, "Invalid operation generated in constant optimizer.");
}
return 0;
}
u256 operator()(FunctionalInstruction const& _instr)
{
return eval(_instr.instruction, _instr.arguments);
}
u256 operator()(FunctionCall const& _funCall)
{
BuiltinFunctionForEVM const* fun = m_dialect.builtin(_funCall.functionName.name);
yulAssert(fun, "Expected builtin function.");
yulAssert(fun->instruction, "Expected EVM instruction.");
return eval(*fun->instruction, _funCall.arguments);
}
u256 operator()(Literal const& _literal)
{
return valueOfLiteral(_literal);
}
u256 operator()(Identifier const&) { yulAssert(false, ""); }
EVMDialect const& m_dialect;
};
}
void ConstantOptimiser::visit(Expression& _e)
{
if (_e.type() == typeid(Literal))
{
Literal const& literal = boost::get<Literal>(_e);
if (literal.kind != LiteralKind::Number)
return;
if (
Expression const* repr =
RepresentationFinder(m_dialect, m_meter, locationOf(_e), m_cache)
.tryFindRepresentation(valueOfLiteral(literal))
)
_e = ASTCopier{}.translate(*repr);
}
else
ASTModifier::visit(_e);
}
Expression const* RepresentationFinder::tryFindRepresentation(dev::u256 const& _value)
{
if (_value < 0x10000)
return nullptr;
Representation const& repr = findRepresentation(_value);
if (repr.expression->type() == typeid(Literal))
return nullptr;
else
return repr.expression.get();
}
Representation const& RepresentationFinder::findRepresentation(dev::u256 const& _value)
{
if (m_cache.count(_value))
return m_cache.at(_value);
Representation routine = represent(_value);
if (dev::bytesRequired(~_value) < dev::bytesRequired(_value))
// Negated is shorter to represent
routine = min(move(routine), represent("not"_yulstring, findRepresentation(~_value)));
// Decompose value into a * 2**k + b where abs(b) << 2**k
for (unsigned bits = 255; bits > 8 && m_maxSteps > 0; --bits)
{
unsigned gapDetector = unsigned((_value >> (bits - 8)) & 0x1ff);
if (gapDetector != 0xff && gapDetector != 0x100)
continue;
u256 powerOfTwo = u256(1) << bits;
u256 upperPart = _value >> bits;
bigint lowerPart = _value & (powerOfTwo - 1);
if ((powerOfTwo - lowerPart) < lowerPart)
{
lowerPart = lowerPart - powerOfTwo; // make it negative
upperPart++;
}
if (upperPart == 0)
continue;
if (abs(lowerPart) >= (powerOfTwo >> 8))
continue;
Representation newRoutine;
if (m_dialect.evmVersion().hasBitwiseShifting())
newRoutine = represent("shl"_yulstring, represent(bits), findRepresentation(upperPart));
else
{
newRoutine = represent("exp"_yulstring, represent(2), represent(bits));
if (upperPart != 1)
newRoutine = represent("mul"_yulstring, findRepresentation(upperPart), newRoutine);
}
if (newRoutine.cost >= routine.cost)
continue;
if (lowerPart > 0)
newRoutine = represent("add"_yulstring, newRoutine, findRepresentation(u256(abs(lowerPart))));
else if (lowerPart < 0)
newRoutine = represent("sub"_yulstring, newRoutine, findRepresentation(u256(abs(lowerPart))));
if (m_maxSteps > 0)
m_maxSteps--;
routine = min(move(routine), move(newRoutine));
}
yulAssert(MiniEVMInterpreter{m_dialect}.eval(*routine.expression) == _value, "Invalid expression generated.");
return m_cache[_value] = move(routine);
}
Representation RepresentationFinder::represent(dev::u256 const& _value) const
{
Representation repr;
repr.expression = make_unique<Expression>(Literal{m_location, LiteralKind::Number, YulString{formatNumber(_value)}, {}});
repr.cost = m_meter.costs(*repr.expression);
return repr;
}
Representation RepresentationFinder::represent(
YulString _instruction,
Representation const& _argument
) const
{
Representation repr;
repr.expression = make_unique<Expression>(FunctionCall{
m_location,
Identifier{m_location, _instruction},
{ASTCopier{}.translate(*_argument.expression)}
});
repr.cost = _argument.cost + m_meter.instructionCosts(*m_dialect.builtin(_instruction)->instruction);
return repr;
}
Representation RepresentationFinder::represent(
YulString _instruction,
Representation const& _arg1,
Representation const& _arg2
) const
{
Representation repr;
repr.expression = make_unique<Expression>(FunctionCall{
m_location,
Identifier{m_location, _instruction},
{ASTCopier{}.translate(*_arg1.expression), ASTCopier{}.translate(*_arg2.expression)}
});
repr.cost = m_meter.instructionCosts(*m_dialect.builtin(_instruction)->instruction) + _arg1.cost + _arg2.cost;
return repr;
}
Representation RepresentationFinder::min(Representation _a, Representation _b)
{
if (_a.cost <= _b.cost)
return _a;
else
return _b;
}
+108
View File
@@ -0,0 +1,108 @@
/*
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/>.
*/
/**
* Optimisation stage that replaces constants by expressions that compute them.
*/
#pragma once
#include <libyul/optimiser/ASTWalker.h>
#include <libyul/YulString.h>
#include <libyul/Dialect.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <libyul/AsmData.h>
#include <liblangutil/SourceLocation.h>
#include <libdevcore/Common.h>
#include <tuple>
#include <map>
#include <memory>
namespace yul
{
struct Dialect;
class GasMeter;
/**
* Optimisation stage that replaces constants by expressions that compute them.
*
* Prerequisite: None
*/
class ConstantOptimiser: public ASTModifier
{
public:
ConstantOptimiser(EVMDialect const& _dialect, GasMeter const& _meter):
m_dialect(_dialect),
m_meter(_meter)
{}
void visit(Expression& _e) override;
struct Representation
{
std::unique_ptr<Expression> expression;
size_t cost = size_t(-1);
};
private:
EVMDialect const& m_dialect;
GasMeter const& m_meter;
std::map<dev::u256, Representation> m_cache;
};
class RepresentationFinder
{
public:
using Representation = ConstantOptimiser::Representation;
RepresentationFinder(
EVMDialect const& _dialect,
GasMeter const& _meter,
langutil::SourceLocation _location,
std::map<dev::u256, Representation>& _cache
):
m_dialect(_dialect),
m_meter(_meter),
m_location(std::move(_location)),
m_cache(_cache)
{}
/// @returns a cheaper representation for the number than its representation
/// as a literal or nullptr otherwise.
Expression const* tryFindRepresentation(dev::u256 const& _value);
private:
/// Recursively try to find the cheapest representation of the given number,
/// literal if necessary.
Representation const& findRepresentation(dev::u256 const& _value);
Representation represent(dev::u256 const& _value) const;
Representation represent(YulString _instruction, Representation const& _arg) const;
Representation represent(YulString _instruction, Representation const& _arg1, Representation const& _arg2) const;
Representation min(Representation _a, Representation _b);
EVMDialect const& m_dialect;
GasMeter const& m_meter;
langutil::SourceLocation m_location;
/// Counter for the complexity of optimization, will stop when it reaches zero.
size_t m_maxSteps = 10000;
std::map<dev::u256, Representation>& m_cache;
};
}
+3
View File
@@ -68,6 +68,9 @@ struct EVMDialect: public Dialect
/// @returns the builtin function of the given name or a nullptr if it is not a builtin function.
BuiltinFunctionForEVM const* builtin(YulString _name) const override;
BuiltinFunctionForEVM const* discardFunction() const override { return builtin("pop"_yulstring); }
BuiltinFunctionForEVM const* equalityFunction() const override { return builtin("eq"_yulstring); }
static EVMDialect const& looseAssemblyForEVM(langutil::EVMVersion _version);
static EVMDialect const& strictAssemblyForEVM(langutil::EVMVersion _version);
static EVMDialect const& strictAssemblyForEVMObjects(langutil::EVMVersion _version);
+123
View File
@@ -0,0 +1,123 @@
/*
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/>.
*/
/**
* Module providing metrics for the EVM optimizer.
*/
#include <libyul/backends/evm/EVMMetrics.h>
#include <libyul/AsmData.h>
#include <libyul/Exceptions.h>
#include <libyul/Utilities.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <libevmasm/Instruction.h>
#include <libevmasm/GasMeter.h>
#include <libdevcore/Visitor.h>
#include <libdevcore/CommonData.h>
using namespace std;
using namespace dev;
using namespace yul;
size_t GasMeter::costs(Expression const& _expression) const
{
return combineCosts(GasMeterVisitor::costs(_expression, m_dialect, m_isCreation));
}
size_t GasMeter::instructionCosts(eth::Instruction _instruction) const
{
return combineCosts(GasMeterVisitor::instructionCosts(_instruction, m_dialect, m_isCreation));
}
size_t GasMeter::combineCosts(std::pair<size_t, size_t> _costs) const
{
return _costs.first * m_runs + _costs.second;
}
pair<size_t, size_t> GasMeterVisitor::costs(
Expression const& _expression,
EVMDialect const& _dialect,
bool _isCreation
)
{
GasMeterVisitor gmv(_dialect, _isCreation);
gmv.visit(_expression);
return {gmv.m_runGas, gmv.m_dataGas};
}
pair<size_t, size_t> GasMeterVisitor::instructionCosts(
dev::eth::Instruction _instruction,
EVMDialect const& _dialect,
bool _isCreation
)
{
GasMeterVisitor gmv(_dialect, _isCreation);
gmv.instructionCostsInternal(_instruction);
return {gmv.m_runGas, gmv.m_dataGas};
}
void GasMeterVisitor::operator()(FunctionCall const& _funCall)
{
ASTWalker::operator()(_funCall);
if (BuiltinFunctionForEVM const* f = m_dialect.builtin(_funCall.functionName.name))
if (f->instruction)
{
instructionCostsInternal(*f->instruction);
return;
}
yulAssert(false, "Functions not implemented.");
}
void GasMeterVisitor::operator()(FunctionalInstruction const& _fun)
{
ASTWalker::operator()(_fun);
instructionCostsInternal(_fun.instruction);
}
void GasMeterVisitor::operator()(Literal const& _lit)
{
m_runGas += dev::eth::GasMeter::runGas(dev::eth::Instruction::PUSH1);
m_dataGas +=
singleByteDataGas() +
size_t(dev::eth::GasMeter::dataGas(dev::toCompactBigEndian(valueOfLiteral(_lit), 1), m_isCreation));
}
void GasMeterVisitor::operator()(Identifier const&)
{
m_runGas += dev::eth::GasMeter::runGas(dev::eth::Instruction::DUP1);
m_dataGas += singleByteDataGas();
}
size_t GasMeterVisitor::singleByteDataGas() const
{
if (m_isCreation)
return dev::eth::GasCosts::txDataNonZeroGas;
else
return dev::eth::GasCosts::createDataGas;
}
void GasMeterVisitor::instructionCostsInternal(dev::eth::Instruction _instruction)
{
if (_instruction == eth::Instruction::EXP)
m_runGas += dev::eth::GasCosts::expGas + dev::eth::GasCosts::expByteGas(m_dialect.evmVersion());
else
m_runGas += dev::eth::GasMeter::runGas(_instruction);
m_dataGas += singleByteDataGas();
}
+101
View File
@@ -0,0 +1,101 @@
/*
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/>.
*/
/**
* Module providing metrics for the optimizer.
*/
#pragma once
#include <libyul/optimiser/ASTWalker.h>
#include <liblangutil/EVMVersion.h>
#include <libevmasm/Instruction.h>
namespace yul
{
struct EVMDialect;
/**
* Gas meter for expressions only involving literals, identifiers and
* EVM instructions.
*
* Assumes that EXP is not used with exponents larger than a single byte.
* Is not particularly exact for anything apart from arithmetic.
*/
class GasMeter
{
public:
GasMeter(EVMDialect const& _dialect, bool _isCreation, size_t _runs):
m_dialect(_dialect),
m_isCreation{_isCreation},
m_runs(_runs)
{}
/// @returns the full combined costs of deploying and evaluating the expression.
size_t costs(Expression const& _expression) const;
/// @returns the combined costs of deploying and running the instruction, not including
/// the costs for its arguments.
size_t instructionCosts(dev::eth::Instruction _instruction) const;
private:
size_t combineCosts(std::pair<size_t, size_t> _costs) const;
EVMDialect const& m_dialect;
bool m_isCreation = false;
size_t m_runs;
};
class GasMeterVisitor: public ASTWalker
{
public:
static std::pair<size_t, size_t> costs(
Expression const& _expression,
EVMDialect const& _dialect,
bool _isCreation
);
static std::pair<size_t, size_t> instructionCosts(
dev::eth::Instruction _instruction,
EVMDialect const& _dialect,
bool _isCreation = false
);
public:
GasMeterVisitor(EVMDialect const& _dialect, bool _isCreation):
m_dialect(_dialect),
m_isCreation{_isCreation}
{}
void operator()(FunctionCall const& _funCall) override;
void operator()(FunctionalInstruction const& _instr) override;
void operator()(Literal const& _literal) override;
void operator()(Identifier const& _identifier) override;
private:
size_t singleByteDataGas() const;
/// Computes the cost of storing and executing the single instruction (excluding its arguments).
/// For EXP, it assumes that the exponent is at most 255.
/// Does not work particularly exact for anything apart from arithmetic.
void instructionCostsInternal(dev::eth::Instruction _instruction);
EVMDialect const& m_dialect;
bool m_isCreation = false;
size_t m_runGas = 0;
size_t m_dataGas = 0;
};
}
+2
View File
@@ -45,6 +45,8 @@ struct WasmDialect: public Dialect
WasmDialect();
BuiltinFunction const* builtin(YulString _name) const override;
BuiltinFunction const* discardFunction() const override { return builtin("drop"_yulstring); }
BuiltinFunction const* equalityFunction() const override { return builtin("i64.eq"_yulstring); }
static WasmDialect const& instance();