Postponing more passes in LTO

Looking at the existing flow of passes for LTO, it appears that most all passes are run on a per file basis, before the call to the gold linker. I’m looking to get people’s feedback on whether there would be an advantage to waiting to run a number of these passes until the linking stage. For example, I believe I saw a post a little while back about postponing vectorization until the linking stage. It seems to me that there could be an advantage to postponing (some) passes until the linking stage, where the entire graph is available. In general, what do people think about the idea of a different flow of LTO where more passes are postponed until the linking stage?

Daniel Stewart

I think there needs to be some amount of cleanup before cross module
inlining otherwise you're going to lose a lot of inlining chances that
you'd have had. It's a bit of a tradeoff. I remember working up a pipeline
with Chandler and Dan at one point and I believe Bob was in on the
discussion too. I don't have notes of the actual pipeline so I'm adding
them all to the thread to pipe up :slight_smile:

-eric

Yes, that seems to be the consensus. -flto during the compile step should imply things like: no vectorization until after cross-module inlining, reduced inlining threshold (only inline if it reduces code size), and other things.

If the notes about that pipeline are still around, I’d love to hear about/look at it. I’d like to investigate some changes to LTO, but certainly want to know what has already been discussed/discovered about the flow.

Daniel

proposal from Chandler was to run the inliner with a threshold of 0 if
given -flto. This would inline in each TU only if that wouldn't
complicate things for inlining during linking.

Implementing it should be fairly simple, so it might be a nice first
thing to try.

Cheers,
Rafael

I have done some preliminary investigation into postponing some of the passes to see what the resulting performance impact would be. This is a fairly crude attempt at moving passes around to see if there is any potential benefit. I have attached the patch I used to do the tests, in case anyone is interested.

Briefly, the patch allows two different flows, with either a flag of –lto-new or –lto-new2. In the first case, the vectorization passes are postponed from the end of populateModulePassManager() function to midway through the addLTOOptimizationPasses(). In the second case, essentially the entire populateModulePassManager() function is deferred until link time.

I ran spec2000/2006 on an ARM platform (Nexus 4), comparing 4 configurations (O3, O3 LTO, O3 LTO new, O3 LTO new 2). I have attached a PDF presenting the results from the test. The first 4 columns have the spec result (ratio) for the 4 different configurations. The second set of columns are the respective test / max(result of 4 configurations). I used this last one to see how well/poor a particular configuration was in comparison to other configurations.

In general, there appears to be some benefit to be gained in a couple of the benchmarks (spec2000/art, spec2006/milc) by postponing vectorization.

I just wanted to present this information to the community to see if there is interest in pursuing the idea of postponing passes.

Daniel

newflow.patch (6.56 KB)

Community LLVM on Nexus4.pdf (120 KB)

Looking at the existing flow of passes for LTO, it appears that most all
passes are run on a per file basis, before the call to the gold linker. I’m
looking to get people’s feedback on whether there would be an advantage to
waiting to run a number of these passes until the linking stage. For
example, I believe I saw a post a little while back about postponing
vectorization until the linking stage. It seems to me that there could be
an advantage to postponing (some) passes until the linking stage, where the
entire graph is available. In general, what do people think about the idea
of a different flow of LTO where more passes are postponed until the
linking stage?

AFAIK, we still mostly obey the per-TU optimization flags. E.g. if you pass
-O3 for each TU, we will run -O3 passes without really keeping in mind that
we are doing LTO (or if we do, then it is fairly minimal). The per-TU
optimization flags can have an enormous impact on the final binary size.
Here are some data points I have recently collected on one of our
first-party games:

noLTO O3perTU:
71.1MiB (control)

LTO O3perTU:
71.8MiB (1% larger)

LTO O0perTU:
67.4MiB (5% smaller)

LTO O1perTU:
68.5MiB (4% smaller)

LTO OsperTU:
65.3MiB (8% smaller)

