Merge pull request #8325 from imapp-pl/yul-phaser-population-tweaks

[yul-phaser] Miscellaneous Population tweaks and improvements
This commit is contained in:
chriseth
2020-02-19 12:35:10 +01:00
committed by GitHub
11 changed files with 366 additions and 95 deletions
+50 -16
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,25 +49,49 @@ namespace solidity::phaser::test
BOOST_AUTO_TEST_SUITE(Phaser)
BOOST_AUTO_TEST_SUITE(ChromosomeTest)
BOOST_AUTO_TEST_CASE(makeRandom_should_create_chromosome_with_random_optimisation_steps)
BOOST_AUTO_TEST_CASE(constructor_should_convert_from_string_to_optimisation_steps)
{
constexpr uint32_t numSteps = 1000;
vector<string> expectedSteps{
ConditionalSimplifier::name,
FunctionHoister::name,
RedundantAssignEliminator::name,
ForLoopConditionOutOfBody::name,
Rematerialiser::name,
ForLoopConditionOutOfBody::name,
ExpressionSimplifier::name,
ForLoopInitRewriter::name,
LoopInvariantCodeMotion::name,
ExpressionInliner::name
};
auto chromosome1 = Chromosome::makeRandom(numSteps);
auto chromosome2 = Chromosome::makeRandom(numSteps);
BOOST_CHECK_EQUAL(chromosome1.length(), numSteps);
BOOST_CHECK_EQUAL(chromosome2.length(), numSteps);
BOOST_TEST(Chromosome("ChrOmOsoMe").optimisationSteps() == expectedSteps);
}
multiset<string> steps1;
multiset<string> steps2;
for (auto const& step: chromosome1.optimisationSteps())
steps1.insert(step);
for (auto const& step: chromosome2.optimisationSteps())
steps2.insert(step);
BOOST_AUTO_TEST_CASE(makeRandom_should_return_different_chromosome_each_time)
{
SimulationRNG::reset(1);
for (size_t i = 0; i < 10; ++i)
BOOST_TEST(Chromosome::makeRandom(100) != Chromosome::makeRandom(100));
}
// Check if steps are different and also if they're not just a permutation of the same set.
// Technically they could be the same and still random but the probability is infinitesimally low.
BOOST_TEST(steps1 != steps2);
BOOST_AUTO_TEST_CASE(makeRandom_should_use_every_possible_step_with_the_same_probability)
{
SimulationRNG::reset(1);
constexpr int samplesPerStep = 100;
constexpr double relativeTolerance = 0.01;
map<string, size_t> stepIndices = enumerateOptmisationSteps();
auto chromosome = Chromosome::makeRandom(stepIndices.size() * samplesPerStep);
vector<size_t> samples;
for (auto& step: chromosome.optimisationSteps())
samples.push_back(stepIndices.at(step));
const double expectedValue = (stepIndices.size() - 1) / 2.0;
const double variance = (stepIndices.size() * stepIndices.size() - 1) / 12.0;
BOOST_TEST(abs(mean(samples) - expectedValue) < expectedValue * relativeTolerance);
BOOST_TEST(abs(meanSquaredError(samples, expectedValue) - variance) < variance * relativeTolerance);
}
BOOST_AUTO_TEST_CASE(constructor_should_store_optimisation_steps)
+17
View File
@@ -19,10 +19,21 @@
#include <libyul/optimiser/Suite.h>
#include <regex>
using namespace std;
using namespace solidity;
using namespace solidity::yul;
vector<size_t> phaser::test::chromosomeLengths(Population const& _population)
{
vector<size_t> lengths;
for (auto const& individual: _population.individuals())
lengths.push_back(individual.chromosome.length());
return lengths;
}
map<string, size_t> phaser::test::enumerateOptmisationSteps()
{
map<string, size_t> stepIndices;
@@ -32,3 +43,9 @@ map<string, size_t> phaser::test::enumerateOptmisationSteps()
return stepIndices;
}
string phaser::test::stripWhitespace(string const& input)
{
regex whitespaceRegex("\\s+");
return regex_replace(input, whitespaceRegex, "");
}
+11
View File
@@ -28,6 +28,8 @@
#pragma once
#include <tools/yulPhaser/Population.h>
#include <cassert>
#include <map>
#include <string>
@@ -37,11 +39,20 @@ namespace solidity::phaser::test
{
// CHROMOSOME AND POPULATION HELPERS
/// Returns a vector containing lengths of all chromosomes in the population (in the same order).
std::vector<size_t> chromosomeLengths(Population const& _population);
/// Assigns indices from 0 to N to all optimisation steps available in the OptimiserSuite.
/// This is a convenience helper to make it easier to test their distribution with tools made for
/// integers.
std::map<std::string, size_t> enumerateOptmisationSteps();
// STRING UTILITIES
/// Returns the input string with all the whitespace characters (spaces, line endings, etc.) removed.
std::string stripWhitespace(std::string const& input);
// STATISTICAL UTILITIES
/// Calculates the mean value of a series of samples given in a vector.
+29
View File
@@ -19,11 +19,14 @@
#include <libyul/optimiser/Suite.h>
#include <liblangutil/CharStream.h>
#include <boost/test/unit_test.hpp>
#include <set>
using namespace std;
using namespace solidity::langutil;
using namespace solidity::yul;
using namespace boost::test_tools;
@@ -33,6 +36,18 @@ namespace solidity::phaser::test
BOOST_AUTO_TEST_SUITE(Phaser)
BOOST_AUTO_TEST_SUITE(CommonTest)
BOOST_AUTO_TEST_CASE(chromosomeLengths_should_return_lengths_of_all_chromosomes_in_a_population)
{
CharStream sourceStream("{}", "");
auto program = Program::load(sourceStream);
Population population1(program, {Chromosome(), Chromosome("a"), Chromosome("aa"), Chromosome("aaa")});
BOOST_TEST((chromosomeLengths(population1) == vector<size_t>{0, 1, 2, 3}));
Population population2(program);
BOOST_TEST((chromosomeLengths(population2) == vector<size_t>{}));
}
BOOST_AUTO_TEST_CASE(enumerateOptimisationSteps_should_assing_indices_to_all_available_optimisation_steps)
{
map<string, char> stepsAndAbbreviations = OptimiserSuite::stepNameToAbbreviationMap();
@@ -51,6 +66,20 @@ BOOST_AUTO_TEST_CASE(enumerateOptimisationSteps_should_assing_indices_to_all_ava
}
}
BOOST_AUTO_TEST_CASE(stripWhitespace_should_remove_all_whitespace_characters_from_a_string)
{
BOOST_TEST(stripWhitespace("") == "");
BOOST_TEST(stripWhitespace(" ") == "");
BOOST_TEST(stripWhitespace(" \n\t\v ") == "");
BOOST_TEST(stripWhitespace("abc") == "abc");
BOOST_TEST(stripWhitespace(" abc") == "abc");
BOOST_TEST(stripWhitespace("abc ") == "abc");
BOOST_TEST(stripWhitespace(" a b c ") == "abc");
BOOST_TEST(stripWhitespace(" a b\tc\n") == "abc");
BOOST_TEST(stripWhitespace(" a b \n\n c \n\t\v") == "abc");
}
BOOST_AUTO_TEST_CASE(mean_should_calculate_statistical_mean)
{
BOOST_TEST(mean<int>({0}) == 0.0);
+155 -58
View File
@@ -15,6 +15,8 @@
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
#include <test/yulPhaser/Common.h>
#include <tools/yulPhaser/Chromosome.h>
#include <tools/yulPhaser/Population.h>
#include <tools/yulPhaser/Program.h>
@@ -53,40 +55,80 @@ namespace
}
}
class PopulationFixture
{
protected:
PopulationFixture():
m_sourceStream(SampleSourceCode, ""),
m_program(Program::load(m_sourceStream)) {}
static constexpr char SampleSourceCode[] =
"{\n"
" let factor := 13\n"
" {\n"
" if factor\n"
" {\n"
" let variable := add(1, 2)\n"
" }\n"
" let result := factor\n"
" }\n"
" let something := 6\n"
" {\n"
" {\n"
" {\n"
" let value := 15\n"
" }\n"
" }\n"
" }\n"
" let something_else := mul(mul(something, 1), add(factor, 0))\n"
" if 1 { let x := 1 }\n"
" if 0 { let y := 2 }\n"
"}\n";
CharStream m_sourceStream;
Program m_program;
};
BOOST_AUTO_TEST_SUITE(Phaser)
BOOST_AUTO_TEST_SUITE(PopulationTest)
string const& sampleSourceCode =
"{\n"
" let factor := 13\n"
" {\n"
" if factor\n"
" {\n"
" let variable := add(1, 2)\n"
" }\n"
" let result := factor\n"
" }\n"
" let something := 6\n"
" {\n"
" {\n"
" {\n"
" let value := 15\n"
" }\n"
" }\n"
" }\n"
" let something_else := mul(mul(something, 1), add(factor, 0))\n"
" if 1 { let x := 1 }\n"
" if 0 { let y := 2 }\n"
"}\n";
BOOST_AUTO_TEST_CASE(isFitter_should_use_fitness_as_the_main_criterion)
{
BOOST_TEST(isFitter(Individual{Chromosome("a"), 5}, Individual{Chromosome("a"), 10}));
BOOST_TEST(!isFitter(Individual{Chromosome("a"), 10}, Individual{Chromosome("a"), 5}));
BOOST_AUTO_TEST_CASE(constructor_should_copy_chromosomes_and_not_compute_fitness)
BOOST_TEST(isFitter(Individual{Chromosome("aaa"), 5}, Individual{Chromosome("aaaaa"), 10}));
BOOST_TEST(!isFitter(Individual{Chromosome("aaaaa"), 10}, Individual{Chromosome("aaa"), 5}));
BOOST_TEST(isFitter(Individual{Chromosome("aaaaa"), 5}, Individual{Chromosome("aaa"), 10}));
BOOST_TEST(!isFitter(Individual{Chromosome("aaa"), 10}, Individual{Chromosome("aaaaa"), 5}));
}
BOOST_AUTO_TEST_CASE(isFitter_should_use_alphabetical_order_when_fitness_is_the_same)
{
BOOST_TEST(isFitter(Individual{Chromosome("a"), 3}, Individual{Chromosome("c"), 3}));
BOOST_TEST(!isFitter(Individual{Chromosome("c"), 3}, Individual{Chromosome("a"), 3}));
BOOST_TEST(isFitter(Individual{Chromosome("a"), 3}, Individual{Chromosome("aa"), 3}));
BOOST_TEST(!isFitter(Individual{Chromosome("aa"), 3}, Individual{Chromosome("a"), 3}));
BOOST_TEST(isFitter(Individual{Chromosome("T"), 3}, Individual{Chromosome("a"), 3}));
BOOST_TEST(!isFitter(Individual{Chromosome("a"), 3}, Individual{Chromosome("T"), 3}));
}
BOOST_AUTO_TEST_CASE(isFitter_should_return_false_for_identical_individuals)
{
BOOST_TEST(!isFitter(Individual{Chromosome("a"), 3}, Individual{Chromosome("a"), 3}));
BOOST_TEST(!isFitter(Individual{Chromosome("acT"), 0}, Individual{Chromosome("acT"), 0}));
}
BOOST_FIXTURE_TEST_CASE(constructor_should_copy_chromosomes_and_not_compute_fitness, PopulationFixture)
{
CharStream sourceStream(sampleSourceCode, current_test_case().p_name);
vector<Chromosome> chromosomes = {
Chromosome::makeRandom(5),
Chromosome::makeRandom(10),
};
Population population(Program::load(sourceStream), chromosomes);
Population population(m_program, chromosomes);
BOOST_TEST(population.individuals().size() == 2);
BOOST_TEST(population.individuals()[0].chromosome == chromosomes[0]);
@@ -96,40 +138,88 @@ BOOST_AUTO_TEST_CASE(constructor_should_copy_chromosomes_and_not_compute_fitness
BOOST_TEST(all_of(population.individuals().begin(), population.individuals().end(), fitnessNotSet));
}
BOOST_AUTO_TEST_CASE(makeRandom_should_return_population_with_random_chromosomes)
BOOST_FIXTURE_TEST_CASE(makeRandom_should_get_chromosome_lengths_from_specified_generator, PopulationFixture)
{
CharStream sourceStream(sampleSourceCode, current_test_case().p_name);
auto program = Program::load(sourceStream);
auto population1 = Population::makeRandom(program, 100);
auto population2 = Population::makeRandom(program, 100);
size_t chromosomeCount = 30;
size_t maxLength = 5;
assert(chromosomeCount % maxLength == 0);
BOOST_TEST(population1.individuals().size() == 100);
BOOST_TEST(population2.individuals().size() == 100);
auto nextLength = [counter = 0, maxLength]() mutable { return counter++ % maxLength; };
auto population = Population::makeRandom(m_program, chromosomeCount, nextLength);
int numMatchingPositions = 0;
for (size_t i = 0; i < 100; ++i)
if (population1.individuals()[i].chromosome == population2.individuals()[i].chromosome)
++numMatchingPositions;
// Assume that the results are random if there are no more than 10 identical chromosomes on the
// same positions. One duplicate is very unlikely but still possible after billions of runs
// (especially for short chromosomes). For ten the probability is so small that we can ignore it.
BOOST_TEST(numMatchingPositions < 10);
// We can't rely on the order since the population sorts its chromosomes immediately but
// we can check the number of occurrences of each length.
for (size_t length = 0; length < maxLength; ++length)
BOOST_TEST(
count_if(
population.individuals().begin(),
population.individuals().end(),
[&length](auto const& individual) { return individual.chromosome.length() == length; }
) == chromosomeCount / maxLength
);
}
BOOST_AUTO_TEST_CASE(makeRandom_should_not_compute_fitness)
BOOST_FIXTURE_TEST_CASE(makeRandom_should_get_chromosome_lengths_from_specified_range, PopulationFixture)
{
CharStream sourceStream(sampleSourceCode, current_test_case().p_name);
auto population = Population::makeRandom(Program::load(sourceStream), 5);
auto population = Population::makeRandom(m_program, 100, 5, 10);
BOOST_TEST(all_of(
population.individuals().begin(),
population.individuals().end(),
[](auto const& individual){ return 5 <= individual.chromosome.length() && individual.chromosome.length() <= 10; }
));
}
BOOST_FIXTURE_TEST_CASE(makeRandom_should_use_random_chromosome_length, PopulationFixture)
{
SimulationRNG::reset(1);
constexpr int populationSize = 200;
constexpr int minLength = 5;
constexpr int maxLength = 10;
constexpr double relativeTolerance = 0.05;
auto population = Population::makeRandom(m_program, populationSize, minLength, maxLength);
vector<size_t> samples = chromosomeLengths(population);
const double expectedValue = (maxLength + minLength) / 2.0;
const double variance = ((maxLength - minLength + 1) * (maxLength - minLength + 1) - 1) / 12.0;
BOOST_TEST(abs(mean(samples) - expectedValue) < expectedValue * relativeTolerance);
BOOST_TEST(abs(meanSquaredError(samples, expectedValue) - variance) < variance * relativeTolerance);
}
BOOST_FIXTURE_TEST_CASE(makeRandom_should_return_population_with_random_chromosomes, PopulationFixture)
{
SimulationRNG::reset(1);
constexpr int populationSize = 100;
constexpr int chromosomeLength = 30;
constexpr double relativeTolerance = 0.01;
map<string, size_t> stepIndices = enumerateOptmisationSteps();
auto population = Population::makeRandom(m_program, populationSize, chromosomeLength, chromosomeLength);
vector<size_t> samples;
for (auto& individual: population.individuals())
for (auto& step: individual.chromosome.optimisationSteps())
samples.push_back(stepIndices.at(step));
const double expectedValue = (stepIndices.size() - 1) / 2.0;
const double variance = (stepIndices.size() * stepIndices.size() - 1) / 12.0;
BOOST_TEST(abs(mean(samples) - expectedValue) < expectedValue * relativeTolerance);
BOOST_TEST(abs(meanSquaredError(samples, expectedValue) - variance) < variance * relativeTolerance);
}
BOOST_FIXTURE_TEST_CASE(makeRandom_should_not_compute_fitness, PopulationFixture)
{
auto population = Population::makeRandom(m_program, 3, 5, 10);
BOOST_TEST(all_of(population.individuals().begin(), population.individuals().end(), fitnessNotSet));
}
BOOST_AUTO_TEST_CASE(run_should_evaluate_fitness)
BOOST_FIXTURE_TEST_CASE(run_should_evaluate_fitness, PopulationFixture)
{
stringstream output;
CharStream sourceStream(sampleSourceCode, current_test_case().p_name);
auto population = Population::makeRandom(Program::load(sourceStream), 5);
auto population = Population::makeRandom(m_program, 5, 5, 10);
assert(all_of(population.individuals().begin(), population.individuals().end(), fitnessNotSet));
population.run(1, output);
@@ -137,23 +227,21 @@ BOOST_AUTO_TEST_CASE(run_should_evaluate_fitness)
BOOST_TEST(all_of(population.individuals().begin(), population.individuals().end(), fitnessSet));
}
BOOST_AUTO_TEST_CASE(run_should_not_make_fitness_of_top_chromosomes_worse)
BOOST_FIXTURE_TEST_CASE(run_should_not_make_fitness_of_top_chromosomes_worse, PopulationFixture)
{
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);
Population population(m_program, chromosomes);
size_t initialTopFitness[2] = {
Population::measureFitness(chromosomes[0], program),
Population::measureFitness(chromosomes[1], program),
Population::measureFitness(chromosomes[0], m_program),
Population::measureFitness(chromosomes[1], m_program),
};
for (int i = 0; i < 6; ++i)
@@ -173,6 +261,15 @@ BOOST_AUTO_TEST_CASE(run_should_not_make_fitness_of_top_chromosomes_worse)
}
}
BOOST_FIXTURE_TEST_CASE(plus_operator_should_add_two_populations, PopulationFixture)
{
BOOST_CHECK_EQUAL(
Population(m_program, {Chromosome("ac"), Chromosome("cx")}) +
Population(m_program, {Chromosome("g"), Chromosome("h"), Chromosome("iI")}),
Population(m_program, {Chromosome("ac"), Chromosome("cx"), Chromosome("g"), Chromosome("h"), Chromosome("iI")})
);
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()
+2 -7
View File
@@ -15,6 +15,8 @@
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
#include <test/yulPhaser/Common.h>
#include <tools/yulPhaser/Exceptions.h>
#include <tools/yulPhaser/Program.h>
@@ -30,7 +32,6 @@
#include <boost/test/unit_test.hpp>
#include <cassert>
#include <regex>
#include <string>
using namespace std;
@@ -51,12 +52,6 @@ namespace
else
return _block;
}
string stripWhitespace(string const& input)
{
regex whitespaceRegex("\\s+");
return regex_replace(input, whitespaceRegex, "");
}
}
namespace solidity::phaser::test