Proposal for O1/Og Optimization and Code Generation Pipeline

Hi All,

I’ve been thinking about both O1 and Og optimization levels and have a
proposal for an improved O1 that I think overlaps in functionality
with our desires for Og. The design goal is to rewrite the O1
optimization and code generation pipeline to include the set of
optimizations that minimizes build and test time while retaining our
ability to debug.

This isn’t to minimize efforts around optimized debugging or negate O0
builds, but rather to provide a compromise mode that encompasses some
of the benefits of both. In effect to create a “build mode for
everyday development”.

This proposal is a first approximation guess on direction. I’ll be
exploring different options and combinations, but I think this is a
good place to start for discussion. Unless there are serious
objections to the general direction I’d like to get started so we can
explore and look at the code as it comes through review.

Optimization and Code Generation Pipeline

The optimization passes chosen fall into a few main categories,
redundancy elimination and basic optimization/abstraction elimination.
The idea is that these are going to be the optimizations that a
programmer would expect to happen without affecting debugging. This
means not eliminating redundant calls or non-redundant loads as those
could fail in different ways and locations while executing. These
optimizations will also reduce the overall amount of code going to the
code generator helping both linker input size and code generation
speed.

Dead code elimination

- Dead code elimination (ADCE, BDCE)
- Dead store elimination
- Parts of CFG Simplification
- Removing branches and dead code paths and not including commoning
and speculation

Basic Scalar Optimizations

- Constant propagation including SCCP and IPCP
- Constant merging
- Instruction Combining
- Inlining: always_inline and normal inlining passes
- Memory to register promotion
- CSE of “unobservable” operations
- Reassociation of expressions
- Global optimizations - try to fold globals to constants

Loop Optimizations

Loop optimizations have some problems around debuggability and
observability, but a suggested set of passes would include
optimizations that remove abstractions and not ones that necessarily
optimize for performance.

- Induction Variable Simplification
- LICM but not promotion
- Trivial Unswitching
- Loop rotation
- Full loop unrolling
- Loop deletion

Pass Structure

Overall pass ordering will look similar to the existing pass layout in
llvm with passes added or subtracted for O1 rather than a new pass
ordering. The motivation here is to make the overall proposal easier
to understand initially upstream while also maintaining existing pass
pipeline synergies between passes.

Instruction selection

We will use the fast instruction selector (where it exists) for three reasons:
- Significantly faster code generation than llvm’s dag based
instruction selection
- Better debugability than selection dag - fewer instructions moved around
- Fast instruction selection has been optimized somewhat and
shouldn’t be an outrageous penalty on most architectures

Register allocation

The fast register allocator should be used for compilation speed.

Thoughts?

Thanks!

-eric

I don’t think this is reasonable for anything claiming to have any level of optimization enabled. I would assume basic would be used for this

-Matt

Hi All,

I’ve been thinking about both O1 and Og optimization levels and have a
proposal for an improved O1 that I think overlaps in functionality
with our desires for Og. The design goal is to rewrite the O1
optimization and code generation pipeline to include the set of
optimizations that minimizes build and test time while retaining our
ability to debug.

+1

The fact that our O1 and O2 pipelines are so similar has generally
suggested to me that our O1 pipeline probably does too much.

This isn’t to minimize efforts around optimized debugging or negate O0
builds, but rather to provide a compromise mode that encompasses some
of the benefits of both. In effect to create a “build mode for
everyday development”.

This proposal is a first approximation guess on direction. I’ll be
exploring different options and combinations, but I think this is a
good place to start for discussion. Unless there are serious
objections to the general direction I’d like to get started so we can
explore and look at the code as it comes through review.

Optimization and Code Generation Pipeline

The optimization passes chosen fall into a few main categories,
redundancy elimination and basic optimization/abstraction elimination.
The idea is that these are going to be the optimizations that a
programmer would expect to happen without affecting debugging. This
means not eliminating redundant calls or non-redundant loads as those
could fail in different ways and locations while executing. These
optimizations will also reduce the overall amount of code going to the
code generator helping both linker input size and code generation
speed.