This is with a 3.4-based compiler btw, but is in keeping with what I
observed last Summer, so I assume that the significant effect on binary
size is still present today.
FYI, these elf sizes are also with no debug info.

Here is a visualization of those same binary files, but broken down by text
and data sections (as given by llvm-size; bss was not significantly
affected so it was omitted):

http://i.imgur.com/Ie5Plgx.png

​As you can see (and would expect), LTO does a good job of reducing the
data size, since it can use whole-program analysis to eliminate it. This
benefit does not depend on the per-TU optimization level, also as you would
expect.
The text section however has a different behavior. I'm still investigating,
but I suspect any size regression is largely due to excessive inlining (as
I think most people would expect). It is interesting to note that between
the -Os LTO case and -O3 LTO case, there is a text size difference of
(20.7/14.3 - 1) ~ 45%. Also, looking at this again, I don't understand why
I didn't do anything with -O2 (I'll eventually need to re-obtain these
datasets with a ToT compiler anyway, and I will be sure to grab -O2 data);
my experience is that Clang's -O3 is sufficiently similar to -O2 that I'm
fairly confident that this missing data is not going to significantly alter
the findings of my preliminary analysis in the upcoming days.

For starters, here is a plot showing how much of the total text size is
attributable to functions of each size, comparing -O3 noLTO with -O3 LTO:

http://i.imgur.com/pfIo0sy.png [*]
To understand this plot, imagine if you were to take all the functions in
the binary, and group them into a small number of buckets of
similarly-sized functions. Each bar represents one bucket, and the height
of the bar represents the total size of all the functions in that bucket.
The width and position of the bucket indicate which range of sizes it
corresponds to.
Although the general behavior is a shift in the distribution to the right
(functions are becoming larger with LTO), there is also an increase in
total area under the bars, which is perhaps best visualized by looking at
the same plot, but with each bar indicating the cumulative total (imagine
that you were to call std::accumulate on the list of bar heights from the
previous plot):

http://i.imgur.com/q7Iq7AH.png
The overall text size regression adds up to nearly 25%.

[*] The two outliers in the non-LTO case are:
- the global initializers (_GLOBAL__I_a), whose size is significantly
reduced by LTO from about 400k to 100k (this single function corresponds to
the entire furthest-right bar). Note: the right-most bar for the LTO
dataset (>100kB functions) is this function (slimmed down to about 100k)
and one other that was subjected to an unusually large amount of inlining
and grew from 2k to about 125k.
- an unusually large dead function that LTO was able to remove but was not
being removed before (this single function corresponds to the entire
second-to-furthest-right bar).

-- Sean Silva

In the future could you please do some sort of visualization of your data, or at least provide the raw data in a machine-readable format so that others can do so?

It is incredibly easy to come to incorrect conclusions when looking at lists of numbers because at any given moment you have a very localized view of the dataset and are prone to locally pattern-match and form a selection bias that corrupts your ability to make a proper decision in the context of the entire dataset. Even if you go on to look at the rest of the data, this selection bias limits your ability to come to a correct “global” conclusion.

Appropriate reliable summary statistics can also help, but are not panacea. In using, say, 2 summary statistics (e.g. mean and standard deviation), one is discarding a large number of degrees of freedom from the dataset. This is fine if you have good reason to believe that these 2 degrees of freedom adequately explain the underlying dataset (e.g. there is a sound theoretical description of the phenomenon being measured that suggests it should follow a Gaussian distribution; hence mean and stddev completely characterize the distribution). However, in the world of programs and compiler optimizations, there is very rarely a good reason to believe that any particular dataset (e.g. benchmark results for SPEC for a particular optimization) is explained by a handful of common summary statistics, and so looking only at summary statistics can often conceal important insights into the data (or even be actively misleading). This is especially true when looking across different programs (I die a little inside every time I see someone cite a SPEC geomean).

