Merge pull request #8514 from imapp-pl/yul-phaser-prefix-option

[yul-phaser] --prefix option
This commit is contained in:
chriseth 2020-03-24 17:35:45 +01:00 committed by GitHub
commit f89e154693
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 5 deletions

View File

@ -324,11 +324,14 @@ BOOST_AUTO_TEST_CASE(build_should_load_programs_from_files)
{ {
TemporaryDirectory tempDir; TemporaryDirectory tempDir;
vector<string> sources{"{}", "{{}}", "{{{}}}"}; vector<string> sources{"{}", "{{}}", "{{{}}}"};
ProgramFactory::Options options{/* inputFiles = */ { ProgramFactory::Options options{
/* inputFiles = */ {
tempDir.memberPath("program1.yul"), tempDir.memberPath("program1.yul"),
tempDir.memberPath("program2.yul"), tempDir.memberPath("program2.yul"),
tempDir.memberPath("program3.yul"), tempDir.memberPath("program3.yul"),
}}; },
/* prefix = */ "",
};
for (size_t i = 0; i < sources.size(); ++i) for (size_t i = 0; i < sources.size(); ++i)
{ {
@ -346,6 +349,31 @@ BOOST_AUTO_TEST_CASE(build_should_load_programs_from_files)
} }
} }
BOOST_AUTO_TEST_CASE(build_should_apply_prefix)
{
TemporaryDirectory tempDir;
ProgramFactory::Options options{
/* inputFiles = */ {tempDir.memberPath("program1.yul")},
/* prefix = */ "f",
};
CharStream nestedSource("{{{let x:= 1}}}", "");
Program nestedProgram = get<Program>(Program::load(nestedSource));
Program flatProgram = get<Program>(Program::load(nestedSource));
flatProgram.optimise(Chromosome("f").optimisationSteps());
assert(toString(nestedProgram) != toString(flatProgram));
{
ofstream tmpFile(options.inputFiles[0]);
tmpFile << nestedSource.source() << endl;
}
vector<Program> programs = ProgramFactory::build(options);
BOOST_TEST(programs.size() == 1);
BOOST_TEST(toString(programs[0]) == toString(flatProgram));
}
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()

View File

@ -285,6 +285,7 @@ ProgramFactory::Options ProgramFactory::Options::fromCommandLine(po::variables_m
{ {
return { return {
_arguments["input-files"].as<vector<string>>(), _arguments["input-files"].as<vector<string>>(),
_arguments["prefix"].as<string>(),
}; };
} }
@ -300,6 +301,8 @@ vector<Program> ProgramFactory::build(Options const& _options)
cerr << get<ErrorList>(programOrErrors) << endl; cerr << get<ErrorList>(programOrErrors) << endl;
assertThrow(false, InvalidProgram, "Failed to load program " + path); assertThrow(false, InvalidProgram, "Failed to load program " + path);
} }
get<Program>(programOrErrors).optimise(Chromosome(_options.prefix).optimisationSteps());
inputPrograms.push_back(move(get<Program>(programOrErrors))); inputPrograms.push_back(move(get<Program>(programOrErrors)));
} }
@ -348,6 +351,18 @@ Phaser::CommandLineDescription Phaser::buildCommandLineDescription()
generalDescription.add_options() generalDescription.add_options()
("help", "Show help message and exit.") ("help", "Show help message and exit.")
("input-files", po::value<vector<string>>()->required()->value_name("<PATH>"), "Input files.") ("input-files", po::value<vector<string>>()->required()->value_name("<PATH>"), "Input files.")
(
"prefix",
po::value<string>()->value_name("<CHROMOSOME>")->default_value(""),
"Initial optimisation steps automatically applied to every input program.\n"
"The result is treated as if it was the actual input, i.e. the steps are not considered "
"a part of the chromosomes and cannot be mutated. The values of relative metric values "
"are also relative to the fitness of a program with these steps applied rather than the "
"fitness of the original program.\n"
"Note that phaser always adds a 'hgo' prefix to ensure that chromosomes can "
"contain arbitrary optimisation steps. This implicit prefix cannot be changed or "
"or removed using this option. The value given here is applied after it."
)
("seed", po::value<uint32_t>()->value_name("<NUM>"), "Seed for the random number generator.") ("seed", po::value<uint32_t>()->value_name("<NUM>"), "Seed for the random number generator.")
( (
"rounds", "rounds",

View File

@ -169,6 +169,7 @@ public:
struct Options struct Options
{ {
std::vector<std::string> inputFiles; std::vector<std::string> inputFiles;
std::string prefix;
static Options fromCommandLine(boost::program_options::variables_map const& _arguments); static Options fromCommandLine(boost::program_options::variables_map const& _arguments);
}; };