-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
[mono] Use bitshift instead of multiply in Unsafe.Add intrinsic if possible #103775
Conversation
Tagging subscribers to this area: @BrzVlad, @kotlarmilos |
Where were we converting the multiplication to a shift before ? |
I'm not sure precisely, but I found code in transform.c that does it. I can't explain the regression otherwise. |
Where is this actual code, since I'm not aware of it ? |
You're right and i misread, I was looking at the optimizer that converts integer DIV with power of two divisors into shifts. I can't find anything that converts muls, so it makes no sense that this regressed. |
Should we land the revert and verify that it fixes the regression just to rule out other possibilities? It appears to have also regressed IndexOf chars but not bytes or strings. |
Yeah, I think that's reasonable, since my theory was wrong. |
interp_add_ins (td, power2 > 0 ? MINT_SHL_P_IMM : MINT_MUL_P_IMM); | ||
td->last_ins->data [0] = (gint16)(power2 > 0 ? power2 : esize); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't this just be a general optimization done against MINT_MUL_P_IMM
rather than one done specifically here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optimizations like that will only happen when the method is being optimized, so it makes the intrinsic lower value to punt it to the superoptimizer pass. We could add it to the superoptimizer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is one of those "we should always do this, even if we aren't optimizing" type transforms.
That is, in RyuJIT we have different knobs that determine what type of optimizations can be done and there's some simple optimizations we do always regardless.
In particular we have a general OptimizationDisabled
which returns true if we are in Tier 0
or we are marked as needing to produce debuggable code (such as if a debugger is attached). This (and its logical inverse OptimizationEnabled
) are used in most code to determine if we should optimize or not.
However, we also recognize that there's places where it is fundamentally beneficial to perform basic transforms regardless. For example, in gtFoldExpr
rather than checking OptimizationDisabled
we instead manually check if we need to produce debuggable code or if we are marked as JIT_FLAG_MIN_OPT
, which is typically only set if the first compilation pass hit some internal limitation and failed to produce code (so it disables any and all optimizations as a fail safe so things still work, even if slow): https://github.com/dotnet/runtime/blob/main/src/coreclr/jit/gentree.cpp#L13487-L13501 -- This is called unconditionally from places like import: https://github.com/dotnet/runtime/blob/main/src/coreclr/jit/importer.cpp#L7206
This same general consideration also applies to more general optimizations, such as in Morph
(https://github.com/dotnet/runtime/blob/main/src/coreclr/jit/morph.cpp#L13104), where we then skip additional constant folding if optimizations are disabled (https://github.com/dotnet/runtime/blob/main/src/coreclr/jit/morph.cpp#L8288-L8292, unlike import which did this same bit unconditionally), but we do some other core optimizations such as replacing multiplication with shift regardless of optimization level: https://github.com/dotnet/runtime/blob/main/src/coreclr/jit/morph.cpp#L10456-L10520
This also allows code to do basic optimizations such as dead code elimination where trivially possible, leading to better throughput and handling, as we can differentiate between the levels of optimization and whether we need to be debuggable or not.
I imagine that Mono (JIT, LLVM, and Interpreter) would benefit from the same type of considerations and would see better perf and throughput if it mirrored the general premise
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Vlad can correct me on this, but iirc we don't do any dead code elimination in non-optimized mode right now. I've been thinking recently that it might be beneficial to always do DCE. That would require some amount of constant folding or similar analysis though, I think. We have some methods in the BCL that are full of dead code, and we burn cycles processing all that dead code during codegen for unoptimized methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would require some amount of constant folding or similar analysis though
Agreed. That's exactly why we do the basic constant folding during import in RyuJIT (only skipping them if we're in the true minopts mode or have a debugger attached) and then similarly do basic DCE based on this as well under similar circumstances.
Previously the normal flow would have been to inline the Unsafe.Add method, and the interp optimizer could turn the multiply into a bitshift. The new intrinsic wouldn't do that, so while it would improve performance for untiered methods and slightly reduce startup time, it would potentially regress performance for tiered methods. This should fix that.
Alternative to #103774