In compilers we are usually more interested in actually discovering which parameters are responsible for variation, rather than increasing confidence in the values of an a priori set of known parameters. E.g. if you are measuring the time it takes a laser beam to bounce off the moon and come back to you (in order to measure the distance of the moon) you have an a priori known set of parameters that well-characterize the data you obtain, based on your knowledge of the measurement apparatus, atmospheric dispersion, the fact that you know the moon is moving in an orbit, etc. You can perform repeated measurements with the apparatus to narrow in on the values of the parameters. In compilers, we rarely have such a simple and small set of parameters that are known to adequately characterize the data we are trying to understand; when investigating an optimization’s results, we are almost always investigating a situation that would resemble (in the moon-bounce analogy) an unknown variation that turns out to be due to whether the lab assistant is leaning against the apparatus or not. You’re not going to find out that the lab assistant’s pose is at fault by looking at your “repeatedly do them to increase confidence in the values” measurements (e.g. the actual moon-bounce measurements; or looking at the average time for a particular SPEC benchmark to run); you find it by getting up and going to the room with the apparatus and investigating all manner of things until you narrow in on the lab assistant’s pose (usually this takes the form of having to dig around in assembly, extract kernels, time particular sub-things, profile things, look at what how the code changes throughout the optimization pipeline, instrument things, etc.; there are tons of places for the root cause to hide).

If you have to remember something from that last paragraph, remember that not everything boils down to click “run” and get a timing for SPEC. Often you need to take some time to narrow in on the damn lab assistant. Sometimes just the timing of a particular benchmark leads to a “lab assistant” situation (although hopefully this doesn’t happen too often; it does happen though: e.g. I have been in a situation where a benchmark surprisingly goes 50% faster on about 1/10 runs). When working across different programs, you are almost always in a “lab assistant” situation.

– Sean Silva

This looks really interesting. As my colleague Gao mentioned in his lightning talk on our LTO implementation at the last developer meeting we’re definitely interested in seeing if there are any potential gains to be had by deferring passes.

I’d like to give it a try on some of our codebases, although with the Christmas break coming up and various other commitments after that in January I’m not sure when I’ll be able to look at it properly. If there is any other followup here, then please don’t let that block anything from moving forward, but otherwise I’ll do my best to reply here as soon as I possibly can after doing some experimentation!

Greg Bedwell
SN Systems - Sony Computer Entertainment Group

Also, a couple notes about the use of standard deviation (and general stuff about benchmarking):

  1. It is vital to keep in mind the distinction between a “sample statistic” and a “population statistic”. A sample statistic is a statistic derived from the dataset of measured values, while a population statistic is a mathematical function of the underlying probability distribution of the phenomenon (some abstract “population” of possibilities) being sampled.

  2. As you take more measurements, the standard deviation sample statistic converges to the standard deviation population statistic regardless of the underlying population distribution.

  3. Every probability distribution has a defined standard deviation (it is just a mathematical function of the distribution). For a Gaussian distribution, the standard deviation (in conjunction with the mean) completely describes the probability distribution. This is emphatically not the case for all probability distributions [1]. In order to derive insight from a standard deviation sample statistic, you must a priori have a good reason to believe that the underlying probability distribution’s standard deviation provides useful information about the actual distribution.

  4. Any probability distribution can be made to have any standard deviation without changing its distribution shape (technically, at most a linear transformation of the coordinate is needed). To pick a random example, for a highly bimodal distribution the standard deviation doesn’t really give you any useful information about the overall shape (and it doesn’t exactly correspond between the spacing between the modes either).

  5. The standard deviation is nothing more than a root-mean-square average. Basically, it is a very popular statistic because there is an extremely powerful way to model phenomena for which the root-mean-square average is the actual meaningful value that ties into the model. The theory I’m talking about is used extensively in almost every science and engineering discipline outside of software engineering (and the purely digital part of hardware) [2]. Since this modeling technique is rarely directly relevant in software, there is little reason to assume that the standard deviation is a good statistic (there may be a good reason to, but I haven’t seen one).

