2020-02-21 14:55:02 +00:00
|
|
|
/*
|
|
|
|
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/Phaser.h>
|
|
|
|
|
2020-02-21 15:10:07 +00:00
|
|
|
#include <tools/yulPhaser/AlgorithmRunner.h>
|
2020-02-26 15:20:44 +00:00
|
|
|
#include <tools/yulPhaser/Common.h>
|
2020-02-21 14:55:02 +00:00
|
|
|
#include <tools/yulPhaser/Exceptions.h>
|
|
|
|
#include <tools/yulPhaser/FitnessMetrics.h>
|
|
|
|
#include <tools/yulPhaser/GeneticAlgorithms.h>
|
|
|
|
#include <tools/yulPhaser/Program.h>
|
|
|
|
#include <tools/yulPhaser/SimulationRNG.h>
|
|
|
|
|
|
|
|
#include <liblangutil/CharStream.h>
|
|
|
|
|
|
|
|
#include <libsolutil/Assertions.h>
|
2020-02-26 15:20:44 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
2020-02-21 14:55:02 +00:00
|
|
|
#include <libsolutil/CommonIO.h>
|
|
|
|
|
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::langutil;
|
|
|
|
using namespace solidity::util;
|
|
|
|
using namespace solidity::phaser;
|
|
|
|
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
2020-02-26 15:20:44 +00:00
|
|
|
namespace
|
2020-02-21 14:55:02 +00:00
|
|
|
{
|
|
|
|
|
2020-02-26 15:20:44 +00:00
|
|
|
map<Algorithm, string> const AlgorithmToStringMap =
|
|
|
|
{
|
|
|
|
{Algorithm::Random, "random"},
|
|
|
|
{Algorithm::GEWEP, "GEWEP"},
|
|
|
|
};
|
|
|
|
map<string, Algorithm> const StringToAlgorithmMap = invertMap(AlgorithmToStringMap);
|
2020-02-21 14:55:02 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-02-26 15:20:44 +00:00
|
|
|
istream& phaser::operator>>(istream& _inputStream, Algorithm& _algorithm) { return deserializeChoice(_inputStream, _algorithm, StringToAlgorithmMap); }
|
|
|
|
ostream& phaser::operator<<(ostream& _outputStream, Algorithm _algorithm) { return serializeChoice(_outputStream, _algorithm, AlgorithmToStringMap); }
|
2020-02-21 14:55:02 +00:00
|
|
|
|
2020-02-21 16:18:13 +00:00
|
|
|
GeneticAlgorithmFactory::Options GeneticAlgorithmFactory::Options::fromCommandLine(po::variables_map const& _arguments)
|
2020-02-21 14:55:02 +00:00
|
|
|
{
|
2020-02-21 16:18:13 +00:00
|
|
|
return {
|
|
|
|
_arguments["algorithm"].as<Algorithm>(),
|
|
|
|
};
|
|
|
|
}
|
2020-02-21 14:55:02 +00:00
|
|
|
|
2020-02-21 16:18:13 +00:00
|
|
|
unique_ptr<GeneticAlgorithm> GeneticAlgorithmFactory::build(
|
|
|
|
Options const& _options,
|
|
|
|
size_t _populationSize,
|
|
|
|
size_t _minChromosomeLength,
|
|
|
|
size_t _maxChromosomeLength
|
|
|
|
)
|
|
|
|
{
|
|
|
|
assert(_populationSize > 0);
|
|
|
|
|
|
|
|
switch (_options.algorithm)
|
|
|
|
{
|
|
|
|
case Algorithm::Random:
|
|
|
|
return make_unique<RandomAlgorithm>(RandomAlgorithm::Options{
|
|
|
|
/* elitePoolSize = */ 1.0 / _populationSize,
|
|
|
|
/* minChromosomeLength = */ _minChromosomeLength,
|
|
|
|
/* maxChromosomeLength = */ _maxChromosomeLength,
|
|
|
|
});
|
|
|
|
case Algorithm::GEWEP:
|
|
|
|
return make_unique<GenerationalElitistWithExclusivePools>(GenerationalElitistWithExclusivePools::Options{
|
|
|
|
/* mutationPoolSize = */ 0.25,
|
|
|
|
/* crossoverPoolSize = */ 0.25,
|
|
|
|
/* randomisationChance = */ 0.9,
|
|
|
|
/* deletionVsAdditionChance = */ 0.5,
|
|
|
|
/* percentGenesToRandomise = */ 1.0 / _maxChromosomeLength,
|
|
|
|
/* percentGenesToAddOrDelete = */ 1.0 / _maxChromosomeLength,
|
|
|
|
});
|
|
|
|
default:
|
|
|
|
assertThrow(false, solidity::util::Exception, "Invalid Algorithm value.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unique_ptr<FitnessMetric> FitnessMetricFactory::build(
|
|
|
|
Program _program
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return make_unique<ProgramSize>(move(_program), RepetitionCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
Population PopulationFactory::build(
|
|
|
|
shared_ptr<FitnessMetric> _fitnessMetric
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return Population::makeRandom(
|
|
|
|
move(_fitnessMetric),
|
|
|
|
PopulationSize,
|
|
|
|
MinChromosomeLength,
|
|
|
|
MaxChromosomeLength
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProgramFactory::Options ProgramFactory::Options::fromCommandLine(po::variables_map const& _arguments)
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
_arguments["input-file"].as<string>(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Program ProgramFactory::build(Options const& _options)
|
|
|
|
{
|
|
|
|
CharStream sourceCode = loadSource(_options.inputFile);
|
2020-02-28 02:33:46 +00:00
|
|
|
variant<Program, ErrorList> programOrErrors = Program::load(sourceCode);
|
|
|
|
if (holds_alternative<ErrorList>(programOrErrors))
|
|
|
|
{
|
|
|
|
cerr << get<ErrorList>(programOrErrors) << endl;
|
|
|
|
assertThrow(false, InvalidProgram, "Failed to load program " + _options.inputFile);
|
|
|
|
}
|
|
|
|
return move(get<Program>(programOrErrors));
|
2020-02-21 16:18:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CharStream ProgramFactory::loadSource(string const& _sourcePath)
|
2020-02-21 14:55:02 +00:00
|
|
|
{
|
|
|
|
assertThrow(boost::filesystem::exists(_sourcePath), InvalidProgram, "Source file does not exist");
|
|
|
|
|
|
|
|
string sourceCode = readFileAsString(_sourcePath);
|
|
|
|
return CharStream(sourceCode, _sourcePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Phaser::main(int _argc, char** _argv)
|
|
|
|
{
|
|
|
|
CommandLineParsingResult parsingResult = parseCommandLine(_argc, _argv);
|
|
|
|
if (parsingResult.exitCode != 0)
|
|
|
|
return parsingResult.exitCode;
|
|
|
|
|
|
|
|
initialiseRNG(parsingResult.arguments);
|
|
|
|
|
2020-02-21 16:18:13 +00:00
|
|
|
runAlgorithm(parsingResult.arguments);
|
2020-02-21 14:55:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-21 18:23:33 +00:00
|
|
|
Phaser::CommandLineDescription Phaser::buildCommandLineDescription()
|
2020-02-21 14:55:02 +00:00
|
|
|
{
|
2020-02-21 18:23:33 +00:00
|
|
|
size_t const lineLength = po::options_description::m_default_line_length;
|
|
|
|
size_t const minDescriptionLength = lineLength - 23;
|
|
|
|
|
|
|
|
po::options_description keywordDescription(
|
2020-02-21 14:55:02 +00:00
|
|
|
"yul-phaser, a tool for finding the best sequence of Yul optimisation phases.\n"
|
|
|
|
"\n"
|
|
|
|
"Usage: yul-phaser [options] <file>\n"
|
|
|
|
"Reads <file> as Yul code and tries to find the best order in which to run optimisation"
|
|
|
|
" phases using a genetic algorithm.\n"
|
|
|
|
"Example:\n"
|
|
|
|
"yul-phaser program.yul\n"
|
|
|
|
"\n"
|
|
|
|
"Allowed options",
|
2020-02-21 18:23:33 +00:00
|
|
|
lineLength,
|
|
|
|
minDescriptionLength
|
2020-02-21 14:55:02 +00:00
|
|
|
);
|
2020-02-21 18:03:10 +00:00
|
|
|
|
|
|
|
po::options_description generalDescription("GENERAL", lineLength, minDescriptionLength);
|
|
|
|
generalDescription.add_options()
|
2020-02-21 14:55:02 +00:00
|
|
|
("help", "Show help message and exit.")
|
2020-02-25 17:52:56 +00:00
|
|
|
("input-file", po::value<string>()->required()->value_name("<PATH>"), "Input file.")
|
|
|
|
("seed", po::value<uint32_t>()->value_name("<NUM>"), "Seed for the random number generator.")
|
2020-02-21 18:03:10 +00:00
|
|
|
;
|
|
|
|
keywordDescription.add(generalDescription);
|
|
|
|
|
|
|
|
po::options_description algorithmDescription("ALGORITHM", lineLength, minDescriptionLength);
|
|
|
|
algorithmDescription.add_options()
|
2020-02-21 14:55:02 +00:00
|
|
|
(
|
|
|
|
"algorithm",
|
2020-02-25 17:52:56 +00:00
|
|
|
po::value<Algorithm>()->value_name("<NAME>")->default_value(Algorithm::GEWEP),
|
2020-02-21 14:55:02 +00:00
|
|
|
"Algorithm"
|
|
|
|
)
|
|
|
|
;
|
2020-02-21 18:03:10 +00:00
|
|
|
keywordDescription.add(algorithmDescription);
|
|
|
|
|
2020-02-21 14:55:02 +00:00
|
|
|
|
|
|
|
po::positional_options_description positionalDescription;
|
|
|
|
positionalDescription.add("input-file", 1);
|
2020-02-21 18:23:33 +00:00
|
|
|
|
|
|
|
return {keywordDescription, positionalDescription};
|
|
|
|
}
|
|
|
|
|
|
|
|
Phaser::CommandLineParsingResult Phaser::parseCommandLine(int _argc, char** _argv)
|
|
|
|
{
|
|
|
|
auto [keywordDescription, positionalDescription] = buildCommandLineDescription();
|
|
|
|
|
|
|
|
po::variables_map arguments;
|
2020-02-21 14:55:02 +00:00
|
|
|
po::notify(arguments);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
po::command_line_parser parser(_argc, _argv);
|
2020-02-21 18:23:33 +00:00
|
|
|
parser.options(keywordDescription).positional(positionalDescription);
|
2020-02-21 14:55:02 +00:00
|
|
|
po::store(parser.run(), arguments);
|
|
|
|
}
|
|
|
|
catch (po::error const & _exception)
|
|
|
|
{
|
|
|
|
cerr << _exception.what() << endl;
|
|
|
|
return {1, move(arguments)};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arguments.count("help") > 0)
|
|
|
|
{
|
2020-02-21 18:23:33 +00:00
|
|
|
cout << keywordDescription << endl;
|
2020-02-21 14:55:02 +00:00
|
|
|
return {2, move(arguments)};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arguments.count("input-file") == 0)
|
|
|
|
{
|
|
|
|
cerr << "Missing argument: input-file." << endl;
|
|
|
|
return {1, move(arguments)};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {0, arguments};
|
|
|
|
}
|
|
|
|
|
|
|
|
void Phaser::initialiseRNG(po::variables_map const& _arguments)
|
|
|
|
{
|
|
|
|
uint32_t seed;
|
|
|
|
if (_arguments.count("seed") > 0)
|
|
|
|
seed = _arguments["seed"].as<uint32_t>();
|
|
|
|
else
|
|
|
|
seed = SimulationRNG::generateSeed();
|
|
|
|
|
|
|
|
SimulationRNG::reset(seed);
|
|
|
|
cout << "Random seed: " << seed << endl;
|
|
|
|
}
|
|
|
|
|
2020-02-21 16:18:13 +00:00
|
|
|
void Phaser::runAlgorithm(po::variables_map const& _arguments)
|
2020-02-21 14:55:02 +00:00
|
|
|
{
|
2020-02-21 16:18:13 +00:00
|
|
|
auto programOptions = ProgramFactory::Options::fromCommandLine(_arguments);
|
|
|
|
auto algorithmOptions = GeneticAlgorithmFactory::Options::fromCommandLine(_arguments);
|
|
|
|
|
|
|
|
Program program = ProgramFactory::build(programOptions);
|
|
|
|
unique_ptr<FitnessMetric> fitnessMetric = FitnessMetricFactory::build(move(program));
|
|
|
|
Population population = PopulationFactory::build(move(fitnessMetric));
|
|
|
|
|
|
|
|
unique_ptr<GeneticAlgorithm> geneticAlgorithm = GeneticAlgorithmFactory::build(
|
|
|
|
algorithmOptions,
|
|
|
|
population.individuals().size(),
|
|
|
|
PopulationFactory::MinChromosomeLength,
|
|
|
|
PopulationFactory::MaxChromosomeLength
|
2020-02-21 14:55:02 +00:00
|
|
|
);
|
|
|
|
|
2020-02-21 18:46:18 +00:00
|
|
|
AlgorithmRunner algorithmRunner(population, AlgorithmRunner::Options{}, cout);
|
2020-02-21 16:18:13 +00:00
|
|
|
algorithmRunner.run(*geneticAlgorithm);
|
2020-02-21 14:55:02 +00:00
|
|
|
}
|