mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] Require specifying weights for Program::codeSize()
This commit is contained in:
@@ -23,6 +23,7 @@
|
||||
|
||||
using namespace std;
|
||||
using namespace solidity::util;
|
||||
using namespace solidity::yul;
|
||||
using namespace solidity::phaser;
|
||||
|
||||
Program const& ProgramBasedMetric::program() const
|
||||
@@ -55,18 +56,18 @@ Program ProgramBasedMetric::optimisedProgramNoCache(Chromosome const& _chromosom
|
||||
|
||||
size_t ProgramSize::evaluate(Chromosome const& _chromosome)
|
||||
{
|
||||
return optimisedProgram(_chromosome).codeSize();
|
||||
return optimisedProgram(_chromosome).codeSize(codeWeights());
|
||||
}
|
||||
|
||||
size_t RelativeProgramSize::evaluate(Chromosome const& _chromosome)
|
||||
{
|
||||
size_t const scalingFactor = pow(10, m_fixedPointPrecision);
|
||||
|
||||
size_t unoptimisedSize = optimisedProgram(Chromosome("")).codeSize();
|
||||
size_t unoptimisedSize = optimisedProgram(Chromosome("")).codeSize(codeWeights());
|
||||
if (unoptimisedSize == 0)
|
||||
return scalingFactor;
|
||||
|
||||
size_t optimisedSize = optimisedProgram(_chromosome).codeSize();
|
||||
size_t optimisedSize = optimisedProgram(_chromosome).codeSize(codeWeights());
|
||||
|
||||
return static_cast<size_t>(round(
|
||||
static_cast<double>(optimisedSize) / unoptimisedSize * scalingFactor
|
||||
|
||||
@@ -24,6 +24,8 @@
|
||||
#include <tools/yulPhaser/Program.h>
|
||||
#include <tools/yulPhaser/ProgramCache.h>
|
||||
|
||||
#include <libyul/optimiser/Metrics.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <optional>
|
||||
|
||||
@@ -64,10 +66,12 @@ public:
|
||||
explicit ProgramBasedMetric(
|
||||
std::optional<Program> _program,
|
||||
std::shared_ptr<ProgramCache> _programCache,
|
||||
yul::CodeWeights const& _codeWeights,
|
||||
size_t _repetitionCount = 1
|
||||
):
|
||||
m_program(std::move(_program)),
|
||||
m_programCache(std::move(_programCache)),
|
||||
m_codeWeights(_codeWeights),
|
||||
m_repetitionCount(_repetitionCount)
|
||||
{
|
||||
assert(m_program.has_value() == (m_programCache == nullptr));
|
||||
@@ -75,6 +79,7 @@ public:
|
||||
|
||||
Program const& program() const;
|
||||
ProgramCache const* programCache() const { return m_programCache.get(); }
|
||||
yul::CodeWeights const& codeWeights() const { return m_codeWeights; }
|
||||
size_t repetitionCount() const { return m_repetitionCount; }
|
||||
|
||||
Program optimisedProgram(Chromosome const& _chromosome);
|
||||
@@ -83,6 +88,7 @@ public:
|
||||
private:
|
||||
std::optional<Program> m_program;
|
||||
std::shared_ptr<ProgramCache> m_programCache;
|
||||
yul::CodeWeights m_codeWeights;
|
||||
size_t m_repetitionCount;
|
||||
};
|
||||
|
||||
@@ -111,9 +117,10 @@ public:
|
||||
std::optional<Program> _program,
|
||||
std::shared_ptr<ProgramCache> _programCache,
|
||||
size_t _fixedPointPrecision,
|
||||
yul::CodeWeights const& _weights,
|
||||
size_t _repetitionCount = 1
|
||||
):
|
||||
ProgramBasedMetric(std::move(_program), std::move(_programCache), _repetitionCount),
|
||||
ProgramBasedMetric(std::move(_program), std::move(_programCache), _weights, _repetitionCount),
|
||||
m_fixedPointPrecision(_fixedPointPrecision) {}
|
||||
|
||||
size_t fixedPointPrecision() const { return m_fixedPointPrecision; }
|
||||
|
||||
@@ -39,6 +39,7 @@ using namespace std;
|
||||
using namespace solidity;
|
||||
using namespace solidity::langutil;
|
||||
using namespace solidity::util;
|
||||
using namespace solidity::yul;
|
||||
using namespace solidity::phaser;
|
||||
|
||||
namespace po = boost::program_options;
|
||||
@@ -216,6 +217,7 @@ unique_ptr<FitnessMetric> FitnessMetricFactory::build(
|
||||
metrics.push_back(make_unique<ProgramSize>(
|
||||
_programCaches[i] != nullptr ? optional<Program>{} : move(_programs[i]),
|
||||
move(_programCaches[i]),
|
||||
CodeWeights{},
|
||||
_options.chromosomeRepetitions
|
||||
));
|
||||
|
||||
@@ -228,6 +230,7 @@ unique_ptr<FitnessMetric> FitnessMetricFactory::build(
|
||||
_programCaches[i] != nullptr ? optional<Program>{} : move(_programs[i]),
|
||||
move(_programCaches[i]),
|
||||
_options.relativeMetricScale,
|
||||
CodeWeights{},
|
||||
_options.chromosomeRepetitions
|
||||
));
|
||||
break;
|
||||
|
||||
@@ -207,7 +207,7 @@ unique_ptr<Block> Program::applyOptimisationSteps(
|
||||
return _ast;
|
||||
}
|
||||
|
||||
size_t Program::computeCodeSize(Block const& _ast)
|
||||
size_t Program::computeCodeSize(Block const& _ast, CodeWeights const& _weights)
|
||||
{
|
||||
return CodeSize::codeSizeIncludingFunctions(_ast);
|
||||
return CodeSize::codeSizeIncludingFunctions(_ast, _weights);
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@ namespace solidity::yul
|
||||
|
||||
struct AsmAnalysisInfo;
|
||||
struct Dialect;
|
||||
struct CodeWeights;
|
||||
|
||||
}
|
||||
|
||||
@@ -78,7 +79,7 @@ public:
|
||||
static std::variant<Program, langutil::ErrorList> load(langutil::CharStream& _sourceCode);
|
||||
void optimise(std::vector<std::string> const& _optimisationSteps);
|
||||
|
||||
size_t codeSize() const { return computeCodeSize(*m_ast); }
|
||||
size_t codeSize(yul::CodeWeights const& _weights) const { return computeCodeSize(*m_ast, _weights); }
|
||||
yul::Block const& ast() const { return *m_ast; }
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& _stream, Program const& _program);
|
||||
@@ -113,7 +114,7 @@ private:
|
||||
std::unique_ptr<yul::Block> _ast,
|
||||
std::vector<std::string> const& _optimisationSteps
|
||||
);
|
||||
static size_t computeCodeSize(yul::Block const& _ast);
|
||||
static size_t computeCodeSize(yul::Block const& _ast, yul::CodeWeights const& _weights);
|
||||
|
||||
std::unique_ptr<yul::Block> m_ast;
|
||||
yul::Dialect const& m_dialect;
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
|
||||
#include <tools/yulPhaser/ProgramCache.h>
|
||||
|
||||
#include <libyul/optimiser/Metrics.h>
|
||||
|
||||
#include <libyul/optimiser/Suite.h>
|
||||
|
||||
using namespace std;
|
||||
@@ -133,7 +135,7 @@ size_t ProgramCache::calculateTotalCachedCodeSize() const
|
||||
{
|
||||
size_t size = 0;
|
||||
for (auto const& pair: m_entries)
|
||||
size += pair.second.program.codeSize();
|
||||
size += pair.second.program.codeSize(CodeWeights{});
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user