Okay, so I’ve cast doubt on the use of standard deviation (and actually an argument could be made against the mean too for similar reasons). On a more constructive note, what is a good small set of parameters to summarize measurements of program run time? One interesting model that was brought to my attention by Nick Lewycky is based on the following observation: most “perturbations” of the run time of a fixed program binary on a fixed chip and fixed OS are due to externalities that contend for (or otherwise deprive the program of) execution resources and therefore almost always make it slower.

The model is then that there is a “true” execution time for the program, which is a completely deterministic function of the program, chip, and OS; intuitively, this is time occurs when the program is e.g. able to execute by itself, on a single execution unit of the CPU, with no interrupts knocking it off CPU, and no other threads using any of the execution resources of this CPU. On top of this, we assume that there is a set of perturbations that contend for execution resources and slow the program down.

For this model, it makes sense to approximate the “true” value by the maximum of the recorded values. If we want to use two numbers to summarize the data, it probably makes sense to look at a way of characterizing the extent of the variation from this “true” value. In the absence of proper characterization of the perturbations (such as the distribution of when they occur and how much effect they have when they occur), one simple solution is the minimum. Max and min might be too sensitive to the perturbations (especially disk cache of the program itself on the first run), so a pair of percentiles might be a bit more reliable, such as 10th percentile and 90th percentile.

Next time I’m measuring program run time, I’m going to try out this model. It should be possible to look at a higher-degree-of-freedom representation of the dataset, like a histogram of the distribution, in order to evaluate how well this two-parameter characterization of the data works compared with the typical mean and stddev. Also, ultimately we only need as much precision here as is necessary for what we are doing with the summarized data, which typically is to alert us of a significant change. On a sufficiently quiet machine, the variance of the measurements might be significantly smaller than the thresholds that we consider “significant” so that the mean, median, max, min, 10%-ile etc. are all so close that it does not matter which we use, and we can summarize the data with just a single number, which can be any one of them (and just trigger an error if total variation exceeds a set amount).

However, the noisier the machine (and hence our data), the more important is is to properly model and analyze things to avoid coming to a faulty conclusion (note: reliable conclusions can be made from data with large amounts of noise as long as there is a good model which allows isolating the data we are interested in from the noise [3]).

Nick, I forget from when we talked, but did you guys ever settle on a model like this for your internal benchmarking?

[1] If you are familiar with Fourier theory, using only two statistics to describe a probability distribution is sort of like using only two Fourier components to describe a function. In order to conclude anything you must a priori know that the other components are not important!

[2] The modeling technique I’m talking about is decomposition of square-integrable function spaces into an orthonormal basis (this is by no means the most general way of describing the idea). This is a far-reaching concept and pops up under many names. Things like “frequency domain”, “Fourier transform”, “Laplace transform”, and “spectrum” are among the most elementary. More advanced ones are “wavelets”, “Hilbert space”, “eigenfunctions of a linear operator”. A smattering of use cases: determining the shape of the electron orbitals of hydrogen, designing the fuel control system of a jet engine to make sure the right amount of fuel is being provided at any given moment, designing the circuits sitting at the end of a communication line (or a cell phone antenna) that have to detect incoming signals with the least chance of error, analyzing the structure of molecules based on X-ray diffraction, determining the chemical composition of the atmosphere of a planet orbiting another star.

[3] For example if you know that there is Gaussian noise (even a very large amount) on top of an underlying value, then the underlying value is just the mean of the population (which will be a Gaussian distribution) and can be reliably determined from the mean sample statistic.

Also, a couple notes about the use of standard deviation (and general
stuff about benchmarking):

1. It is *vital* to keep in mind the distinction between a "sample
statistic" and a "population statistic". A sample statistic is a statistic
derived from the dataset of measured values, while a population statistic
is a mathematical function of the underlying probability distribution of
the phenomenon (some abstract "population" of possibilities) being sampled.