Dead code elimination

- Dead code elimination (ADCE, BDCE)

Regarding BDCE: The trivialized values might indeed be irrelevant to
later calculations, but might harm the debugging experience? If BDCE
only was applied at O2 and higher, that's likely not a huge loss.
Regular DCE (meaning without the bit-tracking parts) is probably fine
for O1.

- Dead store elimination
- Parts of CFG Simplification
- Removing branches and dead code paths and not including commoning
and speculation

Basic Scalar Optimizations

- Constant propagation including SCCP and IPCP
- Constant merging
- Instruction Combining
- Inlining: always_inline and normal inlining passes
- Memory to register promotion
- CSE of “unobservable” operations
- Reassociation of expressions
- Global optimizations - try to fold globals to constants

Loop Optimizations

Loop optimizations have some problems around debuggability and
observability, but a suggested set of passes would include
optimizations that remove abstractions and not ones that necessarily
optimize for performance.

- Induction Variable Simplification
- LICM but not promotion
- Trivial Unswitching
- Loop rotation
- Full loop unrolling
- Loop deletion

Pass Structure

Overall pass ordering will look similar to the existing pass layout in
llvm with passes added or subtracted for O1 rather than a new pass
ordering. The motivation here is to make the overall proposal easier
to understand initially upstream while also maintaining existing pass
pipeline synergies between passes.

Instruction selection

We will use the fast instruction selector (where it exists) for three reasons:
- Significantly faster code generation than llvm’s dag based
instruction selection
- Better debugability than selection dag - fewer instructions moved around
- Fast instruction selection has been optimized somewhat and
shouldn’t be an outrageous penalty on most architectures

Register allocation

The fast register allocator should be used for compilation speed.

I'm not sure about this - we should understand the performance impact -
it might be severe.

Thanks again,

Hal

  • Dead code elimination (ADCE, BDCE)

Regarding BDCE: The trivialized values might indeed be irrelevant to
later calculations, but might harm the debugging experience? If BDCE
only was applied at O2 and higher, that’s likely not a huge loss.
Regular DCE (meaning without the bit-tracking parts) is probably fine
for O1.

Probably not. I’ll see what the impact is for sure.

Register allocation

The fast register allocator should be used for compilation speed.

I’m not sure about this - we should understand the performance impact -
it might be severe.

Totally agree. I think evaluating the tradeoffs is going to be key. I also have really no strong opinions and am happy to go where the data takes us.

Thanks :slight_smile:

-eric

Hi Eric,

Hi All,

I’ve been thinking about both O1 and Og optimization levels and have a
proposal for an improved O1 that I think overlaps in functionality
with our desires for Og. The design goal is to rewrite the O1
optimization and code generation pipeline to include the set of
optimizations that minimizes build and test time while retaining our
ability to debug.

That would be nice: how do you distinguish O1 and Og with this view? (which from your list would / wouldn’t be included in Og?)

This isn’t to minimize efforts around optimized debugging or negate O0
builds, but rather to provide a compromise mode that encompasses some
of the benefits of both. In effect to create a “build mode for
everyday development”.

This proposal is a first approximation guess on direction. I’ll be
exploring different options and combinations, but I think this is a
good place to start for discussion. Unless there are serious
objections to the general direction I’d like to get started so we can
explore and look at the code as it comes through review.

Optimization and Code Generation Pipeline

The optimization passes chosen fall into a few main categories,
redundancy elimination and basic optimization/abstraction elimination.
The idea is that these are going to be the optimizations that a
programmer would expect to happen without affecting debugging. This
means not eliminating redundant calls or non-redundant loads as those
could fail in different ways and locations while executing. These
optimizations will also reduce the overall amount of code going to the
code generator helping both linker input size and code generation
speed.

Dead code elimination

  • Dead code elimination (ADCE, BDCE)
  • Dead store elimination
  • Parts of CFG Simplification
  • Removing branches and dead code paths and not including commoning
    and speculation

Basic Scalar Optimizations

  • Constant propagation including SCCP and IPCP
  • Constant merging
  • Instruction Combining
  • Inlining: always_inline and normal inlining passes
  • Memory to register promotion
  • CSE of “unobservable” operations
  • Reassociation of expressions
  • Global optimizations - try to fold globals to constants

Loop Optimizations

Loop optimizations have some problems around debuggability and
observability, but a suggested set of passes would include
optimizations that remove abstractions and not ones that necessarily
optimize for performance.

  • Induction Variable Simplification
  • LICM but not promotion
  • Trivial Unswitching
  • Loop rotation
  • Full loop unrolling
  • Loop deletion

That is already a pretty good list. I would find interesting if we know the opposite list: the passes that we should not include for speed and debugaibility? Vectorizer? Unrolling? Jump Threading?
Also couldn’t constant propagation and reassociation which are in your list hurt debugability?

Thanks!

I like the general direction.
Which seems in line with what’s written already at https://github.com/llvm/llvm-project/blob/master/llvm/include/llvm/Passes/PassBuilder.h#L116 (which is the closest to a written down statement that I know of for what our optimization levels should aim for).

I think that -O1 and -Og in a perfect world are different optimization levels (aiming for slightly different goals); but also agree that improving the -O1 implementation to get closer to its described goal will also mostly help in making it a reasonable baseline for an -Og optimization level if we decided to introduce that.

