Skip to content

Commit

Permalink
Run yul optimizer until it has no effect anymore.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseth committed Mar 5, 2019
1 parent 40171c2 commit f35694f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
9 changes: 8 additions & 1 deletion libyul/optimiser/Metrics.cpp
Expand Up @@ -52,9 +52,16 @@ size_t CodeSize::codeSize(Block const& _block)
return cs.m_size;
}

size_t CodeSize::codeSizeIncludingFunctions(Block const& _block)
{
CodeSize cs(false);
cs(_block);
return cs.m_size;
}

void CodeSize::visit(Statement const& _statement)
{
if (_statement.type() == typeid(FunctionDefinition))
if (_statement.type() == typeid(FunctionDefinition) && m_ignoreFunctions)
return;
else if (!(
_statement.type() == typeid(Block) ||
Expand Down
6 changes: 4 additions & 2 deletions libyul/optimiser/Metrics.h
Expand Up @@ -28,7 +28,7 @@ namespace yul
/**
* Metric for the size of code.
* More specifically, the number of AST nodes.
* Ignores function definitions while traversing the AST.
* Ignores function definitions while traversing the AST by default.
* If you want to know the size of a function, you have to invoke this on its body.
*
* As an exception, the following AST elements have a cost of zero:
Expand All @@ -44,14 +44,16 @@ class CodeSize: public ASTWalker
static size_t codeSize(Statement const& _statement);
static size_t codeSize(Expression const& _expression);
static size_t codeSize(Block const& _block);
static size_t codeSizeIncludingFunctions(Block const& _block);

private:
CodeSize() {}
CodeSize(bool _ignoreFunctions = true): m_ignoreFunctions(_ignoreFunctions) {}

void visit(Statement const& _statement) override;
void visit(Expression const& _expression) override;

private:
bool m_ignoreFunctions;
size_t m_size = 0;
};

Expand Down
11 changes: 10 additions & 1 deletion libyul/optimiser/Suite.cpp
Expand Up @@ -41,6 +41,7 @@
#include <libyul/optimiser/StructuralSimplifier.h>
#include <libyul/optimiser/RedundantAssignEliminator.h>
#include <libyul/optimiser/VarNameCleaner.h>
#include <libyul/optimiser/Metrics.h>
#include <libyul/AsmAnalysis.h>
#include <libyul/AsmAnalysisInfo.h>
#include <libyul/AsmData.h>
Expand Down Expand Up @@ -80,8 +81,16 @@ void OptimiserSuite::run(

NameDispenser dispenser{*_dialect, ast};

for (size_t i = 0; i < 4; i++)
size_t codeSize = 0;
for (size_t rounds = 0; rounds < 12; ++rounds)
{
{
size_t newSize = CodeSize::codeSizeIncludingFunctions(ast);
if (newSize == codeSize)
break;
codeSize = newSize;
}

{
// Turn into SSA and simplify
ExpressionSplitter{*_dialect, dispenser}(ast);
Expand Down

0 comments on commit f35694f

Please sign in to comment.