[llvm] Machine Learned policy for inlining -Oz
This change is a placeholder, used to reference the submitted changes related to this work
Authors: Mircea Trofin, Yundi Qian, Eugene Brevdo
Please refer to the RFC for more details.
The change introduces a machine learned policy for -Oz inlining as a build-time opt-in.
There are two opt-in modes: 'release' and 'development'.
'Release' is the 'day-to-day compiler use'. A pre-trained ML model is compiled ahead of time into a native library, which is then used to make inlining
decisions. Determinism is ensured because the model is unchanged as the compiler runs.
Just like with hand-written heuristics, there is no formal guarantee that the policy performs well, only empirical results on benchmarks, based on which we derive a belief of general applicability.
'Development' is the mode used to train a model. Training happens offline, through reinforcement learning, by providing a training algorithm with traces of decisions made by the compiler on a training corpus (IR modules).
This initial change introduces all the 'release' mode components, together with a reference, pre-trained model, as well as key training mode components. More training mode components will be provided in subsequent changes. The reference model was trained on a Google-internal corpus of ~25K IR modules, and appears to generalize well to clang, opt, SPEC2006 (which are code bases sharing little to nothing with the training corpus), as well as some internal binaries. We observe as much as 6% size reduction (opt), and generally ~3%, when compared to clang -Oz.
To enable either/both modes, reference the buildbot script, as well as the buildbot definition.
TL;DR;
- Release mode.
Get the tensorflow pip package, find out where it is installed:
python3 -m pip install --upgrade pip python3 -m pip install --user tf_nightly==2.3.0.dev20200528 export TF_PIP=$(sudo -u buildbot python3 -m pip show tf_nightly | grep Location | cut -d ' ' -f 2)
Now setup the build by passing cmake -DTENSORFLOW_AOT_PATH=${TF_PIP}/tensorflow
- Development
Get the tensorflow C API library from https://www.tensorflow.org/install/lang_c. Install it somewhere, i.e. $TF_API. Then, pass -DTENSORFLOW_API_PATH=${TF_API} to cmake.
Note that TF_API points to the directory containing the include and lib directories provided by the install package.
The Release and Development mode may co-exist.
To opt-in to one of the modes (including 'default' - which is the manual heuristic), pass the (-mllvm) -enable-ml-advisor={default|release|development}. Note that, for development, you need to also pass in the path to the size estimator model, and/or path to the saved model currently under training - see lib/Analysis/DevelopmentModeInlineAdvisor.cpp.