-
Notifications
You must be signed in to change notification settings - Fork 4.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JIT won't CSE Math.Min with integers #101920
Comments
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
@dotnet/jit-contrib public static int Min(int a, int b)
{
return (a <= b) ? a : b;
} I am curious if we can recognize |
I don't think anything stops us from running if-conversion earlier. One strategy would be to canonicalize into @a74nh I'm curious if you have any thoughts about running if-conversion earlier. I recall you ended up moving the phase a few times when you added it. |
Jump threading should be able to clean this sort of thing up too, but likely is currently blocked by side effects. |
That looks like what we want, BB04 is now bypassed... is there something below there that doesn't get fixed? |
Would jump threading be expected to handle the real world case that @stephentoub showed above, in regex? That one has more interposing stuff. |
An earlier version of If Conversion did have it much earlier (before SSA I think). It was eventually moved so that it was directly after Optimize Bools, because Optimise Bools needs to combine multiple conditions within an You could move them both earlier. I think as long as you've detected loops it should be ok. I'm not sure if it would interfere with some of the loop optimisations (hoisting, inverting etc). |
Egor you probably checked but indeed the lower compares are recognized as redundant, but the chain of adds blocks jump threading.
Either we need to be willing to destroy SSA or we need some way to update it by creating duplicate defs. After: we need to copy the PHI/ADD code in BB13 into BB11 and BB12. The PHI resolves to either V9.1 or V9.2 depending on pred, but we now need two defs for V6, and we need to add a PHI for V6 down in BB16, and update that use to the PHI def. Currently we know that V6.4 has one use, but we don't know where it is, and we don't know all the places we might need to add phis. |
We could I suppose have RBO just build up a list of blocks where jump threading was blocked like this, and remember how to reroute each pred, and then at the end of the optimizer phases, run through that list... provided the flow structure still matches we could then just duplicate IR and rewire flow. It might be a bit fragile though. Or if we moved RBO to the end like we've been envisioning if/when we can pay for opt repeat, it can just do this as a second action. |
I was expecting multiple
Math.Min
calls with the same Int32 arguments to be elided via CSE, but that's not happening.SharpLab
resulting in:
It's not a big deal, but the actual case where I noticed this came from the regex source generator, where it was emitting code like this:
in order to search a span but only up to a specific length; if it doesn't find what it's looking for within that length, it needs to update the position to the end of what it did examine. It's doing so with multiple
Math.Min(span.Length, int32limit)
calls, and each is resulting in the relevant comparisons rather than being CSE'd, even though the inputs provably don't change between calls.The text was updated successfully, but these errors were encountered: