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

[wasm] Use libc directly for math in the jiterpreter, implement more opcodes #82963

Merged
merged 1 commit into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 0 additions & 81 deletions src/mono/mono/mini/interp/jiterpreter.c
Original file line number Diff line number Diff line change
Expand Up @@ -1193,87 +1193,6 @@ mono_jiterp_debug_count ()
return mono_debug_count();
}

EMSCRIPTEN_KEEPALIVE double
mono_jiterp_math_rem (double lhs, double rhs) {
return fmod(lhs, rhs);
}

EMSCRIPTEN_KEEPALIVE double
mono_jiterp_math_atan2 (double lhs, double rhs) {
return atan2(lhs, rhs);
}

EMSCRIPTEN_KEEPALIVE double
mono_jiterp_math_pow (double lhs, double rhs) {
return pow(lhs, rhs);
}

EMSCRIPTEN_KEEPALIVE double
mono_jiterp_math_acos (double value)
{
return acos(value);
}

EMSCRIPTEN_KEEPALIVE double
mono_jiterp_math_cos (double value)
{
return cos(value);
}

EMSCRIPTEN_KEEPALIVE double
mono_jiterp_math_asin (double value)
{
return asin(value);
}

EMSCRIPTEN_KEEPALIVE double
mono_jiterp_math_sin (double value)
{
return sin(value);
}

EMSCRIPTEN_KEEPALIVE double
mono_jiterp_math_atan (double value)
{
return atan(value);
}

EMSCRIPTEN_KEEPALIVE double
mono_jiterp_math_tan (double value)
{
return tan(value);
}

EMSCRIPTEN_KEEPALIVE double
mono_jiterp_math_exp (double value)
{
return exp(value);
}

EMSCRIPTEN_KEEPALIVE double
mono_jiterp_math_log (double value)
{
return log(value);
}

EMSCRIPTEN_KEEPALIVE double
mono_jiterp_math_log2 (double value)
{
return log2(value);
}

EMSCRIPTEN_KEEPALIVE double
mono_jiterp_math_log10 (double value)
{
return log10(value);
}

EMSCRIPTEN_KEEPALIVE double
mono_jiterp_math_fma (double x, double y, double z)
{
return fma(x, y, z);
}

