Hello, On Wed, 8 Sep 2021, Aldy Hernandez wrote:
> > The forward threader guards against this by simply disallowing > > threadings that involve different loops. As I see > > The thread in question (5->9->3) is all within the same outer loop, > though. BTW, the backward threader also disallows threading across > different loops (see path_crosses_loops variable). > > > the threading done here should be 7->3 (outer loop entry) to bb 8 > > rather than one involving the backedge. Also note the condition is > > invariant in the loop and in fact subsumed by the condition outside of > > the loop and it should have been simplified by VRP after pass_ch but I > > see there's a jump threading pass inbetween pass_ch and the next VRP > > which is likely the problem. > > A 7->3->8 thread would cross loops though, because 7 is outside the > outer loop. ... > However, even if there are alternate ways of threading this IL, > something like 5->9->3 could still happen. We need a way to disallow > this. I'm having a hard time determining the hammer for this. I would > vote for disabling threading through latches, but it seems the backward > threader is aware of this scenario and allows it anyhow (see > threaded_through_latch variable). Ughh. The backward threader seems to want to be careful with latches, but still allow it in some situations, in particular when doing so doesn't create a loop with non-simple latches (which is basically a single and empty latch block). If this improvement under discussion leads to creating a non-empty latch then those checks aren't restrictive enough (anymore). I think threading through a latch is always dubious regarding the loop structure, it's basically either loop rotation or iteration peeling, even if it doesn't cause non-simple latches. Those transformations should probably be left to a loop optimizer, or be only done when destroying loop structure is fine (i.e. late). Maybe it's possible to not disable threading over latches alltogether in the backward threader (like it's tried now), but I haven't looked at the specific situation here in depth, so take my view only as opinion from a large distance :-) Does anything break if you brutally disable any backward threading when any of the involved blocks is a latch when current_loops is set? (I guess for that latter test to be effective you want to disable the loop_optimizer_init() for the "late" jump thread passes) Ciao, Michael.