solidity/tools/yulPhaser
2021-05-17 11:35:07 +01:00
..
AlgorithmRunner.cpp Add SPDX license identifier if not present already in source file 2020-07-17 20:24:12 +05:30
AlgorithmRunner.h Fix compilation errors with GCC 11 2021-05-17 11:35:07 +01:00
Chromosome.cpp [yul-phaser] Chromosome: Make it possible to use genes that do not correspond to existing optimiser steps 2020-09-14 16:17:39 +02:00
Chromosome.h Fix compilation errors with GCC 11 2021-05-17 11:35:07 +01:00
Common.cpp Add SPDX license identifier if not present already in source file 2020-07-17 20:24:12 +05:30
Common.h Add SPDX license identifier if not present already in source file 2020-07-17 20:24:12 +05:30
Exceptions.h Add SPDX license identifier if not present already in source file 2020-07-17 20:24:12 +05:30
FitnessMetrics.cpp Enable the -Wconversion warning 2020-12-08 16:45:24 +00:00
FitnessMetrics.h Add SPDX license identifier if not present already in source file 2020-07-17 20:24:12 +05:30
GeneticAlgorithms.cpp Add SPDX license identifier if not present already in source file 2020-07-17 20:24:12 +05:30
GeneticAlgorithms.h Fix compilation errors with GCC 11 2021-05-17 11:35:07 +01:00
main.cpp Add SPDX license identifier if not present already in source file 2020-07-17 20:24:12 +05:30
Mutations.cpp Enable the -Wconversion warning 2020-12-08 16:45:24 +00:00
Mutations.h Add SPDX license identifier if not present already in source file 2020-07-17 20:24:12 +05:30
PairSelections.cpp Enable the -Wconversion warning 2020-12-08 16:45:24 +00:00
PairSelections.h Fix compilation errors with GCC 11 2021-05-17 11:35:07 +01:00
Phaser.cpp Enable the -Wconversion warning 2020-12-08 16:45:24 +00:00
Phaser.h Fix compilation errors with GCC 11 2021-05-17 11:35:07 +01:00
Population.cpp [yul-phaser] isFitter(): Switch from toString() to genes() to make chromosome comparisons a tiny bit faster 2020-09-11 23:09:51 +02:00
Population.h Fix compilation errors with GCC 11 2021-05-17 11:35:07 +01:00
Program.cpp Optimizer context has parameter expectedExecutionsPerDeployment 2021-04-22 17:27:57 +02:00
Program.h Fix compilation errors with GCC 11 2021-05-17 11:35:07 +01:00
ProgramCache.cpp Add SPDX license identifier if not present already in source file 2020-07-17 20:24:12 +05:30
ProgramCache.h Fix compilation errors with GCC 11 2021-05-17 11:35:07 +01:00
README.md More information about --yul-optimizations option in READMEs and docs 2020-05-08 22:29:57 +02:00
Selections.cpp Enable the -Wconversion warning 2020-12-08 16:45:24 +00:00
Selections.h Fix compilation errors with GCC 11 2021-05-17 11:35:07 +01:00
SimulationRNG.cpp Enable the -Wconversion warning 2020-12-08 16:45:24 +00:00
SimulationRNG.h Fix compilation errors with GCC 11 2021-05-17 11:35:07 +01:00

yul-phaser

yul-phaser is an internal tool for finding good sequences of optimisation steps for Yul optimiser.

How it works

The space of possible solutions to this problem (usually referred to as phase-ordering problem) is extremely large and there may even be no single sequence that produces optimal results for all possible programs.

The tool uses genetic algorithms to find sequences that result in better programs than others and to iteratively refine them. The input is a set of one or more Yul programs and each sequence is applied to all of these programs. Optimised programs are given numeric scores according to the selected metric.

Optimisation step sequences are presented in an abbreviated form - as strings of letters where each character represents one step. There's a table listing available abbreviations in the optimiser docs.

How to use it

The application has sensible defaults for most parameters. An invocation can be as simple as:

tools/yul-phaser ../test/libyul/yulOptimizerTests/fullSuite/*.yul \
    --random-population 100

This assumes that you have a working copy of the Solidity repository and you're in the build directory within that working copy.

Run yul-phaser --help for a full list of available options.

Restarting from a previous state

yul-phaser can save the list of sequences found after each round:

tools/yul-phaser *.yul        \
    --random-population   100 \
    --population-autosave /tmp/population.txt

If you stop the application, you can later use the file to continue the search from the point you left off:

tools/yul-phaser *.yul                         \
    --population-from-file /tmp/population.txt \
    --population-autosave  /tmp/population.txt

Analysing a sequence

Apart from running the genetic algorithm, yul-phaser can also provide useful information about a particular sequence.

For example, to see the value of a particular metric for a given sequence and program run:

tools/yul-phaser *.yul            \
    --show-initial-population     \
    --rounds            0         \
    --metric            code-size \
    --metric-aggregator sum       \
    --population        <your sequence>

You can also easily see program code after being optimised using that sequence:

tools/yul-phaser *.yul                    \
    --rounds     0                        \
    --mode       print-optimised-programs \
    --population <your sequence>

Using output from Solidity compiler

yul-phaser can process the intermediate representation produced by solc:

solc/solc <sol file> --ir --output-dir <output directory>

After running this command you'll find one or more .yul files in the output directory. These files contain whole Yul objects rather than just raw Yul programs but yul-phaser is prepared to handle them too.

Using optimisation step sequences with the compiler

You can tell Yul optimiser to use a specific sequence for your code by passing --yul-optimizations option to solc:

solc/solc <sol file> --optimize --ir-optimized --yul-optimizations <sequence>

How to choose good parameters

Choosing good parameters for a genetic algorithm is not a trivial task but phaser's defaults are generally enough to find a sequence that gives results comparable or better than one hand-crafted by an experienced developer for a given set of programs. The difficult part is providing a fairly representative set of input files. If the files you give don't need certain optimisations the tool will find sequences that don't use these optimisations and perform badly for programs that could benefit from them. If all the provided files greatly benefit from a specific optimisation, the sequence may not work well for programs that do not.

We have conducted a set of rough experiments to evaluate some combinations of parameter values. The conclusions were used to adjust the defaults but you might still benefit from some general observations:

  1. The algorithm that performed the best was GEWEP.
  2. Using longer sequences in the initial population yields better results. The algorithm is good at removing superfluous steps.
  3. Preserving the top sequences from previous rounds improves results. Elite should contain at least a few individuals, especially when using the classic algorithm.
  4. Don't set mutation/deletion/addition chance too high. It makes results worse because it destroys the good patterns preserved by crossover. Values around 1-5% seem to work best.
  5. Keep the algorithm running for 1000 rounds or more. It usually finds good sequences faster than that but it can shorten them significantly if you let it run longer. This is especially important when starting with long sequences.