Skip to content
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][interp] Improve precision of native offset estimation #103956

Merged
merged 1 commit into from
Jun 26, 2024

Conversation

BrzVlad
Copy link
Member

@BrzVlad BrzVlad commented Jun 25, 2024

The interpreter had always had problems with emitting branches, because the interpreter IR is actually executable. So when we generate branches we don't know if the branch will be long or short. A fix for this was to always generate long branches, and before the final codegen we compute conservative native offset estimates for instructions / bblocks (which are always larger that what would end up in the final code). Based on these, we determine if some branches can only be short, in which case, during codegen we emit a short branch.

The problem arises with superinstructions doing branches which only have support for short branches. Before this pass, we need to compute the native offset estimates, in order to know if it is safe to optimize with a branch superins. The problem is that, it is hard to maintain the constraint for the native offset estimates if we are computing them so early in the optimization process. This commit fixes the estimate to account for additional moves that can be inserted by the var offset allocator. We still don't account for instructions inserted by some bblock optimizations (it is not clear if these are hit in practice).

This commit is a conservative fix, addressing an existing precision issue with the computation, but a better long term fix would probably be to either deconstruct the super instruction if we detect it needs to do a long branch during final emit or to just move super instruction generation to a very late stage, after all optimizations and codgen happened, as a peephole optimization, where we can be certain whether a certain branch will be short or long.

Fixes #100753

The interpreter had always had problems with emitting branches, because the interpreter IR is actually executable. So when we generate branches we don't know if the branch will be long or short. A fix for this was to always generate long branches, and before the final codegen we compute conservative native offset estimates for instructions / bblocks (which are always larger that what would end up in the final code). Based on these, we determine if some branches can only be short, in which case, during codegen we emit a short branch.

The problem arises with superinstructions doing branches which only have support for short branches. Before this pass, we need to compute the native offset estimates, in order to know if it is safe to optimize with a branch superins. The problem is that, it is hard to maintain the constraint for the native offset estimates if we are computing them so early in the optimization process. This commit fixes the estimate to account for additional moves that can be inserted by the var offset allocator. We still don't account for instructions inserted by some bblock optimizations (it is not clear if these are hit in practice).

This commit is a conservative fix, addressing an existing precision issue with the computation, but a better long term fix would probably be to either deconstruct the super instruction if we detect it needs to do a long branch during final emit or to just move super instruction generation to a very late stage, after all optimizations and codgen happened, as a peephole optimization, where we can be certain whether a certain branch will be short or long.
Copy link
Contributor

Tagging subscribers to this area: @BrzVlad, @kotlarmilos
See info in area-owners.md if you want to be subscribed.

// might end up inserting additional moves for the arguments
int *call_args = ins->info.call_info->call_args;
while (*call_args != -1) {
noe += 4; // mono_interp_oplen [MINT_MOV_VT];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it always a single MINT_MOV_VT per argument?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is either a scalar mov (size 3) or a vt move (which also has size embedded so it is size 4). We are being conservative here since it is not worth checking the signature.

@BrzVlad BrzVlad merged commit f88ba03 into dotnet:main Jun 26, 2024
75 of 79 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Jul 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
2 participants