From 67e57689459fec7723e6dfe53171584a7862f1ba Mon Sep 17 00:00:00 2001 From: Katelyn Gadd Date: Mon, 9 Sep 2024 06:19:10 -0700 Subject: [PATCH] [wasm] Implement MINT_NEWARR in jiterpreter (#107430) --- .../runtime/jiterpreter-trace-generator.ts | 15 +++++++++++++++ src/mono/browser/runtime/jiterpreter.ts | 10 ++++++++++ .../mono/mini/interp/jiterpreter-opcode-values.h | 1 + src/mono/mono/mini/interp/jiterpreter.c | 12 ++++++++++++ 4 files changed, 38 insertions(+) diff --git a/src/mono/browser/runtime/jiterpreter-trace-generator.ts b/src/mono/browser/runtime/jiterpreter-trace-generator.ts index c50647835c2c40..8d88b2ff98e426 100644 --- a/src/mono/browser/runtime/jiterpreter-trace-generator.ts +++ b/src/mono/browser/runtime/jiterpreter-trace-generator.ts @@ -1280,6 +1280,21 @@ export function generateWasmBody ( break; } + case MintOpcode.MINT_NEWARR: { + builder.block(); + append_ldloca(builder, getArgU16(ip, 1), 4); + const vtable = get_imethod_data(frame, getArgU16(ip, 3)); + builder.i32_const(vtable); + append_ldloc(builder, getArgU16(ip, 2), WasmOpcode.i32_load); + builder.callImport("newarr"); + // If the newarr operation succeeded, continue, otherwise bailout + builder.appendU8(WasmOpcode.br_if); + builder.appendULeb(0); + append_bailout(builder, ip, BailoutReason.AllocFailed); + builder.endBlock(); + break; + } + case MintOpcode.MINT_NEWOBJ_INLINED: { builder.block(); // MonoObject *o = mono_gc_alloc_obj (vtable, m_class_get_instance_size (vtable->klass)); diff --git a/src/mono/browser/runtime/jiterpreter.ts b/src/mono/browser/runtime/jiterpreter.ts index f665d0cccc7dcf..7aab49e6bf2668 100644 --- a/src/mono/browser/runtime/jiterpreter.ts +++ b/src/mono/browser/runtime/jiterpreter.ts @@ -272,6 +272,7 @@ function getTraceImports () { ["ckovr_u4", "overflow_check_i4", getRawCwrap("mono_jiterp_overflow_check_u4")], importDef("newobj_i", getRawCwrap("mono_jiterp_try_newobj_inlined")), importDef("newstr", getRawCwrap("mono_jiterp_try_newstr")), + importDef("newarr", getRawCwrap("mono_jiterp_try_newarr")), importDef("ld_del_ptr", getRawCwrap("mono_jiterp_ld_delegate_method_ptr")), importDef("ldtsflda", getRawCwrap("mono_jiterp_ldtsflda")), importDef("conv", getRawCwrap("mono_jiterp_conv")), @@ -465,6 +466,15 @@ function initialize_builder (builder: WasmBuilder) { }, WasmValtype.i32, true ); + builder.defineType( + "newarr", + { + "ppDestination": WasmValtype.i32, + "vtable": WasmValtype.i32, + "length": WasmValtype.i32, + }, + WasmValtype.i32, true + ); builder.defineType( "localloc", { diff --git a/src/mono/mono/mini/interp/jiterpreter-opcode-values.h b/src/mono/mono/mini/interp/jiterpreter-opcode-values.h index a509b8471cfa95..d360bceb0fc32b 100644 --- a/src/mono/mono/mini/interp/jiterpreter-opcode-values.h +++ b/src/mono/mono/mini/interp/jiterpreter-opcode-values.h @@ -98,6 +98,7 @@ OP(MINT_BOX, NORMAL) OP(MINT_BOX_VT, NORMAL) OP(MINT_UNBOX, NORMAL) OP(MINT_NEWSTR, NORMAL) +OP(MINT_NEWARR, NORMAL) OP(MINT_LD_DELEGATE_METHOD_PTR, NORMAL) OP(MINT_LDTSFLDA, NORMAL) OP(MINT_ADD_MUL_I4_IMM, NORMAL) diff --git a/src/mono/mono/mini/interp/jiterpreter.c b/src/mono/mono/mini/interp/jiterpreter.c index 362f01a2390b20..b63ec1b284e48f 100644 --- a/src/mono/mono/mini/interp/jiterpreter.c +++ b/src/mono/mono/mini/interp/jiterpreter.c @@ -202,6 +202,18 @@ mono_jiterp_try_newstr (MonoString **destination, int length) { return *destination != 0; } +EMSCRIPTEN_KEEPALIVE int +mono_jiterp_try_newarr (MonoArray **destination, MonoVTable *vtable, int length) { + if (length < 0) + return 0; + ERROR_DECL(error); + *destination = mono_array_new_specific_checked (vtable, length, error); + if (!is_ok (error)) + *destination = 0; + mono_error_cleanup (error); // FIXME: do not swallow the error + return *destination != 0; +} + EMSCRIPTEN_KEEPALIVE int mono_jiterp_gettype_ref ( MonoObject **destination, MonoObject **source