2. As you take more measurements, the standard deviation sample statistic
converges to the standard deviation population statistic regardless of the
underlying population distribution.

3. *Every* probability distribution has a defined standard deviation (it
is just a mathematical function of the distribution). For a Gaussian
distribution, the standard deviation (in conjunction with the mean)
completely describes the probability distribution. This is emphatically
*not* the case for all probability distributions [1]. In order to derive
insight from a standard deviation sample statistic, you *must* a priori
have a good reason to believe that the underlying probability
distribution's standard deviation provides useful information about the
actual distribution.

4. Any probability distribution can be made to have any standard deviation
without changing its distribution shape (technically, at most a linear
transformation of the coordinate is needed). To pick a random example, for
a highly bimodal distribution the standard deviation doesn't really give
you any useful information about the overall shape (and it doesn't exactly
correspond between the spacing between the modes either).

5. The standard deviation is nothing more than a root-mean-square average.
Basically, it is a very popular statistic because there is an *extremely
powerful* way to model phenomena for which the root-mean-square average is
the actual meaningful value that ties into the model. The theory I'm
talking about is used extensively in almost every science and engineering
discipline *outside of* software engineering (and the purely digital part
of hardware) [2]. Since this modeling technique is rarely directly relevant
in software, there is little reason to assume that the standard deviation
is a good statistic (there may be a good reason to, but I haven't seen one).

Okay, so I've cast doubt on the use of standard deviation (and actually an
argument could be made against the mean too for similar reasons). On a more
constructive note, what *is* a good small set of parameters to summarize
measurements of program run time? One interesting model that was brought to
my attention by Nick Lewycky is based on the following observation: most
"perturbations" of the run time of a fixed program binary on a fixed chip
and fixed OS are due to externalities that contend for (or otherwise
deprive the program of) execution resources and therefore almost always
make it *slower*.

The model is then that there is a "true" execution time for the program,
which is a completely deterministic function of the program, chip, and OS;
intuitively, this is time occurs when the program is e.g. able to execute
by itself, on a single execution unit of the CPU, with no interrupts
knocking it off CPU, and no other threads using any of the execution
resources of this CPU. On top of this, we assume that there is a set of
perturbations that contend for execution resources and slow the program
down.

For this model, it makes sense to approximate the "true" value by the
maximum of the recorded values.

D'oh! Here I meant "maximum performance", which would be the *minimum* time!

-- Sean Silva

Sean Silva wrote:

Also, a couple notes about the use of standard deviation (and general
stuff about benchmarking):

1. It is *vital* to keep in mind the distinction between a "sample
statistic" and a "population statistic". A sample statistic is a
statistic derived from the dataset of measured values, while a
population statistic is a mathematical function of the underlying
probability distribution of the phenomenon (some abstract "population"
of possibilities) being sampled.

2. As you take more measurements, the standard deviation sample
statistic converges to the standard deviation population statistic
regardless of the underlying population distribution.

3. *Every* probability distribution has a defined standard deviation (it
is just a mathematical function of the distribution). For a Gaussian
distribution, the standard deviation (in conjunction with the mean)
completely describes the probability distribution. This is emphatically
*not* the case for all probability distributions [1]. In order to derive
insight from a standard deviation sample statistic, you *must* a priori
have a good reason to believe that the underlying probability
distribution's standard deviation provides useful information about the
actual distribution.

4. Any probability distribution can be made to have any standard
deviation without changing its distribution shape (technically, at most
a linear transformation of the coordinate is needed). To pick a random
example, for a highly bimodal distribution the standard deviation
doesn't really give you any useful information about the overall shape
(and it doesn't exactly correspond between the spacing between the modes
either).

