opt-viewer can tell you more about missed optimization opportunities. The
optimizers that clang
uses can explain the rationale for why their specific
optimization method couldn't be leveraged in given parts of your source.
Grab a very recent clang, 4.0 or later for your architecture/OS distro and install or unpack it on your machine.
The example below is for ARM7a linux, there are several other releases available.
curl -O http://releases.llvm.org/4.0.0/clang+llvm-4.0.0-armv7a-linux-gnueabihf.tar.xz
tar xf clang+llvm-4.0.0-rc1-armv7a-linux-gnueabihf.tar.xz
Or instead you might have Ubuntu. Below is how you would do it for Ubuntu Trusty. If you're not sure which Ubuntu release you have, see "Checking Your Ubuntu Version".
wget -nv -O - http://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
sudo apt-add-repository -y 'deb http://apt.llvm.org/trusty/ llvm-toolchain-trusty-4.0 main'
sudo apt-get update -qq
sudo apt-get install -qq -y clang-4.0 llvm-4.0
Next, you must get a copy of opt-viewer. For now, it's not distributed with clang+llvm releases, so a simple way to get it is to just get a copy of the llvm project source from github. Unpack or clone it somewhere convenient and note where you put it.
Once you have a new clang installed, build your software with it. You must
include "-fsave-optimization-record
" among the arguments. Imagine we have
the following sample C/C++ project:
sample/
└── src
├── bar.cpp
├── baz.cpp
├── foo.c
└── Makefile
If you're using make
, you could add these lines to your Makefile
:
CXXFLAGS+=-fsave-optimization-record
CLAGS+=-fsave-optimization-record
and then clang will build your program or library like so:
clang-4.0 -fsave-optimization-record -c -o foo.o foo.cpp
...
clang
will create build annotation artifacts in the YAML format. Now
you can use opt-viewer
to see these in relation to your project.
First, gather the necessary dependencies for opt-viewer:
virtualenv optviewer_env
source optviewer_env/bin/activate
pip install pyyaml pygments
Let's assume your sample
project is in the same directory as
the one where you unpacked/cloned llvm
. Then you should invoke opt-viewer
like so:
llvm/utils/opt-viewer/opt-viewer.py -source-dir sample/src/ \
sample/src/baz.opt.yaml \
sample/src/bar.opt.yaml \
sample/src/foo.opt.yaml \
./output_report/
In the output_report
dir, we'll have some output files that could look like
below:
If you are able to leverage profile-guided-optimization (PGO), opt-viewer will produce an index that's sorted by the most-frequently executed code ("hotness"). This makes it easy to see which functions and methods would benefit the most from improved optimization.
For more info on PGO, see the clang
manual on
PGO.