This is an archive of the discontinued LLVM Phabricator instance.

[SCEV] Simplify adds to a canonical sum of product
AbandonedPublic

Authored by sanjoy on Nov 7 2015, 12:57 PM.

Details

Summary

This change teaches SCEV to simplify add expressions that contain
multiplications to a sum of products representation, and use the
resulting simplified expression if that simplifies to a constant.

This was motivated by PR25170, but I think simplifying to a canonical
form is a good idea in general for consistency. However, I'm not a 100%
sure that this is a good idea with respect to compile time -- the core
recursive simplification logic has exponential complexity in the number
of SCEVMulExpr's it creates. Perhaps we should cap the depth (or some
other metric) to avoid the truly bad cases here?

In the future this can probably be extended to contruction of
multiplication expressions as well.

Diff Detail

Event Timeline

sanjoy updated this revision to Diff 39642.Nov 7 2015, 12:57 PM
sanjoy retitled this revision from to [SCEV] Simplify adds to a canonical sum of product.
sanjoy updated this object.
sanjoy added reviewers: hfinkel, atrick.
sanjoy added a subscriber: llvm-commits.

Note: this is an RFC patch. At the very least it needs some more tests before it can go in; but I'd like some initial feedback before spending too much time working on a specific approach.

Nice little SumOfProduct utility!

Exponential recursion needs to be prevented. Even without the simplification step, the visitor is exponential. Unlike normal SCEV construction, the recursive visitor is not memoizing results.

I guess the simplification is inherently exponential. I don't know how to avoid that without limitting size of the product. It does look like excess SCEVMulExprs may be created (for unit scale).

If you want to go forward with this, I would realy want to see motivating examples shown in the form of symbolic expressions. I couldn't understand how the utility worked without visualizing examples. It's not completely clear to my why this can't be done in place as a bottom-up SCEV canonicalization/simplification. I would need to work through some examples to understand the issue.

I guess the final question is wheter to use this as the canonical SCEV form rather an an on-the-side simplification. I would be fine with that if the Expander knew how to minimize multiplication by factoring terms.

Adding Michael Z. since I can't really do timely SCEV reviews.

lib/Analysis/ScalarEvolution.cpp
1848

Did you consider SmallPtrSet?

sanjoy abandoned this revision.Dec 27 2015, 8:29 PM

Given that Hal has fixed the issue in the PPC backend a while back, I think checking this in as-is is not the best way forward, so abandoning this revision for now. I agree a better idea is to have a documented-as-exponential ScalarEvolution::ComputeSumOfProducsForm helper (which may be useful to consider later once we have more use cases).