-
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 inTier 0
or we are marked as needing to produce debuggable code (such as if a debugger is attached). This (and its logical inverseOptimizationEnabled
) 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 checkingOptimizationDisabled
we instead manually check if we need to produce debuggable code or if we are marked asJIT_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#L7206This 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-L10520This 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.
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.