Skip to content

Commit

Permalink
[mono][interp] Improve precision of native offset estimation (dotnet#…
Browse files Browse the repository at this point in the history
…103956)

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.
  • Loading branch information
BrzVlad committed Jun 26, 2024
1 parent 5c53e8e commit f88ba03
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/mono/mono/mini/interp/transform-opt.c
Original file line number Diff line number Diff line change
Expand Up @@ -3387,7 +3387,7 @@ can_propagate_var_def (TransformData *td, int var, InterpLivenessPosition cur_li
static void
interp_super_instructions (TransformData *td)
{
interp_compute_native_offset_estimates (td);
interp_compute_native_offset_estimates (td, FALSE);

// Add some actual super instructions
for (int bb_dfs_index = 0; bb_dfs_index < td->bblocks_count_eh; bb_dfs_index++) {
Expand Down
24 changes: 22 additions & 2 deletions src/mono/mono/mini/interp/transform.c
Original file line number Diff line number Diff line change
Expand Up @@ -8791,12 +8791,18 @@ interp_foreach_ins_var (TransformData *td, InterpInst *ins, gpointer data, void
}

int
interp_compute_native_offset_estimates (TransformData *td)
interp_compute_native_offset_estimates (TransformData *td, gboolean final_code)
{
InterpBasicBlock *bb;
int noe = 0;

for (bb = td->entry_bb; bb != NULL; bb = bb->next_bb) {
InterpInst *ins;
// FIXME This doesn't currently hold because of bblock reordering potentially
// inserting additional instructions after the estimate is computed.
//
// if (bb->native_offset_estimate)
// g_assert (bb->native_offset_estimate >= noe);
bb->native_offset_estimate = noe;
if (!td->optimized && bb->patchpoint_bb)
noe += 2;
Expand All @@ -8815,6 +8821,20 @@ interp_compute_native_offset_estimates (TransformData *td)
if (MINT_IS_EMIT_NOP (opcode))
continue;
noe += interp_get_ins_length (ins);

if (!final_code && td->optimized &&
(ins->flags & INTERP_INST_FLAG_CALL) &&
ins->info.call_info &&
ins->info.call_info->call_args) {
// When code is optimized, for a call, the offset allocator
// 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];
call_args++;
}
}

if (!td->optimized)
interp_foreach_ins_var (td, ins, NULL, alloc_unopt_global_local);
}
Expand Down Expand Up @@ -9255,7 +9275,7 @@ generate_compacted_code (InterpMethod *rtm, TransformData *td)

// This iteration could be avoided at the cost of less precise size result, following
// super instruction pass
size = interp_compute_native_offset_estimates (td);
size = interp_compute_native_offset_estimates (td, TRUE);

// Generate the compacted stream of instructions
td->new_code = ip = (guint16*)imethod_alloc0 (td, size * sizeof (guint16));
Expand Down
2 changes: 1 addition & 1 deletion src/mono/mono/mini/interp/transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ void
interp_link_bblocks (TransformData *td, InterpBasicBlock *from, InterpBasicBlock *to);

int
interp_compute_native_offset_estimates (TransformData *td);
interp_compute_native_offset_estimates (TransformData *td, gboolean final_code);

void
interp_optimize_code (TransformData *td);
Expand Down

0 comments on commit f88ba03

Please sign in to comment.