From a66ceb11c683d1a2cc1c62f028163ca3e9e2305e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Fri, 20 Mar 2020 17:44:34 +0100 Subject: [PATCH] [yul-phaser] Add --prefix option --- test/yulPhaser/Phaser.cpp | 38 +++++++++++++++++++++++++++++++++----- tools/yulPhaser/Phaser.cpp | 15 +++++++++++++++ tools/yulPhaser/Phaser.h | 1 + 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/test/yulPhaser/Phaser.cpp b/test/yulPhaser/Phaser.cpp index aab34db61..04dfbe3dc 100644 --- a/test/yulPhaser/Phaser.cpp +++ b/test/yulPhaser/Phaser.cpp @@ -324,11 +324,14 @@ BOOST_AUTO_TEST_CASE(build_should_load_programs_from_files) { TemporaryDirectory tempDir; vector sources{"{}", "{{}}", "{{{}}}"}; - ProgramFactory::Options options{/* inputFiles = */ { - tempDir.memberPath("program1.yul"), - tempDir.memberPath("program2.yul"), - tempDir.memberPath("program3.yul"), - }}; + ProgramFactory::Options options{ + /* inputFiles = */ { + tempDir.memberPath("program1.yul"), + tempDir.memberPath("program2.yul"), + tempDir.memberPath("program3.yul"), + }, + /* prefix = */ "", + }; 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::load(nestedSource)); + Program flatProgram = get(Program::load(nestedSource)); + flatProgram.optimise(Chromosome("f").optimisationSteps()); + assert(toString(nestedProgram) != toString(flatProgram)); + + { + ofstream tmpFile(options.inputFiles[0]); + tmpFile << nestedSource.source() << endl; + } + + vector 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() diff --git a/tools/yulPhaser/Phaser.cpp b/tools/yulPhaser/Phaser.cpp index da0296eff..b29938f22 100644 --- a/tools/yulPhaser/Phaser.cpp +++ b/tools/yulPhaser/Phaser.cpp @@ -285,6 +285,7 @@ ProgramFactory::Options ProgramFactory::Options::fromCommandLine(po::variables_m { return { _arguments["input-files"].as>(), + _arguments["prefix"].as(), }; } @@ -300,6 +301,8 @@ vector ProgramFactory::build(Options const& _options) cerr << get(programOrErrors) << endl; assertThrow(false, InvalidProgram, "Failed to load program " + path); } + + get(programOrErrors).optimise(Chromosome(_options.prefix).optimisationSteps()); inputPrograms.push_back(move(get(programOrErrors))); } @@ -348,6 +351,18 @@ Phaser::CommandLineDescription Phaser::buildCommandLineDescription() generalDescription.add_options() ("help", "Show help message and exit.") ("input-files", po::value>()->required()->value_name(""), "Input files.") + ( + "prefix", + po::value()->value_name("")->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()->value_name(""), "Seed for the random number generator.") ( "rounds", diff --git a/tools/yulPhaser/Phaser.h b/tools/yulPhaser/Phaser.h index 2e72c31f6..9814c9111 100644 --- a/tools/yulPhaser/Phaser.h +++ b/tools/yulPhaser/Phaser.h @@ -169,6 +169,7 @@ public: struct Options { std::vector inputFiles; + std::string prefix; static Options fromCommandLine(boost::program_options::variables_map const& _arguments); };