On 10/27/26 4:23 AM, Richard Biener wrote:
On Tue, Oct 27, 2026 at 10:04 AM Sendao Yan <[email protected]> wrote:
The backward threader only exempts a thread from the
max-jump-thread-duplication-stmts limit when the path goes through the
loop latch _and_ the threaded branch is a multi-way branch.  The
multi-way test looks at the branch statement of the block the path
starts in, so it does not recognize a finite state machine whose state
dispatch has already been lowered into a chain of conditional branches.
Such a path is rejected even though it is exactly the case the
exemption was written for.

This can be seen on a byte-at-a-time loop with a four-state machine
(a reduction of binascii_a2b_base64 from CPython):

   - GCC 11 replicates the loop body and constant-folds the state switch
     away, 2.23 cycles/byte;
   - GCC 12 and later keep the four-way dispatch on every iteration,
     4.10 cycles/byte on trunk;
   - building trunk with --param=fsm-scale-path-stmts=1 accepts the path
     again and gives 2.10 cycles/byte, i.e. the duplication budget is
     what blocks the thread.

A path that crosses the loop latch is a finite state machine path
whether or not the branch statement happens to be a GIMPLE_SWITCH at
the point the threader runs, so use that as the condition instead of
the multi-way branch test.  The path is still bounded by
max-fsm-thread-path-insns, and for -Os/-Oz the m_speed_p check in
profitable_path_p still rejects any path with net_insns() > 1.
The intent is to catch a threading path that goes through the same
branching sequence.  I'm not sure that fully relaxing this to consider
all threads through latches is capturing this.  For example if the thread
ends in the loop exit test or starts at a not control dependent block
and the test is a simple diamond, does that still warrant using the higher
limit?
Right.  We wanted to capture the case where the state on one iteration of the loop tells us where the next iteration of the loop would go *and* the cost of of the jump we thread is likely extraordinarily high -- ie, an indirect jump.  This optimization tends to destroy loop structure and that's not something to take lightly.  So if the indirect has already been lowered to a decision tree, then it wouldn't quality.

We had a bit of threading across loop edges in DOM, but we eventually ripped it all out.  My recollection was managing the equivalences safely got to be too painful and would require the ability to do invalidation rather than just unwinding a stack.


I wonder if a more appropriate fix is to move lowering switches to
after the first FSM threading opportunity.
That might be a better solution.  Certainly worth exploring IMHO.

jeff

Reply via email to