I agree that we should aim to use data to make tradeoffs.
It’s probably relatively easy to collect data on “compile time” and “execution time” metrics; but probably harder on “debuggability”. I wonder if the bot Adrian pointed to in the Debug Info BoF (this one? http://lnt.llvm.org/db_default/v4/nts/124231) at last LLVM dev meeting could help provide data for that. Or are those metrics too far off from “debuggability as perceived by a human developer”?

Thanks,

Kristof

I'm definitely in favour of pursuing a meaningful -Og, it would be
especially welcome in the embedded area. One possibility that may be
worth exploring is to outline the debug illusion that we wish to
achieve at -Og and permit all optimizations that preserve that
illusion yet disable the ones that do not. Arm's old proprietary
compiler armcc defined its optimization levels in this way, with the
manual having high-level description:
http://infocenter.arm.com/help/topic/com.arm.doc.dui0472m/chr1359124935804.html
with -O1 in armcc roughly equivalent to -Og . Of course actually
achieving this and testing for it is more difficult than writing it
down, but I think it did provide a nice framework to think about what
to include. I don't know enough about debug in LLVM to know how
practical this will be.

Peter

Thanks for posting this. I’m absolutely of the opinion that current -O1 is almost a “worst of all worlds” optimization level, where the performance of the generated code isn’t good enough to be particularly useful (for our users at least) but the debug experience is already getting close to being as bad as -O2/3, so I’m personally very happy with your direction of redefining -O1 (especially as that could then open up the way to future enhancements like using PGO data to let us compile everything at -O1 for the build time performance win, except for the critical hot functions that get the full -O2/3 pipeline for the run time performance win).

How will this optimization level interact with LTO (specifically ThinLTO)? Would -O1 -flto=thin to run through a different, faster LTO pipeline or are we expecting that any everyday development build configuration won’t include LTO?

I’m a little bit more on the fence with what this would mean for -Og, as I’d really like to try and come to some sort of community consensus on exactly what -Og should mean and what its aims should be. If you happen to be at EuroLLVM this year then that would be absolutely perfect timing as I’d already submitted a round table topic to try and start just that process [ http://llvm.org/devmtg/2019-04/#rounds ]. My team’s main focus right now is in trying to fix as many -O2 debug experience issues as possible, with the hope that we could consider using an -Og mode to mop up what’s left, but we’ve been surveying our users for a few years now about what they’d find useful in such an optimization level.

The general consensus is that performance must not be significantly worse than -O2. We’ve heard a few numbers thrown around like 5-10% runtime slowdown compared to -O2 being the absolute maximum acceptable level of intrusion for them to consider using such a mode. I’m not really sure how realistic that is and I’m inclined to think that we could probably stretch that limit a little bit here and there if the debugging experience really was that much better, but I think it gives a good indication of at least what our users are looking for. Essentially -O2 but with as few changes as we can get away with making to make the debugging experience better. I know that this is somewhat woolly, so it might be that your proposed pipeline is the closest we can get that matches such an aim, but once we’ve decided what -Og should mean, I’d like to try and justify any changes with some real data. I’m willing for my team to contribute as much data as we can. We’ve also been using dexter [ http://llvm.org/devmtg/2018-04/slides/Bedwell-Measuring_the_User_Debugging_Experience.pdf ] to target our -O2 debugging improvement work, but hopefully it will be useful to provide another datapoint for the effects on the debugging experience of disabling specific passes.

In my mind, -Og probably would incorporate a few things:

  • Tweak certain pass behaviors in order to be more favorable towards debugging [ https://reviews.llvm.org/D59431#1437716 ]

  • Enable features favorable to debugging [ http://llvm.org/devmtg/2017-10/#lightning8 ]

  • Disable whole passes that are known to fundamentally harm the debugging experience if there is no other alternative approach (this proposal?)

  • Still give a decent debug experience when used in conjunction with LTO.

Thanks again for writing up your proposal. I’m really happy to see movement in this area!

-Greg

Awesome start.

Back when I did a similar project at HP/NonStop, the class of optimizations we turned off for our O1 (Og equivalent) tended to be those that reordered code or otherwise messed with the CFG. In fact one of our metrics was:

  • The set of breakpoint locations available at Og should be the same as those available at O0.

This is pretty easy to measure. It can mean either turning off optimizations or doing a better job with the line table; either way you get the preferred user experience. Not saying Clang has to use the “must be the same” criterion, but being able to measure this will be extremely helpful. Comparing the metric with/without a given pass will give us a good idea of how much that pass damages the single-stepping experience, and gives us hard data to decide whether certain passes should stay or go.

I don’t remember whether HP/NonStop turned off constant/value propagation, but I think we did, because that can have a really bad effect on availability of variables. Now, if we’re more industrious about generating DIExpressions to recover values that get optimized away, that’s probably good enough, as usually you want to be looking at things and not so much modifying things during a debugging session.

As for Sony’s users in particular, working in a real-time environment does constrain how much performance we can give away for other benefits like good debugging. I think we’ll have to see how that falls out.

–paulr

Eric Christopher via llvm-dev <llvm-dev@lists.llvm.org> writes:

- Inlining: always_inline and normal inlining passes

This gives me some pause. Many a time I've been in gdb, tried to
execute a method and been told "sorry, can't find this, maybe it was
inlined."

That said, inlining is absolutely crucial for any kind of performance
with C++, so maybe this is an ok tradeoff. Just wanted to raise the
point.

                      -David

A helpful intermediate for -Og would be to do the inlining anyways, but avoid actually deleting the now-unused functions.

Nice to have metrics - so thanks for mentioning that, even if it doesn’t end up being suitable, it’s certainly worth looking at.

Did you do anything similar for the values of variables? I could imagine “printing the value of a variable” (not necessarily being able to modify it) at all those locations should render the same value (not undefined).

& to me, that’s actually where I would’ve guessed -Og (which might be a better discussion for a separate thread, to be honest - as much as it was brought up in the subject of this thread) would diverge from -O1. Doing things like “leaking the value of any variable at the end of its scope” to avoid dead store/unused value elimination (“oh, we saw the last use of this variable half way through the function, so we reused its register for something else later on”) - and that’s a case where that behavior can’t really (that I can think of) be justified to be unconditional at -O1 (because it pessimizes the code in a way that /only/ gives improvements to a debugger, really) - though I’m happy to be wrong/hear other opinions on that.

So my model is more “-Og would be an even more pessimized -O1” (or potentially -Og isn’t really an optimization level, but an orthogonal setting to optimization that does things like actively pessimize certain features to make them more debuggable somewhat independently of what optimizations are used - sort of like the sanitizers) but perhaps that’s inconsistent with what other folks have in mind.

  • Dave

Did you do anything similar for the values of variables? I could imagine “printing the value of a variable” (not necessarily being able to modify it) at all those locations should render the same value (not undefined).

Oh yes! We also had a criterion that the set of available variables at each breakpoint would be the same. (I don’t think we did a runtime analysis to verify the actual values were all the same, the tool I remember was a dumper sort of thing that read the binaries.) This one was mildly tricky, as –O0 tends to report locals using single-locations for a stack slot and not use ranges; I don’t remember what we did about that. Possibly looked at disassembly, and identified the first assignment to each variable? Thus constraining the “true” –O0 available range. Sorry for being fuzzy on this, it was over a decade ago and I didn’t write the tool myself.

So my model is more “-Og would be an even more pessimized -O1” (or potentially -Og isn’t really an optimization level, but an orthogonal setting to optimization that does things like actively pessimize certain features to make them more debuggable somewhat independently of what optimizations are used - sort of like the sanitizers) but perhaps that’s inconsistent with what other folks have in mind.

Distinguishing –O1 from –Og does enable that sort of thing, although you can also have pessimizations under separate flags. For example the “fake use” pessimization; Wolfgang Pieb did a lightning talk at the US 2017 dev meeting on this, his slides say 5-7% performance hit. Our users have come to like it.

–paulr

Hi All,

Looks like people are generally supportive of the changes to O1 even
if there's some discussion around O1/Og equivalence that I'll take to
a separate set of replies. Mostly so that I can get O1 changes
separate from "new Og pipeline" changes even if I think that's the
direction we should ultimately go :slight_smile:

In general though I wanted to give a heads up as to the direction I
was planning on taking things and seeing if anyone necessarily
disagreed with the general idea. I'll plan on working up a set of pass
pipeline patches (after some cleanup) and send out some reviews -
please let me know (privately) if you'd like to be explicitly cc'd on
any review otherwise I'll probably pick a set of people out of the cc
line here.

Thanks a lot for the responses and looking forward to getting some
changes going in this direction!

-eric

Hi All,

Looks like people are generally supportive of the changes to O1 even
if there’s some discussion around O1/Og equivalence that I’ll take to
a separate set of replies. Mostly so that I can get O1 changes
separate from “new Og pipeline” changes even if I think that’s the
direction we should ultimately go :slight_smile:

One comment I’ll make (as I’m somewhat obviously supportive having discussed this before):

I think the key thing isn’t that we necessarily leave -Og here. But that if we have a good -O1 pipeline and use that as a starting point for -Og, then we can critically evaluate whether there is something that we would like to do at -Og but would harm the goals of -O1. If so, that becomes a much more clear and precise basis for diverging. Until then, I suspect it would still be a better starting point than -O2.

Hi All,

Looks like people are generally supportive of the changes to O1 even
if there's some discussion around O1/Og equivalence that I'll take to
a separate set of replies. Mostly so that I can get O1 changes
separate from "new Og pipeline" changes even if I think that's the
direction we should ultimately go :slight_smile:

One comment I'll make (as I'm somewhat obviously supportive having discussed this before):

I think the key thing isn't that we *necessarily* leave -Og here. But that if we have a good -O1 pipeline and use that as a starting point for -Og, then we can critically evaluate whether there is something that we would like to do at -Og but would harm the goals of -O1. If so, that becomes a much more clear and precise basis for diverging. Until then, I suspect it would still be a better starting point than -O2.

Very much agreed of course. :slight_smile:

Thanks for helping clarify!

-eric

Hi Greg,

Thanks for the detailed response!

Thanks for posting this. I'm absolutely of the opinion that current -O1 is almost a "worst of all worlds" optimization level, where the performance of the generated code isn't good enough to be particularly useful (for our users at least) but the debug experience is already getting close to being as bad as -O2/3, so I'm personally very happy with your direction of redefining -O1 (especially as that could then open up the way to future enhancements like using PGO data to let us compile everything at -O1 for the build time performance win, except for the critical hot functions that get the full -O2/3 pipeline for the run time performance win).

Excellent. Thanks.

How will this optimization level interact with LTO (specifically ThinLTO)? Would -O1 -flto=thin to run through a different, faster LTO pipeline or are we expecting that any everyday development build configuration won't include LTO?

I'll be honest... I hadn't thought about it at all. I'd think that the
new O1 would supersede any O1 and so thin/normal lto invoked with O1
would give you the new pipeline.

I'm a little bit more on the fence with what this would mean for -Og, as I'd really like to try and come to some sort of community consensus on exactly what -Og should mean and what its aims should be. If you happen to be at EuroLLVM this year then that would be absolutely perfect timing as I'd already submitted a round table topic to try and start just that process [ http://llvm.org/devmtg/2019-04/#rounds ]. My team's main focus right now is in trying to fix as many -O2 debug experience issues as possible, with the hope that we could consider using an -Og mode to mop up what's left, but we've been surveying our users for a few years now about what they'd find useful in such an optimization level.

Sorry, I won't be able to make it. Conveniently I wrote up a nice
email with my general pipeline discussion. I can do more of a write-up
for debugging metrics if that will help? I've got the start of it
anyhow and since a number of people wanted to talk about it I can do
that this week if you think it will be helpful. I'd prefer to keep the
pass discussions on the mailing list as much as possible so that we
can a) keep our discussions focused, and b) include people who can't
necessarily make particular developer meetings.

The general consensus is that performance must not be significantly worse than -O2. We've heard a few numbers thrown around like 5-10% runtime slowdown compared to -O2 being the absolute maximum acceptable level of intrusion for them to consider using such a mode. I'm not really sure how realistic that is and I'm inclined to think that we could probably stretch that limit a little bit here and there if the debugging experience really was that much better, but I think it gives a good indication of at least what our users are looking for. Essentially -O2 but with as few changes as we can get away with making to make the debugging experience better. I know that this is somewhat woolly, so it might be that your proposed pipeline is the closest we can get that matches such an aim, but once we've decided what -Og should mean, I'd like to try and justify any changes with some real data. I'm willing for my team to contribute as much data as we can. We've also been using dexter [ http://llvm.org/devmtg/2018-04/slides/Bedwell-Measuring_the_User_Debugging_Experience.pdf ] to target our -O2 debugging improvement work, but hopefully it will be useful to provide another datapoint for the effects on the debugging experience of disabling specific passes.

That's a lot of "general consensus" :slight_smile:

Of course, I'm approaching it from the perspective of "faster than O0"
rather than "no slower than O2". I'm also not sure whether or not the
game community is unusual here. I also expect that we can adapt or
change existing passes in order to make them more debugging friendly
over time. For example, the differences I suggested with respect to
cfg cleanup in my proposal. I have a lot of real data I've started
collecting here and I'll work on summarizing some of that in a future
email.

Sounds like a general approach of "let's start carving blocks of
marble and then break out the sanding tools" is welcome though :slight_smile:

In my mind, -Og probably would incorporate a few things:
* Tweak certain pass behaviors in order to be more favorable towards debugging [ https://reviews.llvm.org/D59431#1437716 ]
* Enable features favorable to debugging [ http://llvm.org/devmtg/2017-10/#lightning8 ]
* Disable whole passes that are known to fundamentally harm the debugging experience if there is no other alternative approach (this proposal?)
* Still give a decent debug experience when used in conjunction with LTO.

I don't think we're substantially apart here. I'm very much personally
not a fan of extending the lifetime of local variables, but I think
it's absolutely a worthwhile discussion to have :slight_smile:

Thanks again for writing up your proposal. I'm really happy to see movement in this area!

Happy to help. This is something I've wanted to work on for years at
this point :slight_smile:

-eric

I think this is definitely something worth investigating. It'll
increase object file size, but there's probably some tweaking we can
do as well.

Thanks both of you!

-eric

Awesome start.

Back when I did a similar project at HP/NonStop, the class of optimizations we turned off for our O1 (Og equivalent) tended to be those that reordered code or otherwise messed with the CFG. In fact one of our metrics was:

- The set of breakpoint locations available at Og should be the same as those available at O0.

That's a very interesting metric and yes, should be fairly
straightforward to measure.

This is pretty easy to measure. It can mean either turning off optimizations or doing a better job with the line table; either way you get the preferred user experience. Not saying *Clang* has to use the "must be the same" criterion, but being able to measure this will be extremely helpful. Comparing the metric with/without a given pass will give us a good idea of how much that pass damages the single-stepping experience, and gives us hard data to decide whether certain passes should stay or go.

I don't remember whether HP/NonStop turned off constant/value propagation, but I *think* we did, because that can have a really bad effect on availability of variables. Now, if we're more industrious about generating DIExpressions to recover values that get optimized away, that's probably good enough, as usually you want to be looking at things and not so much modifying things during a debugging session.

That's the idea yes. :slight_smile:

As for Sony's users in particular, working in a real-time environment does constrain how much performance we can give away for other benefits like good debugging. I think we'll have to see how that falls out.

Thanks! It's definitely going to be a bit of a collaborative effort.

-eric

Hi Eric,

Hi All,

I’ve been thinking about both O1 and Og optimization levels and have a
proposal for an improved O1 that I think overlaps in functionality
with our desires for Og. The design goal is to rewrite the O1
optimization and code generation pipeline to include the set of
optimizations that minimizes build and test time while retaining our
ability to debug.

That would be nice: how do you distinguish O1 and Og with this view? (which from your list would / wouldn't be included in Og?)

As a first pass I'd just make them identical, but there's a lot of
work around statistics collecting (I'll refer you to Greg and Paul's
emails as well) before saying "this is 'done'".

This isn’t to minimize efforts around optimized debugging or negate O0
builds, but rather to provide a compromise mode that encompasses some
of the benefits of both. In effect to create a “build mode for
everyday development”.

This proposal is a first approximation guess on direction. I’ll be
exploring different options and combinations, but I think this is a
good place to start for discussion. Unless there are serious
objections to the general direction I’d like to get started so we can
explore and look at the code as it comes through review.

Optimization and Code Generation Pipeline

The optimization passes chosen fall into a few main categories,
redundancy elimination and basic optimization/abstraction elimination.
The idea is that these are going to be the optimizations that a
programmer would expect to happen without affecting debugging. This
means not eliminating redundant calls or non-redundant loads as those
could fail in different ways and locations while executing. These
optimizations will also reduce the overall amount of code going to the
code generator helping both linker input size and code generation
speed.

Dead code elimination

- Dead code elimination (ADCE, BDCE)
- Dead store elimination
- Parts of CFG Simplification
- Removing branches and dead code paths and not including commoning
and speculation

Basic Scalar Optimizations

- Constant propagation including SCCP and IPCP
- Constant merging
- Instruction Combining
- Inlining: always_inline and normal inlining passes
- Memory to register promotion
- CSE of “unobservable” operations
- Reassociation of expressions
- Global optimizations - try to fold globals to constants

Loop Optimizations

Loop optimizations have some problems around debuggability and
observability, but a suggested set of passes would include
optimizations that remove abstractions and not ones that necessarily
optimize for performance.

- Induction Variable Simplification
- LICM but not promotion
- Trivial Unswitching
- Loop rotation
- Full loop unrolling
- Loop deletion

That is already a pretty good list. I would find interesting if we know the opposite list: the passes that we should not include for speed and debugaibility? Vectorizer? Unrolling? Jump Threading?

Jump threading for sure :slight_smile:

Vectorization and the passes to get us there as well as unrolling are
likely to hurt debugging by changing induction variables and overall
stepping behavior. It's -sometimes- possible to recover, but it's
pretty difficult. Things like GVN and aggressive CSE/commoning can
really hurt in that you don't know how you got into a section of code
and from where.

Also couldn't constant propagation and reassociation which are in your list hurt debugability?

The former can affect which variables exist, and there's a little bit
of conversation in Paul's email around how you measure or deal with
it. Reassociation can affect how things move around a bit, but IMO
it's not too terrible. Measuring will help of course :slight_smile:

Thanks!

-eric