5. The standard deviation is nothing more than a root-mean-square
average. Basically, it is a very popular statistic because there is an
*extremely powerful* way to model phenomena for which the
root-mean-square average is the actual meaningful value that ties into
the model. The theory I'm talking about is used extensively in almost
every science and engineering discipline *outside of* software
engineering (and the purely digital part of hardware) [2]. Since this
modeling technique is rarely directly relevant in software, there is
little reason to assume that the standard deviation is a good statistic
(there may be a good reason to, but I haven't seen one).

Okay, so I've cast doubt on the use of standard deviation (and actually
an argument could be made against the mean too for similar reasons). On
a more constructive note, what *is* a good small set of parameters to
summarize measurements of program run time? One interesting model that
was brought to my attention by Nick Lewycky is based on the following
observation: most "perturbations" of the run time of a fixed program
binary on a fixed chip and fixed OS are due to externalities that
contend for (or otherwise deprive the program of) execution resources
and therefore almost always make it *slower*.

The model is then that there is a "true" execution time for the program,
which is a completely deterministic function of the program, chip, and
OS; intuitively, this is time occurs when the program is e.g. able to
execute by itself, on a single execution unit of the CPU, with no
interrupts knocking it off CPU, and no other threads using any of the
execution resources of this CPU. On top of this, we assume that there is
a set of perturbations that contend for execution resources and slow the
program down.

For this model, it makes sense to approximate the "true" value by the
maximum of the recorded values. If we want to use two numbers to
summarize the data, it probably makes sense to look at a way of
characterizing the extent of the variation from this "true" value. In
the absence of proper characterization of the perturbations (such as the
distribution of when they occur and how much effect they have when they
occur), one simple solution is the minimum. Max and min might be *too*
sensitive to the perturbations (especially disk cache of the program
itself on the first run), so a pair of percentiles might be a bit more
reliable, such as 10th percentile and 90th percentile.

Next time I'm measuring program run time, I'm going to try out this
model. It should be possible to look at a higher-degree-of-freedom
representation of the dataset, like a histogram of the distribution, in
order to evaluate how well this two-parameter characterization of the
data works compared with the typical mean and stddev. Also, ultimately
we only need as much precision here as is necessary for what we are
doing with the summarized data, which typically is to alert us of a
significant change. On a sufficiently quiet machine, the variance of the
measurements might be significantly smaller than the thresholds that we
consider "significant" so that the mean, median, max, min, 10%-ile etc.
are all so close that it does not matter which we use, and we can
summarize the data with just a single number, which can be any one of
them (and just trigger an error if total variation exceeds a set amount).

However, the noisier the machine (and hence our data), the more
important is is to properly model and analyze things to avoid coming to
a faulty conclusion (note: reliable conclusions can be made from data
with large amounts of noise as long as there is a good model which
allows isolating the data we are interested in from the noise [3]).

Nick, I forget from when we talked, but did you guys ever settle on a
model like this for your internal benchmarking?

We have not. Our current system (very quiesced machines) works well enough for our current purposes.

Nick

I used to subscribe to this thinking. I no longer do, and I think it has
serious problems. Let me try to explain with a hopefully entertaining
analogy. =D

Back in physics class we would analyze the behavior of basic principles of
physics as they would act upon a brave cadre of scientifically savvy
penguins sliding down slopes under the effect of gravity. Naturally, these
slopes were very slick ice, and the physics problems would be solved
"neglecting friction"[1]. That made the analysis much more tractable and
predictable. It also makes it really inaccurate in the real world.

Now, I'm not actually deriding this approach to physics. The results from
neglecting friction are *very* informative as to the basic principles at
work. In a very similar way, I don't think the model of a "true" execution
time is very informative, but much more about the principles at work than
about the actual performance of the software.

The reality I have observed these days, especially in larger software
systems, is that there is inherently no deterministic and representative
execution to rely on for finding "true" execution time. And too many of the
random fluctuations are actually by-products of the program itself. Some
examples:

- If the program runs uninterrupted on a single core, it may cause that
core to be throttled due to the thermals generated. This can have the
effect of lowering the overall performance more than what you would see if
the program ran *just* under that level without repeated drops in frequency.

- Different optimizations can change the thermal profile by using different
execution units. This is especially true for vector units.

