[yul-phaser] Chromosome: Add a constructor that reads steps from an abbreviation string

This commit is contained in:
Kamil Śliwak 2020-02-08 01:51:04 +01:00
parent 38f79a1761
commit d22c59aa0e
4 changed files with 41 additions and 6 deletions

View File

@ -21,8 +21,18 @@
#include <tools/yulPhaser/SimulationRNG.h>
#include <libyul/optimiser/BlockFlattener.h>
#include <libyul/optimiser/StructuralSimplifier.h>
#include <libyul/optimiser/ConditionalSimplifier.h>
#include <libyul/optimiser/ExpressionInliner.h>
#include <libyul/optimiser/ExpressionSimplifier.h>
#include <libyul/optimiser/ForLoopConditionOutOfBody.h>
#include <libyul/optimiser/ForLoopConditionOutOfBody.h>
#include <libyul/optimiser/ForLoopInitRewriter.h>
#include <libyul/optimiser/FunctionHoister.h>
#include <libyul/optimiser/LoopInvariantCodeMotion.h>
#include <libyul/optimiser/RedundantAssignEliminator.h>
#include <libyul/optimiser/Rematerialiser.h>
#include <libyul/optimiser/Suite.h>
#include <libyul/optimiser/StructuralSimplifier.h>
#include <libyul/optimiser/UnusedPruner.h>
#include <libsolutil/CommonIO.h>
@ -39,6 +49,24 @@ namespace solidity::phaser::test
BOOST_AUTO_TEST_SUITE(Phaser)
BOOST_AUTO_TEST_SUITE(ChromosomeTest)
BOOST_AUTO_TEST_CASE(constructor_should_convert_from_string_to_optimisation_steps)
{
vector<string> expectedSteps{
ConditionalSimplifier::name,
FunctionHoister::name,
RedundantAssignEliminator::name,
ForLoopConditionOutOfBody::name,
Rematerialiser::name,
ForLoopConditionOutOfBody::name,
ExpressionSimplifier::name,
ForLoopInitRewriter::name,
LoopInvariantCodeMotion::name,
ExpressionInliner::name
};
BOOST_TEST(Chromosome("ChrOmOsoMe").optimisationSteps() == expectedSteps);
}
BOOST_AUTO_TEST_CASE(makeRandom_should_create_chromosome_with_random_optimisation_steps)
{
constexpr uint32_t numSteps = 1000;

View File

@ -142,11 +142,11 @@ BOOST_AUTO_TEST_CASE(run_should_not_make_fitness_of_top_chromosomes_worse)
stringstream output;
CharStream sourceStream(sampleSourceCode, current_test_case().p_name);
vector<Chromosome> chromosomes = {
Chromosome({StructuralSimplifier::name}),
Chromosome({BlockFlattener::name}),
Chromosome({SSAReverser::name}),
Chromosome({UnusedPruner::name}),
Chromosome({StructuralSimplifier::name, BlockFlattener::name}),
Chromosome(vector<string>{StructuralSimplifier::name}),
Chromosome(vector<string>{BlockFlattener::name}),
Chromosome(vector<string>{SSAReverser::name}),
Chromosome(vector<string>{UnusedPruner::name}),
Chromosome(vector<string>{StructuralSimplifier::name, BlockFlattener::name}),
};
auto program = Program::load(sourceStream);
Population population(program, chromosomes);

View File

@ -36,6 +36,12 @@ ostream& operator<<(ostream& _stream, Chromosome const& _chromosome);
}
Chromosome::Chromosome(string const& _optimisationSteps)
{
for (char abbreviation: _optimisationSteps)
m_optimisationSteps.push_back(OptimiserSuite::stepAbbreviationToNameMap().at(abbreviation));
}
Chromosome Chromosome::makeRandom(size_t _length)
{
vector<string> steps;

View File

@ -42,6 +42,7 @@ public:
Chromosome() = default;
explicit Chromosome(std::vector<std::string> _optimisationSteps):
m_optimisationSteps(std::move(_optimisationSteps)) {}
explicit Chromosome(std::string const& _optimisationSteps);
static Chromosome makeRandom(size_t _length);
size_t length() const { return m_optimisationSteps.size(); }