Skip to content

Commit 52381a1

Browse files
authored
[wasm] Jiterpreter opcode updates + implement safepoints (#80479)
* Implement CPOBJ_VT_NOREF * Implement STOBJ_VT_NOREF and improve bailout statistics * Support performing safepoints in jiterpreter traces without bailing out
1 parent edadf5f commit 52381a1

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8665,4 +8665,11 @@ mono_jiterp_interp_entry (JiterpEntryData *_data, stackval *sp_args, void *res)
86658665
mono_jiterp_stackval_to_data (type, frame.stack, res);
86668666
}
86678667

8668+
EMSCRIPTEN_KEEPALIVE void
8669+
mono_jiterp_auto_safepoint (InterpFrame *frame, guint16 *ip)
8670+
{
8671+
if (G_UNLIKELY (mono_polling_required))
8672+
do_safepoint (frame, get_context(), ip);
8673+
}
8674+
86688675
#endif

src/mono/mono/mini/interp/jiterpreter.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,8 @@ jiterp_should_abort_trace (InterpInst *ins, gboolean *inside_branch_block)
660660
case MINT_CPOBJ_VT:
661661
case MINT_LDOBJ_VT:
662662
case MINT_STOBJ_VT:
663+
case MINT_STOBJ_VT_NOREF:
664+
case MINT_CPOBJ_VT_NOREF:
663665
case MINT_STRLEN:
664666
case MINT_GETCHR:
665667
case MINT_GETITEM_SPAN:
@@ -680,6 +682,7 @@ jiterp_should_abort_trace (InterpInst *ins, gboolean *inside_branch_block)
680682
case MINT_NEWOBJ_VT_INLINED:
681683
case MINT_LD_DELEGATE_METHOD_PTR:
682684
case MINT_LDTSFLDA:
685+
case MINT_SAFEPOINT:
683686
return TRACE_CONTINUE;
684687

685688
case MINT_BR:
@@ -714,7 +717,6 @@ jiterp_should_abort_trace (InterpInst *ins, gboolean *inside_branch_block)
714717
case MINT_RETHROW:
715718
case MINT_PROF_EXIT:
716719
case MINT_PROF_EXIT_VOID:
717-
case MINT_SAFEPOINT:
718720
return TRACE_ABORT;
719721

720722
case MINT_MOV_SRC_OFF:

src/mono/mono/mini/interp/jiterpreter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ typedef struct {
116116
JiterpEntryDataCache cache;
117117
} JiterpEntryData;
118118

119+
void
120+
mono_jiterp_auto_safepoint (InterpFrame *frame, guint16 *ip);
121+
119122
void
120123
mono_jiterp_interp_entry (JiterpEntryData *_data, stackval *sp_args, void *res);
121124

src/mono/wasm/runtime/jiterpreter.ts

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ function getTraceImports () {
251251
["ldtsflda", "ldtsflda", getRawCwrap("mono_jiterp_ldtsflda")],
252252
["conv_ovf", "conv_ovf", getRawCwrap("mono_jiterp_conv_ovf")],
253253
["relop_fp", "relop_fp", getRawCwrap("mono_jiterp_relop_fp")],
254+
["safepoint", "safepoint", getRawCwrap("mono_jiterp_auto_safepoint")],
254255
];
255256

256257
if (instrumentedMethodNames.length > 0) {
@@ -504,6 +505,12 @@ function generate_wasm (
504505
"opcode": WasmValtype.i32,
505506
}, WasmValtype.i32
506507
);
508+
builder.defineType(
509+
"safepoint", {
510+
"frame": WasmValtype.i32,
511+
"ip": WasmValtype.i32,
512+
}, WasmValtype.void
513+
);
507514

508515
builder.generateTypeSection();
509516

@@ -887,6 +894,10 @@ function generate_wasm_body (
887894
is_dead_opcode = true;
888895
break;
889896

897+
case MintOpcode.MINT_SAFEPOINT:
898+
append_safepoint(builder, ip);
899+
break;
900+
890901
case MintOpcode.MINT_LDLOCA_S:
891902
// Pre-load locals for the store op
892903
builder.local("pLocals");
@@ -917,6 +928,13 @@ function generate_wasm_body (
917928
builder.callImport("value_copy");
918929
break;
919930
}
931+
case MintOpcode.MINT_CPOBJ_VT_NOREF: {
932+
const sizeBytes = getArgU16(ip, 3);
933+
append_ldloc(builder, getArgU16(ip, 1), WasmOpcode.i32_load);
934+
append_ldloc(builder, getArgU16(ip, 2), WasmOpcode.i32_load);
935+
append_memmove_dest_src(builder, sizeBytes);
936+
break;
937+
}
920938
case MintOpcode.MINT_LDOBJ_VT: {
921939
const size = getArgU16(ip, 3);
922940
append_ldloca(builder, getArgU16(ip, 1));
@@ -932,6 +950,13 @@ function generate_wasm_body (
932950
builder.callImport("value_copy");
933951
break;
934952
}
953+
case MintOpcode.MINT_STOBJ_VT_NOREF: {
954+
const sizeBytes = getArgU16(ip, 3);
955+
append_ldloc(builder, getArgU16(ip, 1), WasmOpcode.i32_load);
956+
append_ldloca(builder, getArgU16(ip, 2));
957+
append_memmove_dest_src(builder, sizeBytes);
958+
break;
959+
}
935960

936961
case MintOpcode.MINT_STRLEN:
937962
builder.block();
@@ -2368,16 +2393,15 @@ function emit_branch (
23682393
builder.appendU8(WasmOpcode.br_if);
23692394
builder.appendULeb(0);
23702395

2371-
if (isSafepoint) {
2372-
// We set the high bit on our relative displacement so that the interpreter knows
2373-
// it needs to perform a safepoint after the trace exits
2374-
append_bailout(builder, destination, BailoutReason.SafepointBranchTaken, true);
2375-
} else if (displacement < 0) {
2396+
if (displacement < 0) {
23762397
// This is a backwards branch, and right now we always bail out for those -
23772398
// so just return.
23782399
// FIXME: Why is this not a safepoint?
23792400
append_bailout(builder, destination, BailoutReason.BackwardBranch, true);
23802401
} else {
2402+
// Do a safepoint *before* changing our IP, if necessary
2403+
if (isSafepoint)
2404+
append_safepoint(builder, ip);
23812405
// Branching is enabled, so set eip and exit the current branch block
23822406
builder.branchTargets.add(destination);
23832407
builder.ip_const(destination);
@@ -2925,6 +2949,13 @@ function append_bailout (builder: WasmBuilder, ip: MintOpcodePtr, reason: Bailou
29252949
builder.appendU8(WasmOpcode.return_);
29262950
}
29272951

2952+
function append_safepoint (builder: WasmBuilder, ip: MintOpcodePtr) {
2953+
builder.local("frame");
2954+
// Not ip_const, because we can't pass relative IP to do_safepoint
2955+
builder.i32_const(ip);
2956+
builder.callImport("safepoint");
2957+
}
2958+
29282959
const JITERPRETER_TRAINING = 0;
29292960
const JITERPRETER_NOT_JITTED = 1;
29302961
let mostRecentOptions : JiterpreterOptions | undefined = undefined;

0 commit comments

Comments
 (0)