- If you fully leverage the AVX unit of some intel chips, my understanding
is that regardless of the specific thermals, those units run at a lower
clock!

- Naturally, things like ASLR or other sources of correct but
non-deterministic layout of data in memory will cause fluctuations in the
rate at which cache misses occur. This is probably the most common cause of
non-external fluctuations in performance. Different generated code,
register spill frequencies, etc., can dramatically change these cache
collision rates.

Now, with all of these factors, we have to assume that in real world
scenarios, the programs will not always run at the maximal speed. Instead,
there is, much as you describe in your email, some distribution of
performance that will be observed in practice. The interesting question is
-- can generating different code from the compiler shift the distribution
in a meaningful way that is not correlated with the fastest measurement (or
the N fastest measurement). And I think the above examples are all places
where this can happen. =/

As a consequence, I am increasingly in favor of using very simple but
aggregated statistics for measuring performance and acknowledging the lack
of precision when it is present. We can still very often measure precisely
those localized improvements provided by a specific change even when very
small. We just won't see a reflection from them in many cases when
measuring large systems that have a high noise floor. Hopefully, we can at
least observe good trends in the large system to confirm we are making
progress, and at times we can make large improvements that actually rise
above the noise floor.

Anyways, my somewhat rambling 2 cents on this. Sorry for the not entirely
structured email. =]

In the context I think we’re most interested in, we’re trying to measure the quality of the code being generated. As you mention here, it is very target-dependent and depends largely on instruction set features, cache sizes, and many other details of the implementation.

The only way I have been able to think about getting extremely repeatable results is to run the generated code on an emulator and get a synthetic performance measurement of some kind – the number of ticks on the emulator clock or something like that. (I know with multiple threads that’s too simplistic, and it may not be the only measurement we’re interested in.)

This idea has obvious flaws – emulation is usually slow, and the accuracy of the emulation is critical if you want the numbers to mean something in the real world, and you’d have to have good emulators for all of the targets you really care about. I googled a bit and couldn’t find very much where people are using emulators for performance testing so maybe this idea just doesn’t work.

For one code generator I have worked on we ended up doing our own synthetic performance analysis based on analysis of the IR that we have at the backend, and then instrumenting our output so that at runtime we accumulate the “cost” of the code as it executes. It has flaws but it’s repeatable and your test harness can say “this code looks worse – someone needs to look at it.”

Otherwise I agree that you need a good statistical model and a very controlled hardware/software environment to do meaningful performance testing. It’s a hard problem.

Okay, so I've cast doubt on the use of standard deviation (and actually
an argument could be made against the mean too for similar reasons). On a
more constructive note, what *is* a good small set of parameters to
summarize measurements of program run time? One interesting model that was
brought to my attention by Nick Lewycky is based on the following
observation: most "perturbations" of the run time of a fixed program binary
on a fixed chip and fixed OS are due to externalities that contend for (or
otherwise deprive the program of) execution resources and therefore almost
always make it *slower*.

The model is then that there is a "true" execution time for the program,
which is a completely deterministic function of the program, chip, and OS;
intuitively, this is time occurs when the program is e.g. able to execute
by itself, on a single execution unit of the CPU, with no interrupts
knocking it off CPU, and no other threads using any of the execution
resources of this CPU. On top of this, we assume that there is a set of
perturbations that contend for execution resources and slow the program
down.

For this model, it makes sense to approximate the "true" value by the
maximum of the recorded values. If we want to use two numbers to summarize
the data, it probably makes sense to look at a way of characterizing the
extent of the variation from this "true" value. In the absence of proper
characterization of the perturbations (such as the distribution of when
they occur and how much effect they have when they occur), one simple
solution is the minimum. Max and min might be *too* sensitive to the
perturbations (especially disk cache of the program itself on the first
run), so a pair of percentiles might be a bit more reliable, such as 10th
percentile and 90th percentile.

