[llvm] [SCEV] Introduce SCEVUse, use it instead of const SCEV * (NFCI) (WIP). (PR #91961)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 4 07:59:50 PDT 2024


fhahn wrote:

> I assume that this patch is supposed to just mechanical changes for SCEVUse introduction -- otherwise this would need more adjustments, e.g. the poison analysis code would need to account for these flags, and these are proper poison flags rather than UB flags.
> 

Yes exactly. The first real use would be in https://github.com/llvm/llvm-project/pull/91964

> I think my main high level question here is: What about SCEV unification? We get this straightforwardly for the top-level use flags because we can just compare the underlying pointers. But what about nested expressions?
> 
> If we have `((%a * %b)(u nuw) + %c)` and `((%a * %b) + %c)`, I assume we're actually going to treat these as two distinct, non-unified expressions. We currently rely on equality for SCEVs to be pointer equality, and this would somewhat break that. Do we need to have each SCEV also store a pointer to the unified SCEV without flags or something?

I've update the patch to make a distinction between canonical (no flags in any subexpression) and non-canonical. Only canonical SCEVs can be compared with `==`, with an assertion checking that. A canonical SCEV can be constructed by rebuilding the SCEV expression with all flags dropped. One of the bits in SCEVUse is used to indicate that the wrapped SCEV is canonical or not. So far it is not complete yet, and only supports expressions with flags in Add and Mul expressions.

This unfortunately about doubles the compile-time impact, worst increase is +0.18% for stage2-O3 ( https://llvm-compile-time-tracker.com/compare.php?from=9e831d50a0d67c6779048742657edb437818b681&to=d1a824d7f47b90faa4d708b543b78d28b761b18b&stat=instructions:u) 

I didn't spend any time on tuning yet, but there are likely more places that will need updating as we make use of more SCEVUses.

I added some basic tests for comparing SCEVs with and without flags here https://github.com/llvm/llvm-project/pull/91961/files#diff-218646cbfc7bf44fcc471db9ceb892a5a69a89edfa871a459cedddc991eb67a8



Adding a pointer to each SCEV would be an alternative, I originally thought tracking whether a SCEV is canonical using a bit in the pointer may be less overhead for code that doesn't use any flags, but I'll prototype it to see how it compares



https://github.com/llvm/llvm-project/pull/91961


More information about the llvm-commits mailing list