EMSCRIPTEN_KEEPALIVE int
mono_jiterp_stelem_ref (
MonoArray *o, gint32 aindex, MonoObject *ref
Expand Down
56 changes: 23 additions & 33 deletions src/mono/wasm/runtime/jiterpreter-trace-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1062,19 +1062,11 @@ export function generate_wasm_body (

// LOCAL_VAR (ip [1], double) = fma (LOCAL_VAR (ip [2], double), LOCAL_VAR (ip [3], double), LOCAL_VAR (ip [4], double));
append_ldloc(builder, getArgU16(ip, 2), loadOp);
if (isF32)
builder.appendU8(WasmOpcode.f64_promote_f32);
append_ldloc(builder, getArgU16(ip, 3), loadOp);
if (isF32)
builder.appendU8(WasmOpcode.f64_promote_f32);
append_ldloc(builder, getArgU16(ip, 4), loadOp);
if (isF32)
builder.appendU8(WasmOpcode.f64_promote_f32);

builder.callImport("fma");
builder.callImport(isF32 ? "fmaf" : "fma");

if (isF32)
builder.appendU8(WasmOpcode.f32_demote_f64);
append_stloc_tail(builder, getArgU16(ip, 1), storeOp);
break;
}
Expand Down Expand Up @@ -2584,37 +2576,45 @@ const mathIntrinsicTable : { [opcode: number] : [isUnary: boolean, isF32: boolea
[MintOpcode.MINT_ABSF]: [true, true, WasmOpcode.f32_abs],

[MintOpcode.MINT_ACOS]: [true, false, "acos"],
[MintOpcode.MINT_ACOSF]: [true, true, "acos"],
[MintOpcode.MINT_ACOSF]: [true, true, "acosf"],
[MintOpcode.MINT_ACOSH]: [true, false, "acosh"],
[MintOpcode.MINT_ACOSHF]: [true, true, "acoshf"],
[MintOpcode.MINT_COS]: [true, false, "cos"],
[MintOpcode.MINT_COSF]: [true, true, "cos"],
[MintOpcode.MINT_COSF]: [true, true, "cosf"],
[MintOpcode.MINT_ASIN]: [true, false, "asin"],
[MintOpcode.MINT_ASINF]: [true, true, "asin"],
[MintOpcode.MINT_ASINF]: [true, true, "asinf"],
[MintOpcode.MINT_ASINH]: [true, false, "asinh"],
[MintOpcode.MINT_ASINHF]: [true, true, "asinhf"],
[MintOpcode.MINT_SIN]: [true, false, "sin"],
[MintOpcode.MINT_SINF]: [true, true, "sin"],
[MintOpcode.MINT_SINF]: [true, true, "sinf"],
[MintOpcode.MINT_ATAN]: [true, false, "atan"],
[MintOpcode.MINT_ATANF]: [true, true, "atan"],
[MintOpcode.MINT_ATANF]: [true, true, "atanf"],
[MintOpcode.MINT_ATANH]: [true, false, "atanh"],
[MintOpcode.MINT_ATANHF]: [true, true, "atanhf"],
[MintOpcode.MINT_TAN]: [true, false, "tan"],
[MintOpcode.MINT_TANF]: [true, true, "tan"],
[MintOpcode.MINT_TANF]: [true, true, "tanf"],
[MintOpcode.MINT_CBRT]: [true, false, "cbrt"],
[MintOpcode.MINT_CBRTF]: [true, true, "cbrtf"],
[MintOpcode.MINT_EXP]: [true, false, "exp"],
[MintOpcode.MINT_EXPF]: [true, true, "exp"],
[MintOpcode.MINT_EXPF]: [true, true, "expf"],
[MintOpcode.MINT_LOG]: [true, false, "log"],
[MintOpcode.MINT_LOGF]: [true, true, "log"],
[MintOpcode.MINT_LOGF]: [true, true, "logf"],
[MintOpcode.MINT_LOG2]: [true, false, "log2"],
[MintOpcode.MINT_LOG2F]: [true, true, "log2"],
[MintOpcode.MINT_LOG2F]: [true, true, "log2f"],
[MintOpcode.MINT_LOG10]: [true, false, "log10"],
[MintOpcode.MINT_LOG10F]: [true, true, "log10"],
[MintOpcode.MINT_LOG10F]: [true, true, "log10f"],

[MintOpcode.MINT_MIN]: [false, false, WasmOpcode.f64_min],
[MintOpcode.MINT_MINF]: [false, true, WasmOpcode.f32_min],
[MintOpcode.MINT_MAX]: [false, false, WasmOpcode.f64_max],
[MintOpcode.MINT_MAXF]: [false, true, WasmOpcode.f32_max],

[MintOpcode.MINT_ATAN2]: [false, false, "atan2"],
[MintOpcode.MINT_ATAN2F]: [false, true, "atan2"],
[MintOpcode.MINT_ATAN2F]: [false, true, "atan2f"],
[MintOpcode.MINT_POW]: [false, false, "pow"],
[MintOpcode.MINT_POWF]: [false, true, "pow"],
[MintOpcode.MINT_REM_R4]: [false, true, "rem"],
[MintOpcode.MINT_REM_R8]: [false, false, "rem"],
[MintOpcode.MINT_POWF]: [false, true, "powf"],
[MintOpcode.MINT_REM_R8]: [false, false, "fmod"],
[MintOpcode.MINT_REM_R4]: [false, true, "fmodf"],
};

function emit_math_intrinsic (builder: WasmBuilder, ip: MintOpcodePtr, opcode: MintOpcode) : boolean {
Expand Down Expand Up @@ -2644,29 +2644,19 @@ function emit_math_intrinsic (builder: WasmBuilder, ip: MintOpcodePtr, opcode: M
if (wasmOp) {
builder.appendU8(wasmOp);
} else if (name) {
if (isF32)
builder.appendU8(WasmOpcode.f64_promote_f32);
builder.callImport(name);
if (isF32)
builder.appendU8(WasmOpcode.f32_demote_f64);
} else
throw new Error("internal error");
append_stloc_tail(builder, destOffset, isF32 ? WasmOpcode.f32_store : WasmOpcode.f64_store);
return true;
} else {
append_ldloc(builder, srcOffset, isF32 ? WasmOpcode.f32_load : WasmOpcode.f64_load);
if (isF32 && name)
builder.appendU8(WasmOpcode.f64_promote_f32);
append_ldloc(builder, rhsOffset, isF32 ? WasmOpcode.f32_load : WasmOpcode.f64_load);
if (isF32 && name)
builder.appendU8(WasmOpcode.f64_promote_f32);

if (wasmOp) {
builder.appendU8(wasmOp);
} else if (name) {
builder.callImport(name);
if (isF32)
builder.appendU8(WasmOpcode.f32_demote_f64);
} else
throw new Error("internal error");

Expand Down
102 changes: 75 additions & 27 deletions src/mono/wasm/runtime/jiterpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,24 +224,51 @@ export let traceImports : Array<[string, string, Function]> | undefined;

export let _wrap_trace_function: Function;

const mathOps1 = [
"asin",
"acos",
"atan",
"cos",
"exp",
"log",
"log2",
"log10",
"sin",
"tan",
];

const mathOps2 = [
"rem",
"atan2",
"pow",
];
const mathOps1d = [
"asin",
"acos",
"atan",
"asinh",
"acosh",
"atanh",
"cos",
"sin",
"tan",
"cosh",
"sinh",
"tanh",
"exp",
"log",
"log2",
"log10",
"cbrt",
], mathOps2d = [
"fmod",
"atan2",
"pow",
], mathOps1f = [
"asinf",
"acosf",
"atanf",
"asinhf",
"acoshf",
"atanhf",
"cosf",
"sinf",
"tanf",
"coshf",
"sinhf",
"tanhf",
"expf",
"logf",
"log2f",
"log10f",
"cbrtf",
], mathOps2f = [
"fmodf",
"atan2f",
"powf",
];

function getTraceImports () {
if (traceImports)
Expand Down Expand Up @@ -277,7 +304,8 @@ function getTraceImports () {
importDef("cmpxchg_i32", getRawCwrap("mono_jiterp_cas_i32")),
importDef("cmpxchg_i64", getRawCwrap("mono_jiterp_cas_i64")),
importDef("stelem_ref", getRawCwrap("mono_jiterp_stelem_ref")),
importDef("fma", getRawCwrap("mono_jiterp_math_fma")),
importDef("fma", getRawCwrap("fma")),
importDef("fmaf", getRawCwrap("fmaf")),
];

if (instrumentedMethodNames.length > 0) {
Expand All @@ -288,15 +316,17 @@ function getTraceImports () {
if (nullCheckValidation)
traceImports.push(importDef("notnull", assert_not_null));

for (let i = 0; i < mathOps1.length; i++) {
const mop = mathOps1[i];
traceImports.push([mop, "mathop_d_d", getRawCwrap("mono_jiterp_math_" + mop)]);
}
const pushMathOps = (list: string[], type: string) => {
for (let i = 0; i < list.length; i++) {
const mop = list[i];
traceImports!.push([mop, type, getRawCwrap(mop)]);
}
};

for (let i = 0; i < mathOps2.length; i++) {
const mop = mathOps2[i];
traceImports.push([mop, "mathop_dd_d", getRawCwrap("mono_jiterp_math_" + mop)]);
}
pushMathOps(mathOps1f, "mathop_f_f");
pushMathOps(mathOps2f, "mathop_ff_f");
pushMathOps(mathOps1d, "mathop_d_d");
pushMathOps(mathOps2d, "mathop_dd_d");

return traceImports;
}
Expand Down Expand Up @@ -411,6 +441,24 @@ function initialize_builder (builder: WasmBuilder) {
"rhs": WasmValtype.f64,
}, WasmValtype.f64, true
);
builder.defineType(
"mathop_f_f", {
"value": WasmValtype.f32,
}, WasmValtype.f32, true
);
builder.defineType(
"mathop_ff_f", {
"lhs": WasmValtype.f32,
"rhs": WasmValtype.f32,
}, WasmValtype.f32, true
);
builder.defineType(
"fmaf", {
"x": WasmValtype.f32,
"y": WasmValtype.f32,
"z": WasmValtype.f32,
}, WasmValtype.f32, true
);
builder.defineType(
"fma", {
"x": WasmValtype.f64,
Expand Down
Loading