Supply full object to stack compressor and Optimizer Suite.

This commit is contained in:
chriseth
2019-07-10 18:49:42 +02:00
parent 2a5280faa0
commit 869d69d293
17 changed files with 107 additions and 55 deletions
+7 -4
View File
@@ -26,6 +26,7 @@
#include <libyul/AsmAnalysisInfo.h>
#include <libyul/Utilities.h>
#include <libyul/Exceptions.h>
#include <libyul/Object.h>
#include <liblangutil/ErrorReporter.h>
@@ -69,17 +70,19 @@ bool AsmAnalyzer::analyze(Block const& _block)
return success && !m_errorReporter.hasErrors();
}
AsmAnalysisInfo AsmAnalyzer::analyzeStrictAssertCorrect(Dialect const& _dialect, Block const& _ast)
AsmAnalysisInfo AsmAnalyzer::analyzeStrictAssertCorrect(Dialect const& _dialect, Object const& _object)
{
ErrorList errorList;
langutil::ErrorReporter errors(errorList);
yul::AsmAnalysisInfo analysisInfo;
AsmAnalysisInfo analysisInfo;
bool success = yul::AsmAnalyzer(
analysisInfo,
errors,
Error::Type::SyntaxError,
_dialect
).analyze(_ast);
_dialect,
{},
_object.dataNames()
).analyze(*_object.code);
solAssert(success && errorList.empty(), "Invalid assembly/yul code.");
return analysisInfo;
}
+3 -1
View File
@@ -77,7 +77,9 @@ public:
bool analyze(Block const& _block);
static AsmAnalysisInfo analyzeStrictAssertCorrect(Dialect const& _dialect, Block const& _ast);
/// Performs analysis on the outermost code of the given object and returns the analysis info.
/// Asserts on failure.
static AsmAnalysisInfo analyzeStrictAssertCorrect(Dialect const& _dialect, Object const& _object);
bool operator()(Instruction const&);
bool operator()(Literal const& _literal);
+2 -8
View File
@@ -114,18 +114,13 @@ bool AssemblyStack::analyzeParsed(Object& _object)
solAssert(_object.code, "");
_object.analysisInfo = make_shared<AsmAnalysisInfo>();
set<YulString> objectNames;
objectNames.insert(_object.name);
for (auto const& subObject: _object.subIndexByName)
objectNames.insert(subObject.first);
AsmAnalyzer analyzer(
*_object.analysisInfo,
m_errorReporter,
boost::none,
languageToDialect(m_language, m_evmVersion),
{},
objectNames
_object.dataNames()
);
bool success = analyzer.analyze(*_object.code);
for (auto& subNode: _object.subObjects)
@@ -166,8 +161,7 @@ void AssemblyStack::optimize(Object& _object, bool _isCreation)
OptimiserSuite::run(
dialect,
meter.get(),
*_object.code,
*_object.analysisInfo,
_object,
m_optimiserSettings.optimizeStackAllocation
);
}
+15 -5
View File
@@ -34,7 +34,7 @@ using namespace dev;
map<YulString, int> CompilabilityChecker::run(
Dialect const& _dialect,
Block const& _ast,
Object const& _object,
bool _optimizeStackAllocation
)
{
@@ -46,16 +46,26 @@ map<YulString, int> CompilabilityChecker::run(
if (EVMDialect const* evmDialect = dynamic_cast<EVMDialect const*>(&_dialect))
{
NoOutputEVMDialect noOutputDialect(*evmDialect);
BuiltinContext builtinContext;
yul::AsmAnalysisInfo analysisInfo =
yul::AsmAnalyzer::analyzeStrictAssertCorrect(noOutputDialect, _ast);
yul::AsmAnalyzer::analyzeStrictAssertCorrect(noOutputDialect, _object);
BuiltinContext builtinContext;
builtinContext.currentObject = &_object;
for (auto name: _object.dataNames())
builtinContext.subIDs[name] = 1;
NoOutputAssembly assembly;
CodeTransform transform(assembly, analysisInfo, _ast, noOutputDialect, builtinContext, _optimizeStackAllocation);
CodeTransform transform(
assembly,
analysisInfo,
*_object.code,
noOutputDialect,
builtinContext,
_optimizeStackAllocation
);
try
{
transform(_ast);
transform(*_object.code);
}
catch (StackTooDeepError const&)
{
+5 -1
View File
@@ -22,6 +22,7 @@
#include <libyul/Dialect.h>
#include <libyul/AsmDataForward.h>
#include <libyul/Object.h>
#include <map>
#include <memory>
@@ -33,15 +34,18 @@ namespace yul
* Component that checks whether all variables are reachable on the stack and
* returns a mapping from function name to the largest stack difference found
* in that function (no entry present if that function is compilable).
*
* This only works properly if the outermost block is compilable and
* functions are not nested. Otherwise, it might miss reporting some functions.
*
* Only checks the code of the object itself, does not descend into sub-objects.
*/
class CompilabilityChecker
{
public:
static std::map<YulString, int> run(
Dialect const& _dialect,
Block const& _ast,
Object const& _object,
bool _optimizeStackAllocation
);
};
+11
View File
@@ -59,3 +59,14 @@ string Object::toString(bool _yul) const
return "object \"" + name.str() + "\" {\n" + indent(inner) + "\n}";
}
set<YulString> Object::dataNames() const
{
set<YulString> names;
names.insert(name);
for (auto const& subObject: subIndexByName)
names.insert(subObject.first);
// The empty name is not valid
names.erase(YulString{});
return names;
}
+5
View File
@@ -26,6 +26,7 @@
#include <libdevcore/Common.h>
#include <memory>
#include <set>
namespace yul
{
@@ -63,6 +64,10 @@ public:
/// @returns a (parseable) string representation. Includes types if @a _yul is set.
std::string toString(bool _yul) const override;
/// @returns the set of names of data objects accessible from within the code of
/// this object.
std::set<YulString> dataNames() const;
std::shared_ptr<Block> code;
std::vector<std::shared_ptr<ObjectNode>> subObjects;
std::map<YulString, size_t> subIndexByName;
@@ -553,7 +553,7 @@ Object EVMToEWasmTranslator::run(Object const& _object)
ErrorList errors;
ErrorReporter errorReporter(errors);
AsmAnalyzer analyzer(*ret.analysisInfo, errorReporter, boost::none, WasmDialect::instance());
AsmAnalyzer analyzer(*ret.analysisInfo, errorReporter, boost::none, WasmDialect::instance(), {}, _object.dataNames());
if (!analyzer.analyze(*ret.code))
{
// TODO the errors here are "wrong" because they have invalid source references!
+8 -7
View File
@@ -155,19 +155,20 @@ void eliminateVariables(
bool StackCompressor::run(
Dialect const& _dialect,
Block& _ast,
Object& _object,
bool _optimizeStackAllocation,
size_t _maxIterations
)
{
yulAssert(
_ast.statements.size() > 0 && _ast.statements.at(0).type() == typeid(Block),
_object.code &&
_object.code->statements.size() > 0 && _object.code->statements.at(0).type() == typeid(Block),
"Need to run the function grouper before the stack compressor."
);
bool allowMSizeOptimzation = !SideEffectsCollector(_dialect, _ast).containsMSize();
bool allowMSizeOptimzation = !SideEffectsCollector(_dialect, *_object.code).containsMSize();
for (size_t iterations = 0; iterations < _maxIterations; iterations++)
{
map<YulString, int> stackSurplus = CompilabilityChecker::run(_dialect, _ast, _optimizeStackAllocation);
map<YulString, int> stackSurplus = CompilabilityChecker::run(_dialect, _object, _optimizeStackAllocation);
if (stackSurplus.empty())
return true;
@@ -176,15 +177,15 @@ bool StackCompressor::run(
yulAssert(stackSurplus.at({}) > 0, "Invalid surplus value.");
eliminateVariables(
_dialect,
boost::get<Block>(_ast.statements.at(0)),
boost::get<Block>(_object.code->statements.at(0)),
stackSurplus.at({}),
allowMSizeOptimzation
);
}
for (size_t i = 1; i < _ast.statements.size(); ++i)
for (size_t i = 1; i < _object.code->statements.size(); ++i)
{
FunctionDefinition& fun = boost::get<FunctionDefinition>(_ast.statements[i]);
FunctionDefinition& fun = boost::get<FunctionDefinition>(_object.code->statements[i]);
if (!stackSurplus.count(fun.name))
continue;
+4 -2
View File
@@ -27,13 +27,15 @@ namespace yul
{
struct Dialect;
struct Block;
struct Object;
struct FunctionDefinition;
/**
* Optimisation stage that aggressively rematerializes certain variables in a function to free
* space on the stack until it is compilable.
*
* Only runs on the code of the object itself, does not descend into sub-objects.
*
* Prerequisite: Disambiguator, Function Grouper
*/
class StackCompressor
@@ -43,7 +45,7 @@ public:
/// @returns true if it was successful.
static bool run(
Dialect const& _dialect,
Block& _ast,
Object& _object,
bool _optimizeStackAllocation,
size_t _maxIterations
);
+15 -6
View File
@@ -49,6 +49,7 @@
#include <libyul/AsmAnalysisInfo.h>
#include <libyul/AsmData.h>
#include <libyul/AsmPrinter.h>
#include <libyul/Object.h>
#include <libyul/backends/wasm/WasmDialect.h>
#include <libyul/backends/evm/NoOutputAssembly.h>
@@ -62,8 +63,7 @@ using namespace yul;
void OptimiserSuite::run(
Dialect const& _dialect,
GasMeter const* _meter,
Block& _ast,
AsmAnalysisInfo const& _analysisInfo,
Object& _object,
bool _optimizeStackAllocation,
set<YulString> const& _externallyUsedIdentifiers
)
@@ -71,7 +71,12 @@ void OptimiserSuite::run(
set<YulString> reservedIdentifiers = _externallyUsedIdentifiers;
reservedIdentifiers += _dialect.fixedFunctionNames();
Block ast = boost::get<Block>(Disambiguator(_dialect, _analysisInfo, reservedIdentifiers)(_ast));
*_object.code = boost::get<Block>(Disambiguator(
_dialect,
*_object.analysisInfo,
reservedIdentifiers
)(*_object.code));
Block& ast = *_object.code;
VarDeclInitializer{}(ast);
FunctionHoister{}(ast);
@@ -204,7 +209,12 @@ void OptimiserSuite::run(
FunctionGrouper{}(ast);
// We ignore the return value because we will get a much better error
// message once we perform code generation.
StackCompressor::run(_dialect, ast, _optimizeStackAllocation, stackCompressorMaxIterations);
StackCompressor::run(
_dialect,
_object,
_optimizeStackAllocation,
stackCompressorMaxIterations
);
BlockFlattener{}(ast);
DeadCodeEliminator{_dialect}(ast);
ControlFlowSimplifier{_dialect}(ast);
@@ -224,7 +234,6 @@ void OptimiserSuite::run(
ast.statements.erase(ast.statements.begin());
}
VarNameCleaner{ast, _dialect, reservedIdentifiers}(ast);
yul::AsmAnalyzer::analyzeStrictAssertCorrect(_dialect, ast);
_ast = std::move(ast);
*_object.analysisInfo = AsmAnalyzer::analyzeStrictAssertCorrect(_dialect, _object);
}
+4 -3
View File
@@ -32,9 +32,11 @@ namespace yul
struct AsmAnalysisInfo;
struct Dialect;
class GasMeter;
struct Object;
/**
* Optimiser suite that combines all steps and also provides the settings for the heuristics
* Optimiser suite that combines all steps and also provides the settings for the heuristics.
* Only optimizes the code of the provided object, does not descend into the sub-objects.
*/
class OptimiserSuite
{
@@ -42,8 +44,7 @@ public:
static void run(
Dialect const& _dialect,
GasMeter const* _meter,
Block& _ast,
AsmAnalysisInfo const& _analysisInfo,
Object& _object,
bool _optimizeStackAllocation,
std::set<YulString> const& _externallyUsedIdentifiers = {}
);