This is an archive of the discontinued LLVM Phabricator instance.

[SCEV] Improve AddRecs' range computation
ClosedPublic

Authored by aleksandr.popov on Apr 4 2023, 12:20 PM.

Details

Summary

Apply loop guards to AddRec's start in range computation for
non-self-wrapping AddRecs

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptApr 4 2023, 12:20 PM
aleksandr.popov requested review of this revision.Apr 4 2023, 12:20 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 4 2023, 12:20 PM
mkazantsev added inline comments.Apr 5 2023, 1:46 AM
llvm/lib/Analysis/ScalarEvolution.cpp
6736

Do we also need getRangeViaFactoring?

mkazantsev added inline comments.Apr 5 2023, 2:02 AM
llvm/lib/Analysis/ScalarEvolution.cpp
6711

Refactor with moving const Loop *L = AddRec->getLoop(); earlier?

llvm/test/Analysis/ScalarEvolution/decrementing_addrecs.ll
21

Remove FIXME's

nikic requested changes to this revision.Apr 5 2023, 2:08 AM

This is too expensive in terms of compile-time: http://llvm-compile-time-tracker.com/compare.php?from=712dfec1781db8aa92782b98cac5517db548b7f9&to=2a04edbcecc77f3d80597662a4ba19f521874d62&stat=instructions:u You're probably forcing an extra invalidation cycle here or something.

I'm also not sure whether the overall approach really makes sense. We shouldn't really get more information from the exit value than we already get from the addrec and the BE count. From what I can tell based on the test changes, the primary benefit here seems to be that we now take into account guard information on the addrec start value -- so a possible alternative would be to apply guards to the start value when calculating addrec ranges, or something along those lines?

This revision now requires changes to proceed.Apr 5 2023, 2:08 AM
mkazantsev added a comment.EditedApr 5 2023, 2:35 AM

Yeah. Why exactly getRangeForAffineAR could not infer the same facts? Maybe it's doing something over-conservative? I think we could do more in terms of symbolic computations and not range computations in there.

aleksandr.popov edited the summary of this revision. (Show Details)

@nikic Thanks for the advice, I've applied loop guards to the AddRec's start value in the getRangeForAffineNoSelfWrappingAR and now symbolic computations work for the example from decrementing_addrecs.ll

mkazantsev added inline comments.Apr 6 2023, 4:02 AM
llvm/test/Analysis/ScalarEvolution/decrementing_addrecs.ll
2–3

Let's have 2 run commands - with and without expensive checks, to highlight the difference.

mkazantsev added a comment.EditedApr 6 2023, 4:09 AM

Code change LG, I'll update this test with two different run commands to see what change comes from expensive sharpening and what from your patch.

Also please wait for Nikita to take a look.

LG, but let's wait few days in case if anyone else has concerns/objections.

It would also be great if we could find a way to do it w/o expensive sharpening. There are usually several ways to achieving the same effect.

@aleksandr.popov during this chill period, could you please try to construct a test that shows benefit from your patch w/o expensive sharpening?

nikic added a comment.Apr 7 2023, 2:32 AM

LG, but let's wait few days in case if anyone else has concerns/objections.

It would also be great if we could find a way to do it w/o expensive sharpening. There are usually several ways to achieving the same effect.

@aleksandr.popov during this chill period, could you please try to construct a test that shows benefit from your patch w/o expensive sharpening?

The changed code is only used by expensive sharpening, so no :)

I tried applying loop guards to other places like getRangeForAffineAR() as well, but this again had very bad compile-time impact: https://llvm-compile-time-tracker.com/compare.php?from=2caaec65c04ea7d0e9568b7895b7a46d6100cb75&to=87d1063758a038ffa7a0e358dc97c16ef6297aa3&stat=instructions:u Here's the corresponding diff: https://github.com/llvm/llvm-project/commit/87d1063758a038ffa7a0e358dc97c16ef6297aa3 I was hoping that doing this is pretty cheap, but apparently it isn't.

mkazantsev accepted this revision.EditedApr 10 2023, 2:33 AM

Ah ok, since it's only in expensive sharpening, then it should be fine.

Nikita, what's overall impact of expensive range sharpening together with this patch? It's sad for me that it is switched off by default. I'll maybe need to investigate why exactly it's so expensive and fix it.

This revision was not accepted when it landed; it landed in state Needs Review.Apr 10 2023, 2:48 AM
This revision was automatically updated to reflect the committed changes.
nikic added a comment.Apr 11 2023, 2:57 AM

Nikita, what's overall impact of expensive range sharpening together with this patch? It's sad for me that it is switched off by default. I'll maybe need to investigate why exactly it's so expensive and fix it.

Here are the compile-time results for just flipping the flag to true: http://llvm-compile-time-tracker.com/compare.php?from=b8917ac62ad49a0ce6de026c086599fc5fa35566&to=f0c3b441b6b01bb09c28c8608c5904d1910cd6d1&stat=instructions:u