I used to subscribe to this thinking. I no longer do, and I think it has
serious problems. Let me try to explain with a hopefully entertaining
analogy. =D

Back in physics class we would analyze the behavior of basic principles of
physics as they would act upon a brave cadre of scientifically savvy
penguins sliding down slopes under the effect of gravity. Naturally, these
slopes were very slick ice, and the physics problems would be solved
"neglecting friction"[1]. That made the analysis much more tractable and
predictable. It also makes it really inaccurate in the real world.

Now, I'm not actually deriding this approach to physics. The results from
neglecting friction are *very* informative as to the basic principles at
work. In a very similar way, I don't think the model of a "true" execution
time is very informative, but much more about the principles at work than
about the actual performance of the software.

Absolutely. One the one hand a good model is a useful predictor of reality
when its assumptions are met. Conversely, looking at how reality deviates
from the model helps you understand the thing which is causing a deviation
from the model. The penguins will have a much easier time analyzing dirt
slopes if they already understand gravity from the frictionless slopes.

Of course, it has to actually be a *good* model for this to apply. I don't
know if what I described is a good model. However, it was the first model
for program performance that I've seen that actually seemed to be based on
a pretty sound idea: characterizing externalities as contending for (or
otherwise depriving the program of) execution resources.

The reality I have observed these days, especially in larger software
systems, is that there is inherently no deterministic and representative
execution to rely on for finding "true" execution time. And too many of the
random fluctuations are actually by-products of the program itself. Some
examples:

- If the program runs uninterrupted on a single core, it may cause that
core to be throttled due to the thermals generated. This can have the
effect of lowering the overall performance more than what you would see if
the program ran *just* under that level without repeated drops in frequency.

- Different optimizations can change the thermal profile by using
different execution units. This is especially true for vector units.

- If you fully leverage the AVX unit of some intel chips, my understanding
is that regardless of the specific thermals, those units run at a lower
clock!

- Naturally, things like ASLR or other sources of correct but
non-deterministic layout of data in memory will cause fluctuations in the
rate at which cache misses occur. This is probably the most common cause of
non-external fluctuations in performance. Different generated code,
register spill frequencies, etc., can dramatically change these cache
collision rates.

Now, with all of these factors, we have to assume that in real world
scenarios, the programs will not always run at the maximal speed. Instead,
there is, much as you describe in your email, some distribution of
performance that will be observed in practice. The interesting question is
-- can generating different code from the compiler shift the distribution
in a meaningful way that is not correlated with the fastest measurement (or
the N fastest measurement). And I think the above examples are all places
where this can happen. =/

As a consequence, I am increasingly in favor of using very simple but
aggregated statistics for measuring performance and acknowledging the lack
of precision when it is present. We can still very often measure precisely
those localized improvements provided by a specific change even when very
small. We just won't see a reflection from them in many cases when
measuring large systems that have a high noise floor. Hopefully, we can at
least observe good trends in the large system to confirm we are making
progress, and at times we can make large improvements that actually rise
above the noise floor.

If I'm understanding you correctly, you are emphasizing the need to be sure
that the holistic system we ultimately care about improving is tracking the
improvements we think we're making based on more localized measurements. I
fully agree.

-- Sean Silva

Pass ordering is a design decision, based on assumptions as to what each of the optimizations is going to do, what information it needs, what properties of the code will be created, preserved or eliminated, and so on.
What Daniel showed are the performance results from reordering passes, with keeping their current implementations. This serves more as an indicator of the potential impact of such change if it was made today. You're right that relying only on the statistics may lead to incorrect conclusions, but I don't think that there is any meaningful information that could be extracted through a more detailed analysis of the provided data. The question is really whether changing the pass order could allow the optimizations to be more aggressive or accurate (e.g. through exposing them to a more complete view of the program), and whether the benefits could be expected to exceed the costs (such as increased compilation time). Whether the current implementations of particular optimizations would immediately benefit from such a change is not really that important in the long term.

-Krzysztof