Consider avoiding irreducible loops in async codegen #67774
Labels
Area-Compilers
Code Gen Quality
Room for improvement in the quality of the compiler's generated code
Resolution-Duplicate
The described behavior is tracked in another issue
Whenever there is an
await
inside of a loop (see example below), the resulting IL will have a loop with two ways to enter the loop: 1. from the start, 2. after resumption of theawait
. This causes problems for the JIT.Normally, the JIT can pull some common logic out of loops ("loop invariant code motion") and perform some optimizations with variables that it can prove progress a certain way ("induction variable analysis"). But with irreducible loops (loops with multiple entry points) it cannot perform those optimizations.
From discussion with @AndyAyersMS (as part of his investigation work), a few options came up:
I want to document proposal 4 in more details. We would want to validate the expected benefits (benchmarks of run time) and measure the downside (slightly inflated IL). Some kind of prototype could be useful to explore this further.
Proposed codegen change
TL;DR: when resuming after an
await
, you'd jump to the start of the loop logic, enter normally, and then have another switch just inside the loop that jumps to the right resumption point.Background
When the compiler lowers an
await
, theawait
itself results in a little block like:Then at the top of the method we'll also add a switch dispatch:
But when the
await
is inside of atry
this switch dispatch isn't allowed (can't jump into the middle of atry
).To deal with this, the compiler will produce two switch dispatches: one at the top of the method and another one at the top of the
try
block.This ensures that we only enter
try
blocks from the start.You can find more background details including examples here.
Proposal for loops
We could use a similar lowering strategy for loops, where we'd intentionally avoid jumping into the middle of a loop with our switch dispatches.
This means that loops would only be entered through the start.
Example
The text was updated successfully, but these errors were encountered: