mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] Add ProgramSize metric
This commit is contained in:
parent
096129fbc4
commit
2238919c76
@ -143,6 +143,7 @@ set(yul_phaser_sources
|
|||||||
yulPhaser/Common.cpp
|
yulPhaser/Common.cpp
|
||||||
yulPhaser/CommonTest.cpp
|
yulPhaser/CommonTest.cpp
|
||||||
yulPhaser/Chromosome.cpp
|
yulPhaser/Chromosome.cpp
|
||||||
|
yulPhaser/FitnessMetrics.cpp
|
||||||
yulPhaser/Population.cpp
|
yulPhaser/Population.cpp
|
||||||
yulPhaser/Program.cpp
|
yulPhaser/Program.cpp
|
||||||
yulPhaser/SimulationRNG.cpp
|
yulPhaser/SimulationRNG.cpp
|
||||||
|
80
test/yulPhaser/FitnessMetrics.cpp
Normal file
80
test/yulPhaser/FitnessMetrics.cpp
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
/*
|
||||||
|
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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <tools/yulPhaser/FitnessMetrics.h>
|
||||||
|
|
||||||
|
#include <libyul/optimiser/EquivalentFunctionCombiner.h>
|
||||||
|
#include <libyul/optimiser/UnusedPruner.h>
|
||||||
|
|
||||||
|
#include <liblangutil/CharStream.h>
|
||||||
|
|
||||||
|
#include <boost/test/unit_test.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace solidity::langutil;
|
||||||
|
using namespace solidity::yul;
|
||||||
|
|
||||||
|
namespace solidity::phaser::test
|
||||||
|
{
|
||||||
|
|
||||||
|
class FitnessMetricFixture
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
FitnessMetricFixture():
|
||||||
|
m_sourceStream(SampleSourceCode, ""),
|
||||||
|
m_program(Program::load(m_sourceStream)) {}
|
||||||
|
|
||||||
|
static constexpr char SampleSourceCode[] =
|
||||||
|
"{\n"
|
||||||
|
" function foo() -> result\n"
|
||||||
|
" {\n"
|
||||||
|
" let x := 1\n"
|
||||||
|
" result := 15\n"
|
||||||
|
" }\n"
|
||||||
|
" function bar() -> result\n"
|
||||||
|
" {\n"
|
||||||
|
" result := 15\n"
|
||||||
|
" }\n"
|
||||||
|
" mstore(foo(), bar())\n"
|
||||||
|
"}\n";
|
||||||
|
|
||||||
|
CharStream m_sourceStream;
|
||||||
|
Program m_program;
|
||||||
|
};
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_SUITE(Phaser)
|
||||||
|
BOOST_AUTO_TEST_SUITE(FitnessMetricsTest)
|
||||||
|
BOOST_AUTO_TEST_SUITE(ProgramSizeTest)
|
||||||
|
|
||||||
|
BOOST_FIXTURE_TEST_CASE(evaluate_should_compute_size_of_the_optimised_program, FitnessMetricFixture)
|
||||||
|
{
|
||||||
|
Chromosome chromosome(vector<string>{UnusedPruner::name, EquivalentFunctionCombiner::name});
|
||||||
|
|
||||||
|
Program optimisedProgram = m_program;
|
||||||
|
optimisedProgram.optimise(chromosome.optimisationSteps());
|
||||||
|
assert(m_program.codeSize() != optimisedProgram.codeSize());
|
||||||
|
|
||||||
|
BOOST_TEST(ProgramSize(m_program).evaluate(chromosome) != m_program.codeSize());
|
||||||
|
BOOST_TEST(ProgramSize(m_program).evaluate(chromosome) == optimisedProgram.codeSize());
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -16,3 +16,13 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <tools/yulPhaser/FitnessMetrics.h>
|
#include <tools/yulPhaser/FitnessMetrics.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace solidity::phaser;
|
||||||
|
|
||||||
|
size_t ProgramSize::evaluate(Chromosome const& _chromosome) const
|
||||||
|
{
|
||||||
|
Program programCopy = m_program;
|
||||||
|
programCopy.optimise(_chromosome.optimisationSteps());
|
||||||
|
return programCopy.codeSize();
|
||||||
|
}
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <tools/yulPhaser/Chromosome.h>
|
#include <tools/yulPhaser/Chromosome.h>
|
||||||
|
#include <tools/yulPhaser/Program.h>
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
|
||||||
@ -45,4 +46,19 @@ public:
|
|||||||
virtual size_t evaluate(Chromosome const& _chromosome) const = 0;
|
virtual size_t evaluate(Chromosome const& _chromosome) const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fitness metric based on the size of a specific program after applying the optimisations from the
|
||||||
|
* chromosome to it.
|
||||||
|
*/
|
||||||
|
class ProgramSize: public FitnessMetric
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ProgramSize(Program _program): m_program(std::move(_program)) {}
|
||||||
|
|
||||||
|
size_t evaluate(Chromosome const& _chromosome) const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Program m_program;
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user