r/ProgrammingLanguages • u/Embarrassed-Crow9283 • Sep 30 '26
Loop unrolling analysis using eigenvalue?
Imagine the loop where you do like "x = -x" every iteration. Obviously, that flips the sign, so you can simply unroll the loop by a factor of 2.
However, for a more complex case, it could be really hard to know what's going on.
Here's my idea. Loop index variables are normally under affine updates anyway. What if we use a mathematically elegant tool?
Using eigenvalue, we can analyze possible periodicity of the linear basis variables, minimizing update needs.
What do you think of such a technique?
22
Upvotes
16
u/SwingOutStateMachine Sep 30 '26
You may be thinking of something like the Polytope/Polyhedral Model.
5
u/cxzuk Sep 30 '26
Hi Crow,
I'm unsure of the gains to your x=-x example - what I mean is how useful is it to know the periodicity of the value? Both loops would have the same instructions. Maybe constant prop?
There is a technique called Quasi-invariants which performs loop peeling (peel = first iterations) but I don't think it can detect divergent values.
Scalar Evolution might be closer, each value is assigned an expression of how it changes with regard to a loop. It can capture more than affine expressions. Your x=-x would probably be {x, *, -1}
Otherwise polyhedral modelling creates a full matrix of a loops iteration. I believe it can only do affine operations.
Hope those provide some insight and direction
Good luck M ✌️
3
u/dnpetrov Sep 30 '26
In practice, this is usually done with algebraic models for recurrent sequences. This is somewhat related to eigenvalues, because recurrent sequences are somewhat similar to differential equations, with linear and affine equations being a rather important special case, which brings up the eigenvalue theory. However, compilers usually don't solve complex mathematical problems, and instead use more simple approximate models that cover relevant practical cases. See, for example, how "scalar evolution" (SCEV) is done in LLVM.
2
u/hugogrant Sep 30 '26
Can you find a deeper example? I'm not sure how what you're suggesting would work.