Skip to content

Commit f88ba03

Browse files
authored
[mono][interp] Improve precision of native offset estimation (#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.
1 parent 5c53e8e commit f88ba03

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

src/mono/mono/mini/interp/transform-opt.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -3387,7 +3387,7 @@ can_propagate_var_def (TransformData *td, int var, InterpLivenessPosition cur_li
33873387
static void
33883388
interp_super_instructions (TransformData *td)
33893389
{
3390-
interp_compute_native_offset_estimates (td);
3390+
interp_compute_native_offset_estimates (td, FALSE);
33913391

33923392
// Add some actual super instructions
33933393
for (int bb_dfs_index = 0; bb_dfs_index < td->bblocks_count_eh; bb_dfs_index++) {

src/mono/mono/mini/interp/transform.c

+22-2
Original file line numberDiff line numberDiff line change
@@ -8791,12 +8791,18 @@ interp_foreach_ins_var (TransformData *td, InterpInst *ins, gpointer data, void
87918791
}
87928792

87938793
int
8794-
interp_compute_native_offset_estimates (TransformData *td)
8794+
interp_compute_native_offset_estimates (TransformData *td, gboolean final_code)
87958795
{
87968796
InterpBasicBlock *bb;
87978797
int noe = 0;
8798+
87988799
for (bb = td->entry_bb; bb != NULL; bb = bb->next_bb) {
87998800
InterpInst *ins;
8801+
// FIXME This doesn't currently hold because of bblock reordering potentially
8802+
// inserting additional instructions after the estimate is computed.
8803+
//
8804+
// if (bb->native_offset_estimate)
8805+
// g_assert (bb->native_offset_estimate >= noe);
88008806
bb->native_offset_estimate = noe;
88018807
if (!td->optimized && bb->patchpoint_bb)
88028808
noe += 2;
@@ -8815,6 +8821,20 @@ interp_compute_native_offset_estimates (TransformData *td)
88158821
if (MINT_IS_EMIT_NOP (opcode))
88168822
continue;
88178823
noe += interp_get_ins_length (ins);
8824+
8825+
if (!final_code && td->optimized &&
8826+
(ins->flags & INTERP_INST_FLAG_CALL) &&
8827+
ins->info.call_info &&
8828+
ins->info.call_info->call_args) {
8829+
// When code is optimized, for a call, the offset allocator
8830+
// might end up inserting additional moves for the arguments
8831+
int *call_args = ins->info.call_info->call_args;
8832+
while (*call_args != -1) {
8833+
noe += 4; // mono_interp_oplen [MINT_MOV_VT];
8834+
call_args++;
8835+
}
8836+
}
8837+
88188838
if (!td->optimized)
88198839
interp_foreach_ins_var (td, ins, NULL, alloc_unopt_global_local);
88208840
}
@@ -9255,7 +9275,7 @@ generate_compacted_code (InterpMethod *rtm, TransformData *td)
92559275

92569276
// This iteration could be avoided at the cost of less precise size result, following
92579277
// super instruction pass
9258-
size = interp_compute_native_offset_estimates (td);
9278+
size = interp_compute_native_offset_estimates (td, TRUE);
92599279

92609280
// Generate the compacted stream of instructions
92619281
td->new_code = ip = (guint16*)imethod_alloc0 (td, size * sizeof (guint16));

src/mono/mono/mini/interp/transform.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ void
522522
interp_link_bblocks (TransformData *td, InterpBasicBlock *from, InterpBasicBlock *to);
523523

524524
int
525-
interp_compute_native_offset_estimates (TransformData *td);
525+
interp_compute_native_offset_estimates (TransformData *td, gboolean final_code);
526526

527527
void
528528
interp_optimize_code (TransformData *td);

0 commit comments

Comments
 (0)