You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
JIT should optimize {array | span}.Length division by constant that is power of 2 #59922
It was somewhat trivial for arrays, it is not so trivial for spans, unfortunately, the reason being that most often the span's length is just a GT_LCL_VAR. Perhaps we can mark a local produced for the length specially when promoting the span.
How many other optimizations are there that currently apply only to arrays but could also be relevant to spans? It'd be great to drive that list to zero and then ensure no additional ones creep in. More and more code is being converted from arrays to spans, and we want to avoid unnecessary regressions.
Reacted by Günther Foidl, Miha Zupan, Anton Firszov, hopperpl and GSPP
There are certainly some optimizations that do not benefit spans equally.
The most obvious example is VN, which can "see" through assignments to array elements (because there is intrinsic understanding of array indexing similar to that of fields built into the compiler):
privatestaticintRedundantBranchSpan(Span<int>a){a[1]=0;if(a[1]is0){return1;}a[2]=1;if(a[1]is0)// The compiler will not see this branch as redundant, there was a write to memory.{return1;}return2;}privatestaticintRedundantBranchArray(int[]a){a[1]=0;if(a[1]is0){return1;}a[2]=1;if(a[1]is0)// The compiler will see this branch as redundant since it knows a[1] and a[2] don't interfere{return1;}return2;}// VNs are also used for CSE purposes, so redundant loads may not be optimized as well.privatestaticintSpanDoubleLoad(Span<int>a){intsum=0;sum+=a[1];a[2]=1;sum+=a[1];// This will be a loadreturnsum;}privatestaticintArrayDoubleLoad(int[]a){intsum=0;sum+=a[1];a[2]=1;sum+=a[1];// a[1] will be CSEed and used from a registerreturnsum;}
It is hard for me to say which other optimizations are affected (and how), and how impactful the above is in real-world code, or how hard would the fix be (I suspect though that it will not be so trivial, because the existing mechanism already has some unsightly maintainability problems).
Activity
EgorBo commentedon Oct 4, 2021
heh, I did for arrays (GT_ARRLEN) #32368 🙂
gfoidl commentedon Oct 4, 2021
Perfect 👍🏻, so the step to enable this for Span too isn't that big (hopefully).
SingleAccretion commentedon Oct 4, 2021
It was somewhat trivial for arrays, it is not so trivial for spans, unfortunately, the reason being that most often the span's length is just a
GT_LCL_VAR. Perhaps we can mark a local produced for the length specially when promoting the span.EgorBo commentedon Oct 4, 2021
I made a quick fix and checking diffs now
stephentoub commentedon Oct 4, 2021
How many other optimizations are there that currently apply only to arrays but could also be relevant to spans? It'd be great to drive that list to zero and then ensure no additional ones creep in. More and more code is being converted from arrays to spans, and we want to avoid unnecessary regressions.
SingleAccretion commentedon Oct 4, 2021
There are certainly some optimizations that do not benefit spans equally.
The most obvious example is VN, which can "see" through assignments to array elements (because there is intrinsic understanding of array indexing similar to that of fields built into the compiler):
It is hard for me to say which other optimizations are affected (and how), and how impactful the above is in real-world code, or how hard would the fix be (I suspect though that it will not be so trivial, because the existing mechanism already has some unsightly maintainability problems).
JulieLeeMSFT commentedon Oct 4, 2021
cc @jakobbotsch for VN on span.
cc @dotnet/jit-contrib