Question about Traversing Loops in forward or reverse program order on new pass manager

Hi All,

I have seen performance regressions from new pass manager against legacy pass manager. One of the issues is the order of populating loops on pass manager. The legacy pass manager is traversing loops in reverse program order but the new pass manager is traversing in forward program order. It sometimes causes different output. I have created a review https://reviews.llvm.org/D99774 for reverse program order on new pass manager and had short discussion with Arthur on it. It is not easy to say the reverse order is better than the forward one or vice versa. I would like to share this issue with more people on llvm-dev. If you have idea or experience about this issue, please share it.

Thanks

JinGu Kang

Ping on loop traversal order. Does anybody have any intuition about which way to traverse loops in a function?

I think the traversal order may depend on the optimization. I would actually propose to get rid of those fine-grained pass managers altogether. I think they are abstractions taken too far, to the point of being counter-productive. Instead, loop passes should visit a function at a time, and use utility functions (like iterators of various kinds) to visit loops in the order they want.

On the actual issue here, I think we should stick to the old traversal order for the time being. This would reduce the potential for regressions and make it easier to fully transition to the NPM for everyone.

I think the traversal order may depend on the optimization. I would actually propose to get rid of those fine-grained pass managers altogether. I think they are abstractions taken too far, to the point of being counter-productive. Instead, loop passes should visit a function at a time, and use utility functions (like iterators of various kinds) to visit loops in the order they want.

JFYI, there are very good compile time reasons to visit each loop with all passes. I don't remember the exact numbers, but I remember last time this was assessed that locality made a very measurable impact. Particularly for large machine generated codebases.

If we do have loop transforms which prefer both, we could do two pass approach. We have something analogous to this for inferring attributes in IPO.

I know of no reason to have a strong preference. Assuming the change wasn’t made intentionally - might be good to check with Chandler - I’d default to using the same order from the old pass manager.

One argument in favor of the forward order is that if determine a loop exit is dead, we might be able to avoid visiting a large portion of the function. Just an observation, not an argument in favor per se.

Philip

Do you remember what the locality was of that was the factor?

Right now we’re running a loop pass on each loop, which isn’t very different from running a function pass on each loop in a function, so in that sense we keep the “same code different data” format. Was it the loop structure that was only computed once-ish that made the difference?

My concern is that there are multiple traversal dimensions, not only forward/backward, but also “innerward”/”outerward”, plus optimizing one loop may affect the next one. I think that more “holistic” passes would provide better flexibility.

I meant locality in the memory access patterns. If you have a very large function (e.g. machine generated), you’ll get much better memory locality by visiting one loop for all passes instead of for each pass visiting each loop. Think cache working sets.

However, the more I think about this, I’m now wondering if I was mixing the IPO case with the loop case. I know IPO matters in practice. I’m not sure the loop case does. I might be misremembering. Should be pretty easy to test if you’re interested though.

Philip

Hi All,

How do you think about new pass manager allows each pass to choose the order of loops on new pass manager?

At this moment, some function passes are using appendLoopsToWorklist to iterate loops in forward program order. We could provide appendLoopsToWorklistInReverse or something like that to iterate loops in reverse program order with keeping one way as default on new pass manager.

Thanks